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