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