blob: 55bb93f90ee3aeb89d233f161b5373cce86794b5 [file] [log] [blame]
Chris Lattner8f71f042003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Gabor Greife16561c2007-07-05 17:07:56 +000011// 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
Misha Brukmanf12549d2003-08-28 21:34:13 +000013// to the x.ll file.
Chris Lattnerf284ac52001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//
Chris Lattner6d56c6b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Owen Anderson6773d382009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000021#include "llvm/Type.h"
Chris Lattnere2244462007-04-22 06:31:02 +000022#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000023#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner7f2f0932010-09-02 23:21:44 +000025#include "llvm/Support/FormattedStream.h"
Chris Lattner76d46322006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattner6694f602007-04-29 07:54:31 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000029#include "llvm/Support/ToolOutputFile.h"
Chris Lattner278f5152004-05-27 05:41:36 +000030#include "llvm/System/Signals.h"
Brian Gaeke960707c2003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattner64a67272002-07-25 16:31:09 +000033static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000034InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000035
Chris Lattner64a67272002-07-25 16:31:09 +000036static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000037OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000038 cl::value_desc("filename"));
39
40static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000041Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000042
Chris Lattner216835d2007-02-07 04:39:35 +000043static cl::opt<bool>
44DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
45
Chris Lattner7f2f0932010-09-02 23:21:44 +000046static cl::opt<bool>
47ShowAnnotations("show-annotations",
48 cl::desc("Add informational comments to the .ll file"));
49
50namespace {
51
52class CommentWriter : public AssemblyAnnotationWriter {
53public:
54 void emitFunctionAnnot(const Function *F,
55 formatted_raw_ostream &OS) {
56 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
57 OS << '\n';
58 }
59 void printInfoComment(const Value &V, formatted_raw_ostream &OS) {
60 if (V.getType()->isVoidTy()) return;
61
62 OS.PadToColumn(50);
63 OS << "; [#uses=" << V.getNumUses() << ']'; // Output # uses
64 }
65};
66
67} // end anon namespace
68
Chris Lattner2f7c9632001-06-06 20:29:01 +000069int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000070 // Print a stack trace if we signal out.
71 sys::PrintStackTraceOnErrorSignal();
72 PrettyStackTraceProgram X(argc, argv);
73
Owen Anderson19251ec2009-07-15 22:16:10 +000074 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000075 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner9e6f1f12009-08-23 02:51:22 +000076
77
78 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattner12439ff2004-02-19 20:32:12 +000079
Chris Lattner9e6f1f12009-08-23 02:51:22 +000080 std::string ErrorMessage;
81 std::auto_ptr<Module> M;
82
83 if (MemoryBuffer *Buffer
84 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
85 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
86 delete Buffer;
Chris Lattner47c2e3e2002-09-24 00:09:48 +000087 }
Chris Lattner76d46322006-12-06 01:18:01 +000088
Chris Lattner9e6f1f12009-08-23 02:51:22 +000089 if (M.get() == 0) {
90 errs() << argv[0] << ": ";
91 if (ErrorMessage.size())
92 errs() << ErrorMessage << "\n";
93 else
94 errs() << "bitcode didn't read correctly.\n";
95 return 1;
96 }
97
98 // Just use stdout. We won't actually print anything on it.
99 if (DontPrint)
100 OutputFilename = "-";
101
102 if (OutputFilename.empty()) { // Unspecified output, infer it.
103 if (InputFilename == "-") {
104 OutputFilename = "-";
105 } else {
106 const std::string &IFN = InputFilename;
107 int Len = IFN.length();
108 // If the source ends in .bc, strip it off.
109 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
110 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
111 else
112 OutputFilename = IFN+".ll";
113 }
114 }
Dan Gohmane5929232009-09-11 20:46:33 +0000115
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000116 std::string ErrorInfo;
Dan Gohman268b0f42010-08-20 01:07:01 +0000117 OwningPtr<tool_output_file>
118 Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
119 raw_fd_ostream::F_Binary));
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000120 if (!ErrorInfo.empty()) {
121 errs() << ErrorInfo << '\n';
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000122 return 1;
123 }
124
Chris Lattner7f2f0932010-09-02 23:21:44 +0000125 OwningPtr<AssemblyAnnotationWriter> Annotator;
126 if (ShowAnnotations)
127 Annotator.reset(new CommentWriter());
128
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000129 // All that llvm-dis does is write the assembly to a file.
Dan Gohman972c9c52009-09-15 15:33:42 +0000130 if (!DontPrint)
Chris Lattner7f2f0932010-09-02 23:21:44 +0000131 M->print(Out->os(), Annotator.get());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000132
Dan Gohman268b0f42010-08-20 01:07:01 +0000133 // Declare success.
134 Out->keep();
135
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000136 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000138