blob: 53587bff5a077c413e1925812c394957e55c7577 [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "Interpreter/Interpreter.h"
Chris Lattner61612df2003-12-20 01:45:17 +000017#include "JIT/JIT.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000020#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ModuleProvider.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner8f7f71b2004-06-20 07:46:18 +000023#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukman19684162003-10-16 21:18:05 +000024#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000025#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000027#include "llvm/System/DynamicLibrary.h"
28#include "llvm/Target/TargetData.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000029using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000031namespace {
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000033 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000034}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
Misha Brukmanedf128a2005-04-21 22:36:52 +000036ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
Misha Brukman19684162003-10-16 21:18:05 +000037 CurMod(*P->getModule()), MP(P) {
38 assert(P && "ModuleProvider is null?");
39}
40
41ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000042 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000043}
44
Brian Gaeke8e539482003-09-04 22:57:27 +000045ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000046 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000047}
48
Chris Lattner55d86482003-12-31 20:21:04 +000049/// getGlobalValueAtAddress - Return the LLVM global value object that starts
50/// at the specified address.
51///
52const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +000053 MutexGuard locked(lock);
54
Chris Lattner55d86482003-12-31 20:21:04 +000055 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +000056 if (state.getGlobalAddressReverseMap(locked).empty()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +000057 for (std::map<const GlobalValue*, void *>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000058 state.getGlobalAddressMap(locked).begin(), E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
59 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +000060 }
61
62 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000063 state.getGlobalAddressReverseMap(locked).find(Addr);
64 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +000065}
Chris Lattner87f03102003-12-26 06:50:30 +000066
67// CreateArgv - Turn a vector of strings into a nice argv style array of
68// pointers to null terminated strings.
69//
70static void *CreateArgv(ExecutionEngine *EE,
71 const std::vector<std::string> &InputArgv) {
72 unsigned PtrSize = EE->getTargetData().getPointerSize();
73 char *Result = new char[(InputArgv.size()+1)*PtrSize];
74
75 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
76 const Type *SBytePtr = PointerType::get(Type::SByteTy);
77
78 for (unsigned i = 0; i != InputArgv.size(); ++i) {
79 unsigned Size = InputArgv[i].size()+1;
80 char *Dest = new char[Size];
81 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +000082
Chris Lattner87f03102003-12-26 06:50:30 +000083 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
84 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000085
Chris Lattner87f03102003-12-26 06:50:30 +000086 // Endian safe: Result[i] = (PointerTy)Dest;
87 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
88 SBytePtr);
89 }
90
91 // Null terminate it
92 EE->StoreValueToMemory(PTOGV(0),
93 (GenericValue*)(Result+InputArgv.size()*PtrSize),
94 SBytePtr);
95 return Result;
96}
97
98/// runFunctionAsMain - This is a helper function which wraps runFunction to
99/// handle the common task of starting up main with the specified argc, argv,
100/// and envp parameters.
101int ExecutionEngine::runFunctionAsMain(Function *Fn,
102 const std::vector<std::string> &argv,
103 const char * const * envp) {
104 std::vector<GenericValue> GVArgs;
105 GenericValue GVArgc;
106 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000107 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
108 if (NumArgs) {
109 GVArgs.push_back(GVArgc); // Arg #0 = argc.
110 if (NumArgs > 1) {
111 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
112 assert(((char **)GVTOP(GVArgs[1]))[0] &&
113 "argv[0] was null after CreateArgv");
114 if (NumArgs > 2) {
115 std::vector<std::string> EnvVars;
116 for (unsigned i = 0; envp[i]; ++i)
117 EnvVars.push_back(envp[i]);
118 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
119 }
120 }
121 }
Chris Lattner87f03102003-12-26 06:50:30 +0000122 return runFunction(Fn, GVArgs).IntVal;
123}
124
125
126
Misha Brukman19684162003-10-16 21:18:05 +0000127/// If possible, create a JIT, unless the caller specifically requests an
128/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000129/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000130///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000132 bool ForceInterpreter,
133 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000134 ExecutionEngine *EE = 0;
135
Chris Lattner73011782003-12-28 09:44:37 +0000136 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000137 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000138 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000139
140 // If we can't make a JIT, make an interpreter instead.
Chris Lattner338733f2004-02-01 01:07:25 +0000141 if (EE == 0) {
142 try {
143 Module *M = MP->materializeModule();
144 try {
145 EE = Interpreter::create(M, IL);
146 } catch (...) {
147 std::cerr << "Error creating the interpreter!\n";
148 }
Reid Spencere2947532004-07-07 21:01:38 +0000149 } catch (std::string& errmsg) {
150 std::cerr << "Error reading the bytecode file: " << errmsg << "\n";
Chris Lattner338733f2004-02-01 01:07:25 +0000151 } catch (...) {
152 std::cerr << "Error reading the bytecode file!\n";
153 }
Misha Brukman19684162003-10-16 21:18:05 +0000154 }
Chris Lattner73011782003-12-28 09:44:37 +0000155
Misha Brukmanedf128a2005-04-21 22:36:52 +0000156 if (EE == 0)
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000157 delete IL;
158 else
Misha Brukmanedf128a2005-04-21 22:36:52 +0000159 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000160 // to the function tells DynamicLibrary to load the program, not a library.
161 sys::DynamicLibrary::LoadLibraryPermanently(0);
162
Brian Gaeke82d82772003-09-03 20:34:19 +0000163 return EE;
164}
165
Misha Brukman4afac182003-10-10 17:45:12 +0000166/// getPointerToGlobal - This returns the address of the specified global
167/// value. This may involve code generation if it's a function.
168///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000169void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000170 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000171 return getPointerToFunction(F);
172
Reid Spenceree448632005-07-12 15:51:55 +0000173 MutexGuard locked(lock);
174 assert(state.getGlobalAddressMap(locked)[GV] && "Global hasn't had an address allocated yet?");
175 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000176}
177
Misha Brukman4afac182003-10-10 17:45:12 +0000178/// FIXME: document
Misha Brukmanedf128a2005-04-21 22:36:52 +0000179///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000180GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
181 GenericValue Result;
Chris Lattner6f335f92004-10-26 05:35:14 +0000182 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000183
Chris Lattner9a231222003-05-14 17:51:49 +0000184 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000185 switch (CE->getOpcode()) {
186 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000187 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000188 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
189 uint64_t Offset =
190 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000191
Chris Lattner3db4b622005-10-23 23:54:56 +0000192 if (getTargetData().getPointerSize() == 4)
193 Result.IntVal += Offset;
194 else
195 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000196 return Result;
197 }
Chris Lattner9a231222003-05-14 17:51:49 +0000198 case Instruction::Cast: {
199 // We only need to handle a few cases here. Almost all casts will
200 // automatically fold, just the ones involving pointers won't.
201 //
202 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000203 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000204
Chris Lattner9a231222003-05-14 17:51:49 +0000205 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000206 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000207 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000208
Chris Lattner74cf8192003-08-18 17:23:40 +0000209 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000210 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000211 return GV;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000212
Chris Lattner7d1bd332004-03-16 08:38:56 +0000213 // Handle cast of integer to a pointer...
214 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000215 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000216 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
217 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
218 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
219 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
220 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
221 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
222 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
223 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
224 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
225 default: assert(0 && "Unknown integral type!");
226 }
Chris Lattner9a231222003-05-14 17:51:49 +0000227 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000228 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000229
Chris Lattner9a231222003-05-14 17:51:49 +0000230 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000231 switch (CE->getOperand(0)->getType()->getTypeID()) {
232 default: assert(0 && "Bad add type!"); abort();
233 case Type::LongTyID:
234 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000235 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
236 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000237 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000238 case Type::IntTyID:
239 case Type::UIntTyID:
240 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
241 getConstantValue(CE->getOperand(1)).IntVal;
242 break;
243 case Type::ShortTyID:
244 case Type::UShortTyID:
245 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
246 getConstantValue(CE->getOperand(1)).ShortVal;
247 break;
248 case Type::SByteTyID:
249 case Type::UByteTyID:
250 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
251 getConstantValue(CE->getOperand(1)).SByteVal;
252 break;
253 case Type::FloatTyID:
254 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
255 getConstantValue(CE->getOperand(1)).FloatVal;
256 break;
257 case Type::DoubleTyID:
258 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
259 getConstantValue(CE->getOperand(1)).DoubleVal;
260 break;
261 }
Chris Lattner9a231222003-05-14 17:51:49 +0000262 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000263 default:
264 break;
265 }
266 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
267 abort();
268 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000270 switch (C->getType()->getTypeID()) {
Chris Lattner813c8152005-01-08 20:13:19 +0000271#define GET_CONST_VAL(TY, CTY, CLASS) \
272 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break
273 GET_CONST_VAL(Bool , bool , ConstantBool);
274 GET_CONST_VAL(UByte , unsigned char , ConstantUInt);
275 GET_CONST_VAL(SByte , signed char , ConstantSInt);
276 GET_CONST_VAL(UShort , unsigned short, ConstantUInt);
277 GET_CONST_VAL(Short , signed short , ConstantSInt);
278 GET_CONST_VAL(UInt , unsigned int , ConstantUInt);
279 GET_CONST_VAL(Int , signed int , ConstantSInt);
Chris Lattner4b3141d2005-05-12 06:01:28 +0000280 GET_CONST_VAL(ULong , uint64_t , ConstantUInt);
281 GET_CONST_VAL(Long , int64_t , ConstantSInt);
Chris Lattner813c8152005-01-08 20:13:19 +0000282 GET_CONST_VAL(Float , float , ConstantFP);
283 GET_CONST_VAL(Double , double , ConstantFP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000284#undef GET_CONST_VAL
285 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000286 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000287 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000288 else if (const Function *F = dyn_cast<Function>(C))
289 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
290 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
291 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
292 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000293 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000294 break;
295 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000296 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000297 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000298 }
299 return Result;
300}
301
Misha Brukman4afac182003-10-10 17:45:12 +0000302/// FIXME: document
303///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000304void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000305 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000306 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000307 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000308 case Type::BoolTyID:
309 case Type::UByteTyID:
310 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
311 case Type::UShortTyID:
312 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
313 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
314 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000315 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000316 case Type::FloatTyID:
317 case Type::UIntTyID:
318 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
319 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
320 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
321 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
322 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000323 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000324 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000325 case Type::DoubleTyID:
326 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000327 case Type::LongTyID:
328 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
329 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
330 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
331 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
332 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
333 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
334 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
335 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
336 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000337 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000338 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000339 }
340 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000341 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000342 case Type::BoolTyID:
343 case Type::UByteTyID:
344 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
345 case Type::UShortTyID:
346 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
347 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
348 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000349 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000350 case Type::FloatTyID:
351 case Type::UIntTyID:
352 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
353 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
354 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
355 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
356 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000357 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000358 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000359 case Type::DoubleTyID:
360 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000361 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000362 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000363 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
364 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
365 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
366 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
367 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
368 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
369 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
370 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000371 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000372 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000373 }
374 }
375}
376
Misha Brukman4afac182003-10-10 17:45:12 +0000377/// FIXME: document
378///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000379GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
380 const Type *Ty) {
381 GenericValue Result;
382 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000383 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000384 case Type::BoolTyID:
385 case Type::UByteTyID:
386 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
387 case Type::UShortTyID:
388 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
389 ((unsigned)Ptr->Untyped[1] << 8);
390 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000391 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000392 case Type::FloatTyID:
393 case Type::UIntTyID:
394 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
395 ((unsigned)Ptr->Untyped[1] << 8) |
396 ((unsigned)Ptr->Untyped[2] << 16) |
397 ((unsigned)Ptr->Untyped[3] << 24);
398 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000399 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000400 goto Load4BytesLittleEndian;
401 case Type::DoubleTyID:
402 case Type::ULongTyID:
403 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
404 ((uint64_t)Ptr->Untyped[1] << 8) |
405 ((uint64_t)Ptr->Untyped[2] << 16) |
406 ((uint64_t)Ptr->Untyped[3] << 24) |
407 ((uint64_t)Ptr->Untyped[4] << 32) |
408 ((uint64_t)Ptr->Untyped[5] << 40) |
409 ((uint64_t)Ptr->Untyped[6] << 48) |
410 ((uint64_t)Ptr->Untyped[7] << 56);
411 break;
412 default:
413 std::cout << "Cannot load value of type " << *Ty << "!\n";
414 abort();
415 }
416 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000417 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000418 case Type::BoolTyID:
419 case Type::UByteTyID:
420 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
421 case Type::UShortTyID:
422 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
423 ((unsigned)Ptr->Untyped[0] << 8);
424 break;
425 Load4BytesBigEndian:
426 case Type::FloatTyID:
427 case Type::UIntTyID:
428 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
429 ((unsigned)Ptr->Untyped[2] << 8) |
430 ((unsigned)Ptr->Untyped[1] << 16) |
431 ((unsigned)Ptr->Untyped[0] << 24);
432 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000433 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000434 goto Load4BytesBigEndian;
435 case Type::DoubleTyID:
436 case Type::ULongTyID:
437 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
438 ((uint64_t)Ptr->Untyped[6] << 8) |
439 ((uint64_t)Ptr->Untyped[5] << 16) |
440 ((uint64_t)Ptr->Untyped[4] << 24) |
441 ((uint64_t)Ptr->Untyped[3] << 32) |
442 ((uint64_t)Ptr->Untyped[2] << 40) |
443 ((uint64_t)Ptr->Untyped[1] << 48) |
444 ((uint64_t)Ptr->Untyped[0] << 56);
445 break;
446 default:
447 std::cout << "Cannot load value of type " << *Ty << "!\n";
448 abort();
449 }
450 }
451 return Result;
452}
453
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000454// InitializeMemory - Recursive function to apply a Constant value into the
455// specified memory location...
456//
457void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000458 if (isa<UndefValue>(Init)) {
459 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000460 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
461 unsigned ElementSize =
462 getTargetData().getTypeSize(CP->getType()->getElementType());
463 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
464 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
465 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000466 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000467 GenericValue Val = getConstantValue(Init);
468 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
469 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000470 } else if (isa<ConstantAggregateZero>(Init)) {
Chris Lattner813c8152005-01-08 20:13:19 +0000471 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000472 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000473 }
474
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000475 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000476 case Type::ArrayTyID: {
477 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000478 unsigned ElementSize =
Chris Lattner6e630882005-07-11 02:49:16 +0000479 getTargetData().getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000480 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
481 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000482 return;
483 }
484
485 case Type::StructTyID: {
486 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
487 const StructLayout *SL =
488 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000489 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
490 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000491 return;
492 }
493
494 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000495 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000496 assert(0 && "Unknown constant type to initialize memory with!");
497 }
498}
499
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000500/// EmitGlobals - Emit all of the global variables to memory, storing their
501/// addresses into GlobalAddress. This must make sure to copy the contents of
502/// their initializers into the memory.
503///
504void ExecutionEngine::emitGlobals() {
505 const TargetData &TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000506
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000507 // Loop over all of the global variables in the program, allocating the memory
508 // to hold them.
Chris Lattner6e630882005-07-11 02:49:16 +0000509 Module &M = getModule();
510 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000511 I != E; ++I)
512 if (!I->isExternal()) {
513 // Get the type of the global...
514 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000515
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000516 // Allocate some memory for it!
517 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000518 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000519 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000520 // External variable reference. Try to use the dynamic loader to
521 // get a pointer to it.
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000522 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
523 I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000524 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000525 else {
526 std::cerr << "Could not resolve external global address: "
527 << I->getName() << "\n";
528 abort();
529 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000530 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000531
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000532 // Now that all of the globals are set up in memory, loop through them all and
533 // initialize their contents.
Chris Lattner6e630882005-07-11 02:49:16 +0000534 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000535 I != E; ++I)
536 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000537 EmitGlobalVariable(I);
538}
539
540// EmitGlobalVariable - This method emits the specified global variable to the
541// address specified in GlobalAddresses, or allocates new memory if it's not
542// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000543void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000544 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000545 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
546
Chris Lattnerc07ed132003-12-20 03:36:47 +0000547 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner813c8152005-01-08 20:13:19 +0000548 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000549 if (GA == 0) {
550 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000551 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000552 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000553 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000554
Chris Lattner24b0a182003-12-20 02:45:37 +0000555 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000556 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000557 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000558}