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/Tools/CommandLine.h"
25 #include "llvm/Method.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", 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);
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...
51 Module *C = ParseBytecodeFile(InputFilename.getValue());
53 cerr << "bytecode didn't read correctly.\n";
57 if (OutputFilename.getValue() != "") { // Specified an output filename?
58 Out = new ofstream(OutputFilename.getValue().c_str(),
59 (Force.getValue() ? 0 : ios::noreplace)|ios::out);
61 if (InputFilename.getValue() == "-") {
62 OutputFilename.setValue("-");
65 string IFN = InputFilename.getValue();
66 int Len = IFN.length();
67 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
69 OutputFilename.setValue(string(IFN.begin(), IFN.end()-3));
71 OutputFilename.setValue(IFN); // Append a .ll to it
73 OutputFilename.setValue(OutputFilename.getValue() + ".ll");
74 Out = new ofstream(OutputFilename.getValue().c_str(),
75 (Force.getValue() ? 0 : ios::noreplace)|ios::out);
80 cerr << "Error opening " << OutputFilename.getValue()
81 << ": sending to stdout instead!\n";
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...
88 if (WriteMode.getValue() == Default) {
89 (*Out) << C; // Print out in list order
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.
95 for (Module::iterator I = C->begin(), End = C->end(); I != End; ++I) {
97 (*Out) << "-------------- Method: " << M->getName() << " -------------\n";
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"));
104 case rdfo: // Reverse Depth First ordering
105 copy(cfg::df_begin(M, true), cfg::df_end(M),
106 ostream_iterator<BasicBlock*>(*Out, "\n"));
108 case po: // Post Order
109 copy(cfg::po_begin(M), cfg::po_end(M),
110 ostream_iterator<BasicBlock*>(*Out, "\n"));
112 case rpo: { // Reverse Post Order
113 cfg::ReversePostOrderTraversal RPOT(M);
114 copy(RPOT.begin(), RPOT.end(),
115 ostream_iterator<BasicBlock*>(*Out, "\n"));
126 if (Out != &cout) delete Out;