blob: 7c3a23d219509bd75b49c722de5c97aafd687051 [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;
30
31extern "C" {
32static void SigHandler(int Signal) {
33 siglongjmp(SignalRecoverBuffer, Signal);
34}
35}
36
37static void initializeSignalHandlers() {
38 struct sigaction Action;
39 Action.sa_handler = SigHandler;
40 Action.sa_flags = SA_SIGINFO;
41 sigemptyset(&Action.sa_mask);
42 sigaction(SIGSEGV, &Action, 0);
43 sigaction(SIGBUS, &Action, 0);
44 //sigaction(SIGFP, &Action, 0);
45}
46
Chris Lattner2e42d3a2001-10-15 05:51:48 +000047
48//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000049// Value Manipulation code
50//===----------------------------------------------------------------------===//
51
52static unsigned getOperandSlot(Value *V) {
53 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
54 assert(SN && "Operand does not have a slot number annotation!");
55 return SN->SlotNum;
56}
57
58#define GET_CONST_VAL(TY, CLASS) \
59 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
60
61static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
62 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
63 GenericValue Result;
64 switch (CPV->getType()->getPrimitiveID()) {
65 GET_CONST_VAL(Bool , ConstPoolBool);
66 GET_CONST_VAL(UByte , ConstPoolUInt);
67 GET_CONST_VAL(SByte , ConstPoolSInt);
68 GET_CONST_VAL(UShort , ConstPoolUInt);
69 GET_CONST_VAL(Short , ConstPoolSInt);
70 GET_CONST_VAL(UInt , ConstPoolUInt);
71 GET_CONST_VAL(Int , ConstPoolSInt);
Chris Lattner7b851ab2001-10-15 19:18:26 +000072 GET_CONST_VAL(ULong , ConstPoolUInt);
73 GET_CONST_VAL(Long , ConstPoolSInt);
Chris Lattner39bb5b42001-10-15 13:25:40 +000074 GET_CONST_VAL(Float , ConstPoolFP);
75 GET_CONST_VAL(Double , ConstPoolFP);
76 case Type::PointerTyID:
77 if (isa<ConstPoolPointerNull>(CPV)) {
Chris Lattnerc2593162001-10-27 08:28:11 +000078 Result.ULongVal = 0;
Chris Lattner39bb5b42001-10-15 13:25:40 +000079 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
80 assert(0 && "Not implemented!");
81 } else {
82 assert(0 && "Unknown constant pointer type!");
83 }
84 break;
85 default:
86 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
87 }
88 return Result;
89 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
90 GlobalAddress *Address =
91 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
92 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +000093 Result.ULongVal = (uint64_t)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +000094 return Result;
95 } else {
96 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +000097 unsigned OpSlot = getOperandSlot(V);
98 assert(TyP < SF.Values.size() &&
99 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000100 return SF.Values[TyP][getOperandSlot(V)];
101 }
102}
103
104static void printOperandInfo(Value *V, ExecutionContext &SF) {
105 if (isa<ConstPoolVal>(V)) {
106 cout << "Constant Pool Value\n";
107 } else if (isa<GlobalValue>(V)) {
108 cout << "Global Value\n";
109 } else {
110 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
111 unsigned Slot = getOperandSlot(V);
112 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattner5af0c482001-11-07 04:23:00 +0000113 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000114 }
115}
116
117
118
119static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
120 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
121
122 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
123 SF.Values[TyP][getOperandSlot(V)] = Val;
124}
125
126
127//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000128// Annotation Wrangling code
129//===----------------------------------------------------------------------===//
130
131void Interpreter::initializeExecutionEngine() {
132 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
133 &MethodInfo::Create);
134 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
135 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000136 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000137}
138
139// InitializeMemory - Recursive function to apply a ConstPool value into the
140// specified memory location...
141//
142static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000143#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
144 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000145 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000146 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000147 } return
148
149 switch (Init->getType()->getPrimitiveID()) {
150 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
151 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
152 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
153 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
154 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
155 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
156 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
157 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
158 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
159 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
160 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
161#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000162
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000163 case Type::ArrayTyID: {
164 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
165 const vector<Use> &Val = CPA->getValues();
166 unsigned ElementSize =
167 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
168 for (unsigned i = 0; i < Val.size(); ++i)
169 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
170 return;
171 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000172
173 case Type::StructTyID: {
174 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
175 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
176 const vector<Use> &Val = CPS->getValues();
177 for (unsigned i = 0; i < Val.size(); ++i)
178 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
179 Addr+SL->MemberOffsets[i]);
180 return;
181 }
182
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000183 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000184 if (isa<ConstPoolPointerNull>(Init)) {
185 *(void**)Addr = 0;
186 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
187 GlobalAddress *Address =
188 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
189 *(void**)Addr = (GenericValue*)Address->Ptr;
190 } else {
191 assert(0 && "Unknown Constant pointer type!");
192 }
193 return;
194
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000195 default:
196 cout << "Bad Type: " << Init->getType()->getDescription() << endl;
197 assert(0 && "Unknown constant type to initialize memory with!");
198 }
199}
200
201Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
202 assert(AID == GlobalAddressAID);
203
204 // This annotation will only be created on GlobalValue objects...
205 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
206
207 if (isa<Method>(GVal)) {
208 // The GlobalAddress object for a method is just a pointer to method itself.
209 // Don't delete it when the annotation is gone though!
210 return new GlobalAddress(GVal, false);
211 }
212
213 // Handle the case of a global variable...
214 assert(isa<GlobalVariable>(GVal) &&
215 "Global value found that isn't a method or global variable!");
216 GlobalVariable *GV = cast<GlobalVariable>(GVal);
217
218 // First off, we must allocate space for the global variable to point at...
219 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
220 unsigned NumElements = 1;
221
222 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
223 assert(GV->hasInitializer() && "Const val must have an initializer!");
224 // Allocating a unsized array type?
225 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
226
227 // Get the number of elements being allocated by the array...
228 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
229 }
230
231 // Allocate enough memory to hold the type...
232 void *Addr = malloc(NumElements * TD.getTypeSize(Ty));
233 assert(Addr != 0 && "Null pointer returned by malloc!");
234
235 // Initialize the memory if there is an initializer...
236 if (GV->hasInitializer())
237 InitializeMemory(GV->getInitializer(), (char*)Addr);
238
239 return new GlobalAddress(Addr, true); // Simply invoke the ctor
240}
241
Chris Lattner92101ac2001-08-23 17:05:04 +0000242
243//===----------------------------------------------------------------------===//
244// Binary Instruction Implementations
245//===----------------------------------------------------------------------===//
246
247#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
248 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
249
250static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
251 const Type *Ty, ExecutionContext &SF) {
252 GenericValue Dest;
253 switch (Ty->getPrimitiveID()) {
254 IMPLEMENT_BINARY_OPERATOR(+, UByte);
255 IMPLEMENT_BINARY_OPERATOR(+, SByte);
256 IMPLEMENT_BINARY_OPERATOR(+, UShort);
257 IMPLEMENT_BINARY_OPERATOR(+, Short);
258 IMPLEMENT_BINARY_OPERATOR(+, UInt);
259 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000260 IMPLEMENT_BINARY_OPERATOR(+, ULong);
261 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000262 IMPLEMENT_BINARY_OPERATOR(+, Float);
263 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000264 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000265 default:
266 cout << "Unhandled type for Add instruction: " << Ty << endl;
267 }
268 return Dest;
269}
270
271static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
272 const Type *Ty, ExecutionContext &SF) {
273 GenericValue Dest;
274 switch (Ty->getPrimitiveID()) {
275 IMPLEMENT_BINARY_OPERATOR(-, UByte);
276 IMPLEMENT_BINARY_OPERATOR(-, SByte);
277 IMPLEMENT_BINARY_OPERATOR(-, UShort);
278 IMPLEMENT_BINARY_OPERATOR(-, Short);
279 IMPLEMENT_BINARY_OPERATOR(-, UInt);
280 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000281 IMPLEMENT_BINARY_OPERATOR(-, ULong);
282 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000283 IMPLEMENT_BINARY_OPERATOR(-, Float);
284 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000285 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000286 default:
287 cout << "Unhandled type for Sub instruction: " << Ty << endl;
288 }
289 return Dest;
290}
291
Chris Lattnerc2593162001-10-27 08:28:11 +0000292static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
293 const Type *Ty, ExecutionContext &SF) {
294 GenericValue Dest;
295 switch (Ty->getPrimitiveID()) {
296 IMPLEMENT_BINARY_OPERATOR(*, UByte);
297 IMPLEMENT_BINARY_OPERATOR(*, SByte);
298 IMPLEMENT_BINARY_OPERATOR(*, UShort);
299 IMPLEMENT_BINARY_OPERATOR(*, Short);
300 IMPLEMENT_BINARY_OPERATOR(*, UInt);
301 IMPLEMENT_BINARY_OPERATOR(*, Int);
302 IMPLEMENT_BINARY_OPERATOR(*, ULong);
303 IMPLEMENT_BINARY_OPERATOR(*, Long);
304 IMPLEMENT_BINARY_OPERATOR(*, Float);
305 IMPLEMENT_BINARY_OPERATOR(*, Double);
306 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
307 default:
308 cout << "Unhandled type for Mul instruction: " << Ty << endl;
309 }
310 return Dest;
311}
312
313static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
314 const Type *Ty, ExecutionContext &SF) {
315 GenericValue Dest;
316 switch (Ty->getPrimitiveID()) {
317 IMPLEMENT_BINARY_OPERATOR(/, UByte);
318 IMPLEMENT_BINARY_OPERATOR(/, SByte);
319 IMPLEMENT_BINARY_OPERATOR(/, UShort);
320 IMPLEMENT_BINARY_OPERATOR(/, Short);
321 IMPLEMENT_BINARY_OPERATOR(/, UInt);
322 IMPLEMENT_BINARY_OPERATOR(/, Int);
323 IMPLEMENT_BINARY_OPERATOR(/, ULong);
324 IMPLEMENT_BINARY_OPERATOR(/, Long);
325 IMPLEMENT_BINARY_OPERATOR(/, Float);
326 IMPLEMENT_BINARY_OPERATOR(/, Double);
327 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
328 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000329 cout << "Unhandled type for Div instruction: " << Ty << endl;
330 }
331 return Dest;
332}
333
334static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
335 const Type *Ty, ExecutionContext &SF) {
336 GenericValue Dest;
337 switch (Ty->getPrimitiveID()) {
338 IMPLEMENT_BINARY_OPERATOR(%, UByte);
339 IMPLEMENT_BINARY_OPERATOR(%, SByte);
340 IMPLEMENT_BINARY_OPERATOR(%, UShort);
341 IMPLEMENT_BINARY_OPERATOR(%, Short);
342 IMPLEMENT_BINARY_OPERATOR(%, UInt);
343 IMPLEMENT_BINARY_OPERATOR(%, Int);
344 IMPLEMENT_BINARY_OPERATOR(%, ULong);
345 IMPLEMENT_BINARY_OPERATOR(%, Long);
346 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
347 case Type::FloatTyID:
348 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
349 break;
350 case Type::DoubleTyID:
351 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
352 break;
353 default:
354 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000355 }
356 return Dest;
357}
358
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000359static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
360 const Type *Ty, ExecutionContext &SF) {
361 GenericValue Dest;
362 switch (Ty->getPrimitiveID()) {
363 IMPLEMENT_BINARY_OPERATOR(^, UByte);
364 IMPLEMENT_BINARY_OPERATOR(^, SByte);
365 IMPLEMENT_BINARY_OPERATOR(^, UShort);
366 IMPLEMENT_BINARY_OPERATOR(^, Short);
367 IMPLEMENT_BINARY_OPERATOR(^, UInt);
368 IMPLEMENT_BINARY_OPERATOR(^, Int);
369 IMPLEMENT_BINARY_OPERATOR(^, ULong);
370 IMPLEMENT_BINARY_OPERATOR(^, Long);
371 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
372 default:
373 cout << "Unhandled type for Xor instruction: " << Ty << endl;
374 }
375 return Dest;
376}
377
378
Chris Lattner92101ac2001-08-23 17:05:04 +0000379#define IMPLEMENT_SETCC(OP, TY) \
380 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
381
Chris Lattner92101ac2001-08-23 17:05:04 +0000382static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
383 const Type *Ty, ExecutionContext &SF) {
384 GenericValue Dest;
385 switch (Ty->getPrimitiveID()) {
386 IMPLEMENT_SETCC(==, UByte);
387 IMPLEMENT_SETCC(==, SByte);
388 IMPLEMENT_SETCC(==, UShort);
389 IMPLEMENT_SETCC(==, Short);
390 IMPLEMENT_SETCC(==, UInt);
391 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000392 IMPLEMENT_SETCC(==, ULong);
393 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000394 IMPLEMENT_SETCC(==, Float);
395 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000396 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000397 default:
398 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
399 }
400 return Dest;
401}
402
403static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
404 const Type *Ty, ExecutionContext &SF) {
405 GenericValue Dest;
406 switch (Ty->getPrimitiveID()) {
407 IMPLEMENT_SETCC(!=, UByte);
408 IMPLEMENT_SETCC(!=, SByte);
409 IMPLEMENT_SETCC(!=, UShort);
410 IMPLEMENT_SETCC(!=, Short);
411 IMPLEMENT_SETCC(!=, UInt);
412 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000413 IMPLEMENT_SETCC(!=, ULong);
414 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000415 IMPLEMENT_SETCC(!=, Float);
416 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000417 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000418 default:
419 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
420 }
421 return Dest;
422}
423
424static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
425 const Type *Ty, ExecutionContext &SF) {
426 GenericValue Dest;
427 switch (Ty->getPrimitiveID()) {
428 IMPLEMENT_SETCC(<=, UByte);
429 IMPLEMENT_SETCC(<=, SByte);
430 IMPLEMENT_SETCC(<=, UShort);
431 IMPLEMENT_SETCC(<=, Short);
432 IMPLEMENT_SETCC(<=, UInt);
433 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000434 IMPLEMENT_SETCC(<=, ULong);
435 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000436 IMPLEMENT_SETCC(<=, Float);
437 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000438 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000439 default:
440 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
441 }
442 return Dest;
443}
444
445static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
446 const Type *Ty, ExecutionContext &SF) {
447 GenericValue Dest;
448 switch (Ty->getPrimitiveID()) {
449 IMPLEMENT_SETCC(>=, UByte);
450 IMPLEMENT_SETCC(>=, SByte);
451 IMPLEMENT_SETCC(>=, UShort);
452 IMPLEMENT_SETCC(>=, Short);
453 IMPLEMENT_SETCC(>=, UInt);
454 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000455 IMPLEMENT_SETCC(>=, ULong);
456 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 IMPLEMENT_SETCC(>=, Float);
458 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000459 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 default:
461 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
462 }
463 return Dest;
464}
465
466static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
467 const Type *Ty, ExecutionContext &SF) {
468 GenericValue Dest;
469 switch (Ty->getPrimitiveID()) {
470 IMPLEMENT_SETCC(<, UByte);
471 IMPLEMENT_SETCC(<, SByte);
472 IMPLEMENT_SETCC(<, UShort);
473 IMPLEMENT_SETCC(<, Short);
474 IMPLEMENT_SETCC(<, UInt);
475 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000476 IMPLEMENT_SETCC(<, ULong);
477 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 IMPLEMENT_SETCC(<, Float);
479 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000480 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 default:
482 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
483 }
484 return Dest;
485}
486
487static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
488 const Type *Ty, ExecutionContext &SF) {
489 GenericValue Dest;
490 switch (Ty->getPrimitiveID()) {
491 IMPLEMENT_SETCC(>, UByte);
492 IMPLEMENT_SETCC(>, SByte);
493 IMPLEMENT_SETCC(>, UShort);
494 IMPLEMENT_SETCC(>, Short);
495 IMPLEMENT_SETCC(>, UInt);
496 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000497 IMPLEMENT_SETCC(>, ULong);
498 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 IMPLEMENT_SETCC(>, Float);
500 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000501 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 default:
503 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
504 }
505 return Dest;
506}
507
508static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
509 const Type *Ty = I->getOperand(0)->getType();
510 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
511 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
512 GenericValue R; // Result
513
514 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000515 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
516 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
517 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
518 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
519 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000520 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
522 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
523 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
524 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
525 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
526 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
527 default:
528 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000529 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000530 }
531
532 SetValue(I, R, SF);
533}
534
Chris Lattner92101ac2001-08-23 17:05:04 +0000535//===----------------------------------------------------------------------===//
536// Terminator Instruction Implementations
537//===----------------------------------------------------------------------===//
538
Chris Lattnere43db882001-10-27 04:15:57 +0000539void Interpreter::exitCalled(GenericValue GV) {
540 cout << "Program returned ";
541 print(Type::IntTy, GV);
542 cout << " via 'void exit(int)'\n";
543
544 ExitCode = GV.SByteVal;
545 ECStack.clear();
546}
547
Chris Lattner92101ac2001-08-23 17:05:04 +0000548void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
549 const Type *RetTy = 0;
550 GenericValue Result;
551
552 // Save away the return value... (if we are not 'ret void')
553 if (I->getNumOperands()) {
554 RetTy = I->getReturnValue()->getType();
555 Result = getOperandValue(I->getReturnValue(), SF);
556 }
557
558 // Save previously executing meth
559 const Method *M = ECStack.back().CurMethod;
560
561 // Pop the current stack frame... this invalidates SF
562 ECStack.pop_back();
563
564 if (ECStack.empty()) { // Finished main. Put result into exit code...
565 if (RetTy) { // Nonvoid return type?
Chris Lattner5af0c482001-11-07 04:23:00 +0000566 CW << "Method " << M->getType() << " \"" << M->getName()
567 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000568 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000569 cout << endl;
570
571 if (RetTy->isIntegral())
572 ExitCode = Result.SByteVal; // Capture the exit code of the program
573 } else {
574 ExitCode = 0;
575 }
576 return;
577 }
578
579 // If we have a previous stack frame, and we have a previous call, fill in
580 // the return value...
581 //
582 ExecutionContext &NewSF = ECStack.back();
583 if (NewSF.Caller) {
584 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
585 SetValue(NewSF.Caller, Result, NewSF);
586
587 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000588 } else {
589 // This must be a function that is executing because of a user 'call'
590 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000591 CW << "Method " << M->getType() << " \"" << M->getName()
592 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000593 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000594 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000595 }
596}
597
598void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
599 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
600 BasicBlock *Dest;
601
602 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
603 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000604 Value *Cond = I->getCondition();
605 GenericValue CondVal = getOperandValue(Cond, SF);
606 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000607 Dest = I->getSuccessor(1);
608 }
609 SF.CurBB = Dest; // Update CurBB to branch destination
610 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
611}
612
613//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000614// Memory Instruction Implementations
615//===----------------------------------------------------------------------===//
616
Chris Lattner86660982001-08-27 05:16:50 +0000617void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
618 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
619 unsigned NumElements = 1;
620
621 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000622 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000623 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000624 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000625
626 // Get the number of elements being allocated by the array...
627 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
628 NumElements = NumEl.UIntVal;
629 }
630
631 // Allocate enough memory to hold the type...
632 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +0000633 Result.ULongVal = (uint64_t)malloc(NumElements * TD.getTypeSize(Ty));
634 assert(Result.ULongVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000635 SetValue(I, Result, SF);
636
637 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000638 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000639 }
640}
641
642static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
643 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
644 GenericValue Value = getOperandValue(I->getOperand(0), SF);
645 // TODO: Check to make sure memory is allocated
Chris Lattnerc2593162001-10-27 08:28:11 +0000646 free((void*)Value.ULongVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000647}
648
Chris Lattner95c3af52001-10-29 19:32:19 +0000649
650// getElementOffset - The workhorse for getelementptr, load and store. This
651// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
652// the pointer type specified by argument Arg.
653//
654static uint64_t getElementOffset(Instruction *I, unsigned ArgOff) {
655 assert(isa<PointerType>(I->getOperand(ArgOff)->getType()) &&
656 "Cannot getElementOffset of a nonpointer type!");
657
658 uint64_t Total = 0;
659 const Type *Ty =
660 cast<PointerType>(I->getOperand(ArgOff++)->getType())->getValueType();
661
662 while (ArgOff < I->getNumOperands()) {
663 const StructType *STy = cast<StructType>(Ty);
664 const StructLayout *SLO = TD.getStructLayout(STy);
665
666 // Indicies must be ubyte constants...
667 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
668 assert(CPU->getType() == Type::UByteTy);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000669 unsigned Index = CPU->getValue();
670 Total += SLO->MemberOffsets[Index];
671 Ty = STy->getElementTypes()[Index];
Chris Lattner95c3af52001-10-29 19:32:19 +0000672 }
673
674 return Total;
675}
676
677static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
678 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
679
680 GenericValue Result;
681 Result.ULongVal = SrcPtr + getElementOffset(I, 0);
682 SetValue(I, Result, SF);
683}
684
Chris Lattner86660982001-08-27 05:16:50 +0000685static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000686 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
Chris Lattnerbb76f022001-10-30 20:27:31 +0000687 uint64_t Offset = getElementOffset(I, 0); // Handle any structure indices
688 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000689
690 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000691 GenericValue Result;
692
693 switch (I->getType()->getPrimitiveID()) {
694 case Type::BoolTyID:
695 case Type::UByteTyID:
696 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
697 case Type::UShortTyID:
698 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
699 case Type::UIntTyID:
700 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000701 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000702 case Type::LongTyID:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000703 case Type::PointerTyID: Result.ULongVal = Ptr->PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000704 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
705 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000706 default:
707 cout << "Cannot load value of type " << I->getType() << "!\n";
708 }
709
710 SetValue(I, Result, SF);
711}
712
713static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000714 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
715 SrcPtr += getElementOffset(I, 1); // Handle any structure indices
716
717 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000718 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000719
720 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
721 case Type::BoolTyID:
722 case Type::UByteTyID:
723 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
724 case Type::UShortTyID:
725 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
726 case Type::UIntTyID:
727 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000728 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000729 case Type::LongTyID:
730 case Type::PointerTyID: Ptr->LongVal = Val.LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000731 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
732 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000733 default:
734 cout << "Cannot store value of type " << I->getType() << "!\n";
735 }
736}
737
738
739//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000740// Miscellaneous Instruction Implementations
741//===----------------------------------------------------------------------===//
742
743void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
744 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000745 vector<GenericValue> ArgVals;
746 ArgVals.reserve(I->getNumOperands()-1);
747 for (unsigned i = 1; i < I->getNumOperands(); ++i)
748 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
749
750 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000751}
752
753static void executePHINode(PHINode *I, ExecutionContext &SF) {
754 BasicBlock *PrevBB = SF.PrevBB;
755 Value *IncomingValue = 0;
756
757 // Search for the value corresponding to this previous bb...
758 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
759 if (I->getIncomingBlock(--i) == PrevBB) {
760 IncomingValue = I->getIncomingValue(i);
761 break;
762 }
763 }
764 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
765
766 // Found the value, set as the result...
767 SetValue(I, getOperandValue(IncomingValue, SF), SF);
768}
769
Chris Lattner86660982001-08-27 05:16:50 +0000770#define IMPLEMENT_SHIFT(OP, TY) \
771 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
772
773static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
774 const Type *Ty = I->getOperand(0)->getType();
775 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
776 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
777 GenericValue Dest;
778
779 switch (Ty->getPrimitiveID()) {
780 IMPLEMENT_SHIFT(<<, UByte);
781 IMPLEMENT_SHIFT(<<, SByte);
782 IMPLEMENT_SHIFT(<<, UShort);
783 IMPLEMENT_SHIFT(<<, Short);
784 IMPLEMENT_SHIFT(<<, UInt);
785 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000786 IMPLEMENT_SHIFT(<<, ULong);
787 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000788 default:
789 cout << "Unhandled type for Shl instruction: " << Ty << endl;
790 }
791 SetValue(I, Dest, SF);
792}
793
794static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
795 const Type *Ty = I->getOperand(0)->getType();
796 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
797 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
798 GenericValue Dest;
799
800 switch (Ty->getPrimitiveID()) {
801 IMPLEMENT_SHIFT(>>, UByte);
802 IMPLEMENT_SHIFT(>>, SByte);
803 IMPLEMENT_SHIFT(>>, UShort);
804 IMPLEMENT_SHIFT(>>, Short);
805 IMPLEMENT_SHIFT(>>, UInt);
806 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000807 IMPLEMENT_SHIFT(>>, ULong);
808 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000809 default:
810 cout << "Unhandled type for Shr instruction: " << Ty << endl;
811 }
812 SetValue(I, Dest, SF);
813}
814
815#define IMPLEMENT_CAST(DTY, DCTY, STY) \
816 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
817
818#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
819 case Type::DESTTY##TyID: \
820 switch (SrcTy->getPrimitiveID()) { \
821 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
822 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
823 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
824 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
825 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000826 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
827 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000828 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
829 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000830
831#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
832 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
833 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
834
835#define IMPLEMENT_CAST_CASE_END() \
836 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
837 break; \
838 } \
839 break
840
841#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
842 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
843 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000844 IMPLEMENT_CAST_CASE_END()
845
846static void executeCastInst(CastInst *I, ExecutionContext &SF) {
847 const Type *Ty = I->getType();
848 const Type *SrcTy = I->getOperand(0)->getType();
849 GenericValue Src = getOperandValue(I->getOperand(0), SF);
850 GenericValue Dest;
851
852 switch (Ty->getPrimitiveID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000853 IMPLEMENT_CAST_CASE(UByte , unsigned char);
854 IMPLEMENT_CAST_CASE(SByte , signed char);
855 IMPLEMENT_CAST_CASE(UShort , unsigned short);
856 IMPLEMENT_CAST_CASE(Short , signed char);
857 IMPLEMENT_CAST_CASE(UInt , unsigned int );
858 IMPLEMENT_CAST_CASE(Int , signed int );
859 IMPLEMENT_CAST_CASE(ULong , uint64_t);
860 IMPLEMENT_CAST_CASE(Long , int64_t);
Chris Lattnerc2593162001-10-27 08:28:11 +0000861 IMPLEMENT_CAST_CASE(Pointer, uint64_t);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000862 IMPLEMENT_CAST_CASE(Float , float);
863 IMPLEMENT_CAST_CASE(Double , double);
Chris Lattner86660982001-08-27 05:16:50 +0000864 default:
865 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
866 }
867 SetValue(I, Dest, SF);
868}
Chris Lattner92101ac2001-08-23 17:05:04 +0000869
870
871
872
873//===----------------------------------------------------------------------===//
874// Dispatch and Execution Code
875//===----------------------------------------------------------------------===//
876
877MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
878 // Assign slot numbers to the method arguments...
879 const Method::ArgumentListType &ArgList = M->getArgumentList();
880 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
881 AE = ArgList.end(); AI != AE; ++AI) {
882 MethodArgument *MA = *AI;
883 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
884 }
885
886 // Iterate over all of the instructions...
887 unsigned InstNum = 0;
888 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
889 MI != ME; ++MI) {
890 Instruction *I = *MI; // For each instruction...
891 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
892 }
893}
894
895unsigned MethodInfo::getValueSlot(const Value *V) {
896 unsigned Plane = V->getType()->getUniqueID();
897 if (Plane >= NumPlaneElements.size())
898 NumPlaneElements.resize(Plane+1, 0);
899 return NumPlaneElements[Plane]++;
900}
901
902
Chris Lattner92101ac2001-08-23 17:05:04 +0000903//===----------------------------------------------------------------------===//
904// callMethod - Execute the specified method...
905//
Chris Lattner365a76e2001-09-10 04:49:44 +0000906void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
907 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
908 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
909 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000910 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000911 GenericValue Result = callExternalMethod(M, ArgVals);
912 const Type *RetTy = M->getReturnType();
913
914 // Copy the result back into the result variable if we are not returning
915 // void.
916 if (RetTy != Type::VoidTy) {
917 if (!ECStack.empty() && ECStack.back().Caller) {
918 ExecutionContext &SF = ECStack.back();
919 CallInst *Caller = SF.Caller;
920 SetValue(SF.Caller, Result, SF);
921
922 SF.Caller = 0; // We returned from the call...
923 } else {
924 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +0000925 CW << "Method " << M->getType() << " \"" << M->getName()
926 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000927 print(RetTy, Result);
928 cout << endl;
929
930 if (RetTy->isIntegral())
931 ExitCode = Result.SByteVal; // Capture the exit code of the program
932 }
933 }
934
Chris Lattner92101ac2001-08-23 17:05:04 +0000935 return;
936 }
937
938 // Process the method, assigning instruction numbers to the instructions in
939 // the method. Also calculate the number of values for each type slot active.
940 //
941 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000942 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000943
Chris Lattner92101ac2001-08-23 17:05:04 +0000944 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
945 StackFrame.CurMethod = M;
946 StackFrame.CurBB = M->front();
947 StackFrame.CurInst = StackFrame.CurBB->begin();
948 StackFrame.MethInfo = MethInfo;
949
950 // Initialize the values to nothing...
951 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
952 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
953 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
954
955 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
956
Chris Lattner92101ac2001-08-23 17:05:04 +0000957
Chris Lattner365a76e2001-09-10 04:49:44 +0000958 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000959 assert(ArgVals.size() == M->getArgumentList().size() &&
960 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +0000961 unsigned i = 0;
962 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
963 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
964 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000965 }
966}
967
968// executeInstruction - Interpret a single instruction, increment the "PC", and
969// return true if the next instruction is a breakpoint...
970//
971bool Interpreter::executeInstruction() {
972 assert(!ECStack.empty() && "No program running, cannot execute inst!");
973
974 ExecutionContext &SF = ECStack.back(); // Current stack frame
975 Instruction *I = *SF.CurInst++; // Increment before execute
976
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000977 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +0000978 CW << "Run:" << I;
979
980 // Set a sigsetjmp buffer so that we can recover if an error happens during
981 // instruction execution...
982 //
983 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
984 --SF.CurInst; // Back up to erroring instruction
985 cout << "EXCEPTION OCCURRED [Signal " << _sys_siglistp[SigNo] << "]:\n";
986 printStackTrace();
987 return true;
988 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000989
Chris Lattner92101ac2001-08-23 17:05:04 +0000990 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000991 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000992 } else {
993 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000994 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +0000995 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
996 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000997 // Memory Instructions
998 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000999 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1000 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1001 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1002 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001003 case Instruction::GetElementPtr:
1004 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001005
1006 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001007 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1008 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1009 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1010 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1011 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001012 default:
1013 cout << "Don't know how to execute this instruction!\n-->" << I;
1014 }
1015 }
1016
1017 // Reset the current frame location to the top of stack
1018 CurFrame = ECStack.size()-1;
1019
1020 if (CurFrame == -1) return false; // No breakpoint if no code
1021
1022 // Return true if there is a breakpoint annotation on the instruction...
1023 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1024}
1025
1026void Interpreter::stepInstruction() { // Do the 'step' command
1027 if (ECStack.empty()) {
1028 cout << "Error: no program running, cannot step!\n";
1029 return;
1030 }
1031
1032 // Run an instruction...
1033 executeInstruction();
1034
1035 // Print the next instruction to execute...
1036 printCurrentInstruction();
1037}
1038
1039// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001040void Interpreter::nextInstruction() { // Do the 'next' command
1041 if (ECStack.empty()) {
1042 cout << "Error: no program running, cannot 'next'!\n";
1043 return;
1044 }
1045
1046 // If this is a call instruction, step over the call instruction...
1047 // TODO: ICALL, CALL WITH, ...
1048 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001049 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001050 // Step into the function...
1051 if (executeInstruction()) {
1052 // Hit a breakpoint, print current instruction, then return to user...
1053 cout << "Breakpoint hit!\n";
1054 printCurrentInstruction();
1055 return;
1056 }
1057
Chris Lattnera74a6b52001-10-29 14:08:33 +00001058 // If we we able to step into the function, finish it now. We might not be
1059 // able the step into a function, if it's external for example.
1060 if (ECStack.size() != StackSize)
1061 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001062 else
1063 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001064
Chris Lattner92101ac2001-08-23 17:05:04 +00001065 } else {
1066 // Normal instruction, just step...
1067 stepInstruction();
1068 }
1069}
1070
1071void Interpreter::run() {
1072 if (ECStack.empty()) {
1073 cout << "Error: no program running, cannot run!\n";
1074 return;
1075 }
1076
1077 bool HitBreakpoint = false;
1078 while (!ECStack.empty() && !HitBreakpoint) {
1079 // Run an instruction...
1080 HitBreakpoint = executeInstruction();
1081 }
1082
1083 if (HitBreakpoint) {
1084 cout << "Breakpoint hit!\n";
1085 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001086 // Print the next instruction to execute...
1087 printCurrentInstruction();
1088}
1089
1090void Interpreter::finish() {
1091 if (ECStack.empty()) {
1092 cout << "Error: no program running, cannot run!\n";
1093 return;
1094 }
1095
1096 unsigned StackSize = ECStack.size();
1097 bool HitBreakpoint = false;
1098 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1099 // Run an instruction...
1100 HitBreakpoint = executeInstruction();
1101 }
1102
1103 if (HitBreakpoint) {
1104 cout << "Breakpoint hit!\n";
1105 }
1106
1107 // Print the next instruction to execute...
1108 printCurrentInstruction();
1109}
1110
1111
1112
1113// printCurrentInstruction - Print out the instruction that the virtual PC is
1114// at, or fail silently if no program is running.
1115//
1116void Interpreter::printCurrentInstruction() {
1117 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001118 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1119 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1120
Chris Lattner92101ac2001-08-23 17:05:04 +00001121 Instruction *I = *ECStack.back().CurInst;
1122 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1123 assert(IN && "Instruction has no numbering annotation!");
1124 cout << "#" << IN->InstNum << I;
1125 }
1126}
1127
1128void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001129 switch (Ty->getPrimitiveID()) {
1130 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1131 case Type::SByteTyID: cout << V.SByteVal; break;
1132 case Type::UByteTyID: cout << V.UByteVal; break;
1133 case Type::ShortTyID: cout << V.ShortVal; break;
1134 case Type::UShortTyID: cout << V.UShortVal; break;
1135 case Type::IntTyID: cout << V.IntVal; break;
1136 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001137 case Type::LongTyID: cout << V.LongVal; break;
1138 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001139 case Type::FloatTyID: cout << V.FloatVal; break;
1140 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerc2593162001-10-27 08:28:11 +00001141 case Type::PointerTyID:cout << (void*)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001142 default:
1143 cout << "- Don't know how to print value of this type!";
1144 break;
1145 }
1146}
1147
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001148void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001149 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001150 printValue(Ty, V);
1151}
1152
1153void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001154 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1155 if (!PickedVal) return;
1156
Chris Lattner9636a912001-10-01 16:18:37 +00001157 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001158 CW << M; // Print the method
Chris Lattner92101ac2001-08-23 17:05:04 +00001159 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001160 print(PickedVal->getType(),
1161 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001162 cout << endl;
1163 }
1164
1165}
1166
Chris Lattner86660982001-08-27 05:16:50 +00001167void Interpreter::infoValue(const string &Name) {
1168 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1169 if (!PickedVal) return;
1170
1171 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001172 print(PickedVal->getType(),
1173 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001174 cout << endl;
1175 printOperandInfo(PickedVal, ECStack[CurFrame]);
1176}
1177
Chris Lattner92101ac2001-08-23 17:05:04 +00001178void Interpreter::list() {
1179 if (ECStack.empty())
1180 cout << "Error: No program executing!\n";
1181 else
Chris Lattner5af0c482001-11-07 04:23:00 +00001182 CW << ECStack[CurFrame].CurMethod; // Just print the method out...
Chris Lattner92101ac2001-08-23 17:05:04 +00001183}
1184
1185void Interpreter::printStackTrace() {
1186 if (ECStack.empty()) cout << "No program executing!\n";
1187
1188 for (unsigned i = 0; i < ECStack.size(); ++i) {
1189 cout << (((int)i == CurFrame) ? '>' : '-');
Chris Lattner5af0c482001-11-07 04:23:00 +00001190 CW << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
1191 << ECStack[i].CurMethod->getName() << "\"(";
Chris Lattner92101ac2001-08-23 17:05:04 +00001192 // TODO: Print Args
1193 cout << ")" << endl;
Chris Lattner5af0c482001-11-07 04:23:00 +00001194 CW << *ECStack[i].CurInst;
Chris Lattner92101ac2001-08-23 17:05:04 +00001195 }
1196}