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