1 //===-EDInst.cpp - LLVM Enhanced Disassembler -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
17 #include "EDDisassembler.h"
18 #include "EDOperand.h"
20 #include "llvm/MC/EDInstInfo.h"
21 #include "llvm/MC/MCInst.h"
25 EDInst::EDInst(llvm::MCInst *inst,
27 EDDisassembler &disassembler,
28 const llvm::EDInstInfo *info) :
29 Disassembler(disassembler),
36 OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()];
41 unsigned int numOperands = Operands.size();
43 for (index = 0; index < numOperands; ++index)
44 delete Operands[index];
46 unsigned int numTokens = Tokens.size();
48 for (index = 0; index < numTokens; ++index)
54 uint64_t EDInst::byteSize() {
58 int EDInst::stringify() {
59 if (StringifyResult.valid())
60 return StringifyResult.result();
62 if (Disassembler.printInst(String, *Inst))
63 return StringifyResult.setResult(-1);
65 String.push_back('\n');
67 return StringifyResult.setResult(0);
70 int EDInst::getString(const char*& str) {
79 unsigned EDInst::instID() {
80 return Inst->getOpcode();
83 bool EDInst::isBranch() {
86 ThisInstInfo->instructionType == kInstructionTypeBranch ||
87 ThisInstInfo->instructionType == kInstructionTypeCall;
92 bool EDInst::isMove() {
94 return ThisInstInfo->instructionType == kInstructionTypeMove;
99 int EDInst::parseOperands() {
100 if (ParseResult.valid())
101 return ParseResult.result();
104 return ParseResult.setResult(-1);
106 unsigned int opIndex;
107 unsigned int mcOpIndex = 0;
109 for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
111 (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
112 BranchTarget = opIndex;
115 if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
116 MoveSource = opIndex;
117 else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
118 MoveTarget = opIndex;
121 EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
123 Operands.push_back(operand);
126 return ParseResult.setResult(0);
129 int EDInst::branchTargetID() {
135 int EDInst::moveSourceID() {
141 int EDInst::moveTargetID() {
147 int EDInst::numOperands() {
150 return Operands.size();
153 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
157 if (index >= Operands.size())
160 operand = Operands[index];
164 int EDInst::tokenize() {
165 if (TokenizeResult.valid())
166 return TokenizeResult.result();
168 if (ThisInstInfo == NULL)
169 return TokenizeResult.setResult(-1);
172 return TokenizeResult.setResult(-1);
174 return TokenizeResult.setResult(EDToken::tokenize(Tokens,
181 int EDInst::numTokens() {
184 return Tokens.size();
187 int EDInst::getToken(EDToken *&token, unsigned int index) {
190 token = Tokens[index];
195 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
199 tokvec_t::iterator iter;
201 for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
202 int ret = visitor(*iter);