1 //===----------------------------------------------------------------------===//
4 // This utility may be invoked in the following manner:
5 // dis [options] - Read LLVM bytecode from stdin, write assembly to stdout
6 // dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
9 // --help - Output information about command line switches
10 // -c - Print C code instead of LLVM assembly
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/Bytecode/Reader.h"
16 #include "llvm/Support/CFG.h"
17 #include "Support/CommandLine.h"
18 #include "Support/Signals.h"
19 #include "llvm/Assembly/CWriter.h"
24 // OutputMode - The different orderings to print basic blocks in...
26 llvm = 0, // Generate LLVM assembly (the default)
30 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
31 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
32 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
33 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
34 clEnumVal(llvm, "Output LLVM assembly"),
35 clEnumVal(c , "Output C code for program"),
38 int main(int argc, char **argv) {
39 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
40 std::ostream *Out = &std::cout; // Default to printing to stdout...
42 Module *M = ParseBytecodeFile(InputFilename);
44 cerr << "bytecode didn't read correctly.\n";
48 if (OutputFilename != "") { // Specified an output filename?
49 if (!Force && std::ifstream(OutputFilename.c_str())) {
50 // If force is not specified, make sure not to overwrite a file!
51 cerr << "Error opening '" << OutputFilename
52 << "': File exists! Sending to standard output.\n";
54 Out = new std::ofstream(OutputFilename.c_str());
57 if (InputFilename == "-") {
60 std::string IFN = InputFilename;
61 int Len = IFN.length();
62 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
64 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
66 OutputFilename = IFN; // Append a .ll to it
69 OutputFilename += ".c";
71 OutputFilename += ".ll";
73 if (!Force && std::ifstream(OutputFilename.c_str())) {
74 // If force is not specified, make sure not to overwrite a file!
75 cerr << "Error opening '" << OutputFilename
76 << "': File exists! Sending to standard output.\n";
78 Out = new std::ofstream(OutputFilename.c_str());
80 // Make sure that the Out file gets unlink'd from the disk if we get a
82 RemoveFileOnSignal(OutputFilename);
88 cerr << "Error opening " << OutputFilename
89 << ": sending to stdout instead!\n";
93 // All that dis does is write the assembly or C out to a file...
97 (*Out) << M; // Output LLVM assembly
100 WriteToC(M, *Out); // Convert LLVM to C
105 if (Out != &std::cout) delete Out;