blob: ab56f592b0438fd4f4f398ebb1fb76be6680b68d [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 Lattnerbd199fb2002-12-24 00:01:05 +000016#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000017#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000019#include "llvm/ModuleProvider.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000020#include "llvm/ADT/Statistic.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000022#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000024#include "llvm/System/DynamicLibrary.h"
25#include "llvm/Target/TargetData.h"
Chris Lattner2fe4bb02006-03-22 06:07:50 +000026#include <iostream>
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000027using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000028
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000029namespace {
30 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000031 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000032}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000033
Chris Lattner2fe4bb02006-03-22 06:07:50 +000034ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
35ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
36
Misha Brukmanedf128a2005-04-21 22:36:52 +000037ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
Misha Brukman19684162003-10-16 21:18:05 +000038 CurMod(*P->getModule()), MP(P) {
39 assert(P && "ModuleProvider is null?");
40}
41
42ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000043 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000044}
45
Brian Gaeke8e539482003-09-04 22:57:27 +000046ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000047 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000048}
49
Chris Lattner55d86482003-12-31 20:21:04 +000050/// getGlobalValueAtAddress - Return the LLVM global value object that starts
51/// at the specified address.
52///
53const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +000054 MutexGuard locked(lock);
55
Chris Lattner55d86482003-12-31 20:21:04 +000056 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +000057 if (state.getGlobalAddressReverseMap(locked).empty()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +000058 for (std::map<const GlobalValue*, void *>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000059 state.getGlobalAddressMap(locked).begin(), E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
60 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +000061 }
62
63 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000064 state.getGlobalAddressReverseMap(locked).find(Addr);
65 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +000066}
Chris Lattner87f03102003-12-26 06:50:30 +000067
68// CreateArgv - Turn a vector of strings into a nice argv style array of
69// pointers to null terminated strings.
70//
71static void *CreateArgv(ExecutionEngine *EE,
72 const std::vector<std::string> &InputArgv) {
73 unsigned PtrSize = EE->getTargetData().getPointerSize();
74 char *Result = new char[(InputArgv.size()+1)*PtrSize];
75
76 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
77 const Type *SBytePtr = PointerType::get(Type::SByteTy);
78
79 for (unsigned i = 0; i != InputArgv.size(); ++i) {
80 unsigned Size = InputArgv[i].size()+1;
81 char *Dest = new char[Size];
82 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +000083
Chris Lattner87f03102003-12-26 06:50:30 +000084 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
85 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000086
Chris Lattner87f03102003-12-26 06:50:30 +000087 // Endian safe: Result[i] = (PointerTy)Dest;
88 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
89 SBytePtr);
90 }
91
92 // Null terminate it
93 EE->StoreValueToMemory(PTOGV(0),
94 (GenericValue*)(Result+InputArgv.size()*PtrSize),
95 SBytePtr);
96 return Result;
97}
98
Chris Lattner9ca6cda2006-03-08 18:42:46 +000099
100/// runStaticConstructorsDestructors - This method is used to execute all of
101/// the static constructors or destructors for a module, depending on the
102/// value of isDtors.
103void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
104 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
105 GlobalVariable *GV = CurMod.getNamedGlobal(Name);
106 if (!GV || GV->isExternal() || !GV->hasInternalLinkage()) return;
107
108 // Should be an array of '{ int, void ()* }' structs. The first value is the
109 // init priority, which we ignore.
110 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
111 if (!InitList) return;
112 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
113 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
114 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
115
116 Constant *FP = CS->getOperand(1);
117 if (FP->isNullValue())
118 return; // Found a null terminator, exit.
119
120 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
121 if (CE->getOpcode() == Instruction::Cast)
122 FP = CE->getOperand(0);
123 if (Function *F = dyn_cast<Function>(FP)) {
124 // Execute the ctor/dtor function!
125 runFunction(F, std::vector<GenericValue>());
126 }
127 }
128}
129
Chris Lattner87f03102003-12-26 06:50:30 +0000130/// runFunctionAsMain - This is a helper function which wraps runFunction to
131/// handle the common task of starting up main with the specified argc, argv,
132/// and envp parameters.
133int ExecutionEngine::runFunctionAsMain(Function *Fn,
134 const std::vector<std::string> &argv,
135 const char * const * envp) {
136 std::vector<GenericValue> GVArgs;
137 GenericValue GVArgc;
138 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000139 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
140 if (NumArgs) {
141 GVArgs.push_back(GVArgc); // Arg #0 = argc.
142 if (NumArgs > 1) {
143 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
144 assert(((char **)GVTOP(GVArgs[1]))[0] &&
145 "argv[0] was null after CreateArgv");
146 if (NumArgs > 2) {
147 std::vector<std::string> EnvVars;
148 for (unsigned i = 0; envp[i]; ++i)
149 EnvVars.push_back(envp[i]);
150 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
151 }
152 }
153 }
Chris Lattner87f03102003-12-26 06:50:30 +0000154 return runFunction(Fn, GVArgs).IntVal;
155}
156
Misha Brukman19684162003-10-16 21:18:05 +0000157/// If possible, create a JIT, unless the caller specifically requests an
158/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000159/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000160///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000161ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner726c1ef2006-03-23 05:22:51 +0000162 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000163 ExecutionEngine *EE = 0;
164
Chris Lattner73011782003-12-28 09:44:37 +0000165 // Unless the interpreter was explicitly selected, try making a JIT.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000166 if (!ForceInterpreter && JITCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000167 EE = JITCtor(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +0000168
169 // If we can't make a JIT, make an interpreter instead.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000170 if (EE == 0 && InterpCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000171 EE = InterpCtor(MP);
Chris Lattner73011782003-12-28 09:44:37 +0000172
Chris Lattner726c1ef2006-03-23 05:22:51 +0000173 if (EE) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000174 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000175 // to the function tells DynamicLibrary to load the program, not a library.
176 sys::DynamicLibrary::LoadLibraryPermanently(0);
Chris Lattner726c1ef2006-03-23 05:22:51 +0000177 }
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000178
Brian Gaeke82d82772003-09-03 20:34:19 +0000179 return EE;
180}
181
Misha Brukman4afac182003-10-10 17:45:12 +0000182/// getPointerToGlobal - This returns the address of the specified global
183/// value. This may involve code generation if it's a function.
184///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000185void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000186 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000187 return getPointerToFunction(F);
188
Reid Spenceree448632005-07-12 15:51:55 +0000189 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000190 void *p = state.getGlobalAddressMap(locked)[GV];
191 if (p)
192 return p;
193
194 // Global variable might have been added since interpreter started.
195 if (GlobalVariable *GVar =
196 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
197 EmitGlobalVariable(GVar);
198 else
199 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000200 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000201}
202
Misha Brukman4afac182003-10-10 17:45:12 +0000203/// FIXME: document
Misha Brukmanedf128a2005-04-21 22:36:52 +0000204///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000205GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
206 GenericValue Result;
Chris Lattner6f335f92004-10-26 05:35:14 +0000207 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000208
Chris Lattner9a231222003-05-14 17:51:49 +0000209 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000210 switch (CE->getOpcode()) {
211 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000212 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000213 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
214 uint64_t Offset =
215 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000216
Chris Lattner3db4b622005-10-23 23:54:56 +0000217 if (getTargetData().getPointerSize() == 4)
218 Result.IntVal += Offset;
219 else
220 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000221 return Result;
222 }
Chris Lattner9a231222003-05-14 17:51:49 +0000223 case Instruction::Cast: {
224 // We only need to handle a few cases here. Almost all casts will
225 // automatically fold, just the ones involving pointers won't.
226 //
227 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000228 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000229
Chris Lattner9a231222003-05-14 17:51:49 +0000230 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000231 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000232 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000233
Chris Lattner74cf8192003-08-18 17:23:40 +0000234 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000235 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000236 return GV;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000237
Chris Lattner7d1bd332004-03-16 08:38:56 +0000238 // Handle cast of integer to a pointer...
239 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000240 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000241 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
242 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
243 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
244 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
245 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
246 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
247 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
248 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
249 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
250 default: assert(0 && "Unknown integral type!");
251 }
Chris Lattner9a231222003-05-14 17:51:49 +0000252 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000253 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000254
Chris Lattner9a231222003-05-14 17:51:49 +0000255 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000256 switch (CE->getOperand(0)->getType()->getTypeID()) {
257 default: assert(0 && "Bad add type!"); abort();
258 case Type::LongTyID:
259 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000260 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
261 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000262 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000263 case Type::IntTyID:
264 case Type::UIntTyID:
265 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
266 getConstantValue(CE->getOperand(1)).IntVal;
267 break;
268 case Type::ShortTyID:
269 case Type::UShortTyID:
270 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
271 getConstantValue(CE->getOperand(1)).ShortVal;
272 break;
273 case Type::SByteTyID:
274 case Type::UByteTyID:
275 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
276 getConstantValue(CE->getOperand(1)).SByteVal;
277 break;
278 case Type::FloatTyID:
279 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
280 getConstantValue(CE->getOperand(1)).FloatVal;
281 break;
282 case Type::DoubleTyID:
283 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
284 getConstantValue(CE->getOperand(1)).DoubleVal;
285 break;
286 }
Chris Lattner9a231222003-05-14 17:51:49 +0000287 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000288 default:
289 break;
290 }
291 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
292 abort();
293 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000294
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000295 switch (C->getType()->getTypeID()) {
Chris Lattner813c8152005-01-08 20:13:19 +0000296#define GET_CONST_VAL(TY, CTY, CLASS) \
297 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break
298 GET_CONST_VAL(Bool , bool , ConstantBool);
299 GET_CONST_VAL(UByte , unsigned char , ConstantUInt);
300 GET_CONST_VAL(SByte , signed char , ConstantSInt);
301 GET_CONST_VAL(UShort , unsigned short, ConstantUInt);
302 GET_CONST_VAL(Short , signed short , ConstantSInt);
303 GET_CONST_VAL(UInt , unsigned int , ConstantUInt);
304 GET_CONST_VAL(Int , signed int , ConstantSInt);
Chris Lattner4b3141d2005-05-12 06:01:28 +0000305 GET_CONST_VAL(ULong , uint64_t , ConstantUInt);
306 GET_CONST_VAL(Long , int64_t , ConstantSInt);
Chris Lattner813c8152005-01-08 20:13:19 +0000307 GET_CONST_VAL(Float , float , ConstantFP);
308 GET_CONST_VAL(Double , double , ConstantFP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000309#undef GET_CONST_VAL
310 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000311 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000312 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000313 else if (const Function *F = dyn_cast<Function>(C))
314 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
315 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
316 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
317 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000318 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000319 break;
320 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000321 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000322 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000323 }
324 return Result;
325}
326
Misha Brukman4afac182003-10-10 17:45:12 +0000327/// FIXME: document
328///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000329void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000330 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000331 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000332 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000333 case Type::BoolTyID:
334 case Type::UByteTyID:
335 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
336 case Type::UShortTyID:
337 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
338 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
339 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000340 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000341 case Type::FloatTyID:
342 case Type::UIntTyID:
343 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
344 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
345 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
346 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
347 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000348 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000349 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000350 case Type::DoubleTyID:
351 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000352 case Type::LongTyID:
353 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
354 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
355 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
356 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
357 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
358 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
359 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
360 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
361 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000362 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000363 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000364 }
365 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000366 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000367 case Type::BoolTyID:
368 case Type::UByteTyID:
369 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
370 case Type::UShortTyID:
371 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
372 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
373 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000374 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000375 case Type::FloatTyID:
376 case Type::UIntTyID:
377 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
378 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
379 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
380 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
381 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000382 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000383 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000384 case Type::DoubleTyID:
385 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000386 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000387 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000388 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
389 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
390 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
391 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
392 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
393 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
394 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
395 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000396 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000397 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000398 }
399 }
400}
401
Misha Brukman4afac182003-10-10 17:45:12 +0000402/// FIXME: document
403///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000404GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
405 const Type *Ty) {
406 GenericValue Result;
407 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000408 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000409 case Type::BoolTyID:
410 case Type::UByteTyID:
411 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
412 case Type::UShortTyID:
413 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
414 ((unsigned)Ptr->Untyped[1] << 8);
415 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000416 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000417 case Type::FloatTyID:
418 case Type::UIntTyID:
419 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
420 ((unsigned)Ptr->Untyped[1] << 8) |
421 ((unsigned)Ptr->Untyped[2] << 16) |
422 ((unsigned)Ptr->Untyped[3] << 24);
423 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000424 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000425 goto Load4BytesLittleEndian;
426 case Type::DoubleTyID:
427 case Type::ULongTyID:
428 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
429 ((uint64_t)Ptr->Untyped[1] << 8) |
430 ((uint64_t)Ptr->Untyped[2] << 16) |
431 ((uint64_t)Ptr->Untyped[3] << 24) |
432 ((uint64_t)Ptr->Untyped[4] << 32) |
433 ((uint64_t)Ptr->Untyped[5] << 40) |
434 ((uint64_t)Ptr->Untyped[6] << 48) |
435 ((uint64_t)Ptr->Untyped[7] << 56);
436 break;
437 default:
438 std::cout << "Cannot load value of type " << *Ty << "!\n";
439 abort();
440 }
441 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000442 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000443 case Type::BoolTyID:
444 case Type::UByteTyID:
445 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
446 case Type::UShortTyID:
447 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
448 ((unsigned)Ptr->Untyped[0] << 8);
449 break;
450 Load4BytesBigEndian:
451 case Type::FloatTyID:
452 case Type::UIntTyID:
453 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
454 ((unsigned)Ptr->Untyped[2] << 8) |
455 ((unsigned)Ptr->Untyped[1] << 16) |
456 ((unsigned)Ptr->Untyped[0] << 24);
457 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000458 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000459 goto Load4BytesBigEndian;
460 case Type::DoubleTyID:
461 case Type::ULongTyID:
462 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
463 ((uint64_t)Ptr->Untyped[6] << 8) |
464 ((uint64_t)Ptr->Untyped[5] << 16) |
465 ((uint64_t)Ptr->Untyped[4] << 24) |
466 ((uint64_t)Ptr->Untyped[3] << 32) |
467 ((uint64_t)Ptr->Untyped[2] << 40) |
468 ((uint64_t)Ptr->Untyped[1] << 48) |
469 ((uint64_t)Ptr->Untyped[0] << 56);
470 break;
471 default:
472 std::cout << "Cannot load value of type " << *Ty << "!\n";
473 abort();
474 }
475 }
476 return Result;
477}
478
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000479// InitializeMemory - Recursive function to apply a Constant value into the
480// specified memory location...
481//
482void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000483 if (isa<UndefValue>(Init)) {
484 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000485 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
486 unsigned ElementSize =
487 getTargetData().getTypeSize(CP->getType()->getElementType());
488 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
489 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
490 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000491 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000492 GenericValue Val = getConstantValue(Init);
493 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
494 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000495 } else if (isa<ConstantAggregateZero>(Init)) {
Chris Lattner813c8152005-01-08 20:13:19 +0000496 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000497 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000498 }
499
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000500 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000501 case Type::ArrayTyID: {
502 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000503 unsigned ElementSize =
Chris Lattner6e630882005-07-11 02:49:16 +0000504 getTargetData().getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000505 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
506 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000507 return;
508 }
509
510 case Type::StructTyID: {
511 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
512 const StructLayout *SL =
513 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000514 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
515 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000516 return;
517 }
518
519 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000520 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000521 assert(0 && "Unknown constant type to initialize memory with!");
522 }
523}
524
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000525/// EmitGlobals - Emit all of the global variables to memory, storing their
526/// addresses into GlobalAddress. This must make sure to copy the contents of
527/// their initializers into the memory.
528///
529void ExecutionEngine::emitGlobals() {
530 const TargetData &TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000531
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000532 // Loop over all of the global variables in the program, allocating the memory
533 // to hold them.
Chris Lattner6e630882005-07-11 02:49:16 +0000534 Module &M = getModule();
535 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000536 I != E; ++I)
537 if (!I->isExternal()) {
538 // Get the type of the global...
539 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000540
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000541 // Allocate some memory for it!
542 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000543 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000544 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000545 // External variable reference. Try to use the dynamic loader to
546 // get a pointer to it.
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000547 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
548 I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000549 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000550 else {
551 std::cerr << "Could not resolve external global address: "
552 << I->getName() << "\n";
553 abort();
554 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000555 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000556
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000557 // Now that all of the globals are set up in memory, loop through them all and
558 // initialize their contents.
Chris Lattner6e630882005-07-11 02:49:16 +0000559 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000560 I != E; ++I)
561 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000562 EmitGlobalVariable(I);
563}
564
565// EmitGlobalVariable - This method emits the specified global variable to the
566// address specified in GlobalAddresses, or allocates new memory if it's not
567// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000568void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000569 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000570 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
571
Chris Lattnerc07ed132003-12-20 03:36:47 +0000572 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner813c8152005-01-08 20:13:19 +0000573 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000574 if (GA == 0) {
575 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000576 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000577 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000578 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000579
Chris Lattner24b0a182003-12-20 02:45:37 +0000580 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000581 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000582 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000583}