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/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 return StringifyResult.setResult(0);
69 int EDInst::getString(const char*& str) {
78 unsigned EDInst::instID() {
79 return Inst->getOpcode();
82 bool EDInst::isBranch() {
84 return ThisInstInfo->instructionType == kInstructionTypeBranch;
89 bool EDInst::isMove() {
91 return ThisInstInfo->instructionType == kInstructionTypeMove;
96 int EDInst::parseOperands() {
97 if (ParseResult.valid())
98 return ParseResult.result();
101 return ParseResult.setResult(-1);
103 unsigned int opIndex;
104 unsigned int mcOpIndex = 0;
106 for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
108 (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
109 BranchTarget = opIndex;
112 if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
113 MoveSource = opIndex;
114 else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
115 MoveTarget = opIndex;
118 EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
120 Operands.push_back(operand);
123 return ParseResult.setResult(0);
126 int EDInst::branchTargetID() {
132 int EDInst::moveSourceID() {
138 int EDInst::moveTargetID() {
144 int EDInst::numOperands() {
147 return Operands.size();
150 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
154 if (index >= Operands.size())
157 operand = Operands[index];
161 int EDInst::tokenize() {
162 if (TokenizeResult.valid())
163 return TokenizeResult.result();
166 return TokenizeResult.setResult(-1);
168 return TokenizeResult.setResult(EDToken::tokenize(Tokens,
175 int EDInst::numTokens() {
178 return Tokens.size();
181 int EDInst::getToken(EDToken *&token, unsigned int index) {
184 token = Tokens[index];
189 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
193 tokvec_t::iterator iter;
195 for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
196 int ret = visitor(*iter);