If you thought that it didn't make sense for the disassembler
[oota-llvm.git] / tools / llvm-mc / HexDisassembler.cpp
1 //===- HexDisassembler.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 "HexDisassembler.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
28 using namespace llvm;
29
30 class VectorMemoryObject : public MemoryObject {
31 private:
32   const std::vector<unsigned char> &Bytes;
33 public:
34   VectorMemoryObject(const std::vector<unsigned char> &bytes) : 
35     Bytes(bytes) {
36   }
37   
38   uint64_t getBase() const {
39     return 0;
40   }
41   
42   uint64_t getExtent() const {
43     return Bytes.size();
44   }
45
46   int readByte(uint64_t addr, uint8_t *byte) const {
47     if (addr > getExtent())
48       return -1;
49     *byte = Bytes[addr];
50     return 0;
51   }
52 };
53
54 void printInst(const llvm::MCDisassembler &disassembler,
55                llvm::MCInstPrinter &instPrinter,
56                const std::vector<unsigned char> &bytes) {
57   // Wrap the vector in a MemoryObject.
58   VectorMemoryObject memoryObject(bytes);
59   
60   // Disassemble it.
61   MCInst inst;
62   uint64_t size;
63   
64   std::string verboseOStr;
65   llvm::raw_string_ostream verboseOS(verboseOStr); 
66   
67   if (disassembler.getInstruction(inst, size, memoryObject, 0, verboseOS)) {
68     instPrinter.printInst(&inst);
69     outs() << "\n";
70   } else {
71     errs() << "error: invalid instruction" << "\n";
72     errs() << "Diagnostic log:" << "\n";
73     errs() << verboseOStr.c_str() << "\n";
74   }
75 }
76
77 int HexDisassembler::disassemble(const Target &T, const std::string &Triple,
78                                  MemoryBuffer &Buffer) {
79   // Set up disassembler.
80   llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
81   
82   if (!AsmInfo) {
83     errs() << "error: no assembly info for target " << Triple << "\n";
84     return -1;
85   }
86   
87   llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
88   if (!DisAsm) {
89     errs() << "error: no disassembler for target " << Triple << "\n";
90     return -1;
91   }
92   
93   llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
94   
95   if (!InstPrinter) {
96     errs() << "error: no instruction printer for target " << Triple
97       << "\n";
98     return -1;
99   }
100   
101   SourceMgr SourceManager;
102   SourceManager.AddNewSourceBuffer(&Buffer, SMLoc());
103   
104   // Convert the input to a vector for disassembly.
105   std::vector<unsigned char> ByteArray;
106   
107   StringRef Str = Buffer.getBuffer();
108   while (!Str.empty()) {
109     // Strip horizontal whitespace.
110     if (size_t Pos = Str.find_first_not_of(" \t\r")) {
111       Str = Str.substr(Pos);
112       continue;
113     }
114     
115     // If this is the end of a line or start of a comment, process the
116     // instruction we have so far.
117     if (Str[0] == '\n' || Str[0] == '#') {
118       // If we have bytes to process, do so.
119       if (!ByteArray.empty()) {
120         printInst(*DisAsm, *InstPrinter, ByteArray);
121         ByteArray.clear();
122       }
123       
124       // Strip to the end of line if we already processed any bytes on this
125       // line.  This strips the comment and/or the \n.
126       if (Str[0] == '\n')
127         Str = Str.substr(1);
128       else {
129         Str = Str.substr(Str.find_first_of('\n'));
130         if (!Str.empty())
131           Str = Str.substr(1);
132       }
133       continue;
134     }
135     
136     // Get the current token.
137     size_t Next = Str.find_first_of(" \t\n\r#");
138     StringRef Value = Str.substr(0, Next);
139     
140     // Convert to a byte and add to the byte vector.
141     unsigned ByteVal;
142     if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
143       // If we have an error, print it and skip to the end of line.
144       SourceManager.PrintMessage(SMLoc::getFromPointer(Value.data()),
145                                  "invalid input token", "error");
146       Str = Str.substr(Str.find('\n'));
147       ByteArray.clear();
148       continue;
149     }
150     
151     ByteArray.push_back((unsigned char)ByteVal);
152     Str = Str.substr(Next);
153   }
154   
155   if (!ByteArray.empty())
156     printInst(*DisAsm, *InstPrinter, ByteArray);
157     
158   return 0;
159 }