Include MCRegisterInfo to eliminate a compilation warning.
[oota-llvm.git] / lib / MC / MCDisassembler / Disassembler.cpp
1 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface -*- 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 #include "Disassembler.h"
11 #include "llvm-c/Disassembler.h"
12
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDisassembler.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/Target/TargetRegistry.h"
20 #include "llvm/Target/TargetAsmInfo.h"  // FIXME.
21 #include "llvm/Target/TargetMachine.h"  // FIXME.
22 #include "llvm/Target/TargetSelect.h"
23 #include "llvm/Support/MemoryObject.h"
24
25 namespace llvm {
26 class Target;
27 } // namespace llvm
28 using namespace llvm;
29
30 // LLVMCreateDisasm() creates a disassembler for the TripleName.  Symbolic
31 // disassembly is supported by passing a block of information in the DisInfo
32 // parameter and specifying the TagType and callback functions as described in
33 // the header llvm-c/Disassembler.h .  The pointer to the block and the 
34 // functions can all be passed as NULL.  If successful, this returns a
35 // disassembler context.  If not, it returns NULL.
36 //
37 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
38                                       int TagType, LLVMOpInfoCallback GetOpInfo,
39                                       LLVMSymbolLookupCallback SymbolLookUp) {
40   // Initialize targets and assembly printers/parsers.
41   llvm::InitializeAllTargetInfos();
42   // FIXME: We shouldn't need to initialize the Target(Machine)s.
43   llvm::InitializeAllTargets();
44   llvm::InitializeAllMCAsmInfos();
45   llvm::InitializeAllMCCodeGenInfos();
46   llvm::InitializeAllMCRegisterInfos();
47   llvm::InitializeAllAsmPrinters();
48   llvm::InitializeAllAsmParsers();
49   llvm::InitializeAllDisassemblers();
50
51   // Get the target.
52   std::string Error;
53   const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
54   assert(TheTarget && "Unable to create target!");
55
56   // Get the assembler info needed to setup the MCContext.
57   const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
58   assert(MAI && "Unable to create target asm info!");
59
60   const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
61   assert(MRI && "Unable to create target register info!");
62
63   // Package up features to be passed to target/subtarget
64   std::string FeaturesStr;
65   std::string CPU;
66
67   // FIXME: We shouldn't need to do this (and link in codegen).
68   //        When we split this out, we should do it in a way that makes
69   //        it straightforward to switch subtargets on the fly.
70   TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU,
71                                                      FeaturesStr);
72   assert(TM && "Unable to create target machine!");
73
74   // Get the target assembler info needed to setup the context.
75   const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
76   assert(tai && "Unable to create target assembler!");
77
78   // Set up the MCContext for creating symbols and MCExpr's.
79   MCContext *Ctx = new MCContext(*MAI, *MRI, 0, tai);
80   assert(Ctx && "Unable to create MCContext!");
81
82   // Set up disassembler.
83   MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
84   assert(DisAsm && "Unable to create disassembler!");
85   DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
86
87   // Set up the instruction printer.
88   int AsmPrinterVariant = MAI->getAssemblerDialect();
89   MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
90                                                      *MAI);
91   assert(IP && "Unable to create instruction printer!");
92
93   LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
94                                                 GetOpInfo, SymbolLookUp,
95                                                 TheTarget, MAI, MRI, TM, tai,
96                                                 Ctx, DisAsm, IP);
97   assert(DC && "Allocation failure!");
98   return DC;
99 }
100
101 //
102 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
103 //
104 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
105   LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
106   delete DC;
107 }
108
109 namespace {
110 //
111 // The memory object created by LLVMDisasmInstruction().
112 //
113 class DisasmMemoryObject : public MemoryObject {
114   uint8_t *Bytes;
115   uint64_t Size;
116   uint64_t BasePC;
117 public:
118   DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
119                      Bytes(bytes), Size(size), BasePC(basePC) {}
120  
121   uint64_t getBase() const { return BasePC; }
122   uint64_t getExtent() const { return Size; }
123
124   int readByte(uint64_t Addr, uint8_t *Byte) const {
125     if (Addr - BasePC >= Size)
126       return -1;
127     *Byte = Bytes[Addr - BasePC];
128     return 0;
129   }
130 };
131 } // end anonymous namespace
132
133 //
134 // LLVMDisasmInstruction() disassembles a single instruction using the
135 // disassembler context specified in the parameter DC.  The bytes of the
136 // instruction are specified in the parameter Bytes, and contains at least
137 // BytesSize number of bytes.  The instruction is at the address specified by
138 // the PC parameter.  If a valid instruction can be disassembled its string is
139 // returned indirectly in OutString which whos size is specified in the
140 // parameter OutStringSize.  This function returns the number of bytes in the
141 // instruction or zero if there was no valid instruction.  If this function
142 // returns zero the caller will have to pick how many bytes they want to step
143 // over by printing a .byte, .long etc. to continue.
144 //
145 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
146                              uint64_t BytesSize, uint64_t PC, char *OutString,
147                              size_t OutStringSize){
148   LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
149   // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
150   DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
151
152   uint64_t Size;
153   MCInst Inst;
154   const MCDisassembler *DisAsm = DC->getDisAsm();
155   MCInstPrinter *IP = DC->getIP();
156   if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
157     return 0;
158
159   SmallVector<char, 64> InsnStr;
160   raw_svector_ostream OS(InsnStr);
161   IP->printInst(&Inst, OS);
162   OS.flush();
163
164   assert(OutStringSize != 0 && "Output buffer cannot be zero size");
165   size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
166   std::memcpy(OutString, InsnStr.data(), OutputSize);
167   OutString[OutputSize] = '\0'; // Terminate string.
168
169   return Size;
170 }