Renaming LLVM `dis' to `llvm-dis'.
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'DIS' UTILITY 
3 //
4 // This utility may be invoked in the following manner:
5 //  llvm-dis [options]      - Read LLVM bytecode from stdin, write asm to stdout
6 //  llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
7 //                            to the x.ll file.
8 //  Options:
9 //      --help   - Output information about command line switches
10 //       -c      - Print C code instead of LLVM assembly
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Module.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Assembly/CWriter.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
21 #include <fstream>
22 #include <memory>
23
24 // OutputMode - The different orderings to print basic blocks in...
25 enum OutputMode {
26   llvm = 0,           // Generate LLVM assembly (the default)
27   c,                  // Generate C code
28 };
29
30 static cl::opt<std::string>
31 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
32
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Override output filename"), 
35                cl::value_desc("filename"));
36
37 static cl::opt<bool>
38 Force("f", cl::desc("Overwrite output files"));
39
40 static cl::opt<enum OutputMode>
41 WriteMode(cl::desc("Specify the output format:"),
42           cl::values(clEnumVal(llvm, "Output LLVM assembly"),
43                      clEnumVal(c   , "Output C code for program"),
44                     0));
45
46 int main(int argc, char **argv) {
47   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
48   std::ostream *Out = &std::cout;  // Default to printing to stdout...
49   std::string ErrorMessage;
50
51   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
52   if (M.get() == 0) {
53     std::cerr << argv[0] << ": ";
54     if (ErrorMessage.size())
55       std::cerr << ErrorMessage << "\n";
56     else
57       std::cerr << "bytecode didn't read correctly.\n";
58     return 1;
59   }
60   
61   if (OutputFilename != "") {   // Specified an output filename?
62     if (OutputFilename != "-") { // Not stdout?
63       if (!Force && std::ifstream(OutputFilename.c_str())) {
64         // If force is not specified, make sure not to overwrite a file!
65         std::cerr << argv[0] << ": error opening '" << OutputFilename
66                   << "': file exists! Sending to standard output.\n";
67       } else {
68         Out = new std::ofstream(OutputFilename.c_str());
69       }
70     }
71   } else {
72     if (InputFilename == "-") {
73       OutputFilename = "-";
74     } else {
75       std::string IFN = InputFilename;
76       int Len = IFN.length();
77       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
78         // Source ends in .bc
79         OutputFilename = std::string(IFN.begin(), IFN.end()-3);
80       } else {
81         OutputFilename = IFN;   // Append a .ll to it
82       }
83       if (WriteMode == c)
84         OutputFilename += ".c";
85       else
86         OutputFilename += ".ll";
87
88       if (!Force && std::ifstream(OutputFilename.c_str())) {
89         // If force is not specified, make sure not to overwrite a file!
90         std::cerr << argv[0] << ": error opening '" << OutputFilename
91                   << "': file exists! Sending to standard output.\n";
92       } else {
93         Out = new std::ofstream(OutputFilename.c_str());
94
95         // Make sure that the Out file gets unlink'd from the disk if we get a
96         // SIGINT
97         RemoveFileOnSignal(OutputFilename);
98       }
99     }
100   }
101
102   if (!Out->good()) {
103     std::cerr << argv[0] << ": error opening " << OutputFilename
104               << ": sending to stdout instead!\n";
105     Out = &std::cout;
106   }
107
108   // All that dis does is write the assembly or C out to a file...
109   //
110   PassManager Passes;
111
112   switch (WriteMode) {
113   case llvm:           // Output LLVM assembly
114     Passes.add(new PrintModulePass(Out));
115     break;
116   case c:              // Convert LLVM to C
117     Passes.add(createWriteToCPass(*Out));
118     break;
119   }
120
121   Passes.run(*M.get());
122
123   if (Out != &std::cout) {
124     ((std::ofstream*)Out)->close();
125     delete Out;
126   }
127   return 0;
128 }
129