17330f729Sjoerg //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This utility may be invoked in the following manner:
107330f729Sjoerg // llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout
117330f729Sjoerg // llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
127330f729Sjoerg // to the x.ll file.
137330f729Sjoerg // Options:
147330f729Sjoerg // --help - Output information about command line switches
157330f729Sjoerg //
167330f729Sjoerg //===----------------------------------------------------------------------===//
177330f729Sjoerg
187330f729Sjoerg #include "llvm/Bitcode/BitcodeReader.h"
197330f729Sjoerg #include "llvm/IR/AssemblyAnnotationWriter.h"
207330f729Sjoerg #include "llvm/IR/DebugInfo.h"
217330f729Sjoerg #include "llvm/IR/DiagnosticInfo.h"
227330f729Sjoerg #include "llvm/IR/DiagnosticPrinter.h"
237330f729Sjoerg #include "llvm/IR/IntrinsicInst.h"
247330f729Sjoerg #include "llvm/IR/LLVMContext.h"
257330f729Sjoerg #include "llvm/IR/Module.h"
267330f729Sjoerg #include "llvm/IR/Type.h"
277330f729Sjoerg #include "llvm/Support/CommandLine.h"
287330f729Sjoerg #include "llvm/Support/Error.h"
297330f729Sjoerg #include "llvm/Support/FileSystem.h"
307330f729Sjoerg #include "llvm/Support/FormattedStream.h"
317330f729Sjoerg #include "llvm/Support/InitLLVM.h"
327330f729Sjoerg #include "llvm/Support/MemoryBuffer.h"
337330f729Sjoerg #include "llvm/Support/ToolOutputFile.h"
347330f729Sjoerg #include "llvm/Support/WithColor.h"
357330f729Sjoerg #include <system_error>
367330f729Sjoerg using namespace llvm;
377330f729Sjoerg
38*82d56013Sjoerg static cl::list<std::string> InputFilenames(cl::Positional, cl::ZeroOrMore,
39*82d56013Sjoerg cl::desc("[input bitcode]..."));
407330f729Sjoerg
417330f729Sjoerg static cl::opt<std::string>
427330f729Sjoerg OutputFilename("o", cl::desc("Override output filename"),
437330f729Sjoerg cl::value_desc("filename"));
447330f729Sjoerg
457330f729Sjoerg static cl::opt<bool>
467330f729Sjoerg Force("f", cl::desc("Enable binary output on terminals"));
477330f729Sjoerg
487330f729Sjoerg static cl::opt<bool>
497330f729Sjoerg DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
507330f729Sjoerg
517330f729Sjoerg static cl::opt<bool>
527330f729Sjoerg SetImporting("set-importing",
537330f729Sjoerg cl::desc("Set lazy loading to pretend to import a module"),
547330f729Sjoerg cl::Hidden);
557330f729Sjoerg
567330f729Sjoerg static cl::opt<bool>
577330f729Sjoerg ShowAnnotations("show-annotations",
587330f729Sjoerg cl::desc("Add informational comments to the .ll file"));
597330f729Sjoerg
607330f729Sjoerg static cl::opt<bool> PreserveAssemblyUseListOrder(
617330f729Sjoerg "preserve-ll-uselistorder",
627330f729Sjoerg cl::desc("Preserve use-list order when writing LLVM assembly."),
637330f729Sjoerg cl::init(false), cl::Hidden);
647330f729Sjoerg
657330f729Sjoerg static cl::opt<bool>
667330f729Sjoerg MaterializeMetadata("materialize-metadata",
677330f729Sjoerg cl::desc("Load module without materializing metadata, "
687330f729Sjoerg "then materialize only the metadata"));
697330f729Sjoerg
707330f729Sjoerg namespace {
717330f729Sjoerg
printDebugLoc(const DebugLoc & DL,formatted_raw_ostream & OS)727330f729Sjoerg static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
737330f729Sjoerg OS << DL.getLine() << ":" << DL.getCol();
747330f729Sjoerg if (DILocation *IDL = DL.getInlinedAt()) {
757330f729Sjoerg OS << "@";
767330f729Sjoerg printDebugLoc(IDL, OS);
777330f729Sjoerg }
787330f729Sjoerg }
797330f729Sjoerg class CommentWriter : public AssemblyAnnotationWriter {
807330f729Sjoerg public:
emitFunctionAnnot(const Function * F,formatted_raw_ostream & OS)817330f729Sjoerg void emitFunctionAnnot(const Function *F,
827330f729Sjoerg formatted_raw_ostream &OS) override {
837330f729Sjoerg OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
847330f729Sjoerg OS << '\n';
857330f729Sjoerg }
printInfoComment(const Value & V,formatted_raw_ostream & OS)867330f729Sjoerg void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
877330f729Sjoerg bool Padded = false;
887330f729Sjoerg if (!V.getType()->isVoidTy()) {
897330f729Sjoerg OS.PadToColumn(50);
907330f729Sjoerg Padded = true;
917330f729Sjoerg // Output # uses and type
927330f729Sjoerg OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";
937330f729Sjoerg }
947330f729Sjoerg if (const Instruction *I = dyn_cast<Instruction>(&V)) {
957330f729Sjoerg if (const DebugLoc &DL = I->getDebugLoc()) {
967330f729Sjoerg if (!Padded) {
977330f729Sjoerg OS.PadToColumn(50);
987330f729Sjoerg Padded = true;
997330f729Sjoerg OS << ";";
1007330f729Sjoerg }
1017330f729Sjoerg OS << " [debug line = ";
1027330f729Sjoerg printDebugLoc(DL,OS);
1037330f729Sjoerg OS << "]";
1047330f729Sjoerg }
1057330f729Sjoerg if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
1067330f729Sjoerg if (!Padded) {
1077330f729Sjoerg OS.PadToColumn(50);
1087330f729Sjoerg OS << ";";
1097330f729Sjoerg }
1107330f729Sjoerg OS << " [debug variable = " << DDI->getVariable()->getName() << "]";
1117330f729Sjoerg }
1127330f729Sjoerg else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
1137330f729Sjoerg if (!Padded) {
1147330f729Sjoerg OS.PadToColumn(50);
1157330f729Sjoerg OS << ";";
1167330f729Sjoerg }
1177330f729Sjoerg OS << " [debug variable = " << DVI->getVariable()->getName() << "]";
1187330f729Sjoerg }
1197330f729Sjoerg }
1207330f729Sjoerg }
1217330f729Sjoerg };
1227330f729Sjoerg
1237330f729Sjoerg struct LLVMDisDiagnosticHandler : public DiagnosticHandler {
1247330f729Sjoerg char *Prefix;
LLVMDisDiagnosticHandler__anon9e1ad9340111::LLVMDisDiagnosticHandler1257330f729Sjoerg LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {}
handleDiagnostics__anon9e1ad9340111::LLVMDisDiagnosticHandler1267330f729Sjoerg bool handleDiagnostics(const DiagnosticInfo &DI) override {
1277330f729Sjoerg raw_ostream &OS = errs();
1287330f729Sjoerg OS << Prefix << ": ";
1297330f729Sjoerg switch (DI.getSeverity()) {
1307330f729Sjoerg case DS_Error: WithColor::error(OS); break;
1317330f729Sjoerg case DS_Warning: WithColor::warning(OS); break;
1327330f729Sjoerg case DS_Remark: OS << "remark: "; break;
1337330f729Sjoerg case DS_Note: WithColor::note(OS); break;
1347330f729Sjoerg }
1357330f729Sjoerg
1367330f729Sjoerg DiagnosticPrinterRawOStream DP(OS);
1377330f729Sjoerg DI.print(DP);
1387330f729Sjoerg OS << '\n';
1397330f729Sjoerg
1407330f729Sjoerg if (DI.getSeverity() == DS_Error)
1417330f729Sjoerg exit(1);
1427330f729Sjoerg return true;
1437330f729Sjoerg }
1447330f729Sjoerg };
1457330f729Sjoerg } // end anon namespace
1467330f729Sjoerg
1477330f729Sjoerg static ExitOnError ExitOnErr;
1487330f729Sjoerg
main(int argc,char ** argv)1497330f729Sjoerg int main(int argc, char **argv) {
1507330f729Sjoerg InitLLVM X(argc, argv);
1517330f729Sjoerg
1527330f729Sjoerg ExitOnErr.setBanner(std::string(argv[0]) + ": error: ");
1537330f729Sjoerg
1547330f729Sjoerg LLVMContext Context;
1557330f729Sjoerg Context.setDiagnosticHandler(
1567330f729Sjoerg std::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
1577330f729Sjoerg cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
1587330f729Sjoerg
159*82d56013Sjoerg if (InputFilenames.size() < 1) {
160*82d56013Sjoerg InputFilenames.push_back("-");
161*82d56013Sjoerg } else if (InputFilenames.size() > 1 && !OutputFilename.empty()) {
162*82d56013Sjoerg errs()
163*82d56013Sjoerg << "error: output file name cannot be set for multiple input files\n";
164*82d56013Sjoerg return 1;
165*82d56013Sjoerg }
166*82d56013Sjoerg
167*82d56013Sjoerg for (std::string InputFilename : InputFilenames) {
168*82d56013Sjoerg std::unique_ptr<MemoryBuffer> MB = ExitOnErr(
169*82d56013Sjoerg errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
170*82d56013Sjoerg
171*82d56013Sjoerg BitcodeFileContents IF = ExitOnErr(llvm::getBitcodeFileContents(*MB));
172*82d56013Sjoerg
173*82d56013Sjoerg const size_t N = IF.Mods.size();
174*82d56013Sjoerg
175*82d56013Sjoerg if (OutputFilename == "-" && N > 1)
176*82d56013Sjoerg errs() << "only single module bitcode files can be written to stdout\n";
177*82d56013Sjoerg
178*82d56013Sjoerg for (size_t I = 0; I < N; ++I) {
179*82d56013Sjoerg BitcodeModule MB = IF.Mods[I];
180*82d56013Sjoerg std::unique_ptr<Module> M = ExitOnErr(
181*82d56013Sjoerg MB.getLazyModule(Context, MaterializeMetadata, SetImporting));
1827330f729Sjoerg if (MaterializeMetadata)
1837330f729Sjoerg ExitOnErr(M->materializeMetadata());
1847330f729Sjoerg else
1857330f729Sjoerg ExitOnErr(M->materializeAll());
1867330f729Sjoerg
187*82d56013Sjoerg BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo());
1887330f729Sjoerg std::unique_ptr<ModuleSummaryIndex> Index;
1897330f729Sjoerg if (LTOInfo.HasSummary)
190*82d56013Sjoerg Index = ExitOnErr(MB.getSummary());
191*82d56013Sjoerg
192*82d56013Sjoerg std::string FinalFilename(OutputFilename);
1937330f729Sjoerg
1947330f729Sjoerg // Just use stdout. We won't actually print anything on it.
1957330f729Sjoerg if (DontPrint)
196*82d56013Sjoerg FinalFilename = "-";
1977330f729Sjoerg
198*82d56013Sjoerg if (FinalFilename.empty()) { // Unspecified output, infer it.
1997330f729Sjoerg if (InputFilename == "-") {
200*82d56013Sjoerg FinalFilename = "-";
2017330f729Sjoerg } else {
2027330f729Sjoerg StringRef IFN = InputFilename;
203*82d56013Sjoerg FinalFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str();
204*82d56013Sjoerg if (N > 1)
205*82d56013Sjoerg FinalFilename += std::string(".") + std::to_string(I);
206*82d56013Sjoerg FinalFilename += ".ll";
2077330f729Sjoerg }
208*82d56013Sjoerg } else {
209*82d56013Sjoerg if (N > 1)
210*82d56013Sjoerg FinalFilename += std::string(".") + std::to_string(I);
2117330f729Sjoerg }
2127330f729Sjoerg
2137330f729Sjoerg std::error_code EC;
2147330f729Sjoerg std::unique_ptr<ToolOutputFile> Out(
215*82d56013Sjoerg new ToolOutputFile(FinalFilename, EC, sys::fs::OF_TextWithCRLF));
2167330f729Sjoerg if (EC) {
2177330f729Sjoerg errs() << EC.message() << '\n';
2187330f729Sjoerg return 1;
2197330f729Sjoerg }
2207330f729Sjoerg
2217330f729Sjoerg std::unique_ptr<AssemblyAnnotationWriter> Annotator;
2227330f729Sjoerg if (ShowAnnotations)
2237330f729Sjoerg Annotator.reset(new CommentWriter());
2247330f729Sjoerg
2257330f729Sjoerg // All that llvm-dis does is write the assembly to a file.
2267330f729Sjoerg if (!DontPrint) {
2277330f729Sjoerg M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
2287330f729Sjoerg if (Index)
2297330f729Sjoerg Index->print(Out->os());
2307330f729Sjoerg }
2317330f729Sjoerg
2327330f729Sjoerg // Declare success.
2337330f729Sjoerg Out->keep();
234*82d56013Sjoerg }
235*82d56013Sjoerg }
2367330f729Sjoerg
2377330f729Sjoerg return 0;
2387330f729Sjoerg }
239