blob: 2066fffd2904eb343ed9fc19ce866bbd1e6b363d [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"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/System/Signals.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include <memory>
31using namespace llvm;
32
33static cl::opt<std::string>
34InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
35
36static cl::opt<std::string>
37OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
43static cl::opt<bool>
44DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
45
46int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000047 // Print a stack trace if we signal out.
48 sys::PrintStackTraceOnErrorSignal();
49 PrettyStackTraceProgram X(argc, argv);
50
Owen Andersone84b8b32009-07-15 22:16:10 +000051 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +000052 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000053
54
55 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000057 std::string ErrorMessage;
58 std::auto_ptr<Module> M;
59
60 if (MemoryBuffer *Buffer
61 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
62 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
63 delete Buffer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 }
65
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000066 if (M.get() == 0) {
67 errs() << argv[0] << ": ";
68 if (ErrorMessage.size())
69 errs() << ErrorMessage << "\n";
70 else
71 errs() << "bitcode didn't read correctly.\n";
72 return 1;
73 }
74
75 // Just use stdout. We won't actually print anything on it.
76 if (DontPrint)
77 OutputFilename = "-";
78
79 if (OutputFilename.empty()) { // Unspecified output, infer it.
80 if (InputFilename == "-") {
81 OutputFilename = "-";
82 } else {
83 const std::string &IFN = InputFilename;
84 int Len = IFN.length();
85 // If the source ends in .bc, strip it off.
86 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
87 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
88 else
89 OutputFilename = IFN+".ll";
90 }
91 }
92
93 std::string ErrorInfo;
94 std::auto_ptr<raw_fd_ostream>
95 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
96 (Force?raw_fd_ostream::F_Force : 0) |
97 raw_fd_ostream::F_Binary));
98 if (!ErrorInfo.empty()) {
99 errs() << ErrorInfo << '\n';
100 if (!Force)
101 errs() << "Use -f command line argument to force output\n";
102 return 1;
103 }
104
105 // Make sure that the Out file gets unlinked from the disk if we get a
106 // SIGINT.
107 if (OutputFilename != "-")
108 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
109
110 // All that llvm-dis does is write the assembly to a file.
111 if (!DontPrint) {
112 PassManager Passes;
113 Passes.add(createPrintModulePass(Out.get()));
114 Passes.run(*M.get());
115 }
116
117 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118}
119