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