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 static cl::opt<std::string>
31 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Override output filename"),
35 cl::value_desc("filename"));
38 Force("f", cl::desc("Overwrite output files"));
40 static cl::opt<enum OutputMode>
41 WriteMode(cl::desc("Specify the output format:"),
43 clEnumVal(llvm, "Output LLVM assembly"),
44 clEnumVal(c , "Output C code for program"),
47 int main(int argc, char **argv) {
48 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
49 std::ostream *Out = &std::cout; // Default to printing to stdout...
51 Module *M = ParseBytecodeFile(InputFilename);
53 cerr << argv[0] << ": bytecode didn't read correctly.\n";
57 if (OutputFilename != "") { // Specified an output filename?
58 if (!Force && std::ifstream(OutputFilename.c_str())) {
59 // If force is not specified, make sure not to overwrite a file!
60 cerr << argv[0] << ": error opening '" << OutputFilename
61 << "': file exists! Sending to standard output.\n";
63 Out = new std::ofstream(OutputFilename.c_str());
66 if (InputFilename == "-") {
69 std::string IFN = InputFilename;
70 int Len = IFN.length();
71 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
73 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
75 OutputFilename = IFN; // Append a .ll to it
78 OutputFilename += ".c";
80 OutputFilename += ".ll";
82 if (!Force && std::ifstream(OutputFilename.c_str())) {
83 // If force is not specified, make sure not to overwrite a file!
84 cerr << argv[0] << ": error opening '" << OutputFilename
85 << "': file exists! Sending to standard output.\n";
87 Out = new std::ofstream(OutputFilename.c_str());
89 // Make sure that the Out file gets unlink'd from the disk if we get a
91 RemoveFileOnSignal(OutputFilename);
97 cerr << argv[0] << ": error opening " << OutputFilename
98 << ": sending to stdout instead!\n";
102 // All that dis does is write the assembly or C out to a file...
106 (*Out) << M; // Output LLVM assembly
109 WriteToC(M, *Out); // Convert LLVM to C
114 if (Out != &std::cout) delete Out;