Remove attribution from file headers, per discussion on llvmdev.
[oota-llvm.git] / lib / ExecutionEngine / ExecutionEngine.cpp
index d89a9bb4ac7e15122bc29ceeb66d6032e1cf4f6a..fe6b811ea65b7faed883744eba904ac8559e998e 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include "llvm/Module.h"
 #include "llvm/ModuleProvider.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Config/alloca.h"
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
 #include "llvm/ExecutionEngine/GenericValue.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MutexGuard.h"
 #include "llvm/System/DynamicLibrary.h"
+#include "llvm/System/Host.h"
 #include "llvm/Target/TargetData.h"
 #include <math.h>
 using namespace llvm;
@@ -39,12 +41,6 @@ ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
   assert(P && "ModuleProvider is null?");
 }
 
-ExecutionEngine::ExecutionEngine(Module *M) : LazyFunctionCreator(0) {
-  LazyCompilationDisabled = false;
-  assert(M && "Module is null?");
-  Modules.push_back(new ExistingModuleProvider(M));
-}
-
 ExecutionEngine::~ExecutionEngine() {
   clearAllGlobalMappings();
   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
@@ -174,7 +170,7 @@ static void *CreateArgv(ExecutionEngine *EE,
   char *Result = new char[(InputArgv.size()+1)*PtrSize];
 
   DOUT << "ARGV = " << (void*)Result << "\n";
-  const Type *SBytePtr = PointerType::get(Type::Int8Ty);
+  const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty);
 
   for (unsigned i = 0; i != InputArgv.size(); ++i) {
     unsigned Size = InputArgv[i].size()+1;
@@ -237,6 +233,15 @@ void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
   }
 }
 
+/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
+static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
+  unsigned PtrSize = EE->getTargetData()->getPointerSize();
+  for (unsigned i = 0; i < PtrSize; ++i)
+    if (*(i + (uint8_t*)Loc))
+      return false;
+  return true;
+}
+
 /// runFunctionAsMain - This is a helper function which wraps runFunction to
 /// handle the common task of starting up main with the specified argc, argv,
 /// and envp parameters.
@@ -250,7 +255,8 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn,
   // Check main() type
   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
   const FunctionType *FTy = Fn->getFunctionType();
-  const Type* PPInt8Ty = PointerType::get(PointerType::get(Type::Int8Ty));
+  const Type* PPInt8Ty = 
+    PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
   switch (NumArgs) {
   case 3:
    if (FTy->getParamType(2) != PPInt8Ty) {
@@ -286,7 +292,7 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn,
     GVArgs.push_back(GVArgc); // Arg #0 = argc.
     if (NumArgs > 1) {
       GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
-      assert(((char **)GVTOP(GVArgs[1]))[0] &&
+      assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
              "argv[0] was null after CreateArgv");
       if (NumArgs > 2) {
         std::vector<std::string> EnvVars;
@@ -629,33 +635,44 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
   return Result;
 }
 
+/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
+/// with the integer held in IntVal.
+static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
+                             unsigned StoreBytes) {
+  assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
+  uint8_t *Src = (uint8_t *)IntVal.getRawData();
+
+  if (sys::littleEndianHost())
+    // Little-endian host - the source is ordered from LSB to MSB.  Order the
+    // destination from LSB to MSB: Do a straight copy.
+    memcpy(Dst, Src, StoreBytes);
+  else {
+    // Big-endian host - the source is an array of 64 bit words ordered from
+    // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
+    // from MSB to LSB: Reverse the word order, but not the bytes in a word.
+    while (StoreBytes > sizeof(uint64_t)) {
+      StoreBytes -= sizeof(uint64_t);
+      // May not be aligned so use memcpy.
+      memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
+      Src += sizeof(uint64_t);
+    }
+
+    memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
+  }
+}
+
 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.  Ptr
 /// is the address of the memory at which to store Val, cast to GenericValue *.
 /// It is not a pointer to a GenericValue containing the address at which to
 /// store Val.
-///
 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
                                          const Type *Ty) {
+  const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
+
   switch (Ty->getTypeID()) {
-  case Type::IntegerTyID: {
-    unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
-    GenericValue TmpVal = Val;
-    if (BitWidth <= 8)
-      *((uint8_t*)Ptr) = uint8_t(Val.IntVal.getZExtValue());
-    else if (BitWidth <= 16) {
-      *((uint16_t*)Ptr) = uint16_t(Val.IntVal.getZExtValue());
-    } else if (BitWidth <= 32) {
-      *((uint32_t*)Ptr) = uint32_t(Val.IntVal.getZExtValue());
-    } else if (BitWidth <= 64) {
-      *((uint64_t*)Ptr) = uint64_t(Val.IntVal.getZExtValue());
-    } else {
-      uint64_t *Dest = (uint64_t*)Ptr;
-      const uint64_t *Src = Val.IntVal.getRawData();
-      for (uint32_t i = 0; i < Val.IntVal.getNumWords(); ++i)
-        Dest[i] = Src[i];
-    }
+  case Type::IntegerTyID:
+    StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
     break;
-  }
   case Type::FloatTyID:
     *((float*)Ptr) = Val.FloatVal;
     break;
@@ -673,52 +690,93 @@ void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, GenericValue *
       Dest[4] = Src[3];
       break;
     }
-  case Type::PointerTyID: 
+  case Type::PointerTyID:
+    // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
+    if (StoreBytes != sizeof(PointerTy))
+      memset(Ptr, 0, StoreBytes);
+
     *((PointerTy*)Ptr) = Val.PointerVal;
     break;
   default:
     cerr << "Cannot store value of type " << *Ty << "!\n";
   }
+
+  if (sys::littleEndianHost() != getTargetData()->isLittleEndian())
+    // Host and target are different endian - reverse the stored bytes.
+    std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
+}
+
+/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
+/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
+static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
+  assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
+  uint8_t *Dst = (uint8_t *)IntVal.getRawData();
+
+  if (sys::littleEndianHost())
+    // Little-endian host - the destination must be ordered from LSB to MSB.
+    // The source is ordered from LSB to MSB: Do a straight copy.
+    memcpy(Dst, Src, LoadBytes);
+  else {
+    // Big-endian - the destination is an array of 64 bit words ordered from
+    // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
+    // ordered from MSB to LSB: Reverse the word order, but not the bytes in
+    // a word.
+    while (LoadBytes > sizeof(uint64_t)) {
+      LoadBytes -= sizeof(uint64_t);
+      // May not be aligned so use memcpy.
+      memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
+      Dst += sizeof(uint64_t);
+    }
+
+    memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
+  }
 }
 
 /// FIXME: document
 ///
-void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, 
+void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
                                                   GenericValue *Ptr,
                                                   const Type *Ty) {
+  const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
+
+  if (sys::littleEndianHost() != getTargetData()->isLittleEndian()) {
+    // Host and target are different endian - reverse copy the stored
+    // bytes into a buffer, and load from that.
+    uint8_t *Src = (uint8_t*)Ptr;
+    uint8_t *Buf = (uint8_t*)alloca(LoadBytes);
+    std::reverse_copy(Src, Src + LoadBytes, Buf);
+    Ptr = (GenericValue*)Buf;
+  }
+
   switch (Ty->getTypeID()) {
-  case Type::IntegerTyID: {
-    unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
-    if (BitWidth <= 8)
-      Result.IntVal = APInt(BitWidth, *((uint8_t*)Ptr));
-    else if (BitWidth <= 16) {
-      Result.IntVal = APInt(BitWidth, *((uint16_t*)Ptr));
-    } else if (BitWidth <= 32) {
-      Result.IntVal = APInt(BitWidth, *((uint32_t*)Ptr));
-    } else if (BitWidth <= 64) {
-      Result.IntVal = APInt(BitWidth, *((uint64_t*)Ptr));
-    } else
-      Result.IntVal = APInt(BitWidth, (BitWidth+63)/64, (uint64_t*)Ptr);
+  case Type::IntegerTyID:
+    // An APInt with all words initially zero.
+    Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
+    LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
     break;
-  }
   case Type::FloatTyID:
     Result.FloatVal = *((float*)Ptr);
     break;
   case Type::DoubleTyID:
-    Result.DoubleVal = *((double*)Ptr); 
+    Result.DoubleVal = *((double*)Ptr);
     break;
-  case Type::PointerTyID: 
+  case Type::PointerTyID:
     Result.PointerVal = *((PointerTy*)Ptr);
     break;
   case Type::X86_FP80TyID: {
     // This is endian dependent, but it will only work on x86 anyway.
-    uint16_t x[8], *p = (uint16_t*)Ptr;
+    // FIXME: Will not trap if loading a signaling NaN.
+    uint16_t *p = (uint16_t*)Ptr;
+    union {
+      uint16_t x[8];
+      uint64_t y[2];
+    };
     x[0] = p[1];
     x[1] = p[2];
     x[2] = p[3];
     x[3] = p[4];
     x[4] = p[0];
-    Result.IntVal = APInt(80, 2, x);
+    Result.IntVal = APInt(80, 2, y);
     break;
   }
   default:
@@ -735,7 +793,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;
@@ -744,7 +802,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;
   }
 
@@ -752,7 +810,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;
@@ -843,7 +901,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
@@ -897,7 +955,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];