aboutsummaryrefslogtreecommitdiff
path: root/lib/MC/MCDisassembler/EDInst.h
blob: cc0b562130c0d43b9c44fd5576e343eba47c9d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//===-- EDInst.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
// This file defines the interface for the Enhanced Disassembly library's
// instruction class.  The instruction is responsible for vending the string
// representation, individual tokens and operands for a single instruction.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EDINST_H
#define LLVM_EDINST_H

#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/DataTypes.h"
#include <string>
#include <vector>

namespace llvm {
  class MCInst;
  struct EDInstInfo;
  struct EDToken;
  struct EDDisassembler;
  struct EDOperand;

#ifdef __BLOCKS__
  typedef int (^EDTokenVisitor_t)(EDToken *token);
#endif

/// CachedResult - Encapsulates the result of a function along with the validity
///   of that result, so that slow functions don't need to run twice
struct CachedResult {
  /// True if the result has been obtained by executing the function
  bool Valid;
  /// The result last obtained from the function
  int Result;
  
  /// Constructor - Initializes an invalid result
  CachedResult() : Valid(false) { }
  /// valid - Returns true if the result has been obtained by executing the
  ///   function and false otherwise
  bool valid() { return Valid; }
  /// result - Returns the result of the function or an undefined value if
  ///   valid() is false
  int result() { return Result; }
  /// setResult - Sets the result of the function and declares it valid
  ///   returning the result (so that setResult() can be called from inside a
  ///   return statement)
  /// @arg result - The result of the function
  int setResult(int result) { Result = result; Valid = true; return result; }
};

/// EDInst - Encapsulates a single instruction, which can be queried for its
///   string representation, as well as its operands and tokens
struct EDInst {
  /// The parent disassembler
  EDDisassembler &Disassembler;
  /// The containing MCInst
  llvm::MCInst *Inst;
  /// The instruction information provided by TableGen for this instruction
  const llvm::EDInstInfo *ThisInstInfo;
  /// The number of bytes for the machine code representation of the instruction
  uint64_t ByteSize;
  
  /// The result of the stringify() function
  CachedResult StringifyResult;
  /// The string representation of the instruction
  std::string String;
  /// The order in which operands from the InstInfo's operand information appear
  /// in String
  const signed char* OperandOrder;
  
  /// The result of the parseOperands() function
  CachedResult ParseResult;
  typedef llvm::SmallVector<EDOperand*, 5> opvec_t;
  /// The instruction's operands
  opvec_t Operands;
  /// The operand corresponding to the target, if the instruction is a branch
  int BranchTarget;
  /// The operand corresponding to the source, if the instruction is a move
  int MoveSource;
  /// The operand corresponding to the target, if the instruction is a move
  int MoveTarget;
  
  /// The result of the tokenize() function
  CachedResult TokenizeResult;
  typedef std::vector<EDToken*> tokvec_t;
  /// The instruction's tokens
  tokvec_t Tokens;
  
  /// Constructor - initializes an instruction given the output of the LLVM
  ///   C++ disassembler
  ///
  /// @arg inst         - The MCInst, which will now be owned by this object
  /// @arg byteSize     - The size of the consumed instruction, in bytes
  /// @arg disassembler - The parent disassembler
  /// @arg instInfo     - The instruction information produced by the table
  ///                     generator for this instruction
  EDInst(llvm::MCInst *inst,
         uint64_t byteSize,
         EDDisassembler &disassembler,
         const llvm::EDInstInfo *instInfo);
  ~EDInst();
  
  /// byteSize - returns the number of bytes consumed by the machine code
  ///   representation of the instruction
  uint64_t byteSize();
  /// instID - returns the LLVM instruction ID of the instruction
  unsigned instID();
  
  /// stringify - populates the String and AsmString members of the instruction,
  ///   returning 0 on success or -1 otherwise
  int stringify();
  /// getString - retrieves a pointer to the string representation of the
  ///   instructinon, returning 0 on success or -1 otherwise
  ///
  /// @arg str - A reference to a pointer that, on success, is set to point to
  ///   the string representation of the instruction; this string is still owned
  ///   by the instruction and will be deleted when it is
  int getString(const char *&str);
  
  /// isBranch - Returns true if the instruction is a branch
  bool isBranch();
  /// isMove - Returns true if the instruction is a move
  bool isMove();
  
  /// parseOperands - populates the Operands member of the instruction,
  ///   returning 0 on success or -1 otherwise
  int parseOperands();
  /// branchTargetID - returns the ID (suitable for use with getOperand()) of 
  ///   the target operand if the instruction is a branch, or -1 otherwise
  int branchTargetID();
  /// moveSourceID - returns the ID of the source operand if the instruction
  ///   is a move, or -1 otherwise
  int moveSourceID();
  /// moveTargetID - returns the ID of the target operand if the instruction
  ///   is a move, or -1 otherwise
  int moveTargetID();
  
  /// numOperands - returns the number of operands available to retrieve, or -1
  ///   on error
  int numOperands();
  /// getOperand - retrieves an operand from the instruction's operand list by
  ///   index, returning 0 on success or -1 on error
  ///
  /// @arg operand  - A reference whose target is pointed at the operand on
  ///                 success, although the operand is still owned by the EDInst
  /// @arg index    - The index of the operand in the instruction
  int getOperand(EDOperand *&operand, unsigned int index);

  /// tokenize - populates the Tokens member of the instruction, returning 0 on
  ///   success or -1 otherwise
  int tokenize();
  /// numTokens - returns the number of tokens in the instruction, or -1 on
  ///   error
  int numTokens();
  /// getToken - retrieves a token from the instruction's token list by index,
  ///   returning 0 on success or -1 on error
  ///
  /// @arg token  - A reference whose target is pointed at the token on success,
  ///               although the token is still owned by the EDInst
  /// @arg index  - The index of the token in the instrcutino
  int getToken(EDToken *&token, unsigned int index);

#ifdef __BLOCKS__
  /// visitTokens - Visits each token in turn and applies a block to it,
  ///   returning 0 if all blocks are visited and/or the block signals
  ///   termination by returning 1; returns -1 on error
  ///
  /// @arg visitor  - The visitor block to apply to all tokens.
  int visitTokens(EDTokenVisitor_t visitor);
#endif
};

} // end namespace llvm

#endif