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