blob: 6f71908d2bcf49001e58eea17fc75c95f1e1f340 [file] [log] [blame]
Chris Lattner847da552010-07-20 18:25:19 +00001//===-- EDDisassembler.h - LLVM Enhanced Disassembler -----------*- C++ -*-===//
Sean Callananee5dfd42010-02-01 08:49:35 +00002//
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
Chris Lattner847da552010-07-20 18:25:19 +000016#ifndef LLVM_EDDISASSEMBLER_H
17#define LLVM_EDDISASSEMBLER_H
Sean Callananee5dfd42010-02-01 08:49:35 +000018
Chris Lattnera4f15d62010-07-20 18:59:58 +000019#include "EDInfo.h"
Sean Callananee5dfd42010-02-01 08:49:35 +000020
Sean Callananee5dfd42010-02-01 08:49:35 +000021#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/Triple.h"
23#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/Mutex.h"
Sean Callananee5dfd42010-02-01 08:49:35 +000025
26#include <map>
27#include <set>
Sean Callanan3f4f34c2012-02-04 07:45:35 +000028#include <string>
Sean Callananee5dfd42010-02-01 08:49:35 +000029#include <vector>
30
31namespace llvm {
32class AsmLexer;
Evan Cheng0e6a0522011-07-18 20:57:22 +000033class AsmParser;
Sean Callananee5dfd42010-02-01 08:49:35 +000034class AsmToken;
35class MCContext;
36class MCAsmInfo;
37class MCAsmLexer;
Sean Callananee5dfd42010-02-01 08:49:35 +000038class MCDisassembler;
Sean Callananee5dfd42010-02-01 08:49:35 +000039class MCInst;
Craig Topper17463b32012-04-02 06:09:36 +000040class MCInstPrinter;
41class MCInstrInfo;
Sean Callananee5dfd42010-02-01 08:49:35 +000042class MCParsedAsmOperand;
Evan Cheng0e6a0522011-07-18 20:57:22 +000043class MCRegisterInfo;
Sean Callananee5dfd42010-02-01 08:49:35 +000044class MCStreamer;
Evan Chengebdeeab2011-07-08 01:53:10 +000045class MCSubtargetInfo;
Evan Cheng94b95502011-07-26 00:24:13 +000046class MCTargetAsmLexer;
47class MCTargetAsmParser;
Sean Callananee5dfd42010-02-01 08:49:35 +000048template <typename T> class SmallVectorImpl;
49class SourceMgr;
50class Target;
Sean Callanan9899f702010-04-13 21:21:57 +000051
52struct EDInstInfo;
Chris Lattner847da552010-07-20 18:25:19 +000053struct EDInst;
54struct EDOperand;
55struct EDToken;
56
57typedef int (*EDByteReaderCallback)(uint8_t *byte, uint64_t address, void *arg);
Sean Callananee5dfd42010-02-01 08:49:35 +000058
59/// EDDisassembler - Encapsulates a disassembler for a single architecture and
60/// disassembly syntax. Also manages the static disassembler registry.
61struct EDDisassembler {
Chris Lattner847da552010-07-20 18:25:19 +000062 typedef enum {
63 /*! @constant kEDAssemblySyntaxX86Intel Intel syntax for i386 and x86_64. */
64 kEDAssemblySyntaxX86Intel = 0,
65 /*! @constant kEDAssemblySyntaxX86ATT AT&T syntax for i386 and x86_64. */
66 kEDAssemblySyntaxX86ATT = 1,
67 kEDAssemblySyntaxARMUAL = 2
68 } AssemblySyntax;
69
70
Sean Callananee5dfd42010-02-01 08:49:35 +000071 ////////////////////
72 // Static members //
73 ////////////////////
74
75 /// CPUKey - Encapsulates the descriptor of an architecture/disassembly-syntax
76 /// pair
77 struct CPUKey {
78 /// The architecture type
Sean Callanan3f4f34c2012-02-04 07:45:35 +000079 std::string Triple;
Sean Callananee5dfd42010-02-01 08:49:35 +000080
81 /// The assembly syntax
Chris Lattner847da552010-07-20 18:25:19 +000082 AssemblySyntax Syntax;
Sean Callananee5dfd42010-02-01 08:49:35 +000083
84 /// operator== - Equality operator
85 bool operator==(const CPUKey &key) const {
Sean Callanan3f4f34c2012-02-04 07:45:35 +000086 return (Triple == key.Triple &&
Sean Callananee5dfd42010-02-01 08:49:35 +000087 Syntax == key.Syntax);
88 }
89
90 /// operator< - Less-than operator
91 bool operator<(const CPUKey &key) const {
Sean Callanan3f4f34c2012-02-04 07:45:35 +000092 return ((Triple < key.Triple) ||
93 ((Triple == key.Triple) && Syntax < (key.Syntax)));
Sean Callananee5dfd42010-02-01 08:49:35 +000094 }
95 };
96
97 typedef std::map<CPUKey, EDDisassembler*> DisassemblerMap_t;
98
Sean Callananee5dfd42010-02-01 08:49:35 +000099 /// A map from disassembler specifications to disassemblers. Populated
100 /// lazily.
101 static DisassemblerMap_t sDisassemblers;
102
103 /// getDisassembler - Returns the specified disassemble, or NULL on failure
104 ///
105 /// @arg arch - The desired architecture
106 /// @arg syntax - The desired disassembly syntax
107 static EDDisassembler *getDisassembler(llvm::Triple::ArchType arch,
Chris Lattner847da552010-07-20 18:25:19 +0000108 AssemblySyntax syntax);
Sean Callananee5dfd42010-02-01 08:49:35 +0000109
110 /// getDisassembler - Returns the disassembler for a given combination of
111 /// CPU type, CPU subtype, and assembly syntax, or NULL on failure
112 ///
113 /// @arg str - The string representation of the architecture triple, e.g.,
114 /// "x86_64-apple-darwin"
115 /// @arg syntax - The disassembly syntax for the required disassembler
116 static EDDisassembler *getDisassembler(llvm::StringRef str,
Chris Lattner847da552010-07-20 18:25:19 +0000117 AssemblySyntax syntax);
Sean Callananee5dfd42010-02-01 08:49:35 +0000118
Sean Callananee5dfd42010-02-01 08:49:35 +0000119 ////////////////////////
120 // Per-object members //
121 ////////////////////////
122
Sean Callanan8f993b82010-04-08 00:48:21 +0000123 /// True only if the object has been successfully initialized
Sean Callananee5dfd42010-02-01 08:49:35 +0000124 bool Valid;
Sean Callanan8f993b82010-04-08 00:48:21 +0000125 /// True if the disassembler can provide semantic information
126 bool HasSemantics;
Sean Callananee5dfd42010-02-01 08:49:35 +0000127
Sean Callanan8f993b82010-04-08 00:48:21 +0000128 /// The stream to write errors to
129 llvm::raw_ostream &ErrorStream;
Sean Callananee5dfd42010-02-01 08:49:35 +0000130
Sean Callanan3f4f34c2012-02-04 07:45:35 +0000131 /// The triple/syntax pair for the current architecture
Sean Callananee5dfd42010-02-01 08:49:35 +0000132 CPUKey Key;
Sean Callanan3f4f34c2012-02-04 07:45:35 +0000133 /// The Triple fur the current architecture
134 Triple TgtTriple;
Sean Callananee5dfd42010-02-01 08:49:35 +0000135 /// The LLVM target corresponding to the disassembler
136 const llvm::Target *Tgt;
137 /// The assembly information for the target architecture
138 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo;
James Molloyb9505852011-09-07 17:24:38 +0000139 /// The subtarget information for the target architecture
140 llvm::OwningPtr<const llvm::MCSubtargetInfo> STI;
Craig Topper17463b32012-04-02 06:09:36 +0000141 // The instruction information for the target architecture.
142 llvm::OwningPtr<const llvm::MCInstrInfo> MII;
Evan Cheng0e6a0522011-07-18 20:57:22 +0000143 // The register information for the target architecture.
144 llvm::OwningPtr<const llvm::MCRegisterInfo> MRI;
Sean Callananee5dfd42010-02-01 08:49:35 +0000145 /// 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
148 /// PrinterMutex
149 llvm::OwningPtr<std::string> InstString;
150 /// The output stream for the disassembler; must be guarded with
151 /// PrinterMutex
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
Sean Callanan9899f702010-04-13 21:21:57 +0000161 const llvm::EDInstInfo *InstInfos;
Sean Callananee5dfd42010-02-01 08:49:35 +0000162 /// The target-specific lexer for use in tokenizing strings, in
163 /// target-independent and target-specific portions
164 llvm::OwningPtr<llvm::AsmLexer> GenericAsmLexer;
Evan Cheng94b95502011-07-26 00:24:13 +0000165 llvm::OwningPtr<llvm::MCTargetAsmLexer> SpecificAsmLexer;
Sean Callananee5dfd42010-02-01 08:49:35 +0000166 /// The guard for the above
167 llvm::sys::Mutex ParserMutex;
168 /// The LLVM number used for the target disassembly syntax variant
169 int LLVMSyntaxVariant;
170
171 typedef std::vector<std::string> regvec_t;
172 typedef std::map<std::string, unsigned> regrmap_t;
173
174 /// A vector of registers for quick mapping from LLVM register IDs to names
175 regvec_t RegVec;
176 /// A map of registers for quick mapping from register names to LLVM IDs
177 regrmap_t RegRMap;
178
179 /// A set of register IDs for aliases of the stack pointer for the current
180 /// architecture
181 std::set<unsigned> stackPointers;
182 /// A set of register IDs for aliases of the program counter for the current
183 /// architecture
184 std::set<unsigned> programCounters;
185
186 /// Constructor - initializes a disassembler with all the necessary objects,
187 /// which come pre-allocated from the registry accessor function
188 ///
189 /// @arg key - the architecture and disassembly syntax for the
190 /// disassembler
191 EDDisassembler(CPUKey& key);
192
193 /// valid - reports whether there was a failure in the constructor.
194 bool valid() {
195 return Valid;
196 }
197
Sean Callanan8f993b82010-04-08 00:48:21 +0000198 /// hasSemantics - reports whether the disassembler can provide operands and
199 /// tokens.
200 bool hasSemantics() {
201 return HasSemantics;
202 }
203
Sean Callananee5dfd42010-02-01 08:49:35 +0000204 ~EDDisassembler();
205
206 /// createInst - creates and returns an instruction given a callback and
207 /// memory address, or NULL on failure
208 ///
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,
214 uint64_t address,
215 void *arg);
216
217 /// initMaps - initializes regVec and regRMap using the provided register
218 /// info
219 ///
220 /// @arg registerInfo - the register information to use as a source
Evan Cheng1b0fc9b2011-07-25 20:53:02 +0000221 void initMaps(const llvm::MCRegisterInfo &registerInfo);
Sean Callananee5dfd42010-02-01 08:49:35 +0000222 /// nameWithRegisterID - Returns the name (owned by the EDDisassembler) of a
223 /// register for a given register ID, or NULL on failure
224 ///
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
229 ///
230 /// @arg name - The name of the register
231 unsigned registerIDWithName(const char *name) const;
232
233 /// registerIsStackPointer - reports whether a register ID is an alias for the
234 /// stack pointer register
235 ///
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
240 ///
241 /// @arg registerID - The LLVM register ID
242 bool registerIsProgramCounter(unsigned registerID);
243
244 /// printInst - prints an MCInst to a string, returning 0 on success, or -1
245 /// otherwise
246 ///
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,
251 llvm::MCInst& inst);
252
253 /// parseInst - extracts operands and tokens from a string for use in
254 /// tokenizing the string. Returns 0 on success, or -1 otherwise.
255 ///
256 /// @arg operands - A reference to a vector that will be filled in with the
257 /// parsed operands
258 /// @arg tokens - A reference to a vector that will be filled in with the
259 /// tokens
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);
264
265 /// llvmSyntaxVariant - returns the LLVM syntax variant for this disassembler
266 int llvmSyntaxVariant() const;
267};
268
Chris Lattner847da552010-07-20 18:25:19 +0000269} // end namespace llvm
270
Sean Callananee5dfd42010-02-01 08:49:35 +0000271#endif