1 //===----------------------------------------------------------------------===//
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
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
15 // TODO: add -vcg which prints VCG compatible output.
17 //===----------------------------------------------------------------------===//
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"
29 // OutputMode - The different orderings to print basic blocks in...
31 Default = 0, // Method Order (list order)
32 dfo, // Depth First ordering
33 rdfo, // Reverse Depth First ordering
35 rpo, // Reverse Post Order
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"),
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...
53 Module *C = ParseBytecodeFile(InputFilename);
55 cerr << "bytecode didn't read correctly.\n";
59 if (OutputFilename != "") { // Specified an output filename?
60 Out = new ofstream(OutputFilename.c_str(),
61 (Force ? 0 : ios::noreplace)|ios::out);
63 if (InputFilename == "-") {
67 string IFN = InputFilename;
68 int Len = IFN.length();
69 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
71 OutputFilename = string(IFN.begin(), IFN.end()-3);
73 OutputFilename = IFN; // Append a .ll to it
75 OutputFilename += ".ll";
76 Out = new ofstream(OutputFilename.c_str(),
77 (Force ? 0 : ios::noreplace)|ios::out);
82 cerr << "Error opening " << OutputFilename
83 << ": sending to stdout instead!\n";
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...
90 if (WriteMode == Default) {
91 (*Out) << C; // Print out in list order
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.
97 for (Module::iterator I = C->begin(), End = C->end(); I != End; ++I) {
99 (*Out) << "-------------- Method: " << M->getName() << " -------------\n";
102 case dfo: // Depth First ordering
103 copy(df_begin(M), df_end(M),
104 ostream_iterator<BasicBlock*>(*Out, "\n"));
106 case rdfo: // Reverse Depth First ordering
107 copy(df_begin(M, true), df_end(M),
108 ostream_iterator<BasicBlock*>(*Out, "\n"));
110 case po: // Post Order
111 copy(po_begin(M), po_end(M),
112 ostream_iterator<BasicBlock*>(*Out, "\n"));
114 case rpo: { // Reverse Post Order
115 ReversePostOrderTraversal RPOT(M);
116 copy(RPOT.begin(), RPOT.end(),
117 ostream_iterator<BasicBlock*>(*Out, "\n"));
128 if (Out != &cout) delete Out;