1 //===-- ExecutionEngine.cpp - Common Implementation shared by EE's --------===// 2 // 3 // This file defines the common interface used by the various execution engine 4 // subclasses. 5 // 6 //===----------------------------------------------------------------------===// 7 8 #include "ExecutionEngine.h" 9 #include "GenericValue.h" 10 #include "llvm/DerivedTypes.h" 11 #include "llvm/Constants.h" 12 #include "llvm/Module.h" 13 #include "llvm/Target/TargetData.h" 14 #include "Support/Statistic.h" 15 #include <dlfcn.h> 16 17 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); 18 19 // getPointerToGlobal - This returns the address of the specified global 20 // value. This may involve code generation if it's a function. 21 // 22 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 23 if (const Function *F = dyn_cast<Function>(GV)) 24 return getPointerToFunction(F); 25 26 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?"); 27 return GlobalAddress[GV]; 28 } 29 30 31 GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 32 GenericValue Result; 33 34 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) 35 switch (CE->getOpcode()) { 36 case Instruction::GetElementPtr: { 37 Result = getConstantValue(cast<Constant>(CE->getOperand(0))); 38 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); 39 uint64_t Offset = 40 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); 41 42 Result.LongVal += Offset; 43 return Result; 44 } 45 46 default: 47 std::cerr << "ConstantExpr not handled as global var init: " << *CE 48 << "\n"; 49 abort(); 50 } 51 52 switch (C->getType()->getPrimitiveID()) { 53 #define GET_CONST_VAL(TY, CLASS) \ 54 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break 55 GET_CONST_VAL(Bool , ConstantBool); 56 GET_CONST_VAL(UByte , ConstantUInt); 57 GET_CONST_VAL(SByte , ConstantSInt); 58 GET_CONST_VAL(UShort , ConstantUInt); 59 GET_CONST_VAL(Short , ConstantSInt); 60 GET_CONST_VAL(UInt , ConstantUInt); 61 GET_CONST_VAL(Int , ConstantSInt); 62 GET_CONST_VAL(ULong , ConstantUInt); 63 GET_CONST_VAL(Long , ConstantSInt); 64 GET_CONST_VAL(Float , ConstantFP); 65 GET_CONST_VAL(Double , ConstantFP); 66 #undef GET_CONST_VAL 67 case Type::PointerTyID: 68 if (isa<ConstantPointerNull>(C)) { 69 Result.PointerVal = 0; 70 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){ 71 Result = PTOGV(getPointerToGlobal(CPR->getValue())); 72 73 } else { 74 assert(0 && "Unknown constant pointer type!"); 75 } 76 break; 77 default: 78 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n"; 79 abort(); 80 } 81 return Result; 82 } 83 84 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, 85 const Type *Ty) { 86 if (getTargetData().isLittleEndian()) { 87 switch (Ty->getPrimitiveID()) { 88 case Type::BoolTyID: 89 case Type::UByteTyID: 90 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 91 case Type::UShortTyID: 92 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; 93 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; 94 break; 95 Store4BytesLittleEndian: 96 case Type::FloatTyID: 97 case Type::UIntTyID: 98 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; 99 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; 100 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; 101 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; 102 break; 103 case Type::PointerTyID: if (CurMod.has32BitPointers()) 104 goto Store4BytesLittleEndian; 105 case Type::DoubleTyID: 106 case Type::ULongTyID: 107 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255; 108 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255; 109 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255; 110 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255; 111 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255; 112 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255; 113 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255; 114 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255; 115 break; 116 default: 117 std::cout << "Cannot store value of type " << Ty << "!\n"; 118 } 119 } else { 120 switch (Ty->getPrimitiveID()) { 121 case Type::BoolTyID: 122 case Type::UByteTyID: 123 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; 124 case Type::UShortTyID: 125 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; 126 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; 127 break; 128 Store4BytesBigEndian: 129 case Type::FloatTyID: 130 case Type::UIntTyID: 131 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; 132 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; 133 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; 134 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; 135 break; 136 case Type::PointerTyID: if (CurMod.has32BitPointers()) 137 goto Store4BytesBigEndian; 138 case Type::DoubleTyID: 139 case Type::ULongTyID: 140 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255; 141 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255; 142 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255; 143 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255; 144 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255; 145 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255; 146 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255; 147 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255; 148 break; 149 default: 150 std::cout << "Cannot store value of type " << Ty << "!\n"; 151 } 152 } 153 } 154 155 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, 156 const Type *Ty) { 157 GenericValue Result; 158 if (getTargetData().isLittleEndian()) { 159 switch (Ty->getPrimitiveID()) { 160 case Type::BoolTyID: 161 case Type::UByteTyID: 162 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 163 case Type::UShortTyID: 164 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | 165 ((unsigned)Ptr->Untyped[1] << 8); 166 break; 167 Load4BytesLittleEndian: 168 case Type::FloatTyID: 169 case Type::UIntTyID: 170 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | 171 ((unsigned)Ptr->Untyped[1] << 8) | 172 ((unsigned)Ptr->Untyped[2] << 16) | 173 ((unsigned)Ptr->Untyped[3] << 24); 174 break; 175 case Type::PointerTyID: if (getModule().has32BitPointers()) 176 goto Load4BytesLittleEndian; 177 case Type::DoubleTyID: 178 case Type::ULongTyID: 179 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | 180 ((uint64_t)Ptr->Untyped[1] << 8) | 181 ((uint64_t)Ptr->Untyped[2] << 16) | 182 ((uint64_t)Ptr->Untyped[3] << 24) | 183 ((uint64_t)Ptr->Untyped[4] << 32) | 184 ((uint64_t)Ptr->Untyped[5] << 40) | 185 ((uint64_t)Ptr->Untyped[6] << 48) | 186 ((uint64_t)Ptr->Untyped[7] << 56); 187 break; 188 default: 189 std::cout << "Cannot load value of type " << *Ty << "!\n"; 190 abort(); 191 } 192 } else { 193 switch (Ty->getPrimitiveID()) { 194 case Type::BoolTyID: 195 case Type::UByteTyID: 196 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 197 case Type::UShortTyID: 198 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | 199 ((unsigned)Ptr->Untyped[0] << 8); 200 break; 201 Load4BytesBigEndian: 202 case Type::FloatTyID: 203 case Type::UIntTyID: 204 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | 205 ((unsigned)Ptr->Untyped[2] << 8) | 206 ((unsigned)Ptr->Untyped[1] << 16) | 207 ((unsigned)Ptr->Untyped[0] << 24); 208 break; 209 case Type::PointerTyID: if (getModule().has32BitPointers()) 210 goto Load4BytesBigEndian; 211 case Type::DoubleTyID: 212 case Type::ULongTyID: 213 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | 214 ((uint64_t)Ptr->Untyped[6] << 8) | 215 ((uint64_t)Ptr->Untyped[5] << 16) | 216 ((uint64_t)Ptr->Untyped[4] << 24) | 217 ((uint64_t)Ptr->Untyped[3] << 32) | 218 ((uint64_t)Ptr->Untyped[2] << 40) | 219 ((uint64_t)Ptr->Untyped[1] << 48) | 220 ((uint64_t)Ptr->Untyped[0] << 56); 221 break; 222 default: 223 std::cout << "Cannot load value of type " << *Ty << "!\n"; 224 abort(); 225 } 226 } 227 return Result; 228 } 229 230 231 // InitializeMemory - Recursive function to apply a Constant value into the 232 // specified memory location... 233 // 234 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 235 if (Init->getType()->isFirstClassType()) { 236 GenericValue Val = getConstantValue(Init); 237 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 238 return; 239 } 240 241 switch (Init->getType()->getPrimitiveID()) { 242 case Type::ArrayTyID: { 243 const ConstantArray *CPA = cast<ConstantArray>(Init); 244 const std::vector<Use> &Val = CPA->getValues(); 245 unsigned ElementSize = 246 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType()); 247 for (unsigned i = 0; i < Val.size(); ++i) 248 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize); 249 return; 250 } 251 252 case Type::StructTyID: { 253 const ConstantStruct *CPS = cast<ConstantStruct>(Init); 254 const StructLayout *SL = 255 getTargetData().getStructLayout(cast<StructType>(CPS->getType())); 256 const std::vector<Use> &Val = CPS->getValues(); 257 for (unsigned i = 0; i < Val.size(); ++i) 258 InitializeMemory(cast<Constant>(Val[i].get()), 259 (char*)Addr+SL->MemberOffsets[i]); 260 return; 261 } 262 263 default: 264 std::cerr << "Bad Type: " << Init->getType() << "\n"; 265 assert(0 && "Unknown constant type to initialize memory with!"); 266 } 267 } 268 269 270 271 void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) { 272 if (getTargetData().getPointerSize() == 8) { // 64 bit target? 273 PointerTy *Result = new PointerTy[InputArgv.size()+1]; 274 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 275 276 for (unsigned i = 0; i < InputArgv.size(); ++i) { 277 unsigned Size = InputArgv[i].size()+1; 278 char *Dest = new char[Size]; 279 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 280 281 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 282 Dest[Size-1] = 0; 283 284 // Endian safe: Result[i] = (PointerTy)Dest; 285 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::LongTy); 286 } 287 Result[InputArgv.size()] = 0; 288 return Result; 289 290 } else { // 32 bit target? 291 int *Result = new int[InputArgv.size()+1]; 292 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 293 294 for (unsigned i = 0; i < InputArgv.size(); ++i) { 295 unsigned Size = InputArgv[i].size()+1; 296 char *Dest = new char[Size]; 297 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 298 299 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 300 Dest[Size-1] = 0; 301 302 // Endian safe: Result[i] = (PointerTy)Dest; 303 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::IntTy); 304 } 305 Result[InputArgv.size()] = 0; // null terminate it 306 return Result; 307 } 308 } 309 310 /// EmitGlobals - Emit all of the global variables to memory, storing their 311 /// addresses into GlobalAddress. This must make sure to copy the contents of 312 /// their initializers into the memory. 313 /// 314 void ExecutionEngine::emitGlobals() { 315 const TargetData &TD = getTargetData(); 316 317 // Loop over all of the global variables in the program, allocating the memory 318 // to hold them. 319 for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); 320 I != E; ++I) 321 if (!I->isExternal()) { 322 // Get the type of the global... 323 const Type *Ty = I->getType()->getElementType(); 324 325 // Allocate some memory for it! 326 unsigned Size = TD.getTypeSize(Ty); 327 GlobalAddress[I] = new char[Size]; 328 NumInitBytes += Size; 329 330 DEBUG(std::cerr << "Global '" << I->getName() << "' -> " 331 << (void*)GlobalAddress[I] << "\n"); 332 } else { 333 // External variable reference, try to use dlsym to get a pointer to it in 334 // the LLI image. 335 if (void *SymAddr = dlsym(0, I->getName().c_str())) 336 GlobalAddress[I] = SymAddr; 337 else { 338 std::cerr << "Could not resolve external global address: " 339 << I->getName() << "\n"; 340 abort(); 341 } 342 } 343 344 // Now that all of the globals are set up in memory, loop through them all and 345 // initialize their contents. 346 for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); 347 I != E; ++I) 348 if (!I->isExternal()) 349 InitializeMemory(I->getInitializer(), GlobalAddress[I]); 350 } 351 352