blob: b8b1a39384cd766dcd7057130e07ce6d48cd5abe [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Bitcode/ReaderWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000026#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/System/Signals.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include <memory>
29using namespace llvm;
30
31static cl::opt<std::string>
32InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
33
34static cl::opt<std::string>
35OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
37
38static cl::opt<bool>
Dan Gohman176426d2009-08-25 15:34:52 +000039Force("f", cl::desc("Enable binary output on terminals"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41static cl::opt<bool>
42DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
43
44int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000045 // Print a stack trace if we signal out.
46 sys::PrintStackTraceOnErrorSignal();
47 PrettyStackTraceProgram X(argc, argv);
48
Owen Andersone84b8b32009-07-15 22:16:10 +000049 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +000050 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000051
52
53 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000055 std::string ErrorMessage;
56 std::auto_ptr<Module> M;
57
58 if (MemoryBuffer *Buffer
59 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
60 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
61 delete Buffer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 }
63
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000064 if (M.get() == 0) {
65 errs() << argv[0] << ": ";
66 if (ErrorMessage.size())
67 errs() << ErrorMessage << "\n";
68 else
69 errs() << "bitcode didn't read correctly.\n";
70 return 1;
71 }
72
73 // Just use stdout. We won't actually print anything on it.
74 if (DontPrint)
75 OutputFilename = "-";
76
77 if (OutputFilename.empty()) { // Unspecified output, infer it.
78 if (InputFilename == "-") {
79 OutputFilename = "-";
80 } else {
81 const std::string &IFN = InputFilename;
82 int Len = IFN.length();
83 // If the source ends in .bc, strip it off.
84 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
85 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
86 else
87 OutputFilename = IFN+".ll";
88 }
89 }
Dan Gohman31b20c72009-09-11 20:46:33 +000090
91 // Make sure that the Out file gets unlinked from the disk if we get a
92 // SIGINT.
93 if (OutputFilename != "-")
94 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
95
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000096 std::string ErrorInfo;
97 std::auto_ptr<raw_fd_ostream>
98 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000099 raw_fd_ostream::F_Binary));
100 if (!ErrorInfo.empty()) {
101 errs() << ErrorInfo << '\n';
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000102 return 1;
103 }
104
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000105 // All that llvm-dis does is write the assembly to a file.
Dan Gohman97b59da2009-09-15 15:33:42 +0000106 if (!DontPrint)
107 *Out << *M;
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000108
109 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110}
111