llvm-ar: Clean up memory management with OwningPtr.
[oota-llvm.git] / examples / BrainF / BrainFDriver.cpp
index f4f1e79b822398959e522df1b884b28c64da9724..cd6eabfdffaa5e07135301ee6e6d8cda48cb195c 100644 (file)
 // ./BrainF           prog.bf          #Write as BitCode
 //
 // lli prog.bf.bc                      #Run generated BitCode
-// llvm-ld -native -o=prog prog.bf.bc  #Compile BitCode into native executable
 //
 //===--------------------------------------------------------------------===//
 
 #include "BrainF.h"
-#include "llvm/Constants.h"
-#include "llvm/ModuleProvider.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/ExecutionEngine/GenericValue.h"
 #include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h"
-#include "llvm/Target/TargetSelect.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/raw_ostream.h"
 #include <fstream>
 #include <iostream>
 using namespace llvm;
@@ -91,50 +90,48 @@ int main(int argc, char **argv) {
   LLVMContext &Context = getGlobalContext();
 
   if (InputFilename == "") {
-    std::cerr<<"Error: You must specify the filename of the program to "
+    errs() << "Error: You must specify the filename of the program to "
     "be compiled.  Use --help to see the options.\n";
     abort();
   }
 
   //Get the output stream
-  std::ostream *out = &std::cout;
+  raw_ostream *out = &outs();
   if (!JIT) {
     if (OutputFilename == "") {
       std::string base = InputFilename;
-      if (InputFilename == "-") {base = "a";}
+      if (InputFilename == "-") { base = "a"; }
 
-      //Use default filename
-      const char *suffix = ".bc";
-      OutputFilename = base+suffix;
+      // Use default filename.
+      OutputFilename = base+".bc";
     }
     if (OutputFilename != "-") {
-      out = new std::
-        ofstream(OutputFilename.c_str(),
-                 std::ios::out | std::ios::trunc | std::ios::binary);
+      std::string ErrInfo;
+      out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
+                               raw_fd_ostream::F_Binary);
     }
   }
 
   //Get the input stream
   std::istream *in = &std::cin;
-  if (InputFilename != "-") {
+  if (InputFilename != "-")
     in = new std::ifstream(InputFilename.c_str());
-  }
 
   //Gather the compile flags
   BrainF::CompileFlags cf = BrainF::flag_off;
-  if (ArrayBoundsChecking) {
+  if (ArrayBoundsChecking)
     cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
-  }
 
   //Read the BrainF program
   BrainF bf;
   Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
-  if (in != &std::cin) {delete in;}
+  if (in != &std::cin)
+    delete in;
   addMainFunction(mod);
 
   //Verify generated code
   if (verifyModule(*mod)) {
-    std::cerr<<"Error: module failed verification.  This shouldn't happen.\n";
+    errs() << "Error: module failed verification.  This shouldn't happen.\n";
     abort();
   }
 
@@ -142,7 +139,7 @@ int main(int argc, char **argv) {
   if (JIT) {
     InitializeNativeTarget();
 
-    std::cout << "------- Running JIT -------\n";
+    outs() << "------- Running JIT -------\n";
     ExecutionEngine *ee = EngineBuilder(mod).create();
     std::vector<GenericValue> args;
     Function *brainf_func = mod->getFunction("brainf");
@@ -152,7 +149,8 @@ int main(int argc, char **argv) {
   }
 
   //Clean up
-  if (out != &std::cout) {delete out;}
+  if (out != &outs())
+    delete out;
   delete mod;
 
   llvm_shutdown();