Pull iterators out of CFG.h and CFGdecls and put them in Support directory
[oota-llvm.git] / tools / llvm-dis / 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 <iostream.h>
20 #include <fstream.h>
21 #include "llvm/Module.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Bytecode/Reader.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Method.h"
26 #include "llvm/Support/DepthFirstIterator.h"
27 #include "llvm/Support/PostOrderIterator.h"
28
29 // OutputMode - The different orderings to print basic blocks in...
30 enum OutputMode {
31   Default = 0,           // Method Order (list order)
32   dfo,                   // Depth First ordering
33   rdfo,                  // Reverse Depth First ordering
34   po,                    // Post Order
35   rpo,                   // Reverse Post Order
36 };
37
38 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
39 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
40 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
41 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
42   clEnumVal(Default, "Write basic blocks in bytecode order"),
43   clEnumVal(dfo    , "Write basic blocks in depth first order"),
44   clEnumVal(rdfo   , "Write basic blocks in reverse DFO"),
45   clEnumVal(po     , "Write basic blocks in postorder"),
46   clEnumVal(rpo    , "Write basic blocks in reverse postorder"),
47  0);
48
49 int main(int argc, char **argv) {
50   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
51   ostream *Out = &cout;  // Default to printing to stdout...
52
53   Module *C = ParseBytecodeFile(InputFilename);
54   if (C == 0) {
55     cerr << "bytecode didn't read correctly.\n";
56     return 1;
57   }
58   
59   if (OutputFilename != "") {   // Specified an output filename?
60     Out = new ofstream(OutputFilename.c_str(), 
61                        (Force ? 0 : ios::noreplace)|ios::out);
62   } else {
63     if (InputFilename == "-") {
64       OutputFilename = "-";
65       Out = &cout;
66     } else {
67       string IFN = InputFilename;
68       int Len = IFN.length();
69       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
70         // Source ends in .bc
71         OutputFilename = string(IFN.begin(), IFN.end()-3);
72       } else {
73         OutputFilename = IFN;   // Append a .ll to it
74       }
75       OutputFilename += ".ll";
76       Out = new ofstream(OutputFilename.c_str(), 
77                          (Force ? 0 : ios::noreplace)|ios::out);
78     }
79   }
80
81   if (!Out->good()) {
82     cerr << "Error opening " << OutputFilename
83          << ": sending to stdout instead!\n";
84     Out = &cout;
85   }
86
87   // All that dis does is write the assembly out to a file... which is exactly
88   // what the writer library is supposed to do...
89   //
90   if (WriteMode == Default) {
91     (*Out) << C;           // Print out in list order
92   } else {
93     // TODO: This does not print anything other than the basic blocks in the
94     // methods... more should definately be printed.  It should be valid output
95     // consumable by the assembler.
96     //
97     for (Module::iterator I = C->begin(), End = C->end(); I != End; ++I) {
98       Method *M = *I;
99       (*Out) << "-------------- Method: " << M->getName() << " -------------\n";
100
101       switch (WriteMode) {
102       case dfo:                   // Depth First ordering
103         copy(df_begin(M), df_end(M),
104              ostream_iterator<BasicBlock*>(*Out, "\n"));
105         break;
106       case rdfo:            // Reverse Depth First ordering
107         copy(df_begin(M, true), df_end(M),
108              ostream_iterator<BasicBlock*>(*Out, "\n"));
109         break;
110       case po:                    // Post Order
111         copy(po_begin(M), po_end(M),
112              ostream_iterator<BasicBlock*>(*Out, "\n"));
113         break;
114       case rpo: {           // Reverse Post Order
115         ReversePostOrderTraversal RPOT(M);
116         copy(RPOT.begin(), RPOT.end(),
117              ostream_iterator<BasicBlock*>(*Out, "\n"));
118         break;
119       }
120       default:
121         abort();
122         break;
123       }
124     }
125   }
126   delete C;
127
128   if (Out != &cout) delete Out;
129   return 0;
130 }