blob: d92764e49a6ce4fa4621dbcad900f94782ce693f [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 Lattner697954c2002-01-20 22:54:45 +000022#include <iostream>
23using std::vector;
24using std::cout;
25using std::cerr;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000026
Chris Lattnerf23eb852001-12-14 16:49:29 +000027cl::Flag QuietMode ("quiet" , "Do not emit any non-program output");
28cl::Alias QuietModeA("q" , "Alias for -quiet", cl::NoFlags, QuietMode);
29
Chris Lattnere9bb2df2001-12-03 22:26:30 +000030
Chris Lattner2e42d3a2001-10-15 05:51:48 +000031// Create a TargetData structure to handle memory addressing and size/alignment
32// computations
33//
34static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000035CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000036
37
Chris Lattnere2409062001-11-12 16:19:45 +000038#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattnere2409062001-11-12 16:19:45 +000039static cl::Flag ProfileStructureFields("profilestructfields",
40 "Profile Structure Field Accesses");
41#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000042static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000043#endif
44
Chris Lattner5af0c482001-11-07 04:23:00 +000045sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000046static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000047
48extern "C" {
49static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000050 if (InInstruction)
51 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000052}
53}
54
55static void initializeSignalHandlers() {
56 struct sigaction Action;
57 Action.sa_handler = SigHandler;
58 Action.sa_flags = SA_SIGINFO;
59 sigemptyset(&Action.sa_mask);
60 sigaction(SIGSEGV, &Action, 0);
61 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000062 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000063 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000064}
65
Chris Lattner2e42d3a2001-10-15 05:51:48 +000066
67//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000068// Value Manipulation code
69//===----------------------------------------------------------------------===//
70
71static unsigned getOperandSlot(Value *V) {
72 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
73 assert(SN && "Operand does not have a slot number annotation!");
74 return SN->SlotNum;
75}
76
77#define GET_CONST_VAL(TY, CLASS) \
78 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
79
80static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000081 if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000082 GenericValue Result;
83 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000084 GET_CONST_VAL(Bool , ConstantBool);
85 GET_CONST_VAL(UByte , ConstantUInt);
86 GET_CONST_VAL(SByte , ConstantSInt);
87 GET_CONST_VAL(UShort , ConstantUInt);
88 GET_CONST_VAL(Short , ConstantSInt);
89 GET_CONST_VAL(UInt , ConstantUInt);
90 GET_CONST_VAL(Int , ConstantSInt);
91 GET_CONST_VAL(ULong , ConstantUInt);
92 GET_CONST_VAL(Long , ConstantSInt);
93 GET_CONST_VAL(Float , ConstantFP);
94 GET_CONST_VAL(Double , ConstantFP);
Chris Lattner39bb5b42001-10-15 13:25:40 +000095 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000096 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000097 Result.PointerVal = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000098 } else if (isa<ConstantPointerRef>(CPV)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000099 assert(0 && "Not implemented!");
100 } else {
101 assert(0 && "Unknown constant pointer type!");
102 }
103 break;
104 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000105 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000106 }
107 return Result;
108 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
109 GlobalAddress *Address =
110 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
111 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000112 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000113 return Result;
114 } else {
115 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000116 unsigned OpSlot = getOperandSlot(V);
117 assert(TyP < SF.Values.size() &&
118 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000119 return SF.Values[TyP][getOperandSlot(V)];
120 }
121}
122
123static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000124 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000125 cout << "Constant Pool Value\n";
126 } else if (isa<GlobalValue>(V)) {
127 cout << "Global Value\n";
128 } else {
129 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
130 unsigned Slot = getOperandSlot(V);
131 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000132 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
133 << " Contents=0x";
134
135 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
136 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
137 unsigned char Cur = Buf[i];
138 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
139 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
140 }
Chris Lattner697954c2002-01-20 22:54:45 +0000141 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000142 }
143}
144
145
146
147static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
148 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
149
Chris Lattner697954c2002-01-20 22:54:45 +0000150 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000151 SF.Values[TyP][getOperandSlot(V)] = Val;
152}
153
154
155//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000156// Annotation Wrangling code
157//===----------------------------------------------------------------------===//
158
159void Interpreter::initializeExecutionEngine() {
160 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
161 &MethodInfo::Create);
162 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
163 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000164 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000165}
166
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000167// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000168// specified memory location...
169//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000170static void InitializeMemory(Constant *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000171#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
172 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000173 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000174 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000175 } return
176
177 switch (Init->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000178 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
179 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
180 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
181 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
182 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
183 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
184 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
185 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
186 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
187 INITIALIZE_MEMORY(Float , ConstantFP , float);
188 INITIALIZE_MEMORY(Double , ConstantFP , double);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000189#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000190
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000191 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000192 ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000193 const vector<Use> &Val = CPA->getValues();
194 unsigned ElementSize =
195 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
196 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000197 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000198 return;
199 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000200
201 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000202 ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000203 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
204 const vector<Use> &Val = CPS->getValues();
205 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000206 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000207 Addr+SL->MemberOffsets[i]);
208 return;
209 }
210
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000211 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000212 if (isa<ConstantPointerNull>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000213 *(void**)Addr = 0;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000214 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000215 GlobalAddress *Address =
216 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
217 *(void**)Addr = (GenericValue*)Address->Ptr;
218 } else {
219 assert(0 && "Unknown Constant pointer type!");
220 }
221 return;
222
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000223 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000224 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000225 assert(0 && "Unknown constant type to initialize memory with!");
226 }
227}
228
229Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
230 assert(AID == GlobalAddressAID);
231
232 // This annotation will only be created on GlobalValue objects...
233 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
234
235 if (isa<Method>(GVal)) {
236 // The GlobalAddress object for a method is just a pointer to method itself.
237 // Don't delete it when the annotation is gone though!
238 return new GlobalAddress(GVal, false);
239 }
240
241 // Handle the case of a global variable...
242 assert(isa<GlobalVariable>(GVal) &&
243 "Global value found that isn't a method or global variable!");
244 GlobalVariable *GV = cast<GlobalVariable>(GVal);
245
246 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000247 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000248
249 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000250 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000251 assert(Addr != 0 && "Null pointer returned by malloc!");
252
253 // Initialize the memory if there is an initializer...
254 if (GV->hasInitializer())
255 InitializeMemory(GV->getInitializer(), (char*)Addr);
256
257 return new GlobalAddress(Addr, true); // Simply invoke the ctor
258}
259
Chris Lattner92101ac2001-08-23 17:05:04 +0000260
261//===----------------------------------------------------------------------===//
262// Binary Instruction Implementations
263//===----------------------------------------------------------------------===//
264
265#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
266 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
267
268static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
269 const Type *Ty, ExecutionContext &SF) {
270 GenericValue Dest;
271 switch (Ty->getPrimitiveID()) {
272 IMPLEMENT_BINARY_OPERATOR(+, UByte);
273 IMPLEMENT_BINARY_OPERATOR(+, SByte);
274 IMPLEMENT_BINARY_OPERATOR(+, UShort);
275 IMPLEMENT_BINARY_OPERATOR(+, Short);
276 IMPLEMENT_BINARY_OPERATOR(+, UInt);
277 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000278 IMPLEMENT_BINARY_OPERATOR(+, ULong);
279 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000280 IMPLEMENT_BINARY_OPERATOR(+, Float);
281 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000282 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000283 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000284 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000285 }
286 return Dest;
287}
288
289static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
290 const Type *Ty, ExecutionContext &SF) {
291 GenericValue Dest;
292 switch (Ty->getPrimitiveID()) {
293 IMPLEMENT_BINARY_OPERATOR(-, UByte);
294 IMPLEMENT_BINARY_OPERATOR(-, SByte);
295 IMPLEMENT_BINARY_OPERATOR(-, UShort);
296 IMPLEMENT_BINARY_OPERATOR(-, Short);
297 IMPLEMENT_BINARY_OPERATOR(-, UInt);
298 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000299 IMPLEMENT_BINARY_OPERATOR(-, ULong);
300 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000301 IMPLEMENT_BINARY_OPERATOR(-, Float);
302 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000303 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000304 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000305 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000306 }
307 return Dest;
308}
309
Chris Lattnerc2593162001-10-27 08:28:11 +0000310static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
311 const Type *Ty, ExecutionContext &SF) {
312 GenericValue Dest;
313 switch (Ty->getPrimitiveID()) {
314 IMPLEMENT_BINARY_OPERATOR(*, UByte);
315 IMPLEMENT_BINARY_OPERATOR(*, SByte);
316 IMPLEMENT_BINARY_OPERATOR(*, UShort);
317 IMPLEMENT_BINARY_OPERATOR(*, Short);
318 IMPLEMENT_BINARY_OPERATOR(*, UInt);
319 IMPLEMENT_BINARY_OPERATOR(*, Int);
320 IMPLEMENT_BINARY_OPERATOR(*, ULong);
321 IMPLEMENT_BINARY_OPERATOR(*, Long);
322 IMPLEMENT_BINARY_OPERATOR(*, Float);
323 IMPLEMENT_BINARY_OPERATOR(*, Double);
324 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
325 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000326 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000327 }
328 return Dest;
329}
330
331static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
332 const Type *Ty, ExecutionContext &SF) {
333 GenericValue Dest;
334 switch (Ty->getPrimitiveID()) {
335 IMPLEMENT_BINARY_OPERATOR(/, UByte);
336 IMPLEMENT_BINARY_OPERATOR(/, SByte);
337 IMPLEMENT_BINARY_OPERATOR(/, UShort);
338 IMPLEMENT_BINARY_OPERATOR(/, Short);
339 IMPLEMENT_BINARY_OPERATOR(/, UInt);
340 IMPLEMENT_BINARY_OPERATOR(/, Int);
341 IMPLEMENT_BINARY_OPERATOR(/, ULong);
342 IMPLEMENT_BINARY_OPERATOR(/, Long);
343 IMPLEMENT_BINARY_OPERATOR(/, Float);
344 IMPLEMENT_BINARY_OPERATOR(/, Double);
345 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
346 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000347 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000348 }
349 return Dest;
350}
351
352static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
353 const Type *Ty, ExecutionContext &SF) {
354 GenericValue Dest;
355 switch (Ty->getPrimitiveID()) {
356 IMPLEMENT_BINARY_OPERATOR(%, UByte);
357 IMPLEMENT_BINARY_OPERATOR(%, SByte);
358 IMPLEMENT_BINARY_OPERATOR(%, UShort);
359 IMPLEMENT_BINARY_OPERATOR(%, Short);
360 IMPLEMENT_BINARY_OPERATOR(%, UInt);
361 IMPLEMENT_BINARY_OPERATOR(%, Int);
362 IMPLEMENT_BINARY_OPERATOR(%, ULong);
363 IMPLEMENT_BINARY_OPERATOR(%, Long);
364 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
365 case Type::FloatTyID:
366 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
367 break;
368 case Type::DoubleTyID:
369 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
370 break;
371 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000372 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000373 }
374 return Dest;
375}
376
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000377static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000378 const Type *Ty, ExecutionContext &SF) {
379 GenericValue Dest;
380 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000381 IMPLEMENT_BINARY_OPERATOR(&, UByte);
382 IMPLEMENT_BINARY_OPERATOR(&, SByte);
383 IMPLEMENT_BINARY_OPERATOR(&, UShort);
384 IMPLEMENT_BINARY_OPERATOR(&, Short);
385 IMPLEMENT_BINARY_OPERATOR(&, UInt);
386 IMPLEMENT_BINARY_OPERATOR(&, Int);
387 IMPLEMENT_BINARY_OPERATOR(&, ULong);
388 IMPLEMENT_BINARY_OPERATOR(&, Long);
389 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
390 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000391 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000392 }
393 return Dest;
394}
395
396
397static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
398 const Type *Ty, ExecutionContext &SF) {
399 GenericValue Dest;
400 switch (Ty->getPrimitiveID()) {
401 IMPLEMENT_BINARY_OPERATOR(|, UByte);
402 IMPLEMENT_BINARY_OPERATOR(|, SByte);
403 IMPLEMENT_BINARY_OPERATOR(|, UShort);
404 IMPLEMENT_BINARY_OPERATOR(|, Short);
405 IMPLEMENT_BINARY_OPERATOR(|, UInt);
406 IMPLEMENT_BINARY_OPERATOR(|, Int);
407 IMPLEMENT_BINARY_OPERATOR(|, ULong);
408 IMPLEMENT_BINARY_OPERATOR(|, Long);
409 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
410 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000411 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000412 }
413 return Dest;
414}
415
416
417static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
418 const Type *Ty, ExecutionContext &SF) {
419 GenericValue Dest;
420 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000421 IMPLEMENT_BINARY_OPERATOR(^, UByte);
422 IMPLEMENT_BINARY_OPERATOR(^, SByte);
423 IMPLEMENT_BINARY_OPERATOR(^, UShort);
424 IMPLEMENT_BINARY_OPERATOR(^, Short);
425 IMPLEMENT_BINARY_OPERATOR(^, UInt);
426 IMPLEMENT_BINARY_OPERATOR(^, Int);
427 IMPLEMENT_BINARY_OPERATOR(^, ULong);
428 IMPLEMENT_BINARY_OPERATOR(^, Long);
429 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
430 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000431 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000432 }
433 return Dest;
434}
435
436
Chris Lattner92101ac2001-08-23 17:05:04 +0000437#define IMPLEMENT_SETCC(OP, TY) \
438 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
439
Chris Lattner92101ac2001-08-23 17:05:04 +0000440static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
441 const Type *Ty, ExecutionContext &SF) {
442 GenericValue Dest;
443 switch (Ty->getPrimitiveID()) {
444 IMPLEMENT_SETCC(==, UByte);
445 IMPLEMENT_SETCC(==, SByte);
446 IMPLEMENT_SETCC(==, UShort);
447 IMPLEMENT_SETCC(==, Short);
448 IMPLEMENT_SETCC(==, UInt);
449 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000450 IMPLEMENT_SETCC(==, ULong);
451 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000452 IMPLEMENT_SETCC(==, Float);
453 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000454 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000456 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 }
458 return Dest;
459}
460
461static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
462 const Type *Ty, ExecutionContext &SF) {
463 GenericValue Dest;
464 switch (Ty->getPrimitiveID()) {
465 IMPLEMENT_SETCC(!=, UByte);
466 IMPLEMENT_SETCC(!=, SByte);
467 IMPLEMENT_SETCC(!=, UShort);
468 IMPLEMENT_SETCC(!=, Short);
469 IMPLEMENT_SETCC(!=, UInt);
470 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000471 IMPLEMENT_SETCC(!=, ULong);
472 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 IMPLEMENT_SETCC(!=, Float);
474 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000475 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000476
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000478 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 }
480 return Dest;
481}
482
483static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
484 const Type *Ty, ExecutionContext &SF) {
485 GenericValue Dest;
486 switch (Ty->getPrimitiveID()) {
487 IMPLEMENT_SETCC(<=, UByte);
488 IMPLEMENT_SETCC(<=, SByte);
489 IMPLEMENT_SETCC(<=, UShort);
490 IMPLEMENT_SETCC(<=, Short);
491 IMPLEMENT_SETCC(<=, UInt);
492 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000493 IMPLEMENT_SETCC(<=, ULong);
494 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000495 IMPLEMENT_SETCC(<=, Float);
496 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000497 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000498 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000499 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000500 }
501 return Dest;
502}
503
504static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
505 const Type *Ty, ExecutionContext &SF) {
506 GenericValue Dest;
507 switch (Ty->getPrimitiveID()) {
508 IMPLEMENT_SETCC(>=, UByte);
509 IMPLEMENT_SETCC(>=, SByte);
510 IMPLEMENT_SETCC(>=, UShort);
511 IMPLEMENT_SETCC(>=, Short);
512 IMPLEMENT_SETCC(>=, UInt);
513 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000514 IMPLEMENT_SETCC(>=, ULong);
515 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000516 IMPLEMENT_SETCC(>=, Float);
517 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000518 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000519 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000520 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 }
522 return Dest;
523}
524
525static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
526 const Type *Ty, ExecutionContext &SF) {
527 GenericValue Dest;
528 switch (Ty->getPrimitiveID()) {
529 IMPLEMENT_SETCC(<, UByte);
530 IMPLEMENT_SETCC(<, SByte);
531 IMPLEMENT_SETCC(<, UShort);
532 IMPLEMENT_SETCC(<, Short);
533 IMPLEMENT_SETCC(<, UInt);
534 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000535 IMPLEMENT_SETCC(<, ULong);
536 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000537 IMPLEMENT_SETCC(<, Float);
538 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000539 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000540 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000541 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000542 }
543 return Dest;
544}
545
546static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
547 const Type *Ty, ExecutionContext &SF) {
548 GenericValue Dest;
549 switch (Ty->getPrimitiveID()) {
550 IMPLEMENT_SETCC(>, UByte);
551 IMPLEMENT_SETCC(>, SByte);
552 IMPLEMENT_SETCC(>, UShort);
553 IMPLEMENT_SETCC(>, Short);
554 IMPLEMENT_SETCC(>, UInt);
555 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000556 IMPLEMENT_SETCC(>, ULong);
557 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000558 IMPLEMENT_SETCC(>, Float);
559 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000560 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000561 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000562 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000563 }
564 return Dest;
565}
566
567static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
568 const Type *Ty = I->getOperand(0)->getType();
569 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
570 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
571 GenericValue R; // Result
572
573 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000574 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
575 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
576 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
577 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
578 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000579 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
580 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000581 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000582 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
583 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
584 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
585 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
586 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
587 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
588 default:
589 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000590 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000591 }
592
593 SetValue(I, R, SF);
594}
595
Chris Lattner92101ac2001-08-23 17:05:04 +0000596//===----------------------------------------------------------------------===//
597// Terminator Instruction Implementations
598//===----------------------------------------------------------------------===//
599
Chris Lattnera95c6992001-11-12 16:28:48 +0000600static void PerformExitStuff() {
601#ifdef PROFILE_STRUCTURE_FIELDS
602 // Print out structure field accounting information...
603 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000604 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000605 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000606 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
607 for (; I != E; ++I) {
608 vector<unsigned> &OfC = I->second;
609 CW << " '" << (Value*)I->first << "'\t- Sum=";
610
611 unsigned Sum = 0;
612 for (unsigned i = 0; i < OfC.size(); ++i)
613 Sum += OfC[i];
614 CW << Sum << " - ";
615
616 for (unsigned i = 0; i < OfC.size(); ++i) {
617 if (i) CW << ", ";
618 CW << OfC[i];
619 }
Chris Lattner697954c2002-01-20 22:54:45 +0000620 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000621 }
Chris Lattner697954c2002-01-20 22:54:45 +0000622 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000623
624 CW << "Profile Field Access Percentages:\n";
625 cout.precision(3);
626 for (I = FieldAccessCounts.begin(); I != E; ++I) {
627 vector<unsigned> &OfC = I->second;
628 unsigned Sum = 0;
629 for (unsigned i = 0; i < OfC.size(); ++i)
630 Sum += OfC[i];
631
632 CW << " '" << (Value*)I->first << "'\t- ";
633 for (unsigned i = 0; i < OfC.size(); ++i) {
634 if (i) CW << ", ";
635 CW << double(OfC[i])/Sum;
636 }
Chris Lattner697954c2002-01-20 22:54:45 +0000637 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000638 }
Chris Lattner697954c2002-01-20 22:54:45 +0000639 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000640
Chris Lattnera95c6992001-11-12 16:28:48 +0000641 FieldAccessCounts.clear();
642 }
643#endif
644}
645
Chris Lattnere43db882001-10-27 04:15:57 +0000646void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000647 if (!QuietMode) {
648 cout << "Program returned ";
649 print(Type::IntTy, GV);
650 cout << " via 'void exit(int)'\n";
651 }
Chris Lattnere43db882001-10-27 04:15:57 +0000652
653 ExitCode = GV.SByteVal;
654 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000655 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000656}
657
Chris Lattner92101ac2001-08-23 17:05:04 +0000658void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
659 const Type *RetTy = 0;
660 GenericValue Result;
661
662 // Save away the return value... (if we are not 'ret void')
663 if (I->getNumOperands()) {
664 RetTy = I->getReturnValue()->getType();
665 Result = getOperandValue(I->getReturnValue(), SF);
666 }
667
668 // Save previously executing meth
669 const Method *M = ECStack.back().CurMethod;
670
671 // Pop the current stack frame... this invalidates SF
672 ECStack.pop_back();
673
674 if (ECStack.empty()) { // Finished main. Put result into exit code...
675 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000676 if (!QuietMode) {
677 CW << "Method " << M->getType() << " \"" << M->getName()
678 << "\" returned ";
679 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000680 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000681 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000682
683 if (RetTy->isIntegral())
684 ExitCode = Result.SByteVal; // Capture the exit code of the program
685 } else {
686 ExitCode = 0;
687 }
Chris Lattnere2409062001-11-12 16:19:45 +0000688
Chris Lattnera95c6992001-11-12 16:28:48 +0000689 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000690 return;
691 }
692
693 // If we have a previous stack frame, and we have a previous call, fill in
694 // the return value...
695 //
696 ExecutionContext &NewSF = ECStack.back();
697 if (NewSF.Caller) {
698 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
699 SetValue(NewSF.Caller, Result, NewSF);
700
701 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000702 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000703 // This must be a function that is executing because of a user 'call'
704 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000705 CW << "Method " << M->getType() << " \"" << M->getName()
706 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000707 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000708 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000709 }
710}
711
712void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
713 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
714 BasicBlock *Dest;
715
716 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
717 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000718 Value *Cond = I->getCondition();
719 GenericValue CondVal = getOperandValue(Cond, SF);
720 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000721 Dest = I->getSuccessor(1);
722 }
723 SF.CurBB = Dest; // Update CurBB to branch destination
724 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
725}
726
727//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000728// Memory Instruction Implementations
729//===----------------------------------------------------------------------===//
730
Chris Lattner86660982001-08-27 05:16:50 +0000731void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
Chris Lattner7a176752001-12-04 00:03:30 +0000732 const Type *Ty = I->getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000733 unsigned NumElements = 1;
734
Chris Lattnerf23eb852001-12-14 16:49:29 +0000735 // FIXME: Malloc/Alloca should always have an argument!
Chris Lattner86660982001-08-27 05:16:50 +0000736 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattner86660982001-08-27 05:16:50 +0000737 // 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 Lattnerf23eb852001-12-14 16:49:29 +0000771 const Type *Ty = I->getPointerOperand()->getType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000772
Chris Lattner782b9392001-11-26 18:18:18 +0000773 unsigned ArgOff = I->getFirstIndexOperandNumber();
Chris Lattner95c3af52001-10-29 19:32:19 +0000774 while (ArgOff < I->getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000775 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
776 const StructLayout *SLO = TD.getStructLayout(STy);
777
778 // Indicies must be ubyte constants...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000779 const ConstantUInt *CPU = cast<ConstantUInt>(I->getOperand(ArgOff++));
Chris Lattner782b9392001-11-26 18:18:18 +0000780 assert(CPU->getType() == Type::UByteTy);
781 unsigned Index = CPU->getValue();
782
Chris Lattnere2409062001-11-12 16:19:45 +0000783#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000784 if (ProfileStructureFields) {
785 // Do accounting for this field...
786 vector<unsigned> &OfC = FieldAccessCounts[STy];
787 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
788 OfC[Index]++;
789 }
Chris Lattnere2409062001-11-12 16:19:45 +0000790#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000791
792 Total += SLO->MemberOffsets[Index];
793 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000794 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000795
Chris Lattner782b9392001-11-26 18:18:18 +0000796 // Get the index number for the array... which must be uint type...
797 assert(I->getOperand(ArgOff)->getType() == Type::UIntTy);
798 unsigned Idx = getOperandValue(I->getOperand(ArgOff++), SF).UIntVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000799 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
800 if (Idx >= AT->getNumElements()) {
801 cerr << "Out of range memory access to element #" << Idx
802 << " of a " << AT->getNumElements() << " element array."
803 << " Subscript #" << (ArgOff-I->getFirstIndexOperandNumber())
804 << "\n";
805 // Get outta here!!!
806 siglongjmp(SignalRecoverBuffer, -1);
807 }
Chris Lattner782b9392001-11-26 18:18:18 +0000808
Chris Lattnerf23eb852001-12-14 16:49:29 +0000809 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000810 unsigned Size = TD.getTypeSize(Ty);
811 Total += Size*Idx;
812 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000813 }
814
815 return Total;
816}
817
818static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000819 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000820 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000821
822 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000823 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000824 SetValue(I, Result, SF);
825}
826
Chris Lattner86660982001-08-27 05:16:50 +0000827static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000828 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000829 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000830 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000831 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000832
833 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000834 GenericValue Result;
835
836 switch (I->getType()->getPrimitiveID()) {
837 case Type::BoolTyID:
838 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000839 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000840 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000841 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000842 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000843 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000844 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000845 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
846 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
847 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
848 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000849 default:
850 cout << "Cannot load value of type " << I->getType() << "!\n";
851 }
852
853 SetValue(I, Result, SF);
854}
855
856static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000857 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000858 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000859 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000860
861 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000862 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000863
864 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
865 case Type::BoolTyID:
866 case Type::UByteTyID:
867 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
868 case Type::UShortTyID:
869 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
870 case Type::UIntTyID:
871 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000872 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000873 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
874 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000875 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
876 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000877 default:
878 cout << "Cannot store value of type " << I->getType() << "!\n";
879 }
880}
881
882
883//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000884// Miscellaneous Instruction Implementations
885//===----------------------------------------------------------------------===//
886
887void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
888 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000889 vector<GenericValue> ArgVals;
890 ArgVals.reserve(I->getNumOperands()-1);
891 for (unsigned i = 1; i < I->getNumOperands(); ++i)
892 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
893
Chris Lattner070cf5e2001-11-07 20:12:30 +0000894 // To handle indirect calls, we must get the pointer value from the argument
895 // and treat it as a method pointer.
896 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
897
898 callMethod((Method*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000899}
900
901static void executePHINode(PHINode *I, ExecutionContext &SF) {
902 BasicBlock *PrevBB = SF.PrevBB;
903 Value *IncomingValue = 0;
904
905 // Search for the value corresponding to this previous bb...
906 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
907 if (I->getIncomingBlock(--i) == PrevBB) {
908 IncomingValue = I->getIncomingValue(i);
909 break;
910 }
911 }
912 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
913
914 // Found the value, set as the result...
915 SetValue(I, getOperandValue(IncomingValue, SF), SF);
916}
917
Chris Lattner86660982001-08-27 05:16:50 +0000918#define IMPLEMENT_SHIFT(OP, TY) \
919 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
920
921static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
922 const Type *Ty = I->getOperand(0)->getType();
923 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
924 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
925 GenericValue Dest;
926
927 switch (Ty->getPrimitiveID()) {
928 IMPLEMENT_SHIFT(<<, UByte);
929 IMPLEMENT_SHIFT(<<, SByte);
930 IMPLEMENT_SHIFT(<<, UShort);
931 IMPLEMENT_SHIFT(<<, Short);
932 IMPLEMENT_SHIFT(<<, UInt);
933 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000934 IMPLEMENT_SHIFT(<<, ULong);
935 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000936 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000937 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000938 }
939 SetValue(I, Dest, SF);
940}
941
942static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
943 const Type *Ty = I->getOperand(0)->getType();
944 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
945 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
946 GenericValue Dest;
947
948 switch (Ty->getPrimitiveID()) {
949 IMPLEMENT_SHIFT(>>, UByte);
950 IMPLEMENT_SHIFT(>>, SByte);
951 IMPLEMENT_SHIFT(>>, UShort);
952 IMPLEMENT_SHIFT(>>, Short);
953 IMPLEMENT_SHIFT(>>, UInt);
954 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000955 IMPLEMENT_SHIFT(>>, ULong);
956 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000957 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000958 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000959 }
960 SetValue(I, Dest, SF);
961}
962
963#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000964 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000965
966#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
967 case Type::DESTTY##TyID: \
968 switch (SrcTy->getPrimitiveID()) { \
969 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
970 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
971 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
972 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
973 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000974 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
975 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000976 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000978
979#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
980 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
982
983#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +0000984 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +0000985 break; \
986 } \
987 break
988
989#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
990 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
991 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000992 IMPLEMENT_CAST_CASE_END()
993
994static void executeCastInst(CastInst *I, ExecutionContext &SF) {
995 const Type *Ty = I->getType();
996 const Type *SrcTy = I->getOperand(0)->getType();
997 GenericValue Src = getOperandValue(I->getOperand(0), SF);
998 GenericValue Dest;
999
1000 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001001 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1002 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1003 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1004 IMPLEMENT_CAST_CASE(Short , ( signed char));
1005 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1006 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1007 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1008 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1009 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1010 IMPLEMENT_CAST_CASE(Float , (float));
1011 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001012 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001013 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001014 }
1015 SetValue(I, Dest, SF);
1016}
Chris Lattner92101ac2001-08-23 17:05:04 +00001017
1018
1019
1020
1021//===----------------------------------------------------------------------===//
1022// Dispatch and Execution Code
1023//===----------------------------------------------------------------------===//
1024
1025MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
1026 // Assign slot numbers to the method arguments...
1027 const Method::ArgumentListType &ArgList = M->getArgumentList();
1028 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
1029 AE = ArgList.end(); AI != AE; ++AI) {
1030 MethodArgument *MA = *AI;
1031 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
1032 }
1033
1034 // Iterate over all of the instructions...
1035 unsigned InstNum = 0;
1036 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
1037 MI != ME; ++MI) {
1038 Instruction *I = *MI; // For each instruction...
1039 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
1040 }
1041}
1042
1043unsigned MethodInfo::getValueSlot(const Value *V) {
1044 unsigned Plane = V->getType()->getUniqueID();
1045 if (Plane >= NumPlaneElements.size())
1046 NumPlaneElements.resize(Plane+1, 0);
1047 return NumPlaneElements[Plane]++;
1048}
1049
1050
Chris Lattner92101ac2001-08-23 17:05:04 +00001051//===----------------------------------------------------------------------===//
1052// callMethod - Execute the specified method...
1053//
Chris Lattner365a76e2001-09-10 04:49:44 +00001054void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
1055 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1056 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1057 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001058 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001059 GenericValue Result = callExternalMethod(M, ArgVals);
1060 const Type *RetTy = M->getReturnType();
1061
1062 // Copy the result back into the result variable if we are not returning
1063 // void.
1064 if (RetTy != Type::VoidTy) {
1065 if (!ECStack.empty() && ECStack.back().Caller) {
1066 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001067 SetValue(SF.Caller, Result, SF);
1068
1069 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001070 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001071 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +00001072 CW << "Method " << M->getType() << " \"" << M->getName()
1073 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001074 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001075 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001076
1077 if (RetTy->isIntegral())
1078 ExitCode = Result.SByteVal; // Capture the exit code of the program
1079 }
1080 }
1081
Chris Lattner92101ac2001-08-23 17:05:04 +00001082 return;
1083 }
1084
1085 // Process the method, assigning instruction numbers to the instructions in
1086 // the method. Also calculate the number of values for each type slot active.
1087 //
1088 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001089 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001090
Chris Lattner92101ac2001-08-23 17:05:04 +00001091 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1092 StackFrame.CurMethod = M;
1093 StackFrame.CurBB = M->front();
1094 StackFrame.CurInst = StackFrame.CurBB->begin();
1095 StackFrame.MethInfo = MethInfo;
1096
1097 // Initialize the values to nothing...
1098 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001099 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001100 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1101
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001102 // Taint the initial values of stuff
1103 memset(&StackFrame.Values[i][0], 42,
1104 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1105 }
1106
Chris Lattner92101ac2001-08-23 17:05:04 +00001107 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1108
Chris Lattner92101ac2001-08-23 17:05:04 +00001109
Chris Lattner365a76e2001-09-10 04:49:44 +00001110 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001111 assert(ArgVals.size() == M->getArgumentList().size() &&
1112 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001113 unsigned i = 0;
1114 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1115 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1116 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001117 }
1118}
1119
1120// executeInstruction - Interpret a single instruction, increment the "PC", and
1121// return true if the next instruction is a breakpoint...
1122//
1123bool Interpreter::executeInstruction() {
1124 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1125
1126 ExecutionContext &SF = ECStack.back(); // Current stack frame
1127 Instruction *I = *SF.CurInst++; // Increment before execute
1128
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001129 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001130 CW << "Run:" << I;
1131
1132 // Set a sigsetjmp buffer so that we can recover if an error happens during
1133 // instruction execution...
1134 //
1135 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1136 --SF.CurInst; // Back up to erroring instruction
Chris Lattner782b9392001-11-26 18:18:18 +00001137 if (SigNo != SIGINT && SigNo != -1) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001138 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001139 printStackTrace();
Chris Lattner782b9392001-11-26 18:18:18 +00001140 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001141 cout << "CTRL-C Detected, execution halted.\n";
1142 }
1143 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001144 return true;
1145 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001146
Chris Lattner461f02f2001-11-07 05:31:27 +00001147 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001148 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001149 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001150 } else {
1151 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001152 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001153 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1154 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001155 // Memory Instructions
1156 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001157 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1158 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1159 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1160 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001161 case Instruction::GetElementPtr:
1162 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001163
1164 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001165 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1166 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1167 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1168 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1169 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001170 default:
1171 cout << "Don't know how to execute this instruction!\n-->" << I;
1172 }
1173 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001174 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001175
1176 // Reset the current frame location to the top of stack
1177 CurFrame = ECStack.size()-1;
1178
1179 if (CurFrame == -1) return false; // No breakpoint if no code
1180
1181 // Return true if there is a breakpoint annotation on the instruction...
1182 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1183}
1184
1185void Interpreter::stepInstruction() { // Do the 'step' command
1186 if (ECStack.empty()) {
1187 cout << "Error: no program running, cannot step!\n";
1188 return;
1189 }
1190
1191 // Run an instruction...
1192 executeInstruction();
1193
1194 // Print the next instruction to execute...
1195 printCurrentInstruction();
1196}
1197
1198// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001199void Interpreter::nextInstruction() { // Do the 'next' command
1200 if (ECStack.empty()) {
1201 cout << "Error: no program running, cannot 'next'!\n";
1202 return;
1203 }
1204
1205 // If this is a call instruction, step over the call instruction...
1206 // TODO: ICALL, CALL WITH, ...
1207 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001208 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001209 // Step into the function...
1210 if (executeInstruction()) {
1211 // Hit a breakpoint, print current instruction, then return to user...
1212 cout << "Breakpoint hit!\n";
1213 printCurrentInstruction();
1214 return;
1215 }
1216
Chris Lattnera74a6b52001-10-29 14:08:33 +00001217 // If we we able to step into the function, finish it now. We might not be
1218 // able the step into a function, if it's external for example.
1219 if (ECStack.size() != StackSize)
1220 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001221 else
1222 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001223
Chris Lattner92101ac2001-08-23 17:05:04 +00001224 } else {
1225 // Normal instruction, just step...
1226 stepInstruction();
1227 }
1228}
1229
1230void Interpreter::run() {
1231 if (ECStack.empty()) {
1232 cout << "Error: no program running, cannot run!\n";
1233 return;
1234 }
1235
1236 bool HitBreakpoint = false;
1237 while (!ECStack.empty() && !HitBreakpoint) {
1238 // Run an instruction...
1239 HitBreakpoint = executeInstruction();
1240 }
1241
1242 if (HitBreakpoint) {
1243 cout << "Breakpoint hit!\n";
1244 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001245 // Print the next instruction to execute...
1246 printCurrentInstruction();
1247}
1248
1249void Interpreter::finish() {
1250 if (ECStack.empty()) {
1251 cout << "Error: no program running, cannot run!\n";
1252 return;
1253 }
1254
1255 unsigned StackSize = ECStack.size();
1256 bool HitBreakpoint = false;
1257 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1258 // Run an instruction...
1259 HitBreakpoint = executeInstruction();
1260 }
1261
1262 if (HitBreakpoint) {
1263 cout << "Breakpoint hit!\n";
1264 }
1265
1266 // Print the next instruction to execute...
1267 printCurrentInstruction();
1268}
1269
1270
1271
1272// printCurrentInstruction - Print out the instruction that the virtual PC is
1273// at, or fail silently if no program is running.
1274//
1275void Interpreter::printCurrentInstruction() {
1276 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001277 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1278 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1279
Chris Lattner92101ac2001-08-23 17:05:04 +00001280 Instruction *I = *ECStack.back().CurInst;
1281 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1282 assert(IN && "Instruction has no numbering annotation!");
1283 cout << "#" << IN->InstNum << I;
1284 }
1285}
1286
1287void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001288 switch (Ty->getPrimitiveID()) {
1289 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1290 case Type::SByteTyID: cout << V.SByteVal; break;
1291 case Type::UByteTyID: cout << V.UByteVal; break;
1292 case Type::ShortTyID: cout << V.ShortVal; break;
1293 case Type::UShortTyID: cout << V.UShortVal; break;
1294 case Type::IntTyID: cout << V.IntVal; break;
1295 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001296 case Type::LongTyID: cout << (long)V.LongVal; break;
1297 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001298 case Type::FloatTyID: cout << V.FloatVal; break;
1299 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001300 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001301 default:
1302 cout << "- Don't know how to print value of this type!";
1303 break;
1304 }
1305}
1306
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001307void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001308 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001309 printValue(Ty, V);
1310}
1311
Chris Lattner697954c2002-01-20 22:54:45 +00001312void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001313 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1314 if (!PickedVal) return;
1315
Chris Lattner9636a912001-10-01 16:18:37 +00001316 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001317 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001318 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001319 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001320 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1321 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001322 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001323 print(PickedVal->getType(),
1324 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001325 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001326 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001327}
1328
Chris Lattner697954c2002-01-20 22:54:45 +00001329void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001330 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1331 if (!PickedVal) return;
1332
1333 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001334 print(PickedVal->getType(),
1335 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001336 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001337 printOperandInfo(PickedVal, ECStack[CurFrame]);
1338}
1339
Chris Lattner461f02f2001-11-07 05:31:27 +00001340// printStackFrame - Print information about the specified stack frame, or -1
1341// for the default one.
1342//
1343void Interpreter::printStackFrame(int FrameNo = -1) {
1344 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001345 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001346 const Type *RetTy = Meth->getReturnType();
1347
1348 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1349 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001350
1351 Method::ArgumentListType &Args = Meth->getArgumentList();
1352 for (unsigned i = 0; i < Args.size(); ++i) {
1353 if (i != 0) cout << ", ";
1354 CW << (Value*)Args[i] << "=";
1355
1356 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001357 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001358
Chris Lattner697954c2002-01-20 22:54:45 +00001359 cout << ")\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001360 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001361}
Chris Lattner461f02f2001-11-07 05:31:27 +00001362