LLVM API Documentation
00001 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the common interface used by the various execution engine 00011 // subclasses. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "jit" 00016 #include "llvm/Constants.h" 00017 #include "llvm/DerivedTypes.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/ModuleProvider.h" 00020 #include "llvm/ADT/Statistic.h" 00021 #include "llvm/ExecutionEngine/ExecutionEngine.h" 00022 #include "llvm/ExecutionEngine/GenericValue.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/System/DynamicLibrary.h" 00025 #include "llvm/Target/TargetData.h" 00026 #include <iostream> 00027 using namespace llvm; 00028 00029 namespace { 00030 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); 00031 Statistic<> NumGlobals ("lli", "Number of global vars initialized"); 00032 } 00033 00034 ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; 00035 ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; 00036 00037 ExecutionEngine::ExecutionEngine(ModuleProvider *P) : 00038 CurMod(*P->getModule()), MP(P) { 00039 assert(P && "ModuleProvider is null?"); 00040 } 00041 00042 ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) { 00043 assert(M && "Module is null?"); 00044 } 00045 00046 ExecutionEngine::~ExecutionEngine() { 00047 delete MP; 00048 } 00049 00050 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 00051 /// at the specified address. 00052 /// 00053 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 00054 MutexGuard locked(lock); 00055 00056 // If we haven't computed the reverse mapping yet, do so first. 00057 if (state.getGlobalAddressReverseMap(locked).empty()) { 00058 for (std::map<const GlobalValue*, void *>::iterator I = 00059 state.getGlobalAddressMap(locked).begin(), E = state.getGlobalAddressMap(locked).end(); I != E; ++I) 00060 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, I->first)); 00061 } 00062 00063 std::map<void *, const GlobalValue*>::iterator I = 00064 state.getGlobalAddressReverseMap(locked).find(Addr); 00065 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 00066 } 00067 00068 // CreateArgv - Turn a vector of strings into a nice argv style array of 00069 // pointers to null terminated strings. 00070 // 00071 static void *CreateArgv(ExecutionEngine *EE, 00072 const std::vector<std::string> &InputArgv) { 00073 unsigned PtrSize = EE->getTargetData().getPointerSize(); 00074 char *Result = new char[(InputArgv.size()+1)*PtrSize]; 00075 00076 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 00077 const Type *SBytePtr = PointerType::get(Type::SByteTy); 00078 00079 for (unsigned i = 0; i != InputArgv.size(); ++i) { 00080 unsigned Size = InputArgv[i].size()+1; 00081 char *Dest = new char[Size]; 00082 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 00083 00084 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 00085 Dest[Size-1] = 0; 00086 00087 // Endian safe: Result[i] = (PointerTy)Dest; 00088 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), 00089 SBytePtr); 00090 } 00091 00092 // Null terminate it 00093 EE->StoreValueToMemory(PTOGV(0), 00094 (GenericValue*)(Result+InputArgv.size()*PtrSize), 00095 SBytePtr); 00096 return Result; 00097 } 00098 00099 00100 /// runStaticConstructorsDestructors - This method is used to execute all of 00101 /// the static constructors or destructors for a module, depending on the 00102 /// value of isDtors. 00103 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 00104 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 00105 GlobalVariable *GV = CurMod.getNamedGlobal(Name); 00106 if (!GV || GV->isExternal() || !GV->hasInternalLinkage()) return; 00107 00108 // Should be an array of '{ int, void ()* }' structs. The first value is the 00109 // init priority, which we ignore. 00110 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 00111 if (!InitList) return; 00112 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 00113 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 00114 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 00115 00116 Constant *FP = CS->getOperand(1); 00117 if (FP->isNullValue()) 00118 return; // Found a null terminator, exit. 00119 00120 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 00121 if (CE->getOpcode() == Instruction::Cast) 00122 FP = CE->getOperand(0); 00123 if (Function *F = dyn_cast<Function>(FP)) { 00124 // Execute the ctor/dtor function! 00125 runFunction(F, std::vector<GenericValue>()); 00126 } 00127 } 00128 } 00129 00130 /// runFunctionAsMain - This is a helper function which wraps runFunction to 00131 /// handle the common task of starting up main with the specified argc, argv, 00132 /// and envp parameters. 00133 int ExecutionEngine::runFunctionAsMain(Function *Fn, 00134 const std::vector<std::string> &argv, 00135 const char * const * envp) { 00136 std::vector<GenericValue> GVArgs; 00137 GenericValue GVArgc; 00138 GVArgc.IntVal = argv.size(); 00139 unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 00140 if (NumArgs) { 00141 GVArgs.push_back(GVArgc); // Arg #0 = argc. 00142 if (NumArgs > 1) { 00143 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. 00144 assert(((char **)GVTOP(GVArgs[1]))[0] && 00145 "argv[0] was null after CreateArgv"); 00146 if (NumArgs > 2) { 00147 std::vector<std::string> EnvVars; 00148 for (unsigned i = 0; envp[i]; ++i) 00149 EnvVars.push_back(envp[i]); 00150 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. 00151 } 00152 } 00153 } 00154 return runFunction(Fn, GVArgs).IntVal; 00155 } 00156 00157 /// If possible, create a JIT, unless the caller specifically requests an 00158 /// Interpreter or there's an error. If even an Interpreter cannot be created, 00159 /// NULL is returned. 00160 /// 00161 ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, 00162 bool ForceInterpreter) { 00163 ExecutionEngine *EE = 0; 00164 00165 // Unless the interpreter was explicitly selected, try making a JIT. 00166 if (!ForceInterpreter && JITCtor) 00167 EE = JITCtor(MP); 00168 00169 // If we can't make a JIT, make an interpreter instead. 00170 if (EE == 0 && InterpCtor) 00171 EE = InterpCtor(MP); 00172 00173 if (EE) { 00174 // Make sure we can resolve symbols in the program as well. The zero arg 00175 // to the function tells DynamicLibrary to load the program, not a library. 00176 sys::DynamicLibrary::LoadLibraryPermanently(0); 00177 } 00178 00179 return EE; 00180 } 00181 00182 /// getPointerToGlobal - This returns the address of the specified global 00183 /// value. This may involve code generation if it's a function. 00184 /// 00185 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 00186 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 00187 return getPointerToFunction(F); 00188 00189 MutexGuard locked(lock); 00190 void *p = state.getGlobalAddressMap(locked)[GV]; 00191 if (p) 00192 return p; 00193 00194 // Global variable might have been added since interpreter started. 00195 if (GlobalVariable *GVar = 00196 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 00197 EmitGlobalVariable(GVar); 00198 else 00199 assert("Global hasn't had an address allocated yet!"); 00200 return state.getGlobalAddressMap(locked)[GV]; 00201 } 00202 00203 /// FIXME: document 00204 /// 00205 GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 00206 GenericValue Result; 00207 if (isa<UndefValue>(C)) return Result; 00208 00209 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) { 00210 switch (CE->getOpcode()) { 00211 case Instruction::GetElementPtr: { 00212 Result = getConstantValue(CE->getOperand(0)); 00213 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); 00214 uint64_t Offset = 00215 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); 00216 00217 if (getTargetData().getPointerSize() == 4) 00218 Result.IntVal += Offset; 00219 else 00220 Result.LongVal += Offset; 00221 return Result; 00222 } 00223 case Instruction::Cast: { 00224 // We only need to handle a few cases here. Almost all casts will 00225 // automatically fold, just the ones involving pointers won't. 00226 // 00227 Constant *Op = CE->getOperand(0); 00228 GenericValue GV = getConstantValue(Op); 00229 00230 // Handle cast of pointer to pointer... 00231 if (Op->getType()->getTypeID() == C->getType()->getTypeID()) 00232 return GV; 00233 00234 // Handle a cast of pointer to any integral type... 00235 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral()) 00236 return GV; 00237 00238 // Handle cast of integer to a pointer... 00239 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral()) 00240 switch (Op->getType()->getTypeID()) { 00241 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal); 00242 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal); 00243 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal); 00244 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal); 00245 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal); 00246 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal); 00247 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal); 00248 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal); 00249 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal); 00250 default: assert(0 && "Unknown integral type!"); 00251 } 00252 break; 00253 } 00254 00255 case Instruction::Add: 00256 switch (CE->getOperand(0)->getType()->getTypeID()) { 00257 default: assert(0 && "Bad add type!"); abort(); 00258 case Type::LongTyID: 00259 case Type::ULongTyID: 00260 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal + 00261 getConstantValue(CE->getOperand(1)).LongVal; 00262 break; 00263 case Type::IntTyID: 00264 case Type::UIntTyID: 00265 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal + 00266 getConstantValue(CE->getOperand(1)).IntVal; 00267 break; 00268 case Type::ShortTyID: 00269 case Type::UShortTyID: 00270 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal + 00271 getConstantValue(CE->getOperand(1)).ShortVal; 00272 break; 00273 case Type::SByteTyID: 00274 case Type::UByteTyID: 00275 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal + 00276 getConstantValue(CE->getOperand(1)).SByteVal; 00277 break; 00278 case Type::FloatTyID: 00279 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + 00280 getConstantValue(CE->getOperand(1)).FloatVal; 00281 break; 00282 case Type::DoubleTyID: 00283 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + 00284 getConstantValue(CE->getOperand(1)).DoubleVal; 00285 break; 00286 } 00287 return Result; 00288 default: 00289 break; 00290 } 00291 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; 00292 abort(); 00293 } 00294 00295 switch (C->getType()->getTypeID()) { 00296 #define GET_CONST_VAL(TY, CTY, CLASS) \ 00297 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break 00298 GET_CONST_VAL(Bool , bool , ConstantBool); 00299 GET_CONST_VAL(UByte , unsigned char , ConstantUInt); 00300 GET_CONST_VAL(SByte , signed char , ConstantSInt); 00301 GET_CONST_VAL(UShort , unsigned short, ConstantUInt); 00302 GET_CONST_VAL(Short , signed short , ConstantSInt); 00303 GET_CONST_VAL(UInt , unsigned int , ConstantUInt); 00304 GET_CONST_VAL(Int , signed int , ConstantSInt); 00305 GET_CONST_VAL(ULong , uint64_t , ConstantUInt); 00306 GET_CONST_VAL(Long , int64_t , ConstantSInt); 00307 GET_CONST_VAL(Float , float , ConstantFP); 00308 GET_CONST_VAL(Double , double , ConstantFP); 00309 #undef GET_CONST_VAL 00310 case Type::PointerTyID: 00311 if (isa<ConstantPointerNull>(C)) 00312 Result.PointerVal = 0; 00313 else if (const Function *F = dyn_cast<Function>(C)) 00314 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 00315 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) 00316 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 00317 else 00318 assert(0 && "Unknown constant pointer type!"); 00319 break; 00320 default: 00321 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; 00322 abort(); 00323 } 00324 return Result; 00325 } 00326 00327 /// FIXME: document 00328 /// 00329 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, 00330 const Type *Ty) { 00331 if (getTargetData().isLittleEndian()) { 00332 switch (Ty->getTypeID()) { 00333 case Type::BoolTyID: 00334 case Type::UByteTyID: 00335 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 00336 case Type::UShortTyID: 00337 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; 00338 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; 00339 break; 00340 Store4BytesLittleEndian: 00341 case Type::FloatTyID: 00342 case Type::UIntTyID: 00343 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; 00344 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; 00345 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; 00346 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; 00347 break; 00348 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 00349 goto Store4BytesLittleEndian; 00350 case Type::DoubleTyID: 00351 case Type::ULongTyID: 00352 case Type::LongTyID: 00353 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal ); 00354 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8); 00355 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16); 00356 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24); 00357 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32); 00358 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40); 00359 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48); 00360 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56); 00361 break; 00362 default: 00363 std::cout << "Cannot store value of type " << *Ty << "!\n"; 00364 } 00365 } else { 00366 switch (Ty->getTypeID()) { 00367 case Type::BoolTyID: 00368 case Type::UByteTyID: 00369 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 00370 case Type::UShortTyID: 00371 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; 00372 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; 00373 break; 00374 Store4BytesBigEndian: 00375 case Type::FloatTyID: 00376 case Type::UIntTyID: 00377 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; 00378 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; 00379 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; 00380 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; 00381 break; 00382 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 00383 goto Store4BytesBigEndian; 00384 case Type::DoubleTyID: 00385 case Type::ULongTyID: 00386 case Type::LongTyID: 00387 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal ); 00388 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8); 00389 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16); 00390 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24); 00391 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32); 00392 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40); 00393 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48); 00394 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56); 00395 break; 00396 default: 00397 std::cout << "Cannot store value of type " << *Ty << "!\n"; 00398 } 00399 } 00400 } 00401 00402 /// FIXME: document 00403 /// 00404 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, 00405 const Type *Ty) { 00406 GenericValue Result; 00407 if (getTargetData().isLittleEndian()) { 00408 switch (Ty->getTypeID()) { 00409 case Type::BoolTyID: 00410 case Type::UByteTyID: 00411 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 00412 case Type::UShortTyID: 00413 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | 00414 ((unsigned)Ptr->Untyped[1] << 8); 00415 break; 00416 Load4BytesLittleEndian: 00417 case Type::FloatTyID: 00418 case Type::UIntTyID: 00419 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | 00420 ((unsigned)Ptr->Untyped[1] << 8) | 00421 ((unsigned)Ptr->Untyped[2] << 16) | 00422 ((unsigned)Ptr->Untyped[3] << 24); 00423 break; 00424 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 00425 goto Load4BytesLittleEndian; 00426 case Type::DoubleTyID: 00427 case Type::ULongTyID: 00428 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | 00429 ((uint64_t)Ptr->Untyped[1] << 8) | 00430 ((uint64_t)Ptr->Untyped[2] << 16) | 00431 ((uint64_t)Ptr->Untyped[3] << 24) | 00432 ((uint64_t)Ptr->Untyped[4] << 32) | 00433 ((uint64_t)Ptr->Untyped[5] << 40) | 00434 ((uint64_t)Ptr->Untyped[6] << 48) | 00435 ((uint64_t)Ptr->Untyped[7] << 56); 00436 break; 00437 default: 00438 std::cout << "Cannot load value of type " << *Ty << "!\n"; 00439 abort(); 00440 } 00441 } else { 00442 switch (Ty->getTypeID()) { 00443 case Type::BoolTyID: 00444 case Type::UByteTyID: 00445 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 00446 case Type::UShortTyID: 00447 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | 00448 ((unsigned)Ptr->Untyped[0] << 8); 00449 break; 00450 Load4BytesBigEndian: 00451 case Type::FloatTyID: 00452 case Type::UIntTyID: 00453 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | 00454 ((unsigned)Ptr->Untyped[2] << 8) | 00455 ((unsigned)Ptr->Untyped[1] << 16) | 00456 ((unsigned)Ptr->Untyped[0] << 24); 00457 break; 00458 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 00459 goto Load4BytesBigEndian; 00460 case Type::DoubleTyID: 00461 case Type::ULongTyID: 00462 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | 00463 ((uint64_t)Ptr->Untyped[6] << 8) | 00464 ((uint64_t)Ptr->Untyped[5] << 16) | 00465 ((uint64_t)Ptr->Untyped[4] << 24) | 00466 ((uint64_t)Ptr->Untyped[3] << 32) | 00467 ((uint64_t)Ptr->Untyped[2] << 40) | 00468 ((uint64_t)Ptr->Untyped[1] << 48) | 00469 ((uint64_t)Ptr->Untyped[0] << 56); 00470 break; 00471 default: 00472 std::cout << "Cannot load value of type " << *Ty << "!\n"; 00473 abort(); 00474 } 00475 } 00476 return Result; 00477 } 00478 00479 // InitializeMemory - Recursive function to apply a Constant value into the 00480 // specified memory location... 00481 // 00482 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 00483 if (isa<UndefValue>(Init)) { 00484 return; 00485 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) { 00486 unsigned ElementSize = 00487 getTargetData().getTypeSize(CP->getType()->getElementType()); 00488 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 00489 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 00490 return; 00491 } else if (Init->getType()->isFirstClassType()) { 00492 GenericValue Val = getConstantValue(Init); 00493 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 00494 return; 00495 } else if (isa<ConstantAggregateZero>(Init)) { 00496 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType())); 00497 return; 00498 } 00499 00500 switch (Init->getType()->getTypeID()) { 00501 case Type::ArrayTyID: { 00502 const ConstantArray *CPA = cast<ConstantArray>(Init); 00503 unsigned ElementSize = 00504 getTargetData().getTypeSize(CPA->getType()->getElementType()); 00505 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 00506 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 00507 return; 00508 } 00509 00510 case Type::StructTyID: { 00511 const ConstantStruct *CPS = cast<ConstantStruct>(Init); 00512 const StructLayout *SL = 00513 getTargetData().getStructLayout(cast<StructType>(CPS->getType())); 00514 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 00515 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]); 00516 return; 00517 } 00518 00519 default: 00520 std::cerr << "Bad Type: " << *Init->getType() << "\n"; 00521 assert(0 && "Unknown constant type to initialize memory with!"); 00522 } 00523 } 00524 00525 /// EmitGlobals - Emit all of the global variables to memory, storing their 00526 /// addresses into GlobalAddress. This must make sure to copy the contents of 00527 /// their initializers into the memory. 00528 /// 00529 void ExecutionEngine::emitGlobals() { 00530 const TargetData &TD = getTargetData(); 00531 00532 // Loop over all of the global variables in the program, allocating the memory 00533 // to hold them. 00534 Module &M = getModule(); 00535 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00536 I != E; ++I) 00537 if (!I->isExternal()) { 00538 // Get the type of the global... 00539 const Type *Ty = I->getType()->getElementType(); 00540 00541 // Allocate some memory for it! 00542 unsigned Size = TD.getTypeSize(Ty); 00543 addGlobalMapping(I, new char[Size]); 00544 } else { 00545 // External variable reference. Try to use the dynamic loader to 00546 // get a pointer to it. 00547 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol( 00548 I->getName().c_str())) 00549 addGlobalMapping(I, SymAddr); 00550 else { 00551 std::cerr << "Could not resolve external global address: " 00552 << I->getName() << "\n"; 00553 abort(); 00554 } 00555 } 00556 00557 // Now that all of the globals are set up in memory, loop through them all and 00558 // initialize their contents. 00559 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00560 I != E; ++I) 00561 if (!I->isExternal()) 00562 EmitGlobalVariable(I); 00563 } 00564 00565 // EmitGlobalVariable - This method emits the specified global variable to the 00566 // address specified in GlobalAddresses, or allocates new memory if it's not 00567 // already in the map. 00568 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 00569 void *GA = getPointerToGlobalIfAvailable(GV); 00570 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n"); 00571 00572 const Type *ElTy = GV->getType()->getElementType(); 00573 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy); 00574 if (GA == 0) { 00575 // If it's not already specified, allocate memory for the global. 00576 GA = new char[GVSize]; 00577 addGlobalMapping(GV, GA); 00578 } 00579 00580 InitializeMemory(GV->getInitializer(), GA); 00581 NumInitBytes += (unsigned)GVSize; 00582 ++NumGlobals; 00583 }