SubRegIndex'ize Mips
[oota-llvm.git] / tools / edis / EDInst.h
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 EDInst_
17 #define EDInst_
18
19 #include "llvm-c/EnhancedDisassembly.h"
20
21 #include "llvm/ADT/SmallVector.h"
22
23 #include <string>
24 #include <vector>
25
26 namespace llvm {
27   struct EDInstInfo;
28 }
29
30 /// CachedResult - Encapsulates the result of a function along with the validity
31 ///   of that result, so that slow functions don't need to run twice
32 struct CachedResult {
33   /// True if the result has been obtained by executing the function
34   bool Valid;
35   /// The result last obtained from the function
36   int Result;
37   
38   /// Constructor - Initializes an invalid result
39   CachedResult() : Valid(false) { }
40   /// valid - Returns true if the result has been obtained by executing the
41   ///   function and false otherwise
42   bool valid() { return Valid; }
43   /// result - Returns the result of the function or an undefined value if
44   ///   valid() is false
45   int result() { return Result; }
46   /// setResult - Sets the result of the function and declares it valid
47   ///   returning the result (so that setResult() can be called from inside a
48   ///   return statement)
49   /// @arg result - The result of the function
50   int setResult(int result) { Result = result; Valid = true; return result; }
51 };
52
53 /// EDInst - Encapsulates a single instruction, which can be queried for its
54 ///   string representation, as well as its operands and tokens
55 struct EDInst {
56   /// The parent disassembler
57   EDDisassembler &Disassembler;
58   /// The containing MCInst
59   llvm::MCInst *Inst;
60   /// The instruction information provided by TableGen for this instruction
61   const llvm::EDInstInfo *ThisInstInfo;
62   /// The number of bytes for the machine code representation of the instruction
63   uint64_t ByteSize;
64   
65   /// The result of the stringify() function
66   CachedResult StringifyResult;
67   /// The string representation of the instruction
68   std::string String;
69   /// The order in which operands from the InstInfo's operand information appear
70   /// in String
71   const char* OperandOrder;
72   
73   /// The result of the parseOperands() function
74   CachedResult ParseResult;
75   typedef llvm::SmallVector<EDOperand*, 5> opvec_t;
76   /// The instruction's operands
77   opvec_t Operands;
78   /// The operand corresponding to the target, if the instruction is a branch
79   int BranchTarget;
80   /// The operand corresponding to the source, if the instruction is a move
81   int MoveSource;
82   /// The operand corresponding to the target, if the instruction is a move
83   int MoveTarget;
84   
85   /// The result of the tokenize() function
86   CachedResult TokenizeResult;
87   typedef std::vector<EDToken*> tokvec_t;
88   /// The instruction's tokens
89   tokvec_t Tokens;
90   
91   /// Constructor - initializes an instruction given the output of the LLVM
92   ///   C++ disassembler
93   ///
94   /// @arg inst         - The MCInst, which will now be owned by this object
95   /// @arg byteSize     - The size of the consumed instruction, in bytes
96   /// @arg disassembler - The parent disassembler
97   /// @arg instInfo     - The instruction information produced by the table
98   ///                     generator for this instruction
99   EDInst(llvm::MCInst *inst,
100          uint64_t byteSize,
101          EDDisassembler &disassembler,
102          const llvm::EDInstInfo *instInfo);
103   ~EDInst();
104   
105   /// byteSize - returns the number of bytes consumed by the machine code
106   ///   representation of the instruction
107   uint64_t byteSize();
108   /// instID - returns the LLVM instruction ID of the instruction
109   unsigned instID();
110   
111   /// stringify - populates the String and AsmString members of the instruction,
112   ///   returning 0 on success or -1 otherwise
113   int stringify();
114   /// getString - retrieves a pointer to the string representation of the
115   ///   instructinon, returning 0 on success or -1 otherwise
116   ///
117   /// @arg str - A reference to a pointer that, on success, is set to point to
118   ///   the string representation of the instruction; this string is still owned
119   ///   by the instruction and will be deleted when it is
120   int getString(const char *&str);
121   
122   /// isBranch - Returns true if the instruction is a branch
123   bool isBranch();
124   /// isMove - Returns true if the instruction is a move
125   bool isMove();
126   
127   /// parseOperands - populates the Operands member of the instruction,
128   ///   returning 0 on success or -1 otherwise
129   int parseOperands();
130   /// branchTargetID - returns the ID (suitable for use with getOperand()) of 
131   ///   the target operand if the instruction is a branch, or -1 otherwise
132   int branchTargetID();
133   /// moveSourceID - returns the ID of the source operand if the instruction
134   ///   is a move, or -1 otherwise
135   int moveSourceID();
136   /// moveTargetID - returns the ID of the target operand if the instruction
137   ///   is a move, or -1 otherwise
138   int moveTargetID();
139   
140   /// numOperands - returns the number of operands available to retrieve, or -1
141   ///   on error
142   int numOperands();
143   /// getOperand - retrieves an operand from the instruction's operand list by
144   ///   index, returning 0 on success or -1 on error
145   ///
146   /// @arg operand  - A reference whose target is pointed at the operand on
147   ///                 success, although the operand is still owned by the EDInst
148   /// @arg index    - The index of the operand in the instruction
149   int getOperand(EDOperand *&operand, unsigned int index);
150
151   /// tokenize - populates the Tokens member of the instruction, returning 0 on
152   ///   success or -1 otherwise
153   int tokenize();
154   /// numTokens - returns the number of tokens in the instruction, or -1 on
155   ///   error
156   int numTokens();
157   /// getToken - retrieves a token from the instruction's token list by index,
158   ///   returning 0 on success or -1 on error
159   ///
160   /// @arg token  - A reference whose target is pointed at the token on success,
161   ///               although the token is still owned by the EDInst
162   /// @arg index  - The index of the token in the instrcutino
163   int getToken(EDToken *&token, unsigned int index);
164
165 #ifdef __BLOCKS__
166   /// visitTokens - Visits each token in turn and applies a block to it,
167   ///   returning 0 if all blocks are visited and/or the block signals
168   ///   termination by returning 1; returns -1 on error
169   ///
170   /// @arg visitor  - The visitor block to apply to all tokens.
171   int visitTokens(EDTokenVisitor_t visitor);
172 #endif
173 };
174
175 #endif