blob: 4ea62307113c638fe36d480b9cf641c5f1940c02 [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"
Chris Lattnere7fd5532006-05-08 22:00:52 +000024#include "llvm/Support/MutexGuard.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000025#include "llvm/System/DynamicLibrary.h"
26#include "llvm/Target/TargetData.h"
Chris Lattner2fe4bb02006-03-22 06:07:50 +000027#include <iostream>
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000028using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000029
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000030namespace {
31 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000032 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000033}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034
Chris Lattner2fe4bb02006-03-22 06:07:50 +000035ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
36ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
37
Misha Brukmanedf128a2005-04-21 22:36:52 +000038ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
Misha Brukman19684162003-10-16 21:18:05 +000039 CurMod(*P->getModule()), MP(P) {
40 assert(P && "ModuleProvider is null?");
41}
42
43ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000044 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000045}
46
Brian Gaeke8e539482003-09-04 22:57:27 +000047ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000048 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000049}
50
Chris Lattnere7fd5532006-05-08 22:00:52 +000051/// addGlobalMapping - Tell the execution engine that the specified global is
52/// at the specified location. This is used internally as functions are JIT'd
53/// and as global variables are laid out in memory. It can and should also be
54/// used by clients of the EE that want to have an LLVM global overlay
55/// existing data in memory.
56void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
57 MutexGuard locked(lock);
58
59 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
60 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
61 CurVal = Addr;
62
63 // If we are using the reverse mapping, add it too
64 if (!state.getGlobalAddressReverseMap(locked).empty()) {
65 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
66 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
67 V = GV;
68 }
69}
70
71/// clearAllGlobalMappings - Clear all global mappings and start over again
72/// use in dynamic compilation scenarios when you want to move globals
73void ExecutionEngine::clearAllGlobalMappings() {
74 MutexGuard locked(lock);
75
76 state.getGlobalAddressMap(locked).clear();
77 state.getGlobalAddressReverseMap(locked).clear();
78}
79
80/// updateGlobalMapping - Replace an existing mapping for GV with a new
81/// address. This updates both maps as required. If "Addr" is null, the
82/// entry for the global is removed from the mappings.
83void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
84 MutexGuard locked(lock);
85
86 // Deleting from the mapping?
87 if (Addr == 0) {
88 state.getGlobalAddressMap(locked).erase(GV);
89 if (!state.getGlobalAddressReverseMap(locked).empty())
90 state.getGlobalAddressReverseMap(locked).erase(Addr);
91 return;
92 }
93
94 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
95 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
96 state.getGlobalAddressReverseMap(locked).erase(CurVal);
97 CurVal = Addr;
98
99 // If we are using the reverse mapping, add it too
100 if (!state.getGlobalAddressReverseMap(locked).empty()) {
101 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
102 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
103 V = GV;
104 }
105}
106
107/// getPointerToGlobalIfAvailable - This returns the address of the specified
108/// global value if it is has already been codegen'd, otherwise it returns null.
109///
110void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
111 MutexGuard locked(lock);
112
113 std::map<const GlobalValue*, void*>::iterator I =
114 state.getGlobalAddressMap(locked).find(GV);
115 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
116}
117
Chris Lattner55d86482003-12-31 20:21:04 +0000118/// getGlobalValueAtAddress - Return the LLVM global value object that starts
119/// at the specified address.
120///
121const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000122 MutexGuard locked(lock);
123
Chris Lattner55d86482003-12-31 20:21:04 +0000124 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +0000125 if (state.getGlobalAddressReverseMap(locked).empty()) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000126 for (std::map<const GlobalValue*, void *>::iterator
127 I = state.getGlobalAddressMap(locked).begin(),
128 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
129 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
130 I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000131 }
132
133 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000134 state.getGlobalAddressReverseMap(locked).find(Addr);
135 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000136}
Chris Lattner87f03102003-12-26 06:50:30 +0000137
138// CreateArgv - Turn a vector of strings into a nice argv style array of
139// pointers to null terminated strings.
140//
141static void *CreateArgv(ExecutionEngine *EE,
142 const std::vector<std::string> &InputArgv) {
Owen Andersona69571c2006-05-03 01:29:57 +0000143 unsigned PtrSize = EE->getTargetData()->getPointerSize();
Chris Lattner87f03102003-12-26 06:50:30 +0000144 char *Result = new char[(InputArgv.size()+1)*PtrSize];
145
146 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
147 const Type *SBytePtr = PointerType::get(Type::SByteTy);
148
149 for (unsigned i = 0; i != InputArgv.size(); ++i) {
150 unsigned Size = InputArgv[i].size()+1;
151 char *Dest = new char[Size];
152 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000153
Chris Lattner87f03102003-12-26 06:50:30 +0000154 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
155 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000156
Chris Lattner87f03102003-12-26 06:50:30 +0000157 // Endian safe: Result[i] = (PointerTy)Dest;
158 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
159 SBytePtr);
160 }
161
162 // Null terminate it
163 EE->StoreValueToMemory(PTOGV(0),
164 (GenericValue*)(Result+InputArgv.size()*PtrSize),
165 SBytePtr);
166 return Result;
167}
168
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000169
170/// runStaticConstructorsDestructors - This method is used to execute all of
171/// the static constructors or destructors for a module, depending on the
172/// value of isDtors.
173void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
174 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
175 GlobalVariable *GV = CurMod.getNamedGlobal(Name);
Chris Lattnerb003d762006-04-22 05:02:46 +0000176
177 // If this global has internal linkage, or if it has a use, then it must be
178 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
179 // this is the case, don't execute any of the global ctors, __main will do it.
180 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) return;
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000181
182 // Should be an array of '{ int, void ()* }' structs. The first value is the
183 // init priority, which we ignore.
184 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
185 if (!InitList) return;
186 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
187 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
188 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
189
190 Constant *FP = CS->getOperand(1);
191 if (FP->isNullValue())
192 return; // Found a null terminator, exit.
193
194 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
195 if (CE->getOpcode() == Instruction::Cast)
196 FP = CE->getOperand(0);
197 if (Function *F = dyn_cast<Function>(FP)) {
198 // Execute the ctor/dtor function!
199 runFunction(F, std::vector<GenericValue>());
200 }
201 }
202}
203
Chris Lattner87f03102003-12-26 06:50:30 +0000204/// runFunctionAsMain - This is a helper function which wraps runFunction to
205/// handle the common task of starting up main with the specified argc, argv,
206/// and envp parameters.
207int ExecutionEngine::runFunctionAsMain(Function *Fn,
208 const std::vector<std::string> &argv,
209 const char * const * envp) {
210 std::vector<GenericValue> GVArgs;
211 GenericValue GVArgc;
212 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000213 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
214 if (NumArgs) {
215 GVArgs.push_back(GVArgc); // Arg #0 = argc.
216 if (NumArgs > 1) {
217 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
218 assert(((char **)GVTOP(GVArgs[1]))[0] &&
219 "argv[0] was null after CreateArgv");
220 if (NumArgs > 2) {
221 std::vector<std::string> EnvVars;
222 for (unsigned i = 0; envp[i]; ++i)
223 EnvVars.push_back(envp[i]);
224 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
225 }
226 }
227 }
Chris Lattner87f03102003-12-26 06:50:30 +0000228 return runFunction(Fn, GVArgs).IntVal;
229}
230
Misha Brukman19684162003-10-16 21:18:05 +0000231/// If possible, create a JIT, unless the caller specifically requests an
232/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000233/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000234///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000235ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner726c1ef2006-03-23 05:22:51 +0000236 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000237 ExecutionEngine *EE = 0;
238
Chris Lattner73011782003-12-28 09:44:37 +0000239 // Unless the interpreter was explicitly selected, try making a JIT.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000240 if (!ForceInterpreter && JITCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000241 EE = JITCtor(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +0000242
243 // If we can't make a JIT, make an interpreter instead.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000244 if (EE == 0 && InterpCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000245 EE = InterpCtor(MP);
Chris Lattner73011782003-12-28 09:44:37 +0000246
Chris Lattner726c1ef2006-03-23 05:22:51 +0000247 if (EE) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000249 // to the function tells DynamicLibrary to load the program, not a library.
Chris Lattner6f3ada52006-05-14 19:01:55 +0000250 try {
251 sys::DynamicLibrary::LoadLibraryPermanently(0);
252 } catch (...) {
253 }
Chris Lattner726c1ef2006-03-23 05:22:51 +0000254 }
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000255
Brian Gaeke82d82772003-09-03 20:34:19 +0000256 return EE;
257}
258
Misha Brukman4afac182003-10-10 17:45:12 +0000259/// getPointerToGlobal - This returns the address of the specified global
260/// value. This may involve code generation if it's a function.
261///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000262void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000263 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000264 return getPointerToFunction(F);
265
Reid Spenceree448632005-07-12 15:51:55 +0000266 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000267 void *p = state.getGlobalAddressMap(locked)[GV];
268 if (p)
269 return p;
270
271 // Global variable might have been added since interpreter started.
272 if (GlobalVariable *GVar =
273 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
274 EmitGlobalVariable(GVar);
275 else
276 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000277 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000278}
279
Misha Brukman4afac182003-10-10 17:45:12 +0000280/// FIXME: document
Misha Brukmanedf128a2005-04-21 22:36:52 +0000281///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000282GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
283 GenericValue Result;
Chris Lattner6f335f92004-10-26 05:35:14 +0000284 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000285
Chris Lattner9a231222003-05-14 17:51:49 +0000286 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000287 switch (CE->getOpcode()) {
288 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000289 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000290 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
291 uint64_t Offset =
292 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000293
Owen Andersona69571c2006-05-03 01:29:57 +0000294 if (getTargetData()->getPointerSize() == 4)
Chris Lattner3db4b622005-10-23 23:54:56 +0000295 Result.IntVal += Offset;
296 else
297 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000298 return Result;
299 }
Chris Lattner9a231222003-05-14 17:51:49 +0000300 case Instruction::Cast: {
301 // We only need to handle a few cases here. Almost all casts will
302 // automatically fold, just the ones involving pointers won't.
303 //
304 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000305 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000306
Chris Lattner9a231222003-05-14 17:51:49 +0000307 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000308 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000309 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000310
Chris Lattner74cf8192003-08-18 17:23:40 +0000311 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000312 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000313 return GV;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000314
Chris Lattner7d1bd332004-03-16 08:38:56 +0000315 // Handle cast of integer to a pointer...
316 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000317 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000318 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
319 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
320 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
321 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
322 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
323 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
324 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
325 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
326 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
327 default: assert(0 && "Unknown integral type!");
328 }
Chris Lattner9a231222003-05-14 17:51:49 +0000329 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000330 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000331
Chris Lattner9a231222003-05-14 17:51:49 +0000332 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000333 switch (CE->getOperand(0)->getType()->getTypeID()) {
334 default: assert(0 && "Bad add type!"); abort();
335 case Type::LongTyID:
336 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000337 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
338 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000339 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000340 case Type::IntTyID:
341 case Type::UIntTyID:
342 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
343 getConstantValue(CE->getOperand(1)).IntVal;
344 break;
345 case Type::ShortTyID:
346 case Type::UShortTyID:
347 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
348 getConstantValue(CE->getOperand(1)).ShortVal;
349 break;
350 case Type::SByteTyID:
351 case Type::UByteTyID:
352 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
353 getConstantValue(CE->getOperand(1)).SByteVal;
354 break;
355 case Type::FloatTyID:
356 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
357 getConstantValue(CE->getOperand(1)).FloatVal;
358 break;
359 case Type::DoubleTyID:
360 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
361 getConstantValue(CE->getOperand(1)).DoubleVal;
362 break;
363 }
Chris Lattner9a231222003-05-14 17:51:49 +0000364 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000365 default:
366 break;
367 }
368 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
369 abort();
370 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000371
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000372 switch (C->getType()->getTypeID()) {
Chris Lattner813c8152005-01-08 20:13:19 +0000373#define GET_CONST_VAL(TY, CTY, CLASS) \
374 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break
375 GET_CONST_VAL(Bool , bool , ConstantBool);
376 GET_CONST_VAL(UByte , unsigned char , ConstantUInt);
377 GET_CONST_VAL(SByte , signed char , ConstantSInt);
378 GET_CONST_VAL(UShort , unsigned short, ConstantUInt);
379 GET_CONST_VAL(Short , signed short , ConstantSInt);
380 GET_CONST_VAL(UInt , unsigned int , ConstantUInt);
381 GET_CONST_VAL(Int , signed int , ConstantSInt);
Chris Lattner4b3141d2005-05-12 06:01:28 +0000382 GET_CONST_VAL(ULong , uint64_t , ConstantUInt);
383 GET_CONST_VAL(Long , int64_t , ConstantSInt);
Chris Lattner813c8152005-01-08 20:13:19 +0000384 GET_CONST_VAL(Float , float , ConstantFP);
385 GET_CONST_VAL(Double , double , ConstantFP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000386#undef GET_CONST_VAL
387 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000388 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000389 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000390 else if (const Function *F = dyn_cast<Function>(C))
391 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
392 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
393 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
394 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000395 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000396 break;
397 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000398 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000399 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000400 }
401 return Result;
402}
403
Nate Begeman37efe672006-04-22 18:53:45 +0000404/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
405/// is the address of the memory at which to store Val, cast to GenericValue *.
406/// It is not a pointer to a GenericValue containing the address at which to
407/// store Val.
Misha Brukman4afac182003-10-10 17:45:12 +0000408///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000409void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000410 const Type *Ty) {
Owen Andersona69571c2006-05-03 01:29:57 +0000411 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000412 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000413 case Type::BoolTyID:
414 case Type::UByteTyID:
415 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
416 case Type::UShortTyID:
417 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
418 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
419 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000420 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000421 case Type::FloatTyID:
422 case Type::UIntTyID:
423 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
424 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
425 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
426 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
427 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000428 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000429 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000430 case Type::DoubleTyID:
431 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000432 case Type::LongTyID:
433 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
434 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
435 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
436 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
437 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
438 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
439 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
440 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
441 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000442 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000443 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000444 }
445 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000446 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000447 case Type::BoolTyID:
448 case Type::UByteTyID:
449 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
450 case Type::UShortTyID:
451 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
452 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
453 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000454 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000455 case Type::FloatTyID:
456 case Type::UIntTyID:
457 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
458 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
459 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
460 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
461 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000462 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000463 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000464 case Type::DoubleTyID:
465 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000466 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000467 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000468 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
469 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
470 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
471 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
472 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
473 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
474 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
475 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000476 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000477 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000478 }
479 }
480}
481
Misha Brukman4afac182003-10-10 17:45:12 +0000482/// FIXME: document
483///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000484GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
485 const Type *Ty) {
486 GenericValue Result;
Owen Andersona69571c2006-05-03 01:29:57 +0000487 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000488 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000489 case Type::BoolTyID:
490 case Type::UByteTyID:
491 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
492 case Type::UShortTyID:
493 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
494 ((unsigned)Ptr->Untyped[1] << 8);
495 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000496 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000497 case Type::FloatTyID:
498 case Type::UIntTyID:
499 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
500 ((unsigned)Ptr->Untyped[1] << 8) |
501 ((unsigned)Ptr->Untyped[2] << 16) |
502 ((unsigned)Ptr->Untyped[3] << 24);
503 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000504 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000505 goto Load4BytesLittleEndian;
506 case Type::DoubleTyID:
507 case Type::ULongTyID:
508 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
509 ((uint64_t)Ptr->Untyped[1] << 8) |
510 ((uint64_t)Ptr->Untyped[2] << 16) |
511 ((uint64_t)Ptr->Untyped[3] << 24) |
512 ((uint64_t)Ptr->Untyped[4] << 32) |
513 ((uint64_t)Ptr->Untyped[5] << 40) |
514 ((uint64_t)Ptr->Untyped[6] << 48) |
515 ((uint64_t)Ptr->Untyped[7] << 56);
516 break;
517 default:
518 std::cout << "Cannot load value of type " << *Ty << "!\n";
519 abort();
520 }
521 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000522 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000523 case Type::BoolTyID:
524 case Type::UByteTyID:
525 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
526 case Type::UShortTyID:
527 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
528 ((unsigned)Ptr->Untyped[0] << 8);
529 break;
530 Load4BytesBigEndian:
531 case Type::FloatTyID:
532 case Type::UIntTyID:
533 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
534 ((unsigned)Ptr->Untyped[2] << 8) |
535 ((unsigned)Ptr->Untyped[1] << 16) |
536 ((unsigned)Ptr->Untyped[0] << 24);
537 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000538 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000539 goto Load4BytesBigEndian;
540 case Type::DoubleTyID:
541 case Type::ULongTyID:
542 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
543 ((uint64_t)Ptr->Untyped[6] << 8) |
544 ((uint64_t)Ptr->Untyped[5] << 16) |
545 ((uint64_t)Ptr->Untyped[4] << 24) |
546 ((uint64_t)Ptr->Untyped[3] << 32) |
547 ((uint64_t)Ptr->Untyped[2] << 40) |
548 ((uint64_t)Ptr->Untyped[1] << 48) |
549 ((uint64_t)Ptr->Untyped[0] << 56);
550 break;
551 default:
552 std::cout << "Cannot load value of type " << *Ty << "!\n";
553 abort();
554 }
555 }
556 return Result;
557}
558
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000559// InitializeMemory - Recursive function to apply a Constant value into the
560// specified memory location...
561//
562void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000563 if (isa<UndefValue>(Init)) {
564 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000565 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
566 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000567 getTargetData()->getTypeSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000568 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
569 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
570 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000571 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000572 GenericValue Val = getConstantValue(Init);
573 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
574 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000575 } else if (isa<ConstantAggregateZero>(Init)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000576 memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000577 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000578 }
579
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000580 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000581 case Type::ArrayTyID: {
582 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000583 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000584 getTargetData()->getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000585 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
586 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000587 return;
588 }
589
590 case Type::StructTyID: {
591 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
592 const StructLayout *SL =
Owen Andersona69571c2006-05-03 01:29:57 +0000593 getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000594 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
595 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000596 return;
597 }
598
599 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000600 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000601 assert(0 && "Unknown constant type to initialize memory with!");
602 }
603}
604
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000605/// EmitGlobals - Emit all of the global variables to memory, storing their
606/// addresses into GlobalAddress. This must make sure to copy the contents of
607/// their initializers into the memory.
608///
609void ExecutionEngine::emitGlobals() {
Owen Andersona69571c2006-05-03 01:29:57 +0000610 const TargetData *TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000611
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000612 // Loop over all of the global variables in the program, allocating the memory
613 // to hold them.
Chris Lattner6e630882005-07-11 02:49:16 +0000614 Module &M = getModule();
615 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000616 I != E; ++I)
617 if (!I->isExternal()) {
618 // Get the type of the global...
619 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000620
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000621 // Allocate some memory for it!
Owen Andersona69571c2006-05-03 01:29:57 +0000622 unsigned Size = TD->getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000623 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000624 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000625 // External variable reference. Try to use the dynamic loader to
626 // get a pointer to it.
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000627 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
628 I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000629 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000630 else {
631 std::cerr << "Could not resolve external global address: "
632 << I->getName() << "\n";
633 abort();
634 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000635 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000636
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000637 // Now that all of the globals are set up in memory, loop through them all and
638 // initialize their contents.
Chris Lattner6e630882005-07-11 02:49:16 +0000639 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000640 I != E; ++I)
641 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000642 EmitGlobalVariable(I);
643}
644
645// EmitGlobalVariable - This method emits the specified global variable to the
646// address specified in GlobalAddresses, or allocates new memory if it's not
647// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000648void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000649 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000650 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
651
Chris Lattnerc07ed132003-12-20 03:36:47 +0000652 const Type *ElTy = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000653 size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000654 if (GA == 0) {
655 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000656 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000657 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000658 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000659
Chris Lattner24b0a182003-12-20 02:45:37 +0000660 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000661 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000662 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000663}