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