blob: 0c87f8ce666ce180010600c9b4d63763b7b15947 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
27#include "llvm/System/Signals.h"
28#include <iostream>
29#include <fstream>
30#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) {
47 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
48 try {
49 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
50 sys::PrintStackTraceOnErrorSignal();
51
52 std::ostream *Out = &std::cout; // Default to printing to stdout.
53 std::string ErrorMessage;
54
55 std::auto_ptr<Module> M;
56
57 if (MemoryBuffer *Buffer
58 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
59 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
60 delete Buffer;
61 }
62
63 if (M.get() == 0) {
64 cerr << argv[0] << ": ";
65 if (ErrorMessage.size())
66 cerr << ErrorMessage << "\n";
67 else
68 cerr << "bitcode didn't read correctly.\n";
69 return 1;
70 }
71
72 if (DontPrint) {
73 // Just use stdout. We won't actually print anything on it.
74 } else if (OutputFilename != "") { // Specified an output filename?
75 if (OutputFilename != "-") { // Not stdout?
76 if (!Force && std::ifstream(OutputFilename.c_str())) {
77 // If force is not specified, make sure not to overwrite a file!
78 cerr << argv[0] << ": error opening '" << OutputFilename
79 << "': file exists! Sending to standard output.\n";
80 } else {
81 Out = new std::ofstream(OutputFilename.c_str());
82 }
83 }
84 } else {
85 if (InputFilename == "-") {
86 OutputFilename = "-";
87 } else {
88 std::string IFN = InputFilename;
89 int Len = IFN.length();
90 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
91 // Source ends in .bc
92 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
93 } else {
94 OutputFilename = IFN+".ll";
95 }
96
97 if (!Force && std::ifstream(OutputFilename.c_str())) {
98 // If force is not specified, make sure not to overwrite a file!
99 cerr << argv[0] << ": error opening '" << OutputFilename
100 << "': file exists! Sending to standard output.\n";
101 } else {
102 Out = new std::ofstream(OutputFilename.c_str());
103
104 // Make sure that the Out file gets unlinked from the disk if we get a
105 // SIGINT
106 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
107 }
108 }
109 }
110
111 if (!Out->good()) {
112 cerr << argv[0] << ": error opening " << OutputFilename
113 << ": sending to stdout instead!\n";
114 Out = &std::cout;
115 }
116
117 // All that llvm-dis does is write the assembly to a file.
118 if (!DontPrint) {
119 PassManager Passes;
120 OStream L(*Out);
121 Passes.add(new PrintModulePass(&L));
122 Passes.run(*M.get());
123 }
124
125 if (Out != &std::cout) {
126 ((std::ofstream*)Out)->close();
127 delete Out;
128 }
129 return 0;
130 } catch (const std::string& msg) {
131 cerr << argv[0] << ": " << msg << "\n";
132 } catch (...) {
133 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
134 }
135
136 return 1;
137}
138