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