1 //===-- EDDisassembler.h - LLVM Enhanced Disassembler -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_EDDISASSEMBLER_H
17 #define LLVM_EDDISASSEMBLER_H
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/System/Mutex.h"
38 class TargetAsmParser;
42 class MCParsedAsmOperand;
44 template <typename T> class SmallVectorImpl;
48 class TargetRegisterInfo;
55 typedef int (*EDByteReaderCallback)(uint8_t *byte, uint64_t address, void *arg);
57 /// EDDisassembler - Encapsulates a disassembler for a single architecture and
58 /// disassembly syntax. Also manages the static disassembler registry.
59 struct EDDisassembler {
61 /*! @constant kEDAssemblySyntaxX86Intel Intel syntax for i386 and x86_64. */
62 kEDAssemblySyntaxX86Intel = 0,
63 /*! @constant kEDAssemblySyntaxX86ATT AT&T syntax for i386 and x86_64. */
64 kEDAssemblySyntaxX86ATT = 1,
65 kEDAssemblySyntaxARMUAL = 2
73 /// CPUKey - Encapsulates the descriptor of an architecture/disassembly-syntax
76 /// The architecture type
77 llvm::Triple::ArchType Arch;
79 /// The assembly syntax
80 AssemblySyntax Syntax;
82 /// operator== - Equality operator
83 bool operator==(const CPUKey &key) const {
84 return (Arch == key.Arch &&
85 Syntax == key.Syntax);
88 /// operator< - Less-than operator
89 bool operator<(const CPUKey &key) const {
92 if(Syntax >= key.Syntax)
98 typedef std::map<CPUKey, EDDisassembler*> DisassemblerMap_t;
100 /// True if the disassembler registry has been initialized; false if not
101 static bool sInitialized;
102 /// A map from disassembler specifications to disassemblers. Populated
104 static DisassemblerMap_t sDisassemblers;
106 /// getDisassembler - Returns the specified disassemble, or NULL on failure
108 /// @arg arch - The desired architecture
109 /// @arg syntax - The desired disassembly syntax
110 static EDDisassembler *getDisassembler(llvm::Triple::ArchType arch,
111 AssemblySyntax syntax);
113 /// getDisassembler - Returns the disassembler for a given combination of
114 /// CPU type, CPU subtype, and assembly syntax, or NULL on failure
116 /// @arg str - The string representation of the architecture triple, e.g.,
117 /// "x86_64-apple-darwin"
118 /// @arg syntax - The disassembly syntax for the required disassembler
119 static EDDisassembler *getDisassembler(llvm::StringRef str,
120 AssemblySyntax syntax);
122 /// initialize - Initializes the disassembler registry and the LLVM backend
123 static void initialize();
125 ////////////////////////
126 // Per-object members //
127 ////////////////////////
129 /// True only if the object has been successfully initialized
131 /// True if the disassembler can provide semantic information
134 /// The stream to write errors to
135 llvm::raw_ostream &ErrorStream;
137 /// The architecture/syntax pair for the current architecture
139 /// The LLVM target corresponding to the disassembler
140 const llvm::Target *Tgt;
141 /// The target machine instance.
142 llvm::OwningPtr<llvm::TargetMachine> TargetMachine;
143 /// The assembly information for the target architecture
144 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo;
145 /// The disassembler for the target architecture
146 llvm::OwningPtr<const llvm::MCDisassembler> Disassembler;
147 /// The output string for the instruction printer; must be guarded with
149 llvm::OwningPtr<std::string> InstString;
150 /// The output stream for the disassembler; must be guarded with
152 llvm::OwningPtr<llvm::raw_string_ostream> InstStream;
153 /// The instruction printer for the target architecture; must be guarded with
154 /// PrinterMutex when printing
155 llvm::OwningPtr<llvm::MCInstPrinter> InstPrinter;
156 /// The mutex that guards the instruction printer's printing functions, which
157 /// use a shared stream
158 llvm::sys::Mutex PrinterMutex;
159 /// The array of instruction information provided by the TableGen backend for
160 /// the target architecture
161 const llvm::EDInstInfo *InstInfos;
162 /// The target-specific lexer for use in tokenizing strings, in
163 /// target-independent and target-specific portions
164 llvm::OwningPtr<llvm::AsmLexer> GenericAsmLexer;
165 llvm::OwningPtr<llvm::TargetAsmLexer> SpecificAsmLexer;
166 /// The guard for the above
167 llvm::sys::Mutex ParserMutex;
168 /// The LLVM number used for the target disassembly syntax variant
169 int LLVMSyntaxVariant;
171 typedef std::vector<std::string> regvec_t;
172 typedef std::map<std::string, unsigned> regrmap_t;
174 /// A vector of registers for quick mapping from LLVM register IDs to names
176 /// A map of registers for quick mapping from register names to LLVM IDs
179 /// A set of register IDs for aliases of the stack pointer for the current
181 std::set<unsigned> stackPointers;
182 /// A set of register IDs for aliases of the program counter for the current
184 std::set<unsigned> programCounters;
186 /// Constructor - initializes a disassembler with all the necessary objects,
187 /// which come pre-allocated from the registry accessor function
189 /// @arg key - the architecture and disassembly syntax for the
191 EDDisassembler(CPUKey& key);
193 /// valid - reports whether there was a failure in the constructor.
198 /// hasSemantics - reports whether the disassembler can provide operands and
200 bool hasSemantics() {
206 /// createInst - creates and returns an instruction given a callback and
207 /// memory address, or NULL on failure
209 /// @arg byteReader - A callback function that provides machine code bytes
210 /// @arg address - The address of the first byte of the instruction,
211 /// suitable for passing to byteReader
212 /// @arg arg - An opaque argument for byteReader
213 EDInst *createInst(EDByteReaderCallback byteReader,
217 /// initMaps - initializes regVec and regRMap using the provided register
220 /// @arg registerInfo - the register information to use as a source
221 void initMaps(const llvm::TargetRegisterInfo ®isterInfo);
222 /// nameWithRegisterID - Returns the name (owned by the EDDisassembler) of a
223 /// register for a given register ID, or NULL on failure
225 /// @arg registerID - the ID of the register to be queried
226 const char *nameWithRegisterID(unsigned registerID) const;
227 /// registerIDWithName - Returns the ID of a register for a given register
228 /// name, or (unsigned)-1 on failure
230 /// @arg name - The name of the register
231 unsigned registerIDWithName(const char *name) const;
233 /// registerIsStackPointer - reports whether a register ID is an alias for the
234 /// stack pointer register
236 /// @arg registerID - The LLVM register ID
237 bool registerIsStackPointer(unsigned registerID);
238 /// registerIsStackPointer - reports whether a register ID is an alias for the
239 /// stack pointer register
241 /// @arg registerID - The LLVM register ID
242 bool registerIsProgramCounter(unsigned registerID);
244 /// printInst - prints an MCInst to a string, returning 0 on success, or -1
247 /// @arg str - A reference to a string which is filled in with the string
248 /// representation of the instruction
249 /// @arg inst - A reference to the MCInst to be printed
250 int printInst(std::string& str,
253 /// parseInst - extracts operands and tokens from a string for use in
254 /// tokenizing the string. Returns 0 on success, or -1 otherwise.
256 /// @arg operands - A reference to a vector that will be filled in with the
258 /// @arg tokens - A reference to a vector that will be filled in with the
260 /// @arg str - The string representation of the instruction
261 int parseInst(llvm::SmallVectorImpl<llvm::MCParsedAsmOperand*> &operands,
262 llvm::SmallVectorImpl<llvm::AsmToken> &tokens,
263 const std::string &str);
265 /// llvmSyntaxVariant - returns the LLVM syntax variant for this disassembler
266 int llvmSyntaxVariant() const;
269 } // end namespace llvm