blob: 2f0ee41c52fb6c01dbb493bdec6e76a045a8b0d6 [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 Lattner41c2e5c2001-09-28 22:56:43 +000015#include "llvm/Target/TargetData.h"
Chris Lattner2e42d3a2001-10-15 05:51:48 +000016#include "llvm/GlobalVariable.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000017#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000018#include <signal.h>
19#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000020
21// Create a TargetData structure to handle memory addressing and size/alignment
22// computations
23//
24static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000025CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000026
27
Chris Lattnere2409062001-11-12 16:19:45 +000028#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029#include "Support/CommandLine.h"
Chris Lattnere2409062001-11-12 16:19:45 +000030static cl::Flag ProfileStructureFields("profilestructfields",
31 "Profile Structure Field Accesses");
32#include <map>
33static map<const StructType *, vector<unsigned> > FieldAccessCounts;
34#endif
35
Chris Lattner5af0c482001-11-07 04:23:00 +000036sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000037static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000038
39extern "C" {
40static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000041 if (InInstruction)
42 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000043}
44}
45
46static void initializeSignalHandlers() {
47 struct sigaction Action;
48 Action.sa_handler = SigHandler;
49 Action.sa_flags = SA_SIGINFO;
50 sigemptyset(&Action.sa_mask);
51 sigaction(SIGSEGV, &Action, 0);
52 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000053 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000054 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000055}
56
Chris Lattner2e42d3a2001-10-15 05:51:48 +000057
58//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000059// Value Manipulation code
60//===----------------------------------------------------------------------===//
61
62static unsigned getOperandSlot(Value *V) {
63 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
64 assert(SN && "Operand does not have a slot number annotation!");
65 return SN->SlotNum;
66}
67
68#define GET_CONST_VAL(TY, CLASS) \
69 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
70
71static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
72 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
73 GenericValue Result;
74 switch (CPV->getType()->getPrimitiveID()) {
75 GET_CONST_VAL(Bool , ConstPoolBool);
76 GET_CONST_VAL(UByte , ConstPoolUInt);
77 GET_CONST_VAL(SByte , ConstPoolSInt);
78 GET_CONST_VAL(UShort , ConstPoolUInt);
79 GET_CONST_VAL(Short , ConstPoolSInt);
80 GET_CONST_VAL(UInt , ConstPoolUInt);
81 GET_CONST_VAL(Int , ConstPoolSInt);
Chris Lattner7b851ab2001-10-15 19:18:26 +000082 GET_CONST_VAL(ULong , ConstPoolUInt);
83 GET_CONST_VAL(Long , ConstPoolSInt);
Chris Lattner39bb5b42001-10-15 13:25:40 +000084 GET_CONST_VAL(Float , ConstPoolFP);
85 GET_CONST_VAL(Double , ConstPoolFP);
86 case Type::PointerTyID:
87 if (isa<ConstPoolPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000088 Result.PointerVal = 0;
Chris Lattner39bb5b42001-10-15 13:25:40 +000089 } else if (ConstPoolPointerRef *CPR =dyn_cast<ConstPoolPointerRef>(CPV)) {
90 assert(0 && "Not implemented!");
91 } else {
92 assert(0 && "Unknown constant pointer type!");
93 }
94 break;
95 default:
96 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
97 }
98 return Result;
99 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
100 GlobalAddress *Address =
101 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
102 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000103 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000104 return Result;
105 } else {
106 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000107 unsigned OpSlot = getOperandSlot(V);
108 assert(TyP < SF.Values.size() &&
109 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000110 return SF.Values[TyP][getOperandSlot(V)];
111 }
112}
113
114static void printOperandInfo(Value *V, ExecutionContext &SF) {
115 if (isa<ConstPoolVal>(V)) {
116 cout << "Constant Pool Value\n";
117 } else if (isa<GlobalValue>(V)) {
118 cout << "Global Value\n";
119 } else {
120 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
121 unsigned Slot = getOperandSlot(V);
122 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000123 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
124 << " Contents=0x";
125
126 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
127 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
128 unsigned char Cur = Buf[i];
129 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
130 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
131 }
132 cout << endl;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000133 }
134}
135
136
137
138static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
139 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
140
141 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
142 SF.Values[TyP][getOperandSlot(V)] = Val;
143}
144
145
146//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000147// Annotation Wrangling code
148//===----------------------------------------------------------------------===//
149
150void Interpreter::initializeExecutionEngine() {
151 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
152 &MethodInfo::Create);
153 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
154 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000155 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000156}
157
158// InitializeMemory - Recursive function to apply a ConstPool value into the
159// specified memory location...
160//
161static void InitializeMemory(ConstPoolVal *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000162#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
163 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000164 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000165 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166 } return
167
168 switch (Init->getType()->getPrimitiveID()) {
169 INITIALIZE_MEMORY(Bool , ConstPoolBool, bool);
170 INITIALIZE_MEMORY(UByte , ConstPoolUInt, unsigned char);
171 INITIALIZE_MEMORY(SByte , ConstPoolSInt, signed char);
172 INITIALIZE_MEMORY(UShort , ConstPoolUInt, unsigned short);
173 INITIALIZE_MEMORY(Short , ConstPoolSInt, signed short);
174 INITIALIZE_MEMORY(UInt , ConstPoolUInt, unsigned int);
175 INITIALIZE_MEMORY(Int , ConstPoolSInt, signed int);
176 INITIALIZE_MEMORY(ULong , ConstPoolUInt, uint64_t);
177 INITIALIZE_MEMORY(Long , ConstPoolSInt, int64_t);
178 INITIALIZE_MEMORY(Float , ConstPoolFP , float);
179 INITIALIZE_MEMORY(Double , ConstPoolFP , double);
180#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000181
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000182 case Type::ArrayTyID: {
183 ConstPoolArray *CPA = cast<ConstPoolArray>(Init);
184 const vector<Use> &Val = CPA->getValues();
185 unsigned ElementSize =
186 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
187 for (unsigned i = 0; i < Val.size(); ++i)
188 InitializeMemory(cast<ConstPoolVal>(Val[i].get()), Addr+i*ElementSize);
189 return;
190 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191
192 case Type::StructTyID: {
193 ConstPoolStruct *CPS = cast<ConstPoolStruct>(Init);
194 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
195 const vector<Use> &Val = CPS->getValues();
196 for (unsigned i = 0; i < Val.size(); ++i)
197 InitializeMemory(cast<ConstPoolVal>(Val[i].get()),
198 Addr+SL->MemberOffsets[i]);
199 return;
200 }
201
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000202 case Type::PointerTyID:
Chris Lattner39bb5b42001-10-15 13:25:40 +0000203 if (isa<ConstPoolPointerNull>(Init)) {
204 *(void**)Addr = 0;
205 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(Init)) {
206 GlobalAddress *Address =
207 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
208 *(void**)Addr = (GenericValue*)Address->Ptr;
209 } else {
210 assert(0 && "Unknown Constant pointer type!");
211 }
212 return;
213
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000214 default:
Chris Lattner461f02f2001-11-07 05:31:27 +0000215 CW << "Bad Type: " << Init->getType() << endl;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000216 assert(0 && "Unknown constant type to initialize memory with!");
217 }
218}
219
220Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
221 assert(AID == GlobalAddressAID);
222
223 // This annotation will only be created on GlobalValue objects...
224 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
225
226 if (isa<Method>(GVal)) {
227 // The GlobalAddress object for a method is just a pointer to method itself.
228 // Don't delete it when the annotation is gone though!
229 return new GlobalAddress(GVal, false);
230 }
231
232 // Handle the case of a global variable...
233 assert(isa<GlobalVariable>(GVal) &&
234 "Global value found that isn't a method or global variable!");
235 GlobalVariable *GV = cast<GlobalVariable>(GVal);
236
237 // First off, we must allocate space for the global variable to point at...
238 const Type *Ty = GV->getType()->getValueType(); // Type to be allocated
239 unsigned NumElements = 1;
240
241 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
242 assert(GV->hasInitializer() && "Const val must have an initializer!");
243 // Allocating a unsized array type?
244 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
245
246 // Get the number of elements being allocated by the array...
247 NumElements =cast<ConstPoolArray>(GV->getInitializer())->getValues().size();
248 }
249
250 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000251 void *Addr = calloc(NumElements, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000252 assert(Addr != 0 && "Null pointer returned by malloc!");
253
254 // Initialize the memory if there is an initializer...
255 if (GV->hasInitializer())
256 InitializeMemory(GV->getInitializer(), (char*)Addr);
257
258 return new GlobalAddress(Addr, true); // Simply invoke the ctor
259}
260
Chris Lattner92101ac2001-08-23 17:05:04 +0000261
262//===----------------------------------------------------------------------===//
263// Binary Instruction Implementations
264//===----------------------------------------------------------------------===//
265
266#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
267 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
268
269static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
270 const Type *Ty, ExecutionContext &SF) {
271 GenericValue Dest;
272 switch (Ty->getPrimitiveID()) {
273 IMPLEMENT_BINARY_OPERATOR(+, UByte);
274 IMPLEMENT_BINARY_OPERATOR(+, SByte);
275 IMPLEMENT_BINARY_OPERATOR(+, UShort);
276 IMPLEMENT_BINARY_OPERATOR(+, Short);
277 IMPLEMENT_BINARY_OPERATOR(+, UInt);
278 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000279 IMPLEMENT_BINARY_OPERATOR(+, ULong);
280 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000281 IMPLEMENT_BINARY_OPERATOR(+, Float);
282 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000283 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000284 default:
285 cout << "Unhandled type for Add instruction: " << Ty << endl;
286 }
287 return Dest;
288}
289
290static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
291 const Type *Ty, ExecutionContext &SF) {
292 GenericValue Dest;
293 switch (Ty->getPrimitiveID()) {
294 IMPLEMENT_BINARY_OPERATOR(-, UByte);
295 IMPLEMENT_BINARY_OPERATOR(-, SByte);
296 IMPLEMENT_BINARY_OPERATOR(-, UShort);
297 IMPLEMENT_BINARY_OPERATOR(-, Short);
298 IMPLEMENT_BINARY_OPERATOR(-, UInt);
299 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000300 IMPLEMENT_BINARY_OPERATOR(-, ULong);
301 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 IMPLEMENT_BINARY_OPERATOR(-, Float);
303 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000304 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000305 default:
306 cout << "Unhandled type for Sub instruction: " << Ty << endl;
307 }
308 return Dest;
309}
310
Chris Lattnerc2593162001-10-27 08:28:11 +0000311static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
312 const Type *Ty, ExecutionContext &SF) {
313 GenericValue Dest;
314 switch (Ty->getPrimitiveID()) {
315 IMPLEMENT_BINARY_OPERATOR(*, UByte);
316 IMPLEMENT_BINARY_OPERATOR(*, SByte);
317 IMPLEMENT_BINARY_OPERATOR(*, UShort);
318 IMPLEMENT_BINARY_OPERATOR(*, Short);
319 IMPLEMENT_BINARY_OPERATOR(*, UInt);
320 IMPLEMENT_BINARY_OPERATOR(*, Int);
321 IMPLEMENT_BINARY_OPERATOR(*, ULong);
322 IMPLEMENT_BINARY_OPERATOR(*, Long);
323 IMPLEMENT_BINARY_OPERATOR(*, Float);
324 IMPLEMENT_BINARY_OPERATOR(*, Double);
325 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
326 default:
327 cout << "Unhandled type for Mul instruction: " << Ty << endl;
328 }
329 return Dest;
330}
331
332static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
333 const Type *Ty, ExecutionContext &SF) {
334 GenericValue Dest;
335 switch (Ty->getPrimitiveID()) {
336 IMPLEMENT_BINARY_OPERATOR(/, UByte);
337 IMPLEMENT_BINARY_OPERATOR(/, SByte);
338 IMPLEMENT_BINARY_OPERATOR(/, UShort);
339 IMPLEMENT_BINARY_OPERATOR(/, Short);
340 IMPLEMENT_BINARY_OPERATOR(/, UInt);
341 IMPLEMENT_BINARY_OPERATOR(/, Int);
342 IMPLEMENT_BINARY_OPERATOR(/, ULong);
343 IMPLEMENT_BINARY_OPERATOR(/, Long);
344 IMPLEMENT_BINARY_OPERATOR(/, Float);
345 IMPLEMENT_BINARY_OPERATOR(/, Double);
346 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
347 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000348 cout << "Unhandled type for Div instruction: " << Ty << endl;
349 }
350 return Dest;
351}
352
353static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
354 const Type *Ty, ExecutionContext &SF) {
355 GenericValue Dest;
356 switch (Ty->getPrimitiveID()) {
357 IMPLEMENT_BINARY_OPERATOR(%, UByte);
358 IMPLEMENT_BINARY_OPERATOR(%, SByte);
359 IMPLEMENT_BINARY_OPERATOR(%, UShort);
360 IMPLEMENT_BINARY_OPERATOR(%, Short);
361 IMPLEMENT_BINARY_OPERATOR(%, UInt);
362 IMPLEMENT_BINARY_OPERATOR(%, Int);
363 IMPLEMENT_BINARY_OPERATOR(%, ULong);
364 IMPLEMENT_BINARY_OPERATOR(%, Long);
365 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
366 case Type::FloatTyID:
367 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
368 break;
369 case Type::DoubleTyID:
370 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
371 break;
372 default:
373 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000374 }
375 return Dest;
376}
377
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000378static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000379 const Type *Ty, ExecutionContext &SF) {
380 GenericValue Dest;
381 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000382 IMPLEMENT_BINARY_OPERATOR(&, UByte);
383 IMPLEMENT_BINARY_OPERATOR(&, SByte);
384 IMPLEMENT_BINARY_OPERATOR(&, UShort);
385 IMPLEMENT_BINARY_OPERATOR(&, Short);
386 IMPLEMENT_BINARY_OPERATOR(&, UInt);
387 IMPLEMENT_BINARY_OPERATOR(&, Int);
388 IMPLEMENT_BINARY_OPERATOR(&, ULong);
389 IMPLEMENT_BINARY_OPERATOR(&, Long);
390 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
391 default:
392 cout << "Unhandled type for And instruction: " << Ty << endl;
393 }
394 return Dest;
395}
396
397
398static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
399 const Type *Ty, ExecutionContext &SF) {
400 GenericValue Dest;
401 switch (Ty->getPrimitiveID()) {
402 IMPLEMENT_BINARY_OPERATOR(|, UByte);
403 IMPLEMENT_BINARY_OPERATOR(|, SByte);
404 IMPLEMENT_BINARY_OPERATOR(|, UShort);
405 IMPLEMENT_BINARY_OPERATOR(|, Short);
406 IMPLEMENT_BINARY_OPERATOR(|, UInt);
407 IMPLEMENT_BINARY_OPERATOR(|, Int);
408 IMPLEMENT_BINARY_OPERATOR(|, ULong);
409 IMPLEMENT_BINARY_OPERATOR(|, Long);
410 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
411 default:
412 cout << "Unhandled type for Or instruction: " << Ty << endl;
413 }
414 return Dest;
415}
416
417
418static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
419 const Type *Ty, ExecutionContext &SF) {
420 GenericValue Dest;
421 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000422 IMPLEMENT_BINARY_OPERATOR(^, UByte);
423 IMPLEMENT_BINARY_OPERATOR(^, SByte);
424 IMPLEMENT_BINARY_OPERATOR(^, UShort);
425 IMPLEMENT_BINARY_OPERATOR(^, Short);
426 IMPLEMENT_BINARY_OPERATOR(^, UInt);
427 IMPLEMENT_BINARY_OPERATOR(^, Int);
428 IMPLEMENT_BINARY_OPERATOR(^, ULong);
429 IMPLEMENT_BINARY_OPERATOR(^, Long);
430 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
431 default:
432 cout << "Unhandled type for Xor instruction: " << Ty << endl;
433 }
434 return Dest;
435}
436
437
Chris Lattner92101ac2001-08-23 17:05:04 +0000438#define IMPLEMENT_SETCC(OP, TY) \
439 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
440
Chris Lattner92101ac2001-08-23 17:05:04 +0000441static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
442 const Type *Ty, ExecutionContext &SF) {
443 GenericValue Dest;
444 switch (Ty->getPrimitiveID()) {
445 IMPLEMENT_SETCC(==, UByte);
446 IMPLEMENT_SETCC(==, SByte);
447 IMPLEMENT_SETCC(==, UShort);
448 IMPLEMENT_SETCC(==, Short);
449 IMPLEMENT_SETCC(==, UInt);
450 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000451 IMPLEMENT_SETCC(==, ULong);
452 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 IMPLEMENT_SETCC(==, Float);
454 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000455 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000456 default:
457 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
458 }
459 return Dest;
460}
461
462static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
463 const Type *Ty, ExecutionContext &SF) {
464 GenericValue Dest;
465 switch (Ty->getPrimitiveID()) {
466 IMPLEMENT_SETCC(!=, UByte);
467 IMPLEMENT_SETCC(!=, SByte);
468 IMPLEMENT_SETCC(!=, UShort);
469 IMPLEMENT_SETCC(!=, Short);
470 IMPLEMENT_SETCC(!=, UInt);
471 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000472 IMPLEMENT_SETCC(!=, ULong);
473 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 IMPLEMENT_SETCC(!=, Float);
475 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000476 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000477
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 default:
479 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
480 }
481 return Dest;
482}
483
484static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
485 const Type *Ty, ExecutionContext &SF) {
486 GenericValue Dest;
487 switch (Ty->getPrimitiveID()) {
488 IMPLEMENT_SETCC(<=, UByte);
489 IMPLEMENT_SETCC(<=, SByte);
490 IMPLEMENT_SETCC(<=, UShort);
491 IMPLEMENT_SETCC(<=, Short);
492 IMPLEMENT_SETCC(<=, UInt);
493 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000494 IMPLEMENT_SETCC(<=, ULong);
495 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 IMPLEMENT_SETCC(<=, Float);
497 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000498 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 default:
500 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
501 }
502 return Dest;
503}
504
505static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
506 const Type *Ty, ExecutionContext &SF) {
507 GenericValue Dest;
508 switch (Ty->getPrimitiveID()) {
509 IMPLEMENT_SETCC(>=, UByte);
510 IMPLEMENT_SETCC(>=, SByte);
511 IMPLEMENT_SETCC(>=, UShort);
512 IMPLEMENT_SETCC(>=, Short);
513 IMPLEMENT_SETCC(>=, UInt);
514 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000515 IMPLEMENT_SETCC(>=, ULong);
516 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 IMPLEMENT_SETCC(>=, Float);
518 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000519 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000520 default:
521 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
522 }
523 return Dest;
524}
525
526static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
527 const Type *Ty, ExecutionContext &SF) {
528 GenericValue Dest;
529 switch (Ty->getPrimitiveID()) {
530 IMPLEMENT_SETCC(<, UByte);
531 IMPLEMENT_SETCC(<, SByte);
532 IMPLEMENT_SETCC(<, UShort);
533 IMPLEMENT_SETCC(<, Short);
534 IMPLEMENT_SETCC(<, UInt);
535 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000536 IMPLEMENT_SETCC(<, ULong);
537 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 IMPLEMENT_SETCC(<, Float);
539 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000540 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000541 default:
542 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
543 }
544 return Dest;
545}
546
547static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
548 const Type *Ty, ExecutionContext &SF) {
549 GenericValue Dest;
550 switch (Ty->getPrimitiveID()) {
551 IMPLEMENT_SETCC(>, UByte);
552 IMPLEMENT_SETCC(>, SByte);
553 IMPLEMENT_SETCC(>, UShort);
554 IMPLEMENT_SETCC(>, Short);
555 IMPLEMENT_SETCC(>, UInt);
556 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000557 IMPLEMENT_SETCC(>, ULong);
558 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 IMPLEMENT_SETCC(>, Float);
560 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000561 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 default:
563 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
564 }
565 return Dest;
566}
567
568static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
569 const Type *Ty = I->getOperand(0)->getType();
570 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
571 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
572 GenericValue R; // Result
573
574 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000575 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
576 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
577 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
578 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
579 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000580 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
581 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000582 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000583 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
584 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
585 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
586 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
587 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
588 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
589 default:
590 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000591 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000592 }
593
594 SetValue(I, R, SF);
595}
596
Chris Lattner92101ac2001-08-23 17:05:04 +0000597//===----------------------------------------------------------------------===//
598// Terminator Instruction Implementations
599//===----------------------------------------------------------------------===//
600
Chris Lattnera95c6992001-11-12 16:28:48 +0000601static void PerformExitStuff() {
602#ifdef PROFILE_STRUCTURE_FIELDS
603 // Print out structure field accounting information...
604 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000605 CW << "Profile Field Access Counts:\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000606 map<const StructType *, vector<unsigned> >::iterator
607 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
608 for (; I != E; ++I) {
609 vector<unsigned> &OfC = I->second;
610 CW << " '" << (Value*)I->first << "'\t- Sum=";
611
612 unsigned Sum = 0;
613 for (unsigned i = 0; i < OfC.size(); ++i)
614 Sum += OfC[i];
615 CW << Sum << " - ";
616
617 for (unsigned i = 0; i < OfC.size(); ++i) {
618 if (i) CW << ", ";
619 CW << OfC[i];
620 }
621 CW << endl;
622 }
623 CW << endl;
Chris Lattner84efe092001-11-12 20:13:14 +0000624
625 CW << "Profile Field Access Percentages:\n";
626 cout.precision(3);
627 for (I = FieldAccessCounts.begin(); I != E; ++I) {
628 vector<unsigned> &OfC = I->second;
629 unsigned Sum = 0;
630 for (unsigned i = 0; i < OfC.size(); ++i)
631 Sum += OfC[i];
632
633 CW << " '" << (Value*)I->first << "'\t- ";
634 for (unsigned i = 0; i < OfC.size(); ++i) {
635 if (i) CW << ", ";
636 CW << double(OfC[i])/Sum;
637 }
638 CW << endl;
639 }
640 CW << endl;
641
Chris Lattnera95c6992001-11-12 16:28:48 +0000642 FieldAccessCounts.clear();
643 }
644#endif
645}
646
Chris Lattnere43db882001-10-27 04:15:57 +0000647void Interpreter::exitCalled(GenericValue GV) {
648 cout << "Program returned ";
649 print(Type::IntTy, GV);
650 cout << " via 'void exit(int)'\n";
651
652 ExitCode = GV.SByteVal;
653 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000654 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000655}
656
Chris Lattner92101ac2001-08-23 17:05:04 +0000657void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
658 const Type *RetTy = 0;
659 GenericValue Result;
660
661 // Save away the return value... (if we are not 'ret void')
662 if (I->getNumOperands()) {
663 RetTy = I->getReturnValue()->getType();
664 Result = getOperandValue(I->getReturnValue(), SF);
665 }
666
667 // Save previously executing meth
668 const Method *M = ECStack.back().CurMethod;
669
670 // Pop the current stack frame... this invalidates SF
671 ECStack.pop_back();
672
673 if (ECStack.empty()) { // Finished main. Put result into exit code...
674 if (RetTy) { // Nonvoid return type?
Chris Lattner5af0c482001-11-07 04:23:00 +0000675 CW << "Method " << M->getType() << " \"" << M->getName()
676 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000677 print(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000678 cout << endl;
679
680 if (RetTy->isIntegral())
681 ExitCode = Result.SByteVal; // Capture the exit code of the program
682 } else {
683 ExitCode = 0;
684 }
Chris Lattnere2409062001-11-12 16:19:45 +0000685
Chris Lattnera95c6992001-11-12 16:28:48 +0000686 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000687 return;
688 }
689
690 // If we have a previous stack frame, and we have a previous call, fill in
691 // the return value...
692 //
693 ExecutionContext &NewSF = ECStack.back();
694 if (NewSF.Caller) {
695 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
696 SetValue(NewSF.Caller, Result, NewSF);
697
698 NewSF.Caller = 0; // We returned from the call...
Chris Lattner365a76e2001-09-10 04:49:44 +0000699 } else {
700 // This must be a function that is executing because of a user 'call'
701 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000702 CW << "Method " << M->getType() << " \"" << M->getName()
703 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000704 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000705 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000706 }
707}
708
709void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
710 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
711 BasicBlock *Dest;
712
713 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
714 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000715 Value *Cond = I->getCondition();
716 GenericValue CondVal = getOperandValue(Cond, SF);
717 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000718 Dest = I->getSuccessor(1);
719 }
720 SF.CurBB = Dest; // Update CurBB to branch destination
721 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
722}
723
724//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000725// Memory Instruction Implementations
726//===----------------------------------------------------------------------===//
727
Chris Lattner86660982001-08-27 05:16:50 +0000728void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
729 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
730 unsigned NumElements = 1;
731
732 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattnerb00c5822001-10-02 03:41:24 +0000733 assert(isa<ArrayType>(Ty) && cast<const ArrayType>(Ty)->isUnsized() &&
Chris Lattner86660982001-08-27 05:16:50 +0000734 "Allocation inst with size operand for !unsized array type???");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000735 Ty = cast<const ArrayType>(Ty)->getElementType(); // Get the actual type...
Chris Lattner86660982001-08-27 05:16:50 +0000736
737 // Get the number of elements being allocated by the array...
738 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
739 NumElements = NumEl.UIntVal;
740 }
741
742 // Allocate enough memory to hold the type...
743 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000744 // FIXME: Don't use CALLOC, use a tainted malloc.
745 Result.PointerVal = (PointerTy)calloc(NumElements, TD.getTypeSize(Ty));
746 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000747 SetValue(I, Result, SF);
748
749 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000750 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000751 }
752}
753
754static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
755 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
756 GenericValue Value = getOperandValue(I->getOperand(0), SF);
757 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000758 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000759}
760
Chris Lattner95c3af52001-10-29 19:32:19 +0000761
762// getElementOffset - The workhorse for getelementptr, load and store. This
763// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
764// the pointer type specified by argument Arg.
765//
Chris Lattner782b9392001-11-26 18:18:18 +0000766static PointerTy getElementOffset(MemAccessInst *I, ExecutionContext &SF) {
767 assert(isa<PointerType>(I->getPointerOperand()->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000768 "Cannot getElementOffset of a nonpointer type!");
769
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000770 PointerTy Total = 0;
Chris Lattner95c3af52001-10-29 19:32:19 +0000771 const Type *Ty =
Chris Lattner782b9392001-11-26 18:18:18 +0000772 cast<PointerType>(I->getPointerOperand()->getType())->getValueType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000773
Chris Lattner782b9392001-11-26 18:18:18 +0000774 unsigned ArgOff = I->getFirstIndexOperandNumber();
Chris Lattner95c3af52001-10-29 19:32:19 +0000775 while (ArgOff < I->getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000776 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
777 const StructLayout *SLO = TD.getStructLayout(STy);
778
779 // Indicies must be ubyte constants...
780 const ConstPoolUInt *CPU = cast<ConstPoolUInt>(I->getOperand(ArgOff++));
781 assert(CPU->getType() == Type::UByteTy);
782 unsigned Index = CPU->getValue();
783
Chris Lattnere2409062001-11-12 16:19:45 +0000784#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000785 if (ProfileStructureFields) {
786 // Do accounting for this field...
787 vector<unsigned> &OfC = FieldAccessCounts[STy];
788 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
789 OfC[Index]++;
790 }
Chris Lattnere2409062001-11-12 16:19:45 +0000791#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000792
793 Total += SLO->MemberOffsets[Index];
794 Ty = STy->getElementTypes()[Index];
795 } else {
796 const ArrayType *AT = cast<ArrayType>(Ty);
Chris Lattnere2409062001-11-12 16:19:45 +0000797
Chris Lattner782b9392001-11-26 18:18:18 +0000798 // Get the index number for the array... which must be uint type...
799 assert(I->getOperand(ArgOff)->getType() == Type::UIntTy);
800 unsigned Idx = getOperandValue(I->getOperand(ArgOff++), SF).UIntVal;
801 if (AT->isSized() && Idx >= (unsigned)AT->getNumElements()) {
802 cerr << "Out of range memory access to element #" << Idx
803 << " of a " << AT->getNumElements() << " element array."
804 << " Subscript #" << (ArgOff-I->getFirstIndexOperandNumber())
805 << "\n";
806 // Get outta here!!!
807 siglongjmp(SignalRecoverBuffer, -1);
808 }
809
810 Ty = AT->getElementType();
811 unsigned Size = TD.getTypeSize(Ty);
812 Total += Size*Idx;
813 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000814 }
815
816 return Total;
817}
818
819static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000820 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000821 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000822
823 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000824 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000825 SetValue(I, Result, SF);
826}
827
Chris Lattner86660982001-08-27 05:16:50 +0000828static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000829 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000830 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000831 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000832 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000833
834 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000835 GenericValue Result;
836
837 switch (I->getType()->getPrimitiveID()) {
838 case Type::BoolTyID:
839 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000840 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000841 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000842 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000843 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000844 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000845 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000846 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
847 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
848 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
849 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000850 default:
851 cout << "Cannot load value of type " << I->getType() << "!\n";
852 }
853
854 SetValue(I, Result, SF);
855}
856
857static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000858 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000859 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000860 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000861
862 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000863 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000864
865 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
866 case Type::BoolTyID:
867 case Type::UByteTyID:
868 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
869 case Type::UShortTyID:
870 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
871 case Type::UIntTyID:
872 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000873 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000874 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
875 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000876 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
877 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000878 default:
879 cout << "Cannot store value of type " << I->getType() << "!\n";
880 }
881}
882
883
884//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000885// Miscellaneous Instruction Implementations
886//===----------------------------------------------------------------------===//
887
888void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
889 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000890 vector<GenericValue> ArgVals;
891 ArgVals.reserve(I->getNumOperands()-1);
892 for (unsigned i = 1; i < I->getNumOperands(); ++i)
893 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
894
Chris Lattner070cf5e2001-11-07 20:12:30 +0000895 // To handle indirect calls, we must get the pointer value from the argument
896 // and treat it as a method pointer.
897 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
898
899 callMethod((Method*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000900}
901
902static void executePHINode(PHINode *I, ExecutionContext &SF) {
903 BasicBlock *PrevBB = SF.PrevBB;
904 Value *IncomingValue = 0;
905
906 // Search for the value corresponding to this previous bb...
907 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
908 if (I->getIncomingBlock(--i) == PrevBB) {
909 IncomingValue = I->getIncomingValue(i);
910 break;
911 }
912 }
913 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
914
915 // Found the value, set as the result...
916 SetValue(I, getOperandValue(IncomingValue, SF), SF);
917}
918
Chris Lattner86660982001-08-27 05:16:50 +0000919#define IMPLEMENT_SHIFT(OP, TY) \
920 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
921
922static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
923 const Type *Ty = I->getOperand(0)->getType();
924 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
925 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
926 GenericValue Dest;
927
928 switch (Ty->getPrimitiveID()) {
929 IMPLEMENT_SHIFT(<<, UByte);
930 IMPLEMENT_SHIFT(<<, SByte);
931 IMPLEMENT_SHIFT(<<, UShort);
932 IMPLEMENT_SHIFT(<<, Short);
933 IMPLEMENT_SHIFT(<<, UInt);
934 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000935 IMPLEMENT_SHIFT(<<, ULong);
936 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000937 default:
938 cout << "Unhandled type for Shl instruction: " << Ty << endl;
939 }
940 SetValue(I, Dest, SF);
941}
942
943static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
944 const Type *Ty = I->getOperand(0)->getType();
945 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
946 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
947 GenericValue Dest;
948
949 switch (Ty->getPrimitiveID()) {
950 IMPLEMENT_SHIFT(>>, UByte);
951 IMPLEMENT_SHIFT(>>, SByte);
952 IMPLEMENT_SHIFT(>>, UShort);
953 IMPLEMENT_SHIFT(>>, Short);
954 IMPLEMENT_SHIFT(>>, UInt);
955 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000956 IMPLEMENT_SHIFT(>>, ULong);
957 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000958 default:
959 cout << "Unhandled type for Shr instruction: " << Ty << endl;
960 }
961 SetValue(I, Dest, SF);
962}
963
964#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000965 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000966
967#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
968 case Type::DESTTY##TyID: \
969 switch (SrcTy->getPrimitiveID()) { \
970 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
971 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
972 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
973 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
974 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000975 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
976 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000977 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000979
980#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
983
984#define IMPLEMENT_CAST_CASE_END() \
985 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
986 break; \
987 } \
988 break
989
990#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
991 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
992 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000993 IMPLEMENT_CAST_CASE_END()
994
995static void executeCastInst(CastInst *I, ExecutionContext &SF) {
996 const Type *Ty = I->getType();
997 const Type *SrcTy = I->getOperand(0)->getType();
998 GenericValue Src = getOperandValue(I->getOperand(0), SF);
999 GenericValue Dest;
1000
1001 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001002 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1003 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1004 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1005 IMPLEMENT_CAST_CASE(Short , ( signed char));
1006 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1007 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1008 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1009 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1010 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1011 IMPLEMENT_CAST_CASE(Float , (float));
1012 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001013 default:
1014 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
1015 }
1016 SetValue(I, Dest, SF);
1017}
Chris Lattner92101ac2001-08-23 17:05:04 +00001018
1019
1020
1021
1022//===----------------------------------------------------------------------===//
1023// Dispatch and Execution Code
1024//===----------------------------------------------------------------------===//
1025
1026MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
1027 // Assign slot numbers to the method arguments...
1028 const Method::ArgumentListType &ArgList = M->getArgumentList();
1029 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
1030 AE = ArgList.end(); AI != AE; ++AI) {
1031 MethodArgument *MA = *AI;
1032 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
1033 }
1034
1035 // Iterate over all of the instructions...
1036 unsigned InstNum = 0;
1037 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
1038 MI != ME; ++MI) {
1039 Instruction *I = *MI; // For each instruction...
1040 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
1041 }
1042}
1043
1044unsigned MethodInfo::getValueSlot(const Value *V) {
1045 unsigned Plane = V->getType()->getUniqueID();
1046 if (Plane >= NumPlaneElements.size())
1047 NumPlaneElements.resize(Plane+1, 0);
1048 return NumPlaneElements[Plane]++;
1049}
1050
1051
Chris Lattner92101ac2001-08-23 17:05:04 +00001052//===----------------------------------------------------------------------===//
1053// callMethod - Execute the specified method...
1054//
Chris Lattner365a76e2001-09-10 04:49:44 +00001055void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
1056 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1057 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1058 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001059 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001060 GenericValue Result = callExternalMethod(M, ArgVals);
1061 const Type *RetTy = M->getReturnType();
1062
1063 // Copy the result back into the result variable if we are not returning
1064 // void.
1065 if (RetTy != Type::VoidTy) {
1066 if (!ECStack.empty() && ECStack.back().Caller) {
1067 ExecutionContext &SF = ECStack.back();
1068 CallInst *Caller = SF.Caller;
1069 SetValue(SF.Caller, Result, SF);
1070
1071 SF.Caller = 0; // We returned from the call...
1072 } else {
1073 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +00001074 CW << "Method " << M->getType() << " \"" << M->getName()
1075 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001076 print(RetTy, Result);
1077 cout << endl;
1078
1079 if (RetTy->isIntegral())
1080 ExitCode = Result.SByteVal; // Capture the exit code of the program
1081 }
1082 }
1083
Chris Lattner92101ac2001-08-23 17:05:04 +00001084 return;
1085 }
1086
1087 // Process the method, assigning instruction numbers to the instructions in
1088 // the method. Also calculate the number of values for each type slot active.
1089 //
1090 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001091 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001092
Chris Lattner92101ac2001-08-23 17:05:04 +00001093 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1094 StackFrame.CurMethod = M;
1095 StackFrame.CurBB = M->front();
1096 StackFrame.CurInst = StackFrame.CurBB->begin();
1097 StackFrame.MethInfo = MethInfo;
1098
1099 // Initialize the values to nothing...
1100 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001101 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001102 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1103
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001104 // Taint the initial values of stuff
1105 memset(&StackFrame.Values[i][0], 42,
1106 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1107 }
1108
Chris Lattner92101ac2001-08-23 17:05:04 +00001109 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1110
Chris Lattner92101ac2001-08-23 17:05:04 +00001111
Chris Lattner365a76e2001-09-10 04:49:44 +00001112 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001113 assert(ArgVals.size() == M->getArgumentList().size() &&
1114 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001115 unsigned i = 0;
1116 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1117 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1118 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001119 }
1120}
1121
1122// executeInstruction - Interpret a single instruction, increment the "PC", and
1123// return true if the next instruction is a breakpoint...
1124//
1125bool Interpreter::executeInstruction() {
1126 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1127
1128 ExecutionContext &SF = ECStack.back(); // Current stack frame
1129 Instruction *I = *SF.CurInst++; // Increment before execute
1130
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001131 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001132 CW << "Run:" << I;
1133
1134 // Set a sigsetjmp buffer so that we can recover if an error happens during
1135 // instruction execution...
1136 //
1137 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1138 --SF.CurInst; // Back up to erroring instruction
Chris Lattner782b9392001-11-26 18:18:18 +00001139 if (SigNo != SIGINT && SigNo != -1) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001140 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001141 printStackTrace();
Chris Lattner782b9392001-11-26 18:18:18 +00001142 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001143 cout << "CTRL-C Detected, execution halted.\n";
1144 }
1145 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001146 return true;
1147 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001148
Chris Lattner461f02f2001-11-07 05:31:27 +00001149 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001150 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001151 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001152 } else {
1153 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001154 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001155 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1156 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001157 // Memory Instructions
1158 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001159 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1160 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1161 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1162 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001163 case Instruction::GetElementPtr:
1164 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001165
1166 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001167 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1168 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1169 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1170 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1171 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001172 default:
1173 cout << "Don't know how to execute this instruction!\n-->" << I;
1174 }
1175 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001176 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001177
1178 // Reset the current frame location to the top of stack
1179 CurFrame = ECStack.size()-1;
1180
1181 if (CurFrame == -1) return false; // No breakpoint if no code
1182
1183 // Return true if there is a breakpoint annotation on the instruction...
1184 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1185}
1186
1187void Interpreter::stepInstruction() { // Do the 'step' command
1188 if (ECStack.empty()) {
1189 cout << "Error: no program running, cannot step!\n";
1190 return;
1191 }
1192
1193 // Run an instruction...
1194 executeInstruction();
1195
1196 // Print the next instruction to execute...
1197 printCurrentInstruction();
1198}
1199
1200// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001201void Interpreter::nextInstruction() { // Do the 'next' command
1202 if (ECStack.empty()) {
1203 cout << "Error: no program running, cannot 'next'!\n";
1204 return;
1205 }
1206
1207 // If this is a call instruction, step over the call instruction...
1208 // TODO: ICALL, CALL WITH, ...
1209 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001210 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001211 // Step into the function...
1212 if (executeInstruction()) {
1213 // Hit a breakpoint, print current instruction, then return to user...
1214 cout << "Breakpoint hit!\n";
1215 printCurrentInstruction();
1216 return;
1217 }
1218
Chris Lattnera74a6b52001-10-29 14:08:33 +00001219 // If we we able to step into the function, finish it now. We might not be
1220 // able the step into a function, if it's external for example.
1221 if (ECStack.size() != StackSize)
1222 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001223 else
1224 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001225
Chris Lattner92101ac2001-08-23 17:05:04 +00001226 } else {
1227 // Normal instruction, just step...
1228 stepInstruction();
1229 }
1230}
1231
1232void Interpreter::run() {
1233 if (ECStack.empty()) {
1234 cout << "Error: no program running, cannot run!\n";
1235 return;
1236 }
1237
1238 bool HitBreakpoint = false;
1239 while (!ECStack.empty() && !HitBreakpoint) {
1240 // Run an instruction...
1241 HitBreakpoint = executeInstruction();
1242 }
1243
1244 if (HitBreakpoint) {
1245 cout << "Breakpoint hit!\n";
1246 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001247 // Print the next instruction to execute...
1248 printCurrentInstruction();
1249}
1250
1251void Interpreter::finish() {
1252 if (ECStack.empty()) {
1253 cout << "Error: no program running, cannot run!\n";
1254 return;
1255 }
1256
1257 unsigned StackSize = ECStack.size();
1258 bool HitBreakpoint = false;
1259 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1260 // Run an instruction...
1261 HitBreakpoint = executeInstruction();
1262 }
1263
1264 if (HitBreakpoint) {
1265 cout << "Breakpoint hit!\n";
1266 }
1267
1268 // Print the next instruction to execute...
1269 printCurrentInstruction();
1270}
1271
1272
1273
1274// printCurrentInstruction - Print out the instruction that the virtual PC is
1275// at, or fail silently if no program is running.
1276//
1277void Interpreter::printCurrentInstruction() {
1278 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001279 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1280 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1281
Chris Lattner92101ac2001-08-23 17:05:04 +00001282 Instruction *I = *ECStack.back().CurInst;
1283 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1284 assert(IN && "Instruction has no numbering annotation!");
1285 cout << "#" << IN->InstNum << I;
1286 }
1287}
1288
1289void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001290 switch (Ty->getPrimitiveID()) {
1291 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1292 case Type::SByteTyID: cout << V.SByteVal; break;
1293 case Type::UByteTyID: cout << V.UByteVal; break;
1294 case Type::ShortTyID: cout << V.ShortVal; break;
1295 case Type::UShortTyID: cout << V.UShortVal; break;
1296 case Type::IntTyID: cout << V.IntVal; break;
1297 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001298 case Type::LongTyID: cout << V.LongVal; break;
1299 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001300 case Type::FloatTyID: cout << V.FloatVal; break;
1301 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001302 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001303 default:
1304 cout << "- Don't know how to print value of this type!";
1305 break;
1306 }
1307}
1308
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001309void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001310 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001311 printValue(Ty, V);
1312}
1313
1314void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001315 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1316 if (!PickedVal) return;
1317
Chris Lattner9636a912001-10-01 16:18:37 +00001318 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001319 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001320 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1321 CW << "type %" << Name << " = " << Ty->getDescription() << endl;
1322 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1323 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001324 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001325 print(PickedVal->getType(),
1326 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001327 cout << endl;
1328 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001329}
1330
Chris Lattner86660982001-08-27 05:16:50 +00001331void Interpreter::infoValue(const string &Name) {
1332 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1333 if (!PickedVal) return;
1334
1335 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001336 print(PickedVal->getType(),
1337 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001338 cout << endl;
1339 printOperandInfo(PickedVal, ECStack[CurFrame]);
1340}
1341
Chris Lattner461f02f2001-11-07 05:31:27 +00001342// printStackFrame - Print information about the specified stack frame, or -1
1343// for the default one.
1344//
1345void Interpreter::printStackFrame(int FrameNo = -1) {
1346 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001347 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001348 const Type *RetTy = Meth->getReturnType();
1349
1350 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1351 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001352
1353 Method::ArgumentListType &Args = Meth->getArgumentList();
1354 for (unsigned i = 0; i < Args.size(); ++i) {
1355 if (i != 0) cout << ", ";
1356 CW << (Value*)Args[i] << "=";
1357
1358 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001359 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001360
1361 cout << ")" << endl;
1362 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001363}
Chris Lattner461f02f2001-11-07 05:31:27 +00001364