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