• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- EDInst.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_EDINST_H
17 #define LLVM_EDINST_H
18 
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include <string>
22 #include <vector>
23 
24 namespace llvm {
25   class MCInst;
26   struct EDInstInfo;
27   struct EDToken;
28   struct EDDisassembler;
29   struct EDOperand;
30 
31 #ifdef __BLOCKS__
32   typedef int (^EDTokenVisitor_t)(EDToken *token);
33 #endif
34 
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
37 struct CachedResult {
38   /// True if the result has been obtained by executing the function
39   bool Valid;
40   /// The result last obtained from the function
41   int Result;
42 
43   /// Constructor - Initializes an invalid result
CachedResultCachedResult44   CachedResult() : Valid(false) { }
45   /// valid - Returns true if the result has been obtained by executing the
46   ///   function and false otherwise
validCachedResult47   bool valid() { return Valid; }
48   /// result - Returns the result of the function or an undefined value if
49   ///   valid() is false
resultCachedResult50   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
53   ///   return statement)
54   /// @arg result - The result of the function
setResultCachedResult55   int setResult(int result) { Result = result; Valid = true; return result; }
56 };
57 
58 /// EDInst - Encapsulates a single instruction, which can be queried for its
59 ///   string representation, as well as its operands and tokens
60 struct EDInst {
61   /// The parent disassembler
62   EDDisassembler &Disassembler;
63   /// The containing MCInst
64   llvm::MCInst *Inst;
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
68   uint64_t ByteSize;
69 
70   /// The result of the stringify() function
71   CachedResult StringifyResult;
72   /// The string representation of the instruction
73   std::string String;
74   /// The order in which operands from the InstInfo's operand information appear
75   /// in String
76   const char* OperandOrder;
77 
78   /// The result of the parseOperands() function
79   CachedResult ParseResult;
80   typedef llvm::SmallVector<EDOperand*, 5> opvec_t;
81   /// The instruction's operands
82   opvec_t Operands;
83   /// The operand corresponding to the target, if the instruction is a branch
84   int BranchTarget;
85   /// The operand corresponding to the source, if the instruction is a move
86   int MoveSource;
87   /// The operand corresponding to the target, if the instruction is a move
88   int MoveTarget;
89 
90   /// The result of the tokenize() function
91   CachedResult TokenizeResult;
92   typedef std::vector<EDToken*> tokvec_t;
93   /// The instruction's tokens
94   tokvec_t Tokens;
95 
96   /// Constructor - initializes an instruction given the output of the LLVM
97   ///   C++ disassembler
98   ///
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,
105          uint64_t byteSize,
106          EDDisassembler &disassembler,
107          const llvm::EDInstInfo *instInfo);
108   ~EDInst();
109 
110   /// byteSize - returns the number of bytes consumed by the machine code
111   ///   representation of the instruction
112   uint64_t byteSize();
113   /// instID - returns the LLVM instruction ID of the instruction
114   unsigned instID();
115 
116   /// stringify - populates the String and AsmString members of the instruction,
117   ///   returning 0 on success or -1 otherwise
118   int stringify();
119   /// getString - retrieves a pointer to the string representation of the
120   ///   instructinon, returning 0 on success or -1 otherwise
121   ///
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);
126 
127   /// isBranch - Returns true if the instruction is a branch
128   bool isBranch();
129   /// isMove - Returns true if the instruction is a move
130   bool isMove();
131 
132   /// parseOperands - populates the Operands member of the instruction,
133   ///   returning 0 on success or -1 otherwise
134   int parseOperands();
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
140   int moveSourceID();
141   /// moveTargetID - returns the ID of the target operand if the instruction
142   ///   is a move, or -1 otherwise
143   int moveTargetID();
144 
145   /// numOperands - returns the number of operands available to retrieve, or -1
146   ///   on error
147   int numOperands();
148   /// getOperand - retrieves an operand from the instruction's operand list by
149   ///   index, returning 0 on success or -1 on error
150   ///
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);
155 
156   /// tokenize - populates the Tokens member of the instruction, returning 0 on
157   ///   success or -1 otherwise
158   int tokenize();
159   /// numTokens - returns the number of tokens in the instruction, or -1 on
160   ///   error
161   int numTokens();
162   /// getToken - retrieves a token from the instruction's token list by index,
163   ///   returning 0 on success or -1 on error
164   ///
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);
169 
170 #ifdef __BLOCKS__
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
174   ///
175   /// @arg visitor  - The visitor block to apply to all tokens.
176   int visitTokens(EDTokenVisitor_t visitor);
177 #endif
178 };
179 
180 } // end namespace llvm
181 
182 #endif
183