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
17 // TODO: add -vcg which prints VCG compatible output.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Module.h"
22 #include "llvm/Bytecode/Reader.h"
23 #include "llvm/Support/CFG.h"
24 #include "Support/DepthFirstIterator.h"
25 #include "Support/PostOrderIterator.h"
26 #include "Support/CommandLine.h"
27 #include "Support/Signals.h"
28 #include "llvm/Assembly/CWriter.h"
33 // OutputMode - The different orderings to print basic blocks in...
35 Default = 0, // Function Order (list order)
36 dfo, // Depth First ordering
37 rdfo, // Reverse Depth First ordering
39 rpo, // Reverse Post Order
44 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
45 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
46 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
47 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
48 clEnumVal(Default, "Write basic blocks in bytecode order"),
49 clEnumVal(dfo , "Write basic blocks in depth first order"),
50 clEnumVal(rdfo , "Write basic blocks in reverse DFO"),
51 clEnumVal(po , "Write basic blocks in postorder"),
52 clEnumVal(rpo , "Write basic blocks in reverse postorder"),
54 clEnumVal(c , "Write corresponding C code"),
57 int main(int argc, char **argv) {
58 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
59 std::ostream *Out = &std::cout; // Default to printing to stdout...
61 Module *M = ParseBytecodeFile(InputFilename);
63 cerr << "bytecode didn't read correctly.\n";
67 if (OutputFilename != "") { // Specified an output filename?
68 if (!Force && std::ifstream(OutputFilename.c_str())) {
69 // If force is not specified, make sure not to overwrite a file!
70 cerr << "Error opening '" << OutputFilename
71 << "': File exists! Sending to standard output.\n";
73 Out = new std::ofstream(OutputFilename.c_str());
76 if (InputFilename == "-") {
79 std::string IFN = InputFilename;
80 int Len = IFN.length();
81 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
83 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
85 OutputFilename = IFN; // Append a .ll to it
88 OutputFilename += ".c";
90 OutputFilename += ".ll";
92 if (!Force && std::ifstream(OutputFilename.c_str())) {
93 // If force is not specified, make sure not to overwrite a file!
94 cerr << "Error opening '" << OutputFilename
95 << "': File exists! Sending to standard output.\n";
97 Out = new std::ofstream(OutputFilename.c_str());
99 // Make sure that the Out file gets unlink'd from the disk if we get a
101 RemoveFileOnSignal(OutputFilename);
107 cerr << "Error opening " << OutputFilename
108 << ": sending to stdout instead!\n";
112 // All that dis does is write the assembly or C out to a file... which is
113 // exactly what the writer or cwriter library is supposed to do...
114 if (WriteMode == Default) {
115 (*Out) << M; // Print out in list order
116 } else if (WriteMode == c) {
119 // TODO: This does not print anything other than the basic blocks in the
120 // functions... more should definately be printed. It should be valid
121 // output consumable by the assembler.
123 for (Module::iterator I = M->begin(), End = M->end(); I != End; ++I) {
125 (*Out) << "-------------- Method: " << F->getName() << " -------------\n";
128 case dfo: // Depth First ordering
129 copy(df_begin(F), df_end(F),
130 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
132 case rdfo: // Reverse Depth First ordering
133 copy(df_begin(F, true), df_end(F),
134 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
136 case po: // Post Order
137 copy(po_begin(F), po_end(F),
138 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
140 case rpo: { // Reverse Post Order
141 #if 0 // FIXME, GCC 3.0.4 bug
142 ReversePostOrderTraversal<Function*> RPOT(F);
143 copy(RPOT.begin(), RPOT.end(),
144 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
156 if (Out != &std::cout) delete Out;