blob: c9fd5776546cbfad5c4acc8dd5e2d72afa68f235 [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 }
140 } catch (...) {
141 std::cerr << "Error reading the bytecode file!\n";
142 }
Misha Brukman19684162003-10-16 21:18:05 +0000143 }
Chris Lattner73011782003-12-28 09:44:37 +0000144
145 if (EE == 0) delete IL;
Brian Gaeke82d82772003-09-03 20:34:19 +0000146 return EE;
147}
148
Misha Brukman4afac182003-10-10 17:45:12 +0000149/// getPointerToGlobal - This returns the address of the specified global
150/// value. This may involve code generation if it's a function.
151///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000152void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000153 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000154 return getPointerToFunction(F);
155
Chris Lattner55d86482003-12-31 20:21:04 +0000156 assert(GlobalAddressMap[GV] && "Global hasn't had an address allocated yet?");
157 return GlobalAddressMap[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000158}
159
Misha Brukman4afac182003-10-10 17:45:12 +0000160/// FIXME: document
161///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000162GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
163 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000164
Chris Lattner9a231222003-05-14 17:51:49 +0000165 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000166 switch (CE->getOpcode()) {
167 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000168 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000169 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
170 uint64_t Offset =
171 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
172
173 Result.LongVal += Offset;
174 return Result;
175 }
Chris Lattner9a231222003-05-14 17:51:49 +0000176 case Instruction::Cast: {
177 // We only need to handle a few cases here. Almost all casts will
178 // automatically fold, just the ones involving pointers won't.
179 //
180 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000181 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000182
Chris Lattner9a231222003-05-14 17:51:49 +0000183 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000184 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000185 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000186
Chris Lattner74cf8192003-08-18 17:23:40 +0000187 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000188 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000189 return GV;
Chris Lattner74cf8192003-08-18 17:23:40 +0000190
Chris Lattner7d1bd332004-03-16 08:38:56 +0000191 // Handle cast of integer to a pointer...
192 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000193 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000194 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
195 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
196 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
197 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
198 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
199 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
200 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
201 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
202 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
203 default: assert(0 && "Unknown integral type!");
204 }
Chris Lattner9a231222003-05-14 17:51:49 +0000205 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000206 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000207
Chris Lattner9a231222003-05-14 17:51:49 +0000208 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000209 if (CE->getOperand(0)->getType() == Type::LongTy ||
210 CE->getOperand(0)->getType() == Type::ULongTy)
211 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
212 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000213 else
214 break;
215 return Result;
216
217 default:
218 break;
219 }
220 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
221 abort();
222 }
223
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000224 switch (C->getType()->getTypeID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000225#define GET_CONST_VAL(TY, CLASS) \
226 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000227 GET_CONST_VAL(Bool , ConstantBool);
228 GET_CONST_VAL(UByte , ConstantUInt);
229 GET_CONST_VAL(SByte , ConstantSInt);
230 GET_CONST_VAL(UShort , ConstantUInt);
231 GET_CONST_VAL(Short , ConstantSInt);
232 GET_CONST_VAL(UInt , ConstantUInt);
233 GET_CONST_VAL(Int , ConstantSInt);
234 GET_CONST_VAL(ULong , ConstantUInt);
235 GET_CONST_VAL(Long , ConstantSInt);
236 GET_CONST_VAL(Float , ConstantFP);
237 GET_CONST_VAL(Double , ConstantFP);
238#undef GET_CONST_VAL
239 case Type::PointerTyID:
240 if (isa<ConstantPointerNull>(C)) {
241 Result.PointerVal = 0;
242 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
Chris Lattner38f0eba2003-12-08 08:22:48 +0000243 if (Function *F =
244 const_cast<Function*>(dyn_cast<Function>(CPR->getValue())))
245 Result = PTOGV(getPointerToFunctionOrStub(F));
246 else
Chris Lattnerc07ed132003-12-20 03:36:47 +0000247 Result = PTOGV(getOrEmitGlobalVariable(
248 cast<GlobalVariable>(CPR->getValue())));
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000249
250 } else {
251 assert(0 && "Unknown constant pointer type!");
252 }
253 break;
254 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000255 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000256 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000257 }
258 return Result;
259}
260
Misha Brukman4afac182003-10-10 17:45:12 +0000261/// FIXME: document
262///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000263void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000264 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000265 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000266 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000267 case Type::BoolTyID:
268 case Type::UByteTyID:
269 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
270 case Type::UShortTyID:
271 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
272 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
273 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000274 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000275 case Type::FloatTyID:
276 case Type::UIntTyID:
277 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
278 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
279 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
280 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
281 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000282 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000283 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000284 case Type::DoubleTyID:
285 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000286 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000287 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
288 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
289 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
290 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
291 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
292 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
293 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
294 break;
295 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000296 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000297 }
298 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000299 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000300 case Type::BoolTyID:
301 case Type::UByteTyID:
302 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
303 case Type::UShortTyID:
304 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
305 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
306 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000307 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000308 case Type::FloatTyID:
309 case Type::UIntTyID:
310 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
311 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
312 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
313 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
314 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000315 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000316 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000317 case Type::DoubleTyID:
318 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000319 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000320 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
321 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
322 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
323 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
324 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
325 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
326 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
327 break;
328 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000329 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000330 }
331 }
332}
333
Misha Brukman4afac182003-10-10 17:45:12 +0000334/// FIXME: document
335///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000336GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
337 const Type *Ty) {
338 GenericValue Result;
339 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000340 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000341 case Type::BoolTyID:
342 case Type::UByteTyID:
343 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
344 case Type::UShortTyID:
345 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
346 ((unsigned)Ptr->Untyped[1] << 8);
347 break;
348 Load4BytesLittleEndian:
349 case Type::FloatTyID:
350 case Type::UIntTyID:
351 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
352 ((unsigned)Ptr->Untyped[1] << 8) |
353 ((unsigned)Ptr->Untyped[2] << 16) |
354 ((unsigned)Ptr->Untyped[3] << 24);
355 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000356 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000357 goto Load4BytesLittleEndian;
358 case Type::DoubleTyID:
359 case Type::ULongTyID:
360 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
361 ((uint64_t)Ptr->Untyped[1] << 8) |
362 ((uint64_t)Ptr->Untyped[2] << 16) |
363 ((uint64_t)Ptr->Untyped[3] << 24) |
364 ((uint64_t)Ptr->Untyped[4] << 32) |
365 ((uint64_t)Ptr->Untyped[5] << 40) |
366 ((uint64_t)Ptr->Untyped[6] << 48) |
367 ((uint64_t)Ptr->Untyped[7] << 56);
368 break;
369 default:
370 std::cout << "Cannot load value of type " << *Ty << "!\n";
371 abort();
372 }
373 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000374 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000375 case Type::BoolTyID:
376 case Type::UByteTyID:
377 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
378 case Type::UShortTyID:
379 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
380 ((unsigned)Ptr->Untyped[0] << 8);
381 break;
382 Load4BytesBigEndian:
383 case Type::FloatTyID:
384 case Type::UIntTyID:
385 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
386 ((unsigned)Ptr->Untyped[2] << 8) |
387 ((unsigned)Ptr->Untyped[1] << 16) |
388 ((unsigned)Ptr->Untyped[0] << 24);
389 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000390 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000391 goto Load4BytesBigEndian;
392 case Type::DoubleTyID:
393 case Type::ULongTyID:
394 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
395 ((uint64_t)Ptr->Untyped[6] << 8) |
396 ((uint64_t)Ptr->Untyped[5] << 16) |
397 ((uint64_t)Ptr->Untyped[4] << 24) |
398 ((uint64_t)Ptr->Untyped[3] << 32) |
399 ((uint64_t)Ptr->Untyped[2] << 40) |
400 ((uint64_t)Ptr->Untyped[1] << 48) |
401 ((uint64_t)Ptr->Untyped[0] << 56);
402 break;
403 default:
404 std::cout << "Cannot load value of type " << *Ty << "!\n";
405 abort();
406 }
407 }
408 return Result;
409}
410
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000411// InitializeMemory - Recursive function to apply a Constant value into the
412// specified memory location...
413//
414void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
415 if (Init->getType()->isFirstClassType()) {
416 GenericValue Val = getConstantValue(Init);
417 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
418 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000419 } else if (isa<ConstantAggregateZero>(Init)) {
420 unsigned Size = getTargetData().getTypeSize(Init->getType());
421 memset(Addr, 0, Size);
422 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000423 }
424
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000425 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000426 case Type::ArrayTyID: {
427 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000428 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000429 unsigned ElementSize =
430 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
431 for (unsigned i = 0; i < Val.size(); ++i)
432 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
433 return;
434 }
435
436 case Type::StructTyID: {
437 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
438 const StructLayout *SL =
439 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000440 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000441 for (unsigned i = 0; i < Val.size(); ++i)
442 InitializeMemory(cast<Constant>(Val[i].get()),
443 (char*)Addr+SL->MemberOffsets[i]);
444 return;
445 }
446
447 default:
448 std::cerr << "Bad Type: " << Init->getType() << "\n";
449 assert(0 && "Unknown constant type to initialize memory with!");
450 }
451}
452
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000453/// EmitGlobals - Emit all of the global variables to memory, storing their
454/// addresses into GlobalAddress. This must make sure to copy the contents of
455/// their initializers into the memory.
456///
457void ExecutionEngine::emitGlobals() {
458 const TargetData &TD = getTargetData();
459
460 // Loop over all of the global variables in the program, allocating the memory
461 // to hold them.
462 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
463 I != E; ++I)
464 if (!I->isExternal()) {
465 // Get the type of the global...
466 const Type *Ty = I->getType()->getElementType();
467
468 // Allocate some memory for it!
469 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000470 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000471 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000472 // External variable reference. Try to use the dynamic loader to
473 // get a pointer to it.
474 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000475 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000476 else {
477 std::cerr << "Could not resolve external global address: "
478 << I->getName() << "\n";
479 abort();
480 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000481 }
482
483 // Now that all of the globals are set up in memory, loop through them all and
484 // initialize their contents.
485 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
486 I != E; ++I)
487 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000488 EmitGlobalVariable(I);
489}
490
491// EmitGlobalVariable - This method emits the specified global variable to the
492// address specified in GlobalAddresses, or allocates new memory if it's not
493// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000494void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000495 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000496 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
497
Chris Lattnerc07ed132003-12-20 03:36:47 +0000498 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000499 if (GA == 0) {
500 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000501 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner55d86482003-12-31 20:21:04 +0000502 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000503 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000504
Chris Lattner24b0a182003-12-20 02:45:37 +0000505 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000506 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000507 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000508}