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