Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 1 | //===-- EDDisassembler.h - LLVM Enhanced Disassembler -----------*- C++ -*-===// |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 16 | #ifndef LLVM_EDDISASSEMBLER_H |
| 17 | #define LLVM_EDDISASSEMBLER_H |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 18 | |
Chris Lattner | a4f15d6 | 2010-07-20 18:59:58 +0000 | [diff] [blame] | 19 | #include "EDInfo.h" |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 20 | |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "llvm/ADT/Triple.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mutex.h" |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 25 | |
| 26 | #include <map> |
| 27 | #include <set> |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 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; |
Daniel Dunbar | 0261243 | 2010-07-19 00:33:43 +0000 | [diff] [blame] | 47 | class TargetMachine; |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 48 | class TargetRegisterInfo; |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 49 | |
| 50 | struct EDInstInfo; |
Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 51 | struct EDInst; |
| 52 | struct EDOperand; |
| 53 | struct EDToken; |
| 54 | |
| 55 | typedef int (*EDByteReaderCallback)(uint8_t *byte, uint64_t address, void *arg); |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 56 | |
| 57 | /// EDDisassembler - Encapsulates a disassembler for a single architecture and |
| 58 | /// disassembly syntax. Also manages the static disassembler registry. |
| 59 | struct EDDisassembler { |
Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 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 | |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 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 |
Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 80 | AssemblySyntax Syntax; |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 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 { |
Sean Callanan | 7a387e4 | 2011-03-12 03:27:54 +0000 | [diff] [blame] | 90 | return ((Arch < key.Arch) || |
| 91 | ((Arch == key.Arch) && Syntax < (key.Syntax))); |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
| 95 | typedef std::map<CPUKey, EDDisassembler*> DisassemblerMap_t; |
| 96 | |
| 97 | /// True if the disassembler registry has been initialized; false if not |
| 98 | static bool sInitialized; |
| 99 | /// 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 Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 108 | AssemblySyntax syntax); |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 109 | |
| 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 Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 117 | AssemblySyntax syntax); |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 118 | |
| 119 | /// initialize - Initializes the disassembler registry and the LLVM backend |
| 120 | static void initialize(); |
| 121 | |
| 122 | //////////////////////// |
| 123 | // Per-object members // |
| 124 | //////////////////////// |
| 125 | |
Sean Callanan | 8f993b8 | 2010-04-08 00:48:21 +0000 | [diff] [blame] | 126 | /// True only if the object has been successfully initialized |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 127 | bool Valid; |
Sean Callanan | 8f993b8 | 2010-04-08 00:48:21 +0000 | [diff] [blame] | 128 | /// True if the disassembler can provide semantic information |
| 129 | bool HasSemantics; |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 130 | |
Sean Callanan | 8f993b8 | 2010-04-08 00:48:21 +0000 | [diff] [blame] | 131 | /// The stream to write errors to |
| 132 | llvm::raw_ostream &ErrorStream; |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 133 | |
| 134 | /// The architecture/syntax pair for the current architecture |
| 135 | CPUKey Key; |
| 136 | /// The LLVM target corresponding to the disassembler |
| 137 | const llvm::Target *Tgt; |
Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 138 | /// The target machine instance. |
Daniel Dunbar | 0261243 | 2010-07-19 00:33:43 +0000 | [diff] [blame] | 139 | llvm::OwningPtr<llvm::TargetMachine> TargetMachine; |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 140 | /// The assembly information for the target architecture |
| 141 | llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo; |
| 142 | /// The disassembler for the target architecture |
| 143 | llvm::OwningPtr<const llvm::MCDisassembler> Disassembler; |
| 144 | /// The output string for the instruction printer; must be guarded with |
| 145 | /// PrinterMutex |
| 146 | llvm::OwningPtr<std::string> InstString; |
| 147 | /// The output stream for the disassembler; must be guarded with |
| 148 | /// PrinterMutex |
| 149 | llvm::OwningPtr<llvm::raw_string_ostream> InstStream; |
| 150 | /// The instruction printer for the target architecture; must be guarded with |
| 151 | /// PrinterMutex when printing |
| 152 | llvm::OwningPtr<llvm::MCInstPrinter> InstPrinter; |
| 153 | /// The mutex that guards the instruction printer's printing functions, which |
| 154 | /// use a shared stream |
| 155 | llvm::sys::Mutex PrinterMutex; |
| 156 | /// The array of instruction information provided by the TableGen backend for |
| 157 | /// the target architecture |
Sean Callanan | 9899f70 | 2010-04-13 21:21:57 +0000 | [diff] [blame] | 158 | const llvm::EDInstInfo *InstInfos; |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 159 | /// The target-specific lexer for use in tokenizing strings, in |
| 160 | /// target-independent and target-specific portions |
| 161 | llvm::OwningPtr<llvm::AsmLexer> GenericAsmLexer; |
| 162 | llvm::OwningPtr<llvm::TargetAsmLexer> SpecificAsmLexer; |
| 163 | /// The guard for the above |
| 164 | llvm::sys::Mutex ParserMutex; |
| 165 | /// The LLVM number used for the target disassembly syntax variant |
| 166 | int LLVMSyntaxVariant; |
| 167 | |
| 168 | typedef std::vector<std::string> regvec_t; |
| 169 | typedef std::map<std::string, unsigned> regrmap_t; |
| 170 | |
| 171 | /// A vector of registers for quick mapping from LLVM register IDs to names |
| 172 | regvec_t RegVec; |
| 173 | /// A map of registers for quick mapping from register names to LLVM IDs |
| 174 | regrmap_t RegRMap; |
| 175 | |
| 176 | /// A set of register IDs for aliases of the stack pointer for the current |
| 177 | /// architecture |
| 178 | std::set<unsigned> stackPointers; |
| 179 | /// A set of register IDs for aliases of the program counter for the current |
| 180 | /// architecture |
| 181 | std::set<unsigned> programCounters; |
| 182 | |
| 183 | /// Constructor - initializes a disassembler with all the necessary objects, |
| 184 | /// which come pre-allocated from the registry accessor function |
| 185 | /// |
| 186 | /// @arg key - the architecture and disassembly syntax for the |
| 187 | /// disassembler |
| 188 | EDDisassembler(CPUKey& key); |
| 189 | |
| 190 | /// valid - reports whether there was a failure in the constructor. |
| 191 | bool valid() { |
| 192 | return Valid; |
| 193 | } |
| 194 | |
Sean Callanan | 8f993b8 | 2010-04-08 00:48:21 +0000 | [diff] [blame] | 195 | /// hasSemantics - reports whether the disassembler can provide operands and |
| 196 | /// tokens. |
| 197 | bool hasSemantics() { |
| 198 | return HasSemantics; |
| 199 | } |
| 200 | |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 201 | ~EDDisassembler(); |
| 202 | |
| 203 | /// createInst - creates and returns an instruction given a callback and |
| 204 | /// memory address, or NULL on failure |
| 205 | /// |
| 206 | /// @arg byteReader - A callback function that provides machine code bytes |
| 207 | /// @arg address - The address of the first byte of the instruction, |
| 208 | /// suitable for passing to byteReader |
| 209 | /// @arg arg - An opaque argument for byteReader |
| 210 | EDInst *createInst(EDByteReaderCallback byteReader, |
| 211 | uint64_t address, |
| 212 | void *arg); |
| 213 | |
| 214 | /// initMaps - initializes regVec and regRMap using the provided register |
| 215 | /// info |
| 216 | /// |
| 217 | /// @arg registerInfo - the register information to use as a source |
| 218 | void initMaps(const llvm::TargetRegisterInfo ®isterInfo); |
| 219 | /// nameWithRegisterID - Returns the name (owned by the EDDisassembler) of a |
| 220 | /// register for a given register ID, or NULL on failure |
| 221 | /// |
| 222 | /// @arg registerID - the ID of the register to be queried |
| 223 | const char *nameWithRegisterID(unsigned registerID) const; |
| 224 | /// registerIDWithName - Returns the ID of a register for a given register |
| 225 | /// name, or (unsigned)-1 on failure |
| 226 | /// |
| 227 | /// @arg name - The name of the register |
| 228 | unsigned registerIDWithName(const char *name) const; |
| 229 | |
| 230 | /// registerIsStackPointer - reports whether a register ID is an alias for the |
| 231 | /// stack pointer register |
| 232 | /// |
| 233 | /// @arg registerID - The LLVM register ID |
| 234 | bool registerIsStackPointer(unsigned registerID); |
| 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 registerIsProgramCounter(unsigned registerID); |
| 240 | |
| 241 | /// printInst - prints an MCInst to a string, returning 0 on success, or -1 |
| 242 | /// otherwise |
| 243 | /// |
| 244 | /// @arg str - A reference to a string which is filled in with the string |
| 245 | /// representation of the instruction |
| 246 | /// @arg inst - A reference to the MCInst to be printed |
| 247 | int printInst(std::string& str, |
| 248 | llvm::MCInst& inst); |
| 249 | |
| 250 | /// parseInst - extracts operands and tokens from a string for use in |
| 251 | /// tokenizing the string. Returns 0 on success, or -1 otherwise. |
| 252 | /// |
| 253 | /// @arg operands - A reference to a vector that will be filled in with the |
| 254 | /// parsed operands |
| 255 | /// @arg tokens - A reference to a vector that will be filled in with the |
| 256 | /// tokens |
| 257 | /// @arg str - The string representation of the instruction |
| 258 | int parseInst(llvm::SmallVectorImpl<llvm::MCParsedAsmOperand*> &operands, |
| 259 | llvm::SmallVectorImpl<llvm::AsmToken> &tokens, |
| 260 | const std::string &str); |
| 261 | |
| 262 | /// llvmSyntaxVariant - returns the LLVM syntax variant for this disassembler |
| 263 | int llvmSyntaxVariant() const; |
| 264 | }; |
| 265 | |
Chris Lattner | 847da55 | 2010-07-20 18:25:19 +0000 | [diff] [blame] | 266 | } // end namespace llvm |
| 267 | |
Sean Callanan | ee5dfd4 | 2010-02-01 08:49:35 +0000 | [diff] [blame] | 268 | #endif |