blob: 68379f4e168b771b6913bbc133906684d8d630e5 [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 Lattnere2cbbce2002-04-29 18:56:45 +000013#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.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 Lattner0b12b5f2002-06-25 16:13:21 +0000171static void InitializeMemory(const 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 Lattner0b12b5f2002-06-25 16:13:21 +0000193 const 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 Lattner0b12b5f2002-06-25 16:13:21 +0000203 const 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 Lattner0b12b5f2002-06-25 16:13:21 +0000215 } else if (const ConstantPointerRef *CPR =
216 dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000217 GlobalAddress *Address =
218 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
219 *(void**)Addr = (GenericValue*)Address->Ptr;
220 } else {
221 assert(0 && "Unknown Constant pointer type!");
222 }
223 return;
224
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000225 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000226 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000227 assert(0 && "Unknown constant type to initialize memory with!");
228 }
229}
230
231Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
232 assert(AID == GlobalAddressAID);
233
234 // This annotation will only be created on GlobalValue objects...
235 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
236
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000237 if (isa<Function>(GVal)) {
238 // The GlobalAddress object for a function is just a pointer to function
239 // itself. Don't delete it when the annotation is gone though!
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000240 return new GlobalAddress(GVal, false);
241 }
242
243 // Handle the case of a global variable...
244 assert(isa<GlobalVariable>(GVal) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000245 "Global value found that isn't a function or global variable!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000246 GlobalVariable *GV = cast<GlobalVariable>(GVal);
247
248 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000249 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000250
251 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000252 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000253 assert(Addr != 0 && "Null pointer returned by malloc!");
254
255 // Initialize the memory if there is an initializer...
256 if (GV->hasInitializer())
257 InitializeMemory(GV->getInitializer(), (char*)Addr);
258
259 return new GlobalAddress(Addr, true); // Simply invoke the ctor
260}
261
Chris Lattner92101ac2001-08-23 17:05:04 +0000262
263//===----------------------------------------------------------------------===//
Chris Lattner2adcd832002-05-03 19:52:30 +0000264// Unary Instruction Implementations
265//===----------------------------------------------------------------------===//
266
267#define IMPLEMENT_UNARY_OPERATOR(OP, TY) \
268 case Type::TY##TyID: Dest.TY##Val = OP Src.TY##Val; break
269
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000270static void executeNotInst(UnaryOperator &I, ExecutionContext &SF) {
271 const Type *Ty = I.getOperand(0)->getType();
272 GenericValue Src = getOperandValue(I.getOperand(0), SF);
Chris Lattner2adcd832002-05-03 19:52:30 +0000273 GenericValue Dest;
274 switch (Ty->getPrimitiveID()) {
275 IMPLEMENT_UNARY_OPERATOR(~, UByte);
276 IMPLEMENT_UNARY_OPERATOR(~, SByte);
277 IMPLEMENT_UNARY_OPERATOR(~, UShort);
278 IMPLEMENT_UNARY_OPERATOR(~, Short);
279 IMPLEMENT_UNARY_OPERATOR(~, UInt);
280 IMPLEMENT_UNARY_OPERATOR(~, Int);
281 IMPLEMENT_UNARY_OPERATOR(~, ULong);
282 IMPLEMENT_UNARY_OPERATOR(~, Long);
283 IMPLEMENT_UNARY_OPERATOR(~, Pointer);
284 default:
285 cout << "Unhandled type for Not instruction: " << Ty << "\n";
286 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000287 SetValue(&I, Dest, SF);
Chris Lattner2adcd832002-05-03 19:52:30 +0000288}
289
290//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000291// Binary Instruction Implementations
292//===----------------------------------------------------------------------===//
293
294#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
295 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
296
297static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
298 const Type *Ty, ExecutionContext &SF) {
299 GenericValue Dest;
300 switch (Ty->getPrimitiveID()) {
301 IMPLEMENT_BINARY_OPERATOR(+, UByte);
302 IMPLEMENT_BINARY_OPERATOR(+, SByte);
303 IMPLEMENT_BINARY_OPERATOR(+, UShort);
304 IMPLEMENT_BINARY_OPERATOR(+, Short);
305 IMPLEMENT_BINARY_OPERATOR(+, UInt);
306 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000307 IMPLEMENT_BINARY_OPERATOR(+, ULong);
308 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000309 IMPLEMENT_BINARY_OPERATOR(+, Float);
310 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000311 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000312 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000313 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000314 }
315 return Dest;
316}
317
318static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
319 const Type *Ty, ExecutionContext &SF) {
320 GenericValue Dest;
321 switch (Ty->getPrimitiveID()) {
322 IMPLEMENT_BINARY_OPERATOR(-, UByte);
323 IMPLEMENT_BINARY_OPERATOR(-, SByte);
324 IMPLEMENT_BINARY_OPERATOR(-, UShort);
325 IMPLEMENT_BINARY_OPERATOR(-, Short);
326 IMPLEMENT_BINARY_OPERATOR(-, UInt);
327 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000328 IMPLEMENT_BINARY_OPERATOR(-, ULong);
329 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000330 IMPLEMENT_BINARY_OPERATOR(-, Float);
331 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000332 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000333 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000334 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000335 }
336 return Dest;
337}
338
Chris Lattnerc2593162001-10-27 08:28:11 +0000339static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
340 const Type *Ty, ExecutionContext &SF) {
341 GenericValue Dest;
342 switch (Ty->getPrimitiveID()) {
343 IMPLEMENT_BINARY_OPERATOR(*, UByte);
344 IMPLEMENT_BINARY_OPERATOR(*, SByte);
345 IMPLEMENT_BINARY_OPERATOR(*, UShort);
346 IMPLEMENT_BINARY_OPERATOR(*, Short);
347 IMPLEMENT_BINARY_OPERATOR(*, UInt);
348 IMPLEMENT_BINARY_OPERATOR(*, Int);
349 IMPLEMENT_BINARY_OPERATOR(*, ULong);
350 IMPLEMENT_BINARY_OPERATOR(*, Long);
351 IMPLEMENT_BINARY_OPERATOR(*, Float);
352 IMPLEMENT_BINARY_OPERATOR(*, Double);
353 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
354 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000355 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000356 }
357 return Dest;
358}
359
360static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
361 const Type *Ty, ExecutionContext &SF) {
362 GenericValue Dest;
363 switch (Ty->getPrimitiveID()) {
364 IMPLEMENT_BINARY_OPERATOR(/, UByte);
365 IMPLEMENT_BINARY_OPERATOR(/, SByte);
366 IMPLEMENT_BINARY_OPERATOR(/, UShort);
367 IMPLEMENT_BINARY_OPERATOR(/, Short);
368 IMPLEMENT_BINARY_OPERATOR(/, UInt);
369 IMPLEMENT_BINARY_OPERATOR(/, Int);
370 IMPLEMENT_BINARY_OPERATOR(/, ULong);
371 IMPLEMENT_BINARY_OPERATOR(/, Long);
372 IMPLEMENT_BINARY_OPERATOR(/, Float);
373 IMPLEMENT_BINARY_OPERATOR(/, Double);
374 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
375 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000376 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000377 }
378 return Dest;
379}
380
381static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
382 const Type *Ty, ExecutionContext &SF) {
383 GenericValue Dest;
384 switch (Ty->getPrimitiveID()) {
385 IMPLEMENT_BINARY_OPERATOR(%, UByte);
386 IMPLEMENT_BINARY_OPERATOR(%, SByte);
387 IMPLEMENT_BINARY_OPERATOR(%, UShort);
388 IMPLEMENT_BINARY_OPERATOR(%, Short);
389 IMPLEMENT_BINARY_OPERATOR(%, UInt);
390 IMPLEMENT_BINARY_OPERATOR(%, Int);
391 IMPLEMENT_BINARY_OPERATOR(%, ULong);
392 IMPLEMENT_BINARY_OPERATOR(%, Long);
393 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
394 case Type::FloatTyID:
395 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
396 break;
397 case Type::DoubleTyID:
398 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
399 break;
400 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000401 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000402 }
403 return Dest;
404}
405
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000406static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000407 const Type *Ty, ExecutionContext &SF) {
408 GenericValue Dest;
409 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000410 IMPLEMENT_BINARY_OPERATOR(&, UByte);
411 IMPLEMENT_BINARY_OPERATOR(&, SByte);
412 IMPLEMENT_BINARY_OPERATOR(&, UShort);
413 IMPLEMENT_BINARY_OPERATOR(&, Short);
414 IMPLEMENT_BINARY_OPERATOR(&, UInt);
415 IMPLEMENT_BINARY_OPERATOR(&, Int);
416 IMPLEMENT_BINARY_OPERATOR(&, ULong);
417 IMPLEMENT_BINARY_OPERATOR(&, Long);
418 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
419 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000420 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000421 }
422 return Dest;
423}
424
425
426static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
427 const Type *Ty, ExecutionContext &SF) {
428 GenericValue Dest;
429 switch (Ty->getPrimitiveID()) {
430 IMPLEMENT_BINARY_OPERATOR(|, UByte);
431 IMPLEMENT_BINARY_OPERATOR(|, SByte);
432 IMPLEMENT_BINARY_OPERATOR(|, UShort);
433 IMPLEMENT_BINARY_OPERATOR(|, Short);
434 IMPLEMENT_BINARY_OPERATOR(|, UInt);
435 IMPLEMENT_BINARY_OPERATOR(|, Int);
436 IMPLEMENT_BINARY_OPERATOR(|, ULong);
437 IMPLEMENT_BINARY_OPERATOR(|, Long);
438 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
439 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000440 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000441 }
442 return Dest;
443}
444
445
446static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
447 const Type *Ty, ExecutionContext &SF) {
448 GenericValue Dest;
449 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000450 IMPLEMENT_BINARY_OPERATOR(^, UByte);
451 IMPLEMENT_BINARY_OPERATOR(^, SByte);
452 IMPLEMENT_BINARY_OPERATOR(^, UShort);
453 IMPLEMENT_BINARY_OPERATOR(^, Short);
454 IMPLEMENT_BINARY_OPERATOR(^, UInt);
455 IMPLEMENT_BINARY_OPERATOR(^, Int);
456 IMPLEMENT_BINARY_OPERATOR(^, ULong);
457 IMPLEMENT_BINARY_OPERATOR(^, Long);
458 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
459 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000460 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000461 }
462 return Dest;
463}
464
465
Chris Lattner92101ac2001-08-23 17:05:04 +0000466#define IMPLEMENT_SETCC(OP, TY) \
467 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
468
Chris Lattner92101ac2001-08-23 17:05:04 +0000469static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
470 const Type *Ty, ExecutionContext &SF) {
471 GenericValue Dest;
472 switch (Ty->getPrimitiveID()) {
473 IMPLEMENT_SETCC(==, UByte);
474 IMPLEMENT_SETCC(==, SByte);
475 IMPLEMENT_SETCC(==, UShort);
476 IMPLEMENT_SETCC(==, Short);
477 IMPLEMENT_SETCC(==, UInt);
478 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000479 IMPLEMENT_SETCC(==, ULong);
480 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 IMPLEMENT_SETCC(==, Float);
482 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000483 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000484 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000485 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000486 }
487 return Dest;
488}
489
490static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
491 const Type *Ty, ExecutionContext &SF) {
492 GenericValue Dest;
493 switch (Ty->getPrimitiveID()) {
494 IMPLEMENT_SETCC(!=, UByte);
495 IMPLEMENT_SETCC(!=, SByte);
496 IMPLEMENT_SETCC(!=, UShort);
497 IMPLEMENT_SETCC(!=, Short);
498 IMPLEMENT_SETCC(!=, UInt);
499 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000500 IMPLEMENT_SETCC(!=, ULong);
501 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 IMPLEMENT_SETCC(!=, Float);
503 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000504 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000505
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000507 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 }
509 return Dest;
510}
511
512static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
513 const Type *Ty, ExecutionContext &SF) {
514 GenericValue Dest;
515 switch (Ty->getPrimitiveID()) {
516 IMPLEMENT_SETCC(<=, UByte);
517 IMPLEMENT_SETCC(<=, SByte);
518 IMPLEMENT_SETCC(<=, UShort);
519 IMPLEMENT_SETCC(<=, Short);
520 IMPLEMENT_SETCC(<=, UInt);
521 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000522 IMPLEMENT_SETCC(<=, ULong);
523 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000524 IMPLEMENT_SETCC(<=, Float);
525 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000526 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000528 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000529 }
530 return Dest;
531}
532
533static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
534 const Type *Ty, ExecutionContext &SF) {
535 GenericValue Dest;
536 switch (Ty->getPrimitiveID()) {
537 IMPLEMENT_SETCC(>=, UByte);
538 IMPLEMENT_SETCC(>=, SByte);
539 IMPLEMENT_SETCC(>=, UShort);
540 IMPLEMENT_SETCC(>=, Short);
541 IMPLEMENT_SETCC(>=, UInt);
542 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000543 IMPLEMENT_SETCC(>=, ULong);
544 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000545 IMPLEMENT_SETCC(>=, Float);
546 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000547 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000548 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000549 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000550 }
551 return Dest;
552}
553
554static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
555 const Type *Ty, ExecutionContext &SF) {
556 GenericValue Dest;
557 switch (Ty->getPrimitiveID()) {
558 IMPLEMENT_SETCC(<, UByte);
559 IMPLEMENT_SETCC(<, SByte);
560 IMPLEMENT_SETCC(<, UShort);
561 IMPLEMENT_SETCC(<, Short);
562 IMPLEMENT_SETCC(<, UInt);
563 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000564 IMPLEMENT_SETCC(<, ULong);
565 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000566 IMPLEMENT_SETCC(<, Float);
567 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000568 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000569 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000570 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000571 }
572 return Dest;
573}
574
575static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
576 const Type *Ty, ExecutionContext &SF) {
577 GenericValue Dest;
578 switch (Ty->getPrimitiveID()) {
579 IMPLEMENT_SETCC(>, UByte);
580 IMPLEMENT_SETCC(>, SByte);
581 IMPLEMENT_SETCC(>, UShort);
582 IMPLEMENT_SETCC(>, Short);
583 IMPLEMENT_SETCC(>, UInt);
584 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000585 IMPLEMENT_SETCC(>, ULong);
586 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000587 IMPLEMENT_SETCC(>, Float);
588 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000589 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000590 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000591 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000592 }
593 return Dest;
594}
595
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000596static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
597 const Type *Ty = I.getOperand(0)->getType();
598 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
599 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000600 GenericValue R; // Result
601
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000602 switch (I.getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000603 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
604 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
605 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
606 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
607 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000608 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
609 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000610 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000611 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
612 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
613 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
614 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
615 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
616 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
617 default:
618 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000619 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000620 }
621
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000622 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000623}
624
Chris Lattner92101ac2001-08-23 17:05:04 +0000625//===----------------------------------------------------------------------===//
626// Terminator Instruction Implementations
627//===----------------------------------------------------------------------===//
628
Chris Lattnera95c6992001-11-12 16:28:48 +0000629static void PerformExitStuff() {
630#ifdef PROFILE_STRUCTURE_FIELDS
631 // Print out structure field accounting information...
632 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000633 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000634 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000635 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
636 for (; I != E; ++I) {
637 vector<unsigned> &OfC = I->second;
638 CW << " '" << (Value*)I->first << "'\t- Sum=";
639
640 unsigned Sum = 0;
641 for (unsigned i = 0; i < OfC.size(); ++i)
642 Sum += OfC[i];
643 CW << Sum << " - ";
644
645 for (unsigned i = 0; i < OfC.size(); ++i) {
646 if (i) CW << ", ";
647 CW << OfC[i];
648 }
Chris Lattner697954c2002-01-20 22:54:45 +0000649 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000650 }
Chris Lattner697954c2002-01-20 22:54:45 +0000651 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000652
653 CW << "Profile Field Access Percentages:\n";
654 cout.precision(3);
655 for (I = FieldAccessCounts.begin(); I != E; ++I) {
656 vector<unsigned> &OfC = I->second;
657 unsigned Sum = 0;
658 for (unsigned i = 0; i < OfC.size(); ++i)
659 Sum += OfC[i];
660
661 CW << " '" << (Value*)I->first << "'\t- ";
662 for (unsigned i = 0; i < OfC.size(); ++i) {
663 if (i) CW << ", ";
664 CW << double(OfC[i])/Sum;
665 }
Chris Lattner697954c2002-01-20 22:54:45 +0000666 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000667 }
Chris Lattner697954c2002-01-20 22:54:45 +0000668 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000669
Chris Lattnera95c6992001-11-12 16:28:48 +0000670 FieldAccessCounts.clear();
671 }
672#endif
673}
674
Chris Lattnere43db882001-10-27 04:15:57 +0000675void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000676 if (!QuietMode) {
677 cout << "Program returned ";
678 print(Type::IntTy, GV);
679 cout << " via 'void exit(int)'\n";
680 }
Chris Lattnere43db882001-10-27 04:15:57 +0000681
682 ExitCode = GV.SByteVal;
683 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000684 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000685}
686
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000687void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000688 const Type *RetTy = 0;
689 GenericValue Result;
690
691 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000692 if (I.getNumOperands()) {
693 RetTy = I.getReturnValue()->getType();
694 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000695 }
696
697 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000698 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000699
700 // Pop the current stack frame... this invalidates SF
701 ECStack.pop_back();
702
703 if (ECStack.empty()) { // Finished main. Put result into exit code...
704 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000705 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000706 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000707 << "\" returned ";
708 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000709 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000710 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000711
712 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000713 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000714 } else {
715 ExitCode = 0;
716 }
Chris Lattnere2409062001-11-12 16:19:45 +0000717
Chris Lattnera95c6992001-11-12 16:28:48 +0000718 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000719 return;
720 }
721
722 // If we have a previous stack frame, and we have a previous call, fill in
723 // the return value...
724 //
725 ExecutionContext &NewSF = ECStack.back();
726 if (NewSF.Caller) {
727 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
728 SetValue(NewSF.Caller, Result, NewSF);
729
730 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000731 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000732 // This must be a function that is executing because of a user 'call'
733 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000734 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000735 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000736 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000737 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000738 }
739}
740
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000741void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000742 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
743 BasicBlock *Dest;
744
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000745 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
746 if (!I.isUnconditional()) {
747 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000748 GenericValue CondVal = getOperandValue(Cond, SF);
749 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000750 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000751 }
752 SF.CurBB = Dest; // Update CurBB to branch destination
753 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
754}
755
756//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000757// Memory Instruction Implementations
758//===----------------------------------------------------------------------===//
759
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000760void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
761 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000762
Chris Lattnercc82cc12002-04-28 21:57:33 +0000763 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000764 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000765
766 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000767 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000768 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
769
770 GenericValue Result;
771 Result.PointerVal = (PointerTy)Memory;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000772 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000773 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000774
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000775 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000776 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000777}
778
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000779static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
780 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
781 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000782 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000783 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000784}
785
Chris Lattner95c3af52001-10-29 19:32:19 +0000786
787// getElementOffset - The workhorse for getelementptr, load and store. This
788// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
789// the pointer type specified by argument Arg.
790//
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000791static PointerTy getElementOffset(MemAccessInst &I, ExecutionContext &SF) {
792 assert(isa<PointerType>(I.getPointerOperand()->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000793 "Cannot getElementOffset of a nonpointer type!");
794
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000795 PointerTy Total = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000796 const Type *Ty = I.getPointerOperand()->getType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000797
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000798 unsigned ArgOff = I.getFirstIndexOperandNumber();
799 while (ArgOff < I.getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000800 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
801 const StructLayout *SLO = TD.getStructLayout(STy);
802
803 // Indicies must be ubyte constants...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000804 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand(ArgOff++));
Chris Lattner782b9392001-11-26 18:18:18 +0000805 assert(CPU->getType() == Type::UByteTy);
806 unsigned Index = CPU->getValue();
807
Chris Lattnere2409062001-11-12 16:19:45 +0000808#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000809 if (ProfileStructureFields) {
810 // Do accounting for this field...
811 vector<unsigned> &OfC = FieldAccessCounts[STy];
812 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
813 OfC[Index]++;
814 }
Chris Lattnere2409062001-11-12 16:19:45 +0000815#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000816
817 Total += SLO->MemberOffsets[Index];
818 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000819 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000820
Chris Lattner782b9392001-11-26 18:18:18 +0000821 // Get the index number for the array... which must be uint type...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000822 assert(I.getOperand(ArgOff)->getType() == Type::UIntTy);
823 unsigned Idx = getOperandValue(I.getOperand(ArgOff++), SF).UIntVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000824 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000825 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000826 cerr << "Out of range memory access to element #" << Idx
827 << " of a " << AT->getNumElements() << " element array."
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000828 << " Subscript #" << (ArgOff-I.getFirstIndexOperandNumber())
Chris Lattnerf23eb852001-12-14 16:49:29 +0000829 << "\n";
830 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000831 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000832 }
Chris Lattner782b9392001-11-26 18:18:18 +0000833
Chris Lattnerf23eb852001-12-14 16:49:29 +0000834 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000835 unsigned Size = TD.getTypeSize(Ty);
836 Total += Size*Idx;
837 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000838 }
839
840 return Total;
841}
842
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000843static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
844 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000845 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000846
847 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000848 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000849 SetValue(&I, Result, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000850}
851
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000852static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
853 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000854 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000855 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000856 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000857
858 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000859 GenericValue Result;
860
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000861 switch (I.getType()->getPrimitiveID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000862 case Type::BoolTyID:
863 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000864 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000865 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000866 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000867 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000868 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000869 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000870 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
871 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
872 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
873 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000874 default:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000875 cout << "Cannot load value of type " << I.getType() << "!\n";
Chris Lattner86660982001-08-27 05:16:50 +0000876 }
877
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000878 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000879}
880
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000881static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
882 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000883 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000884 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000885
886 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000887 GenericValue Val = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000888
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000889 switch (I.getOperand(0)->getType()->getPrimitiveID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000890 case Type::BoolTyID:
891 case Type::UByteTyID:
892 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
893 case Type::UShortTyID:
894 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
895 case Type::UIntTyID:
896 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000897 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000898 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
899 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000900 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
901 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000902 default:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000903 cout << "Cannot store value of type " << I.getType() << "!\n";
Chris Lattner86660982001-08-27 05:16:50 +0000904 }
905}
906
907
908//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000909// Miscellaneous Instruction Implementations
910//===----------------------------------------------------------------------===//
911
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000912void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
913 ECStack.back().Caller = &I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000914 vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000915 ArgVals.reserve(I.getNumOperands()-1);
916 for (unsigned i = 1; i < I.getNumOperands(); ++i)
917 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner365a76e2001-09-10 04:49:44 +0000918
Chris Lattner070cf5e2001-11-07 20:12:30 +0000919 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000920 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000921 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +0000922
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000923 callMethod((Function*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000924}
925
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000926static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000927 BasicBlock *PrevBB = SF.PrevBB;
928 Value *IncomingValue = 0;
929
930 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000931 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
932 if (I.getIncomingBlock(--i) == PrevBB) {
933 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +0000934 break;
935 }
936 }
937 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
938
939 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000940 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000941}
942
Chris Lattner86660982001-08-27 05:16:50 +0000943#define IMPLEMENT_SHIFT(OP, TY) \
944 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
945
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000946static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
947 const Type *Ty = I.getOperand(0)->getType();
948 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
949 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000950 GenericValue Dest;
951
952 switch (Ty->getPrimitiveID()) {
953 IMPLEMENT_SHIFT(<<, UByte);
954 IMPLEMENT_SHIFT(<<, SByte);
955 IMPLEMENT_SHIFT(<<, UShort);
956 IMPLEMENT_SHIFT(<<, Short);
957 IMPLEMENT_SHIFT(<<, UInt);
958 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000959 IMPLEMENT_SHIFT(<<, ULong);
960 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000961 IMPLEMENT_SHIFT(<<, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000962 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000963 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000964 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000965 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000966}
967
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000968static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
969 const Type *Ty = I.getOperand(0)->getType();
970 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
971 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000972 GenericValue Dest;
973
974 switch (Ty->getPrimitiveID()) {
975 IMPLEMENT_SHIFT(>>, UByte);
976 IMPLEMENT_SHIFT(>>, SByte);
977 IMPLEMENT_SHIFT(>>, UShort);
978 IMPLEMENT_SHIFT(>>, Short);
979 IMPLEMENT_SHIFT(>>, UInt);
980 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000981 IMPLEMENT_SHIFT(>>, ULong);
982 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000983 IMPLEMENT_SHIFT(>>, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000984 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000985 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000986 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000987 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000988}
989
990#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000991 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000992
993#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
994 case Type::DESTTY##TyID: \
995 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000996 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000997 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
998 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
999 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1000 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1001 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001002 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1003 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001004 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1005 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001006
1007#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1008 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1009 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1010
1011#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +00001012 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +00001013 break; \
1014 } \
1015 break
1016
1017#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1018 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1019 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001020 IMPLEMENT_CAST_CASE_END()
1021
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001022static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1023 const Type *Ty = I.getType();
1024 const Type *SrcTy = I.getOperand(0)->getType();
1025 GenericValue Src = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001026 GenericValue Dest;
1027
1028 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001029 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1030 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1031 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1032 IMPLEMENT_CAST_CASE(Short , ( signed char));
1033 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1034 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1035 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1036 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1037 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1038 IMPLEMENT_CAST_CASE(Float , (float));
1039 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001040 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001041 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001042 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001043 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001044}
Chris Lattner92101ac2001-08-23 17:05:04 +00001045
1046
1047
1048
1049//===----------------------------------------------------------------------===//
1050// Dispatch and Execution Code
1051//===----------------------------------------------------------------------===//
1052
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001053MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001054 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001055 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1056 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001057
1058 // Iterate over all of the instructions...
1059 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001060 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1061 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1062 // For each instruction... Add Annote
1063 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001064}
1065
1066unsigned MethodInfo::getValueSlot(const Value *V) {
1067 unsigned Plane = V->getType()->getUniqueID();
1068 if (Plane >= NumPlaneElements.size())
1069 NumPlaneElements.resize(Plane+1, 0);
1070 return NumPlaneElements[Plane]++;
1071}
1072
1073
Chris Lattner92101ac2001-08-23 17:05:04 +00001074//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001075// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001076//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001077void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001078 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1079 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1080 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001081 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001082 GenericValue Result = callExternalMethod(M, ArgVals);
1083 const Type *RetTy = M->getReturnType();
1084
1085 // Copy the result back into the result variable if we are not returning
1086 // void.
1087 if (RetTy != Type::VoidTy) {
1088 if (!ECStack.empty() && ECStack.back().Caller) {
1089 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001090 SetValue(SF.Caller, Result, SF);
1091
1092 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001093 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001094 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001095 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001096 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001097 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001098 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001099
1100 if (RetTy->isIntegral())
1101 ExitCode = Result.SByteVal; // Capture the exit code of the program
1102 }
1103 }
1104
Chris Lattner92101ac2001-08-23 17:05:04 +00001105 return;
1106 }
1107
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001108 // Process the function, assigning instruction numbers to the instructions in
1109 // the function. Also calculate the number of values for each type slot
1110 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001111 //
1112 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001113 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001114
Chris Lattner92101ac2001-08-23 17:05:04 +00001115 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1116 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001117 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001118 StackFrame.CurInst = StackFrame.CurBB->begin();
1119 StackFrame.MethInfo = MethInfo;
1120
1121 // Initialize the values to nothing...
1122 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001123 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001124 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1125
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001126 // Taint the initial values of stuff
1127 memset(&StackFrame.Values[i][0], 42,
1128 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1129 }
1130
Chris Lattner92101ac2001-08-23 17:05:04 +00001131 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1132
Chris Lattner92101ac2001-08-23 17:05:04 +00001133
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001134 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001135 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001136 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001137 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001138 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1139 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001140}
1141
1142// executeInstruction - Interpret a single instruction, increment the "PC", and
1143// return true if the next instruction is a breakpoint...
1144//
1145bool Interpreter::executeInstruction() {
1146 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1147
1148 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001149 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001150
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001151 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001152 CW << "Run:" << I;
1153
1154 // Set a sigsetjmp buffer so that we can recover if an error happens during
1155 // instruction execution...
1156 //
1157 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1158 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001159 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001160 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001161 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001162 // If -abort-on-exception was specified, terminate LLI instead of trying
1163 // to debug it.
1164 //
1165 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001166 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001167 cout << "CTRL-C Detected, execution halted.\n";
1168 }
1169 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001170 return true;
1171 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001172
Chris Lattner461f02f2001-11-07 05:31:27 +00001173 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001174 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001175 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001176 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001177 switch (I.getOpcode()) {
Chris Lattner2adcd832002-05-03 19:52:30 +00001178 case Instruction::Not: executeNotInst(cast<UnaryOperator>(I),SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001179 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001180 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1181 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001182 // Memory Instructions
1183 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001184 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001185 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1186 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1187 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001188 case Instruction::GetElementPtr:
1189 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001190
1191 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001192 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1193 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1194 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1195 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1196 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001197 default:
1198 cout << "Don't know how to execute this instruction!\n-->" << I;
1199 }
1200 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001201 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001202
1203 // Reset the current frame location to the top of stack
1204 CurFrame = ECStack.size()-1;
1205
1206 if (CurFrame == -1) return false; // No breakpoint if no code
1207
1208 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001209 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001210}
1211
1212void Interpreter::stepInstruction() { // Do the 'step' command
1213 if (ECStack.empty()) {
1214 cout << "Error: no program running, cannot step!\n";
1215 return;
1216 }
1217
1218 // Run an instruction...
1219 executeInstruction();
1220
1221 // Print the next instruction to execute...
1222 printCurrentInstruction();
1223}
1224
1225// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001226void Interpreter::nextInstruction() { // Do the 'next' command
1227 if (ECStack.empty()) {
1228 cout << "Error: no program running, cannot 'next'!\n";
1229 return;
1230 }
1231
1232 // If this is a call instruction, step over the call instruction...
1233 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001234 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001235 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001236 // Step into the function...
1237 if (executeInstruction()) {
1238 // Hit a breakpoint, print current instruction, then return to user...
1239 cout << "Breakpoint hit!\n";
1240 printCurrentInstruction();
1241 return;
1242 }
1243
Chris Lattnera74a6b52001-10-29 14:08:33 +00001244 // If we we able to step into the function, finish it now. We might not be
1245 // able the step into a function, if it's external for example.
1246 if (ECStack.size() != StackSize)
1247 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001248 else
1249 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001250
Chris Lattner92101ac2001-08-23 17:05:04 +00001251 } else {
1252 // Normal instruction, just step...
1253 stepInstruction();
1254 }
1255}
1256
1257void Interpreter::run() {
1258 if (ECStack.empty()) {
1259 cout << "Error: no program running, cannot run!\n";
1260 return;
1261 }
1262
1263 bool HitBreakpoint = false;
1264 while (!ECStack.empty() && !HitBreakpoint) {
1265 // Run an instruction...
1266 HitBreakpoint = executeInstruction();
1267 }
1268
1269 if (HitBreakpoint) {
1270 cout << "Breakpoint hit!\n";
1271 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001272 // Print the next instruction to execute...
1273 printCurrentInstruction();
1274}
1275
1276void Interpreter::finish() {
1277 if (ECStack.empty()) {
1278 cout << "Error: no program running, cannot run!\n";
1279 return;
1280 }
1281
1282 unsigned StackSize = ECStack.size();
1283 bool HitBreakpoint = false;
1284 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1285 // Run an instruction...
1286 HitBreakpoint = executeInstruction();
1287 }
1288
1289 if (HitBreakpoint) {
1290 cout << "Breakpoint hit!\n";
1291 }
1292
1293 // Print the next instruction to execute...
1294 printCurrentInstruction();
1295}
1296
1297
1298
1299// printCurrentInstruction - Print out the instruction that the virtual PC is
1300// at, or fail silently if no program is running.
1301//
1302void Interpreter::printCurrentInstruction() {
1303 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001304 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1305 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1306
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001307 Instruction &I = *ECStack.back().CurInst;
1308 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001309 assert(IN && "Instruction has no numbering annotation!");
1310 cout << "#" << IN->InstNum << I;
1311 }
1312}
1313
1314void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001315 switch (Ty->getPrimitiveID()) {
1316 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1317 case Type::SByteTyID: cout << V.SByteVal; break;
1318 case Type::UByteTyID: cout << V.UByteVal; break;
1319 case Type::ShortTyID: cout << V.ShortVal; break;
1320 case Type::UShortTyID: cout << V.UShortVal; break;
1321 case Type::IntTyID: cout << V.IntVal; break;
1322 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001323 case Type::LongTyID: cout << (long)V.LongVal; break;
1324 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001325 case Type::FloatTyID: cout << V.FloatVal; break;
1326 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001327 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001328 default:
1329 cout << "- Don't know how to print value of this type!";
1330 break;
1331 }
1332}
1333
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001334void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001335 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001336 printValue(Ty, V);
1337}
1338
Chris Lattner697954c2002-01-20 22:54:45 +00001339void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001340 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1341 if (!PickedVal) return;
1342
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001343 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1344 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001345 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001346 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001347 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1348 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001349 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001350 print(PickedVal->getType(),
1351 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001352 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001353 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001354}
1355
Chris Lattner697954c2002-01-20 22:54:45 +00001356void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001357 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1358 if (!PickedVal) return;
1359
1360 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001361 print(PickedVal->getType(),
1362 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001363 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001364 printOperandInfo(PickedVal, ECStack[CurFrame]);
1365}
1366
Chris Lattner461f02f2001-11-07 05:31:27 +00001367// printStackFrame - Print information about the specified stack frame, or -1
1368// for the default one.
1369//
1370void Interpreter::printStackFrame(int FrameNo = -1) {
1371 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001372 Function *F = ECStack[FrameNo].CurMethod;
1373 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001374
1375 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001376 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001377
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001378 unsigned i = 0;
1379 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001380 if (i != 0) cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001381 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001382
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001383 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001384 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001385
Chris Lattner697954c2002-01-20 22:54:45 +00001386 cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001387
1388 if (FrameNo != int(ECStack.size()-1)) {
1389 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1390 CW << --I;
1391 } else {
1392 CW << *ECStack[FrameNo].CurInst;
1393 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001394}
Chris Lattner461f02f2001-11-07 05:31:27 +00001395