Added the enhanced disassembly library's implementation and
[oota-llvm.git] / tools / ed / EDInst.cpp
1 //===-EDInst.cpp - LLVM Enhanced Disassembler -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Enhanced Disassembly library's instruction class.
11 // The instruction is responsible for vending the string representation, 
12 // individual tokens, and operands for a single instruction.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "EDDisassembler.h"
17 #include "EDInst.h"
18 #include "EDOperand.h"
19 #include "EDToken.h"
20
21 #include "llvm/MC/MCInst.h"
22
23 using namespace llvm;
24
25 EDInst::EDInst(llvm::MCInst *inst,
26                uint64_t byteSize, 
27                EDDisassembler &disassembler,
28                const InstInfo *info) :
29   Disassembler(disassembler),
30   Inst(inst),
31   ThisInstInfo(info),
32   ByteSize(byteSize),
33   BranchTarget(-1),
34   MoveSource(-1),
35   MoveTarget(-1) {
36 }
37
38 EDInst::~EDInst() {
39   unsigned int index;
40   unsigned int numOperands = Operands.size();
41   
42   for (index = 0; index < numOperands; ++index)
43     delete Operands[index];
44   
45   unsigned int numTokens = Tokens.size();
46   
47   for (index = 0; index < numTokens; ++index)
48     delete Tokens[index];
49   
50   delete Inst;
51 }
52
53 uint64_t EDInst::byteSize() {
54   return ByteSize;
55 }
56
57 int EDInst::stringify() {
58   if (StringifyResult.valid())
59     return StringifyResult.result();
60   
61   if (Disassembler.printInst(String, *Inst))
62     return StringifyResult.setResult(-1);
63
64   OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()];
65   
66   return StringifyResult.setResult(0);
67 }
68
69 int EDInst::getString(const char*& str) {
70   if (stringify())
71     return -1;
72   
73   str = String.c_str();
74   
75   return 0;
76 }
77
78 unsigned EDInst::instID() {
79   return Inst->getOpcode();
80 }
81
82 bool EDInst::isBranch() {
83   if (ThisInstInfo)
84     return ThisInstInfo->instructionFlags & kInstructionFlagBranch;
85   else
86     return false;
87 }
88
89 bool EDInst::isMove() {
90   if (ThisInstInfo)
91     return ThisInstInfo->instructionFlags & kInstructionFlagMove;
92   else
93     return false;
94 }
95
96 int EDInst::parseOperands() {
97   if (ParseResult.valid())
98     return ParseResult.result(); 
99   
100   if (!ThisInstInfo)
101     return ParseResult.setResult(-1);
102   
103   unsigned int opIndex;
104   unsigned int mcOpIndex = 0;
105   
106   for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
107     if (isBranch() &&
108         (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
109       BranchTarget = opIndex;
110     }
111     else if (isMove()) {
112       if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
113         MoveSource = opIndex;
114       else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
115         MoveTarget = opIndex;
116     }
117     
118     EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
119     
120     Operands.push_back(operand);
121   }
122   
123   return ParseResult.setResult(0);
124 }
125
126 int EDInst::branchTargetID() {
127   if (parseOperands())
128     return -1;
129   return BranchTarget;
130 }
131
132 int EDInst::moveSourceID() {
133   if (parseOperands())
134     return -1;
135   return MoveSource;
136 }
137
138 int EDInst::moveTargetID() {
139   if (parseOperands())
140     return -1;
141   return MoveTarget;
142 }
143
144 int EDInst::numOperands() {
145   if (parseOperands())
146     return -1;
147   return Operands.size();
148 }
149
150 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
151   if (parseOperands())
152     return -1;
153   
154   if (index >= Operands.size())
155     return -1;
156   
157   operand = Operands[index];
158   return 0;
159 }
160
161 int EDInst::tokenize() {
162   if (TokenizeResult.valid())
163     return TokenizeResult.result();
164   
165   if (stringify())
166     return TokenizeResult.setResult(-1);
167     
168   return TokenizeResult.setResult(EDToken::tokenize(Tokens,
169                                                     String,
170                                                     OperandOrder,
171                                                     Disassembler));
172     
173 }
174
175 int EDInst::numTokens() {
176   if (tokenize())
177     return -1;
178   return Tokens.size();
179 }
180
181 int EDInst::getToken(EDToken *&token, unsigned int index) {
182   if (tokenize())
183     return -1;
184   token = Tokens[index];
185   return 0;
186 }
187
188 #ifdef __BLOCKS__
189 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
190   if (tokenize())
191     return -1;
192   
193   tokvec_t::iterator iter;
194   
195   for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
196     int ret = visitor(*iter);
197     if (ret == 1)
198       return 0;
199     if (ret != 0)
200       return -1;
201   }
202   
203   return 0;
204 }
205 #endif