SubRegIndex'ize Mips
[oota-llvm.git] / tools / edis / 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/EDInstInfo.h"
22 #include "llvm/MC/MCInst.h"
23
24 using namespace llvm;
25
26 EDInst::EDInst(llvm::MCInst *inst,
27                uint64_t byteSize, 
28                EDDisassembler &disassembler,
29                const llvm::EDInstInfo *info) :
30   Disassembler(disassembler),
31   Inst(inst),
32   ThisInstInfo(info),
33   ByteSize(byteSize),
34   BranchTarget(-1),
35   MoveSource(-1),
36   MoveTarget(-1) {
37   OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()];
38 }
39
40 EDInst::~EDInst() {
41   unsigned int index;
42   unsigned int numOperands = Operands.size();
43   
44   for (index = 0; index < numOperands; ++index)
45     delete Operands[index];
46   
47   unsigned int numTokens = Tokens.size();
48   
49   for (index = 0; index < numTokens; ++index)
50     delete Tokens[index];
51   
52   delete Inst;
53 }
54
55 uint64_t EDInst::byteSize() {
56   return ByteSize;
57 }
58
59 int EDInst::stringify() {
60   if (StringifyResult.valid())
61     return StringifyResult.result();
62   
63   if (Disassembler.printInst(String, *Inst))
64     return StringifyResult.setResult(-1);
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 
85       ThisInstInfo->instructionType == kInstructionTypeBranch ||
86       ThisInstInfo->instructionType == kInstructionTypeCall;
87   else
88     return false;
89 }
90
91 bool EDInst::isMove() {
92   if (ThisInstInfo)
93     return ThisInstInfo->instructionType == kInstructionTypeMove;
94   else
95     return false;
96 }
97
98 int EDInst::parseOperands() {
99   if (ParseResult.valid())
100     return ParseResult.result();
101   
102   if (!ThisInstInfo)
103     return ParseResult.setResult(-1);
104   
105   unsigned int opIndex;
106   unsigned int mcOpIndex = 0;
107   
108   for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
109     if (isBranch() &&
110         (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
111       BranchTarget = opIndex;
112     }
113     else if (isMove()) {
114       if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
115         MoveSource = opIndex;
116       else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
117         MoveTarget = opIndex;
118     }
119     
120     EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
121     
122     Operands.push_back(operand);
123   }
124   
125   return ParseResult.setResult(0);
126 }
127
128 int EDInst::branchTargetID() {
129   if (parseOperands())
130     return -1;
131   return BranchTarget;
132 }
133
134 int EDInst::moveSourceID() {
135   if (parseOperands())
136     return -1;
137   return MoveSource;
138 }
139
140 int EDInst::moveTargetID() {
141   if (parseOperands())
142     return -1;
143   return MoveTarget;
144 }
145
146 int EDInst::numOperands() {
147   if (parseOperands())
148     return -1;
149   return Operands.size();
150 }
151
152 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
153   if (parseOperands())
154     return -1;
155   
156   if (index >= Operands.size())
157     return -1;
158   
159   operand = Operands[index];
160   return 0;
161 }
162
163 int EDInst::tokenize() {
164   if (TokenizeResult.valid())
165     return TokenizeResult.result();
166   
167   if (stringify())
168     return TokenizeResult.setResult(-1);
169     
170   return TokenizeResult.setResult(EDToken::tokenize(Tokens,
171                                                     String,
172                                                     OperandOrder,
173                                                     Disassembler));
174     
175 }
176
177 int EDInst::numTokens() {
178   if (tokenize())
179     return -1;
180   return Tokens.size();
181 }
182
183 int EDInst::getToken(EDToken *&token, unsigned int index) {
184   if (tokenize())
185     return -1;
186   token = Tokens[index];
187   return 0;
188 }
189
190 #ifdef __BLOCKS__
191 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
192   if (tokenize())
193     return -1;
194   
195   tokvec_t::iterator iter;
196   
197   for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
198     int ret = visitor(*iter);
199     if (ret == 1)
200       return 0;
201     if (ret != 0)
202       return -1;
203   }
204   
205   return 0;
206 }
207 #endif