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"
21 #include "llvm/MC/EDInstInfo.h"
22 #include "llvm/MC/MCInst.h"
26 EDInst::EDInst(llvm::MCInst *inst,
28 EDDisassembler &disassembler,
29 const llvm::EDInstInfo *info) :
30 Disassembler(disassembler),
37 OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()];
42 unsigned int numOperands = Operands.size();
44 for (index = 0; index < numOperands; ++index)
45 delete Operands[index];
47 unsigned int numTokens = Tokens.size();
49 for (index = 0; index < numTokens; ++index)
55 uint64_t EDInst::byteSize() {
59 int EDInst::stringify() {
60 if (StringifyResult.valid())
61 return StringifyResult.result();
63 if (Disassembler.printInst(String, *Inst))
64 return StringifyResult.setResult(-1);
66 String.push_back('\n');
68 return StringifyResult.setResult(0);
71 int EDInst::getString(const char*& str) {
80 unsigned EDInst::instID() {
81 return Inst->getOpcode();
84 bool EDInst::isBranch() {
87 ThisInstInfo->instructionType == kInstructionTypeBranch ||
88 ThisInstInfo->instructionType == kInstructionTypeCall;
93 bool EDInst::isMove() {
95 return ThisInstInfo->instructionType == kInstructionTypeMove;
100 int EDInst::parseOperands() {
101 if (ParseResult.valid())
102 return ParseResult.result();
105 return ParseResult.setResult(-1);
107 unsigned int opIndex;
108 unsigned int mcOpIndex = 0;
110 for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
112 (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
113 BranchTarget = opIndex;
116 if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
117 MoveSource = opIndex;
118 else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
119 MoveTarget = opIndex;
122 EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
124 Operands.push_back(operand);
127 return ParseResult.setResult(0);
130 int EDInst::branchTargetID() {
136 int EDInst::moveSourceID() {
142 int EDInst::moveTargetID() {
148 int EDInst::numOperands() {
151 return Operands.size();
154 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
158 if (index >= Operands.size())
161 operand = Operands[index];
165 int EDInst::tokenize() {
166 if (TokenizeResult.valid())
167 return TokenizeResult.result();
170 return TokenizeResult.setResult(-1);
172 return TokenizeResult.setResult(EDToken::tokenize(Tokens,
179 int EDInst::numTokens() {
182 return Tokens.size();
185 int EDInst::getToken(EDToken *&token, unsigned int index) {
188 token = Tokens[index];
193 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
197 tokvec_t::iterator iter;
199 for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
200 int ret = visitor(*iter);