1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 // --help - Output information about command line switches
17 //===----------------------------------------------------------------------===//
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Type.h"
22 #include "llvm/IntrinsicInst.h"
23 #include "llvm/Bitcode/ReaderWriter.h"
24 #include "llvm/Analysis/DebugInfo.h"
25 #include "llvm/Assembly/AssemblyAnnotationWriter.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/ToolOutputFile.h"
32 #include "llvm/Support/Signals.h"
33 #include "llvm/Support/system_error.h"
36 static cl::opt
<std::string
>
37 InputFilename(cl::Positional
, cl::desc("<input bitcode>"), cl::init("-"));
39 static cl::opt
<std::string
>
40 OutputFilename("o", cl::desc("Override output filename"),
41 cl::value_desc("filename"));
44 Force("f", cl::desc("Enable binary output on terminals"));
47 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden
);
50 ShowAnnotations("show-annotations",
51 cl::desc("Add informational comments to the .ll file"));
55 static void printDebugLoc(const DebugLoc
&DL
, formatted_raw_ostream
&OS
) {
56 OS
<< DL
.getLine() << ":" << DL
.getCol();
57 if (MDNode
*N
= DL
.getInlinedAt(getGlobalContext())) {
58 DebugLoc IDL
= DebugLoc::getFromDILocation(N
);
59 if (!IDL
.isUnknown()) {
61 printDebugLoc(IDL
,OS
);
65 class CommentWriter
: public AssemblyAnnotationWriter
{
67 void emitFunctionAnnot(const Function
*F
,
68 formatted_raw_ostream
&OS
) {
69 OS
<< "; [#uses=" << F
->getNumUses() << ']'; // Output # uses
72 void printInfoComment(const Value
&V
, formatted_raw_ostream
&OS
) {
74 if (!V
.getType()->isVoidTy()) {
77 OS
<< "; [#uses=" << V
.getNumUses() << " type=" << *V
.getType() << "]"; // Output # uses and type
79 if (const Instruction
*I
= dyn_cast
<Instruction
>(&V
)) {
80 const DebugLoc
&DL
= I
->getDebugLoc();
81 if (!DL
.isUnknown()) {
87 OS
<< " [debug line = ";
91 if (const DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(I
)) {
92 DIVariable
Var(DDI
->getVariable());
98 OS
<< " [debug variable = " << Var
.getName() << "]";
100 else if (const DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(I
)) {
101 DIVariable
Var(DVI
->getVariable());
107 OS
<< " [debug variable = " << Var
.getName() << "]";
113 } // end anon namespace
115 int main(int argc
, char **argv
) {
116 // Print a stack trace if we signal out.
117 sys::PrintStackTraceOnErrorSignal();
118 PrettyStackTraceProgram
X(argc
, argv
);
120 LLVMContext
&Context
= getGlobalContext();
121 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
124 cl::ParseCommandLineOptions(argc
, argv
, "llvm .bc -> .ll disassembler\n");
126 std::string ErrorMessage
;
127 std::auto_ptr
<Module
> M
;
130 OwningPtr
<MemoryBuffer
> BufferPtr
;
131 if (error_code ec
= MemoryBuffer::getFileOrSTDIN(InputFilename
, BufferPtr
))
132 ErrorMessage
= ec
.message();
134 M
.reset(ParseBitcodeFile(BufferPtr
.get(), Context
, &ErrorMessage
));
138 errs() << argv
[0] << ": ";
139 if (ErrorMessage
.size())
140 errs() << ErrorMessage
<< "\n";
142 errs() << "bitcode didn't read correctly.\n";
146 // Just use stdout. We won't actually print anything on it.
148 OutputFilename
= "-";
150 if (OutputFilename
.empty()) { // Unspecified output, infer it.
151 if (InputFilename
== "-") {
152 OutputFilename
= "-";
154 const std::string
&IFN
= InputFilename
;
155 int Len
= IFN
.length();
156 // If the source ends in .bc, strip it off.
157 if (IFN
[Len
-3] == '.' && IFN
[Len
-2] == 'b' && IFN
[Len
-1] == 'c')
158 OutputFilename
= std::string(IFN
.begin(), IFN
.end()-3)+".ll";
160 OutputFilename
= IFN
+".ll";
164 std::string ErrorInfo
;
165 OwningPtr
<tool_output_file
>
166 Out(new tool_output_file(OutputFilename
.c_str(), ErrorInfo
,
167 raw_fd_ostream::F_Binary
));
168 if (!ErrorInfo
.empty()) {
169 errs() << ErrorInfo
<< '\n';
173 OwningPtr
<AssemblyAnnotationWriter
> Annotator
;
175 Annotator
.reset(new CommentWriter());
177 // All that llvm-dis does is write the assembly to a file.
179 M
->print(Out
->os(), Annotator
.get());