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