blob: 069f2f210ce85d9c43daf9d8eedc9eae637c5446 [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 Lattner2e42d3a2001-10-15 05:51:48 +000019
20// Create a TargetData structure to handle memory addressing and size/alignment
21// computations
22//
23static TargetData TD("lli Interpreter");
24
25//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000026// Value Manipulation code
27//===----------------------------------------------------------------------===//
28
29static unsigned getOperandSlot(Value *V) {
30 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
31 assert(SN && "Operand does not have a slot number annotation!");
32 return SN->SlotNum;
33}
34
35#define GET_CONST_VAL(TY, CLASS) \
36 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
37
38static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
39 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
40 GenericValue Result;
41 switch (CPV->getType()->getPrimitiveID()) {
42 GET_CONST_VAL(Bool , ConstPoolBool);
43 GET_CONST_VAL(UByte , ConstPoolUInt);
44 GET_CONST_VAL(SByte , ConstPoolSInt);
45 GET_CONST_VAL(UShort , ConstPoolUInt);
46 GET_CONST_VAL(Short , ConstPoolSInt);
47 GET_CONST_VAL(UInt , ConstPoolUInt);
48 GET_CONST_VAL(Int , ConstPoolSInt);
Chris Lattner7b851ab2001-10-15 19:18:26 +000049 GET_CONST_VAL(ULong , ConstPoolUInt);
50 GET_CONST_VAL(Long , ConstPoolSInt);
Chris Lattner39bb5b42001-10-15 13:25:40 +000051 GET_CONST_VAL(Float , ConstPoolFP);
52 GET_CONST_VAL(Double , ConstPoolFP);
53 case Type::PointerTyID:
54 if (isa<ConstPoolPointerNull>(CPV)) {
Chris Lattnerc2593162001-10-27 08:28:11 +000055 Result.ULongVal = 0;
Chris Lattner39bb5b42001-10-15 13:25:40 +000056 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
57 assert(0 && "Not implemented!");
58 } else {
59 assert(0 && "Unknown constant pointer type!");
60 }
61 break;
62 default:
63 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
64 }
65 return Result;
66 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
67 GlobalAddress *Address =
68 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
69 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +000070 Result.ULongVal = (uint64_t)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +000071 return Result;
72 } else {
73 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +000074 unsigned OpSlot = getOperandSlot(V);
75 assert(TyP < SF.Values.size() &&
76 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +000077 return SF.Values[TyP][getOperandSlot(V)];
78 }
79}
80
81static void printOperandInfo(Value *V, ExecutionContext &SF) {
82 if (isa<ConstPoolVal>(V)) {
83 cout << "Constant Pool Value\n";
84 } else if (isa<GlobalValue>(V)) {
85 cout << "Global Value\n";
86 } else {
87 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
88 unsigned Slot = getOperandSlot(V);
89 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
90 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
91 }
92}
93
94
95
96static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
97 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
98
99 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
100 SF.Values[TyP][getOperandSlot(V)] = Val;
101}
102
103
104//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000105// Annotation Wrangling code
106//===----------------------------------------------------------------------===//
107
108void Interpreter::initializeExecutionEngine() {
109 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
110 &MethodInfo::Create);
111 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
112 &GlobalAddress::Create);
113}
114
115// InitializeMemory - Recursive function to apply a ConstPool value into the
116// specified memory location...
117//
118static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000119#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
120 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000121 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000122 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000123 } return
124
125 switch (Init->getType()->getPrimitiveID()) {
126 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
127 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
128 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
129 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
130 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
131 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
132 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
133 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
134 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
135 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
136 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
137#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000138
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000139 case Type::ArrayTyID: {
140 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
141 const vector<Use> &Val = CPA->getValues();
142 unsigned ElementSize =
143 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
144 for (unsigned i = 0; i < Val.size(); ++i)
145 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
146 return;
147 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000148
149 case Type::StructTyID: {
150 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
151 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
152 const vector<Use> &Val = CPS->getValues();
153 for (unsigned i = 0; i < Val.size(); ++i)
154 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
155 Addr+SL->MemberOffsets[i]);
156 return;
157 }
158
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000159 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000160 if (isa<ConstPoolPointerNull>(Init)) {
161 *(void**)Addr = 0;
162 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
163 GlobalAddress *Address =
164 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
165 *(void**)Addr = (GenericValue*)Address->Ptr;
166 } else {
167 assert(0 && "Unknown Constant pointer type!");
168 }
169 return;
170
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000171 default:
172 cout << "Bad Type: " << Init->getType()->getDescription() << endl;
173 assert(0 && "Unknown constant type to initialize memory with!");
174 }
175}
176
177Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
178 assert(AID == GlobalAddressAID);
179
180 // This annotation will only be created on GlobalValue objects...
181 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
182
183 if (isa<Method>(GVal)) {
184 // The GlobalAddress object for a method is just a pointer to method itself.
185 // Don't delete it when the annotation is gone though!
186 return new GlobalAddress(GVal, false);
187 }
188
189 // Handle the case of a global variable...
190 assert(isa<GlobalVariable>(GVal) &&
191 "Global value found that isn't a method or global variable!");
192 GlobalVariable *GV = cast<GlobalVariable>(GVal);
193
194 // First off, we must allocate space for the global variable to point at...
195 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
196 unsigned NumElements = 1;
197
198 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
199 assert(GV->hasInitializer() && "Const val must have an initializer!");
200 // Allocating a unsized array type?
201 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
202
203 // Get the number of elements being allocated by the array...
204 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
205 }
206
207 // Allocate enough memory to hold the type...
208 void *Addr = malloc(NumElements * TD.getTypeSize(Ty));
209 assert(Addr != 0 && "Null pointer returned by malloc!");
210
211 // Initialize the memory if there is an initializer...
212 if (GV->hasInitializer())
213 InitializeMemory(GV->getInitializer(), (char*)Addr);
214
215 return new GlobalAddress(Addr, true); // Simply invoke the ctor
216}
217
Chris Lattner92101ac2001-08-23 17:05:04 +0000218
219//===----------------------------------------------------------------------===//
220// Binary Instruction Implementations
221//===----------------------------------------------------------------------===//
222
223#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
224 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
225
226static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
227 const Type *Ty, ExecutionContext &SF) {
228 GenericValue Dest;
229 switch (Ty->getPrimitiveID()) {
230 IMPLEMENT_BINARY_OPERATOR(+, UByte);
231 IMPLEMENT_BINARY_OPERATOR(+, SByte);
232 IMPLEMENT_BINARY_OPERATOR(+, UShort);
233 IMPLEMENT_BINARY_OPERATOR(+, Short);
234 IMPLEMENT_BINARY_OPERATOR(+, UInt);
235 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000236 IMPLEMENT_BINARY_OPERATOR(+, ULong);
237 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000238 IMPLEMENT_BINARY_OPERATOR(+, Float);
239 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000240 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000241 default:
242 cout << "Unhandled type for Add instruction: " << Ty << endl;
243 }
244 return Dest;
245}
246
247static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
248 const Type *Ty, ExecutionContext &SF) {
249 GenericValue Dest;
250 switch (Ty->getPrimitiveID()) {
251 IMPLEMENT_BINARY_OPERATOR(-, UByte);
252 IMPLEMENT_BINARY_OPERATOR(-, SByte);
253 IMPLEMENT_BINARY_OPERATOR(-, UShort);
254 IMPLEMENT_BINARY_OPERATOR(-, Short);
255 IMPLEMENT_BINARY_OPERATOR(-, UInt);
256 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000257 IMPLEMENT_BINARY_OPERATOR(-, ULong);
258 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000259 IMPLEMENT_BINARY_OPERATOR(-, Float);
260 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000261 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000262 default:
263 cout << "Unhandled type for Sub instruction: " << Ty << endl;
264 }
265 return Dest;
266}
267
Chris Lattnerc2593162001-10-27 08:28:11 +0000268static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
269 const Type *Ty, ExecutionContext &SF) {
270 GenericValue Dest;
271 switch (Ty->getPrimitiveID()) {
272 IMPLEMENT_BINARY_OPERATOR(*, UByte);
273 IMPLEMENT_BINARY_OPERATOR(*, SByte);
274 IMPLEMENT_BINARY_OPERATOR(*, UShort);
275 IMPLEMENT_BINARY_OPERATOR(*, Short);
276 IMPLEMENT_BINARY_OPERATOR(*, UInt);
277 IMPLEMENT_BINARY_OPERATOR(*, Int);
278 IMPLEMENT_BINARY_OPERATOR(*, ULong);
279 IMPLEMENT_BINARY_OPERATOR(*, Long);
280 IMPLEMENT_BINARY_OPERATOR(*, Float);
281 IMPLEMENT_BINARY_OPERATOR(*, Double);
282 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
283 default:
284 cout << "Unhandled type for Mul instruction: " << Ty << endl;
285 }
286 return Dest;
287}
288
289static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
290 const Type *Ty, ExecutionContext &SF) {
291 GenericValue Dest;
292 switch (Ty->getPrimitiveID()) {
293 IMPLEMENT_BINARY_OPERATOR(/, UByte);
294 IMPLEMENT_BINARY_OPERATOR(/, SByte);
295 IMPLEMENT_BINARY_OPERATOR(/, UShort);
296 IMPLEMENT_BINARY_OPERATOR(/, Short);
297 IMPLEMENT_BINARY_OPERATOR(/, UInt);
298 IMPLEMENT_BINARY_OPERATOR(/, Int);
299 IMPLEMENT_BINARY_OPERATOR(/, ULong);
300 IMPLEMENT_BINARY_OPERATOR(/, Long);
301 IMPLEMENT_BINARY_OPERATOR(/, Float);
302 IMPLEMENT_BINARY_OPERATOR(/, Double);
303 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
304 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000305 cout << "Unhandled type for Div instruction: " << Ty << endl;
306 }
307 return Dest;
308}
309
310static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
311 const Type *Ty, ExecutionContext &SF) {
312 GenericValue Dest;
313 switch (Ty->getPrimitiveID()) {
314 IMPLEMENT_BINARY_OPERATOR(%, UByte);
315 IMPLEMENT_BINARY_OPERATOR(%, SByte);
316 IMPLEMENT_BINARY_OPERATOR(%, UShort);
317 IMPLEMENT_BINARY_OPERATOR(%, Short);
318 IMPLEMENT_BINARY_OPERATOR(%, UInt);
319 IMPLEMENT_BINARY_OPERATOR(%, Int);
320 IMPLEMENT_BINARY_OPERATOR(%, ULong);
321 IMPLEMENT_BINARY_OPERATOR(%, Long);
322 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
323 case Type::FloatTyID:
324 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
325 break;
326 case Type::DoubleTyID:
327 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
328 break;
329 default:
330 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000331 }
332 return Dest;
333}
334
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000335static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
336 const Type *Ty, ExecutionContext &SF) {
337 GenericValue Dest;
338 switch (Ty->getPrimitiveID()) {
339 IMPLEMENT_BINARY_OPERATOR(^, UByte);
340 IMPLEMENT_BINARY_OPERATOR(^, SByte);
341 IMPLEMENT_BINARY_OPERATOR(^, UShort);
342 IMPLEMENT_BINARY_OPERATOR(^, Short);
343 IMPLEMENT_BINARY_OPERATOR(^, UInt);
344 IMPLEMENT_BINARY_OPERATOR(^, Int);
345 IMPLEMENT_BINARY_OPERATOR(^, ULong);
346 IMPLEMENT_BINARY_OPERATOR(^, Long);
347 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
348 default:
349 cout << "Unhandled type for Xor instruction: " << Ty << endl;
350 }
351 return Dest;
352}
353
354
Chris Lattner92101ac2001-08-23 17:05:04 +0000355#define IMPLEMENT_SETCC(OP, TY) \
356 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
357
Chris Lattner92101ac2001-08-23 17:05:04 +0000358static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
359 const Type *Ty, ExecutionContext &SF) {
360 GenericValue Dest;
361 switch (Ty->getPrimitiveID()) {
362 IMPLEMENT_SETCC(==, UByte);
363 IMPLEMENT_SETCC(==, SByte);
364 IMPLEMENT_SETCC(==, UShort);
365 IMPLEMENT_SETCC(==, Short);
366 IMPLEMENT_SETCC(==, UInt);
367 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000368 IMPLEMENT_SETCC(==, ULong);
369 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000370 IMPLEMENT_SETCC(==, Float);
371 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000372 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000373 default:
374 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
375 }
376 return Dest;
377}
378
379static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
380 const Type *Ty, ExecutionContext &SF) {
381 GenericValue Dest;
382 switch (Ty->getPrimitiveID()) {
383 IMPLEMENT_SETCC(!=, UByte);
384 IMPLEMENT_SETCC(!=, SByte);
385 IMPLEMENT_SETCC(!=, UShort);
386 IMPLEMENT_SETCC(!=, Short);
387 IMPLEMENT_SETCC(!=, UInt);
388 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000389 IMPLEMENT_SETCC(!=, ULong);
390 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000391 IMPLEMENT_SETCC(!=, Float);
392 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000393 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000394 default:
395 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
396 }
397 return Dest;
398}
399
400static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
401 const Type *Ty, ExecutionContext &SF) {
402 GenericValue Dest;
403 switch (Ty->getPrimitiveID()) {
404 IMPLEMENT_SETCC(<=, UByte);
405 IMPLEMENT_SETCC(<=, SByte);
406 IMPLEMENT_SETCC(<=, UShort);
407 IMPLEMENT_SETCC(<=, Short);
408 IMPLEMENT_SETCC(<=, UInt);
409 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000410 IMPLEMENT_SETCC(<=, ULong);
411 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000412 IMPLEMENT_SETCC(<=, Float);
413 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000414 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000415 default:
416 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
417 }
418 return Dest;
419}
420
421static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
422 const Type *Ty, ExecutionContext &SF) {
423 GenericValue Dest;
424 switch (Ty->getPrimitiveID()) {
425 IMPLEMENT_SETCC(>=, UByte);
426 IMPLEMENT_SETCC(>=, SByte);
427 IMPLEMENT_SETCC(>=, UShort);
428 IMPLEMENT_SETCC(>=, Short);
429 IMPLEMENT_SETCC(>=, UInt);
430 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000431 IMPLEMENT_SETCC(>=, ULong);
432 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000433 IMPLEMENT_SETCC(>=, Float);
434 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000435 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000436 default:
437 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
438 }
439 return Dest;
440}
441
442static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
443 const Type *Ty, ExecutionContext &SF) {
444 GenericValue Dest;
445 switch (Ty->getPrimitiveID()) {
446 IMPLEMENT_SETCC(<, UByte);
447 IMPLEMENT_SETCC(<, SByte);
448 IMPLEMENT_SETCC(<, UShort);
449 IMPLEMENT_SETCC(<, Short);
450 IMPLEMENT_SETCC(<, UInt);
451 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000452 IMPLEMENT_SETCC(<, ULong);
453 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000454 IMPLEMENT_SETCC(<, Float);
455 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000456 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 default:
458 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
459 }
460 return Dest;
461}
462
463static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
464 const Type *Ty, ExecutionContext &SF) {
465 GenericValue Dest;
466 switch (Ty->getPrimitiveID()) {
467 IMPLEMENT_SETCC(>, UByte);
468 IMPLEMENT_SETCC(>, SByte);
469 IMPLEMENT_SETCC(>, UShort);
470 IMPLEMENT_SETCC(>, Short);
471 IMPLEMENT_SETCC(>, UInt);
472 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000473 IMPLEMENT_SETCC(>, ULong);
474 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 IMPLEMENT_SETCC(>, Float);
476 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000477 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 default:
479 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
480 }
481 return Dest;
482}
483
484static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
485 const Type *Ty = I->getOperand(0)->getType();
486 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
487 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
488 GenericValue R; // Result
489
490 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000491 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
492 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
493 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
494 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
495 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000496 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
498 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
499 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
500 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
501 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
502 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
503 default:
504 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000505 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 }
507
508 SetValue(I, R, SF);
509}
510
Chris Lattner92101ac2001-08-23 17:05:04 +0000511//===----------------------------------------------------------------------===//
512// Terminator Instruction Implementations
513//===----------------------------------------------------------------------===//
514
Chris Lattnere43db882001-10-27 04:15:57 +0000515void Interpreter::exitCalled(GenericValue GV) {
516 cout << "Program returned ";
517 print(Type::IntTy, GV);
518 cout << " via 'void exit(int)'\n";
519
520 ExitCode = GV.SByteVal;
521 ECStack.clear();
522}
523
Chris Lattner92101ac2001-08-23 17:05:04 +0000524void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
525 const Type *RetTy = 0;
526 GenericValue Result;
527
528 // Save away the return value... (if we are not 'ret void')
529 if (I->getNumOperands()) {
530 RetTy = I->getReturnValue()->getType();
531 Result = getOperandValue(I->getReturnValue(), SF);
532 }
533
534 // Save previously executing meth
535 const Method *M = ECStack.back().CurMethod;
536
537 // Pop the current stack frame... this invalidates SF
538 ECStack.pop_back();
539
540 if (ECStack.empty()) { // Finished main. Put result into exit code...
541 if (RetTy) { // Nonvoid return type?
542 cout << "Method " << M->getType() << " \"" << M->getName()
543 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000544 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000545 cout << endl;
546
547 if (RetTy->isIntegral())
548 ExitCode = Result.SByteVal; // Capture the exit code of the program
549 } else {
550 ExitCode = 0;
551 }
552 return;
553 }
554
555 // If we have a previous stack frame, and we have a previous call, fill in
556 // the return value...
557 //
558 ExecutionContext &NewSF = ECStack.back();
559 if (NewSF.Caller) {
560 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
561 SetValue(NewSF.Caller, Result, NewSF);
562
563 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000564 } else {
565 // This must be a function that is executing because of a user 'call'
566 // instruction.
567 cout << "Method " << M->getType() << " \"" << M->getName()
568 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000569 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000570 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000571 }
572}
573
574void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
575 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
576 BasicBlock *Dest;
577
578 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
579 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000580 Value *Cond = I->getCondition();
581 GenericValue CondVal = getOperandValue(Cond, SF);
582 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000583 Dest = I->getSuccessor(1);
584 }
585 SF.CurBB = Dest; // Update CurBB to branch destination
586 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
587}
588
589//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000590// Memory Instruction Implementations
591//===----------------------------------------------------------------------===//
592
Chris Lattner86660982001-08-27 05:16:50 +0000593void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
594 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
595 unsigned NumElements = 1;
596
597 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000598 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000599 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000600 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000601
602 // Get the number of elements being allocated by the array...
603 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
604 NumElements = NumEl.UIntVal;
605 }
606
607 // Allocate enough memory to hold the type...
608 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +0000609 Result.ULongVal = (uint64_t)malloc(NumElements * TD.getTypeSize(Ty));
610 assert(Result.ULongVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000611 SetValue(I, Result, SF);
612
613 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000614 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000615 }
616}
617
618static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
619 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
620 GenericValue Value = getOperandValue(I->getOperand(0), SF);
621 // TODO: Check to make sure memory is allocated
Chris Lattnerc2593162001-10-27 08:28:11 +0000622 free((void*)Value.ULongVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000623}
624
Chris Lattner95c3af52001-10-29 19:32:19 +0000625
626// getElementOffset - The workhorse for getelementptr, load and store. This
627// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
628// the pointer type specified by argument Arg.
629//
630static uint64_t getElementOffset(Instruction *I, unsigned ArgOff) {
631 assert(isa<PointerType>(I->getOperand(ArgOff)->getType()) &&
632 "Cannot getElementOffset of a nonpointer type!");
633
634 uint64_t Total = 0;
635 const Type *Ty =
636 cast<PointerType>(I->getOperand(ArgOff++)->getType())->getValueType();
637
638 while (ArgOff < I->getNumOperands()) {
639 const StructType *STy = cast<StructType>(Ty);
640 const StructLayout *SLO = TD.getStructLayout(STy);
641
642 // Indicies must be ubyte constants...
643 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
644 assert(CPU->getType() == Type::UByteTy);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000645 unsigned Index = CPU->getValue();
646 Total += SLO->MemberOffsets[Index];
647 Ty = STy->getElementTypes()[Index];
Chris Lattner95c3af52001-10-29 19:32:19 +0000648 }
649
650 return Total;
651}
652
653static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
654 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
655
656 GenericValue Result;
657 Result.ULongVal = SrcPtr + getElementOffset(I, 0);
658 SetValue(I, Result, SF);
659}
660
Chris Lattner86660982001-08-27 05:16:50 +0000661static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000662 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
Chris Lattnerbb76f022001-10-30 20:27:31 +0000663 uint64_t Offset = getElementOffset(I, 0); // Handle any structure indices
664 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000665
666 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000667 GenericValue Result;
668
669 switch (I->getType()->getPrimitiveID()) {
670 case Type::BoolTyID:
671 case Type::UByteTyID:
672 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
673 case Type::UShortTyID:
674 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
675 case Type::UIntTyID:
676 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000677 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000678 case Type::LongTyID:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000679 case Type::PointerTyID: Result.ULongVal = Ptr->PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000680 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
681 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000682 default:
683 cout << "Cannot load value of type " << I->getType() << "!\n";
684 }
685
686 SetValue(I, Result, SF);
687}
688
689static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000690 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
691 SrcPtr += getElementOffset(I, 1); // Handle any structure indices
692
693 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000694 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000695
696 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
697 case Type::BoolTyID:
698 case Type::UByteTyID:
699 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
700 case Type::UShortTyID:
701 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
702 case Type::UIntTyID:
703 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000704 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000705 case Type::LongTyID:
706 case Type::PointerTyID: Ptr->LongVal = Val.LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000707 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
708 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000709 default:
710 cout << "Cannot store value of type " << I->getType() << "!\n";
711 }
712}
713
714
715//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000716// Miscellaneous Instruction Implementations
717//===----------------------------------------------------------------------===//
718
719void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
720 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000721 vector<GenericValue> ArgVals;
722 ArgVals.reserve(I->getNumOperands()-1);
723 for (unsigned i = 1; i < I->getNumOperands(); ++i)
724 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
725
726 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000727}
728
729static void executePHINode(PHINode *I, ExecutionContext &SF) {
730 BasicBlock *PrevBB = SF.PrevBB;
731 Value *IncomingValue = 0;
732
733 // Search for the value corresponding to this previous bb...
734 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
735 if (I->getIncomingBlock(--i) == PrevBB) {
736 IncomingValue = I->getIncomingValue(i);
737 break;
738 }
739 }
740 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
741
742 // Found the value, set as the result...
743 SetValue(I, getOperandValue(IncomingValue, SF), SF);
744}
745
Chris Lattner86660982001-08-27 05:16:50 +0000746#define IMPLEMENT_SHIFT(OP, TY) \
747 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
748
749static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
750 const Type *Ty = I->getOperand(0)->getType();
751 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
752 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
753 GenericValue Dest;
754
755 switch (Ty->getPrimitiveID()) {
756 IMPLEMENT_SHIFT(<<, UByte);
757 IMPLEMENT_SHIFT(<<, SByte);
758 IMPLEMENT_SHIFT(<<, UShort);
759 IMPLEMENT_SHIFT(<<, Short);
760 IMPLEMENT_SHIFT(<<, UInt);
761 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000762 IMPLEMENT_SHIFT(<<, ULong);
763 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000764 default:
765 cout << "Unhandled type for Shl instruction: " << Ty << endl;
766 }
767 SetValue(I, Dest, SF);
768}
769
770static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
771 const Type *Ty = I->getOperand(0)->getType();
772 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
773 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
774 GenericValue Dest;
775
776 switch (Ty->getPrimitiveID()) {
777 IMPLEMENT_SHIFT(>>, UByte);
778 IMPLEMENT_SHIFT(>>, SByte);
779 IMPLEMENT_SHIFT(>>, UShort);
780 IMPLEMENT_SHIFT(>>, Short);
781 IMPLEMENT_SHIFT(>>, UInt);
782 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000783 IMPLEMENT_SHIFT(>>, ULong);
784 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000785 default:
786 cout << "Unhandled type for Shr instruction: " << Ty << endl;
787 }
788 SetValue(I, Dest, SF);
789}
790
791#define IMPLEMENT_CAST(DTY, DCTY, STY) \
792 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
793
794#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
795 case Type::DESTTY##TyID: \
796 switch (SrcTy->getPrimitiveID()) { \
797 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
798 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
799 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
800 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
801 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000802 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
803 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000804 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
805 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000806
807#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
808 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
809 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
810
811#define IMPLEMENT_CAST_CASE_END() \
812 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
813 break; \
814 } \
815 break
816
817#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
818 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
819 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000820 IMPLEMENT_CAST_CASE_END()
821
822static void executeCastInst(CastInst *I, ExecutionContext &SF) {
823 const Type *Ty = I->getType();
824 const Type *SrcTy = I->getOperand(0)->getType();
825 GenericValue Src = getOperandValue(I->getOperand(0), SF);
826 GenericValue Dest;
827
828 switch (Ty->getPrimitiveID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000829 IMPLEMENT_CAST_CASE(UByte , unsigned char);
830 IMPLEMENT_CAST_CASE(SByte , signed char);
831 IMPLEMENT_CAST_CASE(UShort , unsigned short);
832 IMPLEMENT_CAST_CASE(Short , signed char);
833 IMPLEMENT_CAST_CASE(UInt , unsigned int );
834 IMPLEMENT_CAST_CASE(Int , signed int );
835 IMPLEMENT_CAST_CASE(ULong , uint64_t);
836 IMPLEMENT_CAST_CASE(Long , int64_t);
Chris Lattnerc2593162001-10-27 08:28:11 +0000837 IMPLEMENT_CAST_CASE(Pointer, uint64_t);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000838 IMPLEMENT_CAST_CASE(Float , float);
839 IMPLEMENT_CAST_CASE(Double , double);
Chris Lattner86660982001-08-27 05:16:50 +0000840 default:
841 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
842 }
843 SetValue(I, Dest, SF);
844}
Chris Lattner92101ac2001-08-23 17:05:04 +0000845
846
847
848
849//===----------------------------------------------------------------------===//
850// Dispatch and Execution Code
851//===----------------------------------------------------------------------===//
852
853MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
854 // Assign slot numbers to the method arguments...
855 const Method::ArgumentListType &ArgList = M->getArgumentList();
856 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
857 AE = ArgList.end(); AI != AE; ++AI) {
858 MethodArgument *MA = *AI;
859 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
860 }
861
862 // Iterate over all of the instructions...
863 unsigned InstNum = 0;
864 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
865 MI != ME; ++MI) {
866 Instruction *I = *MI; // For each instruction...
867 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
868 }
869}
870
871unsigned MethodInfo::getValueSlot(const Value *V) {
872 unsigned Plane = V->getType()->getUniqueID();
873 if (Plane >= NumPlaneElements.size())
874 NumPlaneElements.resize(Plane+1, 0);
875 return NumPlaneElements[Plane]++;
876}
877
878
Chris Lattner92101ac2001-08-23 17:05:04 +0000879//===----------------------------------------------------------------------===//
880// callMethod - Execute the specified method...
881//
Chris Lattner365a76e2001-09-10 04:49:44 +0000882void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
883 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
884 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
885 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000886 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000887 GenericValue Result = callExternalMethod(M, ArgVals);
888 const Type *RetTy = M->getReturnType();
889
890 // Copy the result back into the result variable if we are not returning
891 // void.
892 if (RetTy != Type::VoidTy) {
893 if (!ECStack.empty() && ECStack.back().Caller) {
894 ExecutionContext &SF = ECStack.back();
895 CallInst *Caller = SF.Caller;
896 SetValue(SF.Caller, Result, SF);
897
898 SF.Caller = 0; // We returned from the call...
899 } else {
900 // print it.
901 cout << "Method " << M->getType() << " \"" << M->getName()
902 << "\" returned ";
903 print(RetTy, Result);
904 cout << endl;
905
906 if (RetTy->isIntegral())
907 ExitCode = Result.SByteVal; // Capture the exit code of the program
908 }
909 }
910
Chris Lattner92101ac2001-08-23 17:05:04 +0000911 return;
912 }
913
914 // Process the method, assigning instruction numbers to the instructions in
915 // the method. Also calculate the number of values for each type slot active.
916 //
917 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000918 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000919
Chris Lattner92101ac2001-08-23 17:05:04 +0000920 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
921 StackFrame.CurMethod = M;
922 StackFrame.CurBB = M->front();
923 StackFrame.CurInst = StackFrame.CurBB->begin();
924 StackFrame.MethInfo = MethInfo;
925
926 // Initialize the values to nothing...
927 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
928 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
929 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
930
931 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
932
Chris Lattner92101ac2001-08-23 17:05:04 +0000933
Chris Lattner365a76e2001-09-10 04:49:44 +0000934 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000935 assert(ArgVals.size() == M->getArgumentList().size() &&
936 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +0000937 unsigned i = 0;
938 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
939 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
940 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000941 }
942}
943
944// executeInstruction - Interpret a single instruction, increment the "PC", and
945// return true if the next instruction is a breakpoint...
946//
947bool Interpreter::executeInstruction() {
948 assert(!ECStack.empty() && "No program running, cannot execute inst!");
949
950 ExecutionContext &SF = ECStack.back(); // Current stack frame
951 Instruction *I = *SF.CurInst++; // Increment before execute
952
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000953 if (Trace)
954 cout << "Run:" << I;
955
Chris Lattner92101ac2001-08-23 17:05:04 +0000956 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000957 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000958 } else {
959 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000960 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +0000961 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
962 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000963 // Memory Instructions
964 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000965 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
966 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
967 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
968 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +0000969 case Instruction::GetElementPtr:
970 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000971
972 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +0000973 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
974 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
975 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
976 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
977 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000978 default:
979 cout << "Don't know how to execute this instruction!\n-->" << I;
980 }
981 }
982
983 // Reset the current frame location to the top of stack
984 CurFrame = ECStack.size()-1;
985
986 if (CurFrame == -1) return false; // No breakpoint if no code
987
988 // Return true if there is a breakpoint annotation on the instruction...
989 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
990}
991
992void Interpreter::stepInstruction() { // Do the 'step' command
993 if (ECStack.empty()) {
994 cout << "Error: no program running, cannot step!\n";
995 return;
996 }
997
998 // Run an instruction...
999 executeInstruction();
1000
1001 // Print the next instruction to execute...
1002 printCurrentInstruction();
1003}
1004
1005// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001006void Interpreter::nextInstruction() { // Do the 'next' command
1007 if (ECStack.empty()) {
1008 cout << "Error: no program running, cannot 'next'!\n";
1009 return;
1010 }
1011
1012 // If this is a call instruction, step over the call instruction...
1013 // TODO: ICALL, CALL WITH, ...
1014 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001015 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001016 // Step into the function...
1017 if (executeInstruction()) {
1018 // Hit a breakpoint, print current instruction, then return to user...
1019 cout << "Breakpoint hit!\n";
1020 printCurrentInstruction();
1021 return;
1022 }
1023
Chris Lattnera74a6b52001-10-29 14:08:33 +00001024 // If we we able to step into the function, finish it now. We might not be
1025 // able the step into a function, if it's external for example.
1026 if (ECStack.size() != StackSize)
1027 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001028 else
1029 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001030
Chris Lattner92101ac2001-08-23 17:05:04 +00001031 } else {
1032 // Normal instruction, just step...
1033 stepInstruction();
1034 }
1035}
1036
1037void Interpreter::run() {
1038 if (ECStack.empty()) {
1039 cout << "Error: no program running, cannot run!\n";
1040 return;
1041 }
1042
1043 bool HitBreakpoint = false;
1044 while (!ECStack.empty() && !HitBreakpoint) {
1045 // Run an instruction...
1046 HitBreakpoint = executeInstruction();
1047 }
1048
1049 if (HitBreakpoint) {
1050 cout << "Breakpoint hit!\n";
1051 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001052 // Print the next instruction to execute...
1053 printCurrentInstruction();
1054}
1055
1056void Interpreter::finish() {
1057 if (ECStack.empty()) {
1058 cout << "Error: no program running, cannot run!\n";
1059 return;
1060 }
1061
1062 unsigned StackSize = ECStack.size();
1063 bool HitBreakpoint = false;
1064 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1065 // Run an instruction...
1066 HitBreakpoint = executeInstruction();
1067 }
1068
1069 if (HitBreakpoint) {
1070 cout << "Breakpoint hit!\n";
1071 }
1072
1073 // Print the next instruction to execute...
1074 printCurrentInstruction();
1075}
1076
1077
1078
1079// printCurrentInstruction - Print out the instruction that the virtual PC is
1080// at, or fail silently if no program is running.
1081//
1082void Interpreter::printCurrentInstruction() {
1083 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001084 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1085 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1086
Chris Lattner92101ac2001-08-23 17:05:04 +00001087 Instruction *I = *ECStack.back().CurInst;
1088 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1089 assert(IN && "Instruction has no numbering annotation!");
1090 cout << "#" << IN->InstNum << I;
1091 }
1092}
1093
1094void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001095 switch (Ty->getPrimitiveID()) {
1096 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1097 case Type::SByteTyID: cout << V.SByteVal; break;
1098 case Type::UByteTyID: cout << V.UByteVal; break;
1099 case Type::ShortTyID: cout << V.ShortVal; break;
1100 case Type::UShortTyID: cout << V.UShortVal; break;
1101 case Type::IntTyID: cout << V.IntVal; break;
1102 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001103 case Type::LongTyID: cout << V.LongVal; break;
1104 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001105 case Type::FloatTyID: cout << V.FloatVal; break;
1106 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerc2593162001-10-27 08:28:11 +00001107 case Type::PointerTyID:cout << (void*)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001108 default:
1109 cout << "- Don't know how to print value of this type!";
1110 break;
1111 }
1112}
1113
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001114void Interpreter::print(const Type *Ty, GenericValue V) {
1115 cout << Ty << " ";
1116 printValue(Ty, V);
1117}
1118
1119void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001120 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1121 if (!PickedVal) return;
1122
Chris Lattner9636a912001-10-01 16:18:37 +00001123 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001124 cout << M; // Print the method
1125 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001126 print(PickedVal->getType(),
1127 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001128 cout << endl;
1129 }
1130
1131}
1132
Chris Lattner86660982001-08-27 05:16:50 +00001133void Interpreter::infoValue(const string &Name) {
1134 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1135 if (!PickedVal) return;
1136
1137 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001138 print(PickedVal->getType(),
1139 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001140 cout << endl;
1141 printOperandInfo(PickedVal, ECStack[CurFrame]);
1142}
1143
Chris Lattner92101ac2001-08-23 17:05:04 +00001144void Interpreter::list() {
1145 if (ECStack.empty())
1146 cout << "Error: No program executing!\n";
1147 else
1148 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
1149}
1150
1151void Interpreter::printStackTrace() {
1152 if (ECStack.empty()) cout << "No program executing!\n";
1153
1154 for (unsigned i = 0; i < ECStack.size(); ++i) {
1155 cout << (((int)i == CurFrame) ? '>' : '-');
1156 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
1157 << ECStack[i].CurMethod->getName() << "\"(";
1158 // TODO: Print Args
1159 cout << ")" << endl;
1160 cout << *ECStack[i].CurInst;
1161 }
1162}