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 return StringifyResult.setResult(0);
69 int EDInst::getString(const char*& str) {
78 unsigned EDInst::instID() {
79 return Inst->getOpcode();
82 bool EDInst::isBranch() {
85 ThisInstInfo->instructionType == kInstructionTypeBranch ||
86 ThisInstInfo->instructionType == kInstructionTypeCall;
91 bool EDInst::isMove() {
93 return ThisInstInfo->instructionType == kInstructionTypeMove;
98 int EDInst::parseOperands() {
99 if (ParseResult.valid())
100 return ParseResult.result();
103 return ParseResult.setResult(-1);
105 unsigned int opIndex;
106 unsigned int mcOpIndex = 0;
108 for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
110 (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
111 BranchTarget = opIndex;
114 if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
115 MoveSource = opIndex;
116 else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
117 MoveTarget = opIndex;
120 EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
122 Operands.push_back(operand);
125 return ParseResult.setResult(0);
128 int EDInst::branchTargetID() {
134 int EDInst::moveSourceID() {
140 int EDInst::moveTargetID() {
146 int EDInst::numOperands() {
149 return Operands.size();
152 int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
156 if (index >= Operands.size())
159 operand = Operands[index];
163 int EDInst::tokenize() {
164 if (TokenizeResult.valid())
165 return TokenizeResult.result();
168 return TokenizeResult.setResult(-1);
170 return TokenizeResult.setResult(EDToken::tokenize(Tokens,
177 int EDInst::numTokens() {
180 return Tokens.size();
183 int EDInst::getToken(EDToken *&token, unsigned int index) {
186 token = Tokens[index];
191 int EDInst::visitTokens(EDTokenVisitor_t visitor) {
195 tokvec_t::iterator iter;
197 for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
198 int ret = visitor(*iter);