Remove accidentally committed cruft.
[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 ThisInstInfo->instructionType == kInstructionTypeBranch;
85   else
86     return false;
87 }
88
89 bool EDInst::isMove() {
90   if (ThisInstInfo)
91     return ThisInstInfo->instructionType == kInstructionTypeMove;
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