blob: 89f19e1daee27c4fb9777970dd356a79f4e2513f [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"
26#include "llvm/Support/Streams.h"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000027#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/System/Signals.h"
29#include <iostream>
30#include <fstream>
31#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) {
48 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
49 try {
Dan Gohman6099df82007-10-08 15:45:12 +000050 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 sys::PrintStackTraceOnErrorSignal();
52
53 std::ostream *Out = &std::cout; // Default to printing to stdout.
54 std::string ErrorMessage;
55
56 std::auto_ptr<Module> M;
57
58 if (MemoryBuffer *Buffer
59 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
60 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
61 delete Buffer;
62 }
63
64 if (M.get() == 0) {
65 cerr << argv[0] << ": ";
66 if (ErrorMessage.size())
67 cerr << ErrorMessage << "\n";
68 else
69 cerr << "bitcode didn't read correctly.\n";
70 return 1;
71 }
72
73 if (DontPrint) {
74 // Just use stdout. We won't actually print anything on it.
75 } else if (OutputFilename != "") { // Specified an output filename?
76 if (OutputFilename != "-") { // Not stdout?
77 if (!Force && std::ifstream(OutputFilename.c_str())) {
78 // If force is not specified, make sure not to overwrite a file!
79 cerr << argv[0] << ": error opening '" << OutputFilename
80 << "': file exists! Sending to standard output.\n";
81 } else {
82 Out = new std::ofstream(OutputFilename.c_str());
83 }
84 }
85 } else {
86 if (InputFilename == "-") {
87 OutputFilename = "-";
88 } else {
89 std::string IFN = InputFilename;
90 int Len = IFN.length();
91 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
92 // Source ends in .bc
93 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
94 } else {
95 OutputFilename = IFN+".ll";
96 }
97
98 if (!Force && std::ifstream(OutputFilename.c_str())) {
99 // If force is not specified, make sure not to overwrite a file!
100 cerr << argv[0] << ": error opening '" << OutputFilename
101 << "': file exists! Sending to standard output.\n";
102 } else {
103 Out = new std::ofstream(OutputFilename.c_str());
104
105 // Make sure that the Out file gets unlinked from the disk if we get a
106 // SIGINT
107 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
108 }
109 }
110 }
111
112 if (!Out->good()) {
113 cerr << argv[0] << ": error opening " << OutputFilename
114 << ": sending to stdout instead!\n";
115 Out = &std::cout;
116 }
117
118 // All that llvm-dis does is write the assembly to a file.
119 if (!DontPrint) {
120 PassManager Passes;
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000121 raw_os_ostream L(*Out);
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000122 Passes.add(createPrintModulePass(&L));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 Passes.run(*M.get());
124 }
125
126 if (Out != &std::cout) {
127 ((std::ofstream*)Out)->close();
128 delete Out;
129 }
130 return 0;
131 } catch (const std::string& msg) {
132 cerr << argv[0] << ": " << msg << "\n";
133 } catch (...) {
134 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
135 }
136
137 return 1;
138}
139