For PR351:
[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 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/System/Signals.h"
25 #include <fstream>
26 #include <memory>
27
28 using namespace llvm;
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<bool>
41 CWriteMode("c", cl::desc("Obsolete option, do not use"), cl::ReallyHidden);
42
43 int main(int argc, char **argv) {
44   try {
45     cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
46     sys::PrintStackTraceOnErrorSignal();
47
48     std::ostream *Out = &std::cout;  // Default to printing to stdout...
49     std::string ErrorMessage;
50
51     if (CWriteMode) {
52       std::cerr << "ERROR: llvm-dis no longer contains the C backend. "
53                 << "Use 'llc -march=c' instead!\n";
54       exit(1);
55     }
56
57     std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
58     if (M.get() == 0) {
59       std::cerr << argv[0] << ": ";
60       if (ErrorMessage.size())
61         std::cerr << ErrorMessage << "\n";
62       else
63         std::cerr << "bytecode didn't read correctly.\n";
64       return 1;
65     }
66     
67     if (OutputFilename != "") {   // Specified an output filename?
68       if (OutputFilename != "-") { // Not stdout?
69         if (!Force && std::ifstream(OutputFilename.c_str())) {
70           // If force is not specified, make sure not to overwrite a file!
71           std::cerr << argv[0] << ": error opening '" << OutputFilename
72                     << "': file exists! Sending to standard output.\n";
73         } else {
74           Out = new std::ofstream(OutputFilename.c_str());
75         }
76       }
77     } else {
78       if (InputFilename == "-") {
79         OutputFilename = "-";
80       } else {
81         std::string IFN = InputFilename;
82         int Len = IFN.length();
83         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
84           // Source ends in .bc
85           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
86         } else {
87           OutputFilename = IFN+".ll";
88         }
89
90         if (!Force && std::ifstream(OutputFilename.c_str())) {
91           // If force is not specified, make sure not to overwrite a file!
92           std::cerr << argv[0] << ": error opening '" << OutputFilename
93                     << "': file exists! Sending to standard output.\n";
94         } else {
95           Out = new std::ofstream(OutputFilename.c_str());
96
97           // Make sure that the Out file gets unlinked from the disk if we get a
98           // SIGINT
99           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
100         }
101       }
102     }
103
104     if (!Out->good()) {
105       std::cerr << argv[0] << ": error opening " << OutputFilename
106                 << ": sending to stdout instead!\n";
107       Out = &std::cout;
108     }
109
110     // All that dis does is write the assembly or C out to a file...
111     //
112     PassManager Passes;
113     Passes.add(new PrintModulePass(Out));
114     Passes.run(*M.get());
115
116     if (Out != &std::cout) {
117       ((std::ofstream*)Out)->close();
118       delete Out;
119     }
120     return 0;
121   } catch (const std::string& msg) {
122     std::cerr << argv[0] << ": " << msg << "\n";
123   } catch (...) {
124     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
125   }
126   return 1;
127 }
128