blob: 41948c5806611107522e9ff72eb1f97773dfcf90 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// This file contains the actual instruction interpreter.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Interpreter.h"
8#include "ExecutionAnnotations.h"
9#include "llvm/iOther.h"
10#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000011#include "llvm/iMemory.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000012#include "llvm/Type.h"
13#include "llvm/ConstPoolVals.h"
14#include "llvm/Assembly/Writer.h"
Chris Lattner86660982001-08-27 05:16:50 +000015#include "llvm/Support/DataTypes.h"
Chris Lattner41c2e5c2001-09-28 22:56:43 +000016#include "llvm/Target/TargetData.h"
Chris Lattner2e42d3a2001-10-15 05:51:48 +000017#include "llvm/GlobalVariable.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000018#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000019#include <signal.h>
20#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000021
22// Create a TargetData structure to handle memory addressing and size/alignment
23// computations
24//
25static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000026CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000027
28
29sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000030static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000031
32extern "C" {
33static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000034 if (InInstruction)
35 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000036}
37}
38
39static void initializeSignalHandlers() {
40 struct sigaction Action;
41 Action.sa_handler = SigHandler;
42 Action.sa_flags = SA_SIGINFO;
43 sigemptyset(&Action.sa_mask);
44 sigaction(SIGSEGV, &Action, 0);
45 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000046 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000047 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000048}
49
Chris Lattner2e42d3a2001-10-15 05:51:48 +000050
51//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000052// Value Manipulation code
53//===----------------------------------------------------------------------===//
54
55static unsigned getOperandSlot(Value *V) {
56 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
57 assert(SN && "Operand does not have a slot number annotation!");
58 return SN->SlotNum;
59}
60
61#define GET_CONST_VAL(TY, CLASS) \
62 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
63
64static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
65 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
66 GenericValue Result;
67 switch (CPV->getType()->getPrimitiveID()) {
68 GET_CONST_VAL(Bool , ConstPoolBool);
69 GET_CONST_VAL(UByte , ConstPoolUInt);
70 GET_CONST_VAL(SByte , ConstPoolSInt);
71 GET_CONST_VAL(UShort , ConstPoolUInt);
72 GET_CONST_VAL(Short , ConstPoolSInt);
73 GET_CONST_VAL(UInt , ConstPoolUInt);
74 GET_CONST_VAL(Int , ConstPoolSInt);
Chris Lattner7b851ab2001-10-15 19:18:26 +000075 GET_CONST_VAL(ULong , ConstPoolUInt);
76 GET_CONST_VAL(Long , ConstPoolSInt);
Chris Lattner39bb5b42001-10-15 13:25:40 +000077 GET_CONST_VAL(Float , ConstPoolFP);
78 GET_CONST_VAL(Double , ConstPoolFP);
79 case Type::PointerTyID:
80 if (isa<ConstPoolPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000081 Result.PointerVal = 0;
Chris Lattner39bb5b42001-10-15 13:25:40 +000082 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
83 assert(0 && "Not implemented!");
84 } else {
85 assert(0 && "Unknown constant pointer type!");
86 }
87 break;
88 default:
89 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
90 }
91 return Result;
92 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
93 GlobalAddress *Address =
94 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
95 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +000096 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +000097 return Result;
98 } else {
99 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000100 unsigned OpSlot = getOperandSlot(V);
101 assert(TyP < SF.Values.size() &&
102 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000103 return SF.Values[TyP][getOperandSlot(V)];
104 }
105}
106
107static void printOperandInfo(Value *V, ExecutionContext &SF) {
108 if (isa<ConstPoolVal>(V)) {
109 cout << "Constant Pool Value\n";
110 } else if (isa<GlobalValue>(V)) {
111 cout << "Global Value\n";
112 } else {
113 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
114 unsigned Slot = getOperandSlot(V);
115 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000116 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
117 << " Contents=0x";
118
119 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
120 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
121 unsigned char Cur = Buf[i];
122 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
123 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
124 }
125 cout << endl;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000126 }
127}
128
129
130
131static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
132 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
133
134 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
135 SF.Values[TyP][getOperandSlot(V)] = Val;
136}
137
138
139//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000140// Annotation Wrangling code
141//===----------------------------------------------------------------------===//
142
143void Interpreter::initializeExecutionEngine() {
144 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
145 &MethodInfo::Create);
146 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
147 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000148 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000149}
150
151// InitializeMemory - Recursive function to apply a ConstPool value into the
152// specified memory location...
153//
154static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000155#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
156 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000157 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000158 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000159 } return
160
161 switch (Init->getType()->getPrimitiveID()) {
162 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
163 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
164 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
165 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
166 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
167 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
168 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
169 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
170 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
171 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
172 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
173#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000174
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000175 case Type::ArrayTyID: {
176 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
177 const vector<Use> &Val = CPA->getValues();
178 unsigned ElementSize =
179 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
180 for (unsigned i = 0; i < Val.size(); ++i)
181 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
182 return;
183 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000184
185 case Type::StructTyID: {
186 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
187 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
188 const vector<Use> &Val = CPS->getValues();
189 for (unsigned i = 0; i < Val.size(); ++i)
190 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
191 Addr+SL->MemberOffsets[i]);
192 return;
193 }
194
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000195 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000196 if (isa<ConstPoolPointerNull>(Init)) {
197 *(void**)Addr = 0;
198 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
199 GlobalAddress *Address =
200 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
201 *(void**)Addr = (GenericValue*)Address->Ptr;
202 } else {
203 assert(0 && "Unknown Constant pointer type!");
204 }
205 return;
206
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000207 default:
Chris Lattner461f02f2001-11-07 05:31:27 +0000208 CW << "Bad Type: " << Init->getType() << endl;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000209 assert(0 && "Unknown constant type to initialize memory with!");
210 }
211}
212
213Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
214 assert(AID == GlobalAddressAID);
215
216 // This annotation will only be created on GlobalValue objects...
217 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
218
219 if (isa<Method>(GVal)) {
220 // The GlobalAddress object for a method is just a pointer to method itself.
221 // Don't delete it when the annotation is gone though!
222 return new GlobalAddress(GVal, false);
223 }
224
225 // Handle the case of a global variable...
226 assert(isa<GlobalVariable>(GVal) &&
227 "Global value found that isn't a method or global variable!");
228 GlobalVariable *GV = cast<GlobalVariable>(GVal);
229
230 // First off, we must allocate space for the global variable to point at...
231 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
232 unsigned NumElements = 1;
233
234 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
235 assert(GV->hasInitializer() && "Const val must have an initializer!");
236 // Allocating a unsized array type?
237 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
238
239 // Get the number of elements being allocated by the array...
240 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
241 }
242
243 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000244 void *Addr = calloc(NumElements, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000245 assert(Addr != 0 && "Null pointer returned by malloc!");
246
247 // Initialize the memory if there is an initializer...
248 if (GV->hasInitializer())
249 InitializeMemory(GV->getInitializer(), (char*)Addr);
250
251 return new GlobalAddress(Addr, true); // Simply invoke the ctor
252}
253
Chris Lattner92101ac2001-08-23 17:05:04 +0000254
255//===----------------------------------------------------------------------===//
256// Binary Instruction Implementations
257//===----------------------------------------------------------------------===//
258
259#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
260 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
261
262static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
263 const Type *Ty, ExecutionContext &SF) {
264 GenericValue Dest;
265 switch (Ty->getPrimitiveID()) {
266 IMPLEMENT_BINARY_OPERATOR(+, UByte);
267 IMPLEMENT_BINARY_OPERATOR(+, SByte);
268 IMPLEMENT_BINARY_OPERATOR(+, UShort);
269 IMPLEMENT_BINARY_OPERATOR(+, Short);
270 IMPLEMENT_BINARY_OPERATOR(+, UInt);
271 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000272 IMPLEMENT_BINARY_OPERATOR(+, ULong);
273 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000274 IMPLEMENT_BINARY_OPERATOR(+, Float);
275 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000276 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000277 default:
278 cout << "Unhandled type for Add instruction: " << Ty << endl;
279 }
280 return Dest;
281}
282
283static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
284 const Type *Ty, ExecutionContext &SF) {
285 GenericValue Dest;
286 switch (Ty->getPrimitiveID()) {
287 IMPLEMENT_BINARY_OPERATOR(-, UByte);
288 IMPLEMENT_BINARY_OPERATOR(-, SByte);
289 IMPLEMENT_BINARY_OPERATOR(-, UShort);
290 IMPLEMENT_BINARY_OPERATOR(-, Short);
291 IMPLEMENT_BINARY_OPERATOR(-, UInt);
292 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000293 IMPLEMENT_BINARY_OPERATOR(-, ULong);
294 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000295 IMPLEMENT_BINARY_OPERATOR(-, Float);
296 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000297 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000298 default:
299 cout << "Unhandled type for Sub instruction: " << Ty << endl;
300 }
301 return Dest;
302}
303
Chris Lattnerc2593162001-10-27 08:28:11 +0000304static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
305 const Type *Ty, ExecutionContext &SF) {
306 GenericValue Dest;
307 switch (Ty->getPrimitiveID()) {
308 IMPLEMENT_BINARY_OPERATOR(*, UByte);
309 IMPLEMENT_BINARY_OPERATOR(*, SByte);
310 IMPLEMENT_BINARY_OPERATOR(*, UShort);
311 IMPLEMENT_BINARY_OPERATOR(*, Short);
312 IMPLEMENT_BINARY_OPERATOR(*, UInt);
313 IMPLEMENT_BINARY_OPERATOR(*, Int);
314 IMPLEMENT_BINARY_OPERATOR(*, ULong);
315 IMPLEMENT_BINARY_OPERATOR(*, Long);
316 IMPLEMENT_BINARY_OPERATOR(*, Float);
317 IMPLEMENT_BINARY_OPERATOR(*, Double);
318 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
319 default:
320 cout << "Unhandled type for Mul instruction: " << Ty << endl;
321 }
322 return Dest;
323}
324
325static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
326 const Type *Ty, ExecutionContext &SF) {
327 GenericValue Dest;
328 switch (Ty->getPrimitiveID()) {
329 IMPLEMENT_BINARY_OPERATOR(/, UByte);
330 IMPLEMENT_BINARY_OPERATOR(/, SByte);
331 IMPLEMENT_BINARY_OPERATOR(/, UShort);
332 IMPLEMENT_BINARY_OPERATOR(/, Short);
333 IMPLEMENT_BINARY_OPERATOR(/, UInt);
334 IMPLEMENT_BINARY_OPERATOR(/, Int);
335 IMPLEMENT_BINARY_OPERATOR(/, ULong);
336 IMPLEMENT_BINARY_OPERATOR(/, Long);
337 IMPLEMENT_BINARY_OPERATOR(/, Float);
338 IMPLEMENT_BINARY_OPERATOR(/, Double);
339 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
340 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000341 cout << "Unhandled type for Div instruction: " << Ty << endl;
342 }
343 return Dest;
344}
345
346static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
347 const Type *Ty, ExecutionContext &SF) {
348 GenericValue Dest;
349 switch (Ty->getPrimitiveID()) {
350 IMPLEMENT_BINARY_OPERATOR(%, UByte);
351 IMPLEMENT_BINARY_OPERATOR(%, SByte);
352 IMPLEMENT_BINARY_OPERATOR(%, UShort);
353 IMPLEMENT_BINARY_OPERATOR(%, Short);
354 IMPLEMENT_BINARY_OPERATOR(%, UInt);
355 IMPLEMENT_BINARY_OPERATOR(%, Int);
356 IMPLEMENT_BINARY_OPERATOR(%, ULong);
357 IMPLEMENT_BINARY_OPERATOR(%, Long);
358 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
359 case Type::FloatTyID:
360 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
361 break;
362 case Type::DoubleTyID:
363 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
364 break;
365 default:
366 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000367 }
368 return Dest;
369}
370
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000371static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000372 const Type *Ty, ExecutionContext &SF) {
373 GenericValue Dest;
374 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000375 IMPLEMENT_BINARY_OPERATOR(&, UByte);
376 IMPLEMENT_BINARY_OPERATOR(&, SByte);
377 IMPLEMENT_BINARY_OPERATOR(&, UShort);
378 IMPLEMENT_BINARY_OPERATOR(&, Short);
379 IMPLEMENT_BINARY_OPERATOR(&, UInt);
380 IMPLEMENT_BINARY_OPERATOR(&, Int);
381 IMPLEMENT_BINARY_OPERATOR(&, ULong);
382 IMPLEMENT_BINARY_OPERATOR(&, Long);
383 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
384 default:
385 cout << "Unhandled type for And instruction: " << Ty << endl;
386 }
387 return Dest;
388}
389
390
391static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
392 const Type *Ty, ExecutionContext &SF) {
393 GenericValue Dest;
394 switch (Ty->getPrimitiveID()) {
395 IMPLEMENT_BINARY_OPERATOR(|, UByte);
396 IMPLEMENT_BINARY_OPERATOR(|, SByte);
397 IMPLEMENT_BINARY_OPERATOR(|, UShort);
398 IMPLEMENT_BINARY_OPERATOR(|, Short);
399 IMPLEMENT_BINARY_OPERATOR(|, UInt);
400 IMPLEMENT_BINARY_OPERATOR(|, Int);
401 IMPLEMENT_BINARY_OPERATOR(|, ULong);
402 IMPLEMENT_BINARY_OPERATOR(|, Long);
403 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
404 default:
405 cout << "Unhandled type for Or instruction: " << Ty << endl;
406 }
407 return Dest;
408}
409
410
411static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
412 const Type *Ty, ExecutionContext &SF) {
413 GenericValue Dest;
414 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000415 IMPLEMENT_BINARY_OPERATOR(^, UByte);
416 IMPLEMENT_BINARY_OPERATOR(^, SByte);
417 IMPLEMENT_BINARY_OPERATOR(^, UShort);
418 IMPLEMENT_BINARY_OPERATOR(^, Short);
419 IMPLEMENT_BINARY_OPERATOR(^, UInt);
420 IMPLEMENT_BINARY_OPERATOR(^, Int);
421 IMPLEMENT_BINARY_OPERATOR(^, ULong);
422 IMPLEMENT_BINARY_OPERATOR(^, Long);
423 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
424 default:
425 cout << "Unhandled type for Xor instruction: " << Ty << endl;
426 }
427 return Dest;
428}
429
430
Chris Lattner92101ac2001-08-23 17:05:04 +0000431#define IMPLEMENT_SETCC(OP, TY) \
432 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
433
Chris Lattner92101ac2001-08-23 17:05:04 +0000434static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
435 const Type *Ty, ExecutionContext &SF) {
436 GenericValue Dest;
437 switch (Ty->getPrimitiveID()) {
438 IMPLEMENT_SETCC(==, UByte);
439 IMPLEMENT_SETCC(==, SByte);
440 IMPLEMENT_SETCC(==, UShort);
441 IMPLEMENT_SETCC(==, Short);
442 IMPLEMENT_SETCC(==, UInt);
443 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000444 IMPLEMENT_SETCC(==, ULong);
445 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000446 IMPLEMENT_SETCC(==, Float);
447 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000448 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000449 default:
450 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
451 }
452 return Dest;
453}
454
455static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
456 const Type *Ty, ExecutionContext &SF) {
457 GenericValue Dest;
458 switch (Ty->getPrimitiveID()) {
459 IMPLEMENT_SETCC(!=, UByte);
460 IMPLEMENT_SETCC(!=, SByte);
461 IMPLEMENT_SETCC(!=, UShort);
462 IMPLEMENT_SETCC(!=, Short);
463 IMPLEMENT_SETCC(!=, UInt);
464 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000465 IMPLEMENT_SETCC(!=, ULong);
466 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 IMPLEMENT_SETCC(!=, Float);
468 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000469 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000470
Chris Lattner92101ac2001-08-23 17:05:04 +0000471 default:
472 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
473 }
474 return Dest;
475}
476
477static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
478 const Type *Ty, ExecutionContext &SF) {
479 GenericValue Dest;
480 switch (Ty->getPrimitiveID()) {
481 IMPLEMENT_SETCC(<=, UByte);
482 IMPLEMENT_SETCC(<=, SByte);
483 IMPLEMENT_SETCC(<=, UShort);
484 IMPLEMENT_SETCC(<=, Short);
485 IMPLEMENT_SETCC(<=, UInt);
486 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000487 IMPLEMENT_SETCC(<=, ULong);
488 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000489 IMPLEMENT_SETCC(<=, Float);
490 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000491 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000492 default:
493 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
494 }
495 return Dest;
496}
497
498static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
499 const Type *Ty, ExecutionContext &SF) {
500 GenericValue Dest;
501 switch (Ty->getPrimitiveID()) {
502 IMPLEMENT_SETCC(>=, UByte);
503 IMPLEMENT_SETCC(>=, SByte);
504 IMPLEMENT_SETCC(>=, UShort);
505 IMPLEMENT_SETCC(>=, Short);
506 IMPLEMENT_SETCC(>=, UInt);
507 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000508 IMPLEMENT_SETCC(>=, ULong);
509 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000510 IMPLEMENT_SETCC(>=, Float);
511 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000512 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000513 default:
514 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
515 }
516 return Dest;
517}
518
519static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
520 const Type *Ty, ExecutionContext &SF) {
521 GenericValue Dest;
522 switch (Ty->getPrimitiveID()) {
523 IMPLEMENT_SETCC(<, UByte);
524 IMPLEMENT_SETCC(<, SByte);
525 IMPLEMENT_SETCC(<, UShort);
526 IMPLEMENT_SETCC(<, Short);
527 IMPLEMENT_SETCC(<, UInt);
528 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000529 IMPLEMENT_SETCC(<, ULong);
530 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 IMPLEMENT_SETCC(<, Float);
532 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000533 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000534 default:
535 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
536 }
537 return Dest;
538}
539
540static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
541 const Type *Ty, ExecutionContext &SF) {
542 GenericValue Dest;
543 switch (Ty->getPrimitiveID()) {
544 IMPLEMENT_SETCC(>, UByte);
545 IMPLEMENT_SETCC(>, SByte);
546 IMPLEMENT_SETCC(>, UShort);
547 IMPLEMENT_SETCC(>, Short);
548 IMPLEMENT_SETCC(>, UInt);
549 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000550 IMPLEMENT_SETCC(>, ULong);
551 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000552 IMPLEMENT_SETCC(>, Float);
553 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000554 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000555 default:
556 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
557 }
558 return Dest;
559}
560
561static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
562 const Type *Ty = I->getOperand(0)->getType();
563 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
564 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
565 GenericValue R; // Result
566
567 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000568 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
569 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
570 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
571 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
572 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000573 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
574 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000575 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000576 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
577 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
578 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
579 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
580 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
581 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
582 default:
583 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000584 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000585 }
586
587 SetValue(I, R, SF);
588}
589
Chris Lattner92101ac2001-08-23 17:05:04 +0000590//===----------------------------------------------------------------------===//
591// Terminator Instruction Implementations
592//===----------------------------------------------------------------------===//
593
Chris Lattnere43db882001-10-27 04:15:57 +0000594void Interpreter::exitCalled(GenericValue GV) {
595 cout << "Program returned ";
596 print(Type::IntTy, GV);
597 cout << " via 'void exit(int)'\n";
598
599 ExitCode = GV.SByteVal;
600 ECStack.clear();
601}
602
Chris Lattner92101ac2001-08-23 17:05:04 +0000603void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
604 const Type *RetTy = 0;
605 GenericValue Result;
606
607 // Save away the return value... (if we are not 'ret void')
608 if (I->getNumOperands()) {
609 RetTy = I->getReturnValue()->getType();
610 Result = getOperandValue(I->getReturnValue(), SF);
611 }
612
613 // Save previously executing meth
614 const Method *M = ECStack.back().CurMethod;
615
616 // Pop the current stack frame... this invalidates SF
617 ECStack.pop_back();
618
619 if (ECStack.empty()) { // Finished main. Put result into exit code...
620 if (RetTy) { // Nonvoid return type?
Chris Lattner5af0c482001-11-07 04:23:00 +0000621 CW << "Method " << M->getType() << " \"" << M->getName()
622 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000623 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000624 cout << endl;
625
626 if (RetTy->isIntegral())
627 ExitCode = Result.SByteVal; // Capture the exit code of the program
628 } else {
629 ExitCode = 0;
630 }
631 return;
632 }
633
634 // If we have a previous stack frame, and we have a previous call, fill in
635 // the return value...
636 //
637 ExecutionContext &NewSF = ECStack.back();
638 if (NewSF.Caller) {
639 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
640 SetValue(NewSF.Caller, Result, NewSF);
641
642 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000643 } else {
644 // This must be a function that is executing because of a user 'call'
645 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000646 CW << "Method " << M->getType() << " \"" << M->getName()
647 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000648 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000649 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000650 }
651}
652
653void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
654 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
655 BasicBlock *Dest;
656
657 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
658 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000659 Value *Cond = I->getCondition();
660 GenericValue CondVal = getOperandValue(Cond, SF);
661 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000662 Dest = I->getSuccessor(1);
663 }
664 SF.CurBB = Dest; // Update CurBB to branch destination
665 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
666}
667
668//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000669// Memory Instruction Implementations
670//===----------------------------------------------------------------------===//
671
Chris Lattner86660982001-08-27 05:16:50 +0000672void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
673 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
674 unsigned NumElements = 1;
675
676 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000677 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000678 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000679 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000680
681 // Get the number of elements being allocated by the array...
682 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
683 NumElements = NumEl.UIntVal;
684 }
685
686 // Allocate enough memory to hold the type...
687 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000688 // FIXME: Don't use CALLOC, use a tainted malloc.
689 Result.PointerVal = (PointerTy)calloc(NumElements, TD.getTypeSize(Ty));
690 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000691 SetValue(I, Result, SF);
692
693 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000694 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000695 }
696}
697
698static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
699 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
700 GenericValue Value = getOperandValue(I->getOperand(0), SF);
701 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000702 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000703}
704
Chris Lattner95c3af52001-10-29 19:32:19 +0000705
706// getElementOffset - The workhorse for getelementptr, load and store. This
707// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
708// the pointer type specified by argument Arg.
709//
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000710static PointerTy getElementOffset(Instruction *I, unsigned ArgOff) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000711 assert(isa<PointerType>(I->getOperand(ArgOff)->getType()) &&
712 "Cannot getElementOffset of a nonpointer type!");
713
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000714 PointerTy Total = 0;
Chris Lattner95c3af52001-10-29 19:32:19 +0000715 const Type *Ty =
716 cast<PointerType>(I->getOperand(ArgOff++)->getType())->getValueType();
717
718 while (ArgOff < I->getNumOperands()) {
719 const StructType *STy = cast<StructType>(Ty);
720 const StructLayout *SLO = TD.getStructLayout(STy);
721
722 // Indicies must be ubyte constants...
723 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
724 assert(CPU->getType() == Type::UByteTy);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000725 unsigned Index = CPU->getValue();
726 Total += SLO->MemberOffsets[Index];
727 Ty = STy->getElementTypes()[Index];
Chris Lattner95c3af52001-10-29 19:32:19 +0000728 }
729
730 return Total;
731}
732
733static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000734 GenericValue SRC = getOperandValue(I->getPtrOperand(), SF);
735 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000736
737 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000738 Result.PointerVal = SrcPtr + getElementOffset(I, 0);
Chris Lattner95c3af52001-10-29 19:32:19 +0000739 SetValue(I, Result, SF);
740}
741
Chris Lattner86660982001-08-27 05:16:50 +0000742static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000743 GenericValue SRC = getOperandValue(I->getPtrOperand(), SF);
744 PointerTy SrcPtr = SRC.PointerVal;
745 PointerTy Offset = getElementOffset(I, 0); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000746 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000747
748 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000749 GenericValue Result;
750
751 switch (I->getType()->getPrimitiveID()) {
752 case Type::BoolTyID:
753 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000754 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000755 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000756 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000757 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000758 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000759 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000760 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
761 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
762 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
763 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000764 default:
765 cout << "Cannot load value of type " << I->getType() << "!\n";
766 }
767
768 SetValue(I, Result, SF);
769}
770
771static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000772 GenericValue SRC = getOperandValue(I->getPtrOperand(), SF);
773 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000774 SrcPtr += getElementOffset(I, 1); // Handle any structure indices
775
776 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000777 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000778
779 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
780 case Type::BoolTyID:
781 case Type::UByteTyID:
782 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
783 case Type::UShortTyID:
784 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
785 case Type::UIntTyID:
786 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000787 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000788 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
789 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000790 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
791 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000792 default:
793 cout << "Cannot store value of type " << I->getType() << "!\n";
794 }
795}
796
797
798//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000799// Miscellaneous Instruction Implementations
800//===----------------------------------------------------------------------===//
801
802void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
803 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000804 vector<GenericValue> ArgVals;
805 ArgVals.reserve(I->getNumOperands()-1);
806 for (unsigned i = 1; i < I->getNumOperands(); ++i)
807 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
808
Chris Lattner070cf5e2001-11-07 20:12:30 +0000809 // To handle indirect calls, we must get the pointer value from the argument
810 // and treat it as a method pointer.
811 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
812
813 callMethod((Method*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000814}
815
816static void executePHINode(PHINode *I, ExecutionContext &SF) {
817 BasicBlock *PrevBB = SF.PrevBB;
818 Value *IncomingValue = 0;
819
820 // Search for the value corresponding to this previous bb...
821 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
822 if (I->getIncomingBlock(--i) == PrevBB) {
823 IncomingValue = I->getIncomingValue(i);
824 break;
825 }
826 }
827 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
828
829 // Found the value, set as the result...
830 SetValue(I, getOperandValue(IncomingValue, SF), SF);
831}
832
Chris Lattner86660982001-08-27 05:16:50 +0000833#define IMPLEMENT_SHIFT(OP, TY) \
834 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
835
836static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
837 const Type *Ty = I->getOperand(0)->getType();
838 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
839 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
840 GenericValue Dest;
841
842 switch (Ty->getPrimitiveID()) {
843 IMPLEMENT_SHIFT(<<, UByte);
844 IMPLEMENT_SHIFT(<<, SByte);
845 IMPLEMENT_SHIFT(<<, UShort);
846 IMPLEMENT_SHIFT(<<, Short);
847 IMPLEMENT_SHIFT(<<, UInt);
848 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000849 IMPLEMENT_SHIFT(<<, ULong);
850 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000851 default:
852 cout << "Unhandled type for Shl instruction: " << Ty << endl;
853 }
854 SetValue(I, Dest, SF);
855}
856
857static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
858 const Type *Ty = I->getOperand(0)->getType();
859 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
860 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
861 GenericValue Dest;
862
863 switch (Ty->getPrimitiveID()) {
864 IMPLEMENT_SHIFT(>>, UByte);
865 IMPLEMENT_SHIFT(>>, SByte);
866 IMPLEMENT_SHIFT(>>, UShort);
867 IMPLEMENT_SHIFT(>>, Short);
868 IMPLEMENT_SHIFT(>>, UInt);
869 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000870 IMPLEMENT_SHIFT(>>, ULong);
871 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000872 default:
873 cout << "Unhandled type for Shr instruction: " << Ty << endl;
874 }
875 SetValue(I, Dest, SF);
876}
877
878#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000879 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000880
881#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
882 case Type::DESTTY##TyID: \
883 switch (SrcTy->getPrimitiveID()) { \
884 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
885 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
886 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
887 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
888 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000889 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
890 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000891 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
892 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000893
894#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
895 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
896 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
897
898#define IMPLEMENT_CAST_CASE_END() \
899 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
900 break; \
901 } \
902 break
903
904#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
905 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
906 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000907 IMPLEMENT_CAST_CASE_END()
908
909static void executeCastInst(CastInst *I, ExecutionContext &SF) {
910 const Type *Ty = I->getType();
911 const Type *SrcTy = I->getOperand(0)->getType();
912 GenericValue Src = getOperandValue(I->getOperand(0), SF);
913 GenericValue Dest;
914
915 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000916 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
917 IMPLEMENT_CAST_CASE(SByte , ( signed char));
918 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
919 IMPLEMENT_CAST_CASE(Short , ( signed char));
920 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
921 IMPLEMENT_CAST_CASE(Int , ( signed int ));
922 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
923 IMPLEMENT_CAST_CASE(Long , ( int64_t));
924 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
925 IMPLEMENT_CAST_CASE(Float , (float));
926 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +0000927 default:
928 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
929 }
930 SetValue(I, Dest, SF);
931}
Chris Lattner92101ac2001-08-23 17:05:04 +0000932
933
934
935
936//===----------------------------------------------------------------------===//
937// Dispatch and Execution Code
938//===----------------------------------------------------------------------===//
939
940MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
941 // Assign slot numbers to the method arguments...
942 const Method::ArgumentListType &ArgList = M->getArgumentList();
943 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
944 AE = ArgList.end(); AI != AE; ++AI) {
945 MethodArgument *MA = *AI;
946 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
947 }
948
949 // Iterate over all of the instructions...
950 unsigned InstNum = 0;
951 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
952 MI != ME; ++MI) {
953 Instruction *I = *MI; // For each instruction...
954 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
955 }
956}
957
958unsigned MethodInfo::getValueSlot(const Value *V) {
959 unsigned Plane = V->getType()->getUniqueID();
960 if (Plane >= NumPlaneElements.size())
961 NumPlaneElements.resize(Plane+1, 0);
962 return NumPlaneElements[Plane]++;
963}
964
965
Chris Lattner92101ac2001-08-23 17:05:04 +0000966//===----------------------------------------------------------------------===//
967// callMethod - Execute the specified method...
968//
Chris Lattner365a76e2001-09-10 04:49:44 +0000969void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
970 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
971 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
972 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000973 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000974 GenericValue Result = callExternalMethod(M, ArgVals);
975 const Type *RetTy = M->getReturnType();
976
977 // Copy the result back into the result variable if we are not returning
978 // void.
979 if (RetTy != Type::VoidTy) {
980 if (!ECStack.empty() && ECStack.back().Caller) {
981 ExecutionContext &SF = ECStack.back();
982 CallInst *Caller = SF.Caller;
983 SetValue(SF.Caller, Result, SF);
984
985 SF.Caller = 0; // We returned from the call...
986 } else {
987 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +0000988 CW << "Method " << M->getType() << " \"" << M->getName()
989 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000990 print(RetTy, Result);
991 cout << endl;
992
993 if (RetTy->isIntegral())
994 ExitCode = Result.SByteVal; // Capture the exit code of the program
995 }
996 }
997
Chris Lattner92101ac2001-08-23 17:05:04 +0000998 return;
999 }
1000
1001 // Process the method, assigning instruction numbers to the instructions in
1002 // the method. Also calculate the number of values for each type slot active.
1003 //
1004 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001005 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001006
Chris Lattner92101ac2001-08-23 17:05:04 +00001007 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1008 StackFrame.CurMethod = M;
1009 StackFrame.CurBB = M->front();
1010 StackFrame.CurInst = StackFrame.CurBB->begin();
1011 StackFrame.MethInfo = MethInfo;
1012
1013 // Initialize the values to nothing...
1014 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001015 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001016 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1017
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001018 // Taint the initial values of stuff
1019 memset(&StackFrame.Values[i][0], 42,
1020 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1021 }
1022
Chris Lattner92101ac2001-08-23 17:05:04 +00001023 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1024
Chris Lattner92101ac2001-08-23 17:05:04 +00001025
Chris Lattner365a76e2001-09-10 04:49:44 +00001026 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001027 assert(ArgVals.size() == M->getArgumentList().size() &&
1028 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001029 unsigned i = 0;
1030 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1031 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1032 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001033 }
1034}
1035
1036// executeInstruction - Interpret a single instruction, increment the "PC", and
1037// return true if the next instruction is a breakpoint...
1038//
1039bool Interpreter::executeInstruction() {
1040 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1041
1042 ExecutionContext &SF = ECStack.back(); // Current stack frame
1043 Instruction *I = *SF.CurInst++; // Increment before execute
1044
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001045 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001046 CW << "Run:" << I;
1047
1048 // Set a sigsetjmp buffer so that we can recover if an error happens during
1049 // instruction execution...
1050 //
1051 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1052 --SF.CurInst; // Back up to erroring instruction
Chris Lattner461f02f2001-11-07 05:31:27 +00001053 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001054 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001055 printStackTrace();
1056 } else {
1057 cout << "CTRL-C Detected, execution halted.\n";
1058 }
1059 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001060 return true;
1061 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001062
Chris Lattner461f02f2001-11-07 05:31:27 +00001063 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001064 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001065 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001066 } else {
1067 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001068 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001069 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1070 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001071 // Memory Instructions
1072 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001073 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1074 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1075 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1076 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001077 case Instruction::GetElementPtr:
1078 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001079
1080 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001081 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1082 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1083 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1084 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1085 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001086 default:
1087 cout << "Don't know how to execute this instruction!\n-->" << I;
1088 }
1089 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001090 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001091
1092 // Reset the current frame location to the top of stack
1093 CurFrame = ECStack.size()-1;
1094
1095 if (CurFrame == -1) return false; // No breakpoint if no code
1096
1097 // Return true if there is a breakpoint annotation on the instruction...
1098 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1099}
1100
1101void Interpreter::stepInstruction() { // Do the 'step' command
1102 if (ECStack.empty()) {
1103 cout << "Error: no program running, cannot step!\n";
1104 return;
1105 }
1106
1107 // Run an instruction...
1108 executeInstruction();
1109
1110 // Print the next instruction to execute...
1111 printCurrentInstruction();
1112}
1113
1114// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001115void Interpreter::nextInstruction() { // Do the 'next' command
1116 if (ECStack.empty()) {
1117 cout << "Error: no program running, cannot 'next'!\n";
1118 return;
1119 }
1120
1121 // If this is a call instruction, step over the call instruction...
1122 // TODO: ICALL, CALL WITH, ...
1123 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001124 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001125 // Step into the function...
1126 if (executeInstruction()) {
1127 // Hit a breakpoint, print current instruction, then return to user...
1128 cout << "Breakpoint hit!\n";
1129 printCurrentInstruction();
1130 return;
1131 }
1132
Chris Lattnera74a6b52001-10-29 14:08:33 +00001133 // If we we able to step into the function, finish it now. We might not be
1134 // able the step into a function, if it's external for example.
1135 if (ECStack.size() != StackSize)
1136 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001137 else
1138 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001139
Chris Lattner92101ac2001-08-23 17:05:04 +00001140 } else {
1141 // Normal instruction, just step...
1142 stepInstruction();
1143 }
1144}
1145
1146void Interpreter::run() {
1147 if (ECStack.empty()) {
1148 cout << "Error: no program running, cannot run!\n";
1149 return;
1150 }
1151
1152 bool HitBreakpoint = false;
1153 while (!ECStack.empty() && !HitBreakpoint) {
1154 // Run an instruction...
1155 HitBreakpoint = executeInstruction();
1156 }
1157
1158 if (HitBreakpoint) {
1159 cout << "Breakpoint hit!\n";
1160 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001161 // Print the next instruction to execute...
1162 printCurrentInstruction();
1163}
1164
1165void Interpreter::finish() {
1166 if (ECStack.empty()) {
1167 cout << "Error: no program running, cannot run!\n";
1168 return;
1169 }
1170
1171 unsigned StackSize = ECStack.size();
1172 bool HitBreakpoint = false;
1173 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1174 // Run an instruction...
1175 HitBreakpoint = executeInstruction();
1176 }
1177
1178 if (HitBreakpoint) {
1179 cout << "Breakpoint hit!\n";
1180 }
1181
1182 // Print the next instruction to execute...
1183 printCurrentInstruction();
1184}
1185
1186
1187
1188// printCurrentInstruction - Print out the instruction that the virtual PC is
1189// at, or fail silently if no program is running.
1190//
1191void Interpreter::printCurrentInstruction() {
1192 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001193 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1194 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1195
Chris Lattner92101ac2001-08-23 17:05:04 +00001196 Instruction *I = *ECStack.back().CurInst;
1197 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1198 assert(IN && "Instruction has no numbering annotation!");
1199 cout << "#" << IN->InstNum << I;
1200 }
1201}
1202
1203void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001204 switch (Ty->getPrimitiveID()) {
1205 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1206 case Type::SByteTyID: cout << V.SByteVal; break;
1207 case Type::UByteTyID: cout << V.UByteVal; break;
1208 case Type::ShortTyID: cout << V.ShortVal; break;
1209 case Type::UShortTyID: cout << V.UShortVal; break;
1210 case Type::IntTyID: cout << V.IntVal; break;
1211 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001212 case Type::LongTyID: cout << V.LongVal; break;
1213 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001214 case Type::FloatTyID: cout << V.FloatVal; break;
1215 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001216 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001217 default:
1218 cout << "- Don't know how to print value of this type!";
1219 break;
1220 }
1221}
1222
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001223void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001224 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001225 printValue(Ty, V);
1226}
1227
1228void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001229 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1230 if (!PickedVal) return;
1231
Chris Lattner9636a912001-10-01 16:18:37 +00001232 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001233 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001234 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1235 CW << "type %" << Name << " = " << Ty->getDescription() << endl;
1236 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1237 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001238 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001239 print(PickedVal->getType(),
1240 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001241 cout << endl;
1242 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001243}
1244
Chris Lattner86660982001-08-27 05:16:50 +00001245void Interpreter::infoValue(const string &Name) {
1246 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1247 if (!PickedVal) return;
1248
1249 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001250 print(PickedVal->getType(),
1251 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001252 cout << endl;
1253 printOperandInfo(PickedVal, ECStack[CurFrame]);
1254}
1255
Chris Lattner461f02f2001-11-07 05:31:27 +00001256// printStackFrame - Print information about the specified stack frame, or -1
1257// for the default one.
1258//
1259void Interpreter::printStackFrame(int FrameNo = -1) {
1260 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001261 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001262 const Type *RetTy = Meth->getReturnType();
1263
1264 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1265 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001266
1267 Method::ArgumentListType &Args = Meth->getArgumentList();
1268 for (unsigned i = 0; i < Args.size(); ++i) {
1269 if (i != 0) cout << ", ";
1270 CW << (Value*)Args[i] << "=";
1271
1272 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001273 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001274
1275 cout << ")" << endl;
1276 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001277}
Chris Lattner461f02f2001-11-07 05:31:27 +00001278