xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-dis/llvm-dis.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This utility may be invoked in the following manner:
100b57cec5SDimitry Andric //  llvm-dis [options]      - Read LLVM bitcode from stdin, write asm to stdout
110b57cec5SDimitry Andric //  llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
120b57cec5SDimitry Andric //                            to the x.ll file.
130b57cec5SDimitry Andric //  Options:
140b57cec5SDimitry Andric //      --help   - Output information about command line switches
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h"
190b57cec5SDimitry Andric #include "llvm/IR/AssemblyAnnotationWriter.h"
200b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h"
210b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
220b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h"
230b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
240b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
250b57cec5SDimitry Andric #include "llvm/IR/Module.h"
2681ad6265SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
270b57cec5SDimitry Andric #include "llvm/IR/Type.h"
280b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
290b57cec5SDimitry Andric #include "llvm/Support/Error.h"
300b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
310b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
320b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h"
330b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
340b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
350b57cec5SDimitry Andric #include "llvm/Support/WithColor.h"
360b57cec5SDimitry Andric #include <system_error>
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric 
39fe6060f1SDimitry Andric static cl::OptionCategory DisCategory("Disassembler Options");
400b57cec5SDimitry Andric 
4181ad6265SDimitry Andric static cl::list<std::string> InputFilenames(cl::Positional,
42fe6060f1SDimitry Andric                                             cl::desc("[input bitcode]..."),
43fe6060f1SDimitry Andric                                             cl::cat(DisCategory));
440b57cec5SDimitry Andric 
45fe6060f1SDimitry Andric static cl::opt<std::string> OutputFilename("o",
46fe6060f1SDimitry Andric                                            cl::desc("Override output filename"),
47fe6060f1SDimitry Andric                                            cl::value_desc("filename"),
48fe6060f1SDimitry Andric                                            cl::cat(DisCategory));
490b57cec5SDimitry Andric 
50fe6060f1SDimitry Andric static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"),
51fe6060f1SDimitry Andric                            cl::cat(DisCategory));
52fe6060f1SDimitry Andric 
53fe6060f1SDimitry Andric static cl::opt<bool> DontPrint("disable-output",
54fe6060f1SDimitry Andric                                cl::desc("Don't output the .ll file"),
55fe6060f1SDimitry Andric                                cl::Hidden, cl::cat(DisCategory));
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric static cl::opt<bool>
580b57cec5SDimitry Andric     SetImporting("set-importing",
590b57cec5SDimitry Andric                  cl::desc("Set lazy loading to pretend to import a module"),
60fe6060f1SDimitry Andric                  cl::Hidden, cl::cat(DisCategory));
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric static cl::opt<bool>
630b57cec5SDimitry Andric     ShowAnnotations("show-annotations",
64fe6060f1SDimitry Andric                     cl::desc("Add informational comments to the .ll file"),
65fe6060f1SDimitry Andric                     cl::cat(DisCategory));
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static cl::opt<bool> PreserveAssemblyUseListOrder(
680b57cec5SDimitry Andric     "preserve-ll-uselistorder",
690b57cec5SDimitry Andric     cl::desc("Preserve use-list order when writing LLVM assembly."),
70fe6060f1SDimitry Andric     cl::init(false), cl::Hidden, cl::cat(DisCategory));
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static cl::opt<bool>
730b57cec5SDimitry Andric     MaterializeMetadata("materialize-metadata",
740b57cec5SDimitry Andric                         cl::desc("Load module without materializing metadata, "
75fe6060f1SDimitry Andric                                  "then materialize only the metadata"),
76fe6060f1SDimitry Andric                         cl::cat(DisCategory));
770b57cec5SDimitry Andric 
7804eeddc0SDimitry Andric static cl::opt<bool> PrintThinLTOIndexOnly(
7904eeddc0SDimitry Andric     "print-thinlto-index-only",
8004eeddc0SDimitry Andric     cl::desc("Only read thinlto index and print the index as LLVM assembly."),
8104eeddc0SDimitry Andric     cl::init(false), cl::Hidden, cl::cat(DisCategory));
8204eeddc0SDimitry Andric 
83*0fca6ea1SDimitry Andric extern cl::opt<bool> WriteNewDbgInfoFormat;
84*0fca6ea1SDimitry Andric 
85*0fca6ea1SDimitry Andric extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
86*0fca6ea1SDimitry Andric 
870b57cec5SDimitry Andric namespace {
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
900b57cec5SDimitry Andric   OS << DL.getLine() << ":" << DL.getCol();
910b57cec5SDimitry Andric   if (DILocation *IDL = DL.getInlinedAt()) {
920b57cec5SDimitry Andric     OS << "@";
930b57cec5SDimitry Andric     printDebugLoc(IDL, OS);
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric class CommentWriter : public AssemblyAnnotationWriter {
970b57cec5SDimitry Andric public:
980b57cec5SDimitry Andric   void emitFunctionAnnot(const Function *F,
990b57cec5SDimitry Andric                          formatted_raw_ostream &OS) override {
1000b57cec5SDimitry Andric     OS << "; [#uses=" << F->getNumUses() << ']';  // Output # uses
1010b57cec5SDimitry Andric     OS << '\n';
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
1040b57cec5SDimitry Andric     bool Padded = false;
1050b57cec5SDimitry Andric     if (!V.getType()->isVoidTy()) {
1060b57cec5SDimitry Andric       OS.PadToColumn(50);
1070b57cec5SDimitry Andric       Padded = true;
1080b57cec5SDimitry Andric       // Output # uses and type
1090b57cec5SDimitry Andric       OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
1100b57cec5SDimitry Andric     }
1110b57cec5SDimitry Andric     if (const Instruction *I = dyn_cast<Instruction>(&V)) {
1120b57cec5SDimitry Andric       if (const DebugLoc &DL = I->getDebugLoc()) {
1130b57cec5SDimitry Andric         if (!Padded) {
1140b57cec5SDimitry Andric           OS.PadToColumn(50);
1150b57cec5SDimitry Andric           Padded = true;
1160b57cec5SDimitry Andric           OS << ";";
1170b57cec5SDimitry Andric         }
1180b57cec5SDimitry Andric         OS << " [debug line = ";
1190b57cec5SDimitry Andric         printDebugLoc(DL,OS);
1200b57cec5SDimitry Andric         OS << "]";
1210b57cec5SDimitry Andric       }
1220b57cec5SDimitry Andric       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
1230b57cec5SDimitry Andric         if (!Padded) {
1240b57cec5SDimitry Andric           OS.PadToColumn(50);
1250b57cec5SDimitry Andric           OS << ";";
1260b57cec5SDimitry Andric         }
1270b57cec5SDimitry Andric         OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
1280b57cec5SDimitry Andric       }
1290b57cec5SDimitry Andric       else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
1300b57cec5SDimitry Andric         if (!Padded) {
1310b57cec5SDimitry Andric           OS.PadToColumn(50);
1320b57cec5SDimitry Andric           OS << ";";
1330b57cec5SDimitry Andric         }
1340b57cec5SDimitry Andric         OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
1350b57cec5SDimitry Andric       }
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric };
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
1410b57cec5SDimitry Andric   char *Prefix;
1420b57cec5SDimitry Andric   LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {}
1430b57cec5SDimitry Andric   bool handleDiagnostics(const DiagnosticInfo &DI) override {
1440b57cec5SDimitry Andric     raw_ostream &OS = errs();
1450b57cec5SDimitry Andric     OS << Prefix << ": ";
1460b57cec5SDimitry Andric     switch (DI.getSeverity()) {
1470b57cec5SDimitry Andric       case DS_Error: WithColor::error(OS); break;
1480b57cec5SDimitry Andric       case DS_Warning: WithColor::warning(OS); break;
1490b57cec5SDimitry Andric       case DS_Remark: OS << "remark: "; break;
1500b57cec5SDimitry Andric       case DS_Note: WithColor::note(OS); break;
1510b57cec5SDimitry Andric     }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     DiagnosticPrinterRawOStream DP(OS);
1540b57cec5SDimitry Andric     DI.print(DP);
1550b57cec5SDimitry Andric     OS << '\n';
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric     if (DI.getSeverity() == DS_Error)
1580b57cec5SDimitry Andric       exit(1);
1590b57cec5SDimitry Andric     return true;
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric };
1620b57cec5SDimitry Andric } // end anon namespace
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric static ExitOnError ExitOnErr;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric int main(int argc, char **argv) {
1670b57cec5SDimitry Andric   InitLLVM X(argc, argv);
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
1700b57cec5SDimitry Andric 
171fe6060f1SDimitry Andric   cl::HideUnrelatedOptions({&DisCategory, &getColorCategory()});
172fe6060f1SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
173fe6060f1SDimitry Andric 
174*0fca6ea1SDimitry Andric   // Load bitcode into the new debug info format by default.
175*0fca6ea1SDimitry Andric   if (LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_UNSET)
176*0fca6ea1SDimitry Andric     LoadBitcodeIntoNewDbgInfoFormat = cl::boolOrDefault::BOU_TRUE;
177*0fca6ea1SDimitry Andric 
1780b57cec5SDimitry Andric   LLVMContext Context;
1790b57cec5SDimitry Andric   Context.setDiagnosticHandler(
1808bcb0991SDimitry Andric       std::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
1810b57cec5SDimitry Andric 
182fe6060f1SDimitry Andric   if (InputFilenames.size() < 1) {
183fe6060f1SDimitry Andric     InputFilenames.push_back("-");
184fe6060f1SDimitry Andric   } else if (InputFilenames.size() > 1 && !OutputFilename.empty()) {
185fe6060f1SDimitry Andric     errs()
186fe6060f1SDimitry Andric         << "error: output file name cannot be set for multiple input files\n";
187fe6060f1SDimitry Andric     return 1;
188fe6060f1SDimitry Andric   }
189fe6060f1SDimitry Andric 
190fe6060f1SDimitry Andric   for (std::string InputFilename : InputFilenames) {
19181ad6265SDimitry Andric     ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
19281ad6265SDimitry Andric         MemoryBuffer::getFileOrSTDIN(InputFilename);
19381ad6265SDimitry Andric     if (std::error_code EC = BufferOrErr.getError()) {
19481ad6265SDimitry Andric       WithColor::error() << InputFilename << ": " << EC.message() << '\n';
19581ad6265SDimitry Andric       return 1;
19681ad6265SDimitry Andric     }
19781ad6265SDimitry Andric     std::unique_ptr<MemoryBuffer> MB = std::move(BufferOrErr.get());
198480093f4SDimitry Andric 
199480093f4SDimitry Andric     BitcodeFileContents IF = ExitOnErr(llvm::getBitcodeFileContents(*MB));
200480093f4SDimitry Andric 
201480093f4SDimitry Andric     const size_t N = IF.Mods.size();
202480093f4SDimitry Andric 
203480093f4SDimitry Andric     if (OutputFilename == "-" && N > 1)
204480093f4SDimitry Andric       errs() << "only single module bitcode files can be written to stdout\n";
205480093f4SDimitry Andric 
206fe6060f1SDimitry Andric     for (size_t I = 0; I < N; ++I) {
207fe6060f1SDimitry Andric       BitcodeModule MB = IF.Mods[I];
20804eeddc0SDimitry Andric 
20904eeddc0SDimitry Andric       std::unique_ptr<Module> M;
21004eeddc0SDimitry Andric 
21104eeddc0SDimitry Andric       if (!PrintThinLTOIndexOnly) {
21204eeddc0SDimitry Andric         M = ExitOnErr(
213fe6060f1SDimitry Andric             MB.getLazyModule(Context, MaterializeMetadata, SetImporting));
2140b57cec5SDimitry Andric         if (MaterializeMetadata)
2150b57cec5SDimitry Andric           ExitOnErr(M->materializeMetadata());
2160b57cec5SDimitry Andric         else
2170b57cec5SDimitry Andric           ExitOnErr(M->materializeAll());
21804eeddc0SDimitry Andric       }
2190b57cec5SDimitry Andric 
220480093f4SDimitry Andric       BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo());
2210b57cec5SDimitry Andric       std::unique_ptr<ModuleSummaryIndex> Index;
2220b57cec5SDimitry Andric       if (LTOInfo.HasSummary)
223480093f4SDimitry Andric         Index = ExitOnErr(MB.getSummary());
224480093f4SDimitry Andric 
225480093f4SDimitry Andric       std::string FinalFilename(OutputFilename);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric       // Just use stdout.  We won't actually print anything on it.
2280b57cec5SDimitry Andric       if (DontPrint)
229480093f4SDimitry Andric         FinalFilename = "-";
2300b57cec5SDimitry Andric 
231480093f4SDimitry Andric       if (FinalFilename.empty()) { // Unspecified output, infer it.
2320b57cec5SDimitry Andric         if (InputFilename == "-") {
233480093f4SDimitry Andric           FinalFilename = "-";
2340b57cec5SDimitry Andric         } else {
2350b57cec5SDimitry Andric           StringRef IFN = InputFilename;
2365f757f3fSDimitry Andric           FinalFilename = (IFN.ends_with(".bc") ? IFN.drop_back(3) : IFN).str();
237480093f4SDimitry Andric           if (N > 1)
238fe6060f1SDimitry Andric             FinalFilename += std::string(".") + std::to_string(I);
239480093f4SDimitry Andric           FinalFilename += ".ll";
2400b57cec5SDimitry Andric         }
241480093f4SDimitry Andric       } else {
242480093f4SDimitry Andric         if (N > 1)
243fe6060f1SDimitry Andric           FinalFilename += std::string(".") + std::to_string(I);
2440b57cec5SDimitry Andric       }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric       std::error_code EC;
2470b57cec5SDimitry Andric       std::unique_ptr<ToolOutputFile> Out(
248fe6060f1SDimitry Andric           new ToolOutputFile(FinalFilename, EC, sys::fs::OF_TextWithCRLF));
2490b57cec5SDimitry Andric       if (EC) {
2500b57cec5SDimitry Andric         errs() << EC.message() << '\n';
2510b57cec5SDimitry Andric         return 1;
2520b57cec5SDimitry Andric       }
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric       std::unique_ptr<AssemblyAnnotationWriter> Annotator;
2550b57cec5SDimitry Andric       if (ShowAnnotations)
2560b57cec5SDimitry Andric         Annotator.reset(new CommentWriter());
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric       // All that llvm-dis does is write the assembly to a file.
2590b57cec5SDimitry Andric       if (!DontPrint) {
260*0fca6ea1SDimitry Andric         if (M) {
261*0fca6ea1SDimitry Andric           M->setIsNewDbgInfoFormat(WriteNewDbgInfoFormat);
262*0fca6ea1SDimitry Andric           if (WriteNewDbgInfoFormat)
263*0fca6ea1SDimitry Andric             M->removeDebugIntrinsicDeclarations();
2640b57cec5SDimitry Andric           M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
265*0fca6ea1SDimitry Andric         }
2660b57cec5SDimitry Andric         if (Index)
2670b57cec5SDimitry Andric           Index->print(Out->os());
2680b57cec5SDimitry Andric       }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric       // Declare success.
2710b57cec5SDimitry Andric       Out->keep();
272480093f4SDimitry Andric     }
273fe6060f1SDimitry Andric   }
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   return 0;
2760b57cec5SDimitry Andric }
277