blob: ebe0254071056af42482779ec034eaed1e624718 [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"
Chris Lattner8f7f71b2004-06-20 07:46:18 +000021#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukman19684162003-10-16 21:18:05 +000022#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/Debug.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 Lattner55d86482003-12-31 20:21:04 +000051/// getGlobalValueAtAddress - Return the LLVM global value object that starts
52/// at the specified address.
53///
54const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +000055 MutexGuard locked(lock);
56
Chris Lattner55d86482003-12-31 20:21:04 +000057 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +000058 if (state.getGlobalAddressReverseMap(locked).empty()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +000059 for (std::map<const GlobalValue*, void *>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000060 state.getGlobalAddressMap(locked).begin(), E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
61 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +000062 }
63
64 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000065 state.getGlobalAddressReverseMap(locked).find(Addr);
66 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +000067}
Chris Lattner87f03102003-12-26 06:50:30 +000068
69// CreateArgv - Turn a vector of strings into a nice argv style array of
70// pointers to null terminated strings.
71//
72static void *CreateArgv(ExecutionEngine *EE,
73 const std::vector<std::string> &InputArgv) {
74 unsigned PtrSize = EE->getTargetData().getPointerSize();
75 char *Result = new char[(InputArgv.size()+1)*PtrSize];
76
77 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
78 const Type *SBytePtr = PointerType::get(Type::SByteTy);
79
80 for (unsigned i = 0; i != InputArgv.size(); ++i) {
81 unsigned Size = InputArgv[i].size()+1;
82 char *Dest = new char[Size];
83 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +000084
Chris Lattner87f03102003-12-26 06:50:30 +000085 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
86 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000087
Chris Lattner87f03102003-12-26 06:50:30 +000088 // Endian safe: Result[i] = (PointerTy)Dest;
89 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
90 SBytePtr);
91 }
92
93 // Null terminate it
94 EE->StoreValueToMemory(PTOGV(0),
95 (GenericValue*)(Result+InputArgv.size()*PtrSize),
96 SBytePtr);
97 return Result;
98}
99
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000100
101/// runStaticConstructorsDestructors - This method is used to execute all of
102/// the static constructors or destructors for a module, depending on the
103/// value of isDtors.
104void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
105 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
106 GlobalVariable *GV = CurMod.getNamedGlobal(Name);
107 if (!GV || GV->isExternal() || !GV->hasInternalLinkage()) return;
108
109 // Should be an array of '{ int, void ()* }' structs. The first value is the
110 // init priority, which we ignore.
111 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
112 if (!InitList) return;
113 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
114 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
115 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
116
117 Constant *FP = CS->getOperand(1);
118 if (FP->isNullValue())
119 return; // Found a null terminator, exit.
120
121 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
122 if (CE->getOpcode() == Instruction::Cast)
123 FP = CE->getOperand(0);
124 if (Function *F = dyn_cast<Function>(FP)) {
125 // Execute the ctor/dtor function!
126 runFunction(F, std::vector<GenericValue>());
127 }
128 }
129}
130
Chris Lattner87f03102003-12-26 06:50:30 +0000131/// runFunctionAsMain - This is a helper function which wraps runFunction to
132/// handle the common task of starting up main with the specified argc, argv,
133/// and envp parameters.
134int ExecutionEngine::runFunctionAsMain(Function *Fn,
135 const std::vector<std::string> &argv,
136 const char * const * envp) {
137 std::vector<GenericValue> GVArgs;
138 GenericValue GVArgc;
139 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000140 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
141 if (NumArgs) {
142 GVArgs.push_back(GVArgc); // Arg #0 = argc.
143 if (NumArgs > 1) {
144 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
145 assert(((char **)GVTOP(GVArgs[1]))[0] &&
146 "argv[0] was null after CreateArgv");
147 if (NumArgs > 2) {
148 std::vector<std::string> EnvVars;
149 for (unsigned i = 0; envp[i]; ++i)
150 EnvVars.push_back(envp[i]);
151 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
152 }
153 }
154 }
Chris Lattner87f03102003-12-26 06:50:30 +0000155 return runFunction(Fn, GVArgs).IntVal;
156}
157
Misha Brukman19684162003-10-16 21:18:05 +0000158/// If possible, create a JIT, unless the caller specifically requests an
159/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000160/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000161///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000162ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner726c1ef2006-03-23 05:22:51 +0000163 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000164 ExecutionEngine *EE = 0;
165
Chris Lattner73011782003-12-28 09:44:37 +0000166 // Unless the interpreter was explicitly selected, try making a JIT.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000167 if (!ForceInterpreter && JITCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000168 EE = JITCtor(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +0000169
170 // If we can't make a JIT, make an interpreter instead.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000171 if (EE == 0 && InterpCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000172 EE = InterpCtor(MP);
Chris Lattner73011782003-12-28 09:44:37 +0000173
Chris Lattner726c1ef2006-03-23 05:22:51 +0000174 if (EE) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000175 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000176 // to the function tells DynamicLibrary to load the program, not a library.
177 sys::DynamicLibrary::LoadLibraryPermanently(0);
Chris Lattner726c1ef2006-03-23 05:22:51 +0000178 }
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000179
Brian Gaeke82d82772003-09-03 20:34:19 +0000180 return EE;
181}
182
Misha Brukman4afac182003-10-10 17:45:12 +0000183/// getPointerToGlobal - This returns the address of the specified global
184/// value. This may involve code generation if it's a function.
185///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000186void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000187 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000188 return getPointerToFunction(F);
189
Reid Spenceree448632005-07-12 15:51:55 +0000190 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000191 void *p = state.getGlobalAddressMap(locked)[GV];
192 if (p)
193 return p;
194
195 // Global variable might have been added since interpreter started.
196 if (GlobalVariable *GVar =
197 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
198 EmitGlobalVariable(GVar);
199 else
200 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000201 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000202}
203
Misha Brukman4afac182003-10-10 17:45:12 +0000204/// FIXME: document
Misha Brukmanedf128a2005-04-21 22:36:52 +0000205///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000206GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
207 GenericValue Result;
Chris Lattner6f335f92004-10-26 05:35:14 +0000208 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000209
Chris Lattner9a231222003-05-14 17:51:49 +0000210 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000211 switch (CE->getOpcode()) {
212 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000213 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000214 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
215 uint64_t Offset =
216 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000217
Chris Lattner3db4b622005-10-23 23:54:56 +0000218 if (getTargetData().getPointerSize() == 4)
219 Result.IntVal += Offset;
220 else
221 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000222 return Result;
223 }
Chris Lattner9a231222003-05-14 17:51:49 +0000224 case Instruction::Cast: {
225 // We only need to handle a few cases here. Almost all casts will
226 // automatically fold, just the ones involving pointers won't.
227 //
228 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000229 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000230
Chris Lattner9a231222003-05-14 17:51:49 +0000231 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000232 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000233 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000234
Chris Lattner74cf8192003-08-18 17:23:40 +0000235 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000236 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000237 return GV;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238
Chris Lattner7d1bd332004-03-16 08:38:56 +0000239 // Handle cast of integer to a pointer...
240 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000241 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000242 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
243 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
244 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
245 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
246 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
247 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
248 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
249 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
250 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
251 default: assert(0 && "Unknown integral type!");
252 }
Chris Lattner9a231222003-05-14 17:51:49 +0000253 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000254 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000255
Chris Lattner9a231222003-05-14 17:51:49 +0000256 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000257 switch (CE->getOperand(0)->getType()->getTypeID()) {
258 default: assert(0 && "Bad add type!"); abort();
259 case Type::LongTyID:
260 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000261 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
262 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000263 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000264 case Type::IntTyID:
265 case Type::UIntTyID:
266 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
267 getConstantValue(CE->getOperand(1)).IntVal;
268 break;
269 case Type::ShortTyID:
270 case Type::UShortTyID:
271 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
272 getConstantValue(CE->getOperand(1)).ShortVal;
273 break;
274 case Type::SByteTyID:
275 case Type::UByteTyID:
276 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
277 getConstantValue(CE->getOperand(1)).SByteVal;
278 break;
279 case Type::FloatTyID:
280 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
281 getConstantValue(CE->getOperand(1)).FloatVal;
282 break;
283 case Type::DoubleTyID:
284 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
285 getConstantValue(CE->getOperand(1)).DoubleVal;
286 break;
287 }
Chris Lattner9a231222003-05-14 17:51:49 +0000288 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000289 default:
290 break;
291 }
292 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
293 abort();
294 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000295
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000296 switch (C->getType()->getTypeID()) {
Chris Lattner813c8152005-01-08 20:13:19 +0000297#define GET_CONST_VAL(TY, CTY, CLASS) \
298 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break
299 GET_CONST_VAL(Bool , bool , ConstantBool);
300 GET_CONST_VAL(UByte , unsigned char , ConstantUInt);
301 GET_CONST_VAL(SByte , signed char , ConstantSInt);
302 GET_CONST_VAL(UShort , unsigned short, ConstantUInt);
303 GET_CONST_VAL(Short , signed short , ConstantSInt);
304 GET_CONST_VAL(UInt , unsigned int , ConstantUInt);
305 GET_CONST_VAL(Int , signed int , ConstantSInt);
Chris Lattner4b3141d2005-05-12 06:01:28 +0000306 GET_CONST_VAL(ULong , uint64_t , ConstantUInt);
307 GET_CONST_VAL(Long , int64_t , ConstantSInt);
Chris Lattner813c8152005-01-08 20:13:19 +0000308 GET_CONST_VAL(Float , float , ConstantFP);
309 GET_CONST_VAL(Double , double , ConstantFP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000310#undef GET_CONST_VAL
311 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000312 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000313 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000314 else if (const Function *F = dyn_cast<Function>(C))
315 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
316 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
317 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
318 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000319 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000320 break;
321 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000322 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000323 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000324 }
325 return Result;
326}
327
Misha Brukman4afac182003-10-10 17:45:12 +0000328/// FIXME: document
329///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000330void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000331 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000332 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000333 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000334 case Type::BoolTyID:
335 case Type::UByteTyID:
336 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
337 case Type::UShortTyID:
338 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
339 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
340 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000341 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000342 case Type::FloatTyID:
343 case Type::UIntTyID:
344 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
345 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
346 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
347 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
348 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000349 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000350 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000351 case Type::DoubleTyID:
352 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000353 case Type::LongTyID:
354 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
355 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
356 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
357 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
358 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
359 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
360 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
361 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
362 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000363 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000364 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000365 }
366 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000367 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000368 case Type::BoolTyID:
369 case Type::UByteTyID:
370 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
371 case Type::UShortTyID:
372 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
373 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
374 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000375 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000376 case Type::FloatTyID:
377 case Type::UIntTyID:
378 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
379 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
380 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
381 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
382 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000383 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000384 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000385 case Type::DoubleTyID:
386 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000387 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000388 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000389 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
390 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
391 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
392 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
393 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
394 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
395 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
396 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000397 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000398 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000399 }
400 }
401}
402
Misha Brukman4afac182003-10-10 17:45:12 +0000403/// FIXME: document
404///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000405GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
406 const Type *Ty) {
407 GenericValue Result;
408 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000409 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000410 case Type::BoolTyID:
411 case Type::UByteTyID:
412 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
413 case Type::UShortTyID:
414 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
415 ((unsigned)Ptr->Untyped[1] << 8);
416 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000417 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000418 case Type::FloatTyID:
419 case Type::UIntTyID:
420 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
421 ((unsigned)Ptr->Untyped[1] << 8) |
422 ((unsigned)Ptr->Untyped[2] << 16) |
423 ((unsigned)Ptr->Untyped[3] << 24);
424 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000425 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000426 goto Load4BytesLittleEndian;
427 case Type::DoubleTyID:
428 case Type::ULongTyID:
429 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
430 ((uint64_t)Ptr->Untyped[1] << 8) |
431 ((uint64_t)Ptr->Untyped[2] << 16) |
432 ((uint64_t)Ptr->Untyped[3] << 24) |
433 ((uint64_t)Ptr->Untyped[4] << 32) |
434 ((uint64_t)Ptr->Untyped[5] << 40) |
435 ((uint64_t)Ptr->Untyped[6] << 48) |
436 ((uint64_t)Ptr->Untyped[7] << 56);
437 break;
438 default:
439 std::cout << "Cannot load value of type " << *Ty << "!\n";
440 abort();
441 }
442 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000443 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000444 case Type::BoolTyID:
445 case Type::UByteTyID:
446 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
447 case Type::UShortTyID:
448 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
449 ((unsigned)Ptr->Untyped[0] << 8);
450 break;
451 Load4BytesBigEndian:
452 case Type::FloatTyID:
453 case Type::UIntTyID:
454 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
455 ((unsigned)Ptr->Untyped[2] << 8) |
456 ((unsigned)Ptr->Untyped[1] << 16) |
457 ((unsigned)Ptr->Untyped[0] << 24);
458 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000459 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000460 goto Load4BytesBigEndian;
461 case Type::DoubleTyID:
462 case Type::ULongTyID:
463 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
464 ((uint64_t)Ptr->Untyped[6] << 8) |
465 ((uint64_t)Ptr->Untyped[5] << 16) |
466 ((uint64_t)Ptr->Untyped[4] << 24) |
467 ((uint64_t)Ptr->Untyped[3] << 32) |
468 ((uint64_t)Ptr->Untyped[2] << 40) |
469 ((uint64_t)Ptr->Untyped[1] << 48) |
470 ((uint64_t)Ptr->Untyped[0] << 56);
471 break;
472 default:
473 std::cout << "Cannot load value of type " << *Ty << "!\n";
474 abort();
475 }
476 }
477 return Result;
478}
479
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000480// InitializeMemory - Recursive function to apply a Constant value into the
481// specified memory location...
482//
483void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000484 if (isa<UndefValue>(Init)) {
485 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000486 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
487 unsigned ElementSize =
488 getTargetData().getTypeSize(CP->getType()->getElementType());
489 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
490 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
491 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000492 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000493 GenericValue Val = getConstantValue(Init);
494 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
495 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000496 } else if (isa<ConstantAggregateZero>(Init)) {
Chris Lattner813c8152005-01-08 20:13:19 +0000497 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000498 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000499 }
500
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000501 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000502 case Type::ArrayTyID: {
503 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000504 unsigned ElementSize =
Chris Lattner6e630882005-07-11 02:49:16 +0000505 getTargetData().getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000506 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
507 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000508 return;
509 }
510
511 case Type::StructTyID: {
512 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
513 const StructLayout *SL =
514 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000515 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
516 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000517 return;
518 }
519
520 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000521 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000522 assert(0 && "Unknown constant type to initialize memory with!");
523 }
524}
525
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000526/// EmitGlobals - Emit all of the global variables to memory, storing their
527/// addresses into GlobalAddress. This must make sure to copy the contents of
528/// their initializers into the memory.
529///
530void ExecutionEngine::emitGlobals() {
531 const TargetData &TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000532
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000533 // Loop over all of the global variables in the program, allocating the memory
534 // to hold them.
Chris Lattner6e630882005-07-11 02:49:16 +0000535 Module &M = getModule();
536 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000537 I != E; ++I)
538 if (!I->isExternal()) {
539 // Get the type of the global...
540 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000541
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000542 // Allocate some memory for it!
543 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000544 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000545 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000546 // External variable reference. Try to use the dynamic loader to
547 // get a pointer to it.
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000548 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
549 I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000550 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000551 else {
552 std::cerr << "Could not resolve external global address: "
553 << I->getName() << "\n";
554 abort();
555 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000556 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000557
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000558 // Now that all of the globals are set up in memory, loop through them all and
559 // initialize their contents.
Chris Lattner6e630882005-07-11 02:49:16 +0000560 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000561 I != E; ++I)
562 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000563 EmitGlobalVariable(I);
564}
565
566// EmitGlobalVariable - This method emits the specified global variable to the
567// address specified in GlobalAddresses, or allocates new memory if it's not
568// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000569void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000570 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000571 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
572
Chris Lattnerc07ed132003-12-20 03:36:47 +0000573 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner813c8152005-01-08 20:13:19 +0000574 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000575 if (GA == 0) {
576 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000577 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000578 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000579 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000580
Chris Lattner24b0a182003-12-20 02:45:37 +0000581 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000582 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000583 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000584}