push bytecode decompressor out through APIs. Now the bytecode reader
[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/Compressor.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/Streams.h"
27 #include "llvm/System/Signals.h"
28 #include <iostream>
29 #include <fstream>
30 #include <memory>
31 using namespace llvm;
32
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
35
36 static cl::opt<std::string>
37 OutputFilename("o", cl::desc("Override output filename"),
38                cl::value_desc("filename"));
39
40 static cl::opt<bool>
41 Force("f", cl::desc("Overwrite output files"));
42
43 static cl::opt<bool>
44 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
45
46 int main(int argc, char **argv) {
47   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
48   try {
49     cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
50     sys::PrintStackTraceOnErrorSignal();
51
52     std::ostream *Out = &std::cout;  // Default to printing to stdout.
53     std::string ErrorMessage;
54
55     std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, 
56                                               Compressor::decompressToNewBuffer,
57                                               &ErrorMessage));
58     if (M.get() == 0) {
59       cerr << argv[0] << ": ";
60       if (ErrorMessage.size())
61         cerr << ErrorMessage << "\n";
62       else
63         cerr << "bytecode didn't read correctly.\n";
64       return 1;
65     }
66
67     if (DontPrint) {
68       // Just use stdout.  We won't actually print anything on it.
69     } else if (OutputFilename != "") {   // Specified an output filename?
70       if (OutputFilename != "-") { // Not stdout?
71         if (!Force && std::ifstream(OutputFilename.c_str())) {
72           // If force is not specified, make sure not to overwrite a file!
73           cerr << argv[0] << ": error opening '" << OutputFilename
74                << "': file exists! Sending to standard output.\n";
75         } else {
76           Out = new std::ofstream(OutputFilename.c_str());
77         }
78       }
79     } else {
80       if (InputFilename == "-") {
81         OutputFilename = "-";
82       } else {
83         std::string IFN = InputFilename;
84         int Len = IFN.length();
85         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
86           // Source ends in .bc
87           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
88         } else {
89           OutputFilename = IFN+".ll";
90         }
91
92         if (!Force && std::ifstream(OutputFilename.c_str())) {
93           // If force is not specified, make sure not to overwrite a file!
94           cerr << argv[0] << ": error opening '" << OutputFilename
95                << "': file exists! Sending to standard output.\n";
96         } else {
97           Out = new std::ofstream(OutputFilename.c_str());
98
99           // Make sure that the Out file gets unlinked from the disk if we get a
100           // SIGINT
101           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
102         }
103       }
104     }
105
106     if (!Out->good()) {
107       cerr << argv[0] << ": error opening " << OutputFilename
108            << ": sending to stdout instead!\n";
109       Out = &std::cout;
110     }
111
112     // All that llvm-dis does is write the assembly to a file.
113     if (!DontPrint) {
114       PassManager Passes;
115       OStream L(*Out);
116       Passes.add(new PrintModulePass(&L));
117       Passes.run(*M.get());
118     }
119
120     if (Out != &std::cout) {
121       ((std::ofstream*)Out)->close();
122       delete Out;
123     }
124     return 0;
125   } catch (const std::string& msg) {
126     cerr << argv[0] << ": " << msg << "\n";
127   } catch (...) {
128     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
129   }
130
131   return 1;
132 }
133