2009-07-01 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-llvm-cpp.cpp
index 04ee65d6d0a581d278f6c6aa3f44b86f429b3ac5..f379b20c666ebc941d17288a7778be674561db2d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <stdint.h>
 
+#include <llvm/Support/raw_ostream.h>
 #include <llvm/PassManager.h>
 #include <llvm/ExecutionEngine/ExecutionEngine.h>
 #include <llvm/ExecutionEngine/JITMemoryManager.h>
@@ -30,6 +31,7 @@
 #include <llvm/Transforms/Scalar.h>
 #include <llvm/Support/CommandLine.h>
 #include "llvm/Support/PassNameParser.h"
+#include "llvm/Support/PrettyStackTrace.h"
 #include <llvm/CodeGen/Passes.h>
 #include <llvm/CodeGen/MachineFunctionPass.h>
 //#include <llvm/LinkAllPasses.h>
@@ -39,6 +41,8 @@
 
 #include "mini-llvm-cpp.h"
 
+extern "C" void LLVMInitializeX86TargetInfo();
+
 using namespace llvm;
 
 class MonoJITMemoryManager : public JITMemoryManager
@@ -67,6 +71,9 @@ public:
     void *getDlsymTable() const {
                return mm->getDlsymTable ();
     }
+
+       void setPoisonMemory(bool) {
+       }
       
        void SetDlsymTable(void *ptr);
   
@@ -80,6 +87,8 @@ public:
                                                 unsigned char *FunctionEnd);
 
        unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
+
+       uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
   
        void deallocateMemForFunction(const Function *F);
   
@@ -149,7 +158,13 @@ MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
 {
        return new unsigned char [Size];
 }
-  
+
+uint8_t *
+MonoJITMemoryManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
+{
+       return new unsigned char [Size];
+}
+
 void
 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
 {
@@ -173,6 +188,8 @@ static MonoJITMemoryManager *mono_mm;
 
 static FunctionPassManager *fpm;
 
+static LLVMContext ctx;
+
 void
 mono_llvm_optimize_method (LLVMValueRef method)
 {
@@ -184,7 +201,7 @@ void
 mono_llvm_dump_value (LLVMValueRef value)
 {
        /* Same as LLVMDumpValue (), but print to stdout */
-       cout << (*unwrap<Value> (value));
+       outs () << (*unwrap<Value> (value));
 }
 
 /* Missing overload for building an alloca with an alignment */
@@ -193,7 +210,14 @@ mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty,
                                                LLVMValueRef ArraySize,
                                                int alignment, const char *Name)
 {
-       return wrap (unwrap (builder)->Insert (new AllocaInst(unwrap (Ty), unwrap (ArraySize), alignment), Name));
+       return wrap (unwrap (builder)->Insert (new AllocaInst (unwrap (Ty), unwrap (ArraySize), alignment), Name));
+}
+
+LLVMValueRef 
+mono_llvm_build_volatile_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
+                                                          const char *Name)
+{
+       return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), true, Name));
 }
 
 static cl::list<const PassInfo*, bool, PassNameParser>
@@ -204,15 +228,24 @@ mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, Func
 {
   std::string Error;
 
+  LLVMInitializeX86Target ();
+  LLVMInitializeX86TargetInfo ();
+
   llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false);
 
   mono_mm = new MonoJITMemoryManager ();
   mono_mm->alloc_cb = alloc_cb;
   mono_mm->emitted_cb = emitted_cb;
 
-  ExceptionHandling = true;
+  DwarfExceptionHandling = true;
+  // PrettyStackTrace installs signal handlers which trip up libgc
+  DisablePrettyStackTrace = true;
 
-  ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, false);
+  ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default);
+  if (!EE) {
+         errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
+         g_assert_not_reached ();
+  }
   EE->InstallExceptionTableRegister (exception_cb);
 
   fpm = new FunctionPassManager (unwrap (MP));
@@ -234,7 +267,7 @@ mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, Func
       if (PassInf->getNormalCtor())
                  P = PassInf->getNormalCtor()();
          if (dynamic_cast<MachineFunctionPass*>(P) != 0) {
-                 cerr << PassInf->getPassName () << " is a machine function pass.\n";
+                 errs () << PassInf->getPassName () << " is a machine function pass.\n";
          } else {
                  fpm->add (P);
          }