blob: 5c763d79fbd465e0dd95e8ef4536c41009c31d66 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// This file contains the actual instruction interpreter.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Interpreter.h"
8#include "ExecutionAnnotations.h"
9#include "llvm/iOther.h"
10#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000011#include "llvm/iMemory.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000012#include "llvm/Type.h"
13#include "llvm/ConstPoolVals.h"
14#include "llvm/Assembly/Writer.h"
Chris Lattner86660982001-08-27 05:16:50 +000015#include "llvm/Support/DataTypes.h"
Chris Lattner41c2e5c2001-09-28 22:56:43 +000016#include "llvm/Target/TargetData.h"
Chris Lattner2e42d3a2001-10-15 05:51:48 +000017#include "llvm/GlobalVariable.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000018#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000019#include <signal.h>
20#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000021
22// Create a TargetData structure to handle memory addressing and size/alignment
23// computations
24//
25static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000026CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000027
28
Chris Lattnere2409062001-11-12 16:19:45 +000029#ifdef PROFILE_STRUCTURE_FIELDS
30#include "llvm/Support/CommandLine.h"
31static cl::Flag ProfileStructureFields("profilestructfields",
32 "Profile Structure Field Accesses");
33#include <map>
34static map<const StructType *, vector<unsigned> > FieldAccessCounts;
35#endif
36
Chris Lattner5af0c482001-11-07 04:23:00 +000037sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000038static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000039
40extern "C" {
41static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000042 if (InInstruction)
43 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000044}
45}
46
47static void initializeSignalHandlers() {
48 struct sigaction Action;
49 Action.sa_handler = SigHandler;
50 Action.sa_flags = SA_SIGINFO;
51 sigemptyset(&Action.sa_mask);
52 sigaction(SIGSEGV, &Action, 0);
53 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000054 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000055 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000056}
57
Chris Lattner2e42d3a2001-10-15 05:51:48 +000058
59//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000060// Value Manipulation code
61//===----------------------------------------------------------------------===//
62
63static unsigned getOperandSlot(Value *V) {
64 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
65 assert(SN && "Operand does not have a slot number annotation!");
66 return SN->SlotNum;
67}
68
69#define GET_CONST_VAL(TY, CLASS) \
70 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
71
72static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
73 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
74 GenericValue Result;
75 switch (CPV->getType()->getPrimitiveID()) {
76 GET_CONST_VAL(Bool , ConstPoolBool);
77 GET_CONST_VAL(UByte , ConstPoolUInt);
78 GET_CONST_VAL(SByte , ConstPoolSInt);
79 GET_CONST_VAL(UShort , ConstPoolUInt);
80 GET_CONST_VAL(Short , ConstPoolSInt);
81 GET_CONST_VAL(UInt , ConstPoolUInt);
82 GET_CONST_VAL(Int , ConstPoolSInt);
Chris Lattner7b851ab2001-10-15 19:18:26 +000083 GET_CONST_VAL(ULong , ConstPoolUInt);
84 GET_CONST_VAL(Long , ConstPoolSInt);
Chris Lattner39bb5b42001-10-15 13:25:40 +000085 GET_CONST_VAL(Float , ConstPoolFP);
86 GET_CONST_VAL(Double , ConstPoolFP);
87 case Type::PointerTyID:
88 if (isa<ConstPoolPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000089 Result.PointerVal = 0;
Chris Lattner39bb5b42001-10-15 13:25:40 +000090 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
91 assert(0 && "Not implemented!");
92 } else {
93 assert(0 && "Unknown constant pointer type!");
94 }
95 break;
96 default:
97 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
98 }
99 return Result;
100 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
101 GlobalAddress *Address =
102 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
103 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000104 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000105 return Result;
106 } else {
107 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000108 unsigned OpSlot = getOperandSlot(V);
109 assert(TyP < SF.Values.size() &&
110 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000111 return SF.Values[TyP][getOperandSlot(V)];
112 }
113}
114
115static void printOperandInfo(Value *V, ExecutionContext &SF) {
116 if (isa<ConstPoolVal>(V)) {
117 cout << "Constant Pool Value\n";
118 } else if (isa<GlobalValue>(V)) {
119 cout << "Global Value\n";
120 } else {
121 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
122 unsigned Slot = getOperandSlot(V);
123 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000124 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
125 << " Contents=0x";
126
127 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
128 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
129 unsigned char Cur = Buf[i];
130 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
131 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
132 }
133 cout << endl;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000134 }
135}
136
137
138
139static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
140 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
141
142 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
143 SF.Values[TyP][getOperandSlot(V)] = Val;
144}
145
146
147//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000148// Annotation Wrangling code
149//===----------------------------------------------------------------------===//
150
151void Interpreter::initializeExecutionEngine() {
152 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
153 &MethodInfo::Create);
154 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
155 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000156 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000157}
158
159// InitializeMemory - Recursive function to apply a ConstPool value into the
160// specified memory location...
161//
162static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000163#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
164 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000165 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000166 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000167 } return
168
169 switch (Init->getType()->getPrimitiveID()) {
170 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
171 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
172 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
173 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
174 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
175 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
176 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
177 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
178 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
179 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
180 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
181#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000182
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000183 case Type::ArrayTyID: {
184 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
185 const vector<Use> &Val = CPA->getValues();
186 unsigned ElementSize =
187 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
188 for (unsigned i = 0; i < Val.size(); ++i)
189 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
190 return;
191 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000192
193 case Type::StructTyID: {
194 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
195 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
196 const vector<Use> &Val = CPS->getValues();
197 for (unsigned i = 0; i < Val.size(); ++i)
198 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
199 Addr+SL->MemberOffsets[i]);
200 return;
201 }
202
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000203 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000204 if (isa<ConstPoolPointerNull>(Init)) {
205 *(void**)Addr = 0;
206 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
207 GlobalAddress *Address =
208 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
209 *(void**)Addr = (GenericValue*)Address->Ptr;
210 } else {
211 assert(0 && "Unknown Constant pointer type!");
212 }
213 return;
214
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000215 default:
Chris Lattner461f02f2001-11-07 05:31:27 +0000216 CW << "Bad Type: " << Init->getType() << endl;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000217 assert(0 && "Unknown constant type to initialize memory with!");
218 }
219}
220
221Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
222 assert(AID == GlobalAddressAID);
223
224 // This annotation will only be created on GlobalValue objects...
225 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
226
227 if (isa<Method>(GVal)) {
228 // The GlobalAddress object for a method is just a pointer to method itself.
229 // Don't delete it when the annotation is gone though!
230 return new GlobalAddress(GVal, false);
231 }
232
233 // Handle the case of a global variable...
234 assert(isa<GlobalVariable>(GVal) &&
235 "Global value found that isn't a method or global variable!");
236 GlobalVariable *GV = cast<GlobalVariable>(GVal);
237
238 // First off, we must allocate space for the global variable to point at...
239 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
240 unsigned NumElements = 1;
241
242 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
243 assert(GV->hasInitializer() && "Const val must have an initializer!");
244 // Allocating a unsized array type?
245 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
246
247 // Get the number of elements being allocated by the array...
248 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
249 }
250
251 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000252 void *Addr = calloc(NumElements, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000253 assert(Addr != 0 && "Null pointer returned by malloc!");
254
255 // Initialize the memory if there is an initializer...
256 if (GV->hasInitializer())
257 InitializeMemory(GV->getInitializer(), (char*)Addr);
258
259 return new GlobalAddress(Addr, true); // Simply invoke the ctor
260}
261
Chris Lattner92101ac2001-08-23 17:05:04 +0000262
263//===----------------------------------------------------------------------===//
264// Binary Instruction Implementations
265//===----------------------------------------------------------------------===//
266
267#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
268 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
269
270static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
271 const Type *Ty, ExecutionContext &SF) {
272 GenericValue Dest;
273 switch (Ty->getPrimitiveID()) {
274 IMPLEMENT_BINARY_OPERATOR(+, UByte);
275 IMPLEMENT_BINARY_OPERATOR(+, SByte);
276 IMPLEMENT_BINARY_OPERATOR(+, UShort);
277 IMPLEMENT_BINARY_OPERATOR(+, Short);
278 IMPLEMENT_BINARY_OPERATOR(+, UInt);
279 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000280 IMPLEMENT_BINARY_OPERATOR(+, ULong);
281 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000282 IMPLEMENT_BINARY_OPERATOR(+, Float);
283 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000284 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000285 default:
286 cout << "Unhandled type for Add instruction: " << Ty << endl;
287 }
288 return Dest;
289}
290
291static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
292 const Type *Ty, ExecutionContext &SF) {
293 GenericValue Dest;
294 switch (Ty->getPrimitiveID()) {
295 IMPLEMENT_BINARY_OPERATOR(-, UByte);
296 IMPLEMENT_BINARY_OPERATOR(-, SByte);
297 IMPLEMENT_BINARY_OPERATOR(-, UShort);
298 IMPLEMENT_BINARY_OPERATOR(-, Short);
299 IMPLEMENT_BINARY_OPERATOR(-, UInt);
300 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000301 IMPLEMENT_BINARY_OPERATOR(-, ULong);
302 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000303 IMPLEMENT_BINARY_OPERATOR(-, Float);
304 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000305 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000306 default:
307 cout << "Unhandled type for Sub instruction: " << Ty << endl;
308 }
309 return Dest;
310}
311
Chris Lattnerc2593162001-10-27 08:28:11 +0000312static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
313 const Type *Ty, ExecutionContext &SF) {
314 GenericValue Dest;
315 switch (Ty->getPrimitiveID()) {
316 IMPLEMENT_BINARY_OPERATOR(*, UByte);
317 IMPLEMENT_BINARY_OPERATOR(*, SByte);
318 IMPLEMENT_BINARY_OPERATOR(*, UShort);
319 IMPLEMENT_BINARY_OPERATOR(*, Short);
320 IMPLEMENT_BINARY_OPERATOR(*, UInt);
321 IMPLEMENT_BINARY_OPERATOR(*, Int);
322 IMPLEMENT_BINARY_OPERATOR(*, ULong);
323 IMPLEMENT_BINARY_OPERATOR(*, Long);
324 IMPLEMENT_BINARY_OPERATOR(*, Float);
325 IMPLEMENT_BINARY_OPERATOR(*, Double);
326 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
327 default:
328 cout << "Unhandled type for Mul instruction: " << Ty << endl;
329 }
330 return Dest;
331}
332
333static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
334 const Type *Ty, ExecutionContext &SF) {
335 GenericValue Dest;
336 switch (Ty->getPrimitiveID()) {
337 IMPLEMENT_BINARY_OPERATOR(/, UByte);
338 IMPLEMENT_BINARY_OPERATOR(/, SByte);
339 IMPLEMENT_BINARY_OPERATOR(/, UShort);
340 IMPLEMENT_BINARY_OPERATOR(/, Short);
341 IMPLEMENT_BINARY_OPERATOR(/, UInt);
342 IMPLEMENT_BINARY_OPERATOR(/, Int);
343 IMPLEMENT_BINARY_OPERATOR(/, ULong);
344 IMPLEMENT_BINARY_OPERATOR(/, Long);
345 IMPLEMENT_BINARY_OPERATOR(/, Float);
346 IMPLEMENT_BINARY_OPERATOR(/, Double);
347 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
348 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000349 cout << "Unhandled type for Div instruction: " << Ty << endl;
350 }
351 return Dest;
352}
353
354static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
355 const Type *Ty, ExecutionContext &SF) {
356 GenericValue Dest;
357 switch (Ty->getPrimitiveID()) {
358 IMPLEMENT_BINARY_OPERATOR(%, UByte);
359 IMPLEMENT_BINARY_OPERATOR(%, SByte);
360 IMPLEMENT_BINARY_OPERATOR(%, UShort);
361 IMPLEMENT_BINARY_OPERATOR(%, Short);
362 IMPLEMENT_BINARY_OPERATOR(%, UInt);
363 IMPLEMENT_BINARY_OPERATOR(%, Int);
364 IMPLEMENT_BINARY_OPERATOR(%, ULong);
365 IMPLEMENT_BINARY_OPERATOR(%, Long);
366 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
367 case Type::FloatTyID:
368 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
369 break;
370 case Type::DoubleTyID:
371 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
372 break;
373 default:
374 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000375 }
376 return Dest;
377}
378
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000379static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000380 const Type *Ty, ExecutionContext &SF) {
381 GenericValue Dest;
382 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000383 IMPLEMENT_BINARY_OPERATOR(&, UByte);
384 IMPLEMENT_BINARY_OPERATOR(&, SByte);
385 IMPLEMENT_BINARY_OPERATOR(&, UShort);
386 IMPLEMENT_BINARY_OPERATOR(&, Short);
387 IMPLEMENT_BINARY_OPERATOR(&, UInt);
388 IMPLEMENT_BINARY_OPERATOR(&, Int);
389 IMPLEMENT_BINARY_OPERATOR(&, ULong);
390 IMPLEMENT_BINARY_OPERATOR(&, Long);
391 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
392 default:
393 cout << "Unhandled type for And instruction: " << Ty << endl;
394 }
395 return Dest;
396}
397
398
399static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
400 const Type *Ty, ExecutionContext &SF) {
401 GenericValue Dest;
402 switch (Ty->getPrimitiveID()) {
403 IMPLEMENT_BINARY_OPERATOR(|, UByte);
404 IMPLEMENT_BINARY_OPERATOR(|, SByte);
405 IMPLEMENT_BINARY_OPERATOR(|, UShort);
406 IMPLEMENT_BINARY_OPERATOR(|, Short);
407 IMPLEMENT_BINARY_OPERATOR(|, UInt);
408 IMPLEMENT_BINARY_OPERATOR(|, Int);
409 IMPLEMENT_BINARY_OPERATOR(|, ULong);
410 IMPLEMENT_BINARY_OPERATOR(|, Long);
411 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
412 default:
413 cout << "Unhandled type for Or instruction: " << Ty << endl;
414 }
415 return Dest;
416}
417
418
419static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
420 const Type *Ty, ExecutionContext &SF) {
421 GenericValue Dest;
422 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000423 IMPLEMENT_BINARY_OPERATOR(^, UByte);
424 IMPLEMENT_BINARY_OPERATOR(^, SByte);
425 IMPLEMENT_BINARY_OPERATOR(^, UShort);
426 IMPLEMENT_BINARY_OPERATOR(^, Short);
427 IMPLEMENT_BINARY_OPERATOR(^, UInt);
428 IMPLEMENT_BINARY_OPERATOR(^, Int);
429 IMPLEMENT_BINARY_OPERATOR(^, ULong);
430 IMPLEMENT_BINARY_OPERATOR(^, Long);
431 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
432 default:
433 cout << "Unhandled type for Xor instruction: " << Ty << endl;
434 }
435 return Dest;
436}
437
438
Chris Lattner92101ac2001-08-23 17:05:04 +0000439#define IMPLEMENT_SETCC(OP, TY) \
440 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
441
Chris Lattner92101ac2001-08-23 17:05:04 +0000442static GenericValue executeSetEQInst(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 SetEQ instruction: " << Ty << endl;
459 }
460 return Dest;
461}
462
463static GenericValue executeSetNEInst(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 Lattnerea38c0e2001-11-07 19:46:27 +0000478
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 default:
480 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
481 }
482 return Dest;
483}
484
485static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
486 const Type *Ty, ExecutionContext &SF) {
487 GenericValue Dest;
488 switch (Ty->getPrimitiveID()) {
489 IMPLEMENT_SETCC(<=, UByte);
490 IMPLEMENT_SETCC(<=, SByte);
491 IMPLEMENT_SETCC(<=, UShort);
492 IMPLEMENT_SETCC(<=, Short);
493 IMPLEMENT_SETCC(<=, UInt);
494 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000495 IMPLEMENT_SETCC(<=, ULong);
496 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 IMPLEMENT_SETCC(<=, Float);
498 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000499 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000500 default:
501 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
502 }
503 return Dest;
504}
505
506static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
507 const Type *Ty, ExecutionContext &SF) {
508 GenericValue Dest;
509 switch (Ty->getPrimitiveID()) {
510 IMPLEMENT_SETCC(>=, UByte);
511 IMPLEMENT_SETCC(>=, SByte);
512 IMPLEMENT_SETCC(>=, UShort);
513 IMPLEMENT_SETCC(>=, Short);
514 IMPLEMENT_SETCC(>=, UInt);
515 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000516 IMPLEMENT_SETCC(>=, ULong);
517 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000518 IMPLEMENT_SETCC(>=, Float);
519 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000520 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 default:
522 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
523 }
524 return Dest;
525}
526
527static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
528 const Type *Ty, ExecutionContext &SF) {
529 GenericValue Dest;
530 switch (Ty->getPrimitiveID()) {
531 IMPLEMENT_SETCC(<, UByte);
532 IMPLEMENT_SETCC(<, SByte);
533 IMPLEMENT_SETCC(<, UShort);
534 IMPLEMENT_SETCC(<, Short);
535 IMPLEMENT_SETCC(<, UInt);
536 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000537 IMPLEMENT_SETCC(<, ULong);
538 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000539 IMPLEMENT_SETCC(<, Float);
540 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000541 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000542 default:
543 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
544 }
545 return Dest;
546}
547
548static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
549 const Type *Ty, ExecutionContext &SF) {
550 GenericValue Dest;
551 switch (Ty->getPrimitiveID()) {
552 IMPLEMENT_SETCC(>, UByte);
553 IMPLEMENT_SETCC(>, SByte);
554 IMPLEMENT_SETCC(>, UShort);
555 IMPLEMENT_SETCC(>, Short);
556 IMPLEMENT_SETCC(>, UInt);
557 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000558 IMPLEMENT_SETCC(>, ULong);
559 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000560 IMPLEMENT_SETCC(>, Float);
561 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000562 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000563 default:
564 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
565 }
566 return Dest;
567}
568
569static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
570 const Type *Ty = I->getOperand(0)->getType();
571 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
572 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
573 GenericValue R; // Result
574
575 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000576 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
577 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
578 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
579 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
580 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000581 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
582 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000583 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000584 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
585 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
586 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
587 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
588 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
589 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
590 default:
591 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000592 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000593 }
594
595 SetValue(I, R, SF);
596}
597
Chris Lattner92101ac2001-08-23 17:05:04 +0000598//===----------------------------------------------------------------------===//
599// Terminator Instruction Implementations
600//===----------------------------------------------------------------------===//
601
Chris Lattnera95c6992001-11-12 16:28:48 +0000602static void PerformExitStuff() {
603#ifdef PROFILE_STRUCTURE_FIELDS
604 // Print out structure field accounting information...
605 if (!FieldAccessCounts.empty()) {
606 CW << "Field Access Profile Information:\n";
607 map<const StructType *, vector<unsigned> >::iterator
608 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
609 for (; I != E; ++I) {
610 vector<unsigned> &OfC = I->second;
611 CW << " '" << (Value*)I->first << "'\t- Sum=";
612
613 unsigned Sum = 0;
614 for (unsigned i = 0; i < OfC.size(); ++i)
615 Sum += OfC[i];
616 CW << Sum << " - ";
617
618 for (unsigned i = 0; i < OfC.size(); ++i) {
619 if (i) CW << ", ";
620 CW << OfC[i];
621 }
622 CW << endl;
623 }
624 CW << endl;
625 FieldAccessCounts.clear();
626 }
627#endif
628}
629
Chris Lattnere43db882001-10-27 04:15:57 +0000630void Interpreter::exitCalled(GenericValue GV) {
631 cout << "Program returned ";
632 print(Type::IntTy, GV);
633 cout << " via 'void exit(int)'\n";
634
635 ExitCode = GV.SByteVal;
636 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000637 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000638}
639
Chris Lattner92101ac2001-08-23 17:05:04 +0000640void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
641 const Type *RetTy = 0;
642 GenericValue Result;
643
644 // Save away the return value... (if we are not 'ret void')
645 if (I->getNumOperands()) {
646 RetTy = I->getReturnValue()->getType();
647 Result = getOperandValue(I->getReturnValue(), SF);
648 }
649
650 // Save previously executing meth
651 const Method *M = ECStack.back().CurMethod;
652
653 // Pop the current stack frame... this invalidates SF
654 ECStack.pop_back();
655
656 if (ECStack.empty()) { // Finished main. Put result into exit code...
657 if (RetTy) { // Nonvoid return type?
Chris Lattner5af0c482001-11-07 04:23:00 +0000658 CW << "Method " << M->getType() << " \"" << M->getName()
659 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000660 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000661 cout << endl;
662
663 if (RetTy->isIntegral())
664 ExitCode = Result.SByteVal; // Capture the exit code of the program
665 } else {
666 ExitCode = 0;
667 }
Chris Lattnere2409062001-11-12 16:19:45 +0000668
Chris Lattnera95c6992001-11-12 16:28:48 +0000669 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000670 return;
671 }
672
673 // If we have a previous stack frame, and we have a previous call, fill in
674 // the return value...
675 //
676 ExecutionContext &NewSF = ECStack.back();
677 if (NewSF.Caller) {
678 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
679 SetValue(NewSF.Caller, Result, NewSF);
680
681 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000682 } else {
683 // This must be a function that is executing because of a user 'call'
684 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000685 CW << "Method " << M->getType() << " \"" << M->getName()
686 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000687 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000688 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000689 }
690}
691
692void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
693 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
694 BasicBlock *Dest;
695
696 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
697 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000698 Value *Cond = I->getCondition();
699 GenericValue CondVal = getOperandValue(Cond, SF);
700 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000701 Dest = I->getSuccessor(1);
702 }
703 SF.CurBB = Dest; // Update CurBB to branch destination
704 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
705}
706
707//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000708// Memory Instruction Implementations
709//===----------------------------------------------------------------------===//
710
Chris Lattner86660982001-08-27 05:16:50 +0000711void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
712 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
713 unsigned NumElements = 1;
714
715 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000716 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000717 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000718 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000719
720 // Get the number of elements being allocated by the array...
721 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
722 NumElements = NumEl.UIntVal;
723 }
724
725 // Allocate enough memory to hold the type...
726 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000727 // FIXME: Don't use CALLOC, use a tainted malloc.
728 Result.PointerVal = (PointerTy)calloc(NumElements, TD.getTypeSize(Ty));
729 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000730 SetValue(I, Result, SF);
731
732 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000733 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000734 }
735}
736
737static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
738 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
739 GenericValue Value = getOperandValue(I->getOperand(0), SF);
740 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000741 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000742}
743
Chris Lattner95c3af52001-10-29 19:32:19 +0000744
745// getElementOffset - The workhorse for getelementptr, load and store. This
746// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
747// the pointer type specified by argument Arg.
748//
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000749static PointerTy getElementOffset(Instruction *I, unsigned ArgOff) {
Chris Lattner95c3af52001-10-29 19:32:19 +0000750 assert(isa<PointerType>(I->getOperand(ArgOff)->getType()) &&
751 "Cannot getElementOffset of a nonpointer type!");
752
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000753 PointerTy Total = 0;
Chris Lattner95c3af52001-10-29 19:32:19 +0000754 const Type *Ty =
755 cast<PointerType>(I->getOperand(ArgOff++)->getType())->getValueType();
756
757 while (ArgOff < I->getNumOperands()) {
758 const StructType *STy = cast<StructType>(Ty);
759 const StructLayout *SLO = TD.getStructLayout(STy);
760
761 // Indicies must be ubyte constants...
762 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
763 assert(CPU->getType() == Type::UByteTy);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000764 unsigned Index = CPU->getValue();
Chris Lattnere2409062001-11-12 16:19:45 +0000765
766#ifdef PROFILE_STRUCTURE_FIELDS
767 if (ProfileStructureFields) {
768 // Do accounting for this field...
769 vector<unsigned> &OfC = FieldAccessCounts[STy];
770 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
771 OfC[Index]++;
772 }
773#endif
774
Chris Lattnerbb76f022001-10-30 20:27:31 +0000775 Total += SLO->MemberOffsets[Index];
776 Ty = STy->getElementTypes()[Index];
Chris Lattner95c3af52001-10-29 19:32:19 +0000777 }
778
779 return Total;
780}
781
782static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000783 GenericValue SRC = getOperandValue(I->getPtrOperand(), SF);
784 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000785
786 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000787 Result.PointerVal = SrcPtr + getElementOffset(I, 0);
Chris Lattner95c3af52001-10-29 19:32:19 +0000788 SetValue(I, Result, SF);
789}
790
Chris Lattner86660982001-08-27 05:16:50 +0000791static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000792 GenericValue SRC = getOperandValue(I->getPtrOperand(), SF);
793 PointerTy SrcPtr = SRC.PointerVal;
794 PointerTy Offset = getElementOffset(I, 0); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000795 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000796
797 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000798 GenericValue Result;
799
800 switch (I->getType()->getPrimitiveID()) {
801 case Type::BoolTyID:
802 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000803 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000804 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000805 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000806 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000807 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000808 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000809 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
810 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
811 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
812 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000813 default:
814 cout << "Cannot load value of type " << I->getType() << "!\n";
815 }
816
817 SetValue(I, Result, SF);
818}
819
820static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000821 GenericValue SRC = getOperandValue(I->getPtrOperand(), SF);
822 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000823 SrcPtr += getElementOffset(I, 1); // Handle any structure indices
824
825 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000826 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000827
828 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
829 case Type::BoolTyID:
830 case Type::UByteTyID:
831 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
832 case Type::UShortTyID:
833 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
834 case Type::UIntTyID:
835 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000836 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000837 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
838 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000839 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
840 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000841 default:
842 cout << "Cannot store value of type " << I->getType() << "!\n";
843 }
844}
845
846
847//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000848// Miscellaneous Instruction Implementations
849//===----------------------------------------------------------------------===//
850
851void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
852 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000853 vector<GenericValue> ArgVals;
854 ArgVals.reserve(I->getNumOperands()-1);
855 for (unsigned i = 1; i < I->getNumOperands(); ++i)
856 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
857
Chris Lattner070cf5e2001-11-07 20:12:30 +0000858 // To handle indirect calls, we must get the pointer value from the argument
859 // and treat it as a method pointer.
860 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
861
862 callMethod((Method*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000863}
864
865static void executePHINode(PHINode *I, ExecutionContext &SF) {
866 BasicBlock *PrevBB = SF.PrevBB;
867 Value *IncomingValue = 0;
868
869 // Search for the value corresponding to this previous bb...
870 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
871 if (I->getIncomingBlock(--i) == PrevBB) {
872 IncomingValue = I->getIncomingValue(i);
873 break;
874 }
875 }
876 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
877
878 // Found the value, set as the result...
879 SetValue(I, getOperandValue(IncomingValue, SF), SF);
880}
881
Chris Lattner86660982001-08-27 05:16:50 +0000882#define IMPLEMENT_SHIFT(OP, TY) \
883 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
884
885static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
886 const Type *Ty = I->getOperand(0)->getType();
887 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
888 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
889 GenericValue Dest;
890
891 switch (Ty->getPrimitiveID()) {
892 IMPLEMENT_SHIFT(<<, UByte);
893 IMPLEMENT_SHIFT(<<, SByte);
894 IMPLEMENT_SHIFT(<<, UShort);
895 IMPLEMENT_SHIFT(<<, Short);
896 IMPLEMENT_SHIFT(<<, UInt);
897 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000898 IMPLEMENT_SHIFT(<<, ULong);
899 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000900 default:
901 cout << "Unhandled type for Shl instruction: " << Ty << endl;
902 }
903 SetValue(I, Dest, SF);
904}
905
906static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
907 const Type *Ty = I->getOperand(0)->getType();
908 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
909 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
910 GenericValue Dest;
911
912 switch (Ty->getPrimitiveID()) {
913 IMPLEMENT_SHIFT(>>, UByte);
914 IMPLEMENT_SHIFT(>>, SByte);
915 IMPLEMENT_SHIFT(>>, UShort);
916 IMPLEMENT_SHIFT(>>, Short);
917 IMPLEMENT_SHIFT(>>, UInt);
918 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000919 IMPLEMENT_SHIFT(>>, ULong);
920 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000921 default:
922 cout << "Unhandled type for Shr instruction: " << Ty << endl;
923 }
924 SetValue(I, Dest, SF);
925}
926
927#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000928 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000929
930#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
931 case Type::DESTTY##TyID: \
932 switch (SrcTy->getPrimitiveID()) { \
933 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
934 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
935 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
936 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
937 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000938 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
939 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000940 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000942
943#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
944 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
945 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
946
947#define IMPLEMENT_CAST_CASE_END() \
948 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
949 break; \
950 } \
951 break
952
953#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
954 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
955 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000956 IMPLEMENT_CAST_CASE_END()
957
958static void executeCastInst(CastInst *I, ExecutionContext &SF) {
959 const Type *Ty = I->getType();
960 const Type *SrcTy = I->getOperand(0)->getType();
961 GenericValue Src = getOperandValue(I->getOperand(0), SF);
962 GenericValue Dest;
963
964 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000965 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
966 IMPLEMENT_CAST_CASE(SByte , ( signed char));
967 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
968 IMPLEMENT_CAST_CASE(Short , ( signed char));
969 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
970 IMPLEMENT_CAST_CASE(Int , ( signed int ));
971 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
972 IMPLEMENT_CAST_CASE(Long , ( int64_t));
973 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
974 IMPLEMENT_CAST_CASE(Float , (float));
975 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +0000976 default:
977 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
978 }
979 SetValue(I, Dest, SF);
980}
Chris Lattner92101ac2001-08-23 17:05:04 +0000981
982
983
984
985//===----------------------------------------------------------------------===//
986// Dispatch and Execution Code
987//===----------------------------------------------------------------------===//
988
989MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
990 // Assign slot numbers to the method arguments...
991 const Method::ArgumentListType &ArgList = M->getArgumentList();
992 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
993 AE = ArgList.end(); AI != AE; ++AI) {
994 MethodArgument *MA = *AI;
995 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
996 }
997
998 // Iterate over all of the instructions...
999 unsigned InstNum = 0;
1000 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
1001 MI != ME; ++MI) {
1002 Instruction *I = *MI; // For each instruction...
1003 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
1004 }
1005}
1006
1007unsigned MethodInfo::getValueSlot(const Value *V) {
1008 unsigned Plane = V->getType()->getUniqueID();
1009 if (Plane >= NumPlaneElements.size())
1010 NumPlaneElements.resize(Plane+1, 0);
1011 return NumPlaneElements[Plane]++;
1012}
1013
1014
Chris Lattner92101ac2001-08-23 17:05:04 +00001015//===----------------------------------------------------------------------===//
1016// callMethod - Execute the specified method...
1017//
Chris Lattner365a76e2001-09-10 04:49:44 +00001018void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
1019 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1020 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1021 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001022 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001023 GenericValue Result = callExternalMethod(M, ArgVals);
1024 const Type *RetTy = M->getReturnType();
1025
1026 // Copy the result back into the result variable if we are not returning
1027 // void.
1028 if (RetTy != Type::VoidTy) {
1029 if (!ECStack.empty() && ECStack.back().Caller) {
1030 ExecutionContext &SF = ECStack.back();
1031 CallInst *Caller = SF.Caller;
1032 SetValue(SF.Caller, Result, SF);
1033
1034 SF.Caller = 0; // We returned from the call...
1035 } else {
1036 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +00001037 CW << "Method " << M->getType() << " \"" << M->getName()
1038 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001039 print(RetTy, Result);
1040 cout << endl;
1041
1042 if (RetTy->isIntegral())
1043 ExitCode = Result.SByteVal; // Capture the exit code of the program
1044 }
1045 }
1046
Chris Lattner92101ac2001-08-23 17:05:04 +00001047 return;
1048 }
1049
1050 // Process the method, assigning instruction numbers to the instructions in
1051 // the method. Also calculate the number of values for each type slot active.
1052 //
1053 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001054 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001055
Chris Lattner92101ac2001-08-23 17:05:04 +00001056 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1057 StackFrame.CurMethod = M;
1058 StackFrame.CurBB = M->front();
1059 StackFrame.CurInst = StackFrame.CurBB->begin();
1060 StackFrame.MethInfo = MethInfo;
1061
1062 // Initialize the values to nothing...
1063 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001064 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001065 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1066
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001067 // Taint the initial values of stuff
1068 memset(&StackFrame.Values[i][0], 42,
1069 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1070 }
1071
Chris Lattner92101ac2001-08-23 17:05:04 +00001072 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1073
Chris Lattner92101ac2001-08-23 17:05:04 +00001074
Chris Lattner365a76e2001-09-10 04:49:44 +00001075 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001076 assert(ArgVals.size() == M->getArgumentList().size() &&
1077 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001078 unsigned i = 0;
1079 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1080 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1081 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001082 }
1083}
1084
1085// executeInstruction - Interpret a single instruction, increment the "PC", and
1086// return true if the next instruction is a breakpoint...
1087//
1088bool Interpreter::executeInstruction() {
1089 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1090
1091 ExecutionContext &SF = ECStack.back(); // Current stack frame
1092 Instruction *I = *SF.CurInst++; // Increment before execute
1093
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001094 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001095 CW << "Run:" << I;
1096
1097 // Set a sigsetjmp buffer so that we can recover if an error happens during
1098 // instruction execution...
1099 //
1100 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1101 --SF.CurInst; // Back up to erroring instruction
Chris Lattner461f02f2001-11-07 05:31:27 +00001102 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001103 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001104 printStackTrace();
1105 } else {
1106 cout << "CTRL-C Detected, execution halted.\n";
1107 }
1108 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001109 return true;
1110 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001111
Chris Lattner461f02f2001-11-07 05:31:27 +00001112 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001113 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001114 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001115 } else {
1116 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001117 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001118 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1119 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001120 // Memory Instructions
1121 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001122 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1123 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1124 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1125 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001126 case Instruction::GetElementPtr:
1127 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001128
1129 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001130 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1131 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1132 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1133 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1134 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001135 default:
1136 cout << "Don't know how to execute this instruction!\n-->" << I;
1137 }
1138 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001139 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001140
1141 // Reset the current frame location to the top of stack
1142 CurFrame = ECStack.size()-1;
1143
1144 if (CurFrame == -1) return false; // No breakpoint if no code
1145
1146 // Return true if there is a breakpoint annotation on the instruction...
1147 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1148}
1149
1150void Interpreter::stepInstruction() { // Do the 'step' command
1151 if (ECStack.empty()) {
1152 cout << "Error: no program running, cannot step!\n";
1153 return;
1154 }
1155
1156 // Run an instruction...
1157 executeInstruction();
1158
1159 // Print the next instruction to execute...
1160 printCurrentInstruction();
1161}
1162
1163// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001164void Interpreter::nextInstruction() { // Do the 'next' command
1165 if (ECStack.empty()) {
1166 cout << "Error: no program running, cannot 'next'!\n";
1167 return;
1168 }
1169
1170 // If this is a call instruction, step over the call instruction...
1171 // TODO: ICALL, CALL WITH, ...
1172 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001173 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001174 // Step into the function...
1175 if (executeInstruction()) {
1176 // Hit a breakpoint, print current instruction, then return to user...
1177 cout << "Breakpoint hit!\n";
1178 printCurrentInstruction();
1179 return;
1180 }
1181
Chris Lattnera74a6b52001-10-29 14:08:33 +00001182 // If we we able to step into the function, finish it now. We might not be
1183 // able the step into a function, if it's external for example.
1184 if (ECStack.size() != StackSize)
1185 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001186 else
1187 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001188
Chris Lattner92101ac2001-08-23 17:05:04 +00001189 } else {
1190 // Normal instruction, just step...
1191 stepInstruction();
1192 }
1193}
1194
1195void Interpreter::run() {
1196 if (ECStack.empty()) {
1197 cout << "Error: no program running, cannot run!\n";
1198 return;
1199 }
1200
1201 bool HitBreakpoint = false;
1202 while (!ECStack.empty() && !HitBreakpoint) {
1203 // Run an instruction...
1204 HitBreakpoint = executeInstruction();
1205 }
1206
1207 if (HitBreakpoint) {
1208 cout << "Breakpoint hit!\n";
1209 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001210 // Print the next instruction to execute...
1211 printCurrentInstruction();
1212}
1213
1214void Interpreter::finish() {
1215 if (ECStack.empty()) {
1216 cout << "Error: no program running, cannot run!\n";
1217 return;
1218 }
1219
1220 unsigned StackSize = ECStack.size();
1221 bool HitBreakpoint = false;
1222 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1223 // Run an instruction...
1224 HitBreakpoint = executeInstruction();
1225 }
1226
1227 if (HitBreakpoint) {
1228 cout << "Breakpoint hit!\n";
1229 }
1230
1231 // Print the next instruction to execute...
1232 printCurrentInstruction();
1233}
1234
1235
1236
1237// printCurrentInstruction - Print out the instruction that the virtual PC is
1238// at, or fail silently if no program is running.
1239//
1240void Interpreter::printCurrentInstruction() {
1241 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001242 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1243 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1244
Chris Lattner92101ac2001-08-23 17:05:04 +00001245 Instruction *I = *ECStack.back().CurInst;
1246 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1247 assert(IN && "Instruction has no numbering annotation!");
1248 cout << "#" << IN->InstNum << I;
1249 }
1250}
1251
1252void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001253 switch (Ty->getPrimitiveID()) {
1254 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1255 case Type::SByteTyID: cout << V.SByteVal; break;
1256 case Type::UByteTyID: cout << V.UByteVal; break;
1257 case Type::ShortTyID: cout << V.ShortVal; break;
1258 case Type::UShortTyID: cout << V.UShortVal; break;
1259 case Type::IntTyID: cout << V.IntVal; break;
1260 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001261 case Type::LongTyID: cout << V.LongVal; break;
1262 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001263 case Type::FloatTyID: cout << V.FloatVal; break;
1264 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001265 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001266 default:
1267 cout << "- Don't know how to print value of this type!";
1268 break;
1269 }
1270}
1271
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001272void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001273 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001274 printValue(Ty, V);
1275}
1276
1277void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001278 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1279 if (!PickedVal) return;
1280
Chris Lattner9636a912001-10-01 16:18:37 +00001281 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001282 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001283 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1284 CW << "type %" << Name << " = " << Ty->getDescription() << endl;
1285 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1286 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001287 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001288 print(PickedVal->getType(),
1289 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001290 cout << endl;
1291 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001292}
1293
Chris Lattner86660982001-08-27 05:16:50 +00001294void Interpreter::infoValue(const string &Name) {
1295 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1296 if (!PickedVal) return;
1297
1298 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001299 print(PickedVal->getType(),
1300 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001301 cout << endl;
1302 printOperandInfo(PickedVal, ECStack[CurFrame]);
1303}
1304
Chris Lattner461f02f2001-11-07 05:31:27 +00001305// printStackFrame - Print information about the specified stack frame, or -1
1306// for the default one.
1307//
1308void Interpreter::printStackFrame(int FrameNo = -1) {
1309 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001310 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001311 const Type *RetTy = Meth->getReturnType();
1312
1313 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1314 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001315
1316 Method::ArgumentListType &Args = Meth->getArgumentList();
1317 for (unsigned i = 0; i < Args.size(); ++i) {
1318 if (i != 0) cout << ", ";
1319 CW << (Value*)Args[i] << "=";
1320
1321 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001322 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001323
1324 cout << ")" << endl;
1325 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001326}
Chris Lattner461f02f2001-11-07 05:31:27 +00001327