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