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 //===----------------------------------------------------------------------===//
16 #include "EDDisassembler.h"
18 #include "EDOperand.h"
21 #include "llvm/MC/MCInst.h"
25 EDInst::EDInst(llvm::MCInst *inst,
27 EDDisassembler &disassembler,
28 const InstInfo *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 return StringifyResult.setResult(0);
68 int EDInst::getString(const char*& str) {
77 unsigned EDInst::instID() {
78 return Inst->getOpcode();
81 bool EDInst::isBranch() {
83 return ThisInstInfo->instructionType == kInstructionTypeBranch;
88 bool EDInst::isMove() {
90 return ThisInstInfo->instructionType == kInstructionTypeMove;
95 int EDInst::parseOperands() {
96 if (ParseResult.valid())
97 return ParseResult.result();
100 return ParseResult.setResult(-1);
102 unsigned int opIndex;
103 unsigned int mcOpIndex = 0;
105 for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
107 (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
108 BranchTarget = opIndex;
111 if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
112 MoveSource = opIndex;
113 else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
114 MoveTarget = opIndex;
117 EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
119 Operands.push_back(operand);
122 return ParseResult.setResult(0);
125 int EDInst::branchTargetID() {
131 int EDInst::moveSourceID() {
137 int EDInst::moveTargetID() {
143 int EDInst::numOperands() {
146 return Operands.size();
149 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
153 if (index >= Operands.size())
156 operand = Operands[index];
160 int EDInst::tokenize() {
161 if (TokenizeResult.valid())
162 return TokenizeResult.result();
165 return TokenizeResult.setResult(-1);
167 return TokenizeResult.setResult(EDToken::tokenize(Tokens,
174 int EDInst::numTokens() {
177 return Tokens.size();
180 int EDInst::getToken(EDToken *&token, unsigned int index) {
183 token = Tokens[index];
188 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
192 tokvec_t::iterator iter;
194 for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
195 int ret = visitor(*iter);