blob: b8d1c34d86711d45db551c5b059b99a4459ecd1d [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 Lattnerf23eb852001-12-14 16:49:29 +000017#include "Support/CommandLine.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000018#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000019#include <signal.h>
20#include <setjmp.h>
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::vector;
22using std::cout;
23using std::cerr;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000024
Chris Lattner5ff62e92002-07-22 02:10:13 +000025static cl::opt<bool>
26QuietMode("quiet", cl::desc("Do not emit any non-program output"));
27
28static cl::alias
29QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
30
31static cl::opt<bool>
32ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
33
34static cl::opt<bool>
35AbortOnExceptions("abort-on-exception",
36 cl::desc("Halt execution on a machine exception"));
Chris Lattnere9bb2df2001-12-03 22:26:30 +000037
Chris Lattner2e42d3a2001-10-15 05:51:48 +000038// Create a TargetData structure to handle memory addressing and size/alignment
39// computations
40//
Chris Lattnerdbaf74d2002-10-02 21:10:48 +000041TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000042CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000043
44
Chris Lattnere2409062001-11-12 16:19:45 +000045#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner5ff62e92002-07-22 02:10:13 +000046static cl::opt<bool>
47ProfileStructureFields("profilestructfields",
48 cl::desc("Profile Structure Field Accesses"));
Chris Lattnere2409062001-11-12 16:19:45 +000049#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000050static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000051#endif
52
Chris Lattner5af0c482001-11-07 04:23:00 +000053sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000054static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000055
56extern "C" {
57static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000058 if (InInstruction)
59 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000060}
61}
62
63static void initializeSignalHandlers() {
64 struct sigaction Action;
65 Action.sa_handler = SigHandler;
66 Action.sa_flags = SA_SIGINFO;
67 sigemptyset(&Action.sa_mask);
68 sigaction(SIGSEGV, &Action, 0);
69 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000070 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000071 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000072}
73
Chris Lattner2e42d3a2001-10-15 05:51:48 +000074
75//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000076// Value Manipulation code
77//===----------------------------------------------------------------------===//
78
79static unsigned getOperandSlot(Value *V) {
80 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
81 assert(SN && "Operand does not have a slot number annotation!");
82 return SN->SlotNum;
83}
84
85#define GET_CONST_VAL(TY, CLASS) \
Chris Lattnerfddc7552002-10-15 20:34:05 +000086 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattner39bb5b42001-10-15 13:25:40 +000087
Chris Lattnera34c5682002-08-27 22:33:45 +000088// Operations used by constant expr implementations...
89static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
90 ExecutionContext &SF);
91static GenericValue executeGEPOperation(Value *Src, User::op_iterator IdxBegin,
92 User::op_iterator IdxEnd,
93 ExecutionContext &SF);
94static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
95 const Type *Ty, ExecutionContext &SF);
96
Chris Lattnerfddc7552002-10-15 20:34:05 +000097static GenericValue getConstantValue(const Constant *C) {
98 GenericValue Result;
99 switch (C->getType()->getPrimitiveID()) {
100 GET_CONST_VAL(Bool , ConstantBool);
101 GET_CONST_VAL(UByte , ConstantUInt);
102 GET_CONST_VAL(SByte , ConstantSInt);
103 GET_CONST_VAL(UShort , ConstantUInt);
104 GET_CONST_VAL(Short , ConstantSInt);
105 GET_CONST_VAL(UInt , ConstantUInt);
106 GET_CONST_VAL(Int , ConstantSInt);
107 GET_CONST_VAL(ULong , ConstantUInt);
108 GET_CONST_VAL(Long , ConstantSInt);
109 GET_CONST_VAL(Float , ConstantFP);
110 GET_CONST_VAL(Double , ConstantFP);
111 case Type::PointerTyID:
112 if (isa<ConstantPointerNull>(C)) {
113 Result.PointerVal = 0;
114 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
115 GlobalAddress *Address =
116 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
117 Result.PointerVal = (PointerTy)Address->Ptr;
118 } else {
119 assert(0 && "Unknown constant pointer type!");
120 }
121 break;
122 default:
123 cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
124 }
125 return Result;
126}
127
Chris Lattner39bb5b42001-10-15 13:25:40 +0000128static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000129 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
130 switch (CE->getOpcode()) {
131 case Instruction::Cast:
132 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
133 case Instruction::GetElementPtr:
134 return executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
135 CE->op_end(), SF);
136 case Instruction::Add:
137 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getType(), SF);
140 default:
141 cerr << "Unhandled ConstantExpr: " << CE << "\n";
142 abort();
143 { GenericValue V; return V; }
144 }
145 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfddc7552002-10-15 20:34:05 +0000146 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000147 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
148 GlobalAddress *Address =
149 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
150 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000151 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000152 return Result;
153 } else {
154 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000155 unsigned OpSlot = getOperandSlot(V);
156 assert(TyP < SF.Values.size() &&
157 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000158 return SF.Values[TyP][getOperandSlot(V)];
159 }
160}
161
162static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000163 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000164 cout << "Constant Pool Value\n";
165 } else if (isa<GlobalValue>(V)) {
166 cout << "Global Value\n";
167 } else {
168 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
169 unsigned Slot = getOperandSlot(V);
170 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000171 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
172 << " Contents=0x";
173
174 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
175 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
176 unsigned char Cur = Buf[i];
177 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
178 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
179 }
Chris Lattner697954c2002-01-20 22:54:45 +0000180 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000181 }
182}
183
184
185
186static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
187 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
188
Chris Lattner697954c2002-01-20 22:54:45 +0000189 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000190 SF.Values[TyP][getOperandSlot(V)] = Val;
191}
192
193
194//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000195// Annotation Wrangling code
196//===----------------------------------------------------------------------===//
197
198void Interpreter::initializeExecutionEngine() {
199 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
200 &MethodInfo::Create);
201 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
202 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000203 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000204}
205
Chris Lattnerfddc7552002-10-15 20:34:05 +0000206static void StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
207 const Type *Ty);
208
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000209// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000210// specified memory location...
211//
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000212static void InitializeMemory(const Constant *Init, char *Addr) {
Chris Lattnerfddc7552002-10-15 20:34:05 +0000213
214 if (Init->getType()->isFirstClassType()) {
215 GenericValue Val = getConstantValue(Init);
216 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
217 return;
218 }
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000219
220 switch (Init->getType()->getPrimitiveID()) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000221 case Type::ArrayTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000222 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000223 const vector<Use> &Val = CPA->getValues();
224 unsigned ElementSize =
225 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
226 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000227 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000228 return;
229 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000230
231 case Type::StructTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000232 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000233 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
234 const vector<Use> &Val = CPS->getValues();
235 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000236 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000237 Addr+SL->MemberOffsets[i]);
238 return;
239 }
240
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000241 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000242 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000243 assert(0 && "Unknown constant type to initialize memory with!");
244 }
245}
246
247Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
248 assert(AID == GlobalAddressAID);
249
250 // This annotation will only be created on GlobalValue objects...
251 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
252
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000253 if (isa<Function>(GVal)) {
254 // The GlobalAddress object for a function is just a pointer to function
255 // itself. Don't delete it when the annotation is gone though!
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000256 return new GlobalAddress(GVal, false);
257 }
258
259 // Handle the case of a global variable...
260 assert(isa<GlobalVariable>(GVal) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000261 "Global value found that isn't a function or global variable!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000262 GlobalVariable *GV = cast<GlobalVariable>(GVal);
263
264 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000265 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000266
267 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000268 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000269 assert(Addr != 0 && "Null pointer returned by malloc!");
270
271 // Initialize the memory if there is an initializer...
272 if (GV->hasInitializer())
273 InitializeMemory(GV->getInitializer(), (char*)Addr);
274
275 return new GlobalAddress(Addr, true); // Simply invoke the ctor
276}
277
Chris Lattner2adcd832002-05-03 19:52:30 +0000278//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000279// Binary Instruction Implementations
280//===----------------------------------------------------------------------===//
281
282#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
283 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
284
285static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
286 const Type *Ty, ExecutionContext &SF) {
287 GenericValue Dest;
288 switch (Ty->getPrimitiveID()) {
289 IMPLEMENT_BINARY_OPERATOR(+, UByte);
290 IMPLEMENT_BINARY_OPERATOR(+, SByte);
291 IMPLEMENT_BINARY_OPERATOR(+, UShort);
292 IMPLEMENT_BINARY_OPERATOR(+, Short);
293 IMPLEMENT_BINARY_OPERATOR(+, UInt);
294 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000295 IMPLEMENT_BINARY_OPERATOR(+, ULong);
296 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000297 IMPLEMENT_BINARY_OPERATOR(+, Float);
298 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000299 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000300 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000301 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 }
303 return Dest;
304}
305
306static GenericValue executeSubInst(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 Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000323 }
324 return Dest;
325}
326
Chris Lattnerc2593162001-10-27 08:28:11 +0000327static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
328 const Type *Ty, ExecutionContext &SF) {
329 GenericValue Dest;
330 switch (Ty->getPrimitiveID()) {
331 IMPLEMENT_BINARY_OPERATOR(*, UByte);
332 IMPLEMENT_BINARY_OPERATOR(*, SByte);
333 IMPLEMENT_BINARY_OPERATOR(*, UShort);
334 IMPLEMENT_BINARY_OPERATOR(*, Short);
335 IMPLEMENT_BINARY_OPERATOR(*, UInt);
336 IMPLEMENT_BINARY_OPERATOR(*, Int);
337 IMPLEMENT_BINARY_OPERATOR(*, ULong);
338 IMPLEMENT_BINARY_OPERATOR(*, Long);
339 IMPLEMENT_BINARY_OPERATOR(*, Float);
340 IMPLEMENT_BINARY_OPERATOR(*, Double);
341 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
342 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000343 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000344 }
345 return Dest;
346}
347
348static GenericValue executeDivInst(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 Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000365 }
366 return Dest;
367}
368
369static GenericValue executeRemInst(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(%, Pointer);
382 case Type::FloatTyID:
383 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
384 break;
385 case Type::DoubleTyID:
386 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
387 break;
388 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000389 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000390 }
391 return Dest;
392}
393
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000394static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000395 const Type *Ty, ExecutionContext &SF) {
396 GenericValue Dest;
397 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000398 IMPLEMENT_BINARY_OPERATOR(&, UByte);
399 IMPLEMENT_BINARY_OPERATOR(&, SByte);
400 IMPLEMENT_BINARY_OPERATOR(&, UShort);
401 IMPLEMENT_BINARY_OPERATOR(&, Short);
402 IMPLEMENT_BINARY_OPERATOR(&, UInt);
403 IMPLEMENT_BINARY_OPERATOR(&, Int);
404 IMPLEMENT_BINARY_OPERATOR(&, ULong);
405 IMPLEMENT_BINARY_OPERATOR(&, Long);
406 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
407 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000408 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000409 }
410 return Dest;
411}
412
413
414static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
415 const Type *Ty, ExecutionContext &SF) {
416 GenericValue Dest;
417 switch (Ty->getPrimitiveID()) {
418 IMPLEMENT_BINARY_OPERATOR(|, UByte);
419 IMPLEMENT_BINARY_OPERATOR(|, SByte);
420 IMPLEMENT_BINARY_OPERATOR(|, UShort);
421 IMPLEMENT_BINARY_OPERATOR(|, Short);
422 IMPLEMENT_BINARY_OPERATOR(|, UInt);
423 IMPLEMENT_BINARY_OPERATOR(|, Int);
424 IMPLEMENT_BINARY_OPERATOR(|, ULong);
425 IMPLEMENT_BINARY_OPERATOR(|, Long);
426 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
427 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000428 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000429 }
430 return Dest;
431}
432
433
434static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
435 const Type *Ty, ExecutionContext &SF) {
436 GenericValue Dest;
437 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000438 IMPLEMENT_BINARY_OPERATOR(^, UByte);
439 IMPLEMENT_BINARY_OPERATOR(^, SByte);
440 IMPLEMENT_BINARY_OPERATOR(^, UShort);
441 IMPLEMENT_BINARY_OPERATOR(^, Short);
442 IMPLEMENT_BINARY_OPERATOR(^, UInt);
443 IMPLEMENT_BINARY_OPERATOR(^, Int);
444 IMPLEMENT_BINARY_OPERATOR(^, ULong);
445 IMPLEMENT_BINARY_OPERATOR(^, Long);
446 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
447 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000448 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000449 }
450 return Dest;
451}
452
453
Chris Lattner92101ac2001-08-23 17:05:04 +0000454#define IMPLEMENT_SETCC(OP, TY) \
455 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
456
Chris Lattner92101ac2001-08-23 17:05:04 +0000457static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
458 const Type *Ty, ExecutionContext &SF) {
459 GenericValue Dest;
460 switch (Ty->getPrimitiveID()) {
461 IMPLEMENT_SETCC(==, UByte);
462 IMPLEMENT_SETCC(==, SByte);
463 IMPLEMENT_SETCC(==, UShort);
464 IMPLEMENT_SETCC(==, Short);
465 IMPLEMENT_SETCC(==, UInt);
466 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000467 IMPLEMENT_SETCC(==, ULong);
468 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 IMPLEMENT_SETCC(==, Float);
470 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000471 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000472 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000473 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 }
475 return Dest;
476}
477
478static GenericValue executeSetNEInst(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 Lattnerea38c0e2001-11-07 19:46:27 +0000493
Chris Lattner92101ac2001-08-23 17:05:04 +0000494 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000495 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 }
497 return Dest;
498}
499
500static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
501 const Type *Ty, ExecutionContext &SF) {
502 GenericValue Dest;
503 switch (Ty->getPrimitiveID()) {
504 IMPLEMENT_SETCC(<=, UByte);
505 IMPLEMENT_SETCC(<=, SByte);
506 IMPLEMENT_SETCC(<=, UShort);
507 IMPLEMENT_SETCC(<=, Short);
508 IMPLEMENT_SETCC(<=, UInt);
509 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000510 IMPLEMENT_SETCC(<=, ULong);
511 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000512 IMPLEMENT_SETCC(<=, Float);
513 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000514 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000515 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000516 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 }
518 return Dest;
519}
520
521static GenericValue executeSetGEInst(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 SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 }
539 return Dest;
540}
541
542static GenericValue executeSetLTInst(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 SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 }
560 return Dest;
561}
562
563static GenericValue executeSetGTInst(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 SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000580 }
581 return Dest;
582}
583
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000584static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
585 const Type *Ty = I.getOperand(0)->getType();
586 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
587 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000588 GenericValue R; // Result
589
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000590 switch (I.getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000591 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
592 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
593 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
594 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
595 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000596 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
597 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000598 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000599 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
600 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
601 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
602 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
603 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
604 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
605 default:
606 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000607 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000608 }
609
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000610 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000611}
612
Chris Lattner92101ac2001-08-23 17:05:04 +0000613//===----------------------------------------------------------------------===//
614// Terminator Instruction Implementations
615//===----------------------------------------------------------------------===//
616
Chris Lattnera95c6992001-11-12 16:28:48 +0000617static void PerformExitStuff() {
618#ifdef PROFILE_STRUCTURE_FIELDS
619 // Print out structure field accounting information...
620 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000621 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000622 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000623 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
624 for (; I != E; ++I) {
625 vector<unsigned> &OfC = I->second;
626 CW << " '" << (Value*)I->first << "'\t- Sum=";
627
628 unsigned Sum = 0;
629 for (unsigned i = 0; i < OfC.size(); ++i)
630 Sum += OfC[i];
631 CW << Sum << " - ";
632
633 for (unsigned i = 0; i < OfC.size(); ++i) {
634 if (i) CW << ", ";
635 CW << OfC[i];
636 }
Chris Lattner697954c2002-01-20 22:54:45 +0000637 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000638 }
Chris Lattner697954c2002-01-20 22:54:45 +0000639 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000640
641 CW << "Profile Field Access Percentages:\n";
642 cout.precision(3);
643 for (I = FieldAccessCounts.begin(); I != E; ++I) {
644 vector<unsigned> &OfC = I->second;
645 unsigned Sum = 0;
646 for (unsigned i = 0; i < OfC.size(); ++i)
647 Sum += OfC[i];
648
649 CW << " '" << (Value*)I->first << "'\t- ";
650 for (unsigned i = 0; i < OfC.size(); ++i) {
651 if (i) CW << ", ";
652 CW << double(OfC[i])/Sum;
653 }
Chris Lattner697954c2002-01-20 22:54:45 +0000654 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000655 }
Chris Lattner697954c2002-01-20 22:54:45 +0000656 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000657
Chris Lattnera95c6992001-11-12 16:28:48 +0000658 FieldAccessCounts.clear();
659 }
660#endif
661}
662
Chris Lattnere43db882001-10-27 04:15:57 +0000663void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000664 if (!QuietMode) {
665 cout << "Program returned ";
666 print(Type::IntTy, GV);
667 cout << " via 'void exit(int)'\n";
668 }
Chris Lattnere43db882001-10-27 04:15:57 +0000669
670 ExitCode = GV.SByteVal;
671 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000672 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000673}
674
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000675void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000676 const Type *RetTy = 0;
677 GenericValue Result;
678
679 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000680 if (I.getNumOperands()) {
681 RetTy = I.getReturnValue()->getType();
682 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000683 }
684
685 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000686 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000687
688 // Pop the current stack frame... this invalidates SF
689 ECStack.pop_back();
690
691 if (ECStack.empty()) { // Finished main. Put result into exit code...
692 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000693 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000694 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000695 << "\" returned ";
696 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000697 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000698 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000699
700 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000701 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000702 } else {
703 ExitCode = 0;
704 }
Chris Lattnere2409062001-11-12 16:19:45 +0000705
Chris Lattnera95c6992001-11-12 16:28:48 +0000706 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000707 return;
708 }
709
710 // If we have a previous stack frame, and we have a previous call, fill in
711 // the return value...
712 //
713 ExecutionContext &NewSF = ECStack.back();
714 if (NewSF.Caller) {
715 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
716 SetValue(NewSF.Caller, Result, NewSF);
717
718 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000719 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000720 // This must be a function that is executing because of a user 'call'
721 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000722 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000723 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000724 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000725 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000726 }
727}
728
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000729void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000730 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
731 BasicBlock *Dest;
732
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000733 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
734 if (!I.isUnconditional()) {
735 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000736 GenericValue CondVal = getOperandValue(Cond, SF);
737 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000738 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000739 }
740 SF.CurBB = Dest; // Update CurBB to branch destination
741 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
742}
743
744//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000745// Memory Instruction Implementations
746//===----------------------------------------------------------------------===//
747
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000748void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
749 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000750
Chris Lattnercc82cc12002-04-28 21:57:33 +0000751 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000752 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000753
754 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000755 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000756 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
757
758 GenericValue Result;
759 Result.PointerVal = (PointerTy)Memory;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000760 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000761 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000762
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000763 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000764 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000765}
766
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000767static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
768 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
769 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000770 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000771 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000772}
773
Chris Lattner95c3af52001-10-29 19:32:19 +0000774
Chris Lattnera34c5682002-08-27 22:33:45 +0000775// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000776//
Chris Lattnera34c5682002-08-27 22:33:45 +0000777static GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
778 User::op_iterator E,
779 ExecutionContext &SF) {
780 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000781 "Cannot getElementOffset of a nonpointer type!");
782
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000783 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000784 const Type *Ty = Ptr->getType();
785
786 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000787 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
788 const StructLayout *SLO = TD.getStructLayout(STy);
789
790 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000791 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000792 assert(CPU->getType() == Type::UByteTy);
793 unsigned Index = CPU->getValue();
794
Chris Lattnere2409062001-11-12 16:19:45 +0000795#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000796 if (ProfileStructureFields) {
797 // Do accounting for this field...
798 vector<unsigned> &OfC = FieldAccessCounts[STy];
799 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
800 OfC[Index]++;
801 }
Chris Lattnere2409062001-11-12 16:19:45 +0000802#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000803
804 Total += SLO->MemberOffsets[Index];
805 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000806 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000807
Chris Lattner782b9392001-11-26 18:18:18 +0000808 // Get the index number for the array... which must be uint type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000809 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000810 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000811 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000812 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000813 cerr << "Out of range memory access to element #" << Idx
814 << " of a " << AT->getNumElements() << " element array."
Chris Lattnera34c5682002-08-27 22:33:45 +0000815 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000816 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000817 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000818 }
Chris Lattner782b9392001-11-26 18:18:18 +0000819
Chris Lattnerf23eb852001-12-14 16:49:29 +0000820 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000821 unsigned Size = TD.getTypeSize(Ty);
822 Total += Size*Idx;
823 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000824 }
825
Chris Lattnera34c5682002-08-27 22:33:45 +0000826 GenericValue Result;
827 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
828 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000829}
830
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000831static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000832 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
833 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000834}
835
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000836static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
837 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattner24ea74e2002-08-22 22:49:05 +0000838 GenericValue *Ptr = (GenericValue*)SRC.PointerVal;
Chris Lattner86660982001-08-27 05:16:50 +0000839 GenericValue Result;
840
Chris Lattnerfddc7552002-10-15 20:34:05 +0000841 if (TD.isLittleEndian()) {
842 switch (I.getType()->getPrimitiveID()) {
843 case Type::BoolTyID:
844 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000845 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000846 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000847 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
848 ((unsigned)Ptr->Untyped[1] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000849 break;
850 case Type::FloatTyID:
851 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000852 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
853 ((unsigned)Ptr->Untyped[1] << 8) |
854 ((unsigned)Ptr->Untyped[2] << 16) |
855 ((unsigned)Ptr->Untyped[3] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000856 break;
857 case Type::DoubleTyID:
858 case Type::ULongTyID:
859 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000860 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
861 ((uint64_t)Ptr->Untyped[1] << 8) |
862 ((uint64_t)Ptr->Untyped[2] << 16) |
863 ((uint64_t)Ptr->Untyped[3] << 24) |
864 ((uint64_t)Ptr->Untyped[4] << 32) |
865 ((uint64_t)Ptr->Untyped[5] << 40) |
866 ((uint64_t)Ptr->Untyped[6] << 48) |
867 ((uint64_t)Ptr->Untyped[7] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000868 break;
869 default:
870 cout << "Cannot load value of type " << I.getType() << "!\n";
871 }
872 } else {
873 switch (I.getType()->getPrimitiveID()) {
874 case Type::BoolTyID:
875 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000876 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000877 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000878 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
879 ((unsigned)Ptr->Untyped[0] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000880 break;
881 case Type::FloatTyID:
882 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000883 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
884 ((unsigned)Ptr->Untyped[2] << 8) |
885 ((unsigned)Ptr->Untyped[1] << 16) |
886 ((unsigned)Ptr->Untyped[0] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000887 break;
888 case Type::DoubleTyID:
889 case Type::ULongTyID:
890 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000891 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
892 ((uint64_t)Ptr->Untyped[6] << 8) |
893 ((uint64_t)Ptr->Untyped[5] << 16) |
894 ((uint64_t)Ptr->Untyped[4] << 24) |
895 ((uint64_t)Ptr->Untyped[3] << 32) |
896 ((uint64_t)Ptr->Untyped[2] << 40) |
897 ((uint64_t)Ptr->Untyped[1] << 48) |
898 ((uint64_t)Ptr->Untyped[0] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000899 break;
900 default:
901 cout << "Cannot load value of type " << I.getType() << "!\n";
902 }
Chris Lattner86660982001-08-27 05:16:50 +0000903 }
904
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000905 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000906}
907
Chris Lattnerfddc7552002-10-15 20:34:05 +0000908static void StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
909 const Type *Ty) {
910 if (TD.isLittleEndian()) {
911 switch (Ty->getPrimitiveID()) {
912 case Type::BoolTyID:
913 case Type::UByteTyID:
914 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
915 case Type::UShortTyID:
916 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
917 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
918 break;
919 case Type::FloatTyID:
920 case Type::UIntTyID:
921 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
922 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
923 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
924 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
925 break;
926 case Type::DoubleTyID:
927 case Type::ULongTyID:
928 case Type::LongTyID:
929 case Type::PointerTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
930 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
931 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
932 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
933 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
934 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
935 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
936 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
937 break;
938 default:
Chris Lattner683d5da92002-10-26 01:57:15 +0000939 cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerfddc7552002-10-15 20:34:05 +0000940 }
941 } else {
942 switch (Ty->getPrimitiveID()) {
943 case Type::BoolTyID:
944 case Type::UByteTyID:
945 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
946 case Type::UShortTyID:
947 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
948 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
949 break;
950 case Type::FloatTyID:
951 case Type::UIntTyID:
952 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
953 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
954 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
955 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
956 break;
957 case Type::DoubleTyID:
958 case Type::ULongTyID:
959 case Type::LongTyID:
960 case Type::PointerTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
961 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
962 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
963 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
964 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
965 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
966 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
967 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
968 break;
969 default:
Chris Lattner683d5da92002-10-26 01:57:15 +0000970 cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerfddc7552002-10-15 20:34:05 +0000971 }
Chris Lattner86660982001-08-27 05:16:50 +0000972 }
973}
974
Chris Lattnerfddc7552002-10-15 20:34:05 +0000975static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
976 GenericValue Val = getOperandValue(I.getOperand(0), SF);
977 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattner683d5da92002-10-26 01:57:15 +0000978 StoreValueToMemory(Val, (GenericValue *)SRC.PointerVal,
979 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000980}
981
Chris Lattner86660982001-08-27 05:16:50 +0000982
983//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000984// Miscellaneous Instruction Implementations
985//===----------------------------------------------------------------------===//
986
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000987void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
988 ECStack.back().Caller = &I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000989 vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000990 ArgVals.reserve(I.getNumOperands()-1);
991 for (unsigned i = 1; i < I.getNumOperands(); ++i)
992 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner365a76e2001-09-10 04:49:44 +0000993
Chris Lattner070cf5e2001-11-07 20:12:30 +0000994 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000995 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000996 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +0000997
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000998 callMethod((Function*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000999}
1000
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001001static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001002 BasicBlock *PrevBB = SF.PrevBB;
1003 Value *IncomingValue = 0;
1004
1005 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001006 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
1007 if (I.getIncomingBlock(--i) == PrevBB) {
1008 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +00001009 break;
1010 }
1011 }
1012 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
1013
1014 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001015 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001016}
1017
Chris Lattner86660982001-08-27 05:16:50 +00001018#define IMPLEMENT_SHIFT(OP, TY) \
1019 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
1020
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001021static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
1022 const Type *Ty = I.getOperand(0)->getType();
1023 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1024 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001025 GenericValue Dest;
1026
1027 switch (Ty->getPrimitiveID()) {
1028 IMPLEMENT_SHIFT(<<, UByte);
1029 IMPLEMENT_SHIFT(<<, SByte);
1030 IMPLEMENT_SHIFT(<<, UShort);
1031 IMPLEMENT_SHIFT(<<, Short);
1032 IMPLEMENT_SHIFT(<<, UInt);
1033 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +00001034 IMPLEMENT_SHIFT(<<, ULong);
1035 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +00001036 IMPLEMENT_SHIFT(<<, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001037 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001038 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001039 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001040 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001041}
1042
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001043static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
1044 const Type *Ty = I.getOperand(0)->getType();
1045 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1046 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001047 GenericValue Dest;
1048
1049 switch (Ty->getPrimitiveID()) {
1050 IMPLEMENT_SHIFT(>>, UByte);
1051 IMPLEMENT_SHIFT(>>, SByte);
1052 IMPLEMENT_SHIFT(>>, UShort);
1053 IMPLEMENT_SHIFT(>>, Short);
1054 IMPLEMENT_SHIFT(>>, UInt);
1055 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +00001056 IMPLEMENT_SHIFT(>>, ULong);
1057 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +00001058 IMPLEMENT_SHIFT(>>, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001059 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001060 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001061 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001062 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001063}
1064
1065#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001066 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001067
1068#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1069 case Type::DESTTY##TyID: \
1070 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001071 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001072 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1073 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1074 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1075 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1076 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001077 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1078 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001079 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1080 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001081
1082#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1083 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1084 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1085
1086#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +00001087 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +00001088 break; \
1089 } \
1090 break
1091
1092#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1093 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1094 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001095 IMPLEMENT_CAST_CASE_END()
1096
Chris Lattnera34c5682002-08-27 22:33:45 +00001097static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
1098 ExecutionContext &SF) {
1099 const Type *SrcTy = SrcVal->getType();
1100 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001101
1102 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001103 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1104 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1105 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001106 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001107 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1108 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1109 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1110 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001111 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001112 IMPLEMENT_CAST_CASE(Float , (float));
1113 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001114 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001115 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001116 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001117
1118 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001119}
Chris Lattner92101ac2001-08-23 17:05:04 +00001120
1121
Chris Lattnera34c5682002-08-27 22:33:45 +00001122static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1123 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1124}
Chris Lattner92101ac2001-08-23 17:05:04 +00001125
1126
1127//===----------------------------------------------------------------------===//
1128// Dispatch and Execution Code
1129//===----------------------------------------------------------------------===//
1130
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001131MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001132 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001133 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1134 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001135
1136 // Iterate over all of the instructions...
1137 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001138 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1139 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1140 // For each instruction... Add Annote
1141 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001142}
1143
1144unsigned MethodInfo::getValueSlot(const Value *V) {
1145 unsigned Plane = V->getType()->getUniqueID();
1146 if (Plane >= NumPlaneElements.size())
1147 NumPlaneElements.resize(Plane+1, 0);
1148 return NumPlaneElements[Plane]++;
1149}
1150
1151
Chris Lattner92101ac2001-08-23 17:05:04 +00001152//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001153// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001154//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001155void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001156 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1157 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1158 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001159 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001160 GenericValue Result = callExternalMethod(M, ArgVals);
1161 const Type *RetTy = M->getReturnType();
1162
1163 // Copy the result back into the result variable if we are not returning
1164 // void.
1165 if (RetTy != Type::VoidTy) {
1166 if (!ECStack.empty() && ECStack.back().Caller) {
1167 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001168 SetValue(SF.Caller, Result, SF);
1169
1170 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001171 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001172 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001173 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001174 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001175 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001176 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001177
1178 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +00001179 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +00001180 }
1181 }
1182
Chris Lattner92101ac2001-08-23 17:05:04 +00001183 return;
1184 }
1185
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001186 // Process the function, assigning instruction numbers to the instructions in
1187 // the function. Also calculate the number of values for each type slot
1188 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001189 //
1190 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001191 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001192
Chris Lattner92101ac2001-08-23 17:05:04 +00001193 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1194 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001195 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001196 StackFrame.CurInst = StackFrame.CurBB->begin();
1197 StackFrame.MethInfo = MethInfo;
1198
1199 // Initialize the values to nothing...
1200 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001201 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001202 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1203
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001204 // Taint the initial values of stuff
1205 memset(&StackFrame.Values[i][0], 42,
1206 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1207 }
1208
Chris Lattner92101ac2001-08-23 17:05:04 +00001209 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1210
Chris Lattner92101ac2001-08-23 17:05:04 +00001211
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001212 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001213 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001214 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001215 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001216 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1217 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001218}
1219
1220// executeInstruction - Interpret a single instruction, increment the "PC", and
1221// return true if the next instruction is a breakpoint...
1222//
1223bool Interpreter::executeInstruction() {
1224 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1225
1226 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001227 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001228
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001229 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001230 CW << "Run:" << I;
1231
1232 // Set a sigsetjmp buffer so that we can recover if an error happens during
1233 // instruction execution...
1234 //
1235 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1236 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001237 if (SigNo != SIGINT) {
Chris Lattner8b77be22002-09-13 14:41:38 +00001238 cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001239 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001240 // If -abort-on-exception was specified, terminate LLI instead of trying
1241 // to debug it.
1242 //
1243 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001244 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001245 cout << "CTRL-C Detected, execution halted.\n";
1246 }
1247 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001248 return true;
1249 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001250
Chris Lattner461f02f2001-11-07 05:31:27 +00001251 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001252 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001253 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001254 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001255 switch (I.getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001256 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001257 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1258 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001259 // Memory Instructions
1260 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001261 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001262 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1263 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1264 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001265 case Instruction::GetElementPtr:
1266 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001267
1268 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001269 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1270 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1271 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1272 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1273 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001274 default:
1275 cout << "Don't know how to execute this instruction!\n-->" << I;
1276 }
1277 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001278 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001279
1280 // Reset the current frame location to the top of stack
1281 CurFrame = ECStack.size()-1;
1282
1283 if (CurFrame == -1) return false; // No breakpoint if no code
1284
1285 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001286 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001287}
1288
1289void Interpreter::stepInstruction() { // Do the 'step' command
1290 if (ECStack.empty()) {
1291 cout << "Error: no program running, cannot step!\n";
1292 return;
1293 }
1294
1295 // Run an instruction...
1296 executeInstruction();
1297
1298 // Print the next instruction to execute...
1299 printCurrentInstruction();
1300}
1301
1302// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001303void Interpreter::nextInstruction() { // Do the 'next' command
1304 if (ECStack.empty()) {
1305 cout << "Error: no program running, cannot 'next'!\n";
1306 return;
1307 }
1308
1309 // If this is a call instruction, step over the call instruction...
1310 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001311 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001312 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001313 // Step into the function...
1314 if (executeInstruction()) {
1315 // Hit a breakpoint, print current instruction, then return to user...
1316 cout << "Breakpoint hit!\n";
1317 printCurrentInstruction();
1318 return;
1319 }
1320
Chris Lattnera74a6b52001-10-29 14:08:33 +00001321 // If we we able to step into the function, finish it now. We might not be
1322 // able the step into a function, if it's external for example.
1323 if (ECStack.size() != StackSize)
1324 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001325 else
1326 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001327
Chris Lattner92101ac2001-08-23 17:05:04 +00001328 } else {
1329 // Normal instruction, just step...
1330 stepInstruction();
1331 }
1332}
1333
1334void Interpreter::run() {
1335 if (ECStack.empty()) {
1336 cout << "Error: no program running, cannot run!\n";
1337 return;
1338 }
1339
1340 bool HitBreakpoint = false;
1341 while (!ECStack.empty() && !HitBreakpoint) {
1342 // Run an instruction...
1343 HitBreakpoint = executeInstruction();
1344 }
1345
1346 if (HitBreakpoint) {
1347 cout << "Breakpoint hit!\n";
1348 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001349 // Print the next instruction to execute...
1350 printCurrentInstruction();
1351}
1352
1353void Interpreter::finish() {
1354 if (ECStack.empty()) {
1355 cout << "Error: no program running, cannot run!\n";
1356 return;
1357 }
1358
1359 unsigned StackSize = ECStack.size();
1360 bool HitBreakpoint = false;
1361 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1362 // Run an instruction...
1363 HitBreakpoint = executeInstruction();
1364 }
1365
1366 if (HitBreakpoint) {
1367 cout << "Breakpoint hit!\n";
1368 }
1369
1370 // Print the next instruction to execute...
1371 printCurrentInstruction();
1372}
1373
1374
1375
1376// printCurrentInstruction - Print out the instruction that the virtual PC is
1377// at, or fail silently if no program is running.
1378//
1379void Interpreter::printCurrentInstruction() {
1380 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001381 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1382 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1383
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001384 Instruction &I = *ECStack.back().CurInst;
1385 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001386 assert(IN && "Instruction has no numbering annotation!");
1387 cout << "#" << IN->InstNum << I;
1388 }
1389}
1390
1391void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001392 switch (Ty->getPrimitiveID()) {
1393 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001394 case Type::SByteTyID:
1395 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1396 case Type::UByteTyID:
1397 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001398 case Type::ShortTyID: cout << V.ShortVal; break;
1399 case Type::UShortTyID: cout << V.UShortVal; break;
1400 case Type::IntTyID: cout << V.IntVal; break;
1401 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001402 case Type::LongTyID: cout << (long)V.LongVal; break;
1403 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001404 case Type::FloatTyID: cout << V.FloatVal; break;
1405 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001406 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001407 default:
1408 cout << "- Don't know how to print value of this type!";
1409 break;
1410 }
1411}
1412
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001413void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001414 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001415 printValue(Ty, V);
1416}
1417
Chris Lattner697954c2002-01-20 22:54:45 +00001418void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001419 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1420 if (!PickedVal) return;
1421
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001422 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1423 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001424 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001425 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001426 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1427 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001428 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001429 print(PickedVal->getType(),
1430 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001431 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001432 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001433}
1434
Chris Lattner697954c2002-01-20 22:54:45 +00001435void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001436 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1437 if (!PickedVal) return;
1438
1439 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001440 print(PickedVal->getType(),
1441 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001442 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001443 printOperandInfo(PickedVal, ECStack[CurFrame]);
1444}
1445
Chris Lattner461f02f2001-11-07 05:31:27 +00001446// printStackFrame - Print information about the specified stack frame, or -1
1447// for the default one.
1448//
Chris Lattner601d7152002-07-25 17:37:05 +00001449void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001450 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001451 Function *F = ECStack[FrameNo].CurMethod;
1452 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001453
1454 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001455 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001456
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001457 unsigned i = 0;
1458 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001459 if (i != 0) cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001460 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001461
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001462 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001463 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001464
Chris Lattner697954c2002-01-20 22:54:45 +00001465 cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001466
1467 if (FrameNo != int(ECStack.size()-1)) {
1468 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1469 CW << --I;
1470 } else {
1471 CW << *ECStack[FrameNo].CurInst;
1472 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001473}
Chris Lattner461f02f2001-11-07 05:31:27 +00001474