Change to use GetAddressOfSymbol instead of dlsym.
[oota-llvm.git] / lib / ExecutionEngine / ExecutionEngine.cpp
index 642b178455bcbd98fba514428cf7c2966f7df3d3..fb70ff1e725984f7edc98e34446d6d4f645e594d 100644 (file)
@@ -5,30 +5,51 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "ExecutionEngine.h"
-#include "GenericValue.h"
+#define DEBUG_TYPE "jit"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "JIT/VM.h"
+#include "Interpreter/Interpreter.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Constants.h"
 #include "llvm/Module.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
 #include "llvm/Target/TargetData.h"
 #include "Support/Debug.h"
 #include "Support/Statistic.h"
+#include "Support/DynamicLinker.h"
 #include "Config/dlfcn.h"
 
 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
 
+ExecutionEngine::~ExecutionEngine() {
+  delete &CurMod;
+}
+
+ExecutionEngine *ExecutionEngine::create (Module *M, bool ForceInterpreter,
+                                         bool TraceMode) {
+  ExecutionEngine *EE = 0;
+
+  // If there is nothing that is forcing us to use the interpreter, make a JIT.
+  if (!ForceInterpreter && !TraceMode)
+    EE = VM::create(M);
+
+  // If we can't make a JIT, make an interpreter instead.
+  if (EE == 0)
+    EE = Interpreter::create(M, TraceMode);
+  return EE;
+}
+
 // getPointerToGlobal - This returns the address of the specified global
 // value.  This may involve code generation if it's a function.
 //
 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
-  if (const Function *F = dyn_cast<Function>(GV))
+  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
     return getPointerToFunction(F);
 
   assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
   return GlobalAddress[GV];
 }
 
-
 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
   GenericValue Result;
 
@@ -53,13 +74,14 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
       if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
         return getConstantValue(Op);
 
-      // Handle cast of long to pointer or pointer to long...
-      if ((isa<PointerType>(Op->getType()) && (C->getType() == Type::LongTy ||
-                                               C->getType() == Type::ULongTy))||
-          (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
-                                              Op->getType() == Type::ULongTy))){
+      // Handle a cast of pointer to any integral type...
+      if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
+        return getConstantValue(Op);
+        
+      // Handle cast of long to pointer...
+      if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
+                                             Op->getType() == Type::ULongTy))
         return getConstantValue(Op);
-      }
       break;
     }
 
@@ -130,7 +152,7 @@ void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
                             Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
                             Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
                             break;
-    case Type::PointerTyID: if (CurMod.has32BitPointers())
+    case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
                               goto Store4BytesLittleEndian;
     case Type::DoubleTyID:
     case Type::ULongTyID:
@@ -163,7 +185,7 @@ void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
                             Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
                             Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
                             break;
-    case Type::PointerTyID: if (CurMod.has32BitPointers())
+    case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
                               goto Store4BytesBigEndian;
     case Type::DoubleTyID:
     case Type::ULongTyID:
@@ -202,7 +224,7 @@ GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
                                             ((unsigned)Ptr->Untyped[2] << 16) |
                                             ((unsigned)Ptr->Untyped[3] << 24);
                             break;
-    case Type::PointerTyID: if (getModule().has32BitPointers())
+    case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
                               goto Load4BytesLittleEndian;
     case Type::DoubleTyID:
     case Type::ULongTyID:
@@ -236,7 +258,7 @@ GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
                                             ((unsigned)Ptr->Untyped[1] << 16) |
                                             ((unsigned)Ptr->Untyped[0] << 24);
                             break;
-    case Type::PointerTyID: if (getModule().has32BitPointers())
+    case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
                               goto Load4BytesBigEndian;
     case Type::DoubleTyID:
     case Type::ULongTyID:
@@ -257,7 +279,6 @@ GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
   return Result;
 }
 
-
 // InitializeMemory - Recursive function to apply a Constant value into the
 // specified memory location...
 //
@@ -296,45 +317,6 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
   }
 }
 
-void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) {
-  if (getTargetData().getPointerSize() == 8) {   // 64 bit target?
-    PointerTy *Result = new PointerTy[InputArgv.size()+1];
-    DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
-
-    for (unsigned i = 0; i < InputArgv.size(); ++i) {
-      unsigned Size = InputArgv[i].size()+1;
-      char *Dest = new char[Size];
-      DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
-      
-      copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
-      Dest[Size-1] = 0;
-      
-      // Endian safe: Result[i] = (PointerTy)Dest;
-      StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::LongTy);
-    }
-    Result[InputArgv.size()] = 0;
-    return Result;
-
-  } else {                                      // 32 bit target?
-    int *Result = new int[InputArgv.size()+1];
-    DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
-
-    for (unsigned i = 0; i < InputArgv.size(); ++i) {
-      unsigned Size = InputArgv[i].size()+1;
-      char *Dest = new char[Size];
-      DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
-      
-      copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
-      Dest[Size-1] = 0;
-      
-      // Endian safe: Result[i] = (PointerTy)Dest;
-      StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::IntTy);
-    }
-    Result[InputArgv.size()] = 0;  // null terminate it
-    return Result;
-  }
-}
-
 /// EmitGlobals - Emit all of the global variables to memory, storing their
 /// addresses into GlobalAddress.  This must make sure to copy the contents of
 /// their initializers into the memory.
@@ -358,14 +340,9 @@ void ExecutionEngine::emitGlobals() {
       DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
                      << (void*)GlobalAddress[I] << "\n");
     } else {
-      // On Sparc, RTLD_SELF is already defined and it's not zero
-      // Linux/x86 wants to use a 0, other systems may differ
-#ifndef RTLD_SELF
-#define RTLD_SELF 0
-#endif
-      // External variable reference, try to use dlsym to get a pointer to it in
-      // the LLI image.
-      if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str()))
+      // External variable reference. Try to use the dynamic loader to
+      // get a pointer to it.
+      if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
         GlobalAddress[I] = SymAddr;
       else {
         std::cerr << "Could not resolve external global address: "