Add support for: -o -
[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 //  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
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(
43                      clEnumVal(llvm, "Output LLVM assembly"),
44                      clEnumVal(c   , "Output C code for program"),
45                     0));
46
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...
50   std::string ErrorMessage;
51
52   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
53   if (M.get() == 0) {
54     std::cerr << argv[0] << ": ";
55     if (ErrorMessage.size())
56       std::cerr << ErrorMessage << "\n";
57     else
58       std::cerr << "bytecode didn't read correctly.\n";
59     return 1;
60   }
61   
62   if (OutputFilename != "") {   // Specified an output filename?
63     if (OutputFilename != "-") { // Not stdout?
64       if (!Force && std::ifstream(OutputFilename.c_str())) {
65         // If force is not specified, make sure not to overwrite a file!
66         std::cerr << argv[0] << ": error opening '" << OutputFilename
67                   << "': file exists! Sending to standard output.\n";
68       } else {
69         Out = new std::ofstream(OutputFilename.c_str());
70       }
71     }
72   } else {
73     if (InputFilename == "-") {
74       OutputFilename = "-";
75     } else {
76       std::string IFN = InputFilename;
77       int Len = IFN.length();
78       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
79         // Source ends in .bc
80         OutputFilename = std::string(IFN.begin(), IFN.end()-3);
81       } else {
82         OutputFilename = IFN;   // Append a .ll to it
83       }
84       if (WriteMode == c)
85         OutputFilename += ".c";
86       else
87         OutputFilename += ".ll";
88
89       if (!Force && std::ifstream(OutputFilename.c_str())) {
90         // If force is not specified, make sure not to overwrite a file!
91         std::cerr << argv[0] << ": error opening '" << OutputFilename
92                   << "': file exists! Sending to standard output.\n";
93       } else {
94         Out = new std::ofstream(OutputFilename.c_str());
95
96         // Make sure that the Out file gets unlink'd from the disk if we get a
97         // SIGINT
98         RemoveFileOnSignal(OutputFilename);
99       }
100     }
101   }
102
103   if (!Out->good()) {
104     std::cerr << argv[0] << ": error opening " << OutputFilename
105               << ": sending to stdout instead!\n";
106     Out = &std::cout;
107   }
108
109   // All that dis does is write the assembly or C out to a file...
110   //
111   PassManager Passes;
112
113   switch (WriteMode) {
114   case llvm:           // Output LLVM assembly
115     Passes.add(new PrintModulePass(Out));
116     break;
117   case c:              // Convert LLVM to C
118     Passes.add(createWriteToCPass(*Out));
119     break;
120   }
121
122   Passes.run(*M.get());
123
124   if (Out != &std::cout) {
125     ((std::ofstream*)Out)->close();
126     delete Out;
127   }
128   return 0;
129 }
130