Fixed some bugs.
[llvm/zpu.git] / lib / MC / MCDisassembler / EDDisassembler.cpp
blob697b3d9c05153c49bd44288f5cc54fc68bc3cc4a
1 //===-EDDisassembler.cpp - LLVM Enhanced Disassembler ---------------------===//
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 implements the Enhanced Disassembly library's disassembler class.
11 // The disassembler is responsible for vending individual instructions according
12 // to a given architecture and disassembly syntax.
14 //===----------------------------------------------------------------------===//
16 #include "EDDisassembler.h"
17 #include "EDInst.h"
18 #include "llvm/MC/EDInstInfo.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCDisassembler.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstPrinter.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCParser/AsmLexer.h"
27 #include "llvm/MC/MCParser/MCAsmParser.h"
28 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/MemoryObject.h"
31 #include "llvm/Support/SourceMgr.h"
32 #include "llvm/Target/TargetAsmLexer.h"
33 #include "llvm/Target/TargetAsmParser.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetRegisterInfo.h"
37 #include "llvm/Target/TargetSelect.h"
38 using namespace llvm;
40 bool EDDisassembler::sInitialized = false;
41 EDDisassembler::DisassemblerMap_t EDDisassembler::sDisassemblers;
43 struct TripleMap {
44 Triple::ArchType Arch;
45 const char *String;
48 static struct TripleMap triplemap[] = {
49 { Triple::x86, "i386-unknown-unknown" },
50 { Triple::x86_64, "x86_64-unknown-unknown" },
51 { Triple::arm, "arm-unknown-unknown" },
52 { Triple::thumb, "thumb-unknown-unknown" },
53 { Triple::InvalidArch, NULL, }
56 /// infoFromArch - Returns the TripleMap corresponding to a given architecture,
57 /// or NULL if there is an error
58 ///
59 /// @arg arch - The Triple::ArchType for the desired architecture
60 static const char *tripleFromArch(Triple::ArchType arch) {
61 unsigned int infoIndex;
63 for (infoIndex = 0; triplemap[infoIndex].String != NULL; ++infoIndex) {
64 if (arch == triplemap[infoIndex].Arch)
65 return triplemap[infoIndex].String;
68 return NULL;
71 /// getLLVMSyntaxVariant - gets the constant to use to get an assembly printer
72 /// for the desired assembly syntax, suitable for passing to
73 /// Target::createMCInstPrinter()
74 ///
75 /// @arg arch - The target architecture
76 /// @arg syntax - The assembly syntax in sd form
77 static int getLLVMSyntaxVariant(Triple::ArchType arch,
78 EDDisassembler::AssemblySyntax syntax) {
79 switch (syntax) {
80 default:
81 return -1;
82 // Mappings below from X86AsmPrinter.cpp
83 case EDDisassembler::kEDAssemblySyntaxX86ATT:
84 if (arch == Triple::x86 || arch == Triple::x86_64)
85 return 0;
86 else
87 return -1;
88 case EDDisassembler::kEDAssemblySyntaxX86Intel:
89 if (arch == Triple::x86 || arch == Triple::x86_64)
90 return 1;
91 else
92 return -1;
93 case EDDisassembler::kEDAssemblySyntaxARMUAL:
94 if (arch == Triple::arm || arch == Triple::thumb)
95 return 0;
96 else
97 return -1;
101 void EDDisassembler::initialize() {
102 if (sInitialized)
103 return;
105 sInitialized = true;
107 InitializeAllTargetInfos();
108 InitializeAllTargets();
109 InitializeAllAsmPrinters();
110 InitializeAllAsmParsers();
111 InitializeAllDisassemblers();
114 #undef BRINGUP_TARGET
116 EDDisassembler *EDDisassembler::getDisassembler(Triple::ArchType arch,
117 AssemblySyntax syntax) {
118 CPUKey key;
119 key.Arch = arch;
120 key.Syntax = syntax;
122 EDDisassembler::DisassemblerMap_t::iterator i = sDisassemblers.find(key);
124 if (i != sDisassemblers.end()) {
125 return i->second;
126 } else {
127 EDDisassembler* sdd = new EDDisassembler(key);
128 if (!sdd->valid()) {
129 delete sdd;
130 return NULL;
133 sDisassemblers[key] = sdd;
135 return sdd;
138 return NULL;
141 EDDisassembler *EDDisassembler::getDisassembler(StringRef str,
142 AssemblySyntax syntax) {
143 return getDisassembler(Triple(str).getArch(), syntax);
146 EDDisassembler::EDDisassembler(CPUKey &key) :
147 Valid(false),
148 HasSemantics(false),
149 ErrorStream(nulls()),
150 Key(key) {
151 const char *triple = tripleFromArch(key.Arch);
153 if (!triple)
154 return;
156 LLVMSyntaxVariant = getLLVMSyntaxVariant(key.Arch, key.Syntax);
158 if (LLVMSyntaxVariant < 0)
159 return;
161 std::string tripleString(triple);
162 std::string errorString;
164 Tgt = TargetRegistry::lookupTarget(tripleString,
165 errorString);
167 if (!Tgt)
168 return;
170 std::string featureString;
172 TargetMachine.reset(Tgt->createTargetMachine(tripleString,
173 featureString));
175 const TargetRegisterInfo *registerInfo = TargetMachine->getRegisterInfo();
177 if (!registerInfo)
178 return;
180 initMaps(*registerInfo);
182 AsmInfo.reset(Tgt->createAsmInfo(tripleString));
184 if (!AsmInfo)
185 return;
187 Disassembler.reset(Tgt->createMCDisassembler());
189 if (!Disassembler)
190 return;
192 InstInfos = Disassembler->getEDInfo();
194 InstString.reset(new std::string);
195 InstStream.reset(new raw_string_ostream(*InstString));
196 InstPrinter.reset(Tgt->createMCInstPrinter(LLVMSyntaxVariant, *AsmInfo));
198 if (!InstPrinter)
199 return;
201 GenericAsmLexer.reset(new AsmLexer(*AsmInfo));
202 SpecificAsmLexer.reset(Tgt->createAsmLexer(*AsmInfo));
203 SpecificAsmLexer->InstallLexer(*GenericAsmLexer);
205 initMaps(*TargetMachine->getRegisterInfo());
207 Valid = true;
210 EDDisassembler::~EDDisassembler() {
211 if (!valid())
212 return;
215 namespace {
216 /// EDMemoryObject - a subclass of MemoryObject that allows use of a callback
217 /// as provided by the sd interface. See MemoryObject.
218 class EDMemoryObject : public llvm::MemoryObject {
219 private:
220 EDByteReaderCallback Callback;
221 void *Arg;
222 public:
223 EDMemoryObject(EDByteReaderCallback callback,
224 void *arg) : Callback(callback), Arg(arg) { }
225 ~EDMemoryObject() { }
226 uint64_t getBase() const { return 0x0; }
227 uint64_t getExtent() const { return (uint64_t)-1; }
228 int readByte(uint64_t address, uint8_t *ptr) const {
229 if (!Callback)
230 return -1;
232 if (Callback(ptr, address, Arg))
233 return -1;
235 return 0;
240 EDInst *EDDisassembler::createInst(EDByteReaderCallback byteReader,
241 uint64_t address,
242 void *arg) {
243 EDMemoryObject memoryObject(byteReader, arg);
245 MCInst* inst = new MCInst;
246 uint64_t byteSize;
248 if (!Disassembler->getInstruction(*inst,
249 byteSize,
250 memoryObject,
251 address,
252 ErrorStream)) {
253 delete inst;
254 return NULL;
255 } else {
256 const llvm::EDInstInfo *thisInstInfo;
258 thisInstInfo = &InstInfos[inst->getOpcode()];
260 EDInst* sdInst = new EDInst(inst, byteSize, *this, thisInstInfo);
261 return sdInst;
265 void EDDisassembler::initMaps(const TargetRegisterInfo &registerInfo) {
266 unsigned numRegisters = registerInfo.getNumRegs();
267 unsigned registerIndex;
269 for (registerIndex = 0; registerIndex < numRegisters; ++registerIndex) {
270 const char* registerName = registerInfo.get(registerIndex).Name;
272 RegVec.push_back(registerName);
273 RegRMap[registerName] = registerIndex;
276 switch (Key.Arch) {
277 default:
278 break;
279 case Triple::x86:
280 case Triple::x86_64:
281 stackPointers.insert(registerIDWithName("SP"));
282 stackPointers.insert(registerIDWithName("ESP"));
283 stackPointers.insert(registerIDWithName("RSP"));
285 programCounters.insert(registerIDWithName("IP"));
286 programCounters.insert(registerIDWithName("EIP"));
287 programCounters.insert(registerIDWithName("RIP"));
288 break;
289 case Triple::arm:
290 case Triple::thumb:
291 stackPointers.insert(registerIDWithName("SP"));
293 programCounters.insert(registerIDWithName("PC"));
294 break;
298 const char *EDDisassembler::nameWithRegisterID(unsigned registerID) const {
299 if (registerID >= RegVec.size())
300 return NULL;
301 else
302 return RegVec[registerID].c_str();
305 unsigned EDDisassembler::registerIDWithName(const char *name) const {
306 regrmap_t::const_iterator iter = RegRMap.find(std::string(name));
307 if (iter == RegRMap.end())
308 return 0;
309 else
310 return (*iter).second;
313 bool EDDisassembler::registerIsStackPointer(unsigned registerID) {
314 return (stackPointers.find(registerID) != stackPointers.end());
317 bool EDDisassembler::registerIsProgramCounter(unsigned registerID) {
318 return (programCounters.find(registerID) != programCounters.end());
321 int EDDisassembler::printInst(std::string &str, MCInst &inst) {
322 PrinterMutex.acquire();
324 InstPrinter->printInst(&inst, *InstStream);
325 InstStream->flush();
326 str = *InstString;
327 InstString->clear();
329 PrinterMutex.release();
331 return 0;
334 int EDDisassembler::parseInst(SmallVectorImpl<MCParsedAsmOperand*> &operands,
335 SmallVectorImpl<AsmToken> &tokens,
336 const std::string &str) {
337 int ret = 0;
339 switch (Key.Arch) {
340 default:
341 return -1;
342 case Triple::x86:
343 case Triple::x86_64:
344 case Triple::arm:
345 case Triple::thumb:
346 break;
349 const char *cStr = str.c_str();
350 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(cStr, cStr + strlen(cStr));
352 StringRef instName;
353 SMLoc instLoc;
355 SourceMgr sourceMgr;
356 sourceMgr.AddNewSourceBuffer(buf, SMLoc()); // ownership of buf handed over
357 MCContext context(*AsmInfo);
358 OwningPtr<MCStreamer> streamer(createNullStreamer(context));
359 OwningPtr<MCAsmParser> genericParser(createMCAsmParser(*Tgt, sourceMgr,
360 context, *streamer,
361 *AsmInfo));
362 OwningPtr<TargetAsmParser> TargetParser(Tgt->createAsmParser(*genericParser,
363 *TargetMachine));
365 AsmToken OpcodeToken = genericParser->Lex();
366 AsmToken NextToken = genericParser->Lex(); // consume next token, because specificParser expects us to
368 if (OpcodeToken.is(AsmToken::Identifier)) {
369 instName = OpcodeToken.getString();
370 instLoc = OpcodeToken.getLoc();
372 if (NextToken.isNot(AsmToken::Eof) &&
373 TargetParser->ParseInstruction(instName, instLoc, operands))
374 ret = -1;
375 } else {
376 ret = -1;
379 ParserMutex.acquire();
381 if (!ret) {
382 GenericAsmLexer->setBuffer(buf);
384 while (SpecificAsmLexer->Lex(),
385 SpecificAsmLexer->isNot(AsmToken::Eof) &&
386 SpecificAsmLexer->isNot(AsmToken::EndOfStatement)) {
387 if (SpecificAsmLexer->is(AsmToken::Error)) {
388 ret = -1;
389 break;
391 tokens.push_back(SpecificAsmLexer->getTok());
395 ParserMutex.release();
397 return ret;
400 int EDDisassembler::llvmSyntaxVariant() const {
401 return LLVMSyntaxVariant;