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
16 // -c - Print C code instead of LLVM assembly
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Bytecode/Reader.h"
23 #include "llvm/Assembly/PrintModulePass.h"
24 #include "Support/CommandLine.h"
25 #include "llvm/System/Signals.h"
29 // OutputMode - The different orderings to print basic blocks in...
31 LLVM = 0, // Generate LLVM assembly (the default)
37 static cl::opt<std::string>
38 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
40 static cl::opt<std::string>
41 OutputFilename("o", cl::desc("Override output filename"),
42 cl::value_desc("filename"));
45 Force("f", cl::desc("Overwrite output files"));
47 static cl::opt<enum OutputMode>
48 WriteMode(cl::desc("Specify the output format:"),
49 cl::values(clEnumValN(LLVM, "llvm", "Output LLVM assembly"),
50 clEnumVal(c, "Output C code for program"),
54 int main(int argc, char **argv) {
55 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
56 PrintStackTraceOnErrorSignal();
58 std::ostream *Out = &std::cout; // Default to printing to stdout...
59 std::string ErrorMessage;
62 std::cerr << "ERROR: llvm-dis no longer contains the C backend. "
63 << "Use 'llc -march=c' instead!\n";
67 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
69 std::cerr << argv[0] << ": ";
70 if (ErrorMessage.size())
71 std::cerr << ErrorMessage << "\n";
73 std::cerr << "bytecode didn't read correctly.\n";
77 if (OutputFilename != "") { // Specified an output filename?
78 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 std::cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists! Sending to standard output.\n";
84 Out = new std::ofstream(OutputFilename.c_str());
88 if (InputFilename == "-") {
91 std::string IFN = InputFilename;
92 int Len = IFN.length();
93 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
95 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
97 OutputFilename = IFN+".ll";
100 if (!Force && std::ifstream(OutputFilename.c_str())) {
101 // If force is not specified, make sure not to overwrite a file!
102 std::cerr << argv[0] << ": error opening '" << OutputFilename
103 << "': file exists! Sending to standard output.\n";
105 Out = new std::ofstream(OutputFilename.c_str());
107 // Make sure that the Out file gets unlinked from the disk if we get a
109 RemoveFileOnSignal(OutputFilename);
115 std::cerr << argv[0] << ": error opening " << OutputFilename
116 << ": sending to stdout instead!\n";
120 // All that dis does is write the assembly or C out to a file...
123 Passes.add(new PrintModulePass(Out));
124 Passes.run(*M.get());
126 if (Out != &std::cout) {
127 ((std::ofstream*)Out)->close();