]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | //===-- llvm-dis.cpp - The low-level LLVM 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 utility may be invoked in the following manner: | |
11 | // llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout | |
12 | // llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm | |
13 | // to the x.ll file. | |
14 | // Options: | |
15 | // --help - Output information about command line switches | |
16 | // | |
17 | //===----------------------------------------------------------------------===// | |
18 | ||
970d7e83 | 19 | #include "llvm/IR/LLVMContext.h" |
970d7e83 | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
1a4d82fc JJ |
21 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
22 | #include "llvm/IR/DebugInfo.h" | |
85aaf69f SL |
23 | #include "llvm/IR/DiagnosticInfo.h" |
24 | #include "llvm/IR/DiagnosticPrinter.h" | |
970d7e83 LB |
25 | #include "llvm/IR/IntrinsicInst.h" |
26 | #include "llvm/IR/Module.h" | |
27 | #include "llvm/IR/Type.h" | |
223e47cc LB |
28 | #include "llvm/Support/CommandLine.h" |
29 | #include "llvm/Support/DataStream.h" | |
1a4d82fc | 30 | #include "llvm/Support/FileSystem.h" |
223e47cc LB |
31 | #include "llvm/Support/FormattedStream.h" |
32 | #include "llvm/Support/ManagedStatic.h" | |
33 | #include "llvm/Support/MemoryBuffer.h" | |
34 | #include "llvm/Support/PrettyStackTrace.h" | |
223e47cc | 35 | #include "llvm/Support/Signals.h" |
970d7e83 | 36 | #include "llvm/Support/ToolOutputFile.h" |
1a4d82fc | 37 | #include <system_error> |
223e47cc LB |
38 | using namespace llvm; |
39 | ||
40 | static cl::opt<std::string> | |
41 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); | |
42 | ||
43 | static cl::opt<std::string> | |
44 | OutputFilename("o", cl::desc("Override output filename"), | |
45 | cl::value_desc("filename")); | |
46 | ||
47 | static cl::opt<bool> | |
48 | Force("f", cl::desc("Enable binary output on terminals")); | |
49 | ||
50 | static cl::opt<bool> | |
51 | DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); | |
52 | ||
53 | static cl::opt<bool> | |
54 | ShowAnnotations("show-annotations", | |
55 | cl::desc("Add informational comments to the .ll file")); | |
56 | ||
57 | namespace { | |
58 | ||
59 | static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) { | |
60 | OS << DL.getLine() << ":" << DL.getCol(); | |
61 | if (MDNode *N = DL.getInlinedAt(getGlobalContext())) { | |
62 | DebugLoc IDL = DebugLoc::getFromDILocation(N); | |
63 | if (!IDL.isUnknown()) { | |
64 | OS << "@"; | |
65 | printDebugLoc(IDL,OS); | |
66 | } | |
67 | } | |
68 | } | |
69 | class CommentWriter : public AssemblyAnnotationWriter { | |
70 | public: | |
71 | void emitFunctionAnnot(const Function *F, | |
1a4d82fc | 72 | formatted_raw_ostream &OS) override { |
223e47cc LB |
73 | OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses |
74 | OS << '\n'; | |
75 | } | |
1a4d82fc | 76 | void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { |
223e47cc LB |
77 | bool Padded = false; |
78 | if (!V.getType()->isVoidTy()) { | |
79 | OS.PadToColumn(50); | |
80 | Padded = true; | |
81 | OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; // Output # uses and type | |
82 | } | |
83 | if (const Instruction *I = dyn_cast<Instruction>(&V)) { | |
84 | const DebugLoc &DL = I->getDebugLoc(); | |
85 | if (!DL.isUnknown()) { | |
86 | if (!Padded) { | |
87 | OS.PadToColumn(50); | |
88 | Padded = true; | |
89 | OS << ";"; | |
90 | } | |
91 | OS << " [debug line = "; | |
92 | printDebugLoc(DL,OS); | |
93 | OS << "]"; | |
94 | } | |
95 | if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { | |
96 | DIVariable Var(DDI->getVariable()); | |
97 | if (!Padded) { | |
98 | OS.PadToColumn(50); | |
99 | OS << ";"; | |
100 | } | |
101 | OS << " [debug variable = " << Var.getName() << "]"; | |
102 | } | |
103 | else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { | |
104 | DIVariable Var(DVI->getVariable()); | |
105 | if (!Padded) { | |
106 | OS.PadToColumn(50); | |
107 | OS << ";"; | |
108 | } | |
109 | OS << " [debug variable = " << Var.getName() << "]"; | |
110 | } | |
111 | } | |
112 | } | |
113 | }; | |
114 | ||
115 | } // end anon namespace | |
116 | ||
85aaf69f SL |
117 | static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) { |
118 | assert(DI.getSeverity() == DS_Error && "Only expecting errors"); | |
119 | ||
120 | raw_ostream &OS = errs(); | |
121 | OS << (char *)Context << ": "; | |
122 | DiagnosticPrinterRawOStream DP(OS); | |
123 | DI.print(DP); | |
124 | OS << '\n'; | |
125 | exit(1); | |
126 | } | |
127 | ||
223e47cc LB |
128 | int main(int argc, char **argv) { |
129 | // Print a stack trace if we signal out. | |
130 | sys::PrintStackTraceOnErrorSignal(); | |
131 | PrettyStackTraceProgram X(argc, argv); | |
132 | ||
133 | LLVMContext &Context = getGlobalContext(); | |
134 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. | |
135 | ||
85aaf69f | 136 | Context.setDiagnosticHandler(diagnosticHandler, argv[0]); |
223e47cc LB |
137 | |
138 | cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); | |
139 | ||
140 | std::string ErrorMessage; | |
1a4d82fc | 141 | std::unique_ptr<Module> M; |
223e47cc LB |
142 | |
143 | // Use the bitcode streaming interface | |
85aaf69f SL |
144 | DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage); |
145 | if (Streamer) { | |
223e47cc LB |
146 | std::string DisplayFilename; |
147 | if (InputFilename == "-") | |
148 | DisplayFilename = "<stdin>"; | |
149 | else | |
150 | DisplayFilename = InputFilename; | |
85aaf69f SL |
151 | ErrorOr<std::unique_ptr<Module>> MOrErr = |
152 | getStreamedBitcodeModule(DisplayFilename, Streamer, Context); | |
153 | M = std::move(*MOrErr); | |
154 | M->materializeAllPermanently(); | |
223e47cc LB |
155 | } |
156 | ||
157 | // Just use stdout. We won't actually print anything on it. | |
158 | if (DontPrint) | |
159 | OutputFilename = "-"; | |
160 | ||
161 | if (OutputFilename.empty()) { // Unspecified output, infer it. | |
162 | if (InputFilename == "-") { | |
163 | OutputFilename = "-"; | |
164 | } else { | |
165 | const std::string &IFN = InputFilename; | |
166 | int Len = IFN.length(); | |
167 | // If the source ends in .bc, strip it off. | |
168 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') | |
169 | OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll"; | |
170 | else | |
171 | OutputFilename = IFN+".ll"; | |
172 | } | |
173 | } | |
174 | ||
1a4d82fc JJ |
175 | std::error_code EC; |
176 | std::unique_ptr<tool_output_file> Out( | |
177 | new tool_output_file(OutputFilename, EC, sys::fs::F_None)); | |
178 | if (EC) { | |
179 | errs() << EC.message() << '\n'; | |
223e47cc LB |
180 | return 1; |
181 | } | |
182 | ||
1a4d82fc | 183 | std::unique_ptr<AssemblyAnnotationWriter> Annotator; |
223e47cc LB |
184 | if (ShowAnnotations) |
185 | Annotator.reset(new CommentWriter()); | |
186 | ||
187 | // All that llvm-dis does is write the assembly to a file. | |
188 | if (!DontPrint) | |
189 | M->print(Out->os(), Annotator.get()); | |
190 | ||
191 | // Declare success. | |
192 | Out->keep(); | |
193 | ||
194 | return 0; | |
195 | } |