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