clean up comments
[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 int main(int argc, char **argv) {
41   try {
42     cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
43     sys::PrintStackTraceOnErrorSignal();
44
45     std::ostream *Out = &std::cout;  // Default to printing to stdout.
46     std::string ErrorMessage;
47
48     std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
49     if (M.get() == 0) {
50       std::cerr << argv[0] << ": ";
51       if (ErrorMessage.size())
52         std::cerr << ErrorMessage << "\n";
53       else
54         std::cerr << "bytecode didn't read correctly.\n";
55       return 1;
56     }
57     
58     if (OutputFilename != "") {   // Specified an output filename?
59       if (OutputFilename != "-") { // Not stdout?
60         if (!Force && std::ifstream(OutputFilename.c_str())) {
61           // If force is not specified, make sure not to overwrite a file!
62           std::cerr << argv[0] << ": error opening '" << OutputFilename
63                     << "': file exists! Sending to standard output.\n";
64         } else {
65           Out = new std::ofstream(OutputFilename.c_str());
66         }
67       }
68     } else {
69       if (InputFilename == "-") {
70         OutputFilename = "-";
71       } else {
72         std::string IFN = InputFilename;
73         int Len = IFN.length();
74         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
75           // Source ends in .bc
76           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
77         } else {
78           OutputFilename = IFN+".ll";
79         }
80
81         if (!Force && std::ifstream(OutputFilename.c_str())) {
82           // If force is not specified, make sure not to overwrite a file!
83           std::cerr << argv[0] << ": error opening '" << OutputFilename
84                     << "': file exists! Sending to standard output.\n";
85         } else {
86           Out = new std::ofstream(OutputFilename.c_str());
87
88           // Make sure that the Out file gets unlinked from the disk if we get a
89           // SIGINT
90           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
91         }
92       }
93     }
94
95     if (!Out->good()) {
96       std::cerr << argv[0] << ": error opening " << OutputFilename
97                 << ": sending to stdout instead!\n";
98       Out = &std::cout;
99     }
100
101     // All that llvm-dis does is write the assembly to a file.
102     PassManager Passes;
103     Passes.add(new PrintModulePass(Out));
104     Passes.run(*M.get());
105
106     if (Out != &std::cout) {
107       ((std::ofstream*)Out)->close();
108       delete Out;
109     }
110     return 0;
111   } catch (const std::string& msg) {
112     std::cerr << argv[0] << ": " << msg << "\n";
113   } catch (...) {
114     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
115   }
116   return 1;
117 }
118