blob: 8fa00d5b032bfd4dc190b8f69718c95797c6df4b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This utility may be invoked in the following manner:
11// 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
13// to the x.ll file.
14// Options:
15// --help - Output information about command line switches
16//
17//===----------------------------------------------------------------------===//
18
Owen Anderson25209b42009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Module.h"
21#include "llvm/PassManager.h"
22#include "llvm/Bitcode/ReaderWriter.h"
23#include "llvm/Assembly/PrintModulePass.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000027#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Streams.h"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/System/Signals.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include <memory>
32using namespace llvm;
33
34static cl::opt<std::string>
35InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
36
37static cl::opt<std::string>
38OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Force("f", cl::desc("Overwrite output files"));
43
44static cl::opt<bool>
45DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
46
47int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000048 // Print a stack trace if we signal out.
49 sys::PrintStackTraceOnErrorSignal();
50 PrettyStackTraceProgram X(argc, argv);
51
Owen Anderson25209b42009-07-01 16:58:40 +000052 LLVMContext Context;
Chris Lattnere6012df2009-03-06 05:34:10 +000053 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 try {
Dan Gohman6099df82007-10-08 15:45:12 +000055 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
Dan Gohmandd7ca8e2009-07-15 17:29:42 +000057 raw_ostream *Out = &outs(); // Default to printing to stdout.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 std::string ErrorMessage;
59
60 std::auto_ptr<Module> M;
61
62 if (MemoryBuffer *Buffer
63 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
Owen Andersona148fdd2009-07-01 21:22:36 +000064 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 delete Buffer;
66 }
67
68 if (M.get() == 0) {
69 cerr << argv[0] << ": ";
70 if (ErrorMessage.size())
71 cerr << ErrorMessage << "\n";
72 else
73 cerr << "bitcode didn't read correctly.\n";
74 return 1;
75 }
76
77 if (DontPrint) {
78 // Just use stdout. We won't actually print anything on it.
79 } else if (OutputFilename != "") { // Specified an output filename?
80 if (OutputFilename != "-") { // Not stdout?
Dan Gohmandd7ca8e2009-07-15 17:29:42 +000081 std::string ErrorInfo;
82 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
83 Force, ErrorInfo);
84 if (!ErrorInfo.empty()) {
85 errs() << ErrorInfo << '\n';
86 if (!Force)
87 errs() << "Use -f command line argument to force output\n";
88 delete Out;
89 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 }
91 }
92 } else {
93 if (InputFilename == "-") {
94 OutputFilename = "-";
95 } else {
96 std::string IFN = InputFilename;
97 int Len = IFN.length();
98 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
99 // Source ends in .bc
100 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
101 } else {
102 OutputFilename = IFN+".ll";
103 }
104
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000105 std::string ErrorInfo;
106 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
107 Force, ErrorInfo);
108 if (!ErrorInfo.empty()) {
109 errs() << ErrorInfo << '\n';
110 if (!Force)
111 errs() << "Use -f command line argument to force output\n";
112 delete Out;
113 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000116 // Make sure that the Out file gets unlinked from the disk if we get a
117 // SIGINT
118 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
119 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 }
121
122 // All that llvm-dis does is write the assembly to a file.
123 if (!DontPrint) {
124 PassManager Passes;
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000125 Passes.add(createPrintModulePass(Out));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 Passes.run(*M.get());
127 }
128
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000129 if (Out != &outs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 return 0;
132 } catch (const std::string& msg) {
133 cerr << argv[0] << ": " << msg << "\n";
134 } catch (...) {
135 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
136 }
137
138 return 1;
139}
140