blob: c48ec92b3e99619e833978ed7d625a533751b0f8 [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"
Chris Lattner7061dc52001-12-03 18:02:31 +00009#include "llvm/iPHINode.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000010#include "llvm/iOther.h"
11#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000012#include "llvm/iMemory.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000013#include "llvm/Type.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000014#include "llvm/ConstantVals.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "llvm/Assembly/Writer.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 Lattnerf23eb852001-12-14 16:49:29 +000018#include "Support/CommandLine.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000019#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000020#include <signal.h>
21#include <setjmp.h>
Chris Lattner2e42d3a2001-10-15 05:51:48 +000022
Chris Lattnerf23eb852001-12-14 16:49:29 +000023cl::Flag QuietMode ("quiet" , "Do not emit any non-program output");
24cl::Alias QuietModeA("q" , "Alias for -quiet", cl::NoFlags, QuietMode);
25
Chris Lattnere9bb2df2001-12-03 22:26:30 +000026
Chris Lattner2e42d3a2001-10-15 05:51:48 +000027// Create a TargetData structure to handle memory addressing and size/alignment
28// computations
29//
30static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000031CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000032
33
Chris Lattnere2409062001-11-12 16:19:45 +000034#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattnere2409062001-11-12 16:19:45 +000035static cl::Flag ProfileStructureFields("profilestructfields",
36 "Profile Structure Field Accesses");
37#include <map>
38static map<const StructType *, vector<unsigned> > FieldAccessCounts;
39#endif
40
Chris Lattner5af0c482001-11-07 04:23:00 +000041sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000042static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000043
44extern "C" {
45static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000046 if (InInstruction)
47 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000048}
49}
50
51static void initializeSignalHandlers() {
52 struct sigaction Action;
53 Action.sa_handler = SigHandler;
54 Action.sa_flags = SA_SIGINFO;
55 sigemptyset(&Action.sa_mask);
56 sigaction(SIGSEGV, &Action, 0);
57 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000058 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000059 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000060}
61
Chris Lattner2e42d3a2001-10-15 05:51:48 +000062
63//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000064// Value Manipulation code
65//===----------------------------------------------------------------------===//
66
67static unsigned getOperandSlot(Value *V) {
68 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
69 assert(SN && "Operand does not have a slot number annotation!");
70 return SN->SlotNum;
71}
72
73#define GET_CONST_VAL(TY, CLASS) \
74 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
75
76static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000077 if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000078 GenericValue Result;
79 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000080 GET_CONST_VAL(Bool , ConstantBool);
81 GET_CONST_VAL(UByte , ConstantUInt);
82 GET_CONST_VAL(SByte , ConstantSInt);
83 GET_CONST_VAL(UShort , ConstantUInt);
84 GET_CONST_VAL(Short , ConstantSInt);
85 GET_CONST_VAL(UInt , ConstantUInt);
86 GET_CONST_VAL(Int , ConstantSInt);
87 GET_CONST_VAL(ULong , ConstantUInt);
88 GET_CONST_VAL(Long , ConstantSInt);
89 GET_CONST_VAL(Float , ConstantFP);
90 GET_CONST_VAL(Double , ConstantFP);
Chris Lattner39bb5b42001-10-15 13:25:40 +000091 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000092 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000093 Result.PointerVal = 0;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000094 } else if (ConstantPointerRef *CPR =dyn_cast<ConstantPointerRef>(CPV)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000095 assert(0 && "Not implemented!");
96 } else {
97 assert(0 && "Unknown constant pointer type!");
98 }
99 break;
100 default:
101 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
102 }
103 return Result;
104 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
105 GlobalAddress *Address =
106 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
107 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000108 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000109 return Result;
110 } else {
111 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000112 unsigned OpSlot = getOperandSlot(V);
113 assert(TyP < SF.Values.size() &&
114 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000115 return SF.Values[TyP][getOperandSlot(V)];
116 }
117}
118
119static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000120 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000121 cout << "Constant Pool Value\n";
122 } else if (isa<GlobalValue>(V)) {
123 cout << "Global Value\n";
124 } else {
125 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
126 unsigned Slot = getOperandSlot(V);
127 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000128 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
129 << " Contents=0x";
130
131 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
132 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
133 unsigned char Cur = Buf[i];
134 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
135 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
136 }
137 cout << endl;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000138 }
139}
140
141
142
143static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
144 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
145
146 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
147 SF.Values[TyP][getOperandSlot(V)] = Val;
148}
149
150
151//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000152// Annotation Wrangling code
153//===----------------------------------------------------------------------===//
154
155void Interpreter::initializeExecutionEngine() {
156 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
157 &MethodInfo::Create);
158 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
159 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000160 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000161}
162
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000163// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000164// specified memory location...
165//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000166static void InitializeMemory(Constant *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000167#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
168 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000169 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000170 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000171 } return
172
173 switch (Init->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000174 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
175 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
176 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
177 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
178 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
179 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
180 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
181 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
182 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
183 INITIALIZE_MEMORY(Float , ConstantFP , float);
184 INITIALIZE_MEMORY(Double , ConstantFP , double);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000185#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000186
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000187 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000188 ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000189 const vector<Use> &Val = CPA->getValues();
190 unsigned ElementSize =
191 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
192 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000193 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000194 return;
195 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000196
197 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000198 ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000199 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
200 const vector<Use> &Val = CPS->getValues();
201 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000202 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000203 Addr+SL->MemberOffsets[i]);
204 return;
205 }
206
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000207 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000208 if (isa<ConstantPointerNull>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000209 *(void**)Addr = 0;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000210 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000211 GlobalAddress *Address =
212 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
213 *(void**)Addr = (GenericValue*)Address->Ptr;
214 } else {
215 assert(0 && "Unknown Constant pointer type!");
216 }
217 return;
218
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000219 default:
Chris Lattner461f02f2001-11-07 05:31:27 +0000220 CW << "Bad Type: " << Init->getType() << endl;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000221 assert(0 && "Unknown constant type to initialize memory with!");
222 }
223}
224
225Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
226 assert(AID == GlobalAddressAID);
227
228 // This annotation will only be created on GlobalValue objects...
229 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
230
231 if (isa<Method>(GVal)) {
232 // The GlobalAddress object for a method is just a pointer to method itself.
233 // Don't delete it when the annotation is gone though!
234 return new GlobalAddress(GVal, false);
235 }
236
237 // Handle the case of a global variable...
238 assert(isa<GlobalVariable>(GVal) &&
239 "Global value found that isn't a method or global variable!");
240 GlobalVariable *GV = cast<GlobalVariable>(GVal);
241
242 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000243 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000244
245 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000246 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000247 assert(Addr != 0 && "Null pointer returned by malloc!");
248
249 // Initialize the memory if there is an initializer...
250 if (GV->hasInitializer())
251 InitializeMemory(GV->getInitializer(), (char*)Addr);
252
253 return new GlobalAddress(Addr, true); // Simply invoke the ctor
254}
255
Chris Lattner92101ac2001-08-23 17:05:04 +0000256
257//===----------------------------------------------------------------------===//
258// Binary Instruction Implementations
259//===----------------------------------------------------------------------===//
260
261#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
262 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
263
264static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
265 const Type *Ty, ExecutionContext &SF) {
266 GenericValue Dest;
267 switch (Ty->getPrimitiveID()) {
268 IMPLEMENT_BINARY_OPERATOR(+, UByte);
269 IMPLEMENT_BINARY_OPERATOR(+, SByte);
270 IMPLEMENT_BINARY_OPERATOR(+, UShort);
271 IMPLEMENT_BINARY_OPERATOR(+, Short);
272 IMPLEMENT_BINARY_OPERATOR(+, UInt);
273 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000274 IMPLEMENT_BINARY_OPERATOR(+, ULong);
275 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000276 IMPLEMENT_BINARY_OPERATOR(+, Float);
277 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000278 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000279 default:
280 cout << "Unhandled type for Add instruction: " << Ty << endl;
281 }
282 return Dest;
283}
284
285static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
286 const Type *Ty, ExecutionContext &SF) {
287 GenericValue Dest;
288 switch (Ty->getPrimitiveID()) {
289 IMPLEMENT_BINARY_OPERATOR(-, UByte);
290 IMPLEMENT_BINARY_OPERATOR(-, SByte);
291 IMPLEMENT_BINARY_OPERATOR(-, UShort);
292 IMPLEMENT_BINARY_OPERATOR(-, Short);
293 IMPLEMENT_BINARY_OPERATOR(-, UInt);
294 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000295 IMPLEMENT_BINARY_OPERATOR(-, ULong);
296 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000297 IMPLEMENT_BINARY_OPERATOR(-, Float);
298 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000299 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000300 default:
301 cout << "Unhandled type for Sub instruction: " << Ty << endl;
302 }
303 return Dest;
304}
305
Chris Lattnerc2593162001-10-27 08:28:11 +0000306static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
307 const Type *Ty, ExecutionContext &SF) {
308 GenericValue Dest;
309 switch (Ty->getPrimitiveID()) {
310 IMPLEMENT_BINARY_OPERATOR(*, UByte);
311 IMPLEMENT_BINARY_OPERATOR(*, SByte);
312 IMPLEMENT_BINARY_OPERATOR(*, UShort);
313 IMPLEMENT_BINARY_OPERATOR(*, Short);
314 IMPLEMENT_BINARY_OPERATOR(*, UInt);
315 IMPLEMENT_BINARY_OPERATOR(*, Int);
316 IMPLEMENT_BINARY_OPERATOR(*, ULong);
317 IMPLEMENT_BINARY_OPERATOR(*, Long);
318 IMPLEMENT_BINARY_OPERATOR(*, Float);
319 IMPLEMENT_BINARY_OPERATOR(*, Double);
320 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
321 default:
322 cout << "Unhandled type for Mul instruction: " << Ty << endl;
323 }
324 return Dest;
325}
326
327static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
328 const Type *Ty, ExecutionContext &SF) {
329 GenericValue Dest;
330 switch (Ty->getPrimitiveID()) {
331 IMPLEMENT_BINARY_OPERATOR(/, UByte);
332 IMPLEMENT_BINARY_OPERATOR(/, SByte);
333 IMPLEMENT_BINARY_OPERATOR(/, UShort);
334 IMPLEMENT_BINARY_OPERATOR(/, Short);
335 IMPLEMENT_BINARY_OPERATOR(/, UInt);
336 IMPLEMENT_BINARY_OPERATOR(/, Int);
337 IMPLEMENT_BINARY_OPERATOR(/, ULong);
338 IMPLEMENT_BINARY_OPERATOR(/, Long);
339 IMPLEMENT_BINARY_OPERATOR(/, Float);
340 IMPLEMENT_BINARY_OPERATOR(/, Double);
341 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
342 default:
Chris Lattnerbb76f022001-10-30 20:27:31 +0000343 cout << "Unhandled type for Div instruction: " << Ty << endl;
344 }
345 return Dest;
346}
347
348static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
349 const Type *Ty, ExecutionContext &SF) {
350 GenericValue Dest;
351 switch (Ty->getPrimitiveID()) {
352 IMPLEMENT_BINARY_OPERATOR(%, UByte);
353 IMPLEMENT_BINARY_OPERATOR(%, SByte);
354 IMPLEMENT_BINARY_OPERATOR(%, UShort);
355 IMPLEMENT_BINARY_OPERATOR(%, Short);
356 IMPLEMENT_BINARY_OPERATOR(%, UInt);
357 IMPLEMENT_BINARY_OPERATOR(%, Int);
358 IMPLEMENT_BINARY_OPERATOR(%, ULong);
359 IMPLEMENT_BINARY_OPERATOR(%, Long);
360 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
361 case Type::FloatTyID:
362 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
363 break;
364 case Type::DoubleTyID:
365 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
366 break;
367 default:
368 cout << "Unhandled type for Rem instruction: " << Ty << endl;
Chris Lattnerc2593162001-10-27 08:28:11 +0000369 }
370 return Dest;
371}
372
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000373static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000374 const Type *Ty, ExecutionContext &SF) {
375 GenericValue Dest;
376 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000377 IMPLEMENT_BINARY_OPERATOR(&, UByte);
378 IMPLEMENT_BINARY_OPERATOR(&, SByte);
379 IMPLEMENT_BINARY_OPERATOR(&, UShort);
380 IMPLEMENT_BINARY_OPERATOR(&, Short);
381 IMPLEMENT_BINARY_OPERATOR(&, UInt);
382 IMPLEMENT_BINARY_OPERATOR(&, Int);
383 IMPLEMENT_BINARY_OPERATOR(&, ULong);
384 IMPLEMENT_BINARY_OPERATOR(&, Long);
385 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
386 default:
387 cout << "Unhandled type for And instruction: " << Ty << endl;
388 }
389 return Dest;
390}
391
392
393static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
394 const Type *Ty, ExecutionContext &SF) {
395 GenericValue Dest;
396 switch (Ty->getPrimitiveID()) {
397 IMPLEMENT_BINARY_OPERATOR(|, UByte);
398 IMPLEMENT_BINARY_OPERATOR(|, SByte);
399 IMPLEMENT_BINARY_OPERATOR(|, UShort);
400 IMPLEMENT_BINARY_OPERATOR(|, Short);
401 IMPLEMENT_BINARY_OPERATOR(|, UInt);
402 IMPLEMENT_BINARY_OPERATOR(|, Int);
403 IMPLEMENT_BINARY_OPERATOR(|, ULong);
404 IMPLEMENT_BINARY_OPERATOR(|, Long);
405 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
406 default:
407 cout << "Unhandled type for Or instruction: " << Ty << endl;
408 }
409 return Dest;
410}
411
412
413static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
414 const Type *Ty, ExecutionContext &SF) {
415 GenericValue Dest;
416 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000417 IMPLEMENT_BINARY_OPERATOR(^, UByte);
418 IMPLEMENT_BINARY_OPERATOR(^, SByte);
419 IMPLEMENT_BINARY_OPERATOR(^, UShort);
420 IMPLEMENT_BINARY_OPERATOR(^, Short);
421 IMPLEMENT_BINARY_OPERATOR(^, UInt);
422 IMPLEMENT_BINARY_OPERATOR(^, Int);
423 IMPLEMENT_BINARY_OPERATOR(^, ULong);
424 IMPLEMENT_BINARY_OPERATOR(^, Long);
425 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
426 default:
427 cout << "Unhandled type for Xor instruction: " << Ty << endl;
428 }
429 return Dest;
430}
431
432
Chris Lattner92101ac2001-08-23 17:05:04 +0000433#define IMPLEMENT_SETCC(OP, TY) \
434 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
435
Chris Lattner92101ac2001-08-23 17:05:04 +0000436static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
437 const Type *Ty, ExecutionContext &SF) {
438 GenericValue Dest;
439 switch (Ty->getPrimitiveID()) {
440 IMPLEMENT_SETCC(==, UByte);
441 IMPLEMENT_SETCC(==, SByte);
442 IMPLEMENT_SETCC(==, UShort);
443 IMPLEMENT_SETCC(==, Short);
444 IMPLEMENT_SETCC(==, UInt);
445 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000446 IMPLEMENT_SETCC(==, ULong);
447 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 IMPLEMENT_SETCC(==, Float);
449 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000450 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 default:
452 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
453 }
454 return Dest;
455}
456
457static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
458 const Type *Ty, ExecutionContext &SF) {
459 GenericValue Dest;
460 switch (Ty->getPrimitiveID()) {
461 IMPLEMENT_SETCC(!=, UByte);
462 IMPLEMENT_SETCC(!=, SByte);
463 IMPLEMENT_SETCC(!=, UShort);
464 IMPLEMENT_SETCC(!=, Short);
465 IMPLEMENT_SETCC(!=, UInt);
466 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000467 IMPLEMENT_SETCC(!=, ULong);
468 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 IMPLEMENT_SETCC(!=, Float);
470 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000471 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000472
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 default:
474 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
475 }
476 return Dest;
477}
478
479static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
480 const Type *Ty, ExecutionContext &SF) {
481 GenericValue Dest;
482 switch (Ty->getPrimitiveID()) {
483 IMPLEMENT_SETCC(<=, UByte);
484 IMPLEMENT_SETCC(<=, SByte);
485 IMPLEMENT_SETCC(<=, UShort);
486 IMPLEMENT_SETCC(<=, Short);
487 IMPLEMENT_SETCC(<=, UInt);
488 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000489 IMPLEMENT_SETCC(<=, ULong);
490 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000491 IMPLEMENT_SETCC(<=, Float);
492 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000493 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000494 default:
495 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
496 }
497 return Dest;
498}
499
500static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
501 const Type *Ty, ExecutionContext &SF) {
502 GenericValue Dest;
503 switch (Ty->getPrimitiveID()) {
504 IMPLEMENT_SETCC(>=, UByte);
505 IMPLEMENT_SETCC(>=, SByte);
506 IMPLEMENT_SETCC(>=, UShort);
507 IMPLEMENT_SETCC(>=, Short);
508 IMPLEMENT_SETCC(>=, UInt);
509 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000510 IMPLEMENT_SETCC(>=, ULong);
511 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000512 IMPLEMENT_SETCC(>=, Float);
513 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000514 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000515 default:
516 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
517 }
518 return Dest;
519}
520
521static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
522 const Type *Ty, ExecutionContext &SF) {
523 GenericValue Dest;
524 switch (Ty->getPrimitiveID()) {
525 IMPLEMENT_SETCC(<, UByte);
526 IMPLEMENT_SETCC(<, SByte);
527 IMPLEMENT_SETCC(<, UShort);
528 IMPLEMENT_SETCC(<, Short);
529 IMPLEMENT_SETCC(<, UInt);
530 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000531 IMPLEMENT_SETCC(<, ULong);
532 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 IMPLEMENT_SETCC(<, Float);
534 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000535 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000536 default:
537 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
538 }
539 return Dest;
540}
541
542static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
543 const Type *Ty, ExecutionContext &SF) {
544 GenericValue Dest;
545 switch (Ty->getPrimitiveID()) {
546 IMPLEMENT_SETCC(>, UByte);
547 IMPLEMENT_SETCC(>, SByte);
548 IMPLEMENT_SETCC(>, UShort);
549 IMPLEMENT_SETCC(>, Short);
550 IMPLEMENT_SETCC(>, UInt);
551 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000552 IMPLEMENT_SETCC(>, ULong);
553 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000554 IMPLEMENT_SETCC(>, Float);
555 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000556 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000557 default:
558 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
559 }
560 return Dest;
561}
562
563static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
564 const Type *Ty = I->getOperand(0)->getType();
565 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
566 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
567 GenericValue R; // Result
568
569 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000570 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
571 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
572 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
573 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
574 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000575 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
576 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000577 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000578 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
579 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
580 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
581 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
582 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
583 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
584 default:
585 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000586 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000587 }
588
589 SetValue(I, R, SF);
590}
591
Chris Lattner92101ac2001-08-23 17:05:04 +0000592//===----------------------------------------------------------------------===//
593// Terminator Instruction Implementations
594//===----------------------------------------------------------------------===//
595
Chris Lattnera95c6992001-11-12 16:28:48 +0000596static void PerformExitStuff() {
597#ifdef PROFILE_STRUCTURE_FIELDS
598 // Print out structure field accounting information...
599 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000600 CW << "Profile Field Access Counts:\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000601 map<const StructType *, vector<unsigned> >::iterator
602 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
603 for (; I != E; ++I) {
604 vector<unsigned> &OfC = I->second;
605 CW << " '" << (Value*)I->first << "'\t- Sum=";
606
607 unsigned Sum = 0;
608 for (unsigned i = 0; i < OfC.size(); ++i)
609 Sum += OfC[i];
610 CW << Sum << " - ";
611
612 for (unsigned i = 0; i < OfC.size(); ++i) {
613 if (i) CW << ", ";
614 CW << OfC[i];
615 }
616 CW << endl;
617 }
618 CW << endl;
Chris Lattner84efe092001-11-12 20:13:14 +0000619
620 CW << "Profile Field Access Percentages:\n";
621 cout.precision(3);
622 for (I = FieldAccessCounts.begin(); I != E; ++I) {
623 vector<unsigned> &OfC = I->second;
624 unsigned Sum = 0;
625 for (unsigned i = 0; i < OfC.size(); ++i)
626 Sum += OfC[i];
627
628 CW << " '" << (Value*)I->first << "'\t- ";
629 for (unsigned i = 0; i < OfC.size(); ++i) {
630 if (i) CW << ", ";
631 CW << double(OfC[i])/Sum;
632 }
633 CW << endl;
634 }
635 CW << endl;
636
Chris Lattnera95c6992001-11-12 16:28:48 +0000637 FieldAccessCounts.clear();
638 }
639#endif
640}
641
Chris Lattnere43db882001-10-27 04:15:57 +0000642void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000643 if (!QuietMode) {
644 cout << "Program returned ";
645 print(Type::IntTy, GV);
646 cout << " via 'void exit(int)'\n";
647 }
Chris Lattnere43db882001-10-27 04:15:57 +0000648
649 ExitCode = GV.SByteVal;
650 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000651 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000652}
653
Chris Lattner92101ac2001-08-23 17:05:04 +0000654void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
655 const Type *RetTy = 0;
656 GenericValue Result;
657
658 // Save away the return value... (if we are not 'ret void')
659 if (I->getNumOperands()) {
660 RetTy = I->getReturnValue()->getType();
661 Result = getOperandValue(I->getReturnValue(), SF);
662 }
663
664 // Save previously executing meth
665 const Method *M = ECStack.back().CurMethod;
666
667 // Pop the current stack frame... this invalidates SF
668 ECStack.pop_back();
669
670 if (ECStack.empty()) { // Finished main. Put result into exit code...
671 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000672 if (!QuietMode) {
673 CW << "Method " << M->getType() << " \"" << M->getName()
674 << "\" returned ";
675 print(RetTy, Result);
676 cout << endl;
677 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000678
679 if (RetTy->isIntegral())
680 ExitCode = Result.SByteVal; // Capture the exit code of the program
681 } else {
682 ExitCode = 0;
683 }
Chris Lattnere2409062001-11-12 16:19:45 +0000684
Chris Lattnera95c6992001-11-12 16:28:48 +0000685 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000686 return;
687 }
688
689 // If we have a previous stack frame, and we have a previous call, fill in
690 // the return value...
691 //
692 ExecutionContext &NewSF = ECStack.back();
693 if (NewSF.Caller) {
694 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
695 SetValue(NewSF.Caller, Result, NewSF);
696
697 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000698 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000699 // This must be a function that is executing because of a user 'call'
700 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000701 CW << "Method " << M->getType() << " \"" << M->getName()
702 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000703 print(RetTy, Result);
Chris Lattner365a76e2001-09-10 04:49:44 +0000704 cout << endl;
Chris Lattner92101ac2001-08-23 17:05:04 +0000705 }
706}
707
708void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
709 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
710 BasicBlock *Dest;
711
712 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
713 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000714 Value *Cond = I->getCondition();
715 GenericValue CondVal = getOperandValue(Cond, SF);
716 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000717 Dest = I->getSuccessor(1);
718 }
719 SF.CurBB = Dest; // Update CurBB to branch destination
720 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
721}
722
723//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000724// Memory Instruction Implementations
725//===----------------------------------------------------------------------===//
726
Chris Lattner86660982001-08-27 05:16:50 +0000727void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
Chris Lattner7a176752001-12-04 00:03:30 +0000728 const Type *Ty = I->getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000729 unsigned NumElements = 1;
730
Chris Lattnerf23eb852001-12-14 16:49:29 +0000731 // FIXME: Malloc/Alloca should always have an argument!
Chris Lattner86660982001-08-27 05:16:50 +0000732 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattner86660982001-08-27 05:16:50 +0000733 // Get the number of elements being allocated by the array...
734 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
735 NumElements = NumEl.UIntVal;
736 }
737
738 // Allocate enough memory to hold the type...
739 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000740 // FIXME: Don't use CALLOC, use a tainted malloc.
741 Result.PointerVal = (PointerTy)calloc(NumElements, TD.getTypeSize(Ty));
742 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000743 SetValue(I, Result, SF);
744
745 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000746 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000747 }
748}
749
750static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
751 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
752 GenericValue Value = getOperandValue(I->getOperand(0), SF);
753 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000754 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000755}
756
Chris Lattner95c3af52001-10-29 19:32:19 +0000757
758// getElementOffset - The workhorse for getelementptr, load and store. This
759// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
760// the pointer type specified by argument Arg.
761//
Chris Lattner782b9392001-11-26 18:18:18 +0000762static PointerTy getElementOffset(MemAccessInst *I, ExecutionContext &SF) {
763 assert(isa<PointerType>(I->getPointerOperand()->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000764 "Cannot getElementOffset of a nonpointer type!");
765
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000766 PointerTy Total = 0;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000767 const Type *Ty = I->getPointerOperand()->getType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000768
Chris Lattner782b9392001-11-26 18:18:18 +0000769 unsigned ArgOff = I->getFirstIndexOperandNumber();
Chris Lattner95c3af52001-10-29 19:32:19 +0000770 while (ArgOff < I->getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000771 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
772 const StructLayout *SLO = TD.getStructLayout(STy);
773
774 // Indicies must be ubyte constants...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000775 const ConstantUInt *CPU = cast<ConstantUInt>(I->getOperand(ArgOff++));
Chris Lattner782b9392001-11-26 18:18:18 +0000776 assert(CPU->getType() == Type::UByteTy);
777 unsigned Index = CPU->getValue();
778
Chris Lattnere2409062001-11-12 16:19:45 +0000779#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000780 if (ProfileStructureFields) {
781 // Do accounting for this field...
782 vector<unsigned> &OfC = FieldAccessCounts[STy];
783 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
784 OfC[Index]++;
785 }
Chris Lattnere2409062001-11-12 16:19:45 +0000786#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000787
788 Total += SLO->MemberOffsets[Index];
789 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000790 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000791
Chris Lattner782b9392001-11-26 18:18:18 +0000792 // Get the index number for the array... which must be uint type...
793 assert(I->getOperand(ArgOff)->getType() == Type::UIntTy);
794 unsigned Idx = getOperandValue(I->getOperand(ArgOff++), SF).UIntVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000795 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
796 if (Idx >= AT->getNumElements()) {
797 cerr << "Out of range memory access to element #" << Idx
798 << " of a " << AT->getNumElements() << " element array."
799 << " Subscript #" << (ArgOff-I->getFirstIndexOperandNumber())
800 << "\n";
801 // Get outta here!!!
802 siglongjmp(SignalRecoverBuffer, -1);
803 }
Chris Lattner782b9392001-11-26 18:18:18 +0000804
Chris Lattnerf23eb852001-12-14 16:49:29 +0000805 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000806 unsigned Size = TD.getTypeSize(Ty);
807 Total += Size*Idx;
808 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000809 }
810
811 return Total;
812}
813
814static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000815 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000816 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000817
818 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000819 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000820 SetValue(I, Result, SF);
821}
822
Chris Lattner86660982001-08-27 05:16:50 +0000823static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000824 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000825 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000826 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000827 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000828
829 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000830 GenericValue Result;
831
832 switch (I->getType()->getPrimitiveID()) {
833 case Type::BoolTyID:
834 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000835 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000836 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000837 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000838 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000839 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000840 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000841 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
842 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
843 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
844 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000845 default:
846 cout << "Cannot load value of type " << I->getType() << "!\n";
847 }
848
849 SetValue(I, Result, SF);
850}
851
852static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000853 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000854 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000855 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000856
857 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000858 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000859
860 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
861 case Type::BoolTyID:
862 case Type::UByteTyID:
863 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
864 case Type::UShortTyID:
865 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
866 case Type::UIntTyID:
867 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000868 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000869 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
870 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000871 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
872 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000873 default:
874 cout << "Cannot store value of type " << I->getType() << "!\n";
875 }
876}
877
878
879//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000880// Miscellaneous Instruction Implementations
881//===----------------------------------------------------------------------===//
882
883void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
884 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000885 vector<GenericValue> ArgVals;
886 ArgVals.reserve(I->getNumOperands()-1);
887 for (unsigned i = 1; i < I->getNumOperands(); ++i)
888 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
889
Chris Lattner070cf5e2001-11-07 20:12:30 +0000890 // To handle indirect calls, we must get the pointer value from the argument
891 // and treat it as a method pointer.
892 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
893
894 callMethod((Method*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000895}
896
897static void executePHINode(PHINode *I, ExecutionContext &SF) {
898 BasicBlock *PrevBB = SF.PrevBB;
899 Value *IncomingValue = 0;
900
901 // Search for the value corresponding to this previous bb...
902 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
903 if (I->getIncomingBlock(--i) == PrevBB) {
904 IncomingValue = I->getIncomingValue(i);
905 break;
906 }
907 }
908 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
909
910 // Found the value, set as the result...
911 SetValue(I, getOperandValue(IncomingValue, SF), SF);
912}
913
Chris Lattner86660982001-08-27 05:16:50 +0000914#define IMPLEMENT_SHIFT(OP, TY) \
915 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
916
917static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
918 const Type *Ty = I->getOperand(0)->getType();
919 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
920 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
921 GenericValue Dest;
922
923 switch (Ty->getPrimitiveID()) {
924 IMPLEMENT_SHIFT(<<, UByte);
925 IMPLEMENT_SHIFT(<<, SByte);
926 IMPLEMENT_SHIFT(<<, UShort);
927 IMPLEMENT_SHIFT(<<, Short);
928 IMPLEMENT_SHIFT(<<, UInt);
929 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000930 IMPLEMENT_SHIFT(<<, ULong);
931 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000932 default:
933 cout << "Unhandled type for Shl instruction: " << Ty << endl;
934 }
935 SetValue(I, Dest, SF);
936}
937
938static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
939 const Type *Ty = I->getOperand(0)->getType();
940 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
941 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
942 GenericValue Dest;
943
944 switch (Ty->getPrimitiveID()) {
945 IMPLEMENT_SHIFT(>>, UByte);
946 IMPLEMENT_SHIFT(>>, SByte);
947 IMPLEMENT_SHIFT(>>, UShort);
948 IMPLEMENT_SHIFT(>>, Short);
949 IMPLEMENT_SHIFT(>>, UInt);
950 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000951 IMPLEMENT_SHIFT(>>, ULong);
952 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000953 default:
954 cout << "Unhandled type for Shr instruction: " << Ty << endl;
955 }
956 SetValue(I, Dest, SF);
957}
958
959#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000960 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000961
962#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
963 case Type::DESTTY##TyID: \
964 switch (SrcTy->getPrimitiveID()) { \
965 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
966 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
967 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
968 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
969 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000970 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
971 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000972 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
973 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000974
975#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
976 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
978
979#define IMPLEMENT_CAST_CASE_END() \
980 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
981 break; \
982 } \
983 break
984
985#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
986 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
987 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000988 IMPLEMENT_CAST_CASE_END()
989
990static void executeCastInst(CastInst *I, ExecutionContext &SF) {
991 const Type *Ty = I->getType();
992 const Type *SrcTy = I->getOperand(0)->getType();
993 GenericValue Src = getOperandValue(I->getOperand(0), SF);
994 GenericValue Dest;
995
996 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000997 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
998 IMPLEMENT_CAST_CASE(SByte , ( signed char));
999 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1000 IMPLEMENT_CAST_CASE(Short , ( signed char));
1001 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1002 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1003 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1004 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1005 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1006 IMPLEMENT_CAST_CASE(Float , (float));
1007 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001008 default:
1009 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
1010 }
1011 SetValue(I, Dest, SF);
1012}
Chris Lattner92101ac2001-08-23 17:05:04 +00001013
1014
1015
1016
1017//===----------------------------------------------------------------------===//
1018// Dispatch and Execution Code
1019//===----------------------------------------------------------------------===//
1020
1021MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
1022 // Assign slot numbers to the method arguments...
1023 const Method::ArgumentListType &ArgList = M->getArgumentList();
1024 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
1025 AE = ArgList.end(); AI != AE; ++AI) {
1026 MethodArgument *MA = *AI;
1027 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
1028 }
1029
1030 // Iterate over all of the instructions...
1031 unsigned InstNum = 0;
1032 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
1033 MI != ME; ++MI) {
1034 Instruction *I = *MI; // For each instruction...
1035 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
1036 }
1037}
1038
1039unsigned MethodInfo::getValueSlot(const Value *V) {
1040 unsigned Plane = V->getType()->getUniqueID();
1041 if (Plane >= NumPlaneElements.size())
1042 NumPlaneElements.resize(Plane+1, 0);
1043 return NumPlaneElements[Plane]++;
1044}
1045
1046
Chris Lattner92101ac2001-08-23 17:05:04 +00001047//===----------------------------------------------------------------------===//
1048// callMethod - Execute the specified method...
1049//
Chris Lattner365a76e2001-09-10 04:49:44 +00001050void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
1051 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1052 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1053 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001054 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001055 GenericValue Result = callExternalMethod(M, ArgVals);
1056 const Type *RetTy = M->getReturnType();
1057
1058 // Copy the result back into the result variable if we are not returning
1059 // void.
1060 if (RetTy != Type::VoidTy) {
1061 if (!ECStack.empty() && ECStack.back().Caller) {
1062 ExecutionContext &SF = ECStack.back();
1063 CallInst *Caller = SF.Caller;
1064 SetValue(SF.Caller, Result, SF);
1065
1066 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001067 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001068 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +00001069 CW << "Method " << M->getType() << " \"" << M->getName()
1070 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001071 print(RetTy, Result);
1072 cout << endl;
1073
1074 if (RetTy->isIntegral())
1075 ExitCode = Result.SByteVal; // Capture the exit code of the program
1076 }
1077 }
1078
Chris Lattner92101ac2001-08-23 17:05:04 +00001079 return;
1080 }
1081
1082 // Process the method, assigning instruction numbers to the instructions in
1083 // the method. Also calculate the number of values for each type slot active.
1084 //
1085 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001086 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001087
Chris Lattner92101ac2001-08-23 17:05:04 +00001088 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1089 StackFrame.CurMethod = M;
1090 StackFrame.CurBB = M->front();
1091 StackFrame.CurInst = StackFrame.CurBB->begin();
1092 StackFrame.MethInfo = MethInfo;
1093
1094 // Initialize the values to nothing...
1095 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001096 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001097 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1098
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001099 // Taint the initial values of stuff
1100 memset(&StackFrame.Values[i][0], 42,
1101 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1102 }
1103
Chris Lattner92101ac2001-08-23 17:05:04 +00001104 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1105
Chris Lattner92101ac2001-08-23 17:05:04 +00001106
Chris Lattner365a76e2001-09-10 04:49:44 +00001107 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001108 assert(ArgVals.size() == M->getArgumentList().size() &&
1109 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001110 unsigned i = 0;
1111 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1112 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1113 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001114 }
1115}
1116
1117// executeInstruction - Interpret a single instruction, increment the "PC", and
1118// return true if the next instruction is a breakpoint...
1119//
1120bool Interpreter::executeInstruction() {
1121 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1122
1123 ExecutionContext &SF = ECStack.back(); // Current stack frame
1124 Instruction *I = *SF.CurInst++; // Increment before execute
1125
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001126 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001127 CW << "Run:" << I;
1128
1129 // Set a sigsetjmp buffer so that we can recover if an error happens during
1130 // instruction execution...
1131 //
1132 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1133 --SF.CurInst; // Back up to erroring instruction
Chris Lattner782b9392001-11-26 18:18:18 +00001134 if (SigNo != SIGINT && SigNo != -1) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001135 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001136 printStackTrace();
Chris Lattner782b9392001-11-26 18:18:18 +00001137 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001138 cout << "CTRL-C Detected, execution halted.\n";
1139 }
1140 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001141 return true;
1142 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001143
Chris Lattner461f02f2001-11-07 05:31:27 +00001144 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001145 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001146 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001147 } else {
1148 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001149 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001150 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1151 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001152 // Memory Instructions
1153 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001154 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1155 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1156 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1157 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001158 case Instruction::GetElementPtr:
1159 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001160
1161 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001162 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1163 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1164 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1165 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1166 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001167 default:
1168 cout << "Don't know how to execute this instruction!\n-->" << I;
1169 }
1170 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001171 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001172
1173 // Reset the current frame location to the top of stack
1174 CurFrame = ECStack.size()-1;
1175
1176 if (CurFrame == -1) return false; // No breakpoint if no code
1177
1178 // Return true if there is a breakpoint annotation on the instruction...
1179 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1180}
1181
1182void Interpreter::stepInstruction() { // Do the 'step' command
1183 if (ECStack.empty()) {
1184 cout << "Error: no program running, cannot step!\n";
1185 return;
1186 }
1187
1188 // Run an instruction...
1189 executeInstruction();
1190
1191 // Print the next instruction to execute...
1192 printCurrentInstruction();
1193}
1194
1195// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001196void Interpreter::nextInstruction() { // Do the 'next' command
1197 if (ECStack.empty()) {
1198 cout << "Error: no program running, cannot 'next'!\n";
1199 return;
1200 }
1201
1202 // If this is a call instruction, step over the call instruction...
1203 // TODO: ICALL, CALL WITH, ...
1204 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001205 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001206 // Step into the function...
1207 if (executeInstruction()) {
1208 // Hit a breakpoint, print current instruction, then return to user...
1209 cout << "Breakpoint hit!\n";
1210 printCurrentInstruction();
1211 return;
1212 }
1213
Chris Lattnera74a6b52001-10-29 14:08:33 +00001214 // If we we able to step into the function, finish it now. We might not be
1215 // able the step into a function, if it's external for example.
1216 if (ECStack.size() != StackSize)
1217 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001218 else
1219 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001220
Chris Lattner92101ac2001-08-23 17:05:04 +00001221 } else {
1222 // Normal instruction, just step...
1223 stepInstruction();
1224 }
1225}
1226
1227void Interpreter::run() {
1228 if (ECStack.empty()) {
1229 cout << "Error: no program running, cannot run!\n";
1230 return;
1231 }
1232
1233 bool HitBreakpoint = false;
1234 while (!ECStack.empty() && !HitBreakpoint) {
1235 // Run an instruction...
1236 HitBreakpoint = executeInstruction();
1237 }
1238
1239 if (HitBreakpoint) {
1240 cout << "Breakpoint hit!\n";
1241 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001242 // Print the next instruction to execute...
1243 printCurrentInstruction();
1244}
1245
1246void Interpreter::finish() {
1247 if (ECStack.empty()) {
1248 cout << "Error: no program running, cannot run!\n";
1249 return;
1250 }
1251
1252 unsigned StackSize = ECStack.size();
1253 bool HitBreakpoint = false;
1254 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1255 // Run an instruction...
1256 HitBreakpoint = executeInstruction();
1257 }
1258
1259 if (HitBreakpoint) {
1260 cout << "Breakpoint hit!\n";
1261 }
1262
1263 // Print the next instruction to execute...
1264 printCurrentInstruction();
1265}
1266
1267
1268
1269// printCurrentInstruction - Print out the instruction that the virtual PC is
1270// at, or fail silently if no program is running.
1271//
1272void Interpreter::printCurrentInstruction() {
1273 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001274 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1275 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1276
Chris Lattner92101ac2001-08-23 17:05:04 +00001277 Instruction *I = *ECStack.back().CurInst;
1278 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1279 assert(IN && "Instruction has no numbering annotation!");
1280 cout << "#" << IN->InstNum << I;
1281 }
1282}
1283
1284void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001285 switch (Ty->getPrimitiveID()) {
1286 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1287 case Type::SByteTyID: cout << V.SByteVal; break;
1288 case Type::UByteTyID: cout << V.UByteVal; break;
1289 case Type::ShortTyID: cout << V.ShortVal; break;
1290 case Type::UShortTyID: cout << V.UShortVal; break;
1291 case Type::IntTyID: cout << V.IntVal; break;
1292 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +00001293 case Type::LongTyID: cout << V.LongVal; break;
1294 case Type::ULongTyID: cout << V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001295 case Type::FloatTyID: cout << V.FloatVal; break;
1296 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001297 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001298 default:
1299 cout << "- Don't know how to print value of this type!";
1300 break;
1301 }
1302}
1303
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001304void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001305 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001306 printValue(Ty, V);
1307}
1308
1309void Interpreter::print(const string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001310 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1311 if (!PickedVal) return;
1312
Chris Lattner9636a912001-10-01 16:18:37 +00001313 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001314 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001315 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1316 CW << "type %" << Name << " = " << Ty->getDescription() << endl;
1317 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1318 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001319 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001320 print(PickedVal->getType(),
1321 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001322 cout << endl;
1323 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001324}
1325
Chris Lattner86660982001-08-27 05:16:50 +00001326void Interpreter::infoValue(const string &Name) {
1327 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1328 if (!PickedVal) return;
1329
1330 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001331 print(PickedVal->getType(),
1332 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner86660982001-08-27 05:16:50 +00001333 cout << endl;
1334 printOperandInfo(PickedVal, ECStack[CurFrame]);
1335}
1336
Chris Lattner461f02f2001-11-07 05:31:27 +00001337// printStackFrame - Print information about the specified stack frame, or -1
1338// for the default one.
1339//
1340void Interpreter::printStackFrame(int FrameNo = -1) {
1341 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001342 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001343 const Type *RetTy = Meth->getReturnType();
1344
1345 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1346 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001347
1348 Method::ArgumentListType &Args = Meth->getArgumentList();
1349 for (unsigned i = 0; i < Args.size(); ++i) {
1350 if (i != 0) cout << ", ";
1351 CW << (Value*)Args[i] << "=";
1352
1353 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001354 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001355
1356 cout << ")" << endl;
1357 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001358}
Chris Lattner461f02f2001-11-07 05:31:27 +00001359