1857c21b4SMisha Brukman //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2996fe010SChris Lattner //
3482202a6SJohn Criswell //                     The LLVM Compiler Infrastructure
4482202a6SJohn Criswell //
5f3ebc3f3SChris Lattner // This file is distributed under the University of Illinois Open Source
6f3ebc3f3SChris Lattner // License. See LICENSE.TXT for details.
7482202a6SJohn Criswell //
8482202a6SJohn Criswell //===----------------------------------------------------------------------===//
9482202a6SJohn Criswell //
10996fe010SChris Lattner // This file defines the common interface used by the various execution engine
11996fe010SChris Lattner // subclasses.
12996fe010SChris Lattner //
13996fe010SChris Lattner //===----------------------------------------------------------------------===//
14996fe010SChris Lattner 
15ee937c80SChris Lattner #define DEBUG_TYPE "jit"
16996fe010SChris Lattner #include "llvm/Constants.h"
17260b0c88SMisha Brukman #include "llvm/DerivedTypes.h"
18996fe010SChris Lattner #include "llvm/Module.h"
19260b0c88SMisha Brukman #include "llvm/ModuleProvider.h"
2070e37278SReid Spencer #include "llvm/ADT/Statistic.h"
211202d1b1SDuncan Sands #include "llvm/Config/alloca.h"
22260b0c88SMisha Brukman #include "llvm/ExecutionEngine/ExecutionEngine.h"
23ad481312SChris Lattner #include "llvm/ExecutionEngine/GenericValue.h"
247c16caa3SReid Spencer #include "llvm/Support/Debug.h"
256d8dd189SChris Lattner #include "llvm/Support/MutexGuard.h"
2670e37278SReid Spencer #include "llvm/System/DynamicLibrary.h"
27fde55674SDuncan Sands #include "llvm/System/Host.h"
2870e37278SReid Spencer #include "llvm/Target/TargetData.h"
29579f0713SAnton Korobeynikov #include <cmath>
30579f0713SAnton Korobeynikov #include <cstring>
3129681deeSChris Lattner using namespace llvm;
32996fe010SChris Lattner 
33c346ecd7SChris Lattner STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
34c346ecd7SChris Lattner STATISTIC(NumGlobals  , "Number of global vars initialized");
35996fe010SChris Lattner 
362d52c1b8SChris Lattner ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
372d52c1b8SChris Lattner ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
3821ad494fSNicolas Geoffray ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0;
3921ad494fSNicolas Geoffray 
402d52c1b8SChris Lattner 
41fd6f3257SChris Lattner ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
4287aee74cSChris Lattner   LazyCompilationDisabled = false;
43*cdc0060eSEvan Cheng   GVCompilationDisabled   = false;
4484a9055eSEvan Cheng   SymbolSearchingDisabled = false;
450621caefSChris Lattner   Modules.push_back(P);
46260b0c88SMisha Brukman   assert(P && "ModuleProvider is null?");
47260b0c88SMisha Brukman }
48260b0c88SMisha Brukman 
4992f8b30dSBrian Gaeke ExecutionEngine::~ExecutionEngine() {
50603682adSReid Spencer   clearAllGlobalMappings();
510621caefSChris Lattner   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
520621caefSChris Lattner     delete Modules[i];
5392f8b30dSBrian Gaeke }
5492f8b30dSBrian Gaeke 
55324fe890SDevang Patel /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
56324fe890SDevang Patel /// Release module from ModuleProvider.
57324fe890SDevang Patel Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P,
58324fe890SDevang Patel                                               std::string *ErrInfo) {
59324fe890SDevang Patel   for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(),
60324fe890SDevang Patel         E = Modules.end(); I != E; ++I) {
61324fe890SDevang Patel     ModuleProvider *MP = *I;
62324fe890SDevang Patel     if (MP == P) {
63324fe890SDevang Patel       Modules.erase(I);
648f83fc4dSNate Begeman       clearGlobalMappingsFromModule(MP->getModule());
65324fe890SDevang Patel       return MP->releaseModule(ErrInfo);
66324fe890SDevang Patel     }
67324fe890SDevang Patel   }
68324fe890SDevang Patel   return NULL;
69324fe890SDevang Patel }
70324fe890SDevang Patel 
710621caefSChris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that
720621caefSChris Lattner /// defines FnName.  This is very slow operation and shouldn't be used for
730621caefSChris Lattner /// general code.
740621caefSChris Lattner Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
750621caefSChris Lattner   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
761241d6d5SReid Spencer     if (Function *F = Modules[i]->getModule()->getFunction(FnName))
770621caefSChris Lattner       return F;
780621caefSChris Lattner   }
790621caefSChris Lattner   return 0;
800621caefSChris Lattner }
810621caefSChris Lattner 
820621caefSChris Lattner 
836d8dd189SChris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is
846d8dd189SChris Lattner /// at the specified location.  This is used internally as functions are JIT'd
856d8dd189SChris Lattner /// and as global variables are laid out in memory.  It can and should also be
866d8dd189SChris Lattner /// used by clients of the EE that want to have an LLVM global overlay
876d8dd189SChris Lattner /// existing data in memory.
886d8dd189SChris Lattner void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
896d8dd189SChris Lattner   MutexGuard locked(lock);
906d8dd189SChris Lattner 
915cc53c34SEvan Cheng   DOUT << "Map " << *GV << " to " << Addr << "\n";
926d8dd189SChris Lattner   void *&CurVal = state.getGlobalAddressMap(locked)[GV];
936d8dd189SChris Lattner   assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
946d8dd189SChris Lattner   CurVal = Addr;
956d8dd189SChris Lattner 
966d8dd189SChris Lattner   // If we are using the reverse mapping, add it too
976d8dd189SChris Lattner   if (!state.getGlobalAddressReverseMap(locked).empty()) {
986d8dd189SChris Lattner     const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
996d8dd189SChris Lattner     assert((V == 0 || GV == 0) && "GlobalMapping already established!");
1006d8dd189SChris Lattner     V = GV;
1016d8dd189SChris Lattner   }
1026d8dd189SChris Lattner }
1036d8dd189SChris Lattner 
1046d8dd189SChris Lattner /// clearAllGlobalMappings - Clear all global mappings and start over again
1056d8dd189SChris Lattner /// use in dynamic compilation scenarios when you want to move globals
1066d8dd189SChris Lattner void ExecutionEngine::clearAllGlobalMappings() {
1076d8dd189SChris Lattner   MutexGuard locked(lock);
1086d8dd189SChris Lattner 
1096d8dd189SChris Lattner   state.getGlobalAddressMap(locked).clear();
1106d8dd189SChris Lattner   state.getGlobalAddressReverseMap(locked).clear();
1116d8dd189SChris Lattner }
1126d8dd189SChris Lattner 
1138f83fc4dSNate Begeman /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
1148f83fc4dSNate Begeman /// particular module, because it has been removed from the JIT.
1158f83fc4dSNate Begeman void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
1168f83fc4dSNate Begeman   MutexGuard locked(lock);
1178f83fc4dSNate Begeman 
1188f83fc4dSNate Begeman   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
1198f83fc4dSNate Begeman     state.getGlobalAddressMap(locked).erase(FI);
1208f83fc4dSNate Begeman     state.getGlobalAddressReverseMap(locked).erase(FI);
1218f83fc4dSNate Begeman   }
1228f83fc4dSNate Begeman   for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
1238f83fc4dSNate Begeman        GI != GE; ++GI) {
1248f83fc4dSNate Begeman     state.getGlobalAddressMap(locked).erase(GI);
1258f83fc4dSNate Begeman     state.getGlobalAddressReverseMap(locked).erase(GI);
1268f83fc4dSNate Begeman   }
1278f83fc4dSNate Begeman }
1288f83fc4dSNate Begeman 
1296d8dd189SChris Lattner /// updateGlobalMapping - Replace an existing mapping for GV with a new
1306d8dd189SChris Lattner /// address.  This updates both maps as required.  If "Addr" is null, the
1316d8dd189SChris Lattner /// entry for the global is removed from the mappings.
132ee181730SChris Lattner void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
1336d8dd189SChris Lattner   MutexGuard locked(lock);
1346d8dd189SChris Lattner 
135ee181730SChris Lattner   std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked);
136ee181730SChris Lattner 
1376d8dd189SChris Lattner   // Deleting from the mapping?
1386d8dd189SChris Lattner   if (Addr == 0) {
139ee181730SChris Lattner     std::map<const GlobalValue*, void *>::iterator I = Map.find(GV);
140ee181730SChris Lattner     void *OldVal;
141ee181730SChris Lattner     if (I == Map.end())
142ee181730SChris Lattner       OldVal = 0;
143ee181730SChris Lattner     else {
144ee181730SChris Lattner       OldVal = I->second;
145ee181730SChris Lattner       Map.erase(I);
1466d8dd189SChris Lattner     }
1476d8dd189SChris Lattner 
148ee181730SChris Lattner     if (!state.getGlobalAddressReverseMap(locked).empty())
149ee181730SChris Lattner       state.getGlobalAddressReverseMap(locked).erase(Addr);
150ee181730SChris Lattner     return OldVal;
151ee181730SChris Lattner   }
152ee181730SChris Lattner 
153ee181730SChris Lattner   void *&CurVal = Map[GV];
154ee181730SChris Lattner   void *OldVal = CurVal;
155ee181730SChris Lattner 
1566d8dd189SChris Lattner   if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
1576d8dd189SChris Lattner     state.getGlobalAddressReverseMap(locked).erase(CurVal);
1586d8dd189SChris Lattner   CurVal = Addr;
1596d8dd189SChris Lattner 
1606d8dd189SChris Lattner   // If we are using the reverse mapping, add it too
1616d8dd189SChris Lattner   if (!state.getGlobalAddressReverseMap(locked).empty()) {
1626d8dd189SChris Lattner     const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
1636d8dd189SChris Lattner     assert((V == 0 || GV == 0) && "GlobalMapping already established!");
1646d8dd189SChris Lattner     V = GV;
1656d8dd189SChris Lattner   }
166ee181730SChris Lattner   return OldVal;
1676d8dd189SChris Lattner }
1686d8dd189SChris Lattner 
1696d8dd189SChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified
1706d8dd189SChris Lattner /// global value if it is has already been codegen'd, otherwise it returns null.
1716d8dd189SChris Lattner ///
1726d8dd189SChris Lattner void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
1736d8dd189SChris Lattner   MutexGuard locked(lock);
1746d8dd189SChris Lattner 
1756d8dd189SChris Lattner   std::map<const GlobalValue*, void*>::iterator I =
1766d8dd189SChris Lattner   state.getGlobalAddressMap(locked).find(GV);
1776d8dd189SChris Lattner   return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
1786d8dd189SChris Lattner }
1796d8dd189SChris Lattner 
180748e8579SChris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts
181748e8579SChris Lattner /// at the specified address.
182748e8579SChris Lattner ///
183748e8579SChris Lattner const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
18479876f52SReid Spencer   MutexGuard locked(lock);
18579876f52SReid Spencer 
186748e8579SChris Lattner   // If we haven't computed the reverse mapping yet, do so first.
18779876f52SReid Spencer   if (state.getGlobalAddressReverseMap(locked).empty()) {
1886d8dd189SChris Lattner     for (std::map<const GlobalValue*, void *>::iterator
1896d8dd189SChris Lattner          I = state.getGlobalAddressMap(locked).begin(),
1906d8dd189SChris Lattner          E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
1916d8dd189SChris Lattner       state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
1926d8dd189SChris Lattner                                                                      I->first));
193748e8579SChris Lattner   }
194748e8579SChris Lattner 
195748e8579SChris Lattner   std::map<void *, const GlobalValue*>::iterator I =
19679876f52SReid Spencer     state.getGlobalAddressReverseMap(locked).find(Addr);
19779876f52SReid Spencer   return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
198748e8579SChris Lattner }
1995a0d4829SChris Lattner 
2005a0d4829SChris Lattner // CreateArgv - Turn a vector of strings into a nice argv style array of
2015a0d4829SChris Lattner // pointers to null terminated strings.
2025a0d4829SChris Lattner //
2035a0d4829SChris Lattner static void *CreateArgv(ExecutionEngine *EE,
2045a0d4829SChris Lattner                         const std::vector<std::string> &InputArgv) {
20520a631fdSOwen Anderson   unsigned PtrSize = EE->getTargetData()->getPointerSize();
2065a0d4829SChris Lattner   char *Result = new char[(InputArgv.size()+1)*PtrSize];
2075a0d4829SChris Lattner 
2085834fdb3SBill Wendling   DOUT << "ARGV = " << (void*)Result << "\n";
209edf07887SChristopher Lamb   const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty);
2105a0d4829SChris Lattner 
2115a0d4829SChris Lattner   for (unsigned i = 0; i != InputArgv.size(); ++i) {
2125a0d4829SChris Lattner     unsigned Size = InputArgv[i].size()+1;
2135a0d4829SChris Lattner     char *Dest = new char[Size];
2145834fdb3SBill Wendling     DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n";
2155a0d4829SChris Lattner 
2165a0d4829SChris Lattner     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
2175a0d4829SChris Lattner     Dest[Size-1] = 0;
2185a0d4829SChris Lattner 
2195a0d4829SChris Lattner     // Endian safe: Result[i] = (PointerTy)Dest;
2205a0d4829SChris Lattner     EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
2215a0d4829SChris Lattner                            SBytePtr);
2225a0d4829SChris Lattner   }
2235a0d4829SChris Lattner 
2245a0d4829SChris Lattner   // Null terminate it
2255a0d4829SChris Lattner   EE->StoreValueToMemory(PTOGV(0),
2265a0d4829SChris Lattner                          (GenericValue*)(Result+InputArgv.size()*PtrSize),
2275a0d4829SChris Lattner                          SBytePtr);
2285a0d4829SChris Lattner   return Result;
2295a0d4829SChris Lattner }
2305a0d4829SChris Lattner 
231faae50b6SChris Lattner 
232faae50b6SChris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of
2330621caefSChris Lattner /// the static constructors or destructors for a program, depending on the
234faae50b6SChris Lattner /// value of isDtors.
235faae50b6SChris Lattner void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
236faae50b6SChris Lattner   const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
2370621caefSChris Lattner 
2380621caefSChris Lattner   // Execute global ctors/dtors for each module in the program.
2390621caefSChris Lattner   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
2400621caefSChris Lattner     GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
241fe36eaebSChris Lattner 
242fe36eaebSChris Lattner     // If this global has internal linkage, or if it has a use, then it must be
243fe36eaebSChris Lattner     // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
2440621caefSChris Lattner     // this is the case, don't execute any of the global ctors, __main will do
2450621caefSChris Lattner     // it.
2465301e7c6SReid Spencer     if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) continue;
247faae50b6SChris Lattner 
2480621caefSChris Lattner     // Should be an array of '{ int, void ()* }' structs.  The first value is
2490621caefSChris Lattner     // the init priority, which we ignore.
250faae50b6SChris Lattner     ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
2510621caefSChris Lattner     if (!InitList) continue;
252faae50b6SChris Lattner     for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
2530621caefSChris Lattner       if (ConstantStruct *CS =
2540621caefSChris Lattner           dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
2550621caefSChris Lattner         if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
256faae50b6SChris Lattner 
257faae50b6SChris Lattner         Constant *FP = CS->getOperand(1);
258faae50b6SChris Lattner         if (FP->isNullValue())
2590621caefSChris Lattner           break;  // Found a null terminator, exit.
260faae50b6SChris Lattner 
261faae50b6SChris Lattner         if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
2626c38f0bbSReid Spencer           if (CE->isCast())
263faae50b6SChris Lattner             FP = CE->getOperand(0);
264faae50b6SChris Lattner         if (Function *F = dyn_cast<Function>(FP)) {
265faae50b6SChris Lattner           // Execute the ctor/dtor function!
266faae50b6SChris Lattner           runFunction(F, std::vector<GenericValue>());
267faae50b6SChris Lattner         }
268faae50b6SChris Lattner       }
269faae50b6SChris Lattner   }
2700621caefSChris Lattner }
271faae50b6SChris Lattner 
272cf3e3017SDan Gohman #ifndef NDEBUG
2731202d1b1SDuncan Sands /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
2741202d1b1SDuncan Sands static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
2751202d1b1SDuncan Sands   unsigned PtrSize = EE->getTargetData()->getPointerSize();
2761202d1b1SDuncan Sands   for (unsigned i = 0; i < PtrSize; ++i)
2771202d1b1SDuncan Sands     if (*(i + (uint8_t*)Loc))
2781202d1b1SDuncan Sands       return false;
2791202d1b1SDuncan Sands   return true;
2801202d1b1SDuncan Sands }
281cf3e3017SDan Gohman #endif
2821202d1b1SDuncan Sands 
2835a0d4829SChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to
2845a0d4829SChris Lattner /// handle the common task of starting up main with the specified argc, argv,
2855a0d4829SChris Lattner /// and envp parameters.
2865a0d4829SChris Lattner int ExecutionEngine::runFunctionAsMain(Function *Fn,
2875a0d4829SChris Lattner                                        const std::vector<std::string> &argv,
2885a0d4829SChris Lattner                                        const char * const * envp) {
2895a0d4829SChris Lattner   std::vector<GenericValue> GVArgs;
2905a0d4829SChris Lattner   GenericValue GVArgc;
29187aa65f4SReid Spencer   GVArgc.IntVal = APInt(32, argv.size());
2928c32c111SAnton Korobeynikov 
2938c32c111SAnton Korobeynikov   // Check main() type
294b1cad0b3SChris Lattner   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
2958c32c111SAnton Korobeynikov   const FunctionType *FTy = Fn->getFunctionType();
296edf07887SChristopher Lamb   const Type* PPInt8Ty =
297edf07887SChristopher Lamb     PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
2988c32c111SAnton Korobeynikov   switch (NumArgs) {
2998c32c111SAnton Korobeynikov   case 3:
3008c32c111SAnton Korobeynikov    if (FTy->getParamType(2) != PPInt8Ty) {
3018c32c111SAnton Korobeynikov      cerr << "Invalid type for third argument of main() supplied\n";
3028c32c111SAnton Korobeynikov      abort();
3038c32c111SAnton Korobeynikov    }
304b781886dSAnton Korobeynikov    // FALLS THROUGH
3058c32c111SAnton Korobeynikov   case 2:
3068c32c111SAnton Korobeynikov    if (FTy->getParamType(1) != PPInt8Ty) {
3078c32c111SAnton Korobeynikov      cerr << "Invalid type for second argument of main() supplied\n";
3088c32c111SAnton Korobeynikov      abort();
3098c32c111SAnton Korobeynikov    }
310b781886dSAnton Korobeynikov    // FALLS THROUGH
3118c32c111SAnton Korobeynikov   case 1:
3128c32c111SAnton Korobeynikov    if (FTy->getParamType(0) != Type::Int32Ty) {
3138c32c111SAnton Korobeynikov      cerr << "Invalid type for first argument of main() supplied\n";
3148c32c111SAnton Korobeynikov      abort();
3158c32c111SAnton Korobeynikov    }
316b781886dSAnton Korobeynikov    // FALLS THROUGH
3178c32c111SAnton Korobeynikov   case 0:
3188c32c111SAnton Korobeynikov    if (FTy->getReturnType() != Type::Int32Ty &&
3198c32c111SAnton Korobeynikov        FTy->getReturnType() != Type::VoidTy) {
3208c32c111SAnton Korobeynikov      cerr << "Invalid return type of main() supplied\n";
3218c32c111SAnton Korobeynikov      abort();
3228c32c111SAnton Korobeynikov    }
3238c32c111SAnton Korobeynikov    break;
3248c32c111SAnton Korobeynikov   default:
3258c32c111SAnton Korobeynikov    cerr << "Invalid number of arguments of main() supplied\n";
3268c32c111SAnton Korobeynikov    abort();
3278c32c111SAnton Korobeynikov   }
3288c32c111SAnton Korobeynikov 
329b1cad0b3SChris Lattner   if (NumArgs) {
3305a0d4829SChris Lattner     GVArgs.push_back(GVArgc); // Arg #0 = argc.
331b1cad0b3SChris Lattner     if (NumArgs > 1) {
3325a0d4829SChris Lattner       GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
3331202d1b1SDuncan Sands       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
334b1cad0b3SChris Lattner              "argv[0] was null after CreateArgv");
335b1cad0b3SChris Lattner       if (NumArgs > 2) {
3365a0d4829SChris Lattner         std::vector<std::string> EnvVars;
3375a0d4829SChris Lattner         for (unsigned i = 0; envp[i]; ++i)
3385a0d4829SChris Lattner           EnvVars.push_back(envp[i]);
3395a0d4829SChris Lattner         GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
340b1cad0b3SChris Lattner       }
341b1cad0b3SChris Lattner     }
342b1cad0b3SChris Lattner   }
34387aa65f4SReid Spencer   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
3445a0d4829SChris Lattner }
3455a0d4829SChris Lattner 
346260b0c88SMisha Brukman /// If possible, create a JIT, unless the caller specifically requests an
347260b0c88SMisha Brukman /// Interpreter or there's an error. If even an Interpreter cannot be created,
348260b0c88SMisha Brukman /// NULL is returned.
349857c21b4SMisha Brukman ///
3502f1e2002SMisha Brukman ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
351603682adSReid Spencer                                          bool ForceInterpreter,
3527ff05bf5SEvan Cheng                                          std::string *ErrorStr,
3537ff05bf5SEvan Cheng                                          bool Fast) {
3544bd3bd5bSBrian Gaeke   ExecutionEngine *EE = 0;
3554bd3bd5bSBrian Gaeke 
356a53414fdSNick Lewycky   // Make sure we can resolve symbols in the program as well. The zero arg
357a53414fdSNick Lewycky   // to the function tells DynamicLibrary to load the program, not a library.
358a53414fdSNick Lewycky   if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
359a53414fdSNick Lewycky     return 0;
360a53414fdSNick Lewycky 
361c8c6c03dSChris Lattner   // Unless the interpreter was explicitly selected, try making a JIT.
3622d52c1b8SChris Lattner   if (!ForceInterpreter && JITCtor)
3637ff05bf5SEvan Cheng     EE = JITCtor(MP, ErrorStr, Fast);
3644bd3bd5bSBrian Gaeke 
3654bd3bd5bSBrian Gaeke   // If we can't make a JIT, make an interpreter instead.
3662d52c1b8SChris Lattner   if (EE == 0 && InterpCtor)
3677ff05bf5SEvan Cheng     EE = InterpCtor(MP, ErrorStr, Fast);
368c8c6c03dSChris Lattner 
3694bd3bd5bSBrian Gaeke   return EE;
3704bd3bd5bSBrian Gaeke }
3714bd3bd5bSBrian Gaeke 
372b5163bb9SChris Lattner ExecutionEngine *ExecutionEngine::create(Module *M) {
373b5163bb9SChris Lattner   return create(new ExistingModuleProvider(M));
374b5163bb9SChris Lattner }
375b5163bb9SChris Lattner 
376857c21b4SMisha Brukman /// getPointerToGlobal - This returns the address of the specified global
377857c21b4SMisha Brukman /// value.  This may involve code generation if it's a function.
378857c21b4SMisha Brukman ///
379996fe010SChris Lattner void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
3801678e859SBrian Gaeke   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
381996fe010SChris Lattner     return getPointerToFunction(F);
382996fe010SChris Lattner 
38379876f52SReid Spencer   MutexGuard locked(lock);
38469e84901SJeff Cohen   void *p = state.getGlobalAddressMap(locked)[GV];
38569e84901SJeff Cohen   if (p)
38669e84901SJeff Cohen     return p;
38769e84901SJeff Cohen 
38869e84901SJeff Cohen   // Global variable might have been added since interpreter started.
38969e84901SJeff Cohen   if (GlobalVariable *GVar =
39069e84901SJeff Cohen           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
39169e84901SJeff Cohen     EmitGlobalVariable(GVar);
39269e84901SJeff Cohen   else
3934da5e17cSChris Lattner     assert(0 && "Global hasn't had an address allocated yet!");
39479876f52SReid Spencer   return state.getGlobalAddressMap(locked)[GV];
395996fe010SChris Lattner }
396996fe010SChris Lattner 
3976c38f0bbSReid Spencer /// This function converts a Constant* into a GenericValue. The interesting
3986c38f0bbSReid Spencer /// part is if C is a ConstantExpr.
3992dc9f132SReid Spencer /// @brief Get a GenericValue for a Constant*
400996fe010SChris Lattner GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
4016c38f0bbSReid Spencer   // If its undefined, return the garbage.
4024fd528f2SReid Spencer   if (isa<UndefValue>(C))
4034fd528f2SReid Spencer     return GenericValue();
4049de0d14dSChris Lattner 
4056c38f0bbSReid Spencer   // If the value is a ConstantExpr
4066c38f0bbSReid Spencer   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
4074fd528f2SReid Spencer     Constant *Op0 = CE->getOperand(0);
4089de0d14dSChris Lattner     switch (CE->getOpcode()) {
4099de0d14dSChris Lattner     case Instruction::GetElementPtr: {
4106c38f0bbSReid Spencer       // Compute the index
4114fd528f2SReid Spencer       GenericValue Result = getConstantValue(Op0);
412c44bd78aSChris Lattner       SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
4139de0d14dSChris Lattner       uint64_t Offset =
4144fd528f2SReid Spencer         TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size());
4159de0d14dSChris Lattner 
41687aa65f4SReid Spencer       char* tmp = (char*) Result.PointerVal;
41787aa65f4SReid Spencer       Result = PTOGV(tmp + Offset);
4189de0d14dSChris Lattner       return Result;
4199de0d14dSChris Lattner     }
4204fd528f2SReid Spencer     case Instruction::Trunc: {
4214fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4224fd528f2SReid Spencer       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
4234fd528f2SReid Spencer       GV.IntVal = GV.IntVal.trunc(BitWidth);
4244fd528f2SReid Spencer       return GV;
4254fd528f2SReid Spencer     }
4264fd528f2SReid Spencer     case Instruction::ZExt: {
4274fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4284fd528f2SReid Spencer       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
4294fd528f2SReid Spencer       GV.IntVal = GV.IntVal.zext(BitWidth);
4304fd528f2SReid Spencer       return GV;
4314fd528f2SReid Spencer     }
4324fd528f2SReid Spencer     case Instruction::SExt: {
4334fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4344fd528f2SReid Spencer       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
4354fd528f2SReid Spencer       GV.IntVal = GV.IntVal.sext(BitWidth);
4364fd528f2SReid Spencer       return GV;
4374fd528f2SReid Spencer     }
4384fd528f2SReid Spencer     case Instruction::FPTrunc: {
439a1336cf5SDale Johannesen       // FIXME long double
4404fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4414fd528f2SReid Spencer       GV.FloatVal = float(GV.DoubleVal);
4424fd528f2SReid Spencer       return GV;
4434fd528f2SReid Spencer     }
4444fd528f2SReid Spencer     case Instruction::FPExt:{
445a1336cf5SDale Johannesen       // FIXME long double
4464fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4474fd528f2SReid Spencer       GV.DoubleVal = double(GV.FloatVal);
4484fd528f2SReid Spencer       return GV;
4494fd528f2SReid Spencer     }
4504fd528f2SReid Spencer     case Instruction::UIToFP: {
4514fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4524fd528f2SReid Spencer       if (CE->getType() == Type::FloatTy)
4534fd528f2SReid Spencer         GV.FloatVal = float(GV.IntVal.roundToDouble());
454a1336cf5SDale Johannesen       else if (CE->getType() == Type::DoubleTy)
4554fd528f2SReid Spencer         GV.DoubleVal = GV.IntVal.roundToDouble();
456a1336cf5SDale Johannesen       else if (CE->getType() == Type::X86_FP80Ty) {
457a1336cf5SDale Johannesen         const uint64_t zero[] = {0, 0};
458a1336cf5SDale Johannesen         APFloat apf = APFloat(APInt(80, 2, zero));
459ca24fd90SDan Gohman         (void)apf.convertFromAPInt(GV.IntVal,
460ca24fd90SDan Gohman                                    false,
4619150652bSDale Johannesen                                    APFloat::rmNearestTiesToEven);
462a1336cf5SDale Johannesen         GV.IntVal = apf.convertToAPInt();
463a1336cf5SDale Johannesen       }
4644fd528f2SReid Spencer       return GV;
4654fd528f2SReid Spencer     }
4664fd528f2SReid Spencer     case Instruction::SIToFP: {
4674fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4684fd528f2SReid Spencer       if (CE->getType() == Type::FloatTy)
4694fd528f2SReid Spencer         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
470a1336cf5SDale Johannesen       else if (CE->getType() == Type::DoubleTy)
4714fd528f2SReid Spencer         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
472a1336cf5SDale Johannesen       else if (CE->getType() == Type::X86_FP80Ty) {
473a1336cf5SDale Johannesen         const uint64_t zero[] = { 0, 0};
474a1336cf5SDale Johannesen         APFloat apf = APFloat(APInt(80, 2, zero));
475ca24fd90SDan Gohman         (void)apf.convertFromAPInt(GV.IntVal,
476ca24fd90SDan Gohman                                    true,
4779150652bSDale Johannesen                                    APFloat::rmNearestTiesToEven);
478a1336cf5SDale Johannesen         GV.IntVal = apf.convertToAPInt();
479a1336cf5SDale Johannesen       }
4804fd528f2SReid Spencer       return GV;
4814fd528f2SReid Spencer     }
4824fd528f2SReid Spencer     case Instruction::FPToUI: // double->APInt conversion handles sign
4834fd528f2SReid Spencer     case Instruction::FPToSI: {
4844fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
4854fd528f2SReid Spencer       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
4864fd528f2SReid Spencer       if (Op0->getType() == Type::FloatTy)
4874fd528f2SReid Spencer         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
488a1336cf5SDale Johannesen       else if (Op0->getType() == Type::DoubleTy)
4894fd528f2SReid Spencer         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
490a1336cf5SDale Johannesen       else if (Op0->getType() == Type::X86_FP80Ty) {
491a1336cf5SDale Johannesen         APFloat apf = APFloat(GV.IntVal);
492a1336cf5SDale Johannesen         uint64_t v;
493a1336cf5SDale Johannesen         (void)apf.convertToInteger(&v, BitWidth,
494a1336cf5SDale Johannesen                                    CE->getOpcode()==Instruction::FPToSI,
495a1336cf5SDale Johannesen                                    APFloat::rmTowardZero);
496a1336cf5SDale Johannesen         GV.IntVal = v; // endian?
497a1336cf5SDale Johannesen       }
4984fd528f2SReid Spencer       return GV;
4994fd528f2SReid Spencer     }
5006c38f0bbSReid Spencer     case Instruction::PtrToInt: {
5014fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
5024fd528f2SReid Spencer       uint32_t PtrWidth = TD->getPointerSizeInBits();
5034fd528f2SReid Spencer       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
5044fd528f2SReid Spencer       return GV;
5054fd528f2SReid Spencer     }
5064fd528f2SReid Spencer     case Instruction::IntToPtr: {
5074fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
5084fd528f2SReid Spencer       uint32_t PtrWidth = TD->getPointerSizeInBits();
5094fd528f2SReid Spencer       if (PtrWidth != GV.IntVal.getBitWidth())
5104fd528f2SReid Spencer         GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
5114fd528f2SReid Spencer       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
5124fd528f2SReid Spencer       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
5136c38f0bbSReid Spencer       return GV;
5146c38f0bbSReid Spencer     }
5156c38f0bbSReid Spencer     case Instruction::BitCast: {
5164fd528f2SReid Spencer       GenericValue GV = getConstantValue(Op0);
5174fd528f2SReid Spencer       const Type* DestTy = CE->getType();
5184fd528f2SReid Spencer       switch (Op0->getType()->getTypeID()) {
5194fd528f2SReid Spencer         default: assert(0 && "Invalid bitcast operand");
5204fd528f2SReid Spencer         case Type::IntegerTyID:
5214fd528f2SReid Spencer           assert(DestTy->isFloatingPoint() && "invalid bitcast");
5224fd528f2SReid Spencer           if (DestTy == Type::FloatTy)
5234fd528f2SReid Spencer             GV.FloatVal = GV.IntVal.bitsToFloat();
5244fd528f2SReid Spencer           else if (DestTy == Type::DoubleTy)
5254fd528f2SReid Spencer             GV.DoubleVal = GV.IntVal.bitsToDouble();
5266c38f0bbSReid Spencer           break;
5274fd528f2SReid Spencer         case Type::FloatTyID:
5284fd528f2SReid Spencer           assert(DestTy == Type::Int32Ty && "Invalid bitcast");
5294fd528f2SReid Spencer           GV.IntVal.floatToBits(GV.FloatVal);
5304fd528f2SReid Spencer           break;
5314fd528f2SReid Spencer         case Type::DoubleTyID:
5324fd528f2SReid Spencer           assert(DestTy == Type::Int64Ty && "Invalid bitcast");
5334fd528f2SReid Spencer           GV.IntVal.doubleToBits(GV.DoubleVal);
5344fd528f2SReid Spencer           break;
5354fd528f2SReid Spencer         case Type::PointerTyID:
5364fd528f2SReid Spencer           assert(isa<PointerType>(DestTy) && "Invalid bitcast");
5374fd528f2SReid Spencer           break; // getConstantValue(Op0)  above already converted it
5386c38f0bbSReid Spencer       }
5394fd528f2SReid Spencer       return GV;
54068cbcc3eSChris Lattner     }
54168cbcc3eSChris Lattner     case Instruction::Add:
5424fd528f2SReid Spencer     case Instruction::Sub:
5434fd528f2SReid Spencer     case Instruction::Mul:
5444fd528f2SReid Spencer     case Instruction::UDiv:
5454fd528f2SReid Spencer     case Instruction::SDiv:
5464fd528f2SReid Spencer     case Instruction::URem:
5474fd528f2SReid Spencer     case Instruction::SRem:
5484fd528f2SReid Spencer     case Instruction::And:
5494fd528f2SReid Spencer     case Instruction::Or:
5504fd528f2SReid Spencer     case Instruction::Xor: {
5514fd528f2SReid Spencer       GenericValue LHS = getConstantValue(Op0);
5524fd528f2SReid Spencer       GenericValue RHS = getConstantValue(CE->getOperand(1));
5534fd528f2SReid Spencer       GenericValue GV;
554c4e6bb5fSChris Lattner       switch (CE->getOperand(0)->getType()->getTypeID()) {
555c4e6bb5fSChris Lattner       default: assert(0 && "Bad add type!"); abort();
5567a9c62baSReid Spencer       case Type::IntegerTyID:
5574fd528f2SReid Spencer         switch (CE->getOpcode()) {
5584fd528f2SReid Spencer           default: assert(0 && "Invalid integer opcode");
5594fd528f2SReid Spencer           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
5604fd528f2SReid Spencer           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
5614fd528f2SReid Spencer           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
5624fd528f2SReid Spencer           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
5634fd528f2SReid Spencer           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
5644fd528f2SReid Spencer           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
5654fd528f2SReid Spencer           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
5664fd528f2SReid Spencer           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
5674fd528f2SReid Spencer           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
5684fd528f2SReid Spencer           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
5694fd528f2SReid Spencer         }
570c4e6bb5fSChris Lattner         break;
571c4e6bb5fSChris Lattner       case Type::FloatTyID:
5724fd528f2SReid Spencer         switch (CE->getOpcode()) {
5734fd528f2SReid Spencer           default: assert(0 && "Invalid float opcode"); abort();
5744fd528f2SReid Spencer           case Instruction::Add:
5754fd528f2SReid Spencer             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
5764fd528f2SReid Spencer           case Instruction::Sub:
5774fd528f2SReid Spencer             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
5784fd528f2SReid Spencer           case Instruction::Mul:
5794fd528f2SReid Spencer             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
5804fd528f2SReid Spencer           case Instruction::FDiv:
5814fd528f2SReid Spencer             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
5824fd528f2SReid Spencer           case Instruction::FRem:
5834fd528f2SReid Spencer             GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break;
5844fd528f2SReid Spencer         }
585c4e6bb5fSChris Lattner         break;
586c4e6bb5fSChris Lattner       case Type::DoubleTyID:
5874fd528f2SReid Spencer         switch (CE->getOpcode()) {
5884fd528f2SReid Spencer           default: assert(0 && "Invalid double opcode"); abort();
5894fd528f2SReid Spencer           case Instruction::Add:
5904fd528f2SReid Spencer             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
5914fd528f2SReid Spencer           case Instruction::Sub:
5924fd528f2SReid Spencer             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
5934fd528f2SReid Spencer           case Instruction::Mul:
5944fd528f2SReid Spencer             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
5954fd528f2SReid Spencer           case Instruction::FDiv:
5964fd528f2SReid Spencer             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
5974fd528f2SReid Spencer           case Instruction::FRem:
5984fd528f2SReid Spencer             GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
5994fd528f2SReid Spencer         }
600c4e6bb5fSChris Lattner         break;
601a1336cf5SDale Johannesen       case Type::X86_FP80TyID:
602a1336cf5SDale Johannesen       case Type::PPC_FP128TyID:
603a1336cf5SDale Johannesen       case Type::FP128TyID: {
604a1336cf5SDale Johannesen         APFloat apfLHS = APFloat(LHS.IntVal);
605a1336cf5SDale Johannesen         switch (CE->getOpcode()) {
606a1336cf5SDale Johannesen           default: assert(0 && "Invalid long double opcode"); abort();
607a1336cf5SDale Johannesen           case Instruction::Add:
608a1336cf5SDale Johannesen             apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
609a1336cf5SDale Johannesen             GV.IntVal = apfLHS.convertToAPInt();
610a1336cf5SDale Johannesen             break;
611a1336cf5SDale Johannesen           case Instruction::Sub:
612a1336cf5SDale Johannesen             apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
613a1336cf5SDale Johannesen             GV.IntVal = apfLHS.convertToAPInt();
614a1336cf5SDale Johannesen             break;
615a1336cf5SDale Johannesen           case Instruction::Mul:
616a1336cf5SDale Johannesen             apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
617a1336cf5SDale Johannesen             GV.IntVal = apfLHS.convertToAPInt();
618a1336cf5SDale Johannesen             break;
619a1336cf5SDale Johannesen           case Instruction::FDiv:
620a1336cf5SDale Johannesen             apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
621a1336cf5SDale Johannesen             GV.IntVal = apfLHS.convertToAPInt();
622a1336cf5SDale Johannesen             break;
623a1336cf5SDale Johannesen           case Instruction::FRem:
624a1336cf5SDale Johannesen             apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
625a1336cf5SDale Johannesen             GV.IntVal = apfLHS.convertToAPInt();
626a1336cf5SDale Johannesen             break;
627a1336cf5SDale Johannesen           }
628a1336cf5SDale Johannesen         }
629a1336cf5SDale Johannesen         break;
630c4e6bb5fSChris Lattner       }
6314fd528f2SReid Spencer       return GV;
6324fd528f2SReid Spencer     }
6339de0d14dSChris Lattner     default:
63468cbcc3eSChris Lattner       break;
63568cbcc3eSChris Lattner     }
6364fd528f2SReid Spencer     cerr << "ConstantExpr not handled: " << *CE << "\n";
6379de0d14dSChris Lattner     abort();
6389de0d14dSChris Lattner   }
639996fe010SChris Lattner 
6404fd528f2SReid Spencer   GenericValue Result;
6416b727599SChris Lattner   switch (C->getType()->getTypeID()) {
64287aa65f4SReid Spencer   case Type::FloatTyID:
643bed9dc42SDale Johannesen     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
6447a9c62baSReid Spencer     break;
64587aa65f4SReid Spencer   case Type::DoubleTyID:
646bed9dc42SDale Johannesen     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
64787aa65f4SReid Spencer     break;
648a1336cf5SDale Johannesen   case Type::X86_FP80TyID:
649a1336cf5SDale Johannesen   case Type::FP128TyID:
650a1336cf5SDale Johannesen   case Type::PPC_FP128TyID:
651a1336cf5SDale Johannesen     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().convertToAPInt();
652a1336cf5SDale Johannesen     break;
65387aa65f4SReid Spencer   case Type::IntegerTyID:
65487aa65f4SReid Spencer     Result.IntVal = cast<ConstantInt>(C)->getValue();
65587aa65f4SReid Spencer     break;
656996fe010SChris Lattner   case Type::PointerTyID:
6576a0fd73bSReid Spencer     if (isa<ConstantPointerNull>(C))
658996fe010SChris Lattner       Result.PointerVal = 0;
6596a0fd73bSReid Spencer     else if (const Function *F = dyn_cast<Function>(C))
6606a0fd73bSReid Spencer       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
6616a0fd73bSReid Spencer     else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
6626a0fd73bSReid Spencer       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
663e6492f10SChris Lattner     else
664996fe010SChris Lattner       assert(0 && "Unknown constant pointer type!");
665996fe010SChris Lattner     break;
666996fe010SChris Lattner   default:
6674fd528f2SReid Spencer     cerr << "ERROR: Constant unimplemented for type: " << *C->getType() << "\n";
6689de0d14dSChris Lattner     abort();
669996fe010SChris Lattner   }
670996fe010SChris Lattner   return Result;
671996fe010SChris Lattner }
672996fe010SChris Lattner 
6731202d1b1SDuncan Sands /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
6741202d1b1SDuncan Sands /// with the integer held in IntVal.
6751202d1b1SDuncan Sands static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
6761202d1b1SDuncan Sands                              unsigned StoreBytes) {
6771202d1b1SDuncan Sands   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
6781202d1b1SDuncan Sands   uint8_t *Src = (uint8_t *)IntVal.getRawData();
6795c65cb46SDuncan Sands 
680fde55674SDuncan Sands   if (sys::littleEndianHost())
6811202d1b1SDuncan Sands     // Little-endian host - the source is ordered from LSB to MSB.  Order the
6821202d1b1SDuncan Sands     // destination from LSB to MSB: Do a straight copy.
6835c65cb46SDuncan Sands     memcpy(Dst, Src, StoreBytes);
6845c65cb46SDuncan Sands   else {
6855c65cb46SDuncan Sands     // Big-endian host - the source is an array of 64 bit words ordered from
6861202d1b1SDuncan Sands     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
6871202d1b1SDuncan Sands     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
6885c65cb46SDuncan Sands     while (StoreBytes > sizeof(uint64_t)) {
6895c65cb46SDuncan Sands       StoreBytes -= sizeof(uint64_t);
6905c65cb46SDuncan Sands       // May not be aligned so use memcpy.
6915c65cb46SDuncan Sands       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
6925c65cb46SDuncan Sands       Src += sizeof(uint64_t);
6935c65cb46SDuncan Sands     }
6945c65cb46SDuncan Sands 
6955c65cb46SDuncan Sands     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
696815f8dd2SReid Spencer   }
6977a9c62baSReid Spencer }
6981202d1b1SDuncan Sands 
6991202d1b1SDuncan Sands /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.  Ptr
7001202d1b1SDuncan Sands /// is the address of the memory at which to store Val, cast to GenericValue *.
7011202d1b1SDuncan Sands /// It is not a pointer to a GenericValue containing the address at which to
7021202d1b1SDuncan Sands /// store Val.
7031202d1b1SDuncan Sands void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
7041202d1b1SDuncan Sands                                          const Type *Ty) {
7051202d1b1SDuncan Sands   const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
7061202d1b1SDuncan Sands 
7071202d1b1SDuncan Sands   switch (Ty->getTypeID()) {
7081202d1b1SDuncan Sands   case Type::IntegerTyID:
7091202d1b1SDuncan Sands     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
7101202d1b1SDuncan Sands     break;
711996fe010SChris Lattner   case Type::FloatTyID:
71287aa65f4SReid Spencer     *((float*)Ptr) = Val.FloatVal;
71387aa65f4SReid Spencer     break;
71487aa65f4SReid Spencer   case Type::DoubleTyID:
71587aa65f4SReid Spencer     *((double*)Ptr) = Val.DoubleVal;
716996fe010SChris Lattner     break;
717a1336cf5SDale Johannesen   case Type::X86_FP80TyID: {
718a1336cf5SDale Johannesen       uint16_t *Dest = (uint16_t*)Ptr;
719a1336cf5SDale Johannesen       const uint16_t *Src = (uint16_t*)Val.IntVal.getRawData();
720a1336cf5SDale Johannesen       // This is endian dependent, but it will only work on x86 anyway.
721a1336cf5SDale Johannesen       Dest[0] = Src[4];
722a1336cf5SDale Johannesen       Dest[1] = Src[0];
723a1336cf5SDale Johannesen       Dest[2] = Src[1];
724a1336cf5SDale Johannesen       Dest[3] = Src[2];
725a1336cf5SDale Johannesen       Dest[4] = Src[3];
726a1336cf5SDale Johannesen       break;
727a1336cf5SDale Johannesen     }
7287a9c62baSReid Spencer   case Type::PointerTyID:
7291202d1b1SDuncan Sands     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
7301202d1b1SDuncan Sands     if (StoreBytes != sizeof(PointerTy))
7311202d1b1SDuncan Sands       memset(Ptr, 0, StoreBytes);
7321202d1b1SDuncan Sands 
73387aa65f4SReid Spencer     *((PointerTy*)Ptr) = Val.PointerVal;
734996fe010SChris Lattner     break;
735996fe010SChris Lattner   default:
736f3baad3eSBill Wendling     cerr << "Cannot store value of type " << *Ty << "!\n";
737996fe010SChris Lattner   }
7381202d1b1SDuncan Sands 
7391202d1b1SDuncan Sands   if (sys::littleEndianHost() != getTargetData()->isLittleEndian())
7401202d1b1SDuncan Sands     // Host and target are different endian - reverse the stored bytes.
7411202d1b1SDuncan Sands     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
742996fe010SChris Lattner }
743996fe010SChris Lattner 
7441202d1b1SDuncan Sands /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
7451202d1b1SDuncan Sands /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
7461202d1b1SDuncan Sands static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
7471202d1b1SDuncan Sands   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
7481202d1b1SDuncan Sands   uint8_t *Dst = (uint8_t *)IntVal.getRawData();
7495c65cb46SDuncan Sands 
750fde55674SDuncan Sands   if (sys::littleEndianHost())
7515c65cb46SDuncan Sands     // Little-endian host - the destination must be ordered from LSB to MSB.
7525c65cb46SDuncan Sands     // The source is ordered from LSB to MSB: Do a straight copy.
7535c65cb46SDuncan Sands     memcpy(Dst, Src, LoadBytes);
7545c65cb46SDuncan Sands   else {
7555c65cb46SDuncan Sands     // Big-endian - the destination is an array of 64 bit words ordered from
7565c65cb46SDuncan Sands     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
7575c65cb46SDuncan Sands     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
7585c65cb46SDuncan Sands     // a word.
7595c65cb46SDuncan Sands     while (LoadBytes > sizeof(uint64_t)) {
7605c65cb46SDuncan Sands       LoadBytes -= sizeof(uint64_t);
7615c65cb46SDuncan Sands       // May not be aligned so use memcpy.
7625c65cb46SDuncan Sands       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
7635c65cb46SDuncan Sands       Dst += sizeof(uint64_t);
7645c65cb46SDuncan Sands     }
7655c65cb46SDuncan Sands 
7665c65cb46SDuncan Sands     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
7675c65cb46SDuncan Sands   }
7687a9c62baSReid Spencer }
7691202d1b1SDuncan Sands 
7701202d1b1SDuncan Sands /// FIXME: document
7711202d1b1SDuncan Sands ///
7721202d1b1SDuncan Sands void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
7731202d1b1SDuncan Sands                                           GenericValue *Ptr,
7741202d1b1SDuncan Sands                                           const Type *Ty) {
7751202d1b1SDuncan Sands   const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
7761202d1b1SDuncan Sands 
7771202d1b1SDuncan Sands   if (sys::littleEndianHost() != getTargetData()->isLittleEndian()) {
7781202d1b1SDuncan Sands     // Host and target are different endian - reverse copy the stored
7791202d1b1SDuncan Sands     // bytes into a buffer, and load from that.
7801202d1b1SDuncan Sands     uint8_t *Src = (uint8_t*)Ptr;
7811202d1b1SDuncan Sands     uint8_t *Buf = (uint8_t*)alloca(LoadBytes);
7821202d1b1SDuncan Sands     std::reverse_copy(Src, Src + LoadBytes, Buf);
7831202d1b1SDuncan Sands     Ptr = (GenericValue*)Buf;
7841202d1b1SDuncan Sands   }
7851202d1b1SDuncan Sands 
7861202d1b1SDuncan Sands   switch (Ty->getTypeID()) {
7871202d1b1SDuncan Sands   case Type::IntegerTyID:
7881202d1b1SDuncan Sands     // An APInt with all words initially zero.
7891202d1b1SDuncan Sands     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
7901202d1b1SDuncan Sands     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
7911202d1b1SDuncan Sands     break;
7927f389e8cSChris Lattner   case Type::FloatTyID:
79387aa65f4SReid Spencer     Result.FloatVal = *((float*)Ptr);
79487aa65f4SReid Spencer     break;
79587aa65f4SReid Spencer   case Type::DoubleTyID:
79687aa65f4SReid Spencer     Result.DoubleVal = *((double*)Ptr);
7977f389e8cSChris Lattner     break;
7987a9c62baSReid Spencer   case Type::PointerTyID:
79987aa65f4SReid Spencer     Result.PointerVal = *((PointerTy*)Ptr);
8007f389e8cSChris Lattner     break;
801a1336cf5SDale Johannesen   case Type::X86_FP80TyID: {
802a1336cf5SDale Johannesen     // This is endian dependent, but it will only work on x86 anyway.
80326d6539eSDuncan Sands     // FIXME: Will not trap if loading a signaling NaN.
804ff306287SDuncan Sands     uint16_t *p = (uint16_t*)Ptr;
805ff306287SDuncan Sands     union {
806ff306287SDuncan Sands       uint16_t x[8];
807ff306287SDuncan Sands       uint64_t y[2];
808ff306287SDuncan Sands     };
809a1336cf5SDale Johannesen     x[0] = p[1];
810a1336cf5SDale Johannesen     x[1] = p[2];
811a1336cf5SDale Johannesen     x[2] = p[3];
812a1336cf5SDale Johannesen     x[3] = p[4];
813a1336cf5SDale Johannesen     x[4] = p[0];
814ff306287SDuncan Sands     Result.IntVal = APInt(80, 2, y);
815a1336cf5SDale Johannesen     break;
816a1336cf5SDale Johannesen   }
8177f389e8cSChris Lattner   default:
818f3baad3eSBill Wendling     cerr << "Cannot load value of type " << *Ty << "!\n";
8197f389e8cSChris Lattner     abort();
8207f389e8cSChris Lattner   }
8217f389e8cSChris Lattner }
8227f389e8cSChris Lattner 
823996fe010SChris Lattner // InitializeMemory - Recursive function to apply a Constant value into the
824996fe010SChris Lattner // specified memory location...
825996fe010SChris Lattner //
826996fe010SChris Lattner void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
827b086d382SDale Johannesen   DOUT << "Initializing " << Addr;
828b086d382SDale Johannesen   DEBUG(Init->dump());
82961753bf8SChris Lattner   if (isa<UndefValue>(Init)) {
83061753bf8SChris Lattner     return;
831d84d35baSReid Spencer   } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
83269d62138SRobert Bocchino     unsigned ElementSize =
83344b8721dSDuncan Sands       getTargetData()->getABITypeSize(CP->getType()->getElementType());
83469d62138SRobert Bocchino     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
83569d62138SRobert Bocchino       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
83669d62138SRobert Bocchino     return;
8371dd86b11SChris Lattner   } else if (isa<ConstantAggregateZero>(Init)) {
8381dd86b11SChris Lattner     memset(Addr, 0, (size_t)getTargetData()->getABITypeSize(Init->getType()));
8391dd86b11SChris Lattner     return;
84069ddfbfeSDan Gohman   } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
84169ddfbfeSDan Gohman     unsigned ElementSize =
84269ddfbfeSDan Gohman       getTargetData()->getABITypeSize(CPA->getType()->getElementType());
84369ddfbfeSDan Gohman     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
84469ddfbfeSDan Gohman       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
84569ddfbfeSDan Gohman     return;
84669ddfbfeSDan Gohman   } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
84769ddfbfeSDan Gohman     const StructLayout *SL =
84869ddfbfeSDan Gohman       getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
84969ddfbfeSDan Gohman     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
85069ddfbfeSDan Gohman       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
85169ddfbfeSDan Gohman     return;
85261753bf8SChris Lattner   } else if (Init->getType()->isFirstClassType()) {
853996fe010SChris Lattner     GenericValue Val = getConstantValue(Init);
854996fe010SChris Lattner     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
855996fe010SChris Lattner     return;
856996fe010SChris Lattner   }
857996fe010SChris Lattner 
858f3baad3eSBill Wendling   cerr << "Bad Type: " << *Init->getType() << "\n";
859996fe010SChris Lattner   assert(0 && "Unknown constant type to initialize memory with!");
860996fe010SChris Lattner }
861996fe010SChris Lattner 
862996fe010SChris Lattner /// EmitGlobals - Emit all of the global variables to memory, storing their
863996fe010SChris Lattner /// addresses into GlobalAddress.  This must make sure to copy the contents of
864996fe010SChris Lattner /// their initializers into the memory.
865996fe010SChris Lattner ///
866996fe010SChris Lattner void ExecutionEngine::emitGlobals() {
86720a631fdSOwen Anderson   const TargetData *TD = getTargetData();
868996fe010SChris Lattner 
869996fe010SChris Lattner   // Loop over all of the global variables in the program, allocating the memory
8700621caefSChris Lattner   // to hold them.  If there is more than one module, do a prepass over globals
8710621caefSChris Lattner   // to figure out how the different modules should link together.
8720621caefSChris Lattner   //
8730621caefSChris Lattner   std::map<std::pair<std::string, const Type*>,
8740621caefSChris Lattner            const GlobalValue*> LinkedGlobalsMap;
8750621caefSChris Lattner 
8760621caefSChris Lattner   if (Modules.size() != 1) {
8770621caefSChris Lattner     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
8780621caefSChris Lattner       Module &M = *Modules[m]->getModule();
8790621caefSChris Lattner       for (Module::const_global_iterator I = M.global_begin(),
8800621caefSChris Lattner            E = M.global_end(); I != E; ++I) {
8810621caefSChris Lattner         const GlobalValue *GV = I;
8825301e7c6SReid Spencer         if (GV->hasInternalLinkage() || GV->isDeclaration() ||
8830621caefSChris Lattner             GV->hasAppendingLinkage() || !GV->hasName())
8840621caefSChris Lattner           continue;// Ignore external globals and globals with internal linkage.
8850621caefSChris Lattner 
8860621caefSChris Lattner         const GlobalValue *&GVEntry =
8870621caefSChris Lattner           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
8880621caefSChris Lattner 
8890621caefSChris Lattner         // If this is the first time we've seen this global, it is the canonical
8900621caefSChris Lattner         // version.
8910621caefSChris Lattner         if (!GVEntry) {
8920621caefSChris Lattner           GVEntry = GV;
8930621caefSChris Lattner           continue;
8940621caefSChris Lattner         }
8950621caefSChris Lattner 
8960621caefSChris Lattner         // If the existing global is strong, never replace it.
897d61d39ecSAnton Korobeynikov         if (GVEntry->hasExternalLinkage() ||
898d61d39ecSAnton Korobeynikov             GVEntry->hasDLLImportLinkage() ||
899d61d39ecSAnton Korobeynikov             GVEntry->hasDLLExportLinkage())
9000621caefSChris Lattner           continue;
9010621caefSChris Lattner 
9020621caefSChris Lattner         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
903ce4396bcSDale Johannesen         // symbol.  FIXME is this right for common?
90412c94949SAnton Korobeynikov         if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
9050621caefSChris Lattner           GVEntry = GV;
9060621caefSChris Lattner       }
9070621caefSChris Lattner     }
9080621caefSChris Lattner   }
9090621caefSChris Lattner 
9100621caefSChris Lattner   std::vector<const GlobalValue*> NonCanonicalGlobals;
9110621caefSChris Lattner   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
9120621caefSChris Lattner     Module &M = *Modules[m]->getModule();
9138ffb6611SChris Lattner     for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
9140621caefSChris Lattner          I != E; ++I) {
9150621caefSChris Lattner       // In the multi-module case, see what this global maps to.
9160621caefSChris Lattner       if (!LinkedGlobalsMap.empty()) {
9170621caefSChris Lattner         if (const GlobalValue *GVEntry =
9180621caefSChris Lattner               LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
9190621caefSChris Lattner           // If something else is the canonical global, ignore this one.
9200621caefSChris Lattner           if (GVEntry != &*I) {
9210621caefSChris Lattner             NonCanonicalGlobals.push_back(I);
9220621caefSChris Lattner             continue;
9230621caefSChris Lattner           }
9240621caefSChris Lattner         }
9250621caefSChris Lattner       }
9260621caefSChris Lattner 
9275301e7c6SReid Spencer       if (!I->isDeclaration()) {
9280621caefSChris Lattner         // Get the type of the global.
929996fe010SChris Lattner         const Type *Ty = I->getType()->getElementType();
930996fe010SChris Lattner 
931996fe010SChris Lattner         // Allocate some memory for it!
93244b8721dSDuncan Sands         unsigned Size = TD->getABITypeSize(Ty);
9336bbe3eceSChris Lattner         addGlobalMapping(I, new char[Size]);
934996fe010SChris Lattner       } else {
935e8bbcfc2SBrian Gaeke         // External variable reference. Try to use the dynamic loader to
936e8bbcfc2SBrian Gaeke         // get a pointer to it.
9370621caefSChris Lattner         if (void *SymAddr =
9380621caefSChris Lattner             sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
939748e8579SChris Lattner           addGlobalMapping(I, SymAddr);
9409de0d14dSChris Lattner         else {
941f3baad3eSBill Wendling           cerr << "Could not resolve external global address: "
9429de0d14dSChris Lattner                << I->getName() << "\n";
9439de0d14dSChris Lattner           abort();
9449de0d14dSChris Lattner         }
945996fe010SChris Lattner       }
9460621caefSChris Lattner     }
9470621caefSChris Lattner 
9480621caefSChris Lattner     // If there are multiple modules, map the non-canonical globals to their
9490621caefSChris Lattner     // canonical location.
9500621caefSChris Lattner     if (!NonCanonicalGlobals.empty()) {
9510621caefSChris Lattner       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
9520621caefSChris Lattner         const GlobalValue *GV = NonCanonicalGlobals[i];
9530621caefSChris Lattner         const GlobalValue *CGV =
9540621caefSChris Lattner           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
9550621caefSChris Lattner         void *Ptr = getPointerToGlobalIfAvailable(CGV);
9560621caefSChris Lattner         assert(Ptr && "Canonical global wasn't codegen'd!");
9570621caefSChris Lattner         addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
9580621caefSChris Lattner       }
9590621caefSChris Lattner     }
960996fe010SChris Lattner 
9617a9c62baSReid Spencer     // Now that all of the globals are set up in memory, loop through them all
9627a9c62baSReid Spencer     // and initialize their contents.
9638ffb6611SChris Lattner     for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
9640621caefSChris Lattner          I != E; ++I) {
9655301e7c6SReid Spencer       if (!I->isDeclaration()) {
9660621caefSChris Lattner         if (!LinkedGlobalsMap.empty()) {
9670621caefSChris Lattner           if (const GlobalValue *GVEntry =
9680621caefSChris Lattner                 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
9690621caefSChris Lattner             if (GVEntry != &*I)  // Not the canonical variable.
9700621caefSChris Lattner               continue;
9710621caefSChris Lattner         }
9726bbe3eceSChris Lattner         EmitGlobalVariable(I);
9736bbe3eceSChris Lattner       }
9740621caefSChris Lattner     }
9750621caefSChris Lattner   }
9760621caefSChris Lattner }
9776bbe3eceSChris Lattner 
9786bbe3eceSChris Lattner // EmitGlobalVariable - This method emits the specified global variable to the
9796bbe3eceSChris Lattner // address specified in GlobalAddresses, or allocates new memory if it's not
9806bbe3eceSChris Lattner // already in the map.
981fbcc0aa1SChris Lattner void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
982748e8579SChris Lattner   void *GA = getPointerToGlobalIfAvailable(GV);
9835834fdb3SBill Wendling   DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
984dc631735SChris Lattner 
985fbcc0aa1SChris Lattner   const Type *ElTy = GV->getType()->getElementType();
98644b8721dSDuncan Sands   size_t GVSize = (size_t)getTargetData()->getABITypeSize(ElTy);
9876bbe3eceSChris Lattner   if (GA == 0) {
9886bbe3eceSChris Lattner     // If it's not already specified, allocate memory for the global.
989d215992bSChris Lattner     GA = new char[GVSize];
990748e8579SChris Lattner     addGlobalMapping(GV, GA);
9916bbe3eceSChris Lattner   }
992fbcc0aa1SChris Lattner 
9936bbe3eceSChris Lattner   InitializeMemory(GV->getInitializer(), GA);
994df1f1524SChris Lattner   NumInitBytes += (unsigned)GVSize;
9956bbe3eceSChris Lattner   ++NumGlobals;
996996fe010SChris Lattner }
997