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" |
Chris Lattner | 8f7f71b | 2004-06-20 07:46:18 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chris Lattner | fd13129 | 2003-09-05 20:08:15 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Chris Lattner | fd13129 | 2003-09-05 20:08:15 +0000 | [diff] [blame] | 26 | #include "Support/Debug.h" |
| 27 | #include "Support/Statistic.h" |
Brian Gaeke | 322cdb2 | 2003-10-10 17:02:42 +0000 | [diff] [blame] | 28 | #include "Support/DynamicLinker.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"); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 33 | Statistic<> NumGlobals ("lli", "Number of global vars initialized"); |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 34 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 35 | |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 36 | ExecutionEngine::ExecutionEngine(ModuleProvider *P) : |
| 37 | CurMod(*P->getModule()), MP(P) { |
| 38 | assert(P && "ModuleProvider is null?"); |
| 39 | } |
| 40 | |
| 41 | ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) { |
Misha Brukman | 0570157 | 2003-10-17 18:31:59 +0000 | [diff] [blame] | 42 | assert(M && "Module is null?"); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 45 | ExecutionEngine::~ExecutionEngine() { |
Misha Brukman | 7b2b40f | 2003-10-14 21:36:31 +0000 | [diff] [blame] | 46 | delete MP; |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 49 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 50 | /// at the specified address. |
| 51 | /// |
| 52 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
| 53 | // If we haven't computed the reverse mapping yet, do so first. |
| 54 | if (GlobalAddressReverseMap.empty()) { |
| 55 | for (std::map<const GlobalValue*, void *>::iterator I = |
| 56 | GlobalAddressMap.begin(), E = GlobalAddressMap.end(); I != E; ++I) |
| 57 | GlobalAddressReverseMap.insert(std::make_pair(I->second, I->first)); |
| 58 | } |
| 59 | |
| 60 | std::map<void *, const GlobalValue*>::iterator I = |
| 61 | GlobalAddressReverseMap.find(Addr); |
| 62 | return I != GlobalAddressReverseMap.end() ? I->second : 0; |
| 63 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 64 | |
| 65 | // CreateArgv - Turn a vector of strings into a nice argv style array of |
| 66 | // pointers to null terminated strings. |
| 67 | // |
| 68 | static void *CreateArgv(ExecutionEngine *EE, |
| 69 | const std::vector<std::string> &InputArgv) { |
| 70 | unsigned PtrSize = EE->getTargetData().getPointerSize(); |
| 71 | char *Result = new char[(InputArgv.size()+1)*PtrSize]; |
| 72 | |
| 73 | DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); |
| 74 | const Type *SBytePtr = PointerType::get(Type::SByteTy); |
| 75 | |
| 76 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 77 | unsigned Size = InputArgv[i].size()+1; |
| 78 | char *Dest = new char[Size]; |
| 79 | DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); |
| 80 | |
| 81 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 82 | Dest[Size-1] = 0; |
| 83 | |
| 84 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 85 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), |
| 86 | SBytePtr); |
| 87 | } |
| 88 | |
| 89 | // Null terminate it |
| 90 | EE->StoreValueToMemory(PTOGV(0), |
| 91 | (GenericValue*)(Result+InputArgv.size()*PtrSize), |
| 92 | SBytePtr); |
| 93 | return Result; |
| 94 | } |
| 95 | |
| 96 | /// runFunctionAsMain - This is a helper function which wraps runFunction to |
| 97 | /// handle the common task of starting up main with the specified argc, argv, |
| 98 | /// and envp parameters. |
| 99 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 100 | const std::vector<std::string> &argv, |
| 101 | const char * const * envp) { |
| 102 | std::vector<GenericValue> GVArgs; |
| 103 | GenericValue GVArgc; |
| 104 | GVArgc.IntVal = argv.size(); |
| 105 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 106 | GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. |
| 107 | assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv"); |
| 108 | |
| 109 | std::vector<std::string> EnvVars; |
| 110 | for (unsigned i = 0; envp[i]; ++i) |
| 111 | EnvVars.push_back(envp[i]); |
| 112 | GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. |
| 113 | return runFunction(Fn, GVArgs).IntVal; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 118 | /// If possible, create a JIT, unless the caller specifically requests an |
| 119 | /// Interpreter or there's an error. If even an Interpreter cannot be created, |
| 120 | /// NULL is returned. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 121 | /// |
Misha Brukman | 7b2b40f | 2003-10-14 21:36:31 +0000 | [diff] [blame] | 122 | ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 123 | bool ForceInterpreter, |
| 124 | IntrinsicLowering *IL) { |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 125 | ExecutionEngine *EE = 0; |
| 126 | |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 127 | // Unless the interpreter was explicitly selected, try making a JIT. |
Brian Gaeke | 20a277e | 2003-10-24 19:58:38 +0000 | [diff] [blame] | 128 | if (!ForceInterpreter) |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 129 | EE = JIT::create(MP, IL); |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 130 | |
| 131 | // If we can't make a JIT, make an interpreter instead. |
Chris Lattner | 338733f | 2004-02-01 01:07:25 +0000 | [diff] [blame] | 132 | if (EE == 0) { |
| 133 | try { |
| 134 | Module *M = MP->materializeModule(); |
| 135 | try { |
| 136 | EE = Interpreter::create(M, IL); |
| 137 | } catch (...) { |
| 138 | std::cerr << "Error creating the interpreter!\n"; |
| 139 | } |
Reid Spencer | e294753 | 2004-07-07 21:01:38 +0000 | [diff] [blame] | 140 | } catch (std::string& errmsg) { |
| 141 | std::cerr << "Error reading the bytecode file: " << errmsg << "\n"; |
Chris Lattner | 338733f | 2004-02-01 01:07:25 +0000 | [diff] [blame] | 142 | } catch (...) { |
| 143 | std::cerr << "Error reading the bytecode file!\n"; |
| 144 | } |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 146 | |
| 147 | if (EE == 0) delete IL; |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 148 | return EE; |
| 149 | } |
| 150 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 151 | /// getPointerToGlobal - This returns the address of the specified global |
| 152 | /// value. This may involve code generation if it's a function. |
| 153 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 154 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
Brian Gaeke | 37df460 | 2003-08-13 18:16:14 +0000 | [diff] [blame] | 155 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 156 | return getPointerToFunction(F); |
| 157 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 158 | assert(GlobalAddressMap[GV] && "Global hasn't had an address allocated yet?"); |
| 159 | return GlobalAddressMap[GV]; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 162 | /// FIXME: document |
| 163 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 164 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
| 165 | GenericValue Result; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 167 | if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) { |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 168 | switch (CE->getOpcode()) { |
| 169 | case Instruction::GetElementPtr: { |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 170 | Result = getConstantValue(CE->getOperand(0)); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 171 | std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); |
| 172 | uint64_t Offset = |
| 173 | TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); |
| 174 | |
| 175 | Result.LongVal += Offset; |
| 176 | return Result; |
| 177 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 178 | case Instruction::Cast: { |
| 179 | // We only need to handle a few cases here. Almost all casts will |
| 180 | // automatically fold, just the ones involving pointers won't. |
| 181 | // |
| 182 | Constant *Op = CE->getOperand(0); |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 183 | GenericValue GV = getConstantValue(Op); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 185 | // Handle cast of pointer to pointer... |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 186 | if (Op->getType()->getTypeID() == C->getType()->getTypeID()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 187 | return GV; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 74cf819 | 2003-08-18 17:23:40 +0000 | [diff] [blame] | 189 | // Handle a cast of pointer to any integral type... |
Chris Lattner | c88a4ea | 2003-08-18 17:33:15 +0000 | [diff] [blame] | 190 | if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 191 | return GV; |
Chris Lattner | 74cf819 | 2003-08-18 17:23:40 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 193 | // Handle cast of integer to a pointer... |
| 194 | if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral()) |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 195 | switch (Op->getType()->getTypeID()) { |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 196 | case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal); |
| 197 | case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal); |
| 198 | case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal); |
| 199 | case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal); |
| 200 | case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal); |
| 201 | case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal); |
| 202 | case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal); |
| 203 | case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal); |
| 204 | case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal); |
| 205 | default: assert(0 && "Unknown integral type!"); |
| 206 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 207 | break; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 210 | case Instruction::Add: |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 211 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
| 212 | default: assert(0 && "Bad add type!"); abort(); |
| 213 | case Type::LongTyID: |
| 214 | case Type::ULongTyID: |
Chris Lattner | 6b2125c | 2003-05-14 17:53:49 +0000 | [diff] [blame] | 215 | Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal + |
| 216 | getConstantValue(CE->getOperand(1)).LongVal; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 217 | break; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 218 | case Type::IntTyID: |
| 219 | case Type::UIntTyID: |
| 220 | Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal + |
| 221 | getConstantValue(CE->getOperand(1)).IntVal; |
| 222 | break; |
| 223 | case Type::ShortTyID: |
| 224 | case Type::UShortTyID: |
| 225 | Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal + |
| 226 | getConstantValue(CE->getOperand(1)).ShortVal; |
| 227 | break; |
| 228 | case Type::SByteTyID: |
| 229 | case Type::UByteTyID: |
| 230 | Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal + |
| 231 | getConstantValue(CE->getOperand(1)).SByteVal; |
| 232 | break; |
| 233 | case Type::FloatTyID: |
| 234 | Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + |
| 235 | getConstantValue(CE->getOperand(1)).FloatVal; |
| 236 | break; |
| 237 | case Type::DoubleTyID: |
| 238 | Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + |
| 239 | getConstantValue(CE->getOperand(1)).DoubleVal; |
| 240 | break; |
| 241 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 242 | return Result; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; |
| 247 | abort(); |
| 248 | } |
| 249 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 250 | switch (C->getType()->getTypeID()) { |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 251 | #define GET_CONST_VAL(TY, CLASS) \ |
| 252 | case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 253 | GET_CONST_VAL(Bool , ConstantBool); |
| 254 | GET_CONST_VAL(UByte , ConstantUInt); |
| 255 | GET_CONST_VAL(SByte , ConstantSInt); |
| 256 | GET_CONST_VAL(UShort , ConstantUInt); |
| 257 | GET_CONST_VAL(Short , ConstantSInt); |
| 258 | GET_CONST_VAL(UInt , ConstantUInt); |
| 259 | GET_CONST_VAL(Int , ConstantSInt); |
| 260 | GET_CONST_VAL(ULong , ConstantUInt); |
| 261 | GET_CONST_VAL(Long , ConstantSInt); |
| 262 | GET_CONST_VAL(Float , ConstantFP); |
| 263 | GET_CONST_VAL(Double , ConstantFP); |
| 264 | #undef GET_CONST_VAL |
| 265 | case Type::PointerTyID: |
| 266 | if (isa<ConstantPointerNull>(C)) { |
| 267 | Result.PointerVal = 0; |
| 268 | } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){ |
Chris Lattner | 38f0eba | 2003-12-08 08:22:48 +0000 | [diff] [blame] | 269 | if (Function *F = |
| 270 | const_cast<Function*>(dyn_cast<Function>(CPR->getValue()))) |
| 271 | Result = PTOGV(getPointerToFunctionOrStub(F)); |
| 272 | else |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 273 | Result = PTOGV(getOrEmitGlobalVariable( |
| 274 | cast<GlobalVariable>(CPR->getValue()))); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 275 | |
| 276 | } else { |
| 277 | assert(0 && "Unknown constant pointer type!"); |
| 278 | } |
| 279 | break; |
| 280 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame^] | 281 | std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 282 | abort(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 283 | } |
| 284 | return Result; |
| 285 | } |
| 286 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 287 | /// FIXME: document |
| 288 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 289 | void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 290 | const Type *Ty) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 291 | if (getTargetData().isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 292 | switch (Ty->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 293 | case Type::BoolTyID: |
| 294 | case Type::UByteTyID: |
| 295 | case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; |
| 296 | case Type::UShortTyID: |
| 297 | case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; |
| 298 | Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; |
| 299 | break; |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 300 | Store4BytesLittleEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 301 | case Type::FloatTyID: |
| 302 | case Type::UIntTyID: |
| 303 | case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; |
| 304 | Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; |
| 305 | Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; |
| 306 | Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; |
| 307 | break; |
Chris Lattner | c879e8f | 2003-08-24 19:55:26 +0000 | [diff] [blame] | 308 | case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 309 | goto Store4BytesLittleEndian; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 310 | case Type::DoubleTyID: |
| 311 | case Type::ULongTyID: |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 312 | case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 313 | Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255; |
| 314 | Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255; |
| 315 | Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255; |
| 316 | Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255; |
| 317 | Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255; |
| 318 | Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255; |
| 319 | Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255; |
| 320 | break; |
| 321 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame^] | 322 | std::cout << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 323 | } |
| 324 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 325 | switch (Ty->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 326 | case Type::BoolTyID: |
| 327 | case Type::UByteTyID: |
| 328 | case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; |
| 329 | case Type::UShortTyID: |
| 330 | case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; |
| 331 | Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; |
| 332 | break; |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 333 | Store4BytesBigEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 334 | case Type::FloatTyID: |
| 335 | case Type::UIntTyID: |
| 336 | case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; |
| 337 | Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; |
| 338 | Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; |
| 339 | Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; |
| 340 | break; |
Chris Lattner | c879e8f | 2003-08-24 19:55:26 +0000 | [diff] [blame] | 341 | case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 342 | goto Store4BytesBigEndian; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 343 | case Type::DoubleTyID: |
| 344 | case Type::ULongTyID: |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 345 | case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 346 | Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255; |
| 347 | Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255; |
| 348 | Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255; |
| 349 | Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255; |
| 350 | Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255; |
| 351 | Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255; |
| 352 | Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255; |
| 353 | break; |
| 354 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame^] | 355 | std::cout << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 360 | /// FIXME: document |
| 361 | /// |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 362 | GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, |
| 363 | const Type *Ty) { |
| 364 | GenericValue Result; |
| 365 | if (getTargetData().isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 366 | switch (Ty->getTypeID()) { |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 367 | case Type::BoolTyID: |
| 368 | case Type::UByteTyID: |
| 369 | case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; |
| 370 | case Type::UShortTyID: |
| 371 | case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | |
| 372 | ((unsigned)Ptr->Untyped[1] << 8); |
| 373 | break; |
| 374 | Load4BytesLittleEndian: |
| 375 | case Type::FloatTyID: |
| 376 | case Type::UIntTyID: |
| 377 | case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | |
| 378 | ((unsigned)Ptr->Untyped[1] << 8) | |
| 379 | ((unsigned)Ptr->Untyped[2] << 16) | |
| 380 | ((unsigned)Ptr->Untyped[3] << 24); |
| 381 | break; |
Chris Lattner | c879e8f | 2003-08-24 19:55:26 +0000 | [diff] [blame] | 382 | case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 383 | goto Load4BytesLittleEndian; |
| 384 | case Type::DoubleTyID: |
| 385 | case Type::ULongTyID: |
| 386 | case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | |
| 387 | ((uint64_t)Ptr->Untyped[1] << 8) | |
| 388 | ((uint64_t)Ptr->Untyped[2] << 16) | |
| 389 | ((uint64_t)Ptr->Untyped[3] << 24) | |
| 390 | ((uint64_t)Ptr->Untyped[4] << 32) | |
| 391 | ((uint64_t)Ptr->Untyped[5] << 40) | |
| 392 | ((uint64_t)Ptr->Untyped[6] << 48) | |
| 393 | ((uint64_t)Ptr->Untyped[7] << 56); |
| 394 | break; |
| 395 | default: |
| 396 | std::cout << "Cannot load value of type " << *Ty << "!\n"; |
| 397 | abort(); |
| 398 | } |
| 399 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 400 | switch (Ty->getTypeID()) { |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 401 | case Type::BoolTyID: |
| 402 | case Type::UByteTyID: |
| 403 | case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; |
| 404 | case Type::UShortTyID: |
| 405 | case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | |
| 406 | ((unsigned)Ptr->Untyped[0] << 8); |
| 407 | break; |
| 408 | Load4BytesBigEndian: |
| 409 | case Type::FloatTyID: |
| 410 | case Type::UIntTyID: |
| 411 | case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | |
| 412 | ((unsigned)Ptr->Untyped[2] << 8) | |
| 413 | ((unsigned)Ptr->Untyped[1] << 16) | |
| 414 | ((unsigned)Ptr->Untyped[0] << 24); |
| 415 | break; |
Chris Lattner | c879e8f | 2003-08-24 19:55:26 +0000 | [diff] [blame] | 416 | case Type::PointerTyID: if (getTargetData().getPointerSize() == 4) |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 417 | goto Load4BytesBigEndian; |
| 418 | case Type::DoubleTyID: |
| 419 | case Type::ULongTyID: |
| 420 | case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | |
| 421 | ((uint64_t)Ptr->Untyped[6] << 8) | |
| 422 | ((uint64_t)Ptr->Untyped[5] << 16) | |
| 423 | ((uint64_t)Ptr->Untyped[4] << 24) | |
| 424 | ((uint64_t)Ptr->Untyped[3] << 32) | |
| 425 | ((uint64_t)Ptr->Untyped[2] << 40) | |
| 426 | ((uint64_t)Ptr->Untyped[1] << 48) | |
| 427 | ((uint64_t)Ptr->Untyped[0] << 56); |
| 428 | break; |
| 429 | default: |
| 430 | std::cout << "Cannot load value of type " << *Ty << "!\n"; |
| 431 | abort(); |
| 432 | } |
| 433 | } |
| 434 | return Result; |
| 435 | } |
| 436 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 437 | // InitializeMemory - Recursive function to apply a Constant value into the |
| 438 | // specified memory location... |
| 439 | // |
| 440 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
| 441 | if (Init->getType()->isFirstClassType()) { |
| 442 | GenericValue Val = getConstantValue(Init); |
| 443 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 444 | return; |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 445 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 446 | unsigned Size = getTargetData().getTypeSize(Init->getType()); |
| 447 | memset(Addr, 0, Size); |
| 448 | return; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 451 | switch (Init->getType()->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 452 | case Type::ArrayTyID: { |
| 453 | const ConstantArray *CPA = cast<ConstantArray>(Init); |
Chris Lattner | d6840ac | 2002-12-24 00:39:16 +0000 | [diff] [blame] | 454 | const std::vector<Use> &Val = CPA->getValues(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 455 | unsigned ElementSize = |
| 456 | getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType()); |
| 457 | for (unsigned i = 0; i < Val.size(); ++i) |
| 458 | InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | case Type::StructTyID: { |
| 463 | const ConstantStruct *CPS = cast<ConstantStruct>(Init); |
| 464 | const StructLayout *SL = |
| 465 | getTargetData().getStructLayout(cast<StructType>(CPS->getType())); |
Chris Lattner | d6840ac | 2002-12-24 00:39:16 +0000 | [diff] [blame] | 466 | const std::vector<Use> &Val = CPS->getValues(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 467 | for (unsigned i = 0; i < Val.size(); ++i) |
| 468 | InitializeMemory(cast<Constant>(Val[i].get()), |
| 469 | (char*)Addr+SL->MemberOffsets[i]); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame^] | 474 | std::cerr << "Bad Type: " << *Init->getType() << "\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 475 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 476 | } |
| 477 | } |
| 478 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 479 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 480 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 481 | /// their initializers into the memory. |
| 482 | /// |
| 483 | void ExecutionEngine::emitGlobals() { |
| 484 | const TargetData &TD = getTargetData(); |
| 485 | |
| 486 | // Loop over all of the global variables in the program, allocating the memory |
| 487 | // to hold them. |
| 488 | for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); |
| 489 | I != E; ++I) |
| 490 | if (!I->isExternal()) { |
| 491 | // Get the type of the global... |
| 492 | const Type *Ty = I->getType()->getElementType(); |
| 493 | |
| 494 | // Allocate some memory for it! |
| 495 | unsigned Size = TD.getTypeSize(Ty); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 496 | addGlobalMapping(I, new char[Size]); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 497 | } else { |
Brian Gaeke | 322cdb2 | 2003-10-10 17:02:42 +0000 | [diff] [blame] | 498 | // External variable reference. Try to use the dynamic loader to |
| 499 | // get a pointer to it. |
| 500 | if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str())) |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 501 | addGlobalMapping(I, SymAddr); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 502 | else { |
| 503 | std::cerr << "Could not resolve external global address: " |
| 504 | << I->getName() << "\n"; |
| 505 | abort(); |
| 506 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // Now that all of the globals are set up in memory, loop through them all and |
| 510 | // initialize their contents. |
| 511 | for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); |
| 512 | I != E; ++I) |
| 513 | if (!I->isExternal()) |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 514 | EmitGlobalVariable(I); |
| 515 | } |
| 516 | |
| 517 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 518 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 519 | // already in the map. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 520 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 521 | void *GA = getPointerToGlobalIfAvailable(GV); |
Chris Lattner | 23c4724 | 2004-02-08 19:33:23 +0000 | [diff] [blame] | 522 | DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n"); |
| 523 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 524 | const Type *ElTy = GV->getType()->getElementType(); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 525 | if (GA == 0) { |
| 526 | // If it's not already specified, allocate memory for the global. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 527 | GA = new char[getTargetData().getTypeSize(ElTy)]; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 528 | addGlobalMapping(GV, GA); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 529 | } |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 531 | InitializeMemory(GV->getInitializer(), GA); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 532 | NumInitBytes += getTargetData().getTypeSize(ElTy); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 533 | ++NumGlobals; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 534 | } |