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/Bytecode/Reader.h"
21 #include "llvm/Support/CFG.h"
22 #include "Support/DepthFirstIterator.h"
23 #include "Support/PostOrderIterator.h"
24 #include "Support/CommandLine.h"
25 #include "Support/Signals.h"
30 // OutputMode - The different orderings to print basic blocks in...
32 Default = 0, // Function Order (list order)
33 dfo, // Depth First ordering
34 rdfo, // Reverse Depth First ordering
36 rpo, // Reverse Post Order
39 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
40 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
41 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
42 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
43 clEnumVal(Default, "Write basic blocks in bytecode order"),
44 clEnumVal(dfo , "Write basic blocks in depth first order"),
45 clEnumVal(rdfo , "Write basic blocks in reverse DFO"),
46 clEnumVal(po , "Write basic blocks in postorder"),
47 clEnumVal(rpo , "Write basic blocks in reverse postorder"),
50 int main(int argc, char **argv) {
51 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
52 std::ostream *Out = &std::cout; // Default to printing to stdout...
54 Module *M = ParseBytecodeFile(InputFilename);
56 cerr << "bytecode didn't read correctly.\n";
60 if (OutputFilename != "") { // Specified an output filename?
61 if (!Force && std::ifstream(OutputFilename.c_str())) {
62 // If force is not specified, make sure not to overwrite a file!
63 cerr << "Error opening '" << OutputFilename
64 << "': File exists! Sending to standard output.\n";
66 Out = new std::ofstream(OutputFilename.c_str());
69 if (InputFilename == "-") {
72 std::string IFN = InputFilename;
73 int Len = IFN.length();
74 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
76 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
78 OutputFilename = IFN; // Append a .ll to it
80 OutputFilename += ".ll";
82 if (!Force && std::ifstream(OutputFilename.c_str())) {
83 // If force is not specified, make sure not to overwrite a file!
84 cerr << "Error opening '" << OutputFilename
85 << "': File exists! Sending to standard output.\n";
87 Out = new std::ofstream(OutputFilename.c_str());
89 // Make sure that the Out file gets unlink'd from the disk if we get a
91 RemoveFileOnSignal(OutputFilename);
97 cerr << "Error opening " << OutputFilename
98 << ": sending to stdout instead!\n";
102 // All that dis does is write the assembly out to a file... which is exactly
103 // what the writer library is supposed to do...
105 if (WriteMode == Default) {
106 (*Out) << M; // Print out in list order
108 // TODO: This does not print anything other than the basic blocks in the
109 // functions... more should definately be printed. It should be valid
110 // output consumable by the assembler.
112 for (Module::iterator I = M->begin(), End = M->end(); I != End; ++I) {
114 (*Out) << "-------------- Method: " << F->getName() << " -------------\n";
117 case dfo: // Depth First ordering
118 copy(df_begin(F), df_end(F),
119 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
121 case rdfo: // Reverse Depth First ordering
122 copy(df_begin(F, true), df_end(F),
123 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
125 case po: // Post Order
126 copy(po_begin(F), po_end(F),
127 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
129 case rpo: { // Reverse Post Order
130 #if 0 // FIXME, GCC 3.0.4 bug
131 ReversePostOrderTraversal<Function*> RPOT(F);
132 copy(RPOT.begin(), RPOT.end(),
133 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
145 if (Out != &std::cout) delete Out;