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),
40 unsigned int numOperands = Operands.size();
42 for (index = 0; index < numOperands; ++index)
43 delete Operands[index];
45 unsigned int numTokens = Tokens.size();
47 for (index = 0; index < numTokens; ++index)
53 uint64_t EDInst::byteSize() {
57 int EDInst::stringify() {
58 if (StringifyResult.valid())
59 return StringifyResult.result();
61 if (Disassembler.printInst(String, *Inst))
62 return StringifyResult.setResult(-1);
64 OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()];
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->instructionFlags & kInstructionFlagBranch;
89 bool EDInst::isMove() {
91 return ThisInstInfo->instructionFlags & kInstructionFlagMove;
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);