//===----------------------------------------------------------------------===//
#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...
c, // Generate C code
};
-static cl::opt<string>
+static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
-static cl::opt<string>
+static cl::opt<std::string>
OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
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());
}
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());
}
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;
}