*** empty log message ***
[oota-llvm.git] / tools / llvm-dis / dis.cpp
index eb9a3a39ed067c898027762762a9ec84e910a414..166a178d5745e480ab08cf2ce59e3d8bb20cf4a7 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Module.h"
+#include "llvm/PassManager.h"
 #include "llvm/Bytecode/Reader.h"
-#include "llvm/Support/CFG.h"
+#include "llvm/Assembly/CWriter.h"
+#include "llvm/Assembly/PrintModulePass.h"
 #include "Support/CommandLine.h"
 #include "Support/Signals.h"
-#include "llvm/Assembly/CWriter.h"
 #include <fstream>
-#include <iostream>
+#include <memory>
 using std::cerr;
 
 // OutputMode - The different orderings to print basic blocks in...
@@ -27,29 +28,38 @@ enum OutputMode {
   c,                  // Generate C code
 };
 
-cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
-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(llvm, "Output LLVM assembly"),
-  clEnumVal(c   , "Output C code for program"),
- 0);
+static cl::opt<std::string>
+InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
+
+static cl::opt<std::string>
+OutputFilename("o", cl::desc("Override output filename"),
+               cl::value_desc("filename"));
+
+static cl::opt<bool>
+Force("f", cl::desc("Overwrite output files"));
+
+static cl::opt<enum OutputMode>
+WriteMode(cl::desc("Specify the output format:"),
+          cl::values(
+                     clEnumVal(llvm, "Output LLVM assembly"),
+                     clEnumVal(c   , "Output C code for program"),
+                    0));
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
   std::ostream *Out = &std::cout;  // Default to printing to stdout...
 
-  Module *M = ParseBytecodeFile(InputFilename);
-  if (M == 0) {
-    cerr << "bytecode didn't read correctly.\n";
+  std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
+  if (M.get() == 0) {
+    cerr << argv[0] << ": bytecode didn't read correctly.\n";
     return 1;
   }
   
   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";
+      cerr << argv[0] << ": error opening '" << OutputFilename
+           << "': file exists! Sending to standard output.\n";
     } else {
       Out = new std::ofstream(OutputFilename.c_str());
     }
@@ -72,8 +82,8 @@ int main(int argc, char **argv) {
 
       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";
+        cerr << argv[0] << ": error opening '" << OutputFilename
+             << "': file exists! Sending to standard output.\n";
       } else {
         Out = new std::ofstream(OutputFilename.c_str());
 
@@ -85,24 +95,30 @@ int main(int argc, char **argv) {
   }
 
   if (!Out->good()) {
-    cerr << "Error opening " << OutputFilename
+    cerr << argv[0] << ": error opening " << OutputFilename
         << ": sending to stdout instead!\n";
     Out = &std::cout;
   }
 
   // All that dis does is write the assembly or C out to a file...
   //
+  PassManager Passes;
+
   switch (WriteMode) {
-  case llvm:
-    (*Out) << M;           // Output LLVM assembly
+  case llvm:           // Output LLVM assembly
+    Passes.add(new PrintModulePass(Out));
     break;
-  case c:
-    WriteToC(M, *Out);     // Convert LLVM to C
+  case c:              // Convert LLVM to C
+    Passes.add(createWriteToCPass(*Out));
     break;
   }
-  delete M;
 
-  if (Out != &std::cout) delete Out;
+  Passes.run(*M.get());
+
+  if (Out != &std::cout) {
+    ((std::ofstream*)Out)->close();
+    delete Out;
+  }
   return 0;
 }