fix file header
[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/CWriter.h"
24 #include "llvm/Assembly/PrintModulePass.h"
25 #include "Support/CommandLine.h"
26 #include "Support/Signals.h"
27 #include <fstream>
28 #include <memory>
29
30 // OutputMode - The different orderings to print basic blocks in...
31 enum OutputMode {
32   llvm = 0,           // Generate LLVM assembly (the default)
33   c,                  // Generate C code
34 };
35
36 static cl::opt<std::string>
37 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
38
39 static cl::opt<std::string>
40 OutputFilename("o", cl::desc("Override output filename"), 
41                cl::value_desc("filename"));
42
43 static cl::opt<bool>
44 Force("f", cl::desc("Overwrite output files"));
45
46 static cl::opt<enum OutputMode>
47 WriteMode(cl::desc("Specify the output format:"),
48           cl::values(clEnumVal(llvm, "Output LLVM assembly"),
49                      clEnumVal(c   , "Output C code for program"),
50                     0));
51
52 int main(int argc, char **argv) {
53   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
54   std::ostream *Out = &std::cout;  // Default to printing to stdout...
55   std::string ErrorMessage;
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);
86       } else {
87         OutputFilename = IFN;   // Append a .ll to it
88       }
89       if (WriteMode == c)
90         OutputFilename += ".c";
91       else
92         OutputFilename += ".ll";
93
94       if (!Force && std::ifstream(OutputFilename.c_str())) {
95         // If force is not specified, make sure not to overwrite a file!
96         std::cerr << argv[0] << ": error opening '" << OutputFilename
97                   << "': file exists! Sending to standard output.\n";
98       } else {
99         Out = new std::ofstream(OutputFilename.c_str());
100
101         // Make sure that the Out file gets unlinked from the disk if we get a
102         // SIGINT
103         RemoveFileOnSignal(OutputFilename);
104       }
105     }
106   }
107
108   if (!Out->good()) {
109     std::cerr << argv[0] << ": error opening " << OutputFilename
110               << ": sending to stdout instead!\n";
111     Out = &std::cout;
112   }
113
114   // All that dis does is write the assembly or C out to a file...
115   //
116   PassManager Passes;
117
118   switch (WriteMode) {
119   case llvm:           // Output LLVM assembly
120     Passes.add(new PrintModulePass(Out));
121     break;
122   case c:              // Convert LLVM to C
123     Passes.add(createWriteToCPass(*Out));
124     break;
125   }
126
127   Passes.run(*M.get());
128
129   if (Out != &std::cout) {
130     ((std::ofstream*)Out)->close();
131     delete Out;
132   }
133   return 0;
134 }
135