Implement review feedback for the ConstantBool->ConstantInt merge. Chris
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.cpp
index fe5309558c63d4bbc5432361fe4faba9400eee61..13ee7199d9f908d3abc9a539a6f94abc89bafc73 100644 (file)
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetJITInfo.h"
-#include <iostream>
 using namespace llvm;
 
 #ifdef __APPLE__ 
 #include <AvailabilityMacros.h>
-#if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
-    (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
-     __APPLE_CC__ >= 5330)
+#if defined(MAC_OS_X_VERSION_10_4) && \
+    ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
+     (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
+      __APPLE_CC__ >= 5330))
 // __dso_handle is resolved by Mac OS X dynamic linker.
 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
 #endif
@@ -58,18 +58,18 @@ JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
 
   // Add target data
   MutexGuard locked(lock);
-  FunctionPassManagerPM = state.getPM(locked);
+  FunctionPassManager &PM = state.getPM(locked);
   PM.add(new TargetData(*TM.getTargetData()));
 
-  // Compile LLVM Code down to machine code in the intermediate representation
-  TJI.addPassesToJITCompile(PM);
-
   // Turn the machine code intermediate representation into bytes in memory that
   // may be executed.
-  if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
-    std::cerr << "Target does not support machine code emission!\n";
+  if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
+    cerr << "Target does not support machine code emission!\n";
     abort();
   }
+  
+  // Initialize passes.
+  PM.doInitialization();
 }
 
 JIT::~JIT() {
@@ -95,11 +95,11 @@ GenericValue JIT::runFunction(Function *F,
 
   // Handle some common cases first.  These cases correspond to common `main'
   // prototypes.
-  if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) {
+  if (RetTy == Type::Int32Ty || RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
     switch (ArgValues.size()) {
     case 3:
-      if ((FTy->getParamType(0) == Type::IntTy ||
-           FTy->getParamType(0) == Type::UIntTy) &&
+      if ((FTy->getParamType(0) == Type::Int32Ty ||
+           FTy->getParamType(0) == Type::Int32Ty) &&
           isa<PointerType>(FTy->getParamType(1)) &&
           isa<PointerType>(FTy->getParamType(2))) {
         int (*PF)(int, char **, const char **) =
@@ -107,30 +107,30 @@ GenericValue JIT::runFunction(Function *F,
 
         // Call the function.
         GenericValue rv;
-        rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]),
+        rv.Int32Val = PF(ArgValues[0].Int32Val, (char **)GVTOP(ArgValues[1]),
                        (const char **)GVTOP(ArgValues[2]));
         return rv;
       }
       break;
     case 2:
-      if ((FTy->getParamType(0) == Type::IntTy ||
-           FTy->getParamType(0) == Type::UIntTy) &&
+      if ((FTy->getParamType(0) == Type::Int32Ty ||
+           FTy->getParamType(0) == Type::Int32Ty) &&
           isa<PointerType>(FTy->getParamType(1))) {
         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
 
         // Call the function.
         GenericValue rv;
-        rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]));
+        rv.Int32Val = PF(ArgValues[0].Int32Val, (char **)GVTOP(ArgValues[1]));
         return rv;
       }
       break;
     case 1:
       if (FTy->getNumParams() == 1 &&
-          (FTy->getParamType(0) == Type::IntTy ||
-           FTy->getParamType(0) == Type::UIntTy)) {
+          (FTy->getParamType(0) == Type::Int32Ty ||
+           FTy->getParamType(0) == Type::Int32Ty)) {
         GenericValue rv;
         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
-        rv.IntVal = PF(ArgValues[0].IntVal);
+        rv.Int32Val = PF(ArgValues[0].Int32Val);
         return rv;
       }
       break;
@@ -142,25 +142,21 @@ GenericValue JIT::runFunction(Function *F,
     GenericValue rv;
     switch (RetTy->getTypeID()) {
     default: assert(0 && "Unknown return type for function call!");
-    case Type::BoolTyID:
-      rv.BoolVal = ((bool(*)())(intptr_t)FPtr)();
+    case Type::Int1TyID:
+      rv.Int1Val = ((bool(*)())(intptr_t)FPtr)();
       return rv;
-    case Type::SByteTyID:
-    case Type::UByteTyID:
-      rv.SByteVal = ((char(*)())(intptr_t)FPtr)();
+    case Type::Int8TyID:
+      rv.Int8Val = ((char(*)())(intptr_t)FPtr)();
       return rv;
-    case Type::ShortTyID:
-    case Type::UShortTyID:
-      rv.ShortVal = ((short(*)())(intptr_t)FPtr)();
+    case Type::Int16TyID:
+      rv.Int16Val = ((short(*)())(intptr_t)FPtr)();
       return rv;
     case Type::VoidTyID:
-    case Type::IntTyID:
-    case Type::UIntTyID:
-      rv.IntVal = ((int(*)())(intptr_t)FPtr)();
+    case Type::Int32TyID:
+      rv.Int32Val = ((int(*)())(intptr_t)FPtr)();
       return rv;
-    case Type::LongTyID:
-    case Type::ULongTyID:
-      rv.LongVal = ((int64_t(*)())(intptr_t)FPtr)();
+    case Type::Int64TyID:
+      rv.Int64Val = ((int64_t(*)())(intptr_t)FPtr)();
       return rv;
     case Type::FloatTyID:
       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
@@ -195,25 +191,21 @@ GenericValue JIT::runFunction(Function *F,
     const GenericValue &AV = ArgValues[i];
     switch (ArgTy->getTypeID()) {
     default: assert(0 && "Unknown argument type for function call!");
-    case Type::BoolTyID:   C = ConstantBool::get(AV.BoolVal); break;
-    case Type::SByteTyID:  C = ConstantSInt::get(ArgTy, AV.SByteVal);  break;
-    case Type::UByteTyID:  C = ConstantUInt::get(ArgTy, AV.UByteVal);  break;
-    case Type::ShortTyID:  C = ConstantSInt::get(ArgTy, AV.ShortVal);  break;
-    case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break;
-    case Type::IntTyID:    C = ConstantSInt::get(ArgTy, AV.IntVal);    break;
-    case Type::UIntTyID:   C = ConstantUInt::get(ArgTy, AV.UIntVal);   break;
-    case Type::LongTyID:   C = ConstantSInt::get(ArgTy, AV.LongVal);   break;
-    case Type::ULongTyID:  C = ConstantUInt::get(ArgTy, AV.ULongVal);  break;
-    case Type::FloatTyID:  C = ConstantFP  ::get(ArgTy, AV.FloatVal);  break;
-    case Type::DoubleTyID: C = ConstantFP  ::get(ArgTy, AV.DoubleVal); break;
+    case Type::Int1TyID:   C = ConstantInt::get(ArgTy, AV.Int1Val); break;
+    case Type::Int8TyID:   C = ConstantInt::get(ArgTy, AV.Int8Val);  break;
+    case Type::Int16TyID:  C = ConstantInt::get(ArgTy, AV.Int16Val);  break;
+    case Type::Int32TyID:  C = ConstantInt::get(ArgTy, AV.Int32Val);    break;
+    case Type::Int64TyID:  C = ConstantInt::get(ArgTy, AV.Int64Val);   break;
+    case Type::FloatTyID:  C = ConstantFP ::get(ArgTy, AV.FloatVal);  break;
+    case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
     case Type::PointerTyID:
       void *ArgPtr = GVTOP(AV);
       if (sizeof(void*) == 4) {
-        C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr);
+        C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
       } else {
-        C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr);
+        C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
       }
-      C = ConstantExpr::getCast(C, ArgTy);  // Cast the integer to pointer
+      C = ConstantExpr::getIntToPtr(C, ArgTy);  // Cast the integer to pointer
       break;
     }
     Args.push_back(C);
@@ -279,8 +271,8 @@ void *JIT::getPointerToFunction(Function *F) {
     
     std::string ErrorMsg;
     if (MP->materializeFunction(F, &ErrorMsg)) {
-      std::cerr << "Error reading function '" << F->getName()
-                << "' from bytecode file: " << ErrorMsg << "\n";
+      cerr << "Error reading function '" << F->getName()
+           << "' from bytecode file: " << ErrorMsg << "\n";
       abort();
     }
   }
@@ -309,22 +301,21 @@ void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
 
   // If the global is external, just remember the address.
   if (GV->isExternal()) {
-#ifdef __APPLE__
-#if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
-    (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
-     __APPLE_CC__ >= 5330)
+#if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_4) && \
+    ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
+     (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
+      __APPLE_CC__ >= 5330))
     // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
     // of atexit). It passes the address of linker generated symbol __dso_handle
     // to the function.
     // This configuration change happened at version 5330.
     if (GV->getName() == "__dso_handle")
       return (void*)&__dso_handle;
-#endif
 #endif
     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
     if (Ptr == 0) {
-      std::cerr << "Could not resolve external global address: "
-                << GV->getName() << "\n";
+      cerr << "Could not resolve external global address: "
+           << GV->getName() << "\n";
       abort();
     }
   } else {