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/Function.h"
23 #include "llvm/Support/CFG.h"
24 #include "Support/DepthFirstIterator.h"
25 #include "Support/PostOrderIterator.h"
26 #include "Support/CommandLine.h"
31 // OutputMode - The different orderings to print basic blocks in...
33 Default = 0, // Function Order (list order)
34 dfo, // Depth First ordering
35 rdfo, // Reverse Depth First ordering
37 rpo, // Reverse Post Order
40 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
41 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
42 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
43 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
44 clEnumVal(Default, "Write basic blocks in bytecode order"),
45 clEnumVal(dfo , "Write basic blocks in depth first order"),
46 clEnumVal(rdfo , "Write basic blocks in reverse DFO"),
47 clEnumVal(po , "Write basic blocks in postorder"),
48 clEnumVal(rpo , "Write basic blocks in reverse postorder"),
51 int main(int argc, char **argv) {
52 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
53 std::ostream *Out = &std::cout; // Default to printing to stdout...
55 Module *M = ParseBytecodeFile(InputFilename);
57 cerr << "bytecode didn't read correctly.\n";
61 if (OutputFilename != "") { // Specified an output filename?
62 if (!Force && std::ifstream(OutputFilename.c_str())) {
63 // If force is not specified, make sure not to overwrite a file!
64 cerr << "Error opening '" << OutputFilename
65 << "': File exists! Sending to standard output.\n";
67 Out = new std::ofstream(OutputFilename.c_str());
70 if (InputFilename == "-") {
73 std::string IFN = InputFilename;
74 int Len = IFN.length();
75 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
77 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
79 OutputFilename = IFN; // Append a .ll to it
81 OutputFilename += ".ll";
83 if (!Force && std::ifstream(OutputFilename.c_str())) {
84 // If force is not specified, make sure not to overwrite a file!
85 cerr << "Error opening '" << OutputFilename
86 << "': File exists! Sending to standard output.\n";
88 Out = new std::ofstream(OutputFilename.c_str());
94 cerr << "Error opening " << OutputFilename
95 << ": sending to stdout instead!\n";
99 // All that dis does is write the assembly out to a file... which is exactly
100 // what the writer library is supposed to do...
102 if (WriteMode == Default) {
103 (*Out) << M; // Print out in list order
105 // TODO: This does not print anything other than the basic blocks in the
106 // functions... more should definately be printed. It should be valid
107 // output consumable by the assembler.
109 for (Module::iterator I = M->begin(), End = M->end(); I != End; ++I) {
111 (*Out) << "-------------- Method: " << F->getName() << " -------------\n";
114 case dfo: // Depth First ordering
115 copy(df_begin(F), df_end(F),
116 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
118 case rdfo: // Reverse Depth First ordering
119 copy(df_begin(F, true), df_end(F),
120 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
122 case po: // Post Order
123 copy(po_begin(F), po_end(F),
124 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
126 case rpo: { // Reverse Post Order
127 #if 0 // FIXME, GCC 3.0.4 bug
128 ReversePostOrderTraversal<Function*> RPOT(F);
129 copy(RPOT.begin(), RPOT.end(),
130 std::ostream_iterator<BasicBlock*>(*Out, "\n"));
142 if (Out != &std::cout) delete Out;