Genericize the ReversePostOrderIterator.
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
index 59594763eb1f9c0df3d7294866a874b5b144ec64..04fc60b2f1fef3c89b9414af1fa7b3f4af609a47 100644 (file)
@@ -1,4 +1,4 @@
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
 // LLVM 'DIS' UTILITY 
 //
 // This utility may be invoked in the following manner:
 //
 // TODO: add -vcg which prints VCG compatible output.
 //
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
 
-#include <iostream.h>
-#include <fstream.h>
 #include "llvm/Module.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/Bytecode/Reader.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Method.h"
-#include "llvm/CFG.h"
+#include "llvm/Support/CFG.h"
+#include "Support/DepthFirstIterator.h"
+#include "Support/PostOrderIterator.h"
+#include "Support/CommandLine.h"
+#include <fstream>
+#include <iostream>
+using std::cerr;
 
 // OutputMode - The different orderings to print basic blocks in...
 enum OutputMode {
@@ -35,8 +38,8 @@ enum OutputMode {
 };
 
 cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
-cl::String OutputFilename("o", "Override output filename", 0, "");
-cl::Flag   Force         ("f", "Overwrite output files", 0, false);
+cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
+cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
 cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
   clEnumVal(Default, "Write basic blocks in bytecode order"),
   clEnumVal(dfo    , "Write basic blocks in depth first order"),
@@ -47,46 +50,56 @@ cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
-  ostream *Out = &cout;  // Default to printing to stdout...
+  std::ostream *Out = &std::cout;  // Default to printing to stdout...
 
-  Module *C = ParseBytecodeFile(InputFilename.getValue());
+  Module *C = ParseBytecodeFile(InputFilename);
   if (C == 0) {
     cerr << "bytecode didn't read correctly.\n";
     return 1;
   }
   
-  if (OutputFilename.getValue() != "") {   // Specified an output filename?
-    Out = new ofstream(OutputFilename.getValue().c_str(), 
-                      (Force.getValue() ? 0 : ios::noreplace)|ios::out);
+  if (OutputFilename != "") {   // Specified an output filename?
+    if (!Force && std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename
+           << "': File exists! Sending to standard output.\n";
+    } else {
+      Out = new std::ofstream(OutputFilename.c_str());
+    }
   } else {
-    if (InputFilename.getValue() == "-") {
-      OutputFilename.setValue("-");
-      Out = &cout;
+    if (InputFilename == "-") {
+      OutputFilename = "-";
     } else {
-      string IFN = InputFilename.getValue();
+      std::string IFN = InputFilename;
       int Len = IFN.length();
       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
        // Source ends in .bc
-       OutputFilename.setValue(string(IFN.begin(), IFN.end()-3));
+       OutputFilename = std::string(IFN.begin(), IFN.end()-3);
+      } else {
+       OutputFilename = IFN;   // Append a .ll to it
+      }
+      OutputFilename += ".ll";
+
+      if (!Force && std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename
+             << "': File exists! Sending to standard output.\n";
       } else {
-       OutputFilename.setValue(IFN);   // Append a .ll to it
+        Out = new std::ofstream(OutputFilename.c_str());
       }
-      OutputFilename.setValue(OutputFilename.getValue() + ".ll");
-      Out = new ofstream(OutputFilename.getValue().c_str(), 
-                        (Force.getValue() ? 0 : ios::noreplace)|ios::out);
     }
   }
 
   if (!Out->good()) {
-    cerr << "Error opening " << OutputFilename.getValue() 
+    cerr << "Error opening " << OutputFilename
         << ": sending to stdout instead!\n";
-    Out = &cout;
+    Out = &std::cout;
   }
 
   // All that dis does is write the assembly out to a file... which is exactly
   // what the writer library is supposed to do...
   //
-  if (WriteMode.getValue() == Default) {
+  if (WriteMode == Default) {
     (*Out) << C;           // Print out in list order
   } else {
     // TODO: This does not print anything other than the basic blocks in the
@@ -97,23 +110,23 @@ int main(int argc, char **argv) {
       Method *M = *I;
       (*Out) << "-------------- Method: " << M->getName() << " -------------\n";
 
-      switch (WriteMode.getValue()) {
+      switch (WriteMode) {
       case dfo:                   // Depth First ordering
-       copy(cfg::df_begin(M), cfg::df_end(M),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+       copy(df_begin(M), df_end(M),
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       case rdfo:            // Reverse Depth First ordering
-       copy(cfg::df_begin(M, true), cfg::df_end(M),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+       copy(df_begin(M, true), df_end(M),
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       case po:                    // Post Order
-       copy(cfg::po_begin(M), cfg::po_end(M),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+       copy(po_begin(M), po_end(M),
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       case rpo: {           // Reverse Post Order
-       cfg::ReversePostOrderTraversal RPOT(M);
+       ReversePostOrderTraversal<Method*> RPOT(M);
        copy(RPOT.begin(), RPOT.end(),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       }
       default:
@@ -124,6 +137,6 @@ int main(int argc, char **argv) {
   }
   delete C;
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }