blob: 913ae842bf92d11ce5d56c491708ca048a95d884 [file] [log] [blame]
Sean Callananee5dfd42010-02-01 08:49:35 +00001//===-EDDisassembler.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// disassembler class. The disassembler is responsible for vending individual
12// instructions according to a given architecture and disassembly syntax.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef EDDisassembler_
17#define EDDisassembler_
18
19#include "EDInfo.inc"
20
21#include "llvm-c/EnhancedDisassembly.h"
22
23#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/Triple.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/System/Mutex.h"
27
28#include <map>
29#include <set>
30#include <string>
31#include <vector>
32
33namespace llvm {
34class AsmLexer;
35class AsmToken;
36class MCContext;
37class MCAsmInfo;
38class MCAsmLexer;
39class AsmParser;
40class TargetAsmLexer;
41class TargetAsmParser;
42class MCDisassembler;
43class MCInstPrinter;
44class MCInst;
45class MCParsedAsmOperand;
46class MCStreamer;
47template <typename T> class SmallVectorImpl;
48class SourceMgr;
49class Target;
Daniel Dunbar02612432010-07-19 00:33:43 +000050class TargetMachine;
Sean Callananee5dfd42010-02-01 08:49:35 +000051class TargetRegisterInfo;
Sean Callanan9899f702010-04-13 21:21:57 +000052
53struct EDInstInfo;
Sean Callananee5dfd42010-02-01 08:49:35 +000054}
55
56/// EDDisassembler - Encapsulates a disassembler for a single architecture and
57/// disassembly syntax. Also manages the static disassembler registry.
58struct EDDisassembler {
59 ////////////////////
60 // Static members //
61 ////////////////////
62
63 /// CPUKey - Encapsulates the descriptor of an architecture/disassembly-syntax
64 /// pair
65 struct CPUKey {
66 /// The architecture type
67 llvm::Triple::ArchType Arch;
68
69 /// The assembly syntax
70 EDAssemblySyntax_t Syntax;
71
72 /// operator== - Equality operator
73 bool operator==(const CPUKey &key) const {
74 return (Arch == key.Arch &&
75 Syntax == key.Syntax);
76 }
77
78 /// operator< - Less-than operator
79 bool operator<(const CPUKey &key) const {
80 if(Arch > key.Arch)
81 return false;
82 if(Syntax >= key.Syntax)
83 return false;
84 return true;
85 }
86 };
87
88 typedef std::map<CPUKey, EDDisassembler*> DisassemblerMap_t;
89
90 /// True if the disassembler registry has been initialized; false if not
91 static bool sInitialized;
92 /// A map from disassembler specifications to disassemblers. Populated
93 /// lazily.
94 static DisassemblerMap_t sDisassemblers;
95
96 /// getDisassembler - Returns the specified disassemble, or NULL on failure
97 ///
98 /// @arg arch - The desired architecture
99 /// @arg syntax - The desired disassembly syntax
100 static EDDisassembler *getDisassembler(llvm::Triple::ArchType arch,
101 EDAssemblySyntax_t syntax);
102
103 /// getDisassembler - Returns the disassembler for a given combination of
104 /// CPU type, CPU subtype, and assembly syntax, or NULL on failure
105 ///
106 /// @arg str - The string representation of the architecture triple, e.g.,
107 /// "x86_64-apple-darwin"
108 /// @arg syntax - The disassembly syntax for the required disassembler
109 static EDDisassembler *getDisassembler(llvm::StringRef str,
110 EDAssemblySyntax_t syntax);
111
112 /// initialize - Initializes the disassembler registry and the LLVM backend
113 static void initialize();
114
115 ////////////////////////
116 // Per-object members //
117 ////////////////////////
118
Sean Callanan8f993b82010-04-08 00:48:21 +0000119 /// True only if the object has been successfully initialized
Sean Callananee5dfd42010-02-01 08:49:35 +0000120 bool Valid;
Sean Callanan8f993b82010-04-08 00:48:21 +0000121 /// True if the disassembler can provide semantic information
122 bool HasSemantics;
Sean Callananee5dfd42010-02-01 08:49:35 +0000123
Sean Callanan8f993b82010-04-08 00:48:21 +0000124 /// The stream to write errors to
125 llvm::raw_ostream &ErrorStream;
Sean Callananee5dfd42010-02-01 08:49:35 +0000126
127 /// The architecture/syntax pair for the current architecture
128 CPUKey Key;
129 /// The LLVM target corresponding to the disassembler
130 const llvm::Target *Tgt;
Daniel Dunbar02612432010-07-19 00:33:43 +0000131 /// The target machien instance.
132 llvm::OwningPtr<llvm::TargetMachine> TargetMachine;
Sean Callananee5dfd42010-02-01 08:49:35 +0000133 /// The assembly information for the target architecture
134 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo;
135 /// The disassembler for the target architecture
136 llvm::OwningPtr<const llvm::MCDisassembler> Disassembler;
137 /// The output string for the instruction printer; must be guarded with
138 /// PrinterMutex
139 llvm::OwningPtr<std::string> InstString;
140 /// The output stream for the disassembler; must be guarded with
141 /// PrinterMutex
142 llvm::OwningPtr<llvm::raw_string_ostream> InstStream;
143 /// The instruction printer for the target architecture; must be guarded with
144 /// PrinterMutex when printing
145 llvm::OwningPtr<llvm::MCInstPrinter> InstPrinter;
146 /// The mutex that guards the instruction printer's printing functions, which
147 /// use a shared stream
148 llvm::sys::Mutex PrinterMutex;
149 /// The array of instruction information provided by the TableGen backend for
150 /// the target architecture
Sean Callanan9899f702010-04-13 21:21:57 +0000151 const llvm::EDInstInfo *InstInfos;
Sean Callananee5dfd42010-02-01 08:49:35 +0000152 /// The target-specific lexer for use in tokenizing strings, in
153 /// target-independent and target-specific portions
154 llvm::OwningPtr<llvm::AsmLexer> GenericAsmLexer;
155 llvm::OwningPtr<llvm::TargetAsmLexer> SpecificAsmLexer;
156 /// The guard for the above
157 llvm::sys::Mutex ParserMutex;
158 /// The LLVM number used for the target disassembly syntax variant
159 int LLVMSyntaxVariant;
160
161 typedef std::vector<std::string> regvec_t;
162 typedef std::map<std::string, unsigned> regrmap_t;
163
164 /// A vector of registers for quick mapping from LLVM register IDs to names
165 regvec_t RegVec;
166 /// A map of registers for quick mapping from register names to LLVM IDs
167 regrmap_t RegRMap;
168
169 /// A set of register IDs for aliases of the stack pointer for the current
170 /// architecture
171 std::set<unsigned> stackPointers;
172 /// A set of register IDs for aliases of the program counter for the current
173 /// architecture
174 std::set<unsigned> programCounters;
175
176 /// Constructor - initializes a disassembler with all the necessary objects,
177 /// which come pre-allocated from the registry accessor function
178 ///
179 /// @arg key - the architecture and disassembly syntax for the
180 /// disassembler
181 EDDisassembler(CPUKey& key);
182
183 /// valid - reports whether there was a failure in the constructor.
184 bool valid() {
185 return Valid;
186 }
187
Sean Callanan8f993b82010-04-08 00:48:21 +0000188 /// hasSemantics - reports whether the disassembler can provide operands and
189 /// tokens.
190 bool hasSemantics() {
191 return HasSemantics;
192 }
193
Sean Callananee5dfd42010-02-01 08:49:35 +0000194 ~EDDisassembler();
195
196 /// createInst - creates and returns an instruction given a callback and
197 /// memory address, or NULL on failure
198 ///
199 /// @arg byteReader - A callback function that provides machine code bytes
200 /// @arg address - The address of the first byte of the instruction,
201 /// suitable for passing to byteReader
202 /// @arg arg - An opaque argument for byteReader
203 EDInst *createInst(EDByteReaderCallback byteReader,
204 uint64_t address,
205 void *arg);
206
207 /// initMaps - initializes regVec and regRMap using the provided register
208 /// info
209 ///
210 /// @arg registerInfo - the register information to use as a source
211 void initMaps(const llvm::TargetRegisterInfo &registerInfo);
212 /// nameWithRegisterID - Returns the name (owned by the EDDisassembler) of a
213 /// register for a given register ID, or NULL on failure
214 ///
215 /// @arg registerID - the ID of the register to be queried
216 const char *nameWithRegisterID(unsigned registerID) const;
217 /// registerIDWithName - Returns the ID of a register for a given register
218 /// name, or (unsigned)-1 on failure
219 ///
220 /// @arg name - The name of the register
221 unsigned registerIDWithName(const char *name) const;
222
223 /// registerIsStackPointer - reports whether a register ID is an alias for the
224 /// stack pointer register
225 ///
226 /// @arg registerID - The LLVM register ID
227 bool registerIsStackPointer(unsigned registerID);
228 /// registerIsStackPointer - reports whether a register ID is an alias for the
229 /// stack pointer register
230 ///
231 /// @arg registerID - The LLVM register ID
232 bool registerIsProgramCounter(unsigned registerID);
233
234 /// printInst - prints an MCInst to a string, returning 0 on success, or -1
235 /// otherwise
236 ///
237 /// @arg str - A reference to a string which is filled in with the string
238 /// representation of the instruction
239 /// @arg inst - A reference to the MCInst to be printed
240 int printInst(std::string& str,
241 llvm::MCInst& inst);
242
243 /// parseInst - extracts operands and tokens from a string for use in
244 /// tokenizing the string. Returns 0 on success, or -1 otherwise.
245 ///
246 /// @arg operands - A reference to a vector that will be filled in with the
247 /// parsed operands
248 /// @arg tokens - A reference to a vector that will be filled in with the
249 /// tokens
250 /// @arg str - The string representation of the instruction
251 int parseInst(llvm::SmallVectorImpl<llvm::MCParsedAsmOperand*> &operands,
252 llvm::SmallVectorImpl<llvm::AsmToken> &tokens,
253 const std::string &str);
254
255 /// llvmSyntaxVariant - returns the LLVM syntax variant for this disassembler
256 int llvmSyntaxVariant() const;
257};
258
259#endif