blob: d7a60c7311cb456a76e8dd1bf293e32eb868b052 [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 Lattner92101ac2001-08-23 17:05:04 +0000335#define IMPLEMENT_SETCC(OP, TY) \
336 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
337
Chris Lattner92101ac2001-08-23 17:05:04 +0000338static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
339 const Type *Ty, ExecutionContext &SF) {
340 GenericValue Dest;
341 switch (Ty->getPrimitiveID()) {
342 IMPLEMENT_SETCC(==, UByte);
343 IMPLEMENT_SETCC(==, SByte);
344 IMPLEMENT_SETCC(==, UShort);
345 IMPLEMENT_SETCC(==, Short);
346 IMPLEMENT_SETCC(==, UInt);
347 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000348 IMPLEMENT_SETCC(==, ULong);
349 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000350 IMPLEMENT_SETCC(==, Float);
351 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000352 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000353 default:
354 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
355 }
356 return Dest;
357}
358
359static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
360 const Type *Ty, ExecutionContext &SF) {
361 GenericValue Dest;
362 switch (Ty->getPrimitiveID()) {
363 IMPLEMENT_SETCC(!=, UByte);
364 IMPLEMENT_SETCC(!=, SByte);
365 IMPLEMENT_SETCC(!=, UShort);
366 IMPLEMENT_SETCC(!=, Short);
367 IMPLEMENT_SETCC(!=, UInt);
368 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000369 IMPLEMENT_SETCC(!=, ULong);
370 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000371 IMPLEMENT_SETCC(!=, Float);
372 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000373 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 default:
375 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
376 }
377 return Dest;
378}
379
380static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
381 const Type *Ty, ExecutionContext &SF) {
382 GenericValue Dest;
383 switch (Ty->getPrimitiveID()) {
384 IMPLEMENT_SETCC(<=, UByte);
385 IMPLEMENT_SETCC(<=, SByte);
386 IMPLEMENT_SETCC(<=, UShort);
387 IMPLEMENT_SETCC(<=, Short);
388 IMPLEMENT_SETCC(<=, UInt);
389 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000390 IMPLEMENT_SETCC(<=, ULong);
391 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000392 IMPLEMENT_SETCC(<=, Float);
393 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000394 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000395 default:
396 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
397 }
398 return Dest;
399}
400
401static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
402 const Type *Ty, ExecutionContext &SF) {
403 GenericValue Dest;
404 switch (Ty->getPrimitiveID()) {
405 IMPLEMENT_SETCC(>=, UByte);
406 IMPLEMENT_SETCC(>=, SByte);
407 IMPLEMENT_SETCC(>=, UShort);
408 IMPLEMENT_SETCC(>=, Short);
409 IMPLEMENT_SETCC(>=, UInt);
410 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000411 IMPLEMENT_SETCC(>=, ULong);
412 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000413 IMPLEMENT_SETCC(>=, Float);
414 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000415 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000416 default:
417 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
418 }
419 return Dest;
420}
421
422static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
423 const Type *Ty, ExecutionContext &SF) {
424 GenericValue Dest;
425 switch (Ty->getPrimitiveID()) {
426 IMPLEMENT_SETCC(<, UByte);
427 IMPLEMENT_SETCC(<, SByte);
428 IMPLEMENT_SETCC(<, UShort);
429 IMPLEMENT_SETCC(<, Short);
430 IMPLEMENT_SETCC(<, UInt);
431 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000432 IMPLEMENT_SETCC(<, ULong);
433 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000434 IMPLEMENT_SETCC(<, Float);
435 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000436 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000437 default:
438 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
439 }
440 return Dest;
441}
442
443static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
444 const Type *Ty, ExecutionContext &SF) {
445 GenericValue Dest;
446 switch (Ty->getPrimitiveID()) {
447 IMPLEMENT_SETCC(>, UByte);
448 IMPLEMENT_SETCC(>, SByte);
449 IMPLEMENT_SETCC(>, UShort);
450 IMPLEMENT_SETCC(>, Short);
451 IMPLEMENT_SETCC(>, UInt);
452 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000453 IMPLEMENT_SETCC(>, ULong);
454 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 IMPLEMENT_SETCC(>, Float);
456 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000457 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 default:
459 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
460 }
461 return Dest;
462}
463
464static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
465 const Type *Ty = I->getOperand(0)->getType();
466 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
467 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
468 GenericValue R; // Result
469
470 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000471 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
472 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
473 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
474 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
475 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000476 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
477 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
478 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
479 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
480 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
481 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
482 default:
483 cout << "Don't know how to handle this binary operator!\n-->" << I;
484 }
485
486 SetValue(I, R, SF);
487}
488
Chris Lattner92101ac2001-08-23 17:05:04 +0000489//===----------------------------------------------------------------------===//
490// Terminator Instruction Implementations
491//===----------------------------------------------------------------------===//
492
Chris Lattnere43db882001-10-27 04:15:57 +0000493void Interpreter::exitCalled(GenericValue GV) {
494 cout << "Program returned ";
495 print(Type::IntTy, GV);
496 cout << " via 'void exit(int)'\n";
497
498 ExitCode = GV.SByteVal;
499 ECStack.clear();
500}
501
Chris Lattner92101ac2001-08-23 17:05:04 +0000502void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
503 const Type *RetTy = 0;
504 GenericValue Result;
505
506 // Save away the return value... (if we are not 'ret void')
507 if (I->getNumOperands()) {
508 RetTy = I->getReturnValue()->getType();
509 Result = getOperandValue(I->getReturnValue(), SF);
510 }
511
512 // Save previously executing meth
513 const Method *M = ECStack.back().CurMethod;
514
515 // Pop the current stack frame... this invalidates SF
516 ECStack.pop_back();
517
518 if (ECStack.empty()) { // Finished main. Put result into exit code...
519 if (RetTy) { // Nonvoid return type?
520 cout << "Method " << M->getType() << " \"" << M->getName()
521 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000522 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000523 cout << endl;
524
525 if (RetTy->isIntegral())
526 ExitCode = Result.SByteVal; // Capture the exit code of the program
527 } else {
528 ExitCode = 0;
529 }
530 return;
531 }
532
533 // If we have a previous stack frame, and we have a previous call, fill in
534 // the return value...
535 //
536 ExecutionContext &NewSF = ECStack.back();
537 if (NewSF.Caller) {
538 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
539 SetValue(NewSF.Caller, Result, NewSF);
540
541 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000542 } else {
543 // This must be a function that is executing because of a user 'call'
544 // instruction.
545 cout << "Method " << M->getType() << " \"" << M->getName()
546 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000547 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000548 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000549 }
550}
551
552void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
553 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
554 BasicBlock *Dest;
555
556 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
557 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000558 Value *Cond = I->getCondition();
559 GenericValue CondVal = getOperandValue(Cond, SF);
560 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000561 Dest = I->getSuccessor(1);
562 }
563 SF.CurBB = Dest; // Update CurBB to branch destination
564 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
565}
566
567//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000568// Memory Instruction Implementations
569//===----------------------------------------------------------------------===//
570
Chris Lattner86660982001-08-27 05:16:50 +0000571void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
572 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
573 unsigned NumElements = 1;
574
575 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000576 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000577 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000578 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000579
580 // Get the number of elements being allocated by the array...
581 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
582 NumElements = NumEl.UIntVal;
583 }
584
585 // Allocate enough memory to hold the type...
586 GenericValue Result;
Chris Lattnerc2593162001-10-27 08:28:11 +0000587 Result.ULongVal = (uint64_t)malloc(NumElements * TD.getTypeSize(Ty));
588 assert(Result.ULongVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000589 SetValue(I, Result, SF);
590
591 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000592 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000593 }
594}
595
596static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
597 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
598 GenericValue Value = getOperandValue(I->getOperand(0), SF);
599 // TODO: Check to make sure memory is allocated
Chris Lattnerc2593162001-10-27 08:28:11 +0000600 free((void*)Value.ULongVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000601}
602
Chris Lattner95c3af52001-10-29 19:32:19 +0000603
604// getElementOffset - The workhorse for getelementptr, load and store. This
605// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
606// the pointer type specified by argument Arg.
607//
608static uint64_t getElementOffset(Instruction *I, unsigned ArgOff) {
609 assert(isa<PointerType>(I->getOperand(ArgOff)->getType()) &&
610 "Cannot getElementOffset of a nonpointer type!");
611
612 uint64_t Total = 0;
613 const Type *Ty =
614 cast<PointerType>(I->getOperand(ArgOff++)->getType())->getValueType();
615
616 while (ArgOff < I->getNumOperands()) {
617 const StructType *STy = cast<StructType>(Ty);
618 const StructLayout *SLO = TD.getStructLayout(STy);
619
620 // Indicies must be ubyte constants...
621 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
622 assert(CPU->getType() == Type::UByteTy);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000623 unsigned Index = CPU->getValue();
624 Total += SLO->MemberOffsets[Index];
625 Ty = STy->getElementTypes()[Index];
Chris Lattner95c3af52001-10-29 19:32:19 +0000626 }
627
628 return Total;
629}
630
631static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
632 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
633
634 GenericValue Result;
635 Result.ULongVal = SrcPtr + getElementOffset(I, 0);
636 SetValue(I, Result, SF);
637}
638
Chris Lattner86660982001-08-27 05:16:50 +0000639static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000640 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
Chris Lattnerbb76f022001-10-30 20:27:31 +0000641 uint64_t Offset = getElementOffset(I, 0); // Handle any structure indices
642 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000643
644 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000645 GenericValue Result;
646
647 switch (I->getType()->getPrimitiveID()) {
648 case Type::BoolTyID:
649 case Type::UByteTyID:
650 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
651 case Type::UShortTyID:
652 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
653 case Type::UIntTyID:
654 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000655 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000656 case Type::LongTyID:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000657 case Type::PointerTyID: Result.ULongVal = Ptr->PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000658 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
659 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000660 default:
661 cout << "Cannot load value of type " << I->getType() << "!\n";
662 }
663
664 SetValue(I, Result, SF);
665}
666
667static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000668 uint64_t SrcPtr = getOperandValue(I->getPtrOperand(), SF).ULongVal;
669 SrcPtr += getElementOffset(I, 1); // Handle any structure indices
670
671 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000672 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000673
674 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
675 case Type::BoolTyID:
676 case Type::UByteTyID:
677 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
678 case Type::UShortTyID:
679 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
680 case Type::UIntTyID:
681 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000682 case Type::ULongTyID:
Chris Lattnerc2593162001-10-27 08:28:11 +0000683 case Type::LongTyID:
684 case Type::PointerTyID: Ptr->LongVal = Val.LongVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000685 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
686 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000687 default:
688 cout << "Cannot store value of type " << I->getType() << "!\n";
689 }
690}
691
692
693//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000694// Miscellaneous Instruction Implementations
695//===----------------------------------------------------------------------===//
696
697void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
698 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000699 vector<GenericValue> ArgVals;
700 ArgVals.reserve(I->getNumOperands()-1);
701 for (unsigned i = 1; i < I->getNumOperands(); ++i)
702 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
703
704 callMethod(I->getCalledMethod(), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000705}
706
707static void executePHINode(PHINode *I, ExecutionContext &SF) {
708 BasicBlock *PrevBB = SF.PrevBB;
709 Value *IncomingValue = 0;
710
711 // Search for the value corresponding to this previous bb...
712 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
713 if (I->getIncomingBlock(--i) == PrevBB) {
714 IncomingValue = I->getIncomingValue(i);
715 break;
716 }
717 }
718 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
719
720 // Found the value, set as the result...
721 SetValue(I, getOperandValue(IncomingValue, SF), SF);
722}
723
Chris Lattner86660982001-08-27 05:16:50 +0000724#define IMPLEMENT_SHIFT(OP, TY) \
725 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
726
727static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
728 const Type *Ty = I->getOperand(0)->getType();
729 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
730 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
731 GenericValue Dest;
732
733 switch (Ty->getPrimitiveID()) {
734 IMPLEMENT_SHIFT(<<, UByte);
735 IMPLEMENT_SHIFT(<<, SByte);
736 IMPLEMENT_SHIFT(<<, UShort);
737 IMPLEMENT_SHIFT(<<, Short);
738 IMPLEMENT_SHIFT(<<, UInt);
739 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000740 IMPLEMENT_SHIFT(<<, ULong);
741 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000742 default:
743 cout << "Unhandled type for Shl instruction: " << Ty << endl;
744 }
745 SetValue(I, Dest, SF);
746}
747
748static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
749 const Type *Ty = I->getOperand(0)->getType();
750 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
751 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
752 GenericValue Dest;
753
754 switch (Ty->getPrimitiveID()) {
755 IMPLEMENT_SHIFT(>>, UByte);
756 IMPLEMENT_SHIFT(>>, SByte);
757 IMPLEMENT_SHIFT(>>, UShort);
758 IMPLEMENT_SHIFT(>>, Short);
759 IMPLEMENT_SHIFT(>>, UInt);
760 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000761 IMPLEMENT_SHIFT(>>, ULong);
762 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000763 default:
764 cout << "Unhandled type for Shr instruction: " << Ty << endl;
765 }
766 SetValue(I, Dest, SF);
767}
768
769#define IMPLEMENT_CAST(DTY, DCTY, STY) \
770 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
771
772#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
773 case Type::DESTTY##TyID: \
774 switch (SrcTy->getPrimitiveID()) { \
775 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
776 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
777 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
778 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
779 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000780 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
781 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000782 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
783 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000784
785#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
786 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
787 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
788
789#define IMPLEMENT_CAST_CASE_END() \
790 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
791 break; \
792 } \
793 break
794
795#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
796 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
797 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000798 IMPLEMENT_CAST_CASE_END()
799
800static void executeCastInst(CastInst *I, ExecutionContext &SF) {
801 const Type *Ty = I->getType();
802 const Type *SrcTy = I->getOperand(0)->getType();
803 GenericValue Src = getOperandValue(I->getOperand(0), SF);
804 GenericValue Dest;
805
806 switch (Ty->getPrimitiveID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000807 IMPLEMENT_CAST_CASE(UByte , unsigned char);
808 IMPLEMENT_CAST_CASE(SByte , signed char);
809 IMPLEMENT_CAST_CASE(UShort , unsigned short);
810 IMPLEMENT_CAST_CASE(Short , signed char);
811 IMPLEMENT_CAST_CASE(UInt , unsigned int );
812 IMPLEMENT_CAST_CASE(Int , signed int );
813 IMPLEMENT_CAST_CASE(ULong , uint64_t);
814 IMPLEMENT_CAST_CASE(Long , int64_t);
Chris Lattnerc2593162001-10-27 08:28:11 +0000815 IMPLEMENT_CAST_CASE(Pointer, uint64_t);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000816 IMPLEMENT_CAST_CASE(Float , float);
817 IMPLEMENT_CAST_CASE(Double , double);
Chris Lattner86660982001-08-27 05:16:50 +0000818 default:
819 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
820 }
821 SetValue(I, Dest, SF);
822}
Chris Lattner92101ac2001-08-23 17:05:04 +0000823
824
825
826
827//===----------------------------------------------------------------------===//
828// Dispatch and Execution Code
829//===----------------------------------------------------------------------===//
830
831MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
832 // Assign slot numbers to the method arguments...
833 const Method::ArgumentListType &ArgList = M->getArgumentList();
834 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
835 AE = ArgList.end(); AI != AE; ++AI) {
836 MethodArgument *MA = *AI;
837 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
838 }
839
840 // Iterate over all of the instructions...
841 unsigned InstNum = 0;
842 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
843 MI != ME; ++MI) {
844 Instruction *I = *MI; // For each instruction...
845 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
846 }
847}
848
849unsigned MethodInfo::getValueSlot(const Value *V) {
850 unsigned Plane = V->getType()->getUniqueID();
851 if (Plane >= NumPlaneElements.size())
852 NumPlaneElements.resize(Plane+1, 0);
853 return NumPlaneElements[Plane]++;
854}
855
856
Chris Lattner92101ac2001-08-23 17:05:04 +0000857//===----------------------------------------------------------------------===//
858// callMethod - Execute the specified method...
859//
Chris Lattner365a76e2001-09-10 04:49:44 +0000860void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
861 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
862 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
863 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +0000864 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000865 GenericValue Result = callExternalMethod(M, ArgVals);
866 const Type *RetTy = M->getReturnType();
867
868 // Copy the result back into the result variable if we are not returning
869 // void.
870 if (RetTy != Type::VoidTy) {
871 if (!ECStack.empty() && ECStack.back().Caller) {
872 ExecutionContext &SF = ECStack.back();
873 CallInst *Caller = SF.Caller;
874 SetValue(SF.Caller, Result, SF);
875
876 SF.Caller = 0; // We returned from the call...
877 } else {
878 // print it.
879 cout << "Method " << M->getType() << " \"" << M->getName()
880 << "\" returned ";
881 print(RetTy, Result);
882 cout << endl;
883
884 if (RetTy->isIntegral())
885 ExitCode = Result.SByteVal; // Capture the exit code of the program
886 }
887 }
888
Chris Lattner92101ac2001-08-23 17:05:04 +0000889 return;
890 }
891
892 // Process the method, assigning instruction numbers to the instructions in
893 // the method. Also calculate the number of values for each type slot active.
894 //
895 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +0000896 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +0000897
Chris Lattner92101ac2001-08-23 17:05:04 +0000898 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
899 StackFrame.CurMethod = M;
900 StackFrame.CurBB = M->front();
901 StackFrame.CurInst = StackFrame.CurBB->begin();
902 StackFrame.MethInfo = MethInfo;
903
904 // Initialize the values to nothing...
905 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
906 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
907 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
908
909 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
910
Chris Lattner92101ac2001-08-23 17:05:04 +0000911
Chris Lattner365a76e2001-09-10 04:49:44 +0000912 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000913 assert(ArgVals.size() == M->getArgumentList().size() &&
914 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +0000915 unsigned i = 0;
916 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
917 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
918 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +0000919 }
920}
921
922// executeInstruction - Interpret a single instruction, increment the "PC", and
923// return true if the next instruction is a breakpoint...
924//
925bool Interpreter::executeInstruction() {
926 assert(!ECStack.empty() && "No program running, cannot execute inst!");
927
928 ExecutionContext &SF = ECStack.back(); // Current stack frame
929 Instruction *I = *SF.CurInst++; // Increment before execute
930
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000931 if (Trace)
932 cout << "Run:" << I;
933
Chris Lattner92101ac2001-08-23 17:05:04 +0000934 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000935 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000936 } else {
937 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +0000938 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +0000939 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
940 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000941 // Memory Instructions
942 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000943 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
944 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
945 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
946 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +0000947 case Instruction::GetElementPtr:
948 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +0000949
950 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +0000951 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
952 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
953 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
954 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
955 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000956 default:
957 cout << "Don't know how to execute this instruction!\n-->" << I;
958 }
959 }
960
961 // Reset the current frame location to the top of stack
962 CurFrame = ECStack.size()-1;
963
964 if (CurFrame == -1) return false; // No breakpoint if no code
965
966 // Return true if there is a breakpoint annotation on the instruction...
967 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
968}
969
970void Interpreter::stepInstruction() { // Do the 'step' command
971 if (ECStack.empty()) {
972 cout << "Error: no program running, cannot step!\n";
973 return;
974 }
975
976 // Run an instruction...
977 executeInstruction();
978
979 // Print the next instruction to execute...
980 printCurrentInstruction();
981}
982
983// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +0000984void Interpreter::nextInstruction() { // Do the 'next' command
985 if (ECStack.empty()) {
986 cout << "Error: no program running, cannot 'next'!\n";
987 return;
988 }
989
990 // If this is a call instruction, step over the call instruction...
991 // TODO: ICALL, CALL WITH, ...
992 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +0000993 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +0000994 // Step into the function...
995 if (executeInstruction()) {
996 // Hit a breakpoint, print current instruction, then return to user...
997 cout << "Breakpoint hit!\n";
998 printCurrentInstruction();
999 return;
1000 }
1001
Chris Lattnera74a6b52001-10-29 14:08:33 +00001002 // If we we able to step into the function, finish it now. We might not be
1003 // able the step into a function, if it's external for example.
1004 if (ECStack.size() != StackSize)
1005 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001006 else
1007 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001008
Chris Lattner92101ac2001-08-23 17:05:04 +00001009 } else {
1010 // Normal instruction, just step...
1011 stepInstruction();
1012 }
1013}
1014
1015void Interpreter::run() {
1016 if (ECStack.empty()) {
1017 cout << "Error: no program running, cannot run!\n";
1018 return;
1019 }
1020
1021 bool HitBreakpoint = false;
1022 while (!ECStack.empty() && !HitBreakpoint) {
1023 // Run an instruction...
1024 HitBreakpoint = executeInstruction();
1025 }
1026
1027 if (HitBreakpoint) {
1028 cout << "Breakpoint hit!\n";
1029 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001030 // Print the next instruction to execute...
1031 printCurrentInstruction();
1032}
1033
1034void Interpreter::finish() {
1035 if (ECStack.empty()) {
1036 cout << "Error: no program running, cannot run!\n";
1037 return;
1038 }
1039
1040 unsigned StackSize = ECStack.size();
1041 bool HitBreakpoint = false;
1042 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1043 // Run an instruction...
1044 HitBreakpoint = executeInstruction();
1045 }
1046
1047 if (HitBreakpoint) {
1048 cout << "Breakpoint hit!\n";
1049 }
1050
1051 // Print the next instruction to execute...
1052 printCurrentInstruction();
1053}
1054
1055
1056
1057// printCurrentInstruction - Print out the instruction that the virtual PC is
1058// at, or fail silently if no program is running.
1059//
1060void Interpreter::printCurrentInstruction() {
1061 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001062 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1063 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1064
Chris Lattner92101ac2001-08-23 17:05:04 +00001065 Instruction *I = *ECStack.back().CurInst;
1066 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1067 assert(IN && "Instruction has no numbering annotation!");
1068 cout << "#" << IN->InstNum << I;
1069 }
1070}
1071
1072void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001073 switch (Ty->getPrimitiveID()) {
1074 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1075 case Type::SByteTyID: cout << V.SByteVal; break;
1076 case Type::UByteTyID: cout << V.UByteVal; break;
1077 case Type::ShortTyID: cout << V.ShortVal; break;
1078 case Type::UShortTyID: cout << V.UShortVal; break;
1079 case Type::IntTyID: cout << V.IntVal; break;
1080 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001081 case Type::LongTyID: cout << V.LongVal; break;
1082 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001083 case Type::FloatTyID: cout << V.FloatVal; break;
1084 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerc2593162001-10-27 08:28:11 +00001085 case Type::PointerTyID:cout << (void*)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001086 default:
1087 cout << "- Don't know how to print value of this type!";
1088 break;
1089 }
1090}
1091
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001092void Interpreter::print(const Type *Ty, GenericValue V) {
1093 cout << Ty << " ";
1094 printValue(Ty, V);
1095}
1096
1097void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001098 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1099 if (!PickedVal) return;
1100
Chris Lattner9636a912001-10-01 16:18:37 +00001101 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001102 cout << M; // Print the method
1103 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001104 print(PickedVal->getType(),
1105 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001106 cout << endl;
1107 }
1108
1109}
1110
Chris Lattner86660982001-08-27 05:16:50 +00001111void Interpreter::infoValue(const string &Name) {
1112 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1113 if (!PickedVal) return;
1114
1115 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001116 print(PickedVal->getType(),
1117 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001118 cout << endl;
1119 printOperandInfo(PickedVal, ECStack[CurFrame]);
1120}
1121
Chris Lattner92101ac2001-08-23 17:05:04 +00001122void Interpreter::list() {
1123 if (ECStack.empty())
1124 cout << "Error: No program executing!\n";
1125 else
1126 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
1127}
1128
1129void Interpreter::printStackTrace() {
1130 if (ECStack.empty()) cout << "No program executing!\n";
1131
1132 for (unsigned i = 0; i < ECStack.size(); ++i) {
1133 cout << (((int)i == CurFrame) ? '>' : '-');
1134 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
1135 << ECStack[i].CurMethod->getName() << "\"(";
1136 // TODO: Print Args
1137 cout << ")" << endl;
1138 cout << *ECStack[i].CurInst;
1139 }
1140}