blob: 95610e65b60d4545e54f2dd00a028da1b980de53 [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "Interpreter/Interpreter.h"
Chris Lattner61612df2003-12-20 01:45:17 +000017#include "JIT/JIT.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000020#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ModuleProvider.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner8f7f71b2004-06-20 07:46:18 +000023#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukman19684162003-10-16 21:18:05 +000024#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000025#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000027#include "llvm/System/DynamicLibrary.h"
28#include "llvm/Target/TargetData.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000029using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000031namespace {
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000033 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000034}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
Misha Brukmanedf128a2005-04-21 22:36:52 +000036ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
Misha Brukman19684162003-10-16 21:18:05 +000037 CurMod(*P->getModule()), MP(P) {
38 assert(P && "ModuleProvider is null?");
39}
40
41ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000042 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000043}
44
Brian Gaeke8e539482003-09-04 22:57:27 +000045ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000046 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000047}
48
Chris Lattner55d86482003-12-31 20:21:04 +000049/// getGlobalValueAtAddress - Return the LLVM global value object that starts
50/// at the specified address.
51///
52const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +000053 MutexGuard locked(lock);
54
Chris Lattner55d86482003-12-31 20:21:04 +000055 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +000056 if (state.getGlobalAddressReverseMap(locked).empty()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +000057 for (std::map<const GlobalValue*, void *>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000058 state.getGlobalAddressMap(locked).begin(), E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
59 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +000060 }
61
62 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +000063 state.getGlobalAddressReverseMap(locked).find(Addr);
64 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +000065}
Chris Lattner87f03102003-12-26 06:50:30 +000066
67// CreateArgv - Turn a vector of strings into a nice argv style array of
68// pointers to null terminated strings.
69//
70static void *CreateArgv(ExecutionEngine *EE,
71 const std::vector<std::string> &InputArgv) {
72 unsigned PtrSize = EE->getTargetData().getPointerSize();
73 char *Result = new char[(InputArgv.size()+1)*PtrSize];
74
75 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
76 const Type *SBytePtr = PointerType::get(Type::SByteTy);
77
78 for (unsigned i = 0; i != InputArgv.size(); ++i) {
79 unsigned Size = InputArgv[i].size()+1;
80 char *Dest = new char[Size];
81 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +000082
Chris Lattner87f03102003-12-26 06:50:30 +000083 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
84 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000085
Chris Lattner87f03102003-12-26 06:50:30 +000086 // Endian safe: Result[i] = (PointerTy)Dest;
87 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
88 SBytePtr);
89 }
90
91 // Null terminate it
92 EE->StoreValueToMemory(PTOGV(0),
93 (GenericValue*)(Result+InputArgv.size()*PtrSize),
94 SBytePtr);
95 return Result;
96}
97
98/// runFunctionAsMain - This is a helper function which wraps runFunction to
99/// handle the common task of starting up main with the specified argc, argv,
100/// and envp parameters.
101int ExecutionEngine::runFunctionAsMain(Function *Fn,
102 const std::vector<std::string> &argv,
103 const char * const * envp) {
104 std::vector<GenericValue> GVArgs;
105 GenericValue GVArgc;
106 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000107 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
108 if (NumArgs) {
109 GVArgs.push_back(GVArgc); // Arg #0 = argc.
110 if (NumArgs > 1) {
111 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
112 assert(((char **)GVTOP(GVArgs[1]))[0] &&
113 "argv[0] was null after CreateArgv");
114 if (NumArgs > 2) {
115 std::vector<std::string> EnvVars;
116 for (unsigned i = 0; envp[i]; ++i)
117 EnvVars.push_back(envp[i]);
118 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
119 }
120 }
121 }
Chris Lattner87f03102003-12-26 06:50:30 +0000122 return runFunction(Fn, GVArgs).IntVal;
123}
124
125
126
Misha Brukman19684162003-10-16 21:18:05 +0000127/// If possible, create a JIT, unless the caller specifically requests an
128/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000129/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000130///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000132 bool ForceInterpreter,
133 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000134 ExecutionEngine *EE = 0;
135
Chris Lattner73011782003-12-28 09:44:37 +0000136 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000137 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000138 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000139
140 // If we can't make a JIT, make an interpreter instead.
Chris Lattner338733f2004-02-01 01:07:25 +0000141 if (EE == 0) {
142 try {
143 Module *M = MP->materializeModule();
144 try {
145 EE = Interpreter::create(M, IL);
146 } catch (...) {
147 std::cerr << "Error creating the interpreter!\n";
148 }
Reid Spencere2947532004-07-07 21:01:38 +0000149 } catch (std::string& errmsg) {
150 std::cerr << "Error reading the bytecode file: " << errmsg << "\n";
Chris Lattner338733f2004-02-01 01:07:25 +0000151 } catch (...) {
152 std::cerr << "Error reading the bytecode file!\n";
153 }
Misha Brukman19684162003-10-16 21:18:05 +0000154 }
Chris Lattner73011782003-12-28 09:44:37 +0000155
Misha Brukmanedf128a2005-04-21 22:36:52 +0000156 if (EE == 0)
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000157 delete IL;
158 else
Misha Brukmanedf128a2005-04-21 22:36:52 +0000159 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000160 // to the function tells DynamicLibrary to load the program, not a library.
161 sys::DynamicLibrary::LoadLibraryPermanently(0);
162
Brian Gaeke82d82772003-09-03 20:34:19 +0000163 return EE;
164}
165
Misha Brukman4afac182003-10-10 17:45:12 +0000166/// getPointerToGlobal - This returns the address of the specified global
167/// value. This may involve code generation if it's a function.
168///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000169void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000170 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000171 return getPointerToFunction(F);
172
Reid Spenceree448632005-07-12 15:51:55 +0000173 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000174 void *p = state.getGlobalAddressMap(locked)[GV];
175 if (p)
176 return p;
177
178 // Global variable might have been added since interpreter started.
179 if (GlobalVariable *GVar =
180 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
181 EmitGlobalVariable(GVar);
182 else
183 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000184 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000185}
186
Misha Brukman4afac182003-10-10 17:45:12 +0000187/// FIXME: document
Misha Brukmanedf128a2005-04-21 22:36:52 +0000188///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000189GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
190 GenericValue Result;
Chris Lattner6f335f92004-10-26 05:35:14 +0000191 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000192
Chris Lattner9a231222003-05-14 17:51:49 +0000193 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000194 switch (CE->getOpcode()) {
195 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000196 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000197 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
198 uint64_t Offset =
199 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000200
Chris Lattner3db4b622005-10-23 23:54:56 +0000201 if (getTargetData().getPointerSize() == 4)
202 Result.IntVal += Offset;
203 else
204 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000205 return Result;
206 }
Chris Lattner9a231222003-05-14 17:51:49 +0000207 case Instruction::Cast: {
208 // We only need to handle a few cases here. Almost all casts will
209 // automatically fold, just the ones involving pointers won't.
210 //
211 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000212 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000213
Chris Lattner9a231222003-05-14 17:51:49 +0000214 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000215 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000216 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000217
Chris Lattner74cf8192003-08-18 17:23:40 +0000218 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000219 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000220 return GV;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000221
Chris Lattner7d1bd332004-03-16 08:38:56 +0000222 // Handle cast of integer to a pointer...
223 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000224 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000225 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
226 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
227 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
228 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
229 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
230 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
231 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
232 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
233 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
234 default: assert(0 && "Unknown integral type!");
235 }
Chris Lattner9a231222003-05-14 17:51:49 +0000236 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000237 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000238
Chris Lattner9a231222003-05-14 17:51:49 +0000239 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000240 switch (CE->getOperand(0)->getType()->getTypeID()) {
241 default: assert(0 && "Bad add type!"); abort();
242 case Type::LongTyID:
243 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000244 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
245 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000246 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000247 case Type::IntTyID:
248 case Type::UIntTyID:
249 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
250 getConstantValue(CE->getOperand(1)).IntVal;
251 break;
252 case Type::ShortTyID:
253 case Type::UShortTyID:
254 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
255 getConstantValue(CE->getOperand(1)).ShortVal;
256 break;
257 case Type::SByteTyID:
258 case Type::UByteTyID:
259 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
260 getConstantValue(CE->getOperand(1)).SByteVal;
261 break;
262 case Type::FloatTyID:
263 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
264 getConstantValue(CE->getOperand(1)).FloatVal;
265 break;
266 case Type::DoubleTyID:
267 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
268 getConstantValue(CE->getOperand(1)).DoubleVal;
269 break;
270 }
Chris Lattner9a231222003-05-14 17:51:49 +0000271 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000272 default:
273 break;
274 }
275 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
276 abort();
277 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000278
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000279 switch (C->getType()->getTypeID()) {
Chris Lattner813c8152005-01-08 20:13:19 +0000280#define GET_CONST_VAL(TY, CTY, CLASS) \
281 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break
282 GET_CONST_VAL(Bool , bool , ConstantBool);
283 GET_CONST_VAL(UByte , unsigned char , ConstantUInt);
284 GET_CONST_VAL(SByte , signed char , ConstantSInt);
285 GET_CONST_VAL(UShort , unsigned short, ConstantUInt);
286 GET_CONST_VAL(Short , signed short , ConstantSInt);
287 GET_CONST_VAL(UInt , unsigned int , ConstantUInt);
288 GET_CONST_VAL(Int , signed int , ConstantSInt);
Chris Lattner4b3141d2005-05-12 06:01:28 +0000289 GET_CONST_VAL(ULong , uint64_t , ConstantUInt);
290 GET_CONST_VAL(Long , int64_t , ConstantSInt);
Chris Lattner813c8152005-01-08 20:13:19 +0000291 GET_CONST_VAL(Float , float , ConstantFP);
292 GET_CONST_VAL(Double , double , ConstantFP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000293#undef GET_CONST_VAL
294 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000295 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000296 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000297 else if (const Function *F = dyn_cast<Function>(C))
298 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
299 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
300 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
301 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000302 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000303 break;
304 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000305 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000306 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000307 }
308 return Result;
309}
310
Misha Brukman4afac182003-10-10 17:45:12 +0000311/// FIXME: document
312///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000313void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000314 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000315 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000316 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000317 case Type::BoolTyID:
318 case Type::UByteTyID:
319 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
320 case Type::UShortTyID:
321 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
322 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
323 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000324 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000325 case Type::FloatTyID:
326 case Type::UIntTyID:
327 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
328 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
329 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
330 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
331 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000332 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000333 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000334 case Type::DoubleTyID:
335 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000336 case Type::LongTyID:
337 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
338 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
339 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
340 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
341 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
342 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
343 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
344 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
345 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000346 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000347 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000348 }
349 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000350 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000351 case Type::BoolTyID:
352 case Type::UByteTyID:
353 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
354 case Type::UShortTyID:
355 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
356 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
357 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000358 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000359 case Type::FloatTyID:
360 case Type::UIntTyID:
361 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
362 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
363 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
364 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
365 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000366 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000367 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000368 case Type::DoubleTyID:
369 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000370 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000371 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000372 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
373 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
374 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
375 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
376 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
377 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
378 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
379 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000380 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000381 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000382 }
383 }
384}
385
Misha Brukman4afac182003-10-10 17:45:12 +0000386/// FIXME: document
387///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000388GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
389 const Type *Ty) {
390 GenericValue Result;
391 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000392 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000393 case Type::BoolTyID:
394 case Type::UByteTyID:
395 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
396 case Type::UShortTyID:
397 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
398 ((unsigned)Ptr->Untyped[1] << 8);
399 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000400 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000401 case Type::FloatTyID:
402 case Type::UIntTyID:
403 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
404 ((unsigned)Ptr->Untyped[1] << 8) |
405 ((unsigned)Ptr->Untyped[2] << 16) |
406 ((unsigned)Ptr->Untyped[3] << 24);
407 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000408 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000409 goto Load4BytesLittleEndian;
410 case Type::DoubleTyID:
411 case Type::ULongTyID:
412 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
413 ((uint64_t)Ptr->Untyped[1] << 8) |
414 ((uint64_t)Ptr->Untyped[2] << 16) |
415 ((uint64_t)Ptr->Untyped[3] << 24) |
416 ((uint64_t)Ptr->Untyped[4] << 32) |
417 ((uint64_t)Ptr->Untyped[5] << 40) |
418 ((uint64_t)Ptr->Untyped[6] << 48) |
419 ((uint64_t)Ptr->Untyped[7] << 56);
420 break;
421 default:
422 std::cout << "Cannot load value of type " << *Ty << "!\n";
423 abort();
424 }
425 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000426 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000427 case Type::BoolTyID:
428 case Type::UByteTyID:
429 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
430 case Type::UShortTyID:
431 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
432 ((unsigned)Ptr->Untyped[0] << 8);
433 break;
434 Load4BytesBigEndian:
435 case Type::FloatTyID:
436 case Type::UIntTyID:
437 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
438 ((unsigned)Ptr->Untyped[2] << 8) |
439 ((unsigned)Ptr->Untyped[1] << 16) |
440 ((unsigned)Ptr->Untyped[0] << 24);
441 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000442 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000443 goto Load4BytesBigEndian;
444 case Type::DoubleTyID:
445 case Type::ULongTyID:
446 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
447 ((uint64_t)Ptr->Untyped[6] << 8) |
448 ((uint64_t)Ptr->Untyped[5] << 16) |
449 ((uint64_t)Ptr->Untyped[4] << 24) |
450 ((uint64_t)Ptr->Untyped[3] << 32) |
451 ((uint64_t)Ptr->Untyped[2] << 40) |
452 ((uint64_t)Ptr->Untyped[1] << 48) |
453 ((uint64_t)Ptr->Untyped[0] << 56);
454 break;
455 default:
456 std::cout << "Cannot load value of type " << *Ty << "!\n";
457 abort();
458 }
459 }
460 return Result;
461}
462
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000463// InitializeMemory - Recursive function to apply a Constant value into the
464// specified memory location...
465//
466void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000467 if (isa<UndefValue>(Init)) {
468 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000469 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
470 unsigned ElementSize =
471 getTargetData().getTypeSize(CP->getType()->getElementType());
472 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
473 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
474 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000475 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000476 GenericValue Val = getConstantValue(Init);
477 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
478 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000479 } else if (isa<ConstantAggregateZero>(Init)) {
Chris Lattner813c8152005-01-08 20:13:19 +0000480 memset(Addr, 0, (size_t)getTargetData().getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000481 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000482 }
483
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000484 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000485 case Type::ArrayTyID: {
486 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000487 unsigned ElementSize =
Chris Lattner6e630882005-07-11 02:49:16 +0000488 getTargetData().getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000489 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
490 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000491 return;
492 }
493
494 case Type::StructTyID: {
495 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
496 const StructLayout *SL =
497 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000498 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
499 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000500 return;
501 }
502
503 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000504 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000505 assert(0 && "Unknown constant type to initialize memory with!");
506 }
507}
508
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000509/// EmitGlobals - Emit all of the global variables to memory, storing their
510/// addresses into GlobalAddress. This must make sure to copy the contents of
511/// their initializers into the memory.
512///
513void ExecutionEngine::emitGlobals() {
514 const TargetData &TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000515
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000516 // Loop over all of the global variables in the program, allocating the memory
517 // to hold them.
Chris Lattner6e630882005-07-11 02:49:16 +0000518 Module &M = getModule();
519 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000520 I != E; ++I)
521 if (!I->isExternal()) {
522 // Get the type of the global...
523 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000524
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000525 // Allocate some memory for it!
526 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000527 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000528 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000529 // External variable reference. Try to use the dynamic loader to
530 // get a pointer to it.
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000531 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
532 I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000533 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000534 else {
535 std::cerr << "Could not resolve external global address: "
536 << I->getName() << "\n";
537 abort();
538 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000539 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000540
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000541 // Now that all of the globals are set up in memory, loop through them all and
542 // initialize their contents.
Chris Lattner6e630882005-07-11 02:49:16 +0000543 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000544 I != E; ++I)
545 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000546 EmitGlobalVariable(I);
547}
548
549// EmitGlobalVariable - This method emits the specified global variable to the
550// address specified in GlobalAddresses, or allocates new memory if it's not
551// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000552void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000553 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000554 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
555
Chris Lattnerc07ed132003-12-20 03:36:47 +0000556 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner813c8152005-01-08 20:13:19 +0000557 size_t GVSize = (size_t)getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000558 if (GA == 0) {
559 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000560 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000561 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000562 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000563
Chris Lattner24b0a182003-12-20 02:45:37 +0000564 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000565 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000566 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000567}