Remove extranous #include
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'DIS' UTILITY 
3 //
4 // This utility may be invoked in the following manner:
5 //  dis [options]      - Read LLVM bytecode from stdin, write assembly to stdout
6 //  dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
7 //                       to the x.ll file.
8 //  Options:
9 //      --help   - Output information about command line switches
10 //       -dfo    - Print basic blocks in depth first order
11 //       -rdfo   - Print basic blocks in reverse depth first order
12 //       -po     - Print basic blocks in post order
13 //       -rpo    - Print basic blocks in reverse post order
14 //
15 // TODO: add -vcg which prints VCG compatible output.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Module.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "llvm/Function.h"
22 #include "llvm/Support/CFG.h"
23 #include "Support/DepthFirstIterator.h"
24 #include "Support/PostOrderIterator.h"
25 #include "Support/CommandLine.h"
26 #include <fstream>
27 #include <iostream>
28 using std::cerr;
29
30 // OutputMode - The different orderings to print basic blocks in...
31 enum OutputMode {
32   Default = 0,           // Function Order (list order)
33   dfo,                   // Depth First ordering
34   rdfo,                  // Reverse Depth First ordering
35   po,                    // Post Order
36   rpo,                   // Reverse Post Order
37 };
38
39 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
40 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
41 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
42 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
43   clEnumVal(Default, "Write basic blocks in bytecode order"),
44   clEnumVal(dfo    , "Write basic blocks in depth first order"),
45   clEnumVal(rdfo   , "Write basic blocks in reverse DFO"),
46   clEnumVal(po     , "Write basic blocks in postorder"),
47   clEnumVal(rpo    , "Write basic blocks in reverse postorder"),
48  0);
49
50 int main(int argc, char **argv) {
51   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
52   std::ostream *Out = &std::cout;  // Default to printing to stdout...
53
54   Module *M = ParseBytecodeFile(InputFilename);
55   if (M == 0) {
56     cerr << "bytecode didn't read correctly.\n";
57     return 1;
58   }
59   
60   if (OutputFilename != "") {   // Specified an output filename?
61     if (!Force && std::ifstream(OutputFilename.c_str())) {
62       // If force is not specified, make sure not to overwrite a file!
63       cerr << "Error opening '" << OutputFilename
64            << "': File exists! Sending to standard output.\n";
65     } else {
66       Out = new std::ofstream(OutputFilename.c_str());
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);
77       } else {
78         OutputFilename = IFN;   // Append a .ll to it
79       }
80       OutputFilename += ".ll";
81
82       if (!Force && std::ifstream(OutputFilename.c_str())) {
83         // If force is not specified, make sure not to overwrite a file!
84         cerr << "Error opening '" << OutputFilename
85              << "': File exists! Sending to standard output.\n";
86       } else {
87         Out = new std::ofstream(OutputFilename.c_str());
88       }
89     }
90   }
91
92   if (!Out->good()) {
93     cerr << "Error opening " << OutputFilename
94          << ": sending to stdout instead!\n";
95     Out = &std::cout;
96   }
97
98   // All that dis does is write the assembly out to a file... which is exactly
99   // what the writer library is supposed to do...
100   //
101   if (WriteMode == Default) {
102     (*Out) << M;           // Print out in list order
103   } else {
104     // TODO: This does not print anything other than the basic blocks in the
105     // functions... more should definately be printed.  It should be valid
106     // output consumable by the assembler.
107     //
108     for (Module::iterator I = M->begin(), End = M->end(); I != End; ++I) {
109       Function *F = *I;
110       (*Out) << "-------------- Method: " << F->getName() << " -------------\n";
111
112       switch (WriteMode) {
113       case dfo:                   // Depth First ordering
114         copy(df_begin(F), df_end(F),
115              std::ostream_iterator<BasicBlock*>(*Out, "\n"));
116         break;
117       case rdfo:            // Reverse Depth First ordering
118         copy(df_begin(F, true), df_end(F),
119              std::ostream_iterator<BasicBlock*>(*Out, "\n"));
120         break;
121       case po:                    // Post Order
122         copy(po_begin(F), po_end(F),
123              std::ostream_iterator<BasicBlock*>(*Out, "\n"));
124         break;
125       case rpo: {           // Reverse Post Order
126 #if 0  // FIXME, GCC 3.0.4 bug
127         ReversePostOrderTraversal<Function*> RPOT(F);
128         copy(RPOT.begin(), RPOT.end(),
129              std::ostream_iterator<BasicBlock*>(*Out, "\n"));
130 #endif
131         break;
132       }
133       default:
134         abort();
135         break;
136       }
137     }
138   }
139   delete M;
140
141   if (Out != &std::cout) delete Out;
142   return 0;
143 }