1 //===-- EDInst.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===//
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 defines the interface for the Enhanced Disassembly library's
11 // instruction class. The instruction is responsible for vending the string
12 // representation, individual tokens and operands for a single instruction.
14 //===----------------------------------------------------------------------===//
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/SmallVector.h"
28 struct EDDisassembler
;
32 typedef int (^EDTokenVisitor_t
)(EDToken
*token
);
35 /// CachedResult - Encapsulates the result of a function along with the validity
36 /// of that result, so that slow functions don't need to run twice
38 /// True if the result has been obtained by executing the function
40 /// The result last obtained from the function
43 /// Constructor - Initializes an invalid result
44 CachedResult() : Valid(false) { }
45 /// valid - Returns true if the result has been obtained by executing the
46 /// function and false otherwise
47 bool valid() { return Valid
; }
48 /// result - Returns the result of the function or an undefined value if
50 int result() { return Result
; }
51 /// setResult - Sets the result of the function and declares it valid
52 /// returning the result (so that setResult() can be called from inside a
54 /// @arg result - The result of the function
55 int setResult(int result
) { Result
= result
; Valid
= true; return result
; }
58 /// EDInst - Encapsulates a single instruction, which can be queried for its
59 /// string representation, as well as its operands and tokens
61 /// The parent disassembler
62 EDDisassembler
&Disassembler
;
63 /// The containing MCInst
65 /// The instruction information provided by TableGen for this instruction
66 const llvm::EDInstInfo
*ThisInstInfo
;
67 /// The number of bytes for the machine code representation of the instruction
70 /// The result of the stringify() function
71 CachedResult StringifyResult
;
72 /// The string representation of the instruction
74 /// The order in which operands from the InstInfo's operand information appear
76 const char* OperandOrder
;
78 /// The result of the parseOperands() function
79 CachedResult ParseResult
;
80 typedef llvm::SmallVector
<EDOperand
*, 5> opvec_t
;
81 /// The instruction's operands
83 /// The operand corresponding to the target, if the instruction is a branch
85 /// The operand corresponding to the source, if the instruction is a move
87 /// The operand corresponding to the target, if the instruction is a move
90 /// The result of the tokenize() function
91 CachedResult TokenizeResult
;
92 typedef std::vector
<EDToken
*> tokvec_t
;
93 /// The instruction's tokens
96 /// Constructor - initializes an instruction given the output of the LLVM
99 /// @arg inst - The MCInst, which will now be owned by this object
100 /// @arg byteSize - The size of the consumed instruction, in bytes
101 /// @arg disassembler - The parent disassembler
102 /// @arg instInfo - The instruction information produced by the table
103 /// generator for this instruction
104 EDInst(llvm::MCInst
*inst
,
106 EDDisassembler
&disassembler
,
107 const llvm::EDInstInfo
*instInfo
);
110 /// byteSize - returns the number of bytes consumed by the machine code
111 /// representation of the instruction
113 /// instID - returns the LLVM instruction ID of the instruction
116 /// stringify - populates the String and AsmString members of the instruction,
117 /// returning 0 on success or -1 otherwise
119 /// getString - retrieves a pointer to the string representation of the
120 /// instructinon, returning 0 on success or -1 otherwise
122 /// @arg str - A reference to a pointer that, on success, is set to point to
123 /// the string representation of the instruction; this string is still owned
124 /// by the instruction and will be deleted when it is
125 int getString(const char *&str
);
127 /// isBranch - Returns true if the instruction is a branch
129 /// isMove - Returns true if the instruction is a move
132 /// parseOperands - populates the Operands member of the instruction,
133 /// returning 0 on success or -1 otherwise
135 /// branchTargetID - returns the ID (suitable for use with getOperand()) of
136 /// the target operand if the instruction is a branch, or -1 otherwise
137 int branchTargetID();
138 /// moveSourceID - returns the ID of the source operand if the instruction
139 /// is a move, or -1 otherwise
141 /// moveTargetID - returns the ID of the target operand if the instruction
142 /// is a move, or -1 otherwise
145 /// numOperands - returns the number of operands available to retrieve, or -1
148 /// getOperand - retrieves an operand from the instruction's operand list by
149 /// index, returning 0 on success or -1 on error
151 /// @arg operand - A reference whose target is pointed at the operand on
152 /// success, although the operand is still owned by the EDInst
153 /// @arg index - The index of the operand in the instruction
154 int getOperand(EDOperand
*&operand
, unsigned int index
);
156 /// tokenize - populates the Tokens member of the instruction, returning 0 on
157 /// success or -1 otherwise
159 /// numTokens - returns the number of tokens in the instruction, or -1 on
162 /// getToken - retrieves a token from the instruction's token list by index,
163 /// returning 0 on success or -1 on error
165 /// @arg token - A reference whose target is pointed at the token on success,
166 /// although the token is still owned by the EDInst
167 /// @arg index - The index of the token in the instrcutino
168 int getToken(EDToken
*&token
, unsigned int index
);
171 /// visitTokens - Visits each token in turn and applies a block to it,
172 /// returning 0 if all blocks are visited and/or the block signals
173 /// termination by returning 1; returns -1 on error
175 /// @arg visitor - The visitor block to apply to all tokens.
176 int visitTokens(EDTokenVisitor_t visitor
);
180 } // end namespace llvm