blob: 0fad82964d2fce3faad3a8a67f448d995b46d53d [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);
Chris Lattnerc0fbd572002-02-11 20:19:16 +000029cl::Flag ArrayChecksEnabled("array-checks", "Enable array bound checks");
Chris Lattner74030252002-02-12 15:47:23 +000030cl::Flag AbortOnExceptions("abort-on-exception", "Halt execution on a machine exception");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000031
Chris Lattner2e42d3a2001-10-15 05:51:48 +000032// Create a TargetData structure to handle memory addressing and size/alignment
33// computations
34//
35static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000036CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000037
38
Chris Lattnere2409062001-11-12 16:19:45 +000039#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattnere2409062001-11-12 16:19:45 +000040static cl::Flag ProfileStructureFields("profilestructfields",
41 "Profile Structure Field Accesses");
42#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000043static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000044#endif
45
Chris Lattner5af0c482001-11-07 04:23:00 +000046sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000047static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000048
49extern "C" {
50static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000051 if (InInstruction)
52 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000053}
54}
55
56static void initializeSignalHandlers() {
57 struct sigaction Action;
58 Action.sa_handler = SigHandler;
59 Action.sa_flags = SA_SIGINFO;
60 sigemptyset(&Action.sa_mask);
61 sigaction(SIGSEGV, &Action, 0);
62 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000063 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000064 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000065}
66
Chris Lattner2e42d3a2001-10-15 05:51:48 +000067
68//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000069// Value Manipulation code
70//===----------------------------------------------------------------------===//
71
72static unsigned getOperandSlot(Value *V) {
73 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
74 assert(SN && "Operand does not have a slot number annotation!");
75 return SN->SlotNum;
76}
77
78#define GET_CONST_VAL(TY, CLASS) \
79 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
80
81static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000083 GenericValue Result;
84 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000085 GET_CONST_VAL(Bool , ConstantBool);
86 GET_CONST_VAL(UByte , ConstantUInt);
87 GET_CONST_VAL(SByte , ConstantSInt);
88 GET_CONST_VAL(UShort , ConstantUInt);
89 GET_CONST_VAL(Short , ConstantSInt);
90 GET_CONST_VAL(UInt , ConstantUInt);
91 GET_CONST_VAL(Int , ConstantSInt);
92 GET_CONST_VAL(ULong , ConstantUInt);
93 GET_CONST_VAL(Long , ConstantSInt);
94 GET_CONST_VAL(Float , ConstantFP);
95 GET_CONST_VAL(Double , ConstantFP);
Chris Lattner39bb5b42001-10-15 13:25:40 +000096 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000097 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000098 Result.PointerVal = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000099 } else if (isa<ConstantPointerRef>(CPV)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000100 assert(0 && "Not implemented!");
101 } else {
102 assert(0 && "Unknown constant pointer type!");
103 }
104 break;
105 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000106 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000107 }
108 return Result;
109 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
110 GlobalAddress *Address =
111 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
112 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000113 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000114 return Result;
115 } else {
116 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000117 unsigned OpSlot = getOperandSlot(V);
118 assert(TyP < SF.Values.size() &&
119 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000120 return SF.Values[TyP][getOperandSlot(V)];
121 }
122}
123
124static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000125 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000126 cout << "Constant Pool Value\n";
127 } else if (isa<GlobalValue>(V)) {
128 cout << "Global Value\n";
129 } else {
130 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
131 unsigned Slot = getOperandSlot(V);
132 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000133 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
134 << " Contents=0x";
135
136 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
137 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
138 unsigned char Cur = Buf[i];
139 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
140 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
141 }
Chris Lattner697954c2002-01-20 22:54:45 +0000142 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000143 }
144}
145
146
147
148static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
149 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
150
Chris Lattner697954c2002-01-20 22:54:45 +0000151 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000152 SF.Values[TyP][getOperandSlot(V)] = Val;
153}
154
155
156//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000157// Annotation Wrangling code
158//===----------------------------------------------------------------------===//
159
160void Interpreter::initializeExecutionEngine() {
161 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
162 &MethodInfo::Create);
163 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
164 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000165 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166}
167
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000168// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000169// specified memory location...
170//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171static void InitializeMemory(Constant *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000172#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
173 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000174 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000175 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000176 } return
177
178 switch (Init->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000179 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
180 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
181 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
182 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
183 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
184 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
185 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
186 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
187 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
188 INITIALIZE_MEMORY(Float , ConstantFP , float);
189 INITIALIZE_MEMORY(Double , ConstantFP , double);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000190#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000192 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000193 ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000194 const vector<Use> &Val = CPA->getValues();
195 unsigned ElementSize =
196 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
197 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000198 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000199 return;
200 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000201
202 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000203 ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000204 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
205 const vector<Use> &Val = CPS->getValues();
206 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000207 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000208 Addr+SL->MemberOffsets[i]);
209 return;
210 }
211
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000212 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000213 if (isa<ConstantPointerNull>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000214 *(void**)Addr = 0;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000215 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000216 GlobalAddress *Address =
217 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
218 *(void**)Addr = (GenericValue*)Address->Ptr;
219 } else {
220 assert(0 && "Unknown Constant pointer type!");
221 }
222 return;
223
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000224 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000225 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000226 assert(0 && "Unknown constant type to initialize memory with!");
227 }
228}
229
230Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
231 assert(AID == GlobalAddressAID);
232
233 // This annotation will only be created on GlobalValue objects...
234 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
235
236 if (isa<Method>(GVal)) {
237 // The GlobalAddress object for a method is just a pointer to method itself.
238 // Don't delete it when the annotation is gone though!
239 return new GlobalAddress(GVal, false);
240 }
241
242 // Handle the case of a global variable...
243 assert(isa<GlobalVariable>(GVal) &&
244 "Global value found that isn't a method or global variable!");
245 GlobalVariable *GV = cast<GlobalVariable>(GVal);
246
247 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000248 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000249
250 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000251 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000252 assert(Addr != 0 && "Null pointer returned by malloc!");
253
254 // Initialize the memory if there is an initializer...
255 if (GV->hasInitializer())
256 InitializeMemory(GV->getInitializer(), (char*)Addr);
257
258 return new GlobalAddress(Addr, true); // Simply invoke the ctor
259}
260
Chris Lattner92101ac2001-08-23 17:05:04 +0000261
262//===----------------------------------------------------------------------===//
263// Binary Instruction Implementations
264//===----------------------------------------------------------------------===//
265
266#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
267 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
268
269static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
270 const Type *Ty, ExecutionContext &SF) {
271 GenericValue Dest;
272 switch (Ty->getPrimitiveID()) {
273 IMPLEMENT_BINARY_OPERATOR(+, UByte);
274 IMPLEMENT_BINARY_OPERATOR(+, SByte);
275 IMPLEMENT_BINARY_OPERATOR(+, UShort);
276 IMPLEMENT_BINARY_OPERATOR(+, Short);
277 IMPLEMENT_BINARY_OPERATOR(+, UInt);
278 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000279 IMPLEMENT_BINARY_OPERATOR(+, ULong);
280 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000281 IMPLEMENT_BINARY_OPERATOR(+, Float);
282 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000283 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000284 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000285 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000286 }
287 return Dest;
288}
289
290static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
291 const Type *Ty, ExecutionContext &SF) {
292 GenericValue Dest;
293 switch (Ty->getPrimitiveID()) {
294 IMPLEMENT_BINARY_OPERATOR(-, UByte);
295 IMPLEMENT_BINARY_OPERATOR(-, SByte);
296 IMPLEMENT_BINARY_OPERATOR(-, UShort);
297 IMPLEMENT_BINARY_OPERATOR(-, Short);
298 IMPLEMENT_BINARY_OPERATOR(-, UInt);
299 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000300 IMPLEMENT_BINARY_OPERATOR(-, ULong);
301 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 IMPLEMENT_BINARY_OPERATOR(-, Float);
303 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000304 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000305 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000306 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000307 }
308 return Dest;
309}
310
Chris Lattnerc2593162001-10-27 08:28:11 +0000311static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
312 const Type *Ty, ExecutionContext &SF) {
313 GenericValue Dest;
314 switch (Ty->getPrimitiveID()) {
315 IMPLEMENT_BINARY_OPERATOR(*, UByte);
316 IMPLEMENT_BINARY_OPERATOR(*, SByte);
317 IMPLEMENT_BINARY_OPERATOR(*, UShort);
318 IMPLEMENT_BINARY_OPERATOR(*, Short);
319 IMPLEMENT_BINARY_OPERATOR(*, UInt);
320 IMPLEMENT_BINARY_OPERATOR(*, Int);
321 IMPLEMENT_BINARY_OPERATOR(*, ULong);
322 IMPLEMENT_BINARY_OPERATOR(*, Long);
323 IMPLEMENT_BINARY_OPERATOR(*, Float);
324 IMPLEMENT_BINARY_OPERATOR(*, Double);
325 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
326 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000327 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000328 }
329 return Dest;
330}
331
332static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
333 const Type *Ty, ExecutionContext &SF) {
334 GenericValue Dest;
335 switch (Ty->getPrimitiveID()) {
336 IMPLEMENT_BINARY_OPERATOR(/, UByte);
337 IMPLEMENT_BINARY_OPERATOR(/, SByte);
338 IMPLEMENT_BINARY_OPERATOR(/, UShort);
339 IMPLEMENT_BINARY_OPERATOR(/, Short);
340 IMPLEMENT_BINARY_OPERATOR(/, UInt);
341 IMPLEMENT_BINARY_OPERATOR(/, Int);
342 IMPLEMENT_BINARY_OPERATOR(/, ULong);
343 IMPLEMENT_BINARY_OPERATOR(/, Long);
344 IMPLEMENT_BINARY_OPERATOR(/, Float);
345 IMPLEMENT_BINARY_OPERATOR(/, Double);
346 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
347 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000348 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000349 }
350 return Dest;
351}
352
353static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
354 const Type *Ty, ExecutionContext &SF) {
355 GenericValue Dest;
356 switch (Ty->getPrimitiveID()) {
357 IMPLEMENT_BINARY_OPERATOR(%, UByte);
358 IMPLEMENT_BINARY_OPERATOR(%, SByte);
359 IMPLEMENT_BINARY_OPERATOR(%, UShort);
360 IMPLEMENT_BINARY_OPERATOR(%, Short);
361 IMPLEMENT_BINARY_OPERATOR(%, UInt);
362 IMPLEMENT_BINARY_OPERATOR(%, Int);
363 IMPLEMENT_BINARY_OPERATOR(%, ULong);
364 IMPLEMENT_BINARY_OPERATOR(%, Long);
365 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
366 case Type::FloatTyID:
367 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
368 break;
369 case Type::DoubleTyID:
370 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
371 break;
372 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000373 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000374 }
375 return Dest;
376}
377
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000378static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000379 const Type *Ty, ExecutionContext &SF) {
380 GenericValue Dest;
381 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000382 IMPLEMENT_BINARY_OPERATOR(&, UByte);
383 IMPLEMENT_BINARY_OPERATOR(&, SByte);
384 IMPLEMENT_BINARY_OPERATOR(&, UShort);
385 IMPLEMENT_BINARY_OPERATOR(&, Short);
386 IMPLEMENT_BINARY_OPERATOR(&, UInt);
387 IMPLEMENT_BINARY_OPERATOR(&, Int);
388 IMPLEMENT_BINARY_OPERATOR(&, ULong);
389 IMPLEMENT_BINARY_OPERATOR(&, Long);
390 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
391 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000392 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000393 }
394 return Dest;
395}
396
397
398static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
399 const Type *Ty, ExecutionContext &SF) {
400 GenericValue Dest;
401 switch (Ty->getPrimitiveID()) {
402 IMPLEMENT_BINARY_OPERATOR(|, UByte);
403 IMPLEMENT_BINARY_OPERATOR(|, SByte);
404 IMPLEMENT_BINARY_OPERATOR(|, UShort);
405 IMPLEMENT_BINARY_OPERATOR(|, Short);
406 IMPLEMENT_BINARY_OPERATOR(|, UInt);
407 IMPLEMENT_BINARY_OPERATOR(|, Int);
408 IMPLEMENT_BINARY_OPERATOR(|, ULong);
409 IMPLEMENT_BINARY_OPERATOR(|, Long);
410 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
411 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000412 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000413 }
414 return Dest;
415}
416
417
418static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
419 const Type *Ty, ExecutionContext &SF) {
420 GenericValue Dest;
421 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000422 IMPLEMENT_BINARY_OPERATOR(^, UByte);
423 IMPLEMENT_BINARY_OPERATOR(^, SByte);
424 IMPLEMENT_BINARY_OPERATOR(^, UShort);
425 IMPLEMENT_BINARY_OPERATOR(^, Short);
426 IMPLEMENT_BINARY_OPERATOR(^, UInt);
427 IMPLEMENT_BINARY_OPERATOR(^, Int);
428 IMPLEMENT_BINARY_OPERATOR(^, ULong);
429 IMPLEMENT_BINARY_OPERATOR(^, Long);
430 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
431 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000432 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000433 }
434 return Dest;
435}
436
437
Chris Lattner92101ac2001-08-23 17:05:04 +0000438#define IMPLEMENT_SETCC(OP, TY) \
439 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
440
Chris Lattner92101ac2001-08-23 17:05:04 +0000441static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
442 const Type *Ty, ExecutionContext &SF) {
443 GenericValue Dest;
444 switch (Ty->getPrimitiveID()) {
445 IMPLEMENT_SETCC(==, UByte);
446 IMPLEMENT_SETCC(==, SByte);
447 IMPLEMENT_SETCC(==, UShort);
448 IMPLEMENT_SETCC(==, Short);
449 IMPLEMENT_SETCC(==, UInt);
450 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000451 IMPLEMENT_SETCC(==, ULong);
452 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 IMPLEMENT_SETCC(==, Float);
454 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000455 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000456 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000457 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 }
459 return Dest;
460}
461
462static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
463 const Type *Ty, ExecutionContext &SF) {
464 GenericValue Dest;
465 switch (Ty->getPrimitiveID()) {
466 IMPLEMENT_SETCC(!=, UByte);
467 IMPLEMENT_SETCC(!=, SByte);
468 IMPLEMENT_SETCC(!=, UShort);
469 IMPLEMENT_SETCC(!=, Short);
470 IMPLEMENT_SETCC(!=, UInt);
471 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000472 IMPLEMENT_SETCC(!=, ULong);
473 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 IMPLEMENT_SETCC(!=, Float);
475 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000476 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000477
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000479 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 }
481 return Dest;
482}
483
484static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
485 const Type *Ty, ExecutionContext &SF) {
486 GenericValue Dest;
487 switch (Ty->getPrimitiveID()) {
488 IMPLEMENT_SETCC(<=, UByte);
489 IMPLEMENT_SETCC(<=, SByte);
490 IMPLEMENT_SETCC(<=, UShort);
491 IMPLEMENT_SETCC(<=, Short);
492 IMPLEMENT_SETCC(<=, UInt);
493 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000494 IMPLEMENT_SETCC(<=, ULong);
495 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 IMPLEMENT_SETCC(<=, Float);
497 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000498 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000500 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 }
502 return Dest;
503}
504
505static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
506 const Type *Ty, ExecutionContext &SF) {
507 GenericValue Dest;
508 switch (Ty->getPrimitiveID()) {
509 IMPLEMENT_SETCC(>=, UByte);
510 IMPLEMENT_SETCC(>=, SByte);
511 IMPLEMENT_SETCC(>=, UShort);
512 IMPLEMENT_SETCC(>=, Short);
513 IMPLEMENT_SETCC(>=, UInt);
514 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000515 IMPLEMENT_SETCC(>=, ULong);
516 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 IMPLEMENT_SETCC(>=, Float);
518 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000519 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000520 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000521 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000522 }
523 return Dest;
524}
525
526static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
527 const Type *Ty, ExecutionContext &SF) {
528 GenericValue Dest;
529 switch (Ty->getPrimitiveID()) {
530 IMPLEMENT_SETCC(<, UByte);
531 IMPLEMENT_SETCC(<, SByte);
532 IMPLEMENT_SETCC(<, UShort);
533 IMPLEMENT_SETCC(<, Short);
534 IMPLEMENT_SETCC(<, UInt);
535 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000536 IMPLEMENT_SETCC(<, ULong);
537 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 IMPLEMENT_SETCC(<, Float);
539 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000540 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000541 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000542 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000543 }
544 return Dest;
545}
546
547static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
548 const Type *Ty, ExecutionContext &SF) {
549 GenericValue Dest;
550 switch (Ty->getPrimitiveID()) {
551 IMPLEMENT_SETCC(>, UByte);
552 IMPLEMENT_SETCC(>, SByte);
553 IMPLEMENT_SETCC(>, UShort);
554 IMPLEMENT_SETCC(>, Short);
555 IMPLEMENT_SETCC(>, UInt);
556 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000557 IMPLEMENT_SETCC(>, ULong);
558 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 IMPLEMENT_SETCC(>, Float);
560 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000561 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000563 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000564 }
565 return Dest;
566}
567
568static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
569 const Type *Ty = I->getOperand(0)->getType();
570 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
571 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
572 GenericValue R; // Result
573
574 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000575 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
576 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
577 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
578 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
579 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000580 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
581 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000582 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000583 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
584 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
585 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
586 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
587 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
588 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
589 default:
590 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000591 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000592 }
593
594 SetValue(I, R, SF);
595}
596
Chris Lattner92101ac2001-08-23 17:05:04 +0000597//===----------------------------------------------------------------------===//
598// Terminator Instruction Implementations
599//===----------------------------------------------------------------------===//
600
Chris Lattnera95c6992001-11-12 16:28:48 +0000601static void PerformExitStuff() {
602#ifdef PROFILE_STRUCTURE_FIELDS
603 // Print out structure field accounting information...
604 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000605 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000606 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000607 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
608 for (; I != E; ++I) {
609 vector<unsigned> &OfC = I->second;
610 CW << " '" << (Value*)I->first << "'\t- Sum=";
611
612 unsigned Sum = 0;
613 for (unsigned i = 0; i < OfC.size(); ++i)
614 Sum += OfC[i];
615 CW << Sum << " - ";
616
617 for (unsigned i = 0; i < OfC.size(); ++i) {
618 if (i) CW << ", ";
619 CW << OfC[i];
620 }
Chris Lattner697954c2002-01-20 22:54:45 +0000621 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000622 }
Chris Lattner697954c2002-01-20 22:54:45 +0000623 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000624
625 CW << "Profile Field Access Percentages:\n";
626 cout.precision(3);
627 for (I = FieldAccessCounts.begin(); I != E; ++I) {
628 vector<unsigned> &OfC = I->second;
629 unsigned Sum = 0;
630 for (unsigned i = 0; i < OfC.size(); ++i)
631 Sum += OfC[i];
632
633 CW << " '" << (Value*)I->first << "'\t- ";
634 for (unsigned i = 0; i < OfC.size(); ++i) {
635 if (i) CW << ", ";
636 CW << double(OfC[i])/Sum;
637 }
Chris Lattner697954c2002-01-20 22:54:45 +0000638 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000639 }
Chris Lattner697954c2002-01-20 22:54:45 +0000640 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000641
Chris Lattnera95c6992001-11-12 16:28:48 +0000642 FieldAccessCounts.clear();
643 }
644#endif
645}
646
Chris Lattnere43db882001-10-27 04:15:57 +0000647void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000648 if (!QuietMode) {
649 cout << "Program returned ";
650 print(Type::IntTy, GV);
651 cout << " via 'void exit(int)'\n";
652 }
Chris Lattnere43db882001-10-27 04:15:57 +0000653
654 ExitCode = GV.SByteVal;
655 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000656 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000657}
658
Chris Lattner92101ac2001-08-23 17:05:04 +0000659void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
660 const Type *RetTy = 0;
661 GenericValue Result;
662
663 // Save away the return value... (if we are not 'ret void')
664 if (I->getNumOperands()) {
665 RetTy = I->getReturnValue()->getType();
666 Result = getOperandValue(I->getReturnValue(), SF);
667 }
668
669 // Save previously executing meth
670 const Method *M = ECStack.back().CurMethod;
671
672 // Pop the current stack frame... this invalidates SF
673 ECStack.pop_back();
674
675 if (ECStack.empty()) { // Finished main. Put result into exit code...
676 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000677 if (!QuietMode) {
678 CW << "Method " << M->getType() << " \"" << M->getName()
679 << "\" returned ";
680 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000681 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000682 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000683
684 if (RetTy->isIntegral())
685 ExitCode = Result.SByteVal; // Capture the exit code of the program
686 } else {
687 ExitCode = 0;
688 }
Chris Lattnere2409062001-11-12 16:19:45 +0000689
Chris Lattnera95c6992001-11-12 16:28:48 +0000690 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000691 return;
692 }
693
694 // If we have a previous stack frame, and we have a previous call, fill in
695 // the return value...
696 //
697 ExecutionContext &NewSF = ECStack.back();
698 if (NewSF.Caller) {
699 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
700 SetValue(NewSF.Caller, Result, NewSF);
701
702 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000703 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000704 // This must be a function that is executing because of a user 'call'
705 // instruction.
Chris Lattner5af0c482001-11-07 04:23:00 +0000706 CW << "Method " << M->getType() << " \"" << M->getName()
707 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000708 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000709 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000710 }
711}
712
713void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
714 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
715 BasicBlock *Dest;
716
717 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
718 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000719 Value *Cond = I->getCondition();
720 GenericValue CondVal = getOperandValue(Cond, SF);
721 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000722 Dest = I->getSuccessor(1);
723 }
724 SF.CurBB = Dest; // Update CurBB to branch destination
725 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
726}
727
728//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000729// Memory Instruction Implementations
730//===----------------------------------------------------------------------===//
731
Chris Lattner86660982001-08-27 05:16:50 +0000732void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
Chris Lattner7a176752001-12-04 00:03:30 +0000733 const Type *Ty = I->getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000734 unsigned NumElements = 1;
735
Chris Lattnerf23eb852001-12-14 16:49:29 +0000736 // FIXME: Malloc/Alloca should always have an argument!
Chris Lattner86660982001-08-27 05:16:50 +0000737 if (I->getNumOperands()) { // Allocating a unsized array type?
Chris Lattner86660982001-08-27 05:16:50 +0000738 // Get the number of elements being allocated by the array...
739 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
740 NumElements = NumEl.UIntVal;
741 }
742
743 // Allocate enough memory to hold the type...
744 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000745 // FIXME: Don't use CALLOC, use a tainted malloc.
746 Result.PointerVal = (PointerTy)calloc(NumElements, TD.getTypeSize(Ty));
747 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000748 SetValue(I, Result, SF);
749
750 if (I->getOpcode() == Instruction::Alloca) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000751 // TODO: FIXME: alloca should keep track of memory to free it later...
Chris Lattner86660982001-08-27 05:16:50 +0000752 }
753}
754
755static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
756 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
757 GenericValue Value = getOperandValue(I->getOperand(0), SF);
758 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000759 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000760}
761
Chris Lattner95c3af52001-10-29 19:32:19 +0000762
763// getElementOffset - The workhorse for getelementptr, load and store. This
764// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
765// the pointer type specified by argument Arg.
766//
Chris Lattner782b9392001-11-26 18:18:18 +0000767static PointerTy getElementOffset(MemAccessInst *I, ExecutionContext &SF) {
768 assert(isa<PointerType>(I->getPointerOperand()->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000769 "Cannot getElementOffset of a nonpointer type!");
770
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000771 PointerTy Total = 0;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000772 const Type *Ty = I->getPointerOperand()->getType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000773
Chris Lattner782b9392001-11-26 18:18:18 +0000774 unsigned ArgOff = I->getFirstIndexOperandNumber();
Chris Lattner95c3af52001-10-29 19:32:19 +0000775 while (ArgOff < I->getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000776 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
777 const StructLayout *SLO = TD.getStructLayout(STy);
778
779 // Indicies must be ubyte constants...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000780 const ConstantUInt *CPU = cast<ConstantUInt>(I->getOperand(ArgOff++));
Chris Lattner782b9392001-11-26 18:18:18 +0000781 assert(CPU->getType() == Type::UByteTy);
782 unsigned Index = CPU->getValue();
783
Chris Lattnere2409062001-11-12 16:19:45 +0000784#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000785 if (ProfileStructureFields) {
786 // Do accounting for this field...
787 vector<unsigned> &OfC = FieldAccessCounts[STy];
788 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
789 OfC[Index]++;
790 }
Chris Lattnere2409062001-11-12 16:19:45 +0000791#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000792
793 Total += SLO->MemberOffsets[Index];
794 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000795 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000796
Chris Lattner782b9392001-11-26 18:18:18 +0000797 // Get the index number for the array... which must be uint type...
798 assert(I->getOperand(ArgOff)->getType() == Type::UIntTy);
799 unsigned Idx = getOperandValue(I->getOperand(ArgOff++), SF).UIntVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000800 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000801 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000802 cerr << "Out of range memory access to element #" << Idx
803 << " of a " << AT->getNumElements() << " element array."
804 << " Subscript #" << (ArgOff-I->getFirstIndexOperandNumber())
805 << "\n";
806 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000807 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000808 }
Chris Lattner782b9392001-11-26 18:18:18 +0000809
Chris Lattnerf23eb852001-12-14 16:49:29 +0000810 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000811 unsigned Size = TD.getTypeSize(Ty);
812 Total += Size*Idx;
813 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000814 }
815
816 return Total;
817}
818
819static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000820 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000821 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000822
823 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000824 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000825 SetValue(I, Result, SF);
826}
827
Chris Lattner86660982001-08-27 05:16:50 +0000828static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000829 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000830 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000831 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000832 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000833
834 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000835 GenericValue Result;
836
837 switch (I->getType()->getPrimitiveID()) {
838 case Type::BoolTyID:
839 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000840 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000841 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000842 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000843 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000844 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000845 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000846 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
847 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
848 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
849 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000850 default:
851 cout << "Cannot load value of type " << I->getType() << "!\n";
852 }
853
854 SetValue(I, Result, SF);
855}
856
857static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000858 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000859 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000860 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000861
862 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000863 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000864
865 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
866 case Type::BoolTyID:
867 case Type::UByteTyID:
868 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
869 case Type::UShortTyID:
870 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
871 case Type::UIntTyID:
872 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000873 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000874 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
875 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000876 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
877 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000878 default:
879 cout << "Cannot store value of type " << I->getType() << "!\n";
880 }
881}
882
883
884//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000885// Miscellaneous Instruction Implementations
886//===----------------------------------------------------------------------===//
887
888void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
889 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000890 vector<GenericValue> ArgVals;
891 ArgVals.reserve(I->getNumOperands()-1);
892 for (unsigned i = 1; i < I->getNumOperands(); ++i)
893 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
894
Chris Lattner070cf5e2001-11-07 20:12:30 +0000895 // To handle indirect calls, we must get the pointer value from the argument
896 // and treat it as a method pointer.
897 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
898
899 callMethod((Method*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000900}
901
902static void executePHINode(PHINode *I, ExecutionContext &SF) {
903 BasicBlock *PrevBB = SF.PrevBB;
904 Value *IncomingValue = 0;
905
906 // Search for the value corresponding to this previous bb...
907 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
908 if (I->getIncomingBlock(--i) == PrevBB) {
909 IncomingValue = I->getIncomingValue(i);
910 break;
911 }
912 }
913 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
914
915 // Found the value, set as the result...
916 SetValue(I, getOperandValue(IncomingValue, SF), SF);
917}
918
Chris Lattner86660982001-08-27 05:16:50 +0000919#define IMPLEMENT_SHIFT(OP, TY) \
920 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
921
922static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
923 const Type *Ty = I->getOperand(0)->getType();
924 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
925 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
926 GenericValue Dest;
927
928 switch (Ty->getPrimitiveID()) {
929 IMPLEMENT_SHIFT(<<, UByte);
930 IMPLEMENT_SHIFT(<<, SByte);
931 IMPLEMENT_SHIFT(<<, UShort);
932 IMPLEMENT_SHIFT(<<, Short);
933 IMPLEMENT_SHIFT(<<, UInt);
934 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000935 IMPLEMENT_SHIFT(<<, ULong);
936 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000937 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000938 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000939 }
940 SetValue(I, Dest, SF);
941}
942
943static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
944 const Type *Ty = I->getOperand(0)->getType();
945 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
946 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
947 GenericValue Dest;
948
949 switch (Ty->getPrimitiveID()) {
950 IMPLEMENT_SHIFT(>>, UByte);
951 IMPLEMENT_SHIFT(>>, SByte);
952 IMPLEMENT_SHIFT(>>, UShort);
953 IMPLEMENT_SHIFT(>>, Short);
954 IMPLEMENT_SHIFT(>>, UInt);
955 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000956 IMPLEMENT_SHIFT(>>, ULong);
957 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000958 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000959 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000960 }
961 SetValue(I, Dest, SF);
962}
963
964#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000965 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000966
967#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
968 case Type::DESTTY##TyID: \
969 switch (SrcTy->getPrimitiveID()) { \
970 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
971 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
972 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
973 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
974 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000975 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
976 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000977 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000979
980#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
983
984#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +0000985 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +0000986 break; \
987 } \
988 break
989
990#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
991 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
992 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000993 IMPLEMENT_CAST_CASE_END()
994
995static void executeCastInst(CastInst *I, ExecutionContext &SF) {
996 const Type *Ty = I->getType();
997 const Type *SrcTy = I->getOperand(0)->getType();
998 GenericValue Src = getOperandValue(I->getOperand(0), SF);
999 GenericValue Dest;
1000
1001 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001002 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1003 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1004 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1005 IMPLEMENT_CAST_CASE(Short , ( signed char));
1006 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1007 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1008 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1009 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1010 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1011 IMPLEMENT_CAST_CASE(Float , (float));
1012 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001013 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001015 }
1016 SetValue(I, Dest, SF);
1017}
Chris Lattner92101ac2001-08-23 17:05:04 +00001018
1019
1020
1021
1022//===----------------------------------------------------------------------===//
1023// Dispatch and Execution Code
1024//===----------------------------------------------------------------------===//
1025
1026MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
1027 // Assign slot numbers to the method arguments...
1028 const Method::ArgumentListType &ArgList = M->getArgumentList();
1029 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
1030 AE = ArgList.end(); AI != AE; ++AI) {
1031 MethodArgument *MA = *AI;
1032 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
1033 }
1034
1035 // Iterate over all of the instructions...
1036 unsigned InstNum = 0;
1037 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
1038 MI != ME; ++MI) {
1039 Instruction *I = *MI; // For each instruction...
1040 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
1041 }
1042}
1043
1044unsigned MethodInfo::getValueSlot(const Value *V) {
1045 unsigned Plane = V->getType()->getUniqueID();
1046 if (Plane >= NumPlaneElements.size())
1047 NumPlaneElements.resize(Plane+1, 0);
1048 return NumPlaneElements[Plane]++;
1049}
1050
1051
Chris Lattner92101ac2001-08-23 17:05:04 +00001052//===----------------------------------------------------------------------===//
1053// callMethod - Execute the specified method...
1054//
Chris Lattner365a76e2001-09-10 04:49:44 +00001055void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
1056 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1057 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1058 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001059 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001060 GenericValue Result = callExternalMethod(M, ArgVals);
1061 const Type *RetTy = M->getReturnType();
1062
1063 // Copy the result back into the result variable if we are not returning
1064 // void.
1065 if (RetTy != Type::VoidTy) {
1066 if (!ECStack.empty() && ECStack.back().Caller) {
1067 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001068 SetValue(SF.Caller, Result, SF);
1069
1070 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001071 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001072 // print it.
Chris Lattner5af0c482001-11-07 04:23:00 +00001073 CW << "Method " << M->getType() << " \"" << M->getName()
1074 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001075 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001076 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001077
1078 if (RetTy->isIntegral())
1079 ExitCode = Result.SByteVal; // Capture the exit code of the program
1080 }
1081 }
1082
Chris Lattner92101ac2001-08-23 17:05:04 +00001083 return;
1084 }
1085
1086 // Process the method, assigning instruction numbers to the instructions in
1087 // the method. Also calculate the number of values for each type slot active.
1088 //
1089 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001090 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001091
Chris Lattner92101ac2001-08-23 17:05:04 +00001092 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1093 StackFrame.CurMethod = M;
1094 StackFrame.CurBB = M->front();
1095 StackFrame.CurInst = StackFrame.CurBB->begin();
1096 StackFrame.MethInfo = MethInfo;
1097
1098 // Initialize the values to nothing...
1099 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001100 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001101 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1102
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001103 // Taint the initial values of stuff
1104 memset(&StackFrame.Values[i][0], 42,
1105 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1106 }
1107
Chris Lattner92101ac2001-08-23 17:05:04 +00001108 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1109
Chris Lattner92101ac2001-08-23 17:05:04 +00001110
Chris Lattner365a76e2001-09-10 04:49:44 +00001111 // Run through the method arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001112 assert(ArgVals.size() == M->getArgumentList().size() &&
1113 "Invalid number of values passed to method invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001114 unsigned i = 0;
1115 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
1116 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
1117 SetValue(*MI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001118 }
1119}
1120
1121// executeInstruction - Interpret a single instruction, increment the "PC", and
1122// return true if the next instruction is a breakpoint...
1123//
1124bool Interpreter::executeInstruction() {
1125 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1126
1127 ExecutionContext &SF = ECStack.back(); // Current stack frame
1128 Instruction *I = *SF.CurInst++; // Increment before execute
1129
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001130 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001131 CW << "Run:" << I;
1132
1133 // Set a sigsetjmp buffer so that we can recover if an error happens during
1134 // instruction execution...
1135 //
1136 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1137 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001138 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001139 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001140 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001141 // If -abort-on-exception was specified, terminate LLI instead of trying
1142 // to debug it.
1143 //
1144 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001145 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001146 cout << "CTRL-C Detected, execution halted.\n";
1147 }
1148 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001149 return true;
1150 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001151
Chris Lattner461f02f2001-11-07 05:31:27 +00001152 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001153 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001154 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001155 } else {
1156 switch (I->getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001157 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001158 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1159 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001160 // Memory Instructions
1161 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001162 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1163 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1164 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1165 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001166 case Instruction::GetElementPtr:
1167 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001168
1169 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001170 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1171 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1172 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1173 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1174 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001175 default:
1176 cout << "Don't know how to execute this instruction!\n-->" << I;
1177 }
1178 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001179 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001180
1181 // Reset the current frame location to the top of stack
1182 CurFrame = ECStack.size()-1;
1183
1184 if (CurFrame == -1) return false; // No breakpoint if no code
1185
1186 // Return true if there is a breakpoint annotation on the instruction...
1187 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1188}
1189
1190void Interpreter::stepInstruction() { // Do the 'step' command
1191 if (ECStack.empty()) {
1192 cout << "Error: no program running, cannot step!\n";
1193 return;
1194 }
1195
1196 // Run an instruction...
1197 executeInstruction();
1198
1199 // Print the next instruction to execute...
1200 printCurrentInstruction();
1201}
1202
1203// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001204void Interpreter::nextInstruction() { // Do the 'next' command
1205 if (ECStack.empty()) {
1206 cout << "Error: no program running, cannot 'next'!\n";
1207 return;
1208 }
1209
1210 // If this is a call instruction, step over the call instruction...
1211 // TODO: ICALL, CALL WITH, ...
1212 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001213 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001214 // Step into the function...
1215 if (executeInstruction()) {
1216 // Hit a breakpoint, print current instruction, then return to user...
1217 cout << "Breakpoint hit!\n";
1218 printCurrentInstruction();
1219 return;
1220 }
1221
Chris Lattnera74a6b52001-10-29 14:08:33 +00001222 // If we we able to step into the function, finish it now. We might not be
1223 // able the step into a function, if it's external for example.
1224 if (ECStack.size() != StackSize)
1225 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001226 else
1227 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001228
Chris Lattner92101ac2001-08-23 17:05:04 +00001229 } else {
1230 // Normal instruction, just step...
1231 stepInstruction();
1232 }
1233}
1234
1235void Interpreter::run() {
1236 if (ECStack.empty()) {
1237 cout << "Error: no program running, cannot run!\n";
1238 return;
1239 }
1240
1241 bool HitBreakpoint = false;
1242 while (!ECStack.empty() && !HitBreakpoint) {
1243 // Run an instruction...
1244 HitBreakpoint = executeInstruction();
1245 }
1246
1247 if (HitBreakpoint) {
1248 cout << "Breakpoint hit!\n";
1249 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001250 // Print the next instruction to execute...
1251 printCurrentInstruction();
1252}
1253
1254void Interpreter::finish() {
1255 if (ECStack.empty()) {
1256 cout << "Error: no program running, cannot run!\n";
1257 return;
1258 }
1259
1260 unsigned StackSize = ECStack.size();
1261 bool HitBreakpoint = false;
1262 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1263 // Run an instruction...
1264 HitBreakpoint = executeInstruction();
1265 }
1266
1267 if (HitBreakpoint) {
1268 cout << "Breakpoint hit!\n";
1269 }
1270
1271 // Print the next instruction to execute...
1272 printCurrentInstruction();
1273}
1274
1275
1276
1277// printCurrentInstruction - Print out the instruction that the virtual PC is
1278// at, or fail silently if no program is running.
1279//
1280void Interpreter::printCurrentInstruction() {
1281 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001282 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1283 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1284
Chris Lattner92101ac2001-08-23 17:05:04 +00001285 Instruction *I = *ECStack.back().CurInst;
1286 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1287 assert(IN && "Instruction has no numbering annotation!");
1288 cout << "#" << IN->InstNum << I;
1289 }
1290}
1291
1292void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001293 switch (Ty->getPrimitiveID()) {
1294 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1295 case Type::SByteTyID: cout << V.SByteVal; break;
1296 case Type::UByteTyID: cout << V.UByteVal; break;
1297 case Type::ShortTyID: cout << V.ShortVal; break;
1298 case Type::UShortTyID: cout << V.UShortVal; break;
1299 case Type::IntTyID: cout << V.IntVal; break;
1300 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001301 case Type::LongTyID: cout << (long)V.LongVal; break;
1302 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001303 case Type::FloatTyID: cout << V.FloatVal; break;
1304 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001305 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001306 default:
1307 cout << "- Don't know how to print value of this type!";
1308 break;
1309 }
1310}
1311
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001312void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001313 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001314 printValue(Ty, V);
1315}
1316
Chris Lattner697954c2002-01-20 22:54:45 +00001317void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001318 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1319 if (!PickedVal) return;
1320
Chris Lattner9636a912001-10-01 16:18:37 +00001321 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001322 CW << M; // Print the method
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001323 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001324 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001325 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1326 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001327 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001328 print(PickedVal->getType(),
1329 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001330 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001331 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001332}
1333
Chris Lattner697954c2002-01-20 22:54:45 +00001334void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001335 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1336 if (!PickedVal) return;
1337
1338 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001339 print(PickedVal->getType(),
1340 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001341 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001342 printOperandInfo(PickedVal, ECStack[CurFrame]);
1343}
1344
Chris Lattner461f02f2001-11-07 05:31:27 +00001345// printStackFrame - Print information about the specified stack frame, or -1
1346// for the default one.
1347//
1348void Interpreter::printStackFrame(int FrameNo = -1) {
1349 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner461f02f2001-11-07 05:31:27 +00001350 Method *Meth = ECStack[FrameNo].CurMethod;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001351 const Type *RetTy = Meth->getReturnType();
1352
1353 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1354 << (Value*)RetTy << " \"" << Meth->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001355
1356 Method::ArgumentListType &Args = Meth->getArgumentList();
1357 for (unsigned i = 0; i < Args.size(); ++i) {
1358 if (i != 0) cout << ", ";
1359 CW << (Value*)Args[i] << "=";
1360
1361 printValue(Args[i]->getType(), getOperandValue(Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001362 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001363
Chris Lattner697954c2002-01-20 22:54:45 +00001364 cout << ")\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001365 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001366}
Chris Lattner461f02f2001-11-07 05:31:27 +00001367