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