CodeGen support for aggregate-value function arguments.
[oota-llvm.git] / lib / ExecutionEngine / ExecutionEngine.cpp
index d1ad03f1006accf2b27a827392b4e6f16c8c25c6..8b94ad02840dc8e6fbe386c22601ab7e685f01c2 100644 (file)
@@ -26,7 +26,8 @@
 #include "llvm/System/DynamicLibrary.h"
 #include "llvm/System/Host.h"
 #include "llvm/Target/TargetData.h"
-#include <math.h>
+#include <cmath>
+#include <cstring>
 using namespace llvm;
 
 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
@@ -58,6 +59,7 @@ Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P,
     ModuleProvider *MP = *I;
     if (MP == P) {
       Modules.erase(I);
+      clearGlobalMappingsFromModule(MP->getModule());
       return MP->releaseModule(ErrInfo);
     }
   }
@@ -105,21 +107,49 @@ void ExecutionEngine::clearAllGlobalMappings() {
   state.getGlobalAddressReverseMap(locked).clear();
 }
 
+/// clearGlobalMappingsFromModule - Clear all global mappings that came from a
+/// particular module, because it has been removed from the JIT.
+void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
+  MutexGuard locked(lock);
+  
+  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
+    state.getGlobalAddressMap(locked).erase(FI);
+    state.getGlobalAddressReverseMap(locked).erase(FI);
+  }
+  for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 
+       GI != GE; ++GI) {
+    state.getGlobalAddressMap(locked).erase(GI);
+    state.getGlobalAddressReverseMap(locked).erase(GI);
+  }
+}
+
 /// updateGlobalMapping - Replace an existing mapping for GV with a new
 /// address.  This updates both maps as required.  If "Addr" is null, the
 /// entry for the global is removed from the mappings.
-void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
+void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
   MutexGuard locked(lock);
-  
+
+  std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked);
+
   // Deleting from the mapping?
   if (Addr == 0) {
-    state.getGlobalAddressMap(locked).erase(GV);
+    std::map<const GlobalValue*, void *>::iterator I = Map.find(GV);
+    void *OldVal;
+    if (I == Map.end())
+      OldVal = 0;
+    else {
+      OldVal = I->second;
+      Map.erase(I); 
+    }
+    
     if (!state.getGlobalAddressReverseMap(locked).empty())
       state.getGlobalAddressReverseMap(locked).erase(Addr);
-    return;
+    return OldVal;
   }
   
-  void *&CurVal = state.getGlobalAddressMap(locked)[GV];
+  void *&CurVal = Map[GV];
+  void *OldVal = CurVal;
+
   if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
     state.getGlobalAddressReverseMap(locked).erase(CurVal);
   CurVal = Addr;
@@ -130,6 +160,7 @@ void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
     assert((V == 0 || GV == 0) && "GlobalMapping already established!");
     V = GV;
   }
+  return OldVal;
 }
 
 /// getPointerToGlobalIfAvailable - This returns the address of the specified
@@ -316,6 +347,11 @@ ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
                                          std::string *ErrorStr) {
   ExecutionEngine *EE = 0;
 
+  // Make sure we can resolve symbols in the program as well. The zero arg
+  // to the function tells DynamicLibrary to load the program, not a library.
+  if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
+    return 0;
+
   // Unless the interpreter was explicitly selected, try making a JIT.
   if (!ForceInterpreter && JITCtor)
     EE = JITCtor(MP, ErrorStr);
@@ -324,15 +360,6 @@ ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
   if (EE == 0 && InterpCtor)
     EE = InterpCtor(MP, ErrorStr);
 
-  if (EE) {
-    // Make sure we can resolve symbols in the program as well. The zero arg
-    // to the function tells DynamicLibrary to load the program, not a library.
-    if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) {
-      delete EE;
-      return 0;
-    }
-  }
-
   return EE;
 }
 
@@ -423,9 +450,9 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
       else if (CE->getType() == Type::X86_FP80Ty) {
         const uint64_t zero[] = {0, 0};
         APFloat apf = APFloat(APInt(80, 2, zero));
-        (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData()
-                               GV.IntVal.getBitWidth(), false,
-                               APFloat::rmNearestTiesToEven);
+        (void)apf.convertFromAPInt(GV.IntVal
+                                   false,
+                                   APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
@@ -439,9 +466,9 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
       else if (CE->getType() == Type::X86_FP80Ty) {
         const uint64_t zero[] = { 0, 0};
         APFloat apf = APFloat(APInt(80, 2, zero));
-        (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData()
-                               GV.IntVal.getBitWidth(), true,
-                               APFloat::rmNearestTiesToEven);
+        (void)apf.convertFromAPInt(GV.IntVal
+                                   true,
+                                   APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
@@ -737,8 +764,8 @@ static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
 /// FIXME: document
 ///
 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
-                                                  GenericValue *Ptr,
-                                                  const Type *Ty) {
+                                          GenericValue *Ptr,
+                                          const Type *Ty) {
   const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
 
   if (sys::littleEndianHost() != getTargetData()->isLittleEndian()) {
@@ -799,38 +826,29 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
     return;
-  } else if (Init->getType()->isFirstClassType()) {
-    GenericValue Val = getConstantValue(Init);
-    StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
-    return;
   } else if (isa<ConstantAggregateZero>(Init)) {
     memset(Addr, 0, (size_t)getTargetData()->getABITypeSize(Init->getType()));
     return;
-  }
-
-  switch (Init->getType()->getTypeID()) {
-  case Type::ArrayTyID: {
-    const ConstantArray *CPA = cast<ConstantArray>(Init);
+  } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
     unsigned ElementSize =
       getTargetData()->getABITypeSize(CPA->getType()->getElementType());
     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
     return;
-  }
-
-  case Type::StructTyID: {
-    const ConstantStruct *CPS = cast<ConstantStruct>(Init);
+  } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
     const StructLayout *SL =
       getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
     return;
+  } else if (Init->getType()->isFirstClassType()) {
+    GenericValue Val = getConstantValue(Init);
+    StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
+    return;
   }
 
-  default:
-    cerr << "Bad Type: " << *Init->getType() << "\n";
-    assert(0 && "Unknown constant type to initialize memory with!");
-  }
+  cerr << "Bad Type: " << *Init->getType() << "\n";
+  assert(0 && "Unknown constant type to initialize memory with!");
 }
 
 /// EmitGlobals - Emit all of the global variables to memory, storing their
@@ -874,7 +892,7 @@ void ExecutionEngine::emitGlobals() {
           continue;
         
         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
-        // symbol.
+        // symbol.  FIXME is this right for common?
         if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
           GVEntry = GV;
       }