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 //===----------------------------------------------------------------------===//
19 #include "llvm/Module.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Method.h"
23 #include "Support/DepthFirstIterator.h"
24 #include "Support/PostOrderIterator.h"
25 #include "Support/CommandLine.h"
28 // OutputMode - The different orderings to print basic blocks in...
30 Default = 0, // Method Order (list order)
31 dfo, // Depth First ordering
32 rdfo, // Reverse Depth First ordering
34 rpo, // Reverse Post Order
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"),
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...
52 Module *C = ParseBytecodeFile(InputFilename);
54 cerr << "bytecode didn't read correctly.\n";
58 if (OutputFilename != "") { // Specified an output filename?
59 Out = new ofstream(OutputFilename.c_str(),
60 (Force ? 0 : ios::noreplace)|ios::out);
62 if (InputFilename == "-") {
66 string IFN = InputFilename;
67 int Len = IFN.length();
68 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
70 OutputFilename = string(IFN.begin(), IFN.end()-3);
72 OutputFilename = IFN; // Append a .ll to it
74 OutputFilename += ".ll";
75 Out = new ofstream(OutputFilename.c_str(),
76 (Force ? 0 : ios::noreplace)|ios::out);
81 cerr << "Error opening " << OutputFilename
82 << ": sending to stdout instead!\n";
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...
89 if (WriteMode == Default) {
90 (*Out) << C; // Print out in list order
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.
96 for (Module::iterator I = C->begin(), End = C->end(); I != End; ++I) {
98 (*Out) << "-------------- Method: " << M->getName() << " -------------\n";
101 case dfo: // Depth First ordering
102 copy(df_begin(M), df_end(M),
103 ostream_iterator<BasicBlock*>(*Out, "\n"));
105 case rdfo: // Reverse Depth First ordering
106 copy(df_begin(M, true), df_end(M),
107 ostream_iterator<BasicBlock*>(*Out, "\n"));
109 case po: // Post Order
110 copy(po_begin(M), po_end(M),
111 ostream_iterator<BasicBlock*>(*Out, "\n"));
113 case rpo: { // Reverse Post Order
114 ReversePostOrderTraversal RPOT(M);
115 copy(RPOT.begin(), RPOT.end(),
116 ostream_iterator<BasicBlock*>(*Out, "\n"));
127 if (Out != &cout) delete Out;