blob: a12cf8c9762b9d1b6d97fcc1c8624785b21d0779 [file] [log] [blame]
Chris Lattner1dd27b12003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Gabor Greifa99be512007-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 Brukmanad0bf0f2003-08-28 21:34:13 +000013// to the x.ll file.
Chris Lattner9bff2e92001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner00950542001-06-06 20:29:01 +000016//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000018
Owen Anderson8b477ed2009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000021#include "llvm/Type.h"
Chris Lattnerb3ca3622007-04-22 06:31:02 +000022#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000023#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner6cd71f02010-09-02 23:21:44 +000025#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000029#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000030#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000031#include "llvm/Support/system_error.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Chris Lattnerc7a09852002-07-25 16:31:09 +000034static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000035InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000036
Chris Lattnerc7a09852002-07-25 16:31:09 +000037static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000038OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000039 cl::value_desc("filename"));
40
41static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000042Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000043
Chris Lattner4d544cd2007-02-07 04:39:35 +000044static cl::opt<bool>
45DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
46
Chris Lattner6cd71f02010-09-02 23:21:44 +000047static cl::opt<bool>
48ShowAnnotations("show-annotations",
49 cl::desc("Add informational comments to the .ll file"));
50
51namespace {
52
53class CommentWriter : public AssemblyAnnotationWriter {
54public:
55 void emitFunctionAnnot(const Function *F,
56 formatted_raw_ostream &OS) {
57 OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
58 OS << '\n';
59 }
60 void printInfoComment(const Value &V, formatted_raw_ostream &OS) {
61 if (V.getType()->isVoidTy()) return;
62
63 OS.PadToColumn(50);
64 OS << "; [#uses=" << V.getNumUses() << ']'; // Output # uses
65 }
66};
67
68} // end anon namespace
69
Chris Lattner00950542001-06-06 20:29:01 +000070int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000071 // Print a stack trace if we signal out.
72 sys::PrintStackTraceOnErrorSignal();
73 PrettyStackTraceProgram X(argc, argv);
74
Owen Anderson0d7c6952009-07-15 22:16:10 +000075 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +000076 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner17e9edc2009-08-23 02:51:22 +000077
78
79 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000080
Chris Lattner17e9edc2009-08-23 02:51:22 +000081 std::string ErrorMessage;
Michael J. Spencer333fb042010-12-09 17:36:48 +000082 error_code ec;
Chris Lattner17e9edc2009-08-23 02:51:22 +000083 std::auto_ptr<Module> M;
Michael J. Spencer3ff95632010-12-16 03:29:14 +000084 OwningPtr<MemoryBuffer> BufferPtr;
Michael J. Spencer333fb042010-12-09 17:36:48 +000085
Daniel Dunbar71d35002010-12-16 04:36:42 +000086 if ((ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)))
Michael J. Spencer333fb042010-12-09 17:36:48 +000087 ErrorMessage = ec.message();
Michael J. Spencer3ff95632010-12-16 03:29:14 +000088 else
89 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
Daniel Dunbar71d35002010-12-16 04:36:42 +000090 (void) BufferPtr.take();
Chris Lattnerc30598b2006-12-06 01:18:01 +000091
Chris Lattner17e9edc2009-08-23 02:51:22 +000092 if (M.get() == 0) {
93 errs() << argv[0] << ": ";
94 if (ErrorMessage.size())
95 errs() << ErrorMessage << "\n";
96 else
97 errs() << "bitcode didn't read correctly.\n";
98 return 1;
99 }
100
101 // Just use stdout. We won't actually print anything on it.
102 if (DontPrint)
103 OutputFilename = "-";
104
105 if (OutputFilename.empty()) { // Unspecified output, infer it.
106 if (InputFilename == "-") {
107 OutputFilename = "-";
108 } else {
109 const std::string &IFN = InputFilename;
110 int Len = IFN.length();
111 // If the source ends in .bc, strip it off.
112 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
113 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
114 else
115 OutputFilename = IFN+".ll";
116 }
117 }
Dan Gohmanec080462009-09-11 20:46:33 +0000118
Chris Lattner17e9edc2009-08-23 02:51:22 +0000119 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000120 OwningPtr<tool_output_file>
121 Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
122 raw_fd_ostream::F_Binary));
Chris Lattner17e9edc2009-08-23 02:51:22 +0000123 if (!ErrorInfo.empty()) {
124 errs() << ErrorInfo << '\n';
Chris Lattner17e9edc2009-08-23 02:51:22 +0000125 return 1;
126 }
127
Chris Lattner6cd71f02010-09-02 23:21:44 +0000128 OwningPtr<AssemblyAnnotationWriter> Annotator;
129 if (ShowAnnotations)
130 Annotator.reset(new CommentWriter());
131
Chris Lattner17e9edc2009-08-23 02:51:22 +0000132 // All that llvm-dis does is write the assembly to a file.
Dan Gohman04fc8c82009-09-15 15:33:42 +0000133 if (!DontPrint)
Chris Lattner6cd71f02010-09-02 23:21:44 +0000134 M->print(Out->os(), Annotator.get());
Chris Lattner17e9edc2009-08-23 02:51:22 +0000135
Dan Gohmand5826a32010-08-20 01:07:01 +0000136 // Declare success.
137 Out->keep();
138
Chris Lattner17e9edc2009-08-23 02:51:22 +0000139 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000140}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000141