blob: b768930099e5f174b813902b3617419cd78e8f9d [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +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.
7//
8//===----------------------------------------------------------------------===//
9//
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"
Chris Lattner8f7f71b2004-06-20 07:46:18 +000022#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukman19684162003-10-16 21:18:05 +000023#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000024#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000026#include "Support/Debug.h"
27#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000028#include "Support/DynamicLinker.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 Brukman19684162003-10-16 21:18:05 +000036ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
37 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) {
53 // If we haven't computed the reverse mapping yet, do so first.
54 if (GlobalAddressReverseMap.empty()) {
55 for (std::map<const GlobalValue*, void *>::iterator I =
56 GlobalAddressMap.begin(), E = GlobalAddressMap.end(); I != E; ++I)
57 GlobalAddressReverseMap.insert(std::make_pair(I->second, I->first));
58 }
59
60 std::map<void *, const GlobalValue*>::iterator I =
61 GlobalAddressReverseMap.find(Addr);
62 return I != GlobalAddressReverseMap.end() ? I->second : 0;
63}
Chris Lattner87f03102003-12-26 06:50:30 +000064
65// CreateArgv - Turn a vector of strings into a nice argv style array of
66// pointers to null terminated strings.
67//
68static void *CreateArgv(ExecutionEngine *EE,
69 const std::vector<std::string> &InputArgv) {
70 unsigned PtrSize = EE->getTargetData().getPointerSize();
71 char *Result = new char[(InputArgv.size()+1)*PtrSize];
72
73 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
74 const Type *SBytePtr = PointerType::get(Type::SByteTy);
75
76 for (unsigned i = 0; i != InputArgv.size(); ++i) {
77 unsigned Size = InputArgv[i].size()+1;
78 char *Dest = new char[Size];
79 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
80
81 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
82 Dest[Size-1] = 0;
83
84 // Endian safe: Result[i] = (PointerTy)Dest;
85 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
86 SBytePtr);
87 }
88
89 // Null terminate it
90 EE->StoreValueToMemory(PTOGV(0),
91 (GenericValue*)(Result+InputArgv.size()*PtrSize),
92 SBytePtr);
93 return Result;
94}
95
96/// runFunctionAsMain - This is a helper function which wraps runFunction to
97/// handle the common task of starting up main with the specified argc, argv,
98/// and envp parameters.
99int ExecutionEngine::runFunctionAsMain(Function *Fn,
100 const std::vector<std::string> &argv,
101 const char * const * envp) {
102 std::vector<GenericValue> GVArgs;
103 GenericValue GVArgc;
104 GVArgc.IntVal = argv.size();
105 GVArgs.push_back(GVArgc); // Arg #0 = argc.
106 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
107 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
108
109 std::vector<std::string> EnvVars;
110 for (unsigned i = 0; envp[i]; ++i)
111 EnvVars.push_back(envp[i]);
112 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
113 return runFunction(Fn, GVArgs).IntVal;
114}
115
116
117
Misha Brukman19684162003-10-16 21:18:05 +0000118/// If possible, create a JIT, unless the caller specifically requests an
119/// Interpreter or there's an error. If even an Interpreter cannot be created,
120/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000121///
Misha Brukman7b2b40f2003-10-14 21:36:31 +0000122ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000123 bool ForceInterpreter,
124 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000125 ExecutionEngine *EE = 0;
126
Chris Lattner73011782003-12-28 09:44:37 +0000127 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000128 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000129 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000130
131 // If we can't make a JIT, make an interpreter instead.
Chris Lattner338733f2004-02-01 01:07:25 +0000132 if (EE == 0) {
133 try {
134 Module *M = MP->materializeModule();
135 try {
136 EE = Interpreter::create(M, IL);
137 } catch (...) {
138 std::cerr << "Error creating the interpreter!\n";
139 }
Reid Spencere2947532004-07-07 21:01:38 +0000140 } catch (std::string& errmsg) {
141 std::cerr << "Error reading the bytecode file: " << errmsg << "\n";
Chris Lattner338733f2004-02-01 01:07:25 +0000142 } catch (...) {
143 std::cerr << "Error reading the bytecode file!\n";
144 }
Misha Brukman19684162003-10-16 21:18:05 +0000145 }
Chris Lattner73011782003-12-28 09:44:37 +0000146
147 if (EE == 0) delete IL;
Brian Gaeke82d82772003-09-03 20:34:19 +0000148 return EE;
149}
150
Misha Brukman4afac182003-10-10 17:45:12 +0000151/// getPointerToGlobal - This returns the address of the specified global
152/// value. This may involve code generation if it's a function.
153///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000154void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000155 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000156 return getPointerToFunction(F);
157
Chris Lattner55d86482003-12-31 20:21:04 +0000158 assert(GlobalAddressMap[GV] && "Global hasn't had an address allocated yet?");
159 return GlobalAddressMap[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000160}
161
Misha Brukman4afac182003-10-10 17:45:12 +0000162/// FIXME: document
163///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000164GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
165 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000166
Chris Lattner9a231222003-05-14 17:51:49 +0000167 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000168 switch (CE->getOpcode()) {
169 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000170 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000171 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
172 uint64_t Offset =
173 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
174
175 Result.LongVal += Offset;
176 return Result;
177 }
Chris Lattner9a231222003-05-14 17:51:49 +0000178 case Instruction::Cast: {
179 // We only need to handle a few cases here. Almost all casts will
180 // automatically fold, just the ones involving pointers won't.
181 //
182 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000183 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000184
Chris Lattner9a231222003-05-14 17:51:49 +0000185 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000186 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000187 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000188
Chris Lattner74cf8192003-08-18 17:23:40 +0000189 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000190 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000191 return GV;
Chris Lattner74cf8192003-08-18 17:23:40 +0000192
Chris Lattner7d1bd332004-03-16 08:38:56 +0000193 // Handle cast of integer to a pointer...
194 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000195 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000196 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
197 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
198 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
199 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
200 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
201 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
202 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
203 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
204 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
205 default: assert(0 && "Unknown integral type!");
206 }
Chris Lattner9a231222003-05-14 17:51:49 +0000207 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000208 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000209
Chris Lattner9a231222003-05-14 17:51:49 +0000210 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000211 switch (CE->getOperand(0)->getType()->getTypeID()) {
212 default: assert(0 && "Bad add type!"); abort();
213 case Type::LongTyID:
214 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000215 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
216 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000217 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000218 case Type::IntTyID:
219 case Type::UIntTyID:
220 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
221 getConstantValue(CE->getOperand(1)).IntVal;
222 break;
223 case Type::ShortTyID:
224 case Type::UShortTyID:
225 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
226 getConstantValue(CE->getOperand(1)).ShortVal;
227 break;
228 case Type::SByteTyID:
229 case Type::UByteTyID:
230 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
231 getConstantValue(CE->getOperand(1)).SByteVal;
232 break;
233 case Type::FloatTyID:
234 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
235 getConstantValue(CE->getOperand(1)).FloatVal;
236 break;
237 case Type::DoubleTyID:
238 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
239 getConstantValue(CE->getOperand(1)).DoubleVal;
240 break;
241 }
Chris Lattner9a231222003-05-14 17:51:49 +0000242 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000243 default:
244 break;
245 }
246 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
247 abort();
248 }
249
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000250 switch (C->getType()->getTypeID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000251#define GET_CONST_VAL(TY, CLASS) \
252 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000253 GET_CONST_VAL(Bool , ConstantBool);
254 GET_CONST_VAL(UByte , ConstantUInt);
255 GET_CONST_VAL(SByte , ConstantSInt);
256 GET_CONST_VAL(UShort , ConstantUInt);
257 GET_CONST_VAL(Short , ConstantSInt);
258 GET_CONST_VAL(UInt , ConstantUInt);
259 GET_CONST_VAL(Int , ConstantSInt);
260 GET_CONST_VAL(ULong , ConstantUInt);
261 GET_CONST_VAL(Long , ConstantSInt);
262 GET_CONST_VAL(Float , ConstantFP);
263 GET_CONST_VAL(Double , ConstantFP);
264#undef GET_CONST_VAL
265 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000266 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000267 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000268 else if (const Function *F = dyn_cast<Function>(C))
269 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
270 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
271 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
272 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000273 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000274 break;
275 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000276 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000277 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000278 }
279 return Result;
280}
281
Misha Brukman4afac182003-10-10 17:45:12 +0000282/// FIXME: document
283///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000284void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000285 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000286 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000287 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000288 case Type::BoolTyID:
289 case Type::UByteTyID:
290 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
291 case Type::UShortTyID:
292 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
293 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
294 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000295 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000296 case Type::FloatTyID:
297 case Type::UIntTyID:
298 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
299 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
300 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
301 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
302 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000303 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000304 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000305 case Type::DoubleTyID:
306 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000307 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000308 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
309 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
310 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
311 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
312 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
313 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
314 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
315 break;
316 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000317 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000318 }
319 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000320 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000321 case Type::BoolTyID:
322 case Type::UByteTyID:
323 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
324 case Type::UShortTyID:
325 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
326 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
327 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000328 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000329 case Type::FloatTyID:
330 case Type::UIntTyID:
331 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
332 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
333 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
334 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
335 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000336 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000337 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000338 case Type::DoubleTyID:
339 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000340 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000341 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
342 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
343 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
344 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
345 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
346 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
347 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
348 break;
349 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000350 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000351 }
352 }
353}
354
Misha Brukman4afac182003-10-10 17:45:12 +0000355/// FIXME: document
356///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000357GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
358 const Type *Ty) {
359 GenericValue Result;
360 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000361 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000362 case Type::BoolTyID:
363 case Type::UByteTyID:
364 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
365 case Type::UShortTyID:
366 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
367 ((unsigned)Ptr->Untyped[1] << 8);
368 break;
369 Load4BytesLittleEndian:
370 case Type::FloatTyID:
371 case Type::UIntTyID:
372 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
373 ((unsigned)Ptr->Untyped[1] << 8) |
374 ((unsigned)Ptr->Untyped[2] << 16) |
375 ((unsigned)Ptr->Untyped[3] << 24);
376 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000377 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000378 goto Load4BytesLittleEndian;
379 case Type::DoubleTyID:
380 case Type::ULongTyID:
381 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
382 ((uint64_t)Ptr->Untyped[1] << 8) |
383 ((uint64_t)Ptr->Untyped[2] << 16) |
384 ((uint64_t)Ptr->Untyped[3] << 24) |
385 ((uint64_t)Ptr->Untyped[4] << 32) |
386 ((uint64_t)Ptr->Untyped[5] << 40) |
387 ((uint64_t)Ptr->Untyped[6] << 48) |
388 ((uint64_t)Ptr->Untyped[7] << 56);
389 break;
390 default:
391 std::cout << "Cannot load value of type " << *Ty << "!\n";
392 abort();
393 }
394 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000395 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000396 case Type::BoolTyID:
397 case Type::UByteTyID:
398 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
399 case Type::UShortTyID:
400 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
401 ((unsigned)Ptr->Untyped[0] << 8);
402 break;
403 Load4BytesBigEndian:
404 case Type::FloatTyID:
405 case Type::UIntTyID:
406 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
407 ((unsigned)Ptr->Untyped[2] << 8) |
408 ((unsigned)Ptr->Untyped[1] << 16) |
409 ((unsigned)Ptr->Untyped[0] << 24);
410 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000411 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000412 goto Load4BytesBigEndian;
413 case Type::DoubleTyID:
414 case Type::ULongTyID:
415 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
416 ((uint64_t)Ptr->Untyped[6] << 8) |
417 ((uint64_t)Ptr->Untyped[5] << 16) |
418 ((uint64_t)Ptr->Untyped[4] << 24) |
419 ((uint64_t)Ptr->Untyped[3] << 32) |
420 ((uint64_t)Ptr->Untyped[2] << 40) |
421 ((uint64_t)Ptr->Untyped[1] << 48) |
422 ((uint64_t)Ptr->Untyped[0] << 56);
423 break;
424 default:
425 std::cout << "Cannot load value of type " << *Ty << "!\n";
426 abort();
427 }
428 }
429 return Result;
430}
431
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000432// InitializeMemory - Recursive function to apply a Constant value into the
433// specified memory location...
434//
435void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
436 if (Init->getType()->isFirstClassType()) {
437 GenericValue Val = getConstantValue(Init);
438 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
439 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000440 } else if (isa<ConstantAggregateZero>(Init)) {
441 unsigned Size = getTargetData().getTypeSize(Init->getType());
442 memset(Addr, 0, Size);
443 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000444 }
445
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000446 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000447 case Type::ArrayTyID: {
448 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000449 unsigned ElementSize =
450 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000451 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
452 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000453 return;
454 }
455
456 case Type::StructTyID: {
457 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
458 const StructLayout *SL =
459 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000460 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
461 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000462 return;
463 }
464
465 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000466 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000467 assert(0 && "Unknown constant type to initialize memory with!");
468 }
469}
470
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000471/// EmitGlobals - Emit all of the global variables to memory, storing their
472/// addresses into GlobalAddress. This must make sure to copy the contents of
473/// their initializers into the memory.
474///
475void ExecutionEngine::emitGlobals() {
476 const TargetData &TD = getTargetData();
477
478 // Loop over all of the global variables in the program, allocating the memory
479 // to hold them.
480 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
481 I != E; ++I)
482 if (!I->isExternal()) {
483 // Get the type of the global...
484 const Type *Ty = I->getType()->getElementType();
485
486 // Allocate some memory for it!
487 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000488 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000489 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000490 // External variable reference. Try to use the dynamic loader to
491 // get a pointer to it.
492 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000493 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000494 else {
495 std::cerr << "Could not resolve external global address: "
496 << I->getName() << "\n";
497 abort();
498 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000499 }
500
501 // Now that all of the globals are set up in memory, loop through them all and
502 // initialize their contents.
503 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
504 I != E; ++I)
505 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000506 EmitGlobalVariable(I);
507}
508
509// EmitGlobalVariable - This method emits the specified global variable to the
510// address specified in GlobalAddresses, or allocates new memory if it's not
511// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000512void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000513 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000514 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
515
Chris Lattnerc07ed132003-12-20 03:36:47 +0000516 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000517 if (GA == 0) {
518 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000519 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner55d86482003-12-31 20:21:04 +0000520 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000521 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000522
Chris Lattner24b0a182003-12-20 02:45:37 +0000523 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000524 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000525 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000526}