2009-12-04 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-llvm-cpp.cpp
index 56d44a9ecc6d277d1936e2bcd4c09d5b1488eb6f..4564a0540753c6afe9c0f7c9320d11ceb4dca3a7 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
@@ -63,13 +67,18 @@ public:
     unsigned char *getGOTBase() const {
                return mm->getGOTBase ();
     }
-    
+
+#if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7
     void *getDlsymTable() const {
                return mm->getDlsymTable ();
     }
-      
+
        void SetDlsymTable(void *ptr);
-  
+#endif
+
+       void setPoisonMemory(bool) {
+       }
+
        unsigned char *startFunctionBody(const Function *F, 
                                                                         uintptr_t &ActualSize);
   
@@ -80,6 +89,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);
   
@@ -89,6 +100,14 @@ public:
        void endExceptionTable(const Function *F, unsigned char *TableStart,
                                                   unsigned char *TableEnd, 
                                                   unsigned char* FrameRegister);
+
+#if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION >= 7
+       virtual void deallocateFunctionBody(void*) {
+       }
+
+       virtual void deallocateExceptionTable(void*) {
+       }
+#endif
 };
 
 MonoJITMemoryManager::MonoJITMemoryManager ()
@@ -116,13 +135,15 @@ MonoJITMemoryManager::AllocateGOT()
 {
        mm->AllocateGOT ();
 }
-  
+
+#if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 7  
 void
 MonoJITMemoryManager::SetDlsymTable(void *ptr)
 {
        mm->SetDlsymTable (ptr);
 }
-  
+#endif
+
 unsigned char *
 MonoJITMemoryManager::startFunctionBody(const Function *F, 
                                        uintptr_t &ActualSize)
@@ -149,7 +170,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 +200,8 @@ static MonoJITMemoryManager *mono_mm;
 
 static FunctionPassManager *fpm;
 
+static LLVMContext ctx;
+
 void
 mono_llvm_optimize_method (LLVMValueRef method)
 {
@@ -184,7 +213,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 +222,7 @@ 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 
@@ -203,6 +232,13 @@ mono_llvm_build_volatile_load (LLVMBuilderRef builder, LLVMValueRef PointerVal,
        return wrap(unwrap(builder)->CreateLoad(unwrap(PointerVal), true, Name));
 }
 
+void
+mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v)
+{
+       Value *V = ConstantExpr::getTruncOrBitCast (unwrap<Constant> (v), unwrap (var)->getType ());
+       unwrap (var)->replaceAllUsesWith (V);
+}
+
 static cl::list<const PassInfo*, bool, PassNameParser>
 PassList(cl::desc("Optimizations available:"));
 
@@ -211,17 +247,22 @@ 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, CodeGenOpt::Default);
   if (!EE) {
-         cerr << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
+         errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n";
          g_assert_not_reached ();
   }
   EE->InstallExceptionTableRegister (exception_cb);
@@ -245,7 +286,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);
          }