1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This utility may be invoked in the following manner:
11 // llvm-dis [options] - Read LLVM bytecode from stdin, write asm to stdout
12 // llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
15 // --help - Output information about command line switches
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Bytecode/Reader.h"
23 #include "llvm/Assembly/PrintModulePass.h"
24 #include "llvm/Support/Compressor.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Streams.h"
29 #include "llvm/System/Signals.h"
35 static cl::opt<std::string>
36 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
38 static cl::opt<std::string>
39 OutputFilename("o", cl::desc("Override output filename"),
40 cl::value_desc("filename"));
43 Force("f", cl::desc("Overwrite output files"));
46 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
49 Bitcode("bitcode", cl::desc("Read a bitcode file"));
51 int main(int argc, char **argv) {
52 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
54 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
55 sys::PrintStackTraceOnErrorSignal();
57 std::ostream *Out = &std::cout; // Default to printing to stdout.
58 std::string ErrorMessage;
60 std::auto_ptr<Module> M;
64 if (InputFilename == "-") {
65 Buffer = MemoryBuffer::getSTDIN();
67 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
71 ErrorMessage = "Error reading file '" + InputFilename + "'";
73 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
77 M.reset(ParseBytecodeFile(InputFilename,
78 Compressor::decompressToNewBuffer,
83 cerr << argv[0] << ": ";
84 if (ErrorMessage.size())
85 cerr << ErrorMessage << "\n";
87 cerr << "bytecode didn't read correctly.\n";
92 // Just use stdout. We won't actually print anything on it.
93 } else if (OutputFilename != "") { // Specified an output filename?
94 if (OutputFilename != "-") { // Not stdout?
95 if (!Force && std::ifstream(OutputFilename.c_str())) {
96 // If force is not specified, make sure not to overwrite a file!
97 cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists! Sending to standard output.\n";
100 Out = new std::ofstream(OutputFilename.c_str());
104 if (InputFilename == "-") {
105 OutputFilename = "-";
107 std::string IFN = InputFilename;
108 int Len = IFN.length();
109 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
110 // Source ends in .bc
111 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
113 OutputFilename = IFN+".ll";
116 if (!Force && std::ifstream(OutputFilename.c_str())) {
117 // If force is not specified, make sure not to overwrite a file!
118 cerr << argv[0] << ": error opening '" << OutputFilename
119 << "': file exists! Sending to standard output.\n";
121 Out = new std::ofstream(OutputFilename.c_str());
123 // Make sure that the Out file gets unlinked from the disk if we get a
125 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
131 cerr << argv[0] << ": error opening " << OutputFilename
132 << ": sending to stdout instead!\n";
136 // All that llvm-dis does is write the assembly to a file.
140 Passes.add(new PrintModulePass(&L));
141 Passes.run(*M.get());
144 if (Out != &std::cout) {
145 ((std::ofstream*)Out)->close();
149 } catch (const std::string& msg) {
150 cerr << argv[0] << ": " << msg << "\n";
152 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";