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