]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-dis/llvm-dis.cpp
Merge vendor lld/docs directory from r337145
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-dis / llvm-dis.cpp
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
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>
37 using namespace llvm;
38
39 static cl::opt<std::string>
40 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
41
42 static cl::opt<std::string>
43 OutputFilename("o", cl::desc("Override output filename"),
44                cl::value_desc("filename"));
45
46 static cl::opt<bool>
47 Force("f", cl::desc("Enable binary output on terminals"));
48
49 static cl::opt<bool>
50 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
51
52 static cl::opt<bool>
53     SetImporting("set-importing",
54                  cl::desc("Set lazy loading to pretend to import a module"),
55                  cl::Hidden);
56
57 static cl::opt<bool>
58     ShowAnnotations("show-annotations",
59                     cl::desc("Add informational comments to the .ll file"));
60
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);
65
66 static cl::opt<bool>
67     MaterializeMetadata("materialize-metadata",
68                         cl::desc("Load module without materializing metadata, "
69                                  "then materialize only the metadata"));
70
71 namespace {
72
73 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
74   OS << DL.getLine() << ":" << DL.getCol();
75   if (DILocation *IDL = DL.getInlinedAt()) {
76     OS << "@";
77     printDebugLoc(IDL, OS);
78   }
79 }
80 class CommentWriter : public AssemblyAnnotationWriter {
81 public:
82   void emitFunctionAnnot(const Function *F,
83                          formatted_raw_ostream &OS) override {
84     OS << "; [#uses=" << F->getNumUses() << ']';  // Output # uses
85     OS << '\n';
86   }
87   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
88     bool Padded = false;
89     if (!V.getType()->isVoidTy()) {
90       OS.PadToColumn(50);
91       Padded = true;
92       // Output # uses and type
93       OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
94     }
95     if (const Instruction *I = dyn_cast<Instruction>(&V)) {
96       if (const DebugLoc &DL = I->getDebugLoc()) {
97         if (!Padded) {
98           OS.PadToColumn(50);
99           Padded = true;
100           OS << ";";
101         }
102         OS << " [debug line = ";
103         printDebugLoc(DL,OS);
104         OS << "]";
105       }
106       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
107         if (!Padded) {
108           OS.PadToColumn(50);
109           OS << ";";
110         }
111         OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
112       }
113       else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
114         if (!Padded) {
115           OS.PadToColumn(50);
116           OS << ";";
117         }
118         OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
119       }
120     }
121   }
122 };
123
124 struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
125   char *Prefix;
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;
135     }
136
137     DiagnosticPrinterRawOStream DP(OS);
138     DI.print(DP);
139     OS << '\n';
140
141     if (DI.getSeverity() == DS_Error)
142       exit(1);
143     return true;
144   }
145 };
146 } // end anon namespace
147
148 static ExitOnError ExitOnErr;
149
150 int main(int argc, char **argv) {
151   InitLLVM X(argc, argv);
152
153   ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
154
155   LLVMContext Context;
156   Context.setDiagnosticHandler(
157       llvm::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
158   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
159
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());
166   else
167     ExitOnErr(M->materializeAll());
168
169   BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB));
170   std::unique_ptr<ModuleSummaryIndex> Index;
171   if (LTOInfo.HasSummary)
172     Index = ExitOnErr(getModuleSummaryIndex(*MB));
173
174   // Just use stdout.  We won't actually print anything on it.
175   if (DontPrint)
176     OutputFilename = "-";
177
178   if (OutputFilename.empty()) { // Unspecified output, infer it.
179     if (InputFilename == "-") {
180       OutputFilename = "-";
181     } else {
182       StringRef IFN = InputFilename;
183       OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
184       OutputFilename += ".ll";
185     }
186   }
187
188   std::error_code EC;
189   std::unique_ptr<ToolOutputFile> Out(
190       new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
191   if (EC) {
192     errs() << EC.message() << '\n';
193     return 1;
194   }
195
196   std::unique_ptr<AssemblyAnnotationWriter> Annotator;
197   if (ShowAnnotations)
198     Annotator.reset(new CommentWriter());
199
200   // All that llvm-dis does is write the assembly to a file.
201   if (!DontPrint) {
202     M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
203     if (Index)
204       Index->print(Out->os());
205   }
206
207   // Declare success.
208   Out->keep();
209
210   return 0;
211 }