More templatization.
[oota-llvm.git] / lib / ExecutionEngine / ExecutionEngine.cpp
index c72663e0d4d436414bfbc9aff8dd12ed22a5865f..72db4e436034860539adc58a39a33aa3dcc34441 100644 (file)
@@ -33,13 +33,13 @@ STATISTIC(NumGlobals  , "Number of global vars initialized");
 ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
 ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
 
-ExecutionEngine::ExecutionEngine(ModuleProvider *P) {
+ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
   LazyCompilationDisabled = false;
   Modules.push_back(P);
   assert(P && "ModuleProvider is null?");
 }
 
-ExecutionEngine::ExecutionEngine(Module *M) {
+ExecutionEngine::ExecutionEngine(Module *M) : LazyFunctionCreator(0) {
   LazyCompilationDisabled = false;
   assert(M && "Module is null?");
   Modules.push_back(new ExistingModuleProvider(M));
@@ -51,6 +51,21 @@ ExecutionEngine::~ExecutionEngine() {
     delete Modules[i];
 }
 
+/// removeModuleProvider - Remove a ModuleProvider from the list of modules.
+/// Release module from ModuleProvider.
+Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P, 
+                                              std::string *ErrInfo) {
+  for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 
+        E = Modules.end(); I != E; ++I) {
+    ModuleProvider *MP = *I;
+    if (MP == P) {
+      Modules.erase(I);
+      return MP->releaseModule(ErrInfo);
+    }
+  }
+  return NULL;
+}
+
 /// FindFunctionNamed - Search all of the active modules to find the one that
 /// defines FnName.  This is very slow operation and shouldn't be used for
 /// general code.
@@ -304,15 +319,19 @@ ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
   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.
-    try {
-      sys::DynamicLibrary::LoadLibraryPermanently(0);
-    } catch (...) {
+    if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) {
+      delete EE;
+      return 0;
     }
   }
 
   return EE;
 }
 
+ExecutionEngine *ExecutionEngine::create(Module *M) {
+  return create(new ExistingModuleProvider(M));
+}
+
 /// getPointerToGlobal - This returns the address of the specified global
 /// value.  This may involve code generation if it's a function.
 ///
@@ -396,9 +415,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.convertFromInteger(GV.IntVal.getRawData(), 
+        (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData(), 
                                GV.IntVal.getBitWidth(), false,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
@@ -412,9 +431,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.convertFromInteger(GV.IntVal.getRawData(), 
+        (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData(), 
                                GV.IntVal.getBitWidth(), true,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
@@ -716,7 +735,7 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
     return;
   } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
     unsigned ElementSize =
-      getTargetData()->getTypeSize(CP->getType()->getElementType());
+      getTargetData()->getABITypeSize(CP->getType()->getElementType());
     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
     return;
@@ -725,7 +744,7 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
     return;
   } else if (isa<ConstantAggregateZero>(Init)) {
-    memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
+    memset(Addr, 0, (size_t)getTargetData()->getABITypeSize(Init->getType()));
     return;
   }
 
@@ -733,7 +752,7 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
   case Type::ArrayTyID: {
     const ConstantArray *CPA = cast<ConstantArray>(Init);
     unsigned ElementSize =
-      getTargetData()->getTypeSize(CPA->getType()->getElementType());
+      getTargetData()->getABITypeSize(CPA->getType()->getElementType());
     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
     return;
@@ -824,7 +843,7 @@ void ExecutionEngine::emitGlobals() {
         const Type *Ty = I->getType()->getElementType();
 
         // Allocate some memory for it!
-        unsigned Size = TD->getTypeSize(Ty);
+        unsigned Size = TD->getABITypeSize(Ty);
         addGlobalMapping(I, new char[Size]);
       } else {
         // External variable reference. Try to use the dynamic loader to
@@ -878,7 +897,7 @@ void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
   DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
 
   const Type *ElTy = GV->getType()->getElementType();
-  size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
+  size_t GVSize = (size_t)getTargetData()->getABITypeSize(ElTy);
   if (GA == 0) {
     // If it's not already specified, allocate memory for the global.
     GA = new char[GVSize];