Print the tool name when an error comes from so that I can tell which
[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/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"
20 #include <fstream>
21 #include <iostream>
22 using std::cerr;
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
51   Module *M = ParseBytecodeFile(InputFilename);
52   if (M == 0) {
53     cerr << argv[0] << ": bytecode didn't read correctly.\n";
54     return 1;
55   }
56   
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";
62     } else {
63       Out = new std::ofstream(OutputFilename.c_str());
64     }
65   } else {
66     if (InputFilename == "-") {
67       OutputFilename = "-";
68     } else {
69       std::string IFN = InputFilename;
70       int Len = IFN.length();
71       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
72         // Source ends in .bc
73         OutputFilename = std::string(IFN.begin(), IFN.end()-3);
74       } else {
75         OutputFilename = IFN;   // Append a .ll to it
76       }
77       if (WriteMode == c)
78         OutputFilename += ".c";
79       else
80         OutputFilename += ".ll";
81
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";
86       } else {
87         Out = new std::ofstream(OutputFilename.c_str());
88
89         // Make sure that the Out file gets unlink'd from the disk if we get a
90         // SIGINT
91         RemoveFileOnSignal(OutputFilename);
92       }
93     }
94   }
95
96   if (!Out->good()) {
97     cerr << argv[0] << ": error opening " << OutputFilename
98          << ": sending to stdout instead!\n";
99     Out = &std::cout;
100   }
101
102   // All that dis does is write the assembly or C out to a file...
103   //
104   switch (WriteMode) {
105   case llvm:
106     (*Out) << M;           // Output LLVM assembly
107     break;
108   case c:
109     WriteToC(M, *Out);     // Convert LLVM to C
110     break;
111   }
112   delete M;
113
114   if (Out != &std::cout) delete Out;
115   return 0;
116 }
117