5fde71238dc93bcd9a2647541aa2ae2e7e769c39
[oota-llvm.git] / tools / llvm-mc / Disassembler.cpp
1 //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
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 class implements the disassembler of strings of bytes written in
11 // hexadecimal, from standard input or from a file.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "Disassembler.h"
16
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstPrinter.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/MemoryObject.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/SourceMgr.h"
27 using namespace llvm;
28
29 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
30
31 namespace {
32 class VectorMemoryObject : public MemoryObject {
33 private:
34   const ByteArrayTy &Bytes;
35 public:
36   VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
37   
38   uint64_t getBase() const { return 0; }
39   uint64_t getExtent() const { return Bytes.size(); }
40
41   int readByte(uint64_t Addr, uint8_t *Byte) const {
42     if (Addr > getExtent())
43       return -1;
44     *Byte = Bytes[Addr].first;
45     return 0;
46   }
47 };
48 }
49
50 static bool PrintInst(const llvm::MCDisassembler &DisAsm,
51                       llvm::MCInstPrinter &Printer, const ByteArrayTy &Bytes,
52                       SourceMgr &SM) {
53   // Wrap the vector in a MemoryObject.
54   VectorMemoryObject memoryObject(Bytes);
55   
56   // Disassemble it to a string and get the size of the instruction.
57   MCInst Inst;
58   uint64_t Size;
59   
60   if (!DisAsm.getInstruction(Inst, Size, memoryObject, 0, 
61                              /*REMOVE*/ nulls())) {
62     SM.PrintMessage(SMLoc::getFromPointer(Bytes[0].second),
63                     "invalid instruction encoding", "error");
64     return true;
65   }
66   
67   Printer.printInst(&Inst);
68   outs() << "\n";
69   
70   // If the disassembled instruction was smaller than the number of bytes we
71   // read, reject the excess bytes.
72   if (Bytes.size() != Size) {
73     SM.PrintMessage(SMLoc::getFromPointer(Bytes[Size].second),
74                     "excess data detected in input", "error");
75     return true;
76   }
77   
78   return false;
79 }
80
81 int Disassembler::disassemble(const Target &T, const std::string &Triple,
82                                  MemoryBuffer &Buffer) {
83   // Set up disassembler.
84   llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
85   
86   if (!AsmInfo) {
87     errs() << "error: no assembly info for target " << Triple << "\n";
88     return -1;
89   }
90   
91   llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
92   if (!DisAsm) {
93     errs() << "error: no disassembler for target " << Triple << "\n";
94     return -1;
95   }
96   
97   llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
98   
99   if (!InstPrinter) {
100     errs() << "error: no instruction printer for target " << Triple << '\n';
101     return -1;
102   }
103   
104   bool ErrorOccurred = false;
105   
106   SourceMgr SM;
107   SM.AddNewSourceBuffer(&Buffer, SMLoc());
108   
109   // Convert the input to a vector for disassembly.
110   ByteArrayTy ByteArray;
111   
112   StringRef Str = Buffer.getBuffer();
113   while (!Str.empty()) {
114     // Strip horizontal whitespace.
115     if (size_t Pos = Str.find_first_not_of(" \t\r")) {
116       Str = Str.substr(Pos);
117       continue;
118     }
119     
120     // If this is the end of a line or start of a comment, process the
121     // instruction we have so far.
122     if (Str[0] == '\n' || Str[0] == '#') {
123       // If we have bytes to process, do so.
124       if (!ByteArray.empty()) {
125         ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
126         ByteArray.clear();
127       }
128       
129       // Strip to the end of line if we already processed any bytes on this
130       // line.  This strips the comment and/or the \n.
131       if (Str[0] == '\n')
132         Str = Str.substr(1);
133       else {
134         Str = Str.substr(Str.find_first_of('\n'));
135         if (!Str.empty())
136           Str = Str.substr(1);
137       }
138       continue;
139     }
140     
141     // Get the current token.
142     size_t Next = Str.find_first_of(" \t\n\r#");
143     StringRef Value = Str.substr(0, Next);
144     
145     // Convert to a byte and add to the byte vector.
146     unsigned ByteVal;
147     if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
148       // If we have an error, print it and skip to the end of line.
149       SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
150                                  "invalid input token", "error");
151       ErrorOccurred = true;
152       Str = Str.substr(Str.find('\n'));
153       ByteArray.clear();
154       continue;
155     }
156     
157     ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
158     Str = Str.substr(Next);
159   }
160   
161   if (!ByteArray.empty())
162     ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
163     
164   return ErrorOccurred;
165 }