Missed removing one of the assert()'s from the LLVMCreateDisasmCPU() library
[oota-llvm.git] / lib / MC / MCDisassembler / Disassembler.cpp
1 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
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 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDisassembler.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCInstPrinter.h"
17 #include "llvm/MC/MCInstrInfo.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MemoryObject.h"
22 #include "llvm/Support/TargetRegistry.h"
23
24 namespace llvm {
25 class Target;
26 } // namespace llvm
27 using namespace llvm;
28
29 // LLVMCreateDisasm() creates a disassembler for the TripleName.  Symbolic
30 // disassembly is supported by passing a block of information in the DisInfo
31 // parameter and specifying the TagType and callback functions as described in
32 // the header llvm-c/Disassembler.h .  The pointer to the block and the 
33 // functions can all be passed as NULL.  If successful, this returns a
34 // disassembler context.  If not, it returns NULL.
35 //
36 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
37                                          void *DisInfo, int TagType,
38                                          LLVMOpInfoCallback GetOpInfo,
39                                          LLVMSymbolLookupCallback SymbolLookUp){
40   // Get the target.
41   std::string Error;
42   const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
43   if (!TheTarget)
44     return 0;
45
46   const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
47   if (!MRI)
48     return 0;
49
50   // Get the assembler info needed to setup the MCContext.
51   const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
52   if (!MAI)
53     return 0;
54
55   const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
56   if (!MII)
57     return 0;
58
59   // Package up features to be passed to target/subtarget
60   std::string FeaturesStr;
61
62   const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
63                                                                 FeaturesStr);
64   if (!STI)
65     return 0;
66
67   // Set up the MCContext for creating symbols and MCExpr's.
68   MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
69   if (!Ctx)
70     return 0;
71
72   // Set up disassembler.
73   MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
74   if (!DisAsm)
75     return 0;
76   DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
77
78   // Set up the instruction printer.
79   int AsmPrinterVariant = MAI->getAssemblerDialect();
80   MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
81                                                      *MAI, *MII, *MRI, *STI);
82   if (!IP)
83     return 0;
84
85   LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
86                                                 GetOpInfo, SymbolLookUp,
87                                                 TheTarget, MAI, MRI,
88                                                 STI, MII, Ctx, DisAsm, IP);
89   if (!DC)
90     return 0;
91
92   return DC;
93 }
94
95 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
96                                       int TagType, LLVMOpInfoCallback GetOpInfo,
97                                       LLVMSymbolLookupCallback SymbolLookUp) {
98   return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
99                              SymbolLookUp);
100 }
101
102 //
103 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
104 //
105 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
106   LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
107   delete DC;
108 }
109
110 namespace {
111 //
112 // The memory object created by LLVMDisasmInstruction().
113 //
114 class DisasmMemoryObject : public MemoryObject {
115   uint8_t *Bytes;
116   uint64_t Size;
117   uint64_t BasePC;
118 public:
119   DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
120                      Bytes(bytes), Size(size), BasePC(basePC) {}
121  
122   uint64_t getBase() const { return BasePC; }
123   uint64_t getExtent() const { return Size; }
124
125   int readByte(uint64_t Addr, uint8_t *Byte) const {
126     if (Addr - BasePC >= Size)
127       return -1;
128     *Byte = Bytes[Addr - BasePC];
129     return 0;
130   }
131 };
132 } // end anonymous namespace
133
134 //
135 // LLVMDisasmInstruction() disassembles a single instruction using the
136 // disassembler context specified in the parameter DC.  The bytes of the
137 // instruction are specified in the parameter Bytes, and contains at least
138 // BytesSize number of bytes.  The instruction is at the address specified by
139 // the PC parameter.  If a valid instruction can be disassembled its string is
140 // returned indirectly in OutString which whos size is specified in the
141 // parameter OutStringSize.  This function returns the number of bytes in the
142 // instruction or zero if there was no valid instruction.  If this function
143 // returns zero the caller will have to pick how many bytes they want to step
144 // over by printing a .byte, .long etc. to continue.
145 //
146 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
147                              uint64_t BytesSize, uint64_t PC, char *OutString,
148                              size_t OutStringSize){
149   LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
150   // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
151   DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
152
153   uint64_t Size;
154   MCInst Inst;
155   const MCDisassembler *DisAsm = DC->getDisAsm();
156   MCInstPrinter *IP = DC->getIP();
157   MCDisassembler::DecodeStatus S;
158   S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
159                              /*REMOVE*/ nulls(), DC->CommentStream);
160   switch (S) {
161   case MCDisassembler::Fail:
162   case MCDisassembler::SoftFail:
163     // FIXME: Do something different for soft failure modes?
164     return 0;
165
166   case MCDisassembler::Success: {
167     DC->CommentStream.flush();
168     StringRef Comments = DC->CommentsToEmit.str();
169
170     SmallVector<char, 64> InsnStr;
171     raw_svector_ostream OS(InsnStr);
172     IP->printInst(&Inst, OS, Comments);
173     OS.flush();
174
175     // Tell the comment stream that the vector changed underneath it.
176     DC->CommentsToEmit.clear();
177     DC->CommentStream.resync();
178
179     assert(OutStringSize != 0 && "Output buffer cannot be zero size");
180     size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
181     std::memcpy(OutString, InsnStr.data(), OutputSize);
182     OutString[OutputSize] = '\0'; // Terminate string.
183
184     return Size;
185   }
186   }
187   llvm_unreachable("Invalid DecodeStatus!");
188 }
189
190 //
191 // LLVMSetDisasmOptions() sets the disassembler's options.  It returns 1 if it
192 // can set all the Options and 0 otherwise.
193 //
194 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
195   if (Options & LLVMDisassembler_Option_UseMarkup){
196       LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
197       MCInstPrinter *IP = DC->getIP();
198       IP->setUseMarkup(1);
199       Options &= ~LLVMDisassembler_Option_UseMarkup;
200   }
201   if (Options & LLVMDisassembler_Option_PrintImmHex){
202       LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
203       MCInstPrinter *IP = DC->getIP();
204       IP->setPrintImmHex(1);
205       Options &= ~LLVMDisassembler_Option_PrintImmHex;
206   }
207   if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
208       LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
209       // Try to set up the new instruction printer.
210       const MCAsmInfo *MAI = DC->getAsmInfo();
211       const MCInstrInfo *MII = DC->getInstrInfo();
212       const MCRegisterInfo *MRI = DC->getRegisterInfo();
213       const MCSubtargetInfo *STI = DC->getSubtargetInfo();
214       int AsmPrinterVariant = MAI->getAssemblerDialect();
215       AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
216       MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
217           AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
218       if (IP) {
219         DC->setIP(IP);
220         Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
221       }
222   }
223   return (Options == 0);
224 }