Chris Lattner | 8f71f04 | 2003-10-20 17:58:43 +0000 | [diff] [blame] | 1 | //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 6 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 8 | // |
| 9 | // This utility may be invoked in the following manner: |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 10 | // llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout |
| 11 | // llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm |
Misha Brukman | f12549d | 2003-08-28 21:34:13 +0000 | [diff] [blame] | 12 | // to the x.ll file. |
Chris Lattner | f284ac5 | 2001-06-13 19:55:41 +0000 | [diff] [blame] | 13 | // Options: |
| 14 | // --help - Output information about command line switches |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | // |
Chris Lattner | 6d56c6b | 2001-10-13 07:06:57 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/BitcodeReader.h" |
Chandler Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 19 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DebugInfo.h" |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DiagnosticInfo.h" |
| 22 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IntrinsicInst.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 24 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Module.h" |
| 26 | #include "llvm/IR/Type.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Error.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 29 | #include "llvm/Support/FileSystem.h" |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FormattedStream.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | 2cd41eb | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 34 | #include "llvm/Support/WithColor.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 35 | #include <system_error> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chris Lattner | 64a6727 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 38 | static cl::opt<std::string> |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 39 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 64a6727 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 41 | static cl::opt<std::string> |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 42 | OutputFilename("o", cl::desc("Override output filename"), |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 43 | cl::value_desc("filename")); |
| 44 | |
| 45 | static cl::opt<bool> |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 46 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 216835d | 2007-02-07 04:39:35 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
| 49 | DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); |
| 50 | |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
Charles Saternos | 55d93e7 | 2017-08-15 22:23:44 +0000 | [diff] [blame] | 52 | SetImporting("set-importing", |
| 53 | cl::desc("Set lazy loading to pretend to import a module"), |
| 54 | cl::Hidden); |
| 55 | |
| 56 | static cl::opt<bool> |
| 57 | ShowAnnotations("show-annotations", |
| 58 | cl::desc("Add informational comments to the .ll file")); |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 59 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 60 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 61 | "preserve-ll-uselistorder", |
| 62 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 63 | cl::init(false), cl::Hidden); |
| 64 | |
Peter Collingbourne | 2152189 | 2016-06-21 23:42:48 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
| 66 | MaterializeMetadata("materialize-metadata", |
| 67 | cl::desc("Load module without materializing metadata, " |
| 68 | "then materialize only the metadata")); |
| 69 | |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 70 | namespace { |
Michael J. Spencer | 7fda8fe | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 71 | |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 72 | static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) { |
| 73 | OS << DL.getLine() << ":" << DL.getCol(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 74 | if (DILocation *IDL = DL.getInlinedAt()) { |
Duncan P. N. Exon Smith | b8afdd6 | 2015-03-30 20:04:06 +0000 | [diff] [blame] | 75 | OS << "@"; |
| 76 | printDebugLoc(IDL, OS); |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 79 | class CommentWriter : public AssemblyAnnotationWriter { |
| 80 | public: |
| 81 | void emitFunctionAnnot(const Function *F, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 82 | formatted_raw_ostream &OS) override { |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 83 | OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses |
| 84 | OS << '\n'; |
| 85 | } |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 86 | void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 87 | bool Padded = false; |
| 88 | if (!V.getType()->isVoidTy()) { |
| 89 | OS.PadToColumn(50); |
| 90 | Padded = true; |
Alexey Samsonov | d923508 | 2015-05-11 21:20:20 +0000 | [diff] [blame] | 91 | // Output # uses and type |
| 92 | OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]"; |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 93 | } |
| 94 | if (const Instruction *I = dyn_cast<Instruction>(&V)) { |
Duncan P. N. Exon Smith | b8afdd6 | 2015-03-30 20:04:06 +0000 | [diff] [blame] | 95 | if (const DebugLoc &DL = I->getDebugLoc()) { |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 96 | if (!Padded) { |
| 97 | OS.PadToColumn(50); |
| 98 | Padded = true; |
| 99 | OS << ";"; |
| 100 | } |
| 101 | OS << " [debug line = "; |
| 102 | printDebugLoc(DL,OS); |
| 103 | OS << "]"; |
| 104 | } |
| 105 | if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 106 | if (!Padded) { |
| 107 | OS.PadToColumn(50); |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 108 | OS << ";"; |
| 109 | } |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 110 | OS << " [debug variable = " << DDI->getVariable()->getName() << "]"; |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 111 | } |
| 112 | else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 113 | if (!Padded) { |
| 114 | OS.PadToColumn(50); |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 115 | OS << ";"; |
| 116 | } |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 117 | OS << " [debug variable = " << DVI->getVariable()->getName() << "]"; |
Devang Patel | 982efb5 | 2011-03-11 18:07:33 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
Chris Lattner | 7f2f093 | 2010-09-02 23:21:44 +0000 | [diff] [blame] | 120 | } |
| 121 | }; |
Michael J. Spencer | 7fda8fe | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 122 | |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 123 | struct LLVMDisDiagnosticHandler : public DiagnosticHandler { |
| 124 | char *Prefix; |
| 125 | LLVMDisDiagnosticHandler(char *PrefixPtr) : Prefix(PrefixPtr) {} |
| 126 | bool handleDiagnostics(const DiagnosticInfo &DI) override { |
| 127 | raw_ostream &OS = errs(); |
| 128 | OS << Prefix << ": "; |
| 129 | switch (DI.getSeverity()) { |
Jonas Devlieghere | 2cd41eb | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 130 | case DS_Error: WithColor::error(OS); break; |
| 131 | case DS_Warning: WithColor::warning(OS); break; |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 132 | case DS_Remark: OS << "remark: "; break; |
Jonas Devlieghere | 2cd41eb | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 133 | case DS_Note: WithColor::note(OS); break; |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 134 | } |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 135 | |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 136 | DiagnosticPrinterRawOStream DP(OS); |
| 137 | DI.print(DP); |
| 138 | OS << '\n'; |
| 139 | |
| 140 | if (DI.getSeverity() == DS_Error) |
| 141 | exit(1); |
| 142 | return true; |
Vivek Pandya | df8598d | 2017-09-15 19:53:54 +0000 | [diff] [blame] | 143 | } |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 144 | }; |
| 145 | } // end anon namespace |
Vivek Pandya | df8598d | 2017-09-15 19:53:54 +0000 | [diff] [blame] | 146 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 147 | static ExitOnError ExitOnErr; |
| 148 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 149 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 150 | InitLLVM X(argc, argv); |
Michael J. Spencer | 7fda8fe | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 151 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 152 | ExitOnErr.setBanner(std::string(argv[0]) + ": error: "); |
| 153 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 154 | LLVMContext Context; |
Vivek Pandya | b5ab895 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 155 | Context.setDiagnosticHandler( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 156 | std::make_unique<LLVMDisDiagnosticHandler>(argv[0])); |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 157 | cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); |
Chris Lattner | 12439ff | 2004-02-19 20:32:12 +0000 | [diff] [blame] | 158 | |
Teresa Johnson | 08d5b4e | 2018-05-26 02:34:13 +0000 | [diff] [blame] | 159 | std::unique_ptr<MemoryBuffer> MB = |
| 160 | ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); |
Teresa Johnson | 08d5b4e | 2018-05-26 02:34:13 +0000 | [diff] [blame] | 161 | |
Matthew Voss | 141bb5f | 2019-11-12 16:29:37 -0800 | [diff] [blame] | 162 | BitcodeFileContents IF = ExitOnErr(llvm::getBitcodeFileContents(*MB)); |
Michael J. Spencer | 7fda8fe | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 163 | |
Matthew Voss | 141bb5f | 2019-11-12 16:29:37 -0800 | [diff] [blame] | 164 | const size_t N = IF.Mods.size(); |
Michael J. Spencer | 7fda8fe | 2010-12-16 16:23:30 +0000 | [diff] [blame] | 165 | |
Matthew Voss | 141bb5f | 2019-11-12 16:29:37 -0800 | [diff] [blame] | 166 | if (OutputFilename == "-" && N > 1) |
| 167 | errs() << "only single module bitcode files can be written to stdout\n"; |
| 168 | |
| 169 | for (size_t i = 0; i < N; ++i) { |
| 170 | BitcodeModule MB = IF.Mods[i]; |
| 171 | std::unique_ptr<Module> M = ExitOnErr(MB.getLazyModule(Context, MaterializeMetadata, |
| 172 | SetImporting)); |
| 173 | if (MaterializeMetadata) |
| 174 | ExitOnErr(M->materializeMetadata()); |
| 175 | else |
| 176 | ExitOnErr(M->materializeAll()); |
| 177 | |
| 178 | BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo()); |
| 179 | std::unique_ptr<ModuleSummaryIndex> Index; |
| 180 | if (LTOInfo.HasSummary) |
| 181 | Index = ExitOnErr(MB.getSummary()); |
| 182 | |
| 183 | std::string FinalFilename(OutputFilename); |
| 184 | |
| 185 | // Just use stdout. We won't actually print anything on it. |
| 186 | if (DontPrint) |
| 187 | FinalFilename = "-"; |
| 188 | |
Matthew Voss | e69a736 | 2019-11-14 12:33:52 -0800 | [diff] [blame] | 189 | if (FinalFilename.empty()) { // Unspecified output, infer it. |
Matthew Voss | 141bb5f | 2019-11-12 16:29:37 -0800 | [diff] [blame] | 190 | if (InputFilename == "-") { |
| 191 | FinalFilename = "-"; |
| 192 | } else { |
| 193 | StringRef IFN = InputFilename; |
| 194 | FinalFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str(); |
| 195 | if (N > 1) |
| 196 | FinalFilename += std::string(".") + std::to_string(i); |
| 197 | FinalFilename += ".ll"; |
| 198 | } |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 199 | } else { |
Matthew Voss | 141bb5f | 2019-11-12 16:29:37 -0800 | [diff] [blame] | 200 | if (N > 1) |
| 201 | FinalFilename += std::string(".") + std::to_string(i); |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 202 | } |
Matthew Voss | 141bb5f | 2019-11-12 16:29:37 -0800 | [diff] [blame] | 203 | |
| 204 | std::error_code EC; |
| 205 | std::unique_ptr<ToolOutputFile> Out( |
| 206 | new ToolOutputFile(FinalFilename, EC, sys::fs::OF_Text)); |
| 207 | if (EC) { |
| 208 | errs() << EC.message() << '\n'; |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | std::unique_ptr<AssemblyAnnotationWriter> Annotator; |
| 213 | if (ShowAnnotations) |
| 214 | Annotator.reset(new CommentWriter()); |
| 215 | |
| 216 | // All that llvm-dis does is write the assembly to a file. |
| 217 | if (!DontPrint) { |
| 218 | M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder); |
| 219 | if (Index) |
| 220 | Index->print(Out->os()); |
| 221 | } |
| 222 | |
| 223 | // Declare success. |
| 224 | Out->keep(); |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 225 | } |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 227 | return 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | } |