blob: 3541eb8228f3249e15c1490d80efd6439c872dcc [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
809 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000810}
811
812static void executePHINode(PHINode *I, ExecutionContext &SF) {
813 BasicBlock *PrevBB = SF.PrevBB;
814 Value *IncomingValue = 0;
815
816 // Search for the value corresponding to this previous bb...
817 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
818 if (I->getIncomingBlock(--i) == PrevBB) {
819 IncomingValue = I->getIncomingValue(i);
820 break;
821 }
822 }
823 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
824
825 // Found the value, set as the result...
826 SetValue(I, getOperandValue(IncomingValue, SF), SF);
827}
828
Chris Lattner86660982001-08-27 05:16:50 +0000829#define IMPLEMENT_SHIFT(OP, TY) \
830 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
831
832static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
833 const Type *Ty = I->getOperand(0)->getType();
834 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
835 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
836 GenericValue Dest;
837
838 switch (Ty->getPrimitiveID()) {
839 IMPLEMENT_SHIFT(<<, UByte);
840 IMPLEMENT_SHIFT(<<, SByte);
841 IMPLEMENT_SHIFT(<<, UShort);
842 IMPLEMENT_SHIFT(<<, Short);
843 IMPLEMENT_SHIFT(<<, UInt);
844 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000845 IMPLEMENT_SHIFT(<<, ULong);
846 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000847 default:
848 cout << "Unhandled type for Shl instruction: " << Ty << endl;
849 }
850 SetValue(I, Dest, SF);
851}
852
853static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
854 const Type *Ty = I->getOperand(0)->getType();
855 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
856 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
857 GenericValue Dest;
858
859 switch (Ty->getPrimitiveID()) {
860 IMPLEMENT_SHIFT(>>, UByte);
861 IMPLEMENT_SHIFT(>>, SByte);
862 IMPLEMENT_SHIFT(>>, UShort);
863 IMPLEMENT_SHIFT(>>, Short);
864 IMPLEMENT_SHIFT(>>, UInt);
865 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000866 IMPLEMENT_SHIFT(>>, ULong);
867 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000868 default:
869 cout << "Unhandled type for Shr instruction: " << Ty << endl;
870 }
871 SetValue(I, Dest, SF);
872}
873
874#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000875 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000876
877#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
878 case Type::DESTTY##TyID: \
879 switch (SrcTy->getPrimitiveID()) { \
880 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
881 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
882 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
883 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
884 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000885 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
886 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000887 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
888 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000889
890#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
891 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
892 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
893
894#define IMPLEMENT_CAST_CASE_END() \
895 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
896 break; \
897 } \
898 break
899
900#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
901 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
902 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000903 IMPLEMENT_CAST_CASE_END()
904
905static void executeCastInst(CastInst *I, ExecutionContext &SF) {
906 const Type *Ty = I->getType();
907 const Type *SrcTy = I->getOperand(0)->getType();
908 GenericValue Src = getOperandValue(I->getOperand(0), SF);
909 GenericValue Dest;
910
911 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000912 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
913 IMPLEMENT_CAST_CASE(SByte , ( signed char));
914 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
915 IMPLEMENT_CAST_CASE(Short , ( signed char));
916 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
917 IMPLEMENT_CAST_CASE(Int , ( signed int ));
918 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
919 IMPLEMENT_CAST_CASE(Long , ( int64_t));
920 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
921 IMPLEMENT_CAST_CASE(Float , (float));
922 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +0000923 default:
924 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
925 }
926 SetValue(I, Dest, SF);
927}
Chris Lattner92101ac2001-08-23 17:05:04 +0000928
929
930
931
932//===----------------------------------------------------------------------===//
933// Dispatch and Execution Code
934//===----------------------------------------------------------------------===//
935
936MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
937 // Assign slot numbers to the method arguments...
938 const Method::ArgumentListType &ArgList = M->getArgumentList();
939 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
940 AE = ArgList.end(); AI != AE; ++AI) {
941 MethodArgument *MA = *AI;
942 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
943 }
944
945 // Iterate over all of the instructions...
946 unsigned InstNum = 0;
947 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
948 MI != ME; ++MI) {
949 Instruction *I = *MI; // For each instruction...
950 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
951 }
952}
953
954unsigned MethodInfo::getValueSlot(const Value *V) {
955 unsigned Plane = V->getType()->getUniqueID();
956 if (Plane >= NumPlaneElements.size())
957 NumPlaneElements.resize(Plane+1, 0);
958 return NumPlaneElements[Plane]++;
959}
960
961
Chris Lattner92101ac2001-08-23 17:05:04 +0000962//===----------------------------------------------------------------------===//
963// callMethod - Execute the specified method...
964//
Chris Lattner365a76e2001-09-10 04:49:44 +0000965void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
966 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
967 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
968 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000969 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000970 GenericValue Result = callExternalMethod(M, ArgVals);
971 const Type *RetTy = M->getReturnType();
972
973 // Copy the result back into the result variable if we are not returning
974 // void.
975 if (RetTy != Type::VoidTy) {
976 if (!ECStack.empty() && ECStack.back().Caller) {
977 ExecutionContext &SF = ECStack.back();
978 CallInst *Caller = SF.Caller;
979 SetValue(SF.Caller, Result, SF);
980
981 SF.Caller = 0; // We returned from the call...
982 } else {
983 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +0000984 CW << "Method " << M->getType() << " \"" << M->getName()
985 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000986 print(RetTy, Result);
987 cout << endl;
988
989 if (RetTy->isIntegral())
990 ExitCode = Result.SByteVal; // Capture the exit code of the program
991 }
992 }
993
Chris Lattner92101ac2001-08-23 17:05:04 +0000994 return;
995 }
996
997 // Process the method, assigning instruction numbers to the instructions in
998 // the method. Also calculate the number of values for each type slot active.
999 //
1000 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001001 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001002
Chris Lattner92101ac2001-08-23 17:05:04 +00001003 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1004 StackFrame.CurMethod = M;
1005 StackFrame.CurBB = M->front();
1006 StackFrame.CurInst = StackFrame.CurBB->begin();
1007 StackFrame.MethInfo = MethInfo;
1008
1009 // Initialize the values to nothing...
1010 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001011 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001012 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1013
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001014 // Taint the initial values of stuff
1015 memset(&StackFrame.Values[i][0], 42,
1016 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1017 }
1018
Chris Lattner92101ac2001-08-23 17:05:04 +00001019 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1020
Chris Lattner92101ac2001-08-23 17:05:04 +00001021
Chris Lattner365a76e2001-09-10 04:49:44 +00001022 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001023 assert(ArgVals.size() == M->getArgumentList().size() &&
1024 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001025 unsigned i = 0;
1026 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1027 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1028 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001029 }
1030}
1031
1032// executeInstruction - Interpret a single instruction, increment the "PC", and
1033// return true if the next instruction is a breakpoint...
1034//
1035bool Interpreter::executeInstruction() {
1036 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1037
1038 ExecutionContext &SF = ECStack.back(); // Current stack frame
1039 Instruction *I = *SF.CurInst++; // Increment before execute
1040
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001041 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001042 CW << "Run:" << I;
1043
1044 // Set a sigsetjmp buffer so that we can recover if an error happens during
1045 // instruction execution...
1046 //
1047 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1048 --SF.CurInst; // Back up to erroring instruction
Chris Lattner461f02f2001-11-07 05:31:27 +00001049 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001050 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001051 printStackTrace();
1052 } else {
1053 cout << "CTRL-C Detected, execution halted.\n";
1054 }
1055 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001056 return true;
1057 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001058
Chris Lattner461f02f2001-11-07 05:31:27 +00001059 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001060 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001061 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001062 } else {
1063 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001064 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001065 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1066 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001067 // Memory Instructions
1068 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001069 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1070 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1071 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1072 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001073 case Instruction::GetElementPtr:
1074 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001075
1076 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001077 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1078 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1079 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1080 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1081 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001082 default:
1083 cout << "Don't know how to execute this instruction!\n-->" << I;
1084 }
1085 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001086 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001087
1088 // Reset the current frame location to the top of stack
1089 CurFrame = ECStack.size()-1;
1090
1091 if (CurFrame == -1) return false; // No breakpoint if no code
1092
1093 // Return true if there is a breakpoint annotation on the instruction...
1094 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1095}
1096
1097void Interpreter::stepInstruction() { // Do the 'step' command
1098 if (ECStack.empty()) {
1099 cout << "Error: no program running, cannot step!\n";
1100 return;
1101 }
1102
1103 // Run an instruction...
1104 executeInstruction();
1105
1106 // Print the next instruction to execute...
1107 printCurrentInstruction();
1108}
1109
1110// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001111void Interpreter::nextInstruction() { // Do the 'next' command
1112 if (ECStack.empty()) {
1113 cout << "Error: no program running, cannot 'next'!\n";
1114 return;
1115 }
1116
1117 // If this is a call instruction, step over the call instruction...
1118 // TODO: ICALL, CALL WITH, ...
1119 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001120 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001121 // Step into the function...
1122 if (executeInstruction()) {
1123 // Hit a breakpoint, print current instruction, then return to user...
1124 cout << "Breakpoint hit!\n";
1125 printCurrentInstruction();
1126 return;
1127 }
1128
Chris Lattnera74a6b52001-10-29 14:08:33 +00001129 // If we we able to step into the function, finish it now. We might not be
1130 // able the step into a function, if it's external for example.
1131 if (ECStack.size() != StackSize)
1132 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001133 else
1134 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001135
Chris Lattner92101ac2001-08-23 17:05:04 +00001136 } else {
1137 // Normal instruction, just step...
1138 stepInstruction();
1139 }
1140}
1141
1142void Interpreter::run() {
1143 if (ECStack.empty()) {
1144 cout << "Error: no program running, cannot run!\n";
1145 return;
1146 }
1147
1148 bool HitBreakpoint = false;
1149 while (!ECStack.empty() && !HitBreakpoint) {
1150 // Run an instruction...
1151 HitBreakpoint = executeInstruction();
1152 }
1153
1154 if (HitBreakpoint) {
1155 cout << "Breakpoint hit!\n";
1156 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001157 // Print the next instruction to execute...
1158 printCurrentInstruction();
1159}
1160
1161void Interpreter::finish() {
1162 if (ECStack.empty()) {
1163 cout << "Error: no program running, cannot run!\n";
1164 return;
1165 }
1166
1167 unsigned StackSize = ECStack.size();
1168 bool HitBreakpoint = false;
1169 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1170 // Run an instruction...
1171 HitBreakpoint = executeInstruction();
1172 }
1173
1174 if (HitBreakpoint) {
1175 cout << "Breakpoint hit!\n";
1176 }
1177
1178 // Print the next instruction to execute...
1179 printCurrentInstruction();
1180}
1181
1182
1183
1184// printCurrentInstruction - Print out the instruction that the virtual PC is
1185// at, or fail silently if no program is running.
1186//
1187void Interpreter::printCurrentInstruction() {
1188 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001189 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1190 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1191
Chris Lattner92101ac2001-08-23 17:05:04 +00001192 Instruction *I = *ECStack.back().CurInst;
1193 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1194 assert(IN && "Instruction has no numbering annotation!");
1195 cout << "#" << IN->InstNum << I;
1196 }
1197}
1198
1199void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001200 switch (Ty->getPrimitiveID()) {
1201 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1202 case Type::SByteTyID: cout << V.SByteVal; break;
1203 case Type::UByteTyID: cout << V.UByteVal; break;
1204 case Type::ShortTyID: cout << V.ShortVal; break;
1205 case Type::UShortTyID: cout << V.UShortVal; break;
1206 case Type::IntTyID: cout << V.IntVal; break;
1207 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001208 case Type::LongTyID: cout << V.LongVal; break;
1209 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001210 case Type::FloatTyID: cout << V.FloatVal; break;
1211 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001212 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001213 default:
1214 cout << "- Don't know how to print value of this type!";
1215 break;
1216 }
1217}
1218
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001219void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001220 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001221 printValue(Ty, V);
1222}
1223
1224void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001225 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1226 if (!PickedVal) return;
1227
Chris Lattner9636a912001-10-01 16:18:37 +00001228 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001229 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001230 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1231 CW << "type %" << Name << " = " << Ty->getDescription() << endl;
1232 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1233 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001234 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001235 print(PickedVal->getType(),
1236 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001237 cout << endl;
1238 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001239}
1240
Chris Lattner86660982001-08-27 05:16:50 +00001241void Interpreter::infoValue(const string &Name) {
1242 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1243 if (!PickedVal) return;
1244
1245 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001246 print(PickedVal->getType(),
1247 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001248 cout << endl;
1249 printOperandInfo(PickedVal, ECStack[CurFrame]);
1250}
1251
Chris Lattner461f02f2001-11-07 05:31:27 +00001252// printStackFrame - Print information about the specified stack frame, or -1
1253// for the default one.
1254//
1255void Interpreter::printStackFrame(int FrameNo = -1) {
1256 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001257 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001258 const Type *RetTy = Meth->getReturnType();
1259
1260 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1261 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001262
1263 Method::ArgumentListType &Args = Meth->getArgumentList();
1264 for (unsigned i = 0; i < Args.size(); ++i) {
1265 if (i != 0) cout << ", ";
1266 CW << (Value*)Args[i] << "=";
1267
1268 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001269 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001270
1271 cout << ")" << endl;
1272 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001273}
Chris Lattner461f02f2001-11-07 05:31:27 +00001274