blob: 471e5e2f5d2c8c8a2f64f5320f849db65172124c [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
19#include "llvm/Module.h"
20#include "llvm/PassManager.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/Assembly/PrintModulePass.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Streams.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"
30#include <iostream>
31#include <fstream>
32#include <memory>
33using namespace llvm;
34
35static cl::opt<std::string>
36InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
37
38static cl::opt<std::string>
39OutputFilename("o", cl::desc("Override output filename"),
40 cl::value_desc("filename"));
41
42static cl::opt<bool>
43Force("f", cl::desc("Overwrite output files"));
44
45static cl::opt<bool>
46DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
47
48int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000049 // Print a stack trace if we signal out.
50 sys::PrintStackTraceOnErrorSignal();
51 PrettyStackTraceProgram X(argc, argv);
52
53 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
57 std::ostream *Out = &std::cout; // Default to printing to stdout.
58 std::string ErrorMessage;
59
60 std::auto_ptr<Module> M;
61
62 if (MemoryBuffer *Buffer
63 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
64 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
65 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?
81 if (!Force && std::ifstream(OutputFilename.c_str())) {
82 // If force is not specified, make sure not to overwrite a file!
83 cerr << argv[0] << ": error opening '" << OutputFilename
84 << "': file exists! Sending to standard output.\n";
85 } else {
86 Out = new std::ofstream(OutputFilename.c_str());
87 }
88 }
89 } else {
90 if (InputFilename == "-") {
91 OutputFilename = "-";
92 } else {
93 std::string IFN = InputFilename;
94 int Len = IFN.length();
95 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
96 // Source ends in .bc
97 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
98 } else {
99 OutputFilename = IFN+".ll";
100 }
101
102 if (!Force && std::ifstream(OutputFilename.c_str())) {
103 // If force is not specified, make sure not to overwrite a file!
104 cerr << argv[0] << ": error opening '" << OutputFilename
105 << "': file exists! Sending to standard output.\n";
106 } else {
107 Out = new std::ofstream(OutputFilename.c_str());
108
109 // Make sure that the Out file gets unlinked from the disk if we get a
110 // SIGINT
111 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
112 }
113 }
114 }
115
116 if (!Out->good()) {
117 cerr << argv[0] << ": error opening " << OutputFilename
118 << ": sending to stdout instead!\n";
119 Out = &std::cout;
120 }
121
122 // All that llvm-dis does is write the assembly to a file.
123 if (!DontPrint) {
124 PassManager Passes;
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000125 raw_os_ostream L(*Out);
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000126 Passes.add(createPrintModulePass(&L));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 Passes.run(*M.get());
128 }
129
130 if (Out != &std::cout) {
131 ((std::ofstream*)Out)->close();
132 delete Out;
133 }
134 return 0;
135 } catch (const std::string& msg) {
136 cerr << argv[0] << ": " << msg << "\n";
137 } catch (...) {
138 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
139 }
140
141 return 1;
142}
143