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