1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the common interface used by the various execution engine 11 // subclasses. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #define DEBUG_TYPE "jit" 16 #include "llvm/Constants.h" 17 #include "llvm/DerivedTypes.h" 18 #include "llvm/Module.h" 19 #include "llvm/ModuleProvider.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/ExecutionEngine/ExecutionEngine.h" 22 #include "llvm/ExecutionEngine/GenericValue.h" 23 #include "llvm/Support/Debug.h" 24 #include "llvm/System/DynamicLibrary.h" 25 #include "llvm/Target/TargetData.h" 26 #include <iostream> 27 using namespace llvm; 28 29 namespace { 30 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); 31 Statistic<> NumGlobals ("lli", "Number of global vars initialized"); 32 } 33 34 ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; 35 ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; 36 37 ExecutionEngine::ExecutionEngine(ModuleProvider *P) : 38 CurMod(*P->getModule()), MP(P) { 39 assert(P && "ModuleProvider is null?"); 40 } 41 42 ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) { 43 assert(M && "Module is null?"); 44 } 45 46 ExecutionEngine::~ExecutionEngine() { 47 delete MP; 48 } 49 50 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 51 /// at the specified address. 52 /// 53 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { 54 MutexGuard locked(lock); 55 56 // If we haven't computed the reverse mapping yet, do so first. 57 if (state.getGlobalAddressReverseMap(locked).empty()) { 58 for (std::map<const GlobalValue*, void *>::iterator I = 59 state.getGlobalAddressMap(locked).begin(), E = state.getGlobalAddressMap(locked).end(); I != E; ++I) 60 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, I->first)); 61 } 62 63 std::map<void *, const GlobalValue*>::iterator I = 64 state.getGlobalAddressReverseMap(locked).find(Addr); 65 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; 66 } 67 68 // CreateArgv - Turn a vector of strings into a nice argv style array of 69 // pointers to null terminated strings. 70 // 71 static void *CreateArgv(ExecutionEngine *EE, 72 const std::vector<std::string> &InputArgv) { 73 unsigned PtrSize = EE->getTargetData().getPointerSize(); 74 char *Result = new char[(InputArgv.size()+1)*PtrSize]; 75 76 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 77 const Type *SBytePtr = PointerType::get(Type::SByteTy); 78 79 for (unsigned i = 0; i != InputArgv.size(); ++i) { 80 unsigned Size = InputArgv[i].size()+1; 81 char *Dest = new char[Size]; 82 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 83 84 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 85 Dest[Size-1] = 0; 86 87 // Endian safe: Result[i] = (PointerTy)Dest; 88 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), 89 SBytePtr); 90 } 91 92 // Null terminate it 93 EE->StoreValueToMemory(PTOGV(0), 94 (GenericValue*)(Result+InputArgv.size()*PtrSize), 95 SBytePtr); 96 return Result; 97 } 98 99 100 /// runStaticConstructorsDestructors - This method is used to execute all of 101 /// the static constructors or destructors for a module, depending on the 102 /// value of isDtors. 103 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { 104 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; 105 GlobalVariable *GV = CurMod.getNamedGlobal(Name); 106 107 // If this global has internal linkage, or if it has a use, then it must be 108 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If 109 // this is the case, don't execute any of the global ctors, __main will do it. 110 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) return; 111 112 // Should be an array of '{ int, void ()* }' structs. The first value is the 113 // init priority, which we ignore. 114 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 115 if (!InitList) return; 116 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 117 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 118 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 119 120 Constant *FP = CS->getOperand(1); 121 if (FP->isNullValue()) 122 return; // Found a null terminator, exit. 123 124 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 125 if (CE->getOpcode() == Instruction::Cast) 126 FP = CE->getOperand(0); 127 if (Function *F = dyn_cast<Function>(FP)) { 128 // Execute the ctor/dtor function! 129 runFunction(F, std::vector<GenericValue>()); 130 } 131 } 132 } 133 134 /// runFunctionAsMain - This is a helper function which wraps runFunction to 135 /// handle the common task of starting up main with the specified argc, argv, 136 /// and envp parameters. 137 int ExecutionEngine::runFunctionAsMain(Function *Fn, 138 const std::vector<std::string> &argv, 139 const char * const * envp) { 140 std::vector<GenericValue> GVArgs; 141 GenericValue GVArgc; 142 GVArgc.IntVal = argv.size(); 143 unsigned NumArgs = Fn->getFunctionType()->getNumParams(); 144 if (NumArgs) { 145 GVArgs.push_back(GVArgc); // Arg #0 = argc. 146 if (NumArgs > 1) { 147 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. 148 assert(((char **)GVTOP(GVArgs[1]))[0] && 149 "argv[0] was null after CreateArgv"); 150 if (NumArgs > 2) { 151 std::vector<std::string> EnvVars; 152 for (unsigned i = 0; envp[i]; ++i) 153 EnvVars.push_back(envp[i]); 154 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. 155 } 156 } 157 } 158 return runFunction(Fn, GVArgs).IntVal; 159 } 160 161 /// If possible, create a JIT, unless the caller specifically requests an 162 /// Interpreter or there's an error. If even an Interpreter cannot be created, 163 /// NULL is returned. 164 /// 165 ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, 166 bool ForceInterpreter) { 167 ExecutionEngine *EE = 0; 168 169 // Unless the interpreter was explicitly selected, try making a JIT. 170 if (!ForceInterpreter && JITCtor) 171 EE = JITCtor(MP); 172 173 // If we can't make a JIT, make an interpreter instead. 174 if (EE == 0 && InterpCtor) 175 EE = InterpCtor(MP); 176 177 if (EE) { 178 // Make sure we can resolve symbols in the program as well. The zero arg 179 // to the function tells DynamicLibrary to load the program, not a library. 180 sys::DynamicLibrary::LoadLibraryPermanently(0); 181 } 182 183 return EE; 184 } 185 186 /// getPointerToGlobal - This returns the address of the specified global 187 /// value. This may involve code generation if it's a function. 188 /// 189 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 190 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 191 return getPointerToFunction(F); 192 193 MutexGuard locked(lock); 194 void *p = state.getGlobalAddressMap(locked)[GV]; 195 if (p) 196 return p; 197 198 // Global variable might have been added since interpreter started. 199 if (GlobalVariable *GVar = 200 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) 201 EmitGlobalVariable(GVar); 202 else 203 assert("Global hasn't had an address allocated yet!"); 204 return state.getGlobalAddressMap(locked)[GV]; 205 } 206 207 /// FIXME: document 208 /// 209 GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 210 GenericValue Result; 211 if (isa<UndefValue>(C)) return Result; 212 213 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) { 214 switch (CE->getOpcode()) { 215 case Instruction::GetElementPtr: { 216 Result = getConstantValue(CE->getOperand(0)); 217 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); 218 uint64_t Offset = 219 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); 220 221 if (getTargetData().getPointerSize() == 4) 222 Result.IntVal += Offset; 223 else 224 Result.LongVal += Offset; 225 return Result; 226 } 227 case Instruction::Cast: { 228 // We only need to handle a few cases here. Almost all casts will 229 // automatically fold, just the ones involving pointers won't. 230 // 231 Constant *Op = CE->getOperand(0); 232 GenericValue GV = getConstantValue(Op); 233 234 // Handle cast of pointer to pointer... 235 if (Op->getType()->getTypeID() == C->getType()->getTypeID()) 236 return GV; 237 238 // Handle a cast of pointer to any integral type... 239 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral()) 240 return GV; 241 242 // Handle cast of integer to a pointer... 243 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral()) 244 switch (Op->getType()->getTypeID()) { 245 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal); 246 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal); 247 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal); 248 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal); 249 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal); 250 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal); 251 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal); 252 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal); 253 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal); 254 default: assert(0 && "Unknown integral type!"); 255 } 256 break; 257 } 258 259 case Instruction::Add: 260 switch (CE->getOperand(0)->getType()->getTypeID()) { 261 default: assert(0 && "Bad add type!"); abort(); 262 case Type::LongTyID: 263 case Type::ULongTyID: 264 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal + 265 getConstantValue(CE->getOperand(1)).LongVal; 266 break; 267 case Type::IntTyID: 268 case Type::UIntTyID: 269 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal + 270 getConstantValue(CE->getOperand(1)).IntVal; 271 break; 272 case Type::ShortTyID: 273 case Type::UShortTyID: 274 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal + 275 getConstantValue(CE->getOperand(1)).ShortVal; 276 break; 277 case Type::SByteTyID: 278 case Type::UByteTyID: 279 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal + 280 getConstantValue(CE->getOperand(1)).SByteVal; 281 break; 282 case Type::FloatTyID: 283 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + 284 getConstantValue(CE->getOperand(1)).FloatVal; 285 break; 286 case Type::DoubleTyID: 287 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + 288 getConstantValue(CE->getOperand(1)).DoubleVal; 289 break; 290 } 291 return Result; 292 default: 293 break; 294 } 295 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; 296 abort(); 297 } 298 299 switch (C->getType()->getTypeID()) { 300 #define GET_CONST_VAL(TY, CTY, CLASS) \ 301 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break 302 GET_CONST_VAL(Bool , bool , ConstantBool); 303 GET_CONST_VAL(UByte , unsigned char , ConstantUInt); 304 GET_CONST_VAL(SByte , signed char , ConstantSInt); 305 GET_CONST_VAL(UShort , unsigned short, ConstantUInt); 306 GET_CONST_VAL(Short , signed short , ConstantSInt); 307 GET_CONST_VAL(UInt , unsigned int , ConstantUInt); 308 GET_CONST_VAL(Int , signed int , ConstantSInt); 309 GET_CONST_VAL(ULong , uint64_t , ConstantUInt); 310 GET_CONST_VAL(Long , int64_t , ConstantSInt); 311 GET_CONST_VAL(Float , float , ConstantFP); 312 GET_CONST_VAL(Double , double , ConstantFP); 313 #undef GET_CONST_VAL 314 case Type::PointerTyID: 315 if (isa<ConstantPointerNull>(C)) 316 Result.PointerVal = 0; 317 else if (const Function *F = dyn_cast<Function>(C)) 318 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); 319 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) 320 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); 321 else 322 assert(0 && "Unknown constant pointer type!"); 323 break; 324 default: 325 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; 326 abort(); 327 } 328 return Result; 329 } 330 331 /// FIXME: document 332 /// 333 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, 334 const Type *Ty) { 335 if (getTargetData().isLittleEndian()) { 336 switch (Ty->getTypeID()) { 337 case Type::BoolTyID: 338 case Type::UByteTyID: 339 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 340 case Type::UShortTyID: 341 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; 342 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; 343 break; 344 Store4BytesLittleEndian: 345 case Type::FloatTyID: 346 case Type::UIntTyID: 347 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; 348 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; 349 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; 350 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; 351 break; 352 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 353 goto Store4BytesLittleEndian; 354 case Type::DoubleTyID: 355 case Type::ULongTyID: 356 case Type::LongTyID: 357 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal ); 358 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8); 359 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16); 360 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24); 361 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32); 362 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40); 363 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48); 364 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56); 365 break; 366 default: 367 std::cout << "Cannot store value of type " << *Ty << "!\n"; 368 } 369 } else { 370 switch (Ty->getTypeID()) { 371 case Type::BoolTyID: 372 case Type::UByteTyID: 373 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 374 case Type::UShortTyID: 375 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; 376 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; 377 break; 378 Store4BytesBigEndian: 379 case Type::FloatTyID: 380 case Type::UIntTyID: 381 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; 382 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; 383 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; 384 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; 385 break; 386 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 387 goto Store4BytesBigEndian; 388 case Type::DoubleTyID: 389 case Type::ULongTyID: 390 case Type::LongTyID: 391 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal ); 392 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8); 393 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16); 394 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24); 395 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32); 396 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40); 397 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48); 398 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56); 399 break; 400 default: 401 std::cout << "Cannot store value of type " << *Ty << "!\n"; 402 } 403 } 404 } 405 406 /// FIXME: document 407 /// 408 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, 409 const Type *Ty) { 410 GenericValue Result; 411 if (getTargetData().isLittleEndian()) { 412 switch (Ty->getTypeID()) { 413 case Type::BoolTyID: 414 case Type::UByteTyID: 415 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 416 case Type::UShortTyID: 417 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | 418 ((unsigned)Ptr->Untyped[1] << 8); 419 break; 420 Load4BytesLittleEndian: 421 case Type::FloatTyID: 422 case Type::UIntTyID: 423 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | 424 ((unsigned)Ptr->Untyped[1] << 8) | 425 ((unsigned)Ptr->Untyped[2] << 16) | 426 ((unsigned)Ptr->Untyped[3] << 24); 427 break; 428 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 429 goto Load4BytesLittleEndian; 430 case Type::DoubleTyID: 431 case Type::ULongTyID: 432 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | 433 ((uint64_t)Ptr->Untyped[1] << 8) | 434 ((uint64_t)Ptr->Untyped[2] << 16) | 435 ((uint64_t)Ptr->Untyped[3] << 24) | 436 ((uint64_t)Ptr->Untyped[4] << 32) | 437 ((uint64_t)Ptr->Untyped[5] << 40) | 438 ((uint64_t)Ptr->Untyped[6] << 48) | 439 ((uint64_t)Ptr->Untyped[7] << 56); 440 break; 441 default: 442 std::cout << "Cannot load value of type " << *Ty << "!\n"; 443 abort(); 444 } 445 } else { 446 switch (Ty->getTypeID()) { 447 case Type::BoolTyID: 448 case Type::UByteTyID: 449 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 450 case Type::UShortTyID: 451 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | 452 ((unsigned)Ptr->Untyped[0] << 8); 453 break; 454 Load4BytesBigEndian: 455 case Type::FloatTyID: 456 case Type::UIntTyID: 457 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | 458 ((unsigned)Ptr->Untyped[2] << 8) | 459 ((unsigned)Ptr->Untyped[1] << 16) | 460 ((unsigned)Ptr->Untyped[0] << 24); 461 break; 462 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) 463 goto Load4BytesBigEndian; 464 case Type::DoubleTyID: 465 case Type::ULongTyID: 466 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | 467 ((uint64_t)Ptr->Untyped[6] << 8) | 468 ((uint64_t)Ptr->Untyped[5] << 16) | 469 ((uint64_t)Ptr->Untyped[4] << 24) | 470 ((uint64_t)Ptr->Untyped[3] << 32) | 471 ((uint64_t)Ptr->Untyped[2] << 40) | 472 ((uint64_t)Ptr->Untyped[1] << 48) | 473 ((uint64_t)Ptr->Untyped[0] << 56); 474 break; 475 default: 476 std::cout << "Cannot load value of type " << *Ty << "!\n"; 477 abort(); 478 } 479 } 480 return Result; 481 } 482 483 // InitializeMemory - Recursive function to apply a Constant value into the 484 // specified memory location... 485 // 486 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 487 if (isa<UndefValue>(Init)) { 488 return; 489 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) { 490 unsigned ElementSize = 491 getTargetData().getTypeSize(CP->getType()->getElementType()); 492 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 493 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); 494 return; 495 } else if (Init->getType()->isFirstClassType()) { 496 GenericValue Val = getConstantValue(Init); 497 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 498 return; 499 } else if (isa<ConstantAggregateZero>(Init)) { 500 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType())); 501 return; 502 } 503 504 switch (Init->getType()->getTypeID()) { 505 case Type::ArrayTyID: { 506 const ConstantArray *CPA = cast<ConstantArray>(Init); 507 unsigned ElementSize = 508 getTargetData().getTypeSize(CPA->getType()->getElementType()); 509 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 510 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); 511 return; 512 } 513 514 case Type::StructTyID: { 515 const ConstantStruct *CPS = cast<ConstantStruct>(Init); 516 const StructLayout *SL = 517 getTargetData().getStructLayout(cast<StructType>(CPS->getType())); 518 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 519 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]); 520 return; 521 } 522 523 default: 524 std::cerr << "Bad Type: " << *Init->getType() << "\n"; 525 assert(0 && "Unknown constant type to initialize memory with!"); 526 } 527 } 528 529 /// EmitGlobals - Emit all of the global variables to memory, storing their 530 /// addresses into GlobalAddress. This must make sure to copy the contents of 531 /// their initializers into the memory. 532 /// 533 void ExecutionEngine::emitGlobals() { 534 const TargetData &TD = getTargetData(); 535 536 // Loop over all of the global variables in the program, allocating the memory 537 // to hold them. 538 Module &M = getModule(); 539 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 540 I != E; ++I) 541 if (!I->isExternal()) { 542 // Get the type of the global... 543 const Type *Ty = I->getType()->getElementType(); 544 545 // Allocate some memory for it! 546 unsigned Size = TD.getTypeSize(Ty); 547 addGlobalMapping(I, new char[Size]); 548 } else { 549 // External variable reference. Try to use the dynamic loader to 550 // get a pointer to it. 551 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol( 552 I->getName().c_str())) 553 addGlobalMapping(I, SymAddr); 554 else { 555 std::cerr << "Could not resolve external global address: " 556 << I->getName() << "\n"; 557 abort(); 558 } 559 } 560 561 // Now that all of the globals are set up in memory, loop through them all and 562 // initialize their contents. 563 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 564 I != E; ++I) 565 if (!I->isExternal()) 566 EmitGlobalVariable(I); 567 } 568 569 // EmitGlobalVariable - This method emits the specified global variable to the 570 // address specified in GlobalAddresses, or allocates new memory if it's not 571 // already in the map. 572 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { 573 void *GA = getPointerToGlobalIfAvailable(GV); 574 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n"); 575 576 const Type *ElTy = GV->getType()->getElementType(); 577 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy); 578 if (GA == 0) { 579 // If it's not already specified, allocate memory for the global. 580 GA = new char[GVSize]; 581 addGlobalMapping(GV, GA); 582 } 583 584 InitializeMemory(GV->getInitializer(), GA); 585 NumInitBytes += (unsigned)GVSize; 586 ++NumGlobals; 587 } 588