blob: 5012012ca6576a0b426efc901e6772b937f0e804 [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 Lattner5af0c482001-11-07 04:23:00 +000026CachedWriter CW; // Object to accellerate printing of LLVM
27
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 Lattner5af0c482001-11-07 04:23:00 +000047 //sigaction(SIGFP, &Action, 0);
48}
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 Lattnerc2593162001-10-27 08:28:11 +000081 Result.ULongVal = 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 Lattnerc2593162001-10-27 08:28:11 +000096 Result.ULongVal = (uint64_t)(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 Lattner5af0c482001-11-07 04:23:00 +0000116 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000117 }
118}
119
120
121
122static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
123 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
124
125 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
126 SF.Values[TyP][getOperandSlot(V)] = Val;
127}
128
129
130//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000131// Annotation Wrangling code
132//===----------------------------------------------------------------------===//
133
134void Interpreter::initializeExecutionEngine() {
135 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
136 &MethodInfo::Create);
137 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
138 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000139 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000140}
141
142// InitializeMemory - Recursive function to apply a ConstPool value into the
143// specified memory location...
144//
145static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000146#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
147 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000148 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000149 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000150 } return
151
152 switch (Init->getType()->getPrimitiveID()) {
153 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
154 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
155 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
156 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
157 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
158 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
159 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
160 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
161 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
162 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
163 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
164#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000165
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166 case Type::ArrayTyID: {
167 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
168 const vector<Use> &Val = CPA->getValues();
169 unsigned ElementSize =
170 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
171 for (unsigned i = 0; i < Val.size(); ++i)
172 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
173 return;
174 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000175
176 case Type::StructTyID: {
177 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
178 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
179 const vector<Use> &Val = CPS->getValues();
180 for (unsigned i = 0; i < Val.size(); ++i)
181 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
182 Addr+SL->MemberOffsets[i]);
183 return;
184 }
185
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000186 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000187 if (isa<ConstPoolPointerNull>(Init)) {
188 *(void**)Addr = 0;
189 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
190 GlobalAddress *Address =
191 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
192 *(void**)Addr = (GenericValue*)Address->Ptr;
193 } else {
194 assert(0 && "Unknown Constant pointer type!");
195 }
196 return;
197
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000198 default:
Chris Lattner461f02f2001-11-07 05:31:27 +0000199 CW << "Bad Type: " << Init->getType() << endl;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000200 assert(0 && "Unknown constant type to initialize memory with!");
201 }
202}
203
204Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
205 assert(AID == GlobalAddressAID);
206
207 // This annotation will only be created on GlobalValue objects...
208 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
209
210 if (isa<Method>(GVal)) {
211 // The GlobalAddress object for a method is just a pointer to method itself.
212 // Don't delete it when the annotation is gone though!
213 return new GlobalAddress(GVal, false);
214 }
215
216 // Handle the case of a global variable...
217 assert(isa<GlobalVariable>(GVal) &&
218 "Global value found that isn't a method or global variable!");
219 GlobalVariable *GV = cast<GlobalVariable>(GVal);
220
221 // First off, we must allocate space for the global variable to point at...
222 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
223 unsigned NumElements = 1;
224
225 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
226 assert(GV->hasInitializer() && "Const val must have an initializer!");
227 // Allocating a unsized array type?
228 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
229
230 // Get the number of elements being allocated by the array...
231 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
232 }
233
234 // Allocate enough memory to hold the type...
235 void *Addr = malloc(NumElements * TD.getTypeSize(Ty));
236 assert(Addr != 0 && "Null pointer returned by malloc!");
237
238 // Initialize the memory if there is an initializer...
239 if (GV->hasInitializer())
240 InitializeMemory(GV->getInitializer(), (char*)Addr);
241
242 return new GlobalAddress(Addr, true); // Simply invoke the ctor
243}
244
Chris Lattner92101ac2001-08-23 17:05:04 +0000245
246//===----------------------------------------------------------------------===//
247// Binary Instruction Implementations
248//===----------------------------------------------------------------------===//
249
250#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
251 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
252
253static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
254 const Type *Ty, ExecutionContext &SF) {
255 GenericValue Dest;
256 switch (Ty->getPrimitiveID()) {
257 IMPLEMENT_BINARY_OPERATOR(+, UByte);
258 IMPLEMENT_BINARY_OPERATOR(+, SByte);
259 IMPLEMENT_BINARY_OPERATOR(+, UShort);
260 IMPLEMENT_BINARY_OPERATOR(+, Short);
261 IMPLEMENT_BINARY_OPERATOR(+, UInt);
262 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000263 IMPLEMENT_BINARY_OPERATOR(+, ULong);
264 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000265 IMPLEMENT_BINARY_OPERATOR(+, Float);
266 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000267 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000268 default:
269 cout << "Unhandled type for Add instruction: " << Ty << endl;
270 }
271 return Dest;
272}
273
274static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
275 const Type *Ty, ExecutionContext &SF) {
276 GenericValue Dest;
277 switch (Ty->getPrimitiveID()) {
278 IMPLEMENT_BINARY_OPERATOR(-, UByte);
279 IMPLEMENT_BINARY_OPERATOR(-, SByte);
280 IMPLEMENT_BINARY_OPERATOR(-, UShort);
281 IMPLEMENT_BINARY_OPERATOR(-, Short);
282 IMPLEMENT_BINARY_OPERATOR(-, UInt);
283 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000284 IMPLEMENT_BINARY_OPERATOR(-, ULong);
285 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000286 IMPLEMENT_BINARY_OPERATOR(-, Float);
287 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000288 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000289 default:
290 cout << "Unhandled type for Sub instruction: " << Ty << endl;
291 }
292 return Dest;
293}
294
Chris Lattnerc2593162001-10-27 08:28:11 +0000295static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
296 const Type *Ty, ExecutionContext &SF) {
297 GenericValue Dest;
298 switch (Ty->getPrimitiveID()) {
299 IMPLEMENT_BINARY_OPERATOR(*, UByte);
300 IMPLEMENT_BINARY_OPERATOR(*, SByte);
301 IMPLEMENT_BINARY_OPERATOR(*, UShort);
302 IMPLEMENT_BINARY_OPERATOR(*, Short);
303 IMPLEMENT_BINARY_OPERATOR(*, UInt);
304 IMPLEMENT_BINARY_OPERATOR(*, Int);
305 IMPLEMENT_BINARY_OPERATOR(*, ULong);
306 IMPLEMENT_BINARY_OPERATOR(*, Long);
307 IMPLEMENT_BINARY_OPERATOR(*, Float);
308 IMPLEMENT_BINARY_OPERATOR(*, Double);
309 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
310 default:
311 cout << "Unhandled type for Mul instruction: " << Ty << endl;
312 }
313 return Dest;
314}
315
316static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
317 const Type *Ty, ExecutionContext &SF) {
318 GenericValue Dest;
319 switch (Ty->getPrimitiveID()) {
320 IMPLEMENT_BINARY_OPERATOR(/, UByte);
321 IMPLEMENT_BINARY_OPERATOR(/, SByte);
322 IMPLEMENT_BINARY_OPERATOR(/, UShort);
323 IMPLEMENT_BINARY_OPERATOR(/, Short);
324 IMPLEMENT_BINARY_OPERATOR(/, UInt);
325 IMPLEMENT_BINARY_OPERATOR(/, Int);
326 IMPLEMENT_BINARY_OPERATOR(/, ULong);
327 IMPLEMENT_BINARY_OPERATOR(/, Long);
328 IMPLEMENT_BINARY_OPERATOR(/, Float);
329 IMPLEMENT_BINARY_OPERATOR(/, Double);
330 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
331 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000332 cout << "Unhandled type for Div instruction: " << Ty << endl;
333 }
334 return Dest;
335}
336
337static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
338 const Type *Ty, ExecutionContext &SF) {
339 GenericValue Dest;
340 switch (Ty->getPrimitiveID()) {
341 IMPLEMENT_BINARY_OPERATOR(%, UByte);
342 IMPLEMENT_BINARY_OPERATOR(%, SByte);
343 IMPLEMENT_BINARY_OPERATOR(%, UShort);
344 IMPLEMENT_BINARY_OPERATOR(%, Short);
345 IMPLEMENT_BINARY_OPERATOR(%, UInt);
346 IMPLEMENT_BINARY_OPERATOR(%, Int);
347 IMPLEMENT_BINARY_OPERATOR(%, ULong);
348 IMPLEMENT_BINARY_OPERATOR(%, Long);
349 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
350 case Type::FloatTyID:
351 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
352 break;
353 case Type::DoubleTyID:
354 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
355 break;
356 default:
357 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000358 }
359 return Dest;
360}
361
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000362static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
363 const Type *Ty, ExecutionContext &SF) {
364 GenericValue Dest;
365 switch (Ty->getPrimitiveID()) {
366 IMPLEMENT_BINARY_OPERATOR(^, UByte);
367 IMPLEMENT_BINARY_OPERATOR(^, SByte);
368 IMPLEMENT_BINARY_OPERATOR(^, UShort);
369 IMPLEMENT_BINARY_OPERATOR(^, Short);
370 IMPLEMENT_BINARY_OPERATOR(^, UInt);
371 IMPLEMENT_BINARY_OPERATOR(^, Int);
372 IMPLEMENT_BINARY_OPERATOR(^, ULong);
373 IMPLEMENT_BINARY_OPERATOR(^, Long);
374 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
375 default:
376 cout << "Unhandled type for Xor instruction: " << Ty << endl;
377 }
378 return Dest;
379}
380
381
Chris Lattner92101ac2001-08-23 17:05:04 +0000382#define IMPLEMENT_SETCC(OP, TY) \
383 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
384
Chris Lattner92101ac2001-08-23 17:05:04 +0000385static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
386 const Type *Ty, ExecutionContext &SF) {
387 GenericValue Dest;
388 switch (Ty->getPrimitiveID()) {
389 IMPLEMENT_SETCC(==, UByte);
390 IMPLEMENT_SETCC(==, SByte);
391 IMPLEMENT_SETCC(==, UShort);
392 IMPLEMENT_SETCC(==, Short);
393 IMPLEMENT_SETCC(==, UInt);
394 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000395 IMPLEMENT_SETCC(==, ULong);
396 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000397 IMPLEMENT_SETCC(==, Float);
398 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000399 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000400 default:
401 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
402 }
403 return Dest;
404}
405
406static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
407 const Type *Ty, ExecutionContext &SF) {
408 GenericValue Dest;
409 switch (Ty->getPrimitiveID()) {
410 IMPLEMENT_SETCC(!=, UByte);
411 IMPLEMENT_SETCC(!=, SByte);
412 IMPLEMENT_SETCC(!=, UShort);
413 IMPLEMENT_SETCC(!=, Short);
414 IMPLEMENT_SETCC(!=, UInt);
415 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000416 IMPLEMENT_SETCC(!=, ULong);
417 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000418 IMPLEMENT_SETCC(!=, Float);
419 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000420 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000421 default:
422 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
423 }
424 return Dest;
425}
426
427static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
428 const Type *Ty, ExecutionContext &SF) {
429 GenericValue Dest;
430 switch (Ty->getPrimitiveID()) {
431 IMPLEMENT_SETCC(<=, UByte);
432 IMPLEMENT_SETCC(<=, SByte);
433 IMPLEMENT_SETCC(<=, UShort);
434 IMPLEMENT_SETCC(<=, Short);
435 IMPLEMENT_SETCC(<=, UInt);
436 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000437 IMPLEMENT_SETCC(<=, ULong);
438 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000439 IMPLEMENT_SETCC(<=, Float);
440 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000441 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 default:
443 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
444 }
445 return Dest;
446}
447
448static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
449 const Type *Ty, ExecutionContext &SF) {
450 GenericValue Dest;
451 switch (Ty->getPrimitiveID()) {
452 IMPLEMENT_SETCC(>=, UByte);
453 IMPLEMENT_SETCC(>=, SByte);
454 IMPLEMENT_SETCC(>=, UShort);
455 IMPLEMENT_SETCC(>=, Short);
456 IMPLEMENT_SETCC(>=, UInt);
457 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000458 IMPLEMENT_SETCC(>=, ULong);
459 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 IMPLEMENT_SETCC(>=, Float);
461 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000462 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000463 default:
464 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
465 }
466 return Dest;
467}
468
469static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
470 const Type *Ty, ExecutionContext &SF) {
471 GenericValue Dest;
472 switch (Ty->getPrimitiveID()) {
473 IMPLEMENT_SETCC(<, UByte);
474 IMPLEMENT_SETCC(<, SByte);
475 IMPLEMENT_SETCC(<, UShort);
476 IMPLEMENT_SETCC(<, Short);
477 IMPLEMENT_SETCC(<, UInt);
478 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000479 IMPLEMENT_SETCC(<, ULong);
480 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 IMPLEMENT_SETCC(<, Float);
482 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000483 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000484 default:
485 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
486 }
487 return Dest;
488}
489
490static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
491 const Type *Ty, ExecutionContext &SF) {
492 GenericValue Dest;
493 switch (Ty->getPrimitiveID()) {
494 IMPLEMENT_SETCC(>, UByte);
495 IMPLEMENT_SETCC(>, SByte);
496 IMPLEMENT_SETCC(>, UShort);
497 IMPLEMENT_SETCC(>, Short);
498 IMPLEMENT_SETCC(>, UInt);
499 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000500 IMPLEMENT_SETCC(>, ULong);
501 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 IMPLEMENT_SETCC(>, Float);
503 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000504 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 default:
506 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
507 }
508 return Dest;
509}
510
511static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
512 const Type *Ty = I->getOperand(0)->getType();
513 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
514 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
515 GenericValue R; // Result
516
517 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000518 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
519 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
520 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
521 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
522 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000523 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000524 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
525 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
526 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
527 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
528 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
529 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
530 default:
531 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000532 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 }
534
535 SetValue(I, R, SF);
536}
537
Chris Lattner92101ac2001-08-23 17:05:04 +0000538//===----------------------------------------------------------------------===//
539// Terminator Instruction Implementations
540//===----------------------------------------------------------------------===//
541
Chris Lattnere43db882001-10-27 04:15:57 +0000542void Interpreter::exitCalled(GenericValue GV) {
543 cout << "Program returned ";
544 print(Type::IntTy, GV);
545 cout << " via 'void exit(int)'\n";
546
547 ExitCode = GV.SByteVal;
548 ECStack.clear();
549}
550
Chris Lattner92101ac2001-08-23 17:05:04 +0000551void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
552 const Type *RetTy = 0;
553 GenericValue Result;
554
555 // Save away the return value... (if we are not 'ret void')
556 if (I->getNumOperands()) {
557 RetTy = I->getReturnValue()->getType();
558 Result = getOperandValue(I->getReturnValue(), SF);
559 }
560
561 // Save previously executing meth
562 const Method *M = ECStack.back().CurMethod;
563
564 // Pop the current stack frame... this invalidates SF
565 ECStack.pop_back();
566
567 if (ECStack.empty()) { // Finished main. Put result into exit code...
568 if (RetTy) { // Nonvoid return type?
Chris Lattner5af0c482001-11-07 04:23:00 +0000569 CW << "Method " << M->getType() << " \"" << M->getName()
570 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000571 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000572 cout << endl;
573
574 if (RetTy->isIntegral())
575 ExitCode = Result.SByteVal; // Capture the exit code of the program
576 } else {
577 ExitCode = 0;
578 }
579 return;
580 }
581
582 // If we have a previous stack frame, and we have a previous call, fill in
583 // the return value...
584 //
585 ExecutionContext &NewSF = ECStack.back();
586 if (NewSF.Caller) {
587 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
588 SetValue(NewSF.Caller, Result, NewSF);
589
590 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000591 } else {
592 // This must be a function that is executing because of a user 'call'
593 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000594 CW << "Method " << M->getType() << " \"" << M->getName()
595 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000596 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000597 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000598 }
599}
600
601void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
602 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
603 BasicBlock *Dest;
604
605 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
606 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000607 Value *Cond = I->getCondition();
608 GenericValue CondVal = getOperandValue(Cond, SF);
609 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000610 Dest = I->getSuccessor(1);
611 }
612 SF.CurBB = Dest; // Update CurBB to branch destination
613 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
614}
615
616//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000617// Memory Instruction Implementations
618//===----------------------------------------------------------------------===//
619
Chris Lattner86660982001-08-27 05:16:50 +0000620void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
621 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
622 unsigned NumElements = 1;
623
624 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000625 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000626 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000627 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000628
629 // Get the number of elements being allocated by the array...
630 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
631 NumElements = NumEl.UIntVal;
632 }
633
634 // Allocate enough memory to hold the type...
635 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +0000636 Result.ULongVal = (uint64_t)malloc(NumElements * TD.getTypeSize(Ty));
637 assert(Result.ULongVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000638 SetValue(I, Result, SF);
639
640 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000641 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000642 }
643}
644
645static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
646 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
647 GenericValue Value = getOperandValue(I->getOperand(0), SF);
648 // TODO: Check to make sure memory is allocated
Chris Lattnerc2593162001-10-27 08:28:11 +0000649 free((void*)Value.ULongVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000650}
651
Chris Lattner95c3af52001-10-29 19:32:19 +0000652
653// getElementOffset - The workhorse for getelementptr, load and store. This
654// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
655// the pointer type specified by argument Arg.
656//
657static uint64_t getElementOffset(Instruction *I, unsigned ArgOff) {
658 assert(isa<PointerType>(I->getOperand(ArgOff)->getType()) &&
659 "Cannot getElementOffset of a nonpointer type!");
660
661 uint64_t Total = 0;
662 const Type *Ty =
663 cast<PointerType>(I->getOperand(ArgOff++)->getType())->getValueType();
664
665 while (ArgOff < I->getNumOperands()) {
666 const StructType *STy = cast<StructType>(Ty);
667 const StructLayout *SLO = TD.getStructLayout(STy);
668
669 // Indicies must be ubyte constants...
670 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
671 assert(CPU->getType() == Type::UByteTy);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000672 unsigned Index = CPU->getValue();
673 Total += SLO->MemberOffsets[Index];
674 Ty = STy->getElementTypes()[Index];
Chris Lattner95c3af52001-10-29 19:32:19 +0000675 }
676
677 return Total;
678}
679
680static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
681 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
682
683 GenericValue Result;
684 Result.ULongVal = SrcPtr + getElementOffset(I, 0);
685 SetValue(I, Result, SF);
686}
687
Chris Lattner86660982001-08-27 05:16:50 +0000688static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000689 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
Chris Lattnerbb76f022001-10-30 20:27:31 +0000690 uint64_t Offset = getElementOffset(I, 0); // Handle any structure indices
691 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000692
693 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000694 GenericValue Result;
695
696 switch (I->getType()->getPrimitiveID()) {
697 case Type::BoolTyID:
698 case Type::UByteTyID:
699 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
700 case Type::UShortTyID:
701 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
702 case Type::UIntTyID:
703 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000704 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000705 case Type::LongTyID:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000706 case Type::PointerTyID: Result.ULongVal = Ptr->PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000707 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
708 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000709 default:
710 cout << "Cannot load value of type " << I->getType() << "!\n";
711 }
712
713 SetValue(I, Result, SF);
714}
715
716static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000717 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
718 SrcPtr += getElementOffset(I, 1); // Handle any structure indices
719
720 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000721 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000722
723 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
724 case Type::BoolTyID:
725 case Type::UByteTyID:
726 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
727 case Type::UShortTyID:
728 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
729 case Type::UIntTyID:
730 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000731 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000732 case Type::LongTyID:
733 case Type::PointerTyID: Ptr->LongVal = Val.LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000734 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
735 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000736 default:
737 cout << "Cannot store value of type " << I->getType() << "!\n";
738 }
739}
740
741
742//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000743// Miscellaneous Instruction Implementations
744//===----------------------------------------------------------------------===//
745
746void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
747 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000748 vector<GenericValue> ArgVals;
749 ArgVals.reserve(I->getNumOperands()-1);
750 for (unsigned i = 1; i < I->getNumOperands(); ++i)
751 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
752
753 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000754}
755
756static void executePHINode(PHINode *I, ExecutionContext &SF) {
757 BasicBlock *PrevBB = SF.PrevBB;
758 Value *IncomingValue = 0;
759
760 // Search for the value corresponding to this previous bb...
761 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
762 if (I->getIncomingBlock(--i) == PrevBB) {
763 IncomingValue = I->getIncomingValue(i);
764 break;
765 }
766 }
767 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
768
769 // Found the value, set as the result...
770 SetValue(I, getOperandValue(IncomingValue, SF), SF);
771}
772
Chris Lattner86660982001-08-27 05:16:50 +0000773#define IMPLEMENT_SHIFT(OP, TY) \
774 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
775
776static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
777 const Type *Ty = I->getOperand(0)->getType();
778 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
779 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
780 GenericValue Dest;
781
782 switch (Ty->getPrimitiveID()) {
783 IMPLEMENT_SHIFT(<<, UByte);
784 IMPLEMENT_SHIFT(<<, SByte);
785 IMPLEMENT_SHIFT(<<, UShort);
786 IMPLEMENT_SHIFT(<<, Short);
787 IMPLEMENT_SHIFT(<<, UInt);
788 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000789 IMPLEMENT_SHIFT(<<, ULong);
790 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000791 default:
792 cout << "Unhandled type for Shl instruction: " << Ty << endl;
793 }
794 SetValue(I, Dest, SF);
795}
796
797static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
798 const Type *Ty = I->getOperand(0)->getType();
799 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
800 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
801 GenericValue Dest;
802
803 switch (Ty->getPrimitiveID()) {
804 IMPLEMENT_SHIFT(>>, UByte);
805 IMPLEMENT_SHIFT(>>, SByte);
806 IMPLEMENT_SHIFT(>>, UShort);
807 IMPLEMENT_SHIFT(>>, Short);
808 IMPLEMENT_SHIFT(>>, UInt);
809 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000810 IMPLEMENT_SHIFT(>>, ULong);
811 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000812 default:
813 cout << "Unhandled type for Shr instruction: " << Ty << endl;
814 }
815 SetValue(I, Dest, SF);
816}
817
818#define IMPLEMENT_CAST(DTY, DCTY, STY) \
819 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
820
821#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
822 case Type::DESTTY##TyID: \
823 switch (SrcTy->getPrimitiveID()) { \
824 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
825 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
826 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
827 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
828 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000829 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
830 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000831 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
832 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000833
834#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
835 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
836 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
837
838#define IMPLEMENT_CAST_CASE_END() \
839 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
840 break; \
841 } \
842 break
843
844#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
845 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
846 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000847 IMPLEMENT_CAST_CASE_END()
848
849static void executeCastInst(CastInst *I, ExecutionContext &SF) {
850 const Type *Ty = I->getType();
851 const Type *SrcTy = I->getOperand(0)->getType();
852 GenericValue Src = getOperandValue(I->getOperand(0), SF);
853 GenericValue Dest;
854
855 switch (Ty->getPrimitiveID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000856 IMPLEMENT_CAST_CASE(UByte , unsigned char);
857 IMPLEMENT_CAST_CASE(SByte , signed char);
858 IMPLEMENT_CAST_CASE(UShort , unsigned short);
859 IMPLEMENT_CAST_CASE(Short , signed char);
860 IMPLEMENT_CAST_CASE(UInt , unsigned int );
861 IMPLEMENT_CAST_CASE(Int , signed int );
862 IMPLEMENT_CAST_CASE(ULong , uint64_t);
863 IMPLEMENT_CAST_CASE(Long , int64_t);
Chris Lattnerc2593162001-10-27 08:28:11 +0000864 IMPLEMENT_CAST_CASE(Pointer, uint64_t);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000865 IMPLEMENT_CAST_CASE(Float , float);
866 IMPLEMENT_CAST_CASE(Double , double);
Chris Lattner86660982001-08-27 05:16:50 +0000867 default:
868 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
869 }
870 SetValue(I, Dest, SF);
871}
Chris Lattner92101ac2001-08-23 17:05:04 +0000872
873
874
875
876//===----------------------------------------------------------------------===//
877// Dispatch and Execution Code
878//===----------------------------------------------------------------------===//
879
880MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
881 // Assign slot numbers to the method arguments...
882 const Method::ArgumentListType &ArgList = M->getArgumentList();
883 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
884 AE = ArgList.end(); AI != AE; ++AI) {
885 MethodArgument *MA = *AI;
886 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
887 }
888
889 // Iterate over all of the instructions...
890 unsigned InstNum = 0;
891 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
892 MI != ME; ++MI) {
893 Instruction *I = *MI; // For each instruction...
894 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
895 }
896}
897
898unsigned MethodInfo::getValueSlot(const Value *V) {
899 unsigned Plane = V->getType()->getUniqueID();
900 if (Plane >= NumPlaneElements.size())
901 NumPlaneElements.resize(Plane+1, 0);
902 return NumPlaneElements[Plane]++;
903}
904
905
Chris Lattner92101ac2001-08-23 17:05:04 +0000906//===----------------------------------------------------------------------===//
907// callMethod - Execute the specified method...
908//
Chris Lattner365a76e2001-09-10 04:49:44 +0000909void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
910 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
911 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
912 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000913 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000914 GenericValue Result = callExternalMethod(M, ArgVals);
915 const Type *RetTy = M->getReturnType();
916
917 // Copy the result back into the result variable if we are not returning
918 // void.
919 if (RetTy != Type::VoidTy) {
920 if (!ECStack.empty() && ECStack.back().Caller) {
921 ExecutionContext &SF = ECStack.back();
922 CallInst *Caller = SF.Caller;
923 SetValue(SF.Caller, Result, SF);
924
925 SF.Caller = 0; // We returned from the call...
926 } else {
927 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +0000928 CW << "Method " << M->getType() << " \"" << M->getName()
929 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000930 print(RetTy, Result);
931 cout << endl;
932
933 if (RetTy->isIntegral())
934 ExitCode = Result.SByteVal; // Capture the exit code of the program
935 }
936 }
937
Chris Lattner92101ac2001-08-23 17:05:04 +0000938 return;
939 }
940
941 // Process the method, assigning instruction numbers to the instructions in
942 // the method. Also calculate the number of values for each type slot active.
943 //
944 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000945 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000946
Chris Lattner92101ac2001-08-23 17:05:04 +0000947 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
948 StackFrame.CurMethod = M;
949 StackFrame.CurBB = M->front();
950 StackFrame.CurInst = StackFrame.CurBB->begin();
951 StackFrame.MethInfo = MethInfo;
952
953 // Initialize the values to nothing...
954 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
955 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
956 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
957
958 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
959
Chris Lattner92101ac2001-08-23 17:05:04 +0000960
Chris Lattner365a76e2001-09-10 04:49:44 +0000961 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000962 assert(ArgVals.size() == M->getArgumentList().size() &&
963 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +0000964 unsigned i = 0;
965 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
966 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
967 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000968 }
969}
970
971// executeInstruction - Interpret a single instruction, increment the "PC", and
972// return true if the next instruction is a breakpoint...
973//
974bool Interpreter::executeInstruction() {
975 assert(!ECStack.empty() && "No program running, cannot execute inst!");
976
977 ExecutionContext &SF = ECStack.back(); // Current stack frame
978 Instruction *I = *SF.CurInst++; // Increment before execute
979
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000980 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +0000981 CW << "Run:" << I;
982
983 // Set a sigsetjmp buffer so that we can recover if an error happens during
984 // instruction execution...
985 //
986 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
987 --SF.CurInst; // Back up to erroring instruction
Chris Lattner461f02f2001-11-07 05:31:27 +0000988 if (SigNo != SIGINT) {
989 cout << "EXCEPTION OCCURRED [Signal " << _sys_siglistp[SigNo] << "]:\n";
990 printStackTrace();
991 } else {
992 cout << "CTRL-C Detected, execution halted.\n";
993 }
994 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +0000995 return true;
996 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000997
Chris Lattner461f02f2001-11-07 05:31:27 +0000998 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +0000999 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001000 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001001 } else {
1002 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001003 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001004 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1005 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001006 // Memory Instructions
1007 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001008 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1009 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1010 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1011 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001012 case Instruction::GetElementPtr:
1013 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001014
1015 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001016 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1017 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1018 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1019 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1020 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001021 default:
1022 cout << "Don't know how to execute this instruction!\n-->" << I;
1023 }
1024 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001025 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001026
1027 // Reset the current frame location to the top of stack
1028 CurFrame = ECStack.size()-1;
1029
1030 if (CurFrame == -1) return false; // No breakpoint if no code
1031
1032 // Return true if there is a breakpoint annotation on the instruction...
1033 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1034}
1035
1036void Interpreter::stepInstruction() { // Do the 'step' command
1037 if (ECStack.empty()) {
1038 cout << "Error: no program running, cannot step!\n";
1039 return;
1040 }
1041
1042 // Run an instruction...
1043 executeInstruction();
1044
1045 // Print the next instruction to execute...
1046 printCurrentInstruction();
1047}
1048
1049// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001050void Interpreter::nextInstruction() { // Do the 'next' command
1051 if (ECStack.empty()) {
1052 cout << "Error: no program running, cannot 'next'!\n";
1053 return;
1054 }
1055
1056 // If this is a call instruction, step over the call instruction...
1057 // TODO: ICALL, CALL WITH, ...
1058 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001059 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001060 // Step into the function...
1061 if (executeInstruction()) {
1062 // Hit a breakpoint, print current instruction, then return to user...
1063 cout << "Breakpoint hit!\n";
1064 printCurrentInstruction();
1065 return;
1066 }
1067
Chris Lattnera74a6b52001-10-29 14:08:33 +00001068 // If we we able to step into the function, finish it now. We might not be
1069 // able the step into a function, if it's external for example.
1070 if (ECStack.size() != StackSize)
1071 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001072 else
1073 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001074
Chris Lattner92101ac2001-08-23 17:05:04 +00001075 } else {
1076 // Normal instruction, just step...
1077 stepInstruction();
1078 }
1079}
1080
1081void Interpreter::run() {
1082 if (ECStack.empty()) {
1083 cout << "Error: no program running, cannot run!\n";
1084 return;
1085 }
1086
1087 bool HitBreakpoint = false;
1088 while (!ECStack.empty() && !HitBreakpoint) {
1089 // Run an instruction...
1090 HitBreakpoint = executeInstruction();
1091 }
1092
1093 if (HitBreakpoint) {
1094 cout << "Breakpoint hit!\n";
1095 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001096 // Print the next instruction to execute...
1097 printCurrentInstruction();
1098}
1099
1100void Interpreter::finish() {
1101 if (ECStack.empty()) {
1102 cout << "Error: no program running, cannot run!\n";
1103 return;
1104 }
1105
1106 unsigned StackSize = ECStack.size();
1107 bool HitBreakpoint = false;
1108 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1109 // Run an instruction...
1110 HitBreakpoint = executeInstruction();
1111 }
1112
1113 if (HitBreakpoint) {
1114 cout << "Breakpoint hit!\n";
1115 }
1116
1117 // Print the next instruction to execute...
1118 printCurrentInstruction();
1119}
1120
1121
1122
1123// printCurrentInstruction - Print out the instruction that the virtual PC is
1124// at, or fail silently if no program is running.
1125//
1126void Interpreter::printCurrentInstruction() {
1127 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001128 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1129 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1130
Chris Lattner92101ac2001-08-23 17:05:04 +00001131 Instruction *I = *ECStack.back().CurInst;
1132 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1133 assert(IN && "Instruction has no numbering annotation!");
1134 cout << "#" << IN->InstNum << I;
1135 }
1136}
1137
1138void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001139 switch (Ty->getPrimitiveID()) {
1140 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1141 case Type::SByteTyID: cout << V.SByteVal; break;
1142 case Type::UByteTyID: cout << V.UByteVal; break;
1143 case Type::ShortTyID: cout << V.ShortVal; break;
1144 case Type::UShortTyID: cout << V.UShortVal; break;
1145 case Type::IntTyID: cout << V.IntVal; break;
1146 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001147 case Type::LongTyID: cout << V.LongVal; break;
1148 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001149 case Type::FloatTyID: cout << V.FloatVal; break;
1150 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerc2593162001-10-27 08:28:11 +00001151 case Type::PointerTyID:cout << (void*)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001152 default:
1153 cout << "- Don't know how to print value of this type!";
1154 break;
1155 }
1156}
1157
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001158void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001159 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001160 printValue(Ty, V);
1161}
1162
1163void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001164 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1165 if (!PickedVal) return;
1166
Chris Lattner9636a912001-10-01 16:18:37 +00001167 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001168 CW << M; // Print the method
Chris Lattner92101ac2001-08-23 17:05:04 +00001169 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001170 print(PickedVal->getType(),
1171 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001172 cout << endl;
1173 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001174}
1175
Chris Lattner86660982001-08-27 05:16:50 +00001176void Interpreter::infoValue(const string &Name) {
1177 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1178 if (!PickedVal) return;
1179
1180 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001181 print(PickedVal->getType(),
1182 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001183 cout << endl;
1184 printOperandInfo(PickedVal, ECStack[CurFrame]);
1185}
1186
Chris Lattner461f02f2001-11-07 05:31:27 +00001187// printStackFrame - Print information about the specified stack frame, or -1
1188// for the default one.
1189//
1190void Interpreter::printStackFrame(int FrameNo = -1) {
1191 if (FrameNo == -1) FrameNo = CurFrame;
1192 cout << ((FrameNo == CurFrame) ? '>' : '-');
1193 Method *Meth = ECStack[FrameNo].CurMethod;
1194 CW << "#" << FrameNo << ". " << (Value*)Meth->getType() << " \""
1195 << Meth->getName() << "\"(";
1196
1197 Method::ArgumentListType &Args = Meth->getArgumentList();
1198 for (unsigned i = 0; i < Args.size(); ++i) {
1199 if (i != 0) cout << ", ";
1200 CW << (Value*)Args[i] << "=";
1201
1202 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001203 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001204
1205 cout << ")" << endl;
1206 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001207}
Chris Lattner461f02f2001-11-07 05:31:27 +00001208