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