fix typo
[oota-llvm.git] / lib / System / Disassembler.cpp
1 //===- lib/System/Disassembler.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Anton Korobeynikov and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the necessary glue to call external disassembler
11 // libraries.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h"
16 #include "llvm/System/Disassembler.h"
17
18 #include <cassert>
19 #include <iomanip>
20 #include <string>
21 #include <sstream>
22
23 #if USE_UDIS86
24 #include <udis86.h>
25 #endif
26
27 using namespace llvm;
28
29 std::string llvm::sys::disassembleBuffer(uint8_t* start, size_t length,
30                                          uint64_t pc) {
31   std::stringstream res;
32
33 #if defined (__i386__) || defined (__amd64__) || defined (__x86_64__)
34   unsigned bits;
35 # if defined(__i386__)
36   bits = 32;
37 # else
38   bits = 64;
39 # endif
40   
41 # if USE_UDIS86
42   ud_t ud_obj;
43    
44   ud_init(&ud_obj);
45   ud_set_input_buffer(&ud_obj, start, length);
46   ud_set_mode(&ud_obj, bits);
47   ud_set_pc(&ud_obj, pc);
48   ud_set_syntax(&ud_obj, UD_SYN_ATT);
49   
50   res << std::setbase(16)
51       << std::setw(bits/4);
52   
53   while (ud_disassemble(&ud_obj)) {
54     res << ud_insn_off(&ud_obj) << ":\t" << ud_insn_asm(&ud_obj) << "\n";
55   }
56 # else
57   res << "No disassembler available. See configure help for options.\n";
58 # endif
59   
60 #else
61   res << "No disassembler available. See configure help for options.\n";
62 #endif
63
64   return res.str();
65 }