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