blob: 77d801129a4cedb18e969b6748181323be390899 [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 Lattner5ff62e92002-07-22 02:10:13 +000027static cl::opt<bool>
28QuietMode("quiet", cl::desc("Do not emit any non-program output"));
29
30static cl::alias
31QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
32
33static cl::opt<bool>
34ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
35
36static cl::opt<bool>
37AbortOnExceptions("abort-on-exception",
38 cl::desc("Halt execution on a machine exception"));
Chris Lattnere9bb2df2001-12-03 22:26:30 +000039
Chris Lattner2e42d3a2001-10-15 05:51:48 +000040// Create a TargetData structure to handle memory addressing and size/alignment
41// computations
42//
43static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000044CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000045
46
Chris Lattnere2409062001-11-12 16:19:45 +000047#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner5ff62e92002-07-22 02:10:13 +000048static cl::opt<bool>
49ProfileStructureFields("profilestructfields",
50 cl::desc("Profile Structure Field Accesses"));
Chris Lattnere2409062001-11-12 16:19:45 +000051#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000052static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000053#endif
54
Chris Lattner5af0c482001-11-07 04:23:00 +000055sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000056static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000057
58extern "C" {
59static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000060 if (InInstruction)
61 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000062}
63}
64
65static void initializeSignalHandlers() {
66 struct sigaction Action;
67 Action.sa_handler = SigHandler;
68 Action.sa_flags = SA_SIGINFO;
69 sigemptyset(&Action.sa_mask);
70 sigaction(SIGSEGV, &Action, 0);
71 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000072 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000073 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000074}
75
Chris Lattner2e42d3a2001-10-15 05:51:48 +000076
77//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000078// Value Manipulation code
79//===----------------------------------------------------------------------===//
80
81static unsigned getOperandSlot(Value *V) {
82 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
83 assert(SN && "Operand does not have a slot number annotation!");
84 return SN->SlotNum;
85}
86
87#define GET_CONST_VAL(TY, CLASS) \
88 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
89
90static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000091 if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000092 GenericValue Result;
93 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000094 GET_CONST_VAL(Bool , ConstantBool);
95 GET_CONST_VAL(UByte , ConstantUInt);
96 GET_CONST_VAL(SByte , ConstantSInt);
97 GET_CONST_VAL(UShort , ConstantUInt);
98 GET_CONST_VAL(Short , ConstantSInt);
99 GET_CONST_VAL(UInt , ConstantUInt);
100 GET_CONST_VAL(Int , ConstantSInt);
101 GET_CONST_VAL(ULong , ConstantUInt);
102 GET_CONST_VAL(Long , ConstantSInt);
103 GET_CONST_VAL(Float , ConstantFP);
104 GET_CONST_VAL(Double , ConstantFP);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000105 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000106 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000107 Result.PointerVal = 0;
Chris Lattner697954c2002-01-20 22:54:45 +0000108 } else if (isa<ConstantPointerRef>(CPV)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000109 assert(0 && "Not implemented!");
110 } else {
111 assert(0 && "Unknown constant pointer type!");
112 }
113 break;
114 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000115 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000116 }
117 return Result;
118 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
119 GlobalAddress *Address =
120 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
121 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000122 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000123 return Result;
124 } else {
125 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000126 unsigned OpSlot = getOperandSlot(V);
127 assert(TyP < SF.Values.size() &&
128 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000129 return SF.Values[TyP][getOperandSlot(V)];
130 }
131}
132
133static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000134 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000135 cout << "Constant Pool Value\n";
136 } else if (isa<GlobalValue>(V)) {
137 cout << "Global Value\n";
138 } else {
139 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
140 unsigned Slot = getOperandSlot(V);
141 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000142 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
143 << " Contents=0x";
144
145 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
146 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
147 unsigned char Cur = Buf[i];
148 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
149 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
150 }
Chris Lattner697954c2002-01-20 22:54:45 +0000151 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000152 }
153}
154
155
156
157static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
158 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
159
Chris Lattner697954c2002-01-20 22:54:45 +0000160 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000161 SF.Values[TyP][getOperandSlot(V)] = Val;
162}
163
164
165//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166// Annotation Wrangling code
167//===----------------------------------------------------------------------===//
168
169void Interpreter::initializeExecutionEngine() {
170 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
171 &MethodInfo::Create);
172 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
173 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000174 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000175}
176
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000177// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000178// specified memory location...
179//
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000180static void InitializeMemory(const Constant *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000181#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
182 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000183 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000184 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000185 } return
186
187 switch (Init->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000188 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
189 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
190 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
191 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
192 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
193 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
194 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
195 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
196 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
197 INITIALIZE_MEMORY(Float , ConstantFP , float);
198 INITIALIZE_MEMORY(Double , ConstantFP , double);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000199#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000200
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000201 case Type::ArrayTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000202 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000203 const vector<Use> &Val = CPA->getValues();
204 unsigned ElementSize =
205 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
206 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000207 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000208 return;
209 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000210
211 case Type::StructTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000212 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000213 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
214 const vector<Use> &Val = CPS->getValues();
215 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000216 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000217 Addr+SL->MemberOffsets[i]);
218 return;
219 }
220
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000221 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000222 if (isa<ConstantPointerNull>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000223 *(void**)Addr = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000224 } else if (const ConstantPointerRef *CPR =
225 dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000226 GlobalAddress *Address =
227 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
228 *(void**)Addr = (GenericValue*)Address->Ptr;
229 } else {
230 assert(0 && "Unknown Constant pointer type!");
231 }
232 return;
233
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000234 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000235 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000236 assert(0 && "Unknown constant type to initialize memory with!");
237 }
238}
239
240Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
241 assert(AID == GlobalAddressAID);
242
243 // This annotation will only be created on GlobalValue objects...
244 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
245
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000246 if (isa<Function>(GVal)) {
247 // The GlobalAddress object for a function is just a pointer to function
248 // itself. Don't delete it when the annotation is gone though!
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000249 return new GlobalAddress(GVal, false);
250 }
251
252 // Handle the case of a global variable...
253 assert(isa<GlobalVariable>(GVal) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000254 "Global value found that isn't a function or global variable!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000255 GlobalVariable *GV = cast<GlobalVariable>(GVal);
256
257 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000258 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000259
260 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000261 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000262 assert(Addr != 0 && "Null pointer returned by malloc!");
263
264 // Initialize the memory if there is an initializer...
265 if (GV->hasInitializer())
266 InitializeMemory(GV->getInitializer(), (char*)Addr);
267
268 return new GlobalAddress(Addr, true); // Simply invoke the ctor
269}
270
Chris Lattner92101ac2001-08-23 17:05:04 +0000271
272//===----------------------------------------------------------------------===//
Chris Lattner2adcd832002-05-03 19:52:30 +0000273// Unary Instruction Implementations
274//===----------------------------------------------------------------------===//
275
276#define IMPLEMENT_UNARY_OPERATOR(OP, TY) \
277 case Type::TY##TyID: Dest.TY##Val = OP Src.TY##Val; break
278
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000279static void executeNotInst(UnaryOperator &I, ExecutionContext &SF) {
280 const Type *Ty = I.getOperand(0)->getType();
281 GenericValue Src = getOperandValue(I.getOperand(0), SF);
Chris Lattner2adcd832002-05-03 19:52:30 +0000282 GenericValue Dest;
283 switch (Ty->getPrimitiveID()) {
284 IMPLEMENT_UNARY_OPERATOR(~, UByte);
285 IMPLEMENT_UNARY_OPERATOR(~, SByte);
286 IMPLEMENT_UNARY_OPERATOR(~, UShort);
287 IMPLEMENT_UNARY_OPERATOR(~, Short);
288 IMPLEMENT_UNARY_OPERATOR(~, UInt);
289 IMPLEMENT_UNARY_OPERATOR(~, Int);
290 IMPLEMENT_UNARY_OPERATOR(~, ULong);
291 IMPLEMENT_UNARY_OPERATOR(~, Long);
292 IMPLEMENT_UNARY_OPERATOR(~, Pointer);
293 default:
294 cout << "Unhandled type for Not instruction: " << Ty << "\n";
295 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000296 SetValue(&I, Dest, SF);
Chris Lattner2adcd832002-05-03 19:52:30 +0000297}
298
299//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000300// Binary Instruction Implementations
301//===----------------------------------------------------------------------===//
302
303#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
304 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
305
306static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
307 const Type *Ty, ExecutionContext &SF) {
308 GenericValue Dest;
309 switch (Ty->getPrimitiveID()) {
310 IMPLEMENT_BINARY_OPERATOR(+, UByte);
311 IMPLEMENT_BINARY_OPERATOR(+, SByte);
312 IMPLEMENT_BINARY_OPERATOR(+, UShort);
313 IMPLEMENT_BINARY_OPERATOR(+, Short);
314 IMPLEMENT_BINARY_OPERATOR(+, UInt);
315 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000316 IMPLEMENT_BINARY_OPERATOR(+, ULong);
317 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000318 IMPLEMENT_BINARY_OPERATOR(+, Float);
319 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000320 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000321 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000322 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000323 }
324 return Dest;
325}
326
327static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
328 const Type *Ty, ExecutionContext &SF) {
329 GenericValue Dest;
330 switch (Ty->getPrimitiveID()) {
331 IMPLEMENT_BINARY_OPERATOR(-, UByte);
332 IMPLEMENT_BINARY_OPERATOR(-, SByte);
333 IMPLEMENT_BINARY_OPERATOR(-, UShort);
334 IMPLEMENT_BINARY_OPERATOR(-, Short);
335 IMPLEMENT_BINARY_OPERATOR(-, UInt);
336 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000337 IMPLEMENT_BINARY_OPERATOR(-, ULong);
338 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000339 IMPLEMENT_BINARY_OPERATOR(-, Float);
340 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000341 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000342 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000343 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000344 }
345 return Dest;
346}
347
Chris Lattnerc2593162001-10-27 08:28:11 +0000348static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
349 const Type *Ty, ExecutionContext &SF) {
350 GenericValue Dest;
351 switch (Ty->getPrimitiveID()) {
352 IMPLEMENT_BINARY_OPERATOR(*, UByte);
353 IMPLEMENT_BINARY_OPERATOR(*, SByte);
354 IMPLEMENT_BINARY_OPERATOR(*, UShort);
355 IMPLEMENT_BINARY_OPERATOR(*, Short);
356 IMPLEMENT_BINARY_OPERATOR(*, UInt);
357 IMPLEMENT_BINARY_OPERATOR(*, Int);
358 IMPLEMENT_BINARY_OPERATOR(*, ULong);
359 IMPLEMENT_BINARY_OPERATOR(*, Long);
360 IMPLEMENT_BINARY_OPERATOR(*, Float);
361 IMPLEMENT_BINARY_OPERATOR(*, Double);
362 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
363 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000364 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000365 }
366 return Dest;
367}
368
369static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
370 const Type *Ty, ExecutionContext &SF) {
371 GenericValue Dest;
372 switch (Ty->getPrimitiveID()) {
373 IMPLEMENT_BINARY_OPERATOR(/, UByte);
374 IMPLEMENT_BINARY_OPERATOR(/, SByte);
375 IMPLEMENT_BINARY_OPERATOR(/, UShort);
376 IMPLEMENT_BINARY_OPERATOR(/, Short);
377 IMPLEMENT_BINARY_OPERATOR(/, UInt);
378 IMPLEMENT_BINARY_OPERATOR(/, Int);
379 IMPLEMENT_BINARY_OPERATOR(/, ULong);
380 IMPLEMENT_BINARY_OPERATOR(/, Long);
381 IMPLEMENT_BINARY_OPERATOR(/, Float);
382 IMPLEMENT_BINARY_OPERATOR(/, Double);
383 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
384 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000385 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000386 }
387 return Dest;
388}
389
390static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
391 const Type *Ty, ExecutionContext &SF) {
392 GenericValue Dest;
393 switch (Ty->getPrimitiveID()) {
394 IMPLEMENT_BINARY_OPERATOR(%, UByte);
395 IMPLEMENT_BINARY_OPERATOR(%, SByte);
396 IMPLEMENT_BINARY_OPERATOR(%, UShort);
397 IMPLEMENT_BINARY_OPERATOR(%, Short);
398 IMPLEMENT_BINARY_OPERATOR(%, UInt);
399 IMPLEMENT_BINARY_OPERATOR(%, Int);
400 IMPLEMENT_BINARY_OPERATOR(%, ULong);
401 IMPLEMENT_BINARY_OPERATOR(%, Long);
402 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
403 case Type::FloatTyID:
404 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
405 break;
406 case Type::DoubleTyID:
407 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
408 break;
409 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000410 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000411 }
412 return Dest;
413}
414
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000415static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000416 const Type *Ty, ExecutionContext &SF) {
417 GenericValue Dest;
418 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000419 IMPLEMENT_BINARY_OPERATOR(&, UByte);
420 IMPLEMENT_BINARY_OPERATOR(&, SByte);
421 IMPLEMENT_BINARY_OPERATOR(&, UShort);
422 IMPLEMENT_BINARY_OPERATOR(&, Short);
423 IMPLEMENT_BINARY_OPERATOR(&, UInt);
424 IMPLEMENT_BINARY_OPERATOR(&, Int);
425 IMPLEMENT_BINARY_OPERATOR(&, ULong);
426 IMPLEMENT_BINARY_OPERATOR(&, Long);
427 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
428 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000429 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000430 }
431 return Dest;
432}
433
434
435static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
436 const Type *Ty, ExecutionContext &SF) {
437 GenericValue Dest;
438 switch (Ty->getPrimitiveID()) {
439 IMPLEMENT_BINARY_OPERATOR(|, UByte);
440 IMPLEMENT_BINARY_OPERATOR(|, SByte);
441 IMPLEMENT_BINARY_OPERATOR(|, UShort);
442 IMPLEMENT_BINARY_OPERATOR(|, Short);
443 IMPLEMENT_BINARY_OPERATOR(|, UInt);
444 IMPLEMENT_BINARY_OPERATOR(|, Int);
445 IMPLEMENT_BINARY_OPERATOR(|, ULong);
446 IMPLEMENT_BINARY_OPERATOR(|, Long);
447 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
448 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000449 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000450 }
451 return Dest;
452}
453
454
455static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
456 const Type *Ty, ExecutionContext &SF) {
457 GenericValue Dest;
458 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000459 IMPLEMENT_BINARY_OPERATOR(^, UByte);
460 IMPLEMENT_BINARY_OPERATOR(^, SByte);
461 IMPLEMENT_BINARY_OPERATOR(^, UShort);
462 IMPLEMENT_BINARY_OPERATOR(^, Short);
463 IMPLEMENT_BINARY_OPERATOR(^, UInt);
464 IMPLEMENT_BINARY_OPERATOR(^, Int);
465 IMPLEMENT_BINARY_OPERATOR(^, ULong);
466 IMPLEMENT_BINARY_OPERATOR(^, Long);
467 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
468 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000469 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000470 }
471 return Dest;
472}
473
474
Chris Lattner92101ac2001-08-23 17:05:04 +0000475#define IMPLEMENT_SETCC(OP, TY) \
476 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
477
Chris Lattner92101ac2001-08-23 17:05:04 +0000478static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
479 const Type *Ty, ExecutionContext &SF) {
480 GenericValue Dest;
481 switch (Ty->getPrimitiveID()) {
482 IMPLEMENT_SETCC(==, UByte);
483 IMPLEMENT_SETCC(==, SByte);
484 IMPLEMENT_SETCC(==, UShort);
485 IMPLEMENT_SETCC(==, Short);
486 IMPLEMENT_SETCC(==, UInt);
487 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000488 IMPLEMENT_SETCC(==, ULong);
489 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000490 IMPLEMENT_SETCC(==, Float);
491 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000492 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000493 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000494 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000495 }
496 return Dest;
497}
498
499static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
500 const Type *Ty, ExecutionContext &SF) {
501 GenericValue Dest;
502 switch (Ty->getPrimitiveID()) {
503 IMPLEMENT_SETCC(!=, UByte);
504 IMPLEMENT_SETCC(!=, SByte);
505 IMPLEMENT_SETCC(!=, UShort);
506 IMPLEMENT_SETCC(!=, Short);
507 IMPLEMENT_SETCC(!=, UInt);
508 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000509 IMPLEMENT_SETCC(!=, ULong);
510 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000511 IMPLEMENT_SETCC(!=, Float);
512 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000513 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000514
Chris Lattner92101ac2001-08-23 17:05:04 +0000515 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000516 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 }
518 return Dest;
519}
520
521static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
522 const Type *Ty, ExecutionContext &SF) {
523 GenericValue Dest;
524 switch (Ty->getPrimitiveID()) {
525 IMPLEMENT_SETCC(<=, UByte);
526 IMPLEMENT_SETCC(<=, SByte);
527 IMPLEMENT_SETCC(<=, UShort);
528 IMPLEMENT_SETCC(<=, Short);
529 IMPLEMENT_SETCC(<=, UInt);
530 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000531 IMPLEMENT_SETCC(<=, ULong);
532 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 IMPLEMENT_SETCC(<=, Float);
534 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000535 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000536 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000537 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 }
539 return Dest;
540}
541
542static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
543 const Type *Ty, ExecutionContext &SF) {
544 GenericValue Dest;
545 switch (Ty->getPrimitiveID()) {
546 IMPLEMENT_SETCC(>=, UByte);
547 IMPLEMENT_SETCC(>=, SByte);
548 IMPLEMENT_SETCC(>=, UShort);
549 IMPLEMENT_SETCC(>=, Short);
550 IMPLEMENT_SETCC(>=, UInt);
551 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000552 IMPLEMENT_SETCC(>=, ULong);
553 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000554 IMPLEMENT_SETCC(>=, Float);
555 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000556 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000557 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000558 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 }
560 return Dest;
561}
562
563static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
564 const Type *Ty, ExecutionContext &SF) {
565 GenericValue Dest;
566 switch (Ty->getPrimitiveID()) {
567 IMPLEMENT_SETCC(<, UByte);
568 IMPLEMENT_SETCC(<, SByte);
569 IMPLEMENT_SETCC(<, UShort);
570 IMPLEMENT_SETCC(<, Short);
571 IMPLEMENT_SETCC(<, UInt);
572 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000573 IMPLEMENT_SETCC(<, ULong);
574 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000575 IMPLEMENT_SETCC(<, Float);
576 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000577 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000578 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000579 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000580 }
581 return Dest;
582}
583
584static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
585 const Type *Ty, ExecutionContext &SF) {
586 GenericValue Dest;
587 switch (Ty->getPrimitiveID()) {
588 IMPLEMENT_SETCC(>, UByte);
589 IMPLEMENT_SETCC(>, SByte);
590 IMPLEMENT_SETCC(>, UShort);
591 IMPLEMENT_SETCC(>, Short);
592 IMPLEMENT_SETCC(>, UInt);
593 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000594 IMPLEMENT_SETCC(>, ULong);
595 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000596 IMPLEMENT_SETCC(>, Float);
597 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000598 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000599 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000600 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000601 }
602 return Dest;
603}
604
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000605static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
606 const Type *Ty = I.getOperand(0)->getType();
607 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
608 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000609 GenericValue R; // Result
610
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000611 switch (I.getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000612 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
613 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
614 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
615 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
616 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000617 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
618 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000619 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000620 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
621 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
622 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
623 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
624 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
625 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
626 default:
627 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000628 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000629 }
630
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000631 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000632}
633
Chris Lattner92101ac2001-08-23 17:05:04 +0000634//===----------------------------------------------------------------------===//
635// Terminator Instruction Implementations
636//===----------------------------------------------------------------------===//
637
Chris Lattnera95c6992001-11-12 16:28:48 +0000638static void PerformExitStuff() {
639#ifdef PROFILE_STRUCTURE_FIELDS
640 // Print out structure field accounting information...
641 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000642 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000643 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000644 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
645 for (; I != E; ++I) {
646 vector<unsigned> &OfC = I->second;
647 CW << " '" << (Value*)I->first << "'\t- Sum=";
648
649 unsigned Sum = 0;
650 for (unsigned i = 0; i < OfC.size(); ++i)
651 Sum += OfC[i];
652 CW << Sum << " - ";
653
654 for (unsigned i = 0; i < OfC.size(); ++i) {
655 if (i) CW << ", ";
656 CW << OfC[i];
657 }
Chris Lattner697954c2002-01-20 22:54:45 +0000658 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000659 }
Chris Lattner697954c2002-01-20 22:54:45 +0000660 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000661
662 CW << "Profile Field Access Percentages:\n";
663 cout.precision(3);
664 for (I = FieldAccessCounts.begin(); I != E; ++I) {
665 vector<unsigned> &OfC = I->second;
666 unsigned Sum = 0;
667 for (unsigned i = 0; i < OfC.size(); ++i)
668 Sum += OfC[i];
669
670 CW << " '" << (Value*)I->first << "'\t- ";
671 for (unsigned i = 0; i < OfC.size(); ++i) {
672 if (i) CW << ", ";
673 CW << double(OfC[i])/Sum;
674 }
Chris Lattner697954c2002-01-20 22:54:45 +0000675 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000676 }
Chris Lattner697954c2002-01-20 22:54:45 +0000677 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000678
Chris Lattnera95c6992001-11-12 16:28:48 +0000679 FieldAccessCounts.clear();
680 }
681#endif
682}
683
Chris Lattnere43db882001-10-27 04:15:57 +0000684void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000685 if (!QuietMode) {
686 cout << "Program returned ";
687 print(Type::IntTy, GV);
688 cout << " via 'void exit(int)'\n";
689 }
Chris Lattnere43db882001-10-27 04:15:57 +0000690
691 ExitCode = GV.SByteVal;
692 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000693 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000694}
695
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000696void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000697 const Type *RetTy = 0;
698 GenericValue Result;
699
700 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000701 if (I.getNumOperands()) {
702 RetTy = I.getReturnValue()->getType();
703 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000704 }
705
706 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000707 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000708
709 // Pop the current stack frame... this invalidates SF
710 ECStack.pop_back();
711
712 if (ECStack.empty()) { // Finished main. Put result into exit code...
713 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000714 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000715 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000716 << "\" returned ";
717 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000718 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000719 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000720
721 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000722 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000723 } else {
724 ExitCode = 0;
725 }
Chris Lattnere2409062001-11-12 16:19:45 +0000726
Chris Lattnera95c6992001-11-12 16:28:48 +0000727 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000728 return;
729 }
730
731 // If we have a previous stack frame, and we have a previous call, fill in
732 // the return value...
733 //
734 ExecutionContext &NewSF = ECStack.back();
735 if (NewSF.Caller) {
736 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
737 SetValue(NewSF.Caller, Result, NewSF);
738
739 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000740 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000741 // This must be a function that is executing because of a user 'call'
742 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000743 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000744 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000745 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000746 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000747 }
748}
749
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000750void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000751 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
752 BasicBlock *Dest;
753
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000754 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
755 if (!I.isUnconditional()) {
756 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000757 GenericValue CondVal = getOperandValue(Cond, SF);
758 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000759 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000760 }
761 SF.CurBB = Dest; // Update CurBB to branch destination
762 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
763}
764
765//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000766// Memory Instruction Implementations
767//===----------------------------------------------------------------------===//
768
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000769void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
770 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000771
Chris Lattnercc82cc12002-04-28 21:57:33 +0000772 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000773 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000774
775 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000776 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000777 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
778
779 GenericValue Result;
780 Result.PointerVal = (PointerTy)Memory;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000781 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000782 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000783
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000784 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000785 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000786}
787
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000788static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
789 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
790 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000791 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000792 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000793}
794
Chris Lattner95c3af52001-10-29 19:32:19 +0000795
796// getElementOffset - The workhorse for getelementptr, load and store. This
797// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
798// the pointer type specified by argument Arg.
799//
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000800static PointerTy getElementOffset(MemAccessInst &I, ExecutionContext &SF) {
801 assert(isa<PointerType>(I.getPointerOperand()->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000802 "Cannot getElementOffset of a nonpointer type!");
803
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000804 PointerTy Total = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000805 const Type *Ty = I.getPointerOperand()->getType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000806
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000807 unsigned ArgOff = I.getFirstIndexOperandNumber();
808 while (ArgOff < I.getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000809 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
810 const StructLayout *SLO = TD.getStructLayout(STy);
811
812 // Indicies must be ubyte constants...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000813 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand(ArgOff++));
Chris Lattner782b9392001-11-26 18:18:18 +0000814 assert(CPU->getType() == Type::UByteTy);
815 unsigned Index = CPU->getValue();
816
Chris Lattnere2409062001-11-12 16:19:45 +0000817#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000818 if (ProfileStructureFields) {
819 // Do accounting for this field...
820 vector<unsigned> &OfC = FieldAccessCounts[STy];
821 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
822 OfC[Index]++;
823 }
Chris Lattnere2409062001-11-12 16:19:45 +0000824#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000825
826 Total += SLO->MemberOffsets[Index];
827 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000828 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000829
Chris Lattner782b9392001-11-26 18:18:18 +0000830 // Get the index number for the array... which must be uint type...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000831 assert(I.getOperand(ArgOff)->getType() == Type::UIntTy);
832 unsigned Idx = getOperandValue(I.getOperand(ArgOff++), SF).UIntVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000833 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000834 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000835 cerr << "Out of range memory access to element #" << Idx
836 << " of a " << AT->getNumElements() << " element array."
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000837 << " Subscript #" << (ArgOff-I.getFirstIndexOperandNumber())
Chris Lattnerf23eb852001-12-14 16:49:29 +0000838 << "\n";
839 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000840 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000841 }
Chris Lattner782b9392001-11-26 18:18:18 +0000842
Chris Lattnerf23eb852001-12-14 16:49:29 +0000843 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000844 unsigned Size = TD.getTypeSize(Ty);
845 Total += Size*Idx;
846 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000847 }
848
849 return Total;
850}
851
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000852static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
853 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000854 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000855
856 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000857 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000858 SetValue(&I, Result, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000859}
860
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000861static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
862 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000863 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000864 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000865 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000866
867 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000868 GenericValue Result;
869
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000870 switch (I.getType()->getPrimitiveID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000871 case Type::BoolTyID:
872 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000873 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000874 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000875 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000876 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000877 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000878 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000879 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
880 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
881 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
882 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000883 default:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000884 cout << "Cannot load value of type " << I.getType() << "!\n";
Chris Lattner86660982001-08-27 05:16:50 +0000885 }
886
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000887 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000888}
889
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000890static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
891 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000892 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000893 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000894
895 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000896 GenericValue Val = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000897
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000898 switch (I.getOperand(0)->getType()->getPrimitiveID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000899 case Type::BoolTyID:
900 case Type::UByteTyID:
901 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
902 case Type::UShortTyID:
903 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
904 case Type::UIntTyID:
905 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000906 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000907 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
908 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000909 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
910 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000911 default:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000912 cout << "Cannot store value of type " << I.getType() << "!\n";
Chris Lattner86660982001-08-27 05:16:50 +0000913 }
914}
915
916
917//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000918// Miscellaneous Instruction Implementations
919//===----------------------------------------------------------------------===//
920
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000921void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
922 ECStack.back().Caller = &I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000923 vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000924 ArgVals.reserve(I.getNumOperands()-1);
925 for (unsigned i = 1; i < I.getNumOperands(); ++i)
926 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner365a76e2001-09-10 04:49:44 +0000927
Chris Lattner070cf5e2001-11-07 20:12:30 +0000928 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000929 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000930 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +0000931
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000932 callMethod((Function*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000933}
934
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000935static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000936 BasicBlock *PrevBB = SF.PrevBB;
937 Value *IncomingValue = 0;
938
939 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000940 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
941 if (I.getIncomingBlock(--i) == PrevBB) {
942 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +0000943 break;
944 }
945 }
946 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
947
948 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000949 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000950}
951
Chris Lattner86660982001-08-27 05:16:50 +0000952#define IMPLEMENT_SHIFT(OP, TY) \
953 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
954
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000955static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
956 const Type *Ty = I.getOperand(0)->getType();
957 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
958 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000959 GenericValue Dest;
960
961 switch (Ty->getPrimitiveID()) {
962 IMPLEMENT_SHIFT(<<, UByte);
963 IMPLEMENT_SHIFT(<<, SByte);
964 IMPLEMENT_SHIFT(<<, UShort);
965 IMPLEMENT_SHIFT(<<, Short);
966 IMPLEMENT_SHIFT(<<, UInt);
967 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000968 IMPLEMENT_SHIFT(<<, ULong);
969 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000970 IMPLEMENT_SHIFT(<<, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000971 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000972 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000973 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000974 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000975}
976
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000977static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
978 const Type *Ty = I.getOperand(0)->getType();
979 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
980 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000981 GenericValue Dest;
982
983 switch (Ty->getPrimitiveID()) {
984 IMPLEMENT_SHIFT(>>, UByte);
985 IMPLEMENT_SHIFT(>>, SByte);
986 IMPLEMENT_SHIFT(>>, UShort);
987 IMPLEMENT_SHIFT(>>, Short);
988 IMPLEMENT_SHIFT(>>, UInt);
989 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000990 IMPLEMENT_SHIFT(>>, ULong);
991 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000992 IMPLEMENT_SHIFT(>>, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000993 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000995 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000996 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000997}
998
999#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001000 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001001
1002#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1003 case Type::DESTTY##TyID: \
1004 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001005 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001006 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1007 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1008 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1009 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1010 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001011 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1012 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001013 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1014 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001015
1016#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1017 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1018 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1019
1020#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +00001021 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +00001022 break; \
1023 } \
1024 break
1025
1026#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1027 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1028 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001029 IMPLEMENT_CAST_CASE_END()
1030
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001031static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1032 const Type *Ty = I.getType();
1033 const Type *SrcTy = I.getOperand(0)->getType();
1034 GenericValue Src = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001035 GenericValue Dest;
1036
1037 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001038 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1039 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1040 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1041 IMPLEMENT_CAST_CASE(Short , ( signed char));
1042 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1043 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1044 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1045 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1046 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1047 IMPLEMENT_CAST_CASE(Float , (float));
1048 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001049 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001050 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001051 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001052 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001053}
Chris Lattner92101ac2001-08-23 17:05:04 +00001054
1055
1056
1057
1058//===----------------------------------------------------------------------===//
1059// Dispatch and Execution Code
1060//===----------------------------------------------------------------------===//
1061
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001062MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001063 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001064 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1065 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001066
1067 // Iterate over all of the instructions...
1068 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001069 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1070 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1071 // For each instruction... Add Annote
1072 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001073}
1074
1075unsigned MethodInfo::getValueSlot(const Value *V) {
1076 unsigned Plane = V->getType()->getUniqueID();
1077 if (Plane >= NumPlaneElements.size())
1078 NumPlaneElements.resize(Plane+1, 0);
1079 return NumPlaneElements[Plane]++;
1080}
1081
1082
Chris Lattner92101ac2001-08-23 17:05:04 +00001083//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001084// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001085//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001086void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001087 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1088 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1089 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001090 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001091 GenericValue Result = callExternalMethod(M, ArgVals);
1092 const Type *RetTy = M->getReturnType();
1093
1094 // Copy the result back into the result variable if we are not returning
1095 // void.
1096 if (RetTy != Type::VoidTy) {
1097 if (!ECStack.empty() && ECStack.back().Caller) {
1098 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001099 SetValue(SF.Caller, Result, SF);
1100
1101 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001102 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001103 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001104 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001105 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001106 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001107 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001108
1109 if (RetTy->isIntegral())
1110 ExitCode = Result.SByteVal; // Capture the exit code of the program
1111 }
1112 }
1113
Chris Lattner92101ac2001-08-23 17:05:04 +00001114 return;
1115 }
1116
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001117 // Process the function, assigning instruction numbers to the instructions in
1118 // the function. Also calculate the number of values for each type slot
1119 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001120 //
1121 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001122 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001123
Chris Lattner92101ac2001-08-23 17:05:04 +00001124 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1125 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001126 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001127 StackFrame.CurInst = StackFrame.CurBB->begin();
1128 StackFrame.MethInfo = MethInfo;
1129
1130 // Initialize the values to nothing...
1131 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001132 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001133 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1134
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001135 // Taint the initial values of stuff
1136 memset(&StackFrame.Values[i][0], 42,
1137 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1138 }
1139
Chris Lattner92101ac2001-08-23 17:05:04 +00001140 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1141
Chris Lattner92101ac2001-08-23 17:05:04 +00001142
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001143 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001144 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001145 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001146 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001147 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1148 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001149}
1150
1151// executeInstruction - Interpret a single instruction, increment the "PC", and
1152// return true if the next instruction is a breakpoint...
1153//
1154bool Interpreter::executeInstruction() {
1155 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1156
1157 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001158 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001159
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001160 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001161 CW << "Run:" << I;
1162
1163 // Set a sigsetjmp buffer so that we can recover if an error happens during
1164 // instruction execution...
1165 //
1166 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1167 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001168 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001169 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001170 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001171 // If -abort-on-exception was specified, terminate LLI instead of trying
1172 // to debug it.
1173 //
1174 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001175 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001176 cout << "CTRL-C Detected, execution halted.\n";
1177 }
1178 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001179 return true;
1180 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001181
Chris Lattner461f02f2001-11-07 05:31:27 +00001182 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001183 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001184 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001185 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001186 switch (I.getOpcode()) {
Chris Lattner2adcd832002-05-03 19:52:30 +00001187 case Instruction::Not: executeNotInst(cast<UnaryOperator>(I),SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001188 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001189 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1190 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001191 // Memory Instructions
1192 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001193 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001194 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1195 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1196 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001197 case Instruction::GetElementPtr:
1198 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001199
1200 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001201 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1202 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1203 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1204 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1205 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001206 default:
1207 cout << "Don't know how to execute this instruction!\n-->" << I;
1208 }
1209 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001210 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001211
1212 // Reset the current frame location to the top of stack
1213 CurFrame = ECStack.size()-1;
1214
1215 if (CurFrame == -1) return false; // No breakpoint if no code
1216
1217 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001218 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001219}
1220
1221void Interpreter::stepInstruction() { // Do the 'step' command
1222 if (ECStack.empty()) {
1223 cout << "Error: no program running, cannot step!\n";
1224 return;
1225 }
1226
1227 // Run an instruction...
1228 executeInstruction();
1229
1230 // Print the next instruction to execute...
1231 printCurrentInstruction();
1232}
1233
1234// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001235void Interpreter::nextInstruction() { // Do the 'next' command
1236 if (ECStack.empty()) {
1237 cout << "Error: no program running, cannot 'next'!\n";
1238 return;
1239 }
1240
1241 // If this is a call instruction, step over the call instruction...
1242 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001243 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001244 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001245 // Step into the function...
1246 if (executeInstruction()) {
1247 // Hit a breakpoint, print current instruction, then return to user...
1248 cout << "Breakpoint hit!\n";
1249 printCurrentInstruction();
1250 return;
1251 }
1252
Chris Lattnera74a6b52001-10-29 14:08:33 +00001253 // If we we able to step into the function, finish it now. We might not be
1254 // able the step into a function, if it's external for example.
1255 if (ECStack.size() != StackSize)
1256 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001257 else
1258 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001259
Chris Lattner92101ac2001-08-23 17:05:04 +00001260 } else {
1261 // Normal instruction, just step...
1262 stepInstruction();
1263 }
1264}
1265
1266void Interpreter::run() {
1267 if (ECStack.empty()) {
1268 cout << "Error: no program running, cannot run!\n";
1269 return;
1270 }
1271
1272 bool HitBreakpoint = false;
1273 while (!ECStack.empty() && !HitBreakpoint) {
1274 // Run an instruction...
1275 HitBreakpoint = executeInstruction();
1276 }
1277
1278 if (HitBreakpoint) {
1279 cout << "Breakpoint hit!\n";
1280 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001281 // Print the next instruction to execute...
1282 printCurrentInstruction();
1283}
1284
1285void Interpreter::finish() {
1286 if (ECStack.empty()) {
1287 cout << "Error: no program running, cannot run!\n";
1288 return;
1289 }
1290
1291 unsigned StackSize = ECStack.size();
1292 bool HitBreakpoint = false;
1293 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1294 // Run an instruction...
1295 HitBreakpoint = executeInstruction();
1296 }
1297
1298 if (HitBreakpoint) {
1299 cout << "Breakpoint hit!\n";
1300 }
1301
1302 // Print the next instruction to execute...
1303 printCurrentInstruction();
1304}
1305
1306
1307
1308// printCurrentInstruction - Print out the instruction that the virtual PC is
1309// at, or fail silently if no program is running.
1310//
1311void Interpreter::printCurrentInstruction() {
1312 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001313 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1314 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1315
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001316 Instruction &I = *ECStack.back().CurInst;
1317 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001318 assert(IN && "Instruction has no numbering annotation!");
1319 cout << "#" << IN->InstNum << I;
1320 }
1321}
1322
1323void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001324 switch (Ty->getPrimitiveID()) {
1325 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1326 case Type::SByteTyID: cout << V.SByteVal; break;
1327 case Type::UByteTyID: cout << V.UByteVal; break;
1328 case Type::ShortTyID: cout << V.ShortVal; break;
1329 case Type::UShortTyID: cout << V.UShortVal; break;
1330 case Type::IntTyID: cout << V.IntVal; break;
1331 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001332 case Type::LongTyID: cout << (long)V.LongVal; break;
1333 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001334 case Type::FloatTyID: cout << V.FloatVal; break;
1335 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001336 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001337 default:
1338 cout << "- Don't know how to print value of this type!";
1339 break;
1340 }
1341}
1342
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001343void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001344 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001345 printValue(Ty, V);
1346}
1347
Chris Lattner697954c2002-01-20 22:54:45 +00001348void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001349 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1350 if (!PickedVal) return;
1351
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001352 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1353 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001354 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001355 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001356 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1357 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001358 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001359 print(PickedVal->getType(),
1360 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001361 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001362 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001363}
1364
Chris Lattner697954c2002-01-20 22:54:45 +00001365void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001366 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1367 if (!PickedVal) return;
1368
1369 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001370 print(PickedVal->getType(),
1371 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001372 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001373 printOperandInfo(PickedVal, ECStack[CurFrame]);
1374}
1375
Chris Lattner461f02f2001-11-07 05:31:27 +00001376// printStackFrame - Print information about the specified stack frame, or -1
1377// for the default one.
1378//
Chris Lattner601d7152002-07-25 17:37:05 +00001379void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001380 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001381 Function *F = ECStack[FrameNo].CurMethod;
1382 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001383
1384 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001385 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001386
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001387 unsigned i = 0;
1388 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001389 if (i != 0) cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001390 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001391
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001392 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001393 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001394
Chris Lattner697954c2002-01-20 22:54:45 +00001395 cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001396
1397 if (FrameNo != int(ECStack.size()-1)) {
1398 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1399 CW << --I;
1400 } else {
1401 CW << *ECStack[FrameNo].CurInst;
1402 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001403}
Chris Lattner461f02f2001-11-07 05:31:27 +00001404