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/Bitcode/BitcodeReader.h"
20 #include "llvm/IR/AssemblyAnnotationWriter.h"
21 #include "llvm/IR/DebugInfo.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/IR/DiagnosticPrinter.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Error.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/InitLLVM.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/ToolOutputFile.h"
35 #include "llvm/Support/WithColor.h"
36 #include <system_error>
39 static cl::opt
<std::string
>
40 InputFilename(cl::Positional
, cl::desc("<input bitcode>"), cl::init("-"));
42 static cl::opt
<std::string
>
43 OutputFilename("o", cl::desc("Override output filename"),
44 cl::value_desc("filename"));
47 Force("f", cl::desc("Enable binary output on terminals"));
50 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden
);
53 SetImporting("set-importing",
54 cl::desc("Set lazy loading to pretend to import a module"),
58 ShowAnnotations("show-annotations",
59 cl::desc("Add informational comments to the .ll file"));
61 static cl::opt
<bool> PreserveAssemblyUseListOrder(
62 "preserve-ll-uselistorder",
63 cl::desc("Preserve use-list order when writing LLVM assembly."),
64 cl::init(false), cl::Hidden
);
67 MaterializeMetadata("materialize-metadata",
68 cl::desc("Load module without materializing metadata, "
69 "then materialize only the metadata"));
73 static void printDebugLoc(const DebugLoc
&DL
, formatted_raw_ostream
&OS
) {
74 OS
<< DL
.getLine() << ":" << DL
.getCol();
75 if (DILocation
*IDL
= DL
.getInlinedAt()) {
77 printDebugLoc(IDL
, OS
);
80 class CommentWriter
: public AssemblyAnnotationWriter
{
82 void emitFunctionAnnot(const Function
*F
,
83 formatted_raw_ostream
&OS
) override
{
84 OS
<< "; [#uses=" << F
->getNumUses() << ']'; // Output # uses
87 void printInfoComment(const Value
&V
, formatted_raw_ostream
&OS
) override
{
89 if (!V
.getType()->isVoidTy()) {
92 // Output # uses and type
93 OS
<< "; [#uses=" << V
.getNumUses() << " type=" << *V
.getType() << "]";
95 if (const Instruction
*I
= dyn_cast
<Instruction
>(&V
)) {
96 if (const DebugLoc
&DL
= I
->getDebugLoc()) {
102 OS
<< " [debug line = ";
103 printDebugLoc(DL
,OS
);
106 if (const DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(I
)) {
111 OS
<< " [debug variable = " << DDI
->getVariable()->getName() << "]";
113 else if (const DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(I
)) {
118 OS
<< " [debug variable = " << DVI
->getVariable()->getName() << "]";
124 struct LLVMDisDiagnosticHandler
: public DiagnosticHandler
{
126 LLVMDisDiagnosticHandler(char *PrefixPtr
) : Prefix(PrefixPtr
) {}
127 bool handleDiagnostics(const DiagnosticInfo
&DI
) override
{
128 raw_ostream
&OS
= errs();
129 OS
<< Prefix
<< ": ";
130 switch (DI
.getSeverity()) {
131 case DS_Error
: WithColor::error(OS
); break;
132 case DS_Warning
: WithColor::warning(OS
); break;
133 case DS_Remark
: OS
<< "remark: "; break;
134 case DS_Note
: WithColor::note(OS
); break;
137 DiagnosticPrinterRawOStream
DP(OS
);
141 if (DI
.getSeverity() == DS_Error
)
146 } // end anon namespace
148 static ExitOnError ExitOnErr
;
150 int main(int argc
, char **argv
) {
151 InitLLVM
X(argc
, argv
);
153 ExitOnErr
.setBanner(std::string(argv
[0]) + ": error: ");
156 Context
.setDiagnosticHandler(
157 llvm::make_unique
<LLVMDisDiagnosticHandler
>(argv
[0]));
158 cl::ParseCommandLineOptions(argc
, argv
, "llvm .bc -> .ll disassembler\n");
160 std::unique_ptr
<MemoryBuffer
> MB
=
161 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename
)));
162 std::unique_ptr
<Module
> M
= ExitOnErr(getLazyBitcodeModule(
163 *MB
, Context
, /*ShouldLazyLoadMetadata=*/true, SetImporting
));
164 if (MaterializeMetadata
)
165 ExitOnErr(M
->materializeMetadata());
167 ExitOnErr(M
->materializeAll());
169 BitcodeLTOInfo LTOInfo
= ExitOnErr(getBitcodeLTOInfo(*MB
));
170 std::unique_ptr
<ModuleSummaryIndex
> Index
;
171 if (LTOInfo
.HasSummary
)
172 Index
= ExitOnErr(getModuleSummaryIndex(*MB
));
174 // Just use stdout. We won't actually print anything on it.
176 OutputFilename
= "-";
178 if (OutputFilename
.empty()) { // Unspecified output, infer it.
179 if (InputFilename
== "-") {
180 OutputFilename
= "-";
182 StringRef IFN
= InputFilename
;
183 OutputFilename
= (IFN
.endswith(".bc") ? IFN
.drop_back(3) : IFN
).str();
184 OutputFilename
+= ".ll";
189 std::unique_ptr
<ToolOutputFile
> Out(
190 new ToolOutputFile(OutputFilename
, EC
, sys::fs::F_None
));
192 errs() << EC
.message() << '\n';
196 std::unique_ptr
<AssemblyAnnotationWriter
> Annotator
;
198 Annotator
.reset(new CommentWriter());
200 // All that llvm-dis does is write the assembly to a file.
202 M
->print(Out
->os(), Annotator
.get(), PreserveAssemblyUseListOrder
);
204 Index
->print(Out
->os());