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/CWriter.h"
24 #include "llvm/Assembly/PrintModulePass.h"
25 #include "Support/CommandLine.h"
26 #include "Support/Signals.h"
30 // OutputMode - The different orderings to print basic blocks in...
32 LLVM = 0, // Generate LLVM assembly (the default)
38 static cl::opt<std::string>
39 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
41 static cl::opt<std::string>
42 OutputFilename("o", cl::desc("Override output filename"),
43 cl::value_desc("filename"));
46 Force("f", cl::desc("Overwrite output files"));
48 static cl::opt<enum OutputMode>
49 WriteMode(cl::desc("Specify the output format:"),
50 cl::values(clEnumValN(LLVM, "llvm", "Output LLVM assembly"),
51 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 std::ostream *Out = &std::cout; // Default to printing to stdout...
57 std::string ErrorMessage;
59 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
61 std::cerr << argv[0] << ": ";
62 if (ErrorMessage.size())
63 std::cerr << ErrorMessage << "\n";
65 std::cerr << "bytecode didn't read correctly.\n";
69 if (OutputFilename != "") { // Specified an output filename?
70 if (OutputFilename != "-") { // Not stdout?
71 if (!Force && std::ifstream(OutputFilename.c_str())) {
72 // If force is not specified, make sure not to overwrite a file!
73 std::cerr << argv[0] << ": error opening '" << OutputFilename
74 << "': file exists! Sending to standard output.\n";
76 Out = new std::ofstream(OutputFilename.c_str());
80 if (InputFilename == "-") {
83 std::string IFN = InputFilename;
84 int Len = IFN.length();
85 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
87 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
89 OutputFilename = IFN; // Append a .ll to it
92 OutputFilename += ".c";
94 OutputFilename += ".ll";
96 if (!Force && std::ifstream(OutputFilename.c_str())) {
97 // If force is not specified, make sure not to overwrite a file!
98 std::cerr << argv[0] << ": error opening '" << OutputFilename
99 << "': file exists! Sending to standard output.\n";
101 Out = new std::ofstream(OutputFilename.c_str());
103 // Make sure that the Out file gets unlinked from the disk if we get a
105 RemoveFileOnSignal(OutputFilename);
111 std::cerr << argv[0] << ": error opening " << OutputFilename
112 << ": sending to stdout instead!\n";
116 // All that dis does is write the assembly or C out to a file...
121 case LLVM: // Output LLVM assembly
122 Passes.add(new PrintModulePass(Out));
124 case c: // Convert LLVM to C
125 AddPassesToWriteC(Passes, *Out);
129 Passes.run(*M.get());
131 if (Out != &std::cout) {
132 ((std::ofstream*)Out)->close();