blob: ac011d743bef514dfd2c878d87d5192c9ea78087 [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//
41static TargetData 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) \
86 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
87
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 Lattner39bb5b42001-10-15 13:25:40 +000097static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000098 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
99 switch (CE->getOpcode()) {
100 case Instruction::Cast:
101 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
102 case Instruction::GetElementPtr:
103 return executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
104 CE->op_end(), SF);
105 case Instruction::Add:
106 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
107 getOperandValue(CE->getOperand(1), SF),
108 CE->getType(), SF);
109 default:
110 cerr << "Unhandled ConstantExpr: " << CE << "\n";
111 abort();
112 { GenericValue V; return V; }
113 }
114 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000115 GenericValue Result;
116 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000117 GET_CONST_VAL(Bool , ConstantBool);
118 GET_CONST_VAL(UByte , ConstantUInt);
119 GET_CONST_VAL(SByte , ConstantSInt);
120 GET_CONST_VAL(UShort , ConstantUInt);
121 GET_CONST_VAL(Short , ConstantSInt);
122 GET_CONST_VAL(UInt , ConstantUInt);
123 GET_CONST_VAL(Int , ConstantSInt);
124 GET_CONST_VAL(ULong , ConstantUInt);
125 GET_CONST_VAL(Long , ConstantSInt);
126 GET_CONST_VAL(Float , ConstantFP);
127 GET_CONST_VAL(Double , ConstantFP);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000128 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000129 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000130 Result.PointerVal = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000131 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
132 return getOperandValue(CPR->getValue(), SF);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000133 } else {
134 assert(0 && "Unknown constant pointer type!");
135 }
136 break;
137 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000138 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000139 }
140 return Result;
141 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
142 GlobalAddress *Address =
143 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
144 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000145 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000146 return Result;
147 } else {
148 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000149 unsigned OpSlot = getOperandSlot(V);
150 assert(TyP < SF.Values.size() &&
151 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000152 return SF.Values[TyP][getOperandSlot(V)];
153 }
154}
155
156static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000157 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000158 cout << "Constant Pool Value\n";
159 } else if (isa<GlobalValue>(V)) {
160 cout << "Global Value\n";
161 } else {
162 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
163 unsigned Slot = getOperandSlot(V);
164 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000165 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
166 << " Contents=0x";
167
168 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
169 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
170 unsigned char Cur = Buf[i];
171 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
172 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
173 }
Chris Lattner697954c2002-01-20 22:54:45 +0000174 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000175 }
176}
177
178
179
180static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
181 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
182
Chris Lattner697954c2002-01-20 22:54:45 +0000183 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000184 SF.Values[TyP][getOperandSlot(V)] = Val;
185}
186
187
188//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000189// Annotation Wrangling code
190//===----------------------------------------------------------------------===//
191
192void Interpreter::initializeExecutionEngine() {
193 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
194 &MethodInfo::Create);
195 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
196 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000197 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000198}
199
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000200// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000201// specified memory location...
202//
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000203static void InitializeMemory(const Constant *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000204#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
205 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000206 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000207 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000208 } return
209
210 switch (Init->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000211 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
212 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
213 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
214 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
215 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
216 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
217 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
218 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
219 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
220 INITIALIZE_MEMORY(Float , ConstantFP , float);
221 INITIALIZE_MEMORY(Double , ConstantFP , double);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000222#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000223
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000224 case Type::ArrayTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000225 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000226 const vector<Use> &Val = CPA->getValues();
227 unsigned ElementSize =
228 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
229 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000230 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000231 return;
232 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000233
234 case Type::StructTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000235 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000236 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
237 const vector<Use> &Val = CPS->getValues();
238 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000239 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000240 Addr+SL->MemberOffsets[i]);
241 return;
242 }
243
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000244 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000245 if (isa<ConstantPointerNull>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000246 *(void**)Addr = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000247 } else if (const ConstantPointerRef *CPR =
248 dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000249 GlobalAddress *Address =
250 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
251 *(void**)Addr = (GenericValue*)Address->Ptr;
252 } else {
253 assert(0 && "Unknown Constant pointer type!");
254 }
255 return;
256
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000257 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000258 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000259 assert(0 && "Unknown constant type to initialize memory with!");
260 }
261}
262
263Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
264 assert(AID == GlobalAddressAID);
265
266 // This annotation will only be created on GlobalValue objects...
267 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
268
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269 if (isa<Function>(GVal)) {
270 // The GlobalAddress object for a function is just a pointer to function
271 // itself. Don't delete it when the annotation is gone though!
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000272 return new GlobalAddress(GVal, false);
273 }
274
275 // Handle the case of a global variable...
276 assert(isa<GlobalVariable>(GVal) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000277 "Global value found that isn't a function or global variable!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000278 GlobalVariable *GV = cast<GlobalVariable>(GVal);
279
280 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000281 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000282
283 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000284 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000285 assert(Addr != 0 && "Null pointer returned by malloc!");
286
287 // Initialize the memory if there is an initializer...
288 if (GV->hasInitializer())
289 InitializeMemory(GV->getInitializer(), (char*)Addr);
290
291 return new GlobalAddress(Addr, true); // Simply invoke the ctor
292}
293
Chris Lattner2adcd832002-05-03 19:52:30 +0000294//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000295// Binary Instruction Implementations
296//===----------------------------------------------------------------------===//
297
298#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
299 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
300
301static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
302 const Type *Ty, ExecutionContext &SF) {
303 GenericValue Dest;
304 switch (Ty->getPrimitiveID()) {
305 IMPLEMENT_BINARY_OPERATOR(+, UByte);
306 IMPLEMENT_BINARY_OPERATOR(+, SByte);
307 IMPLEMENT_BINARY_OPERATOR(+, UShort);
308 IMPLEMENT_BINARY_OPERATOR(+, Short);
309 IMPLEMENT_BINARY_OPERATOR(+, UInt);
310 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000311 IMPLEMENT_BINARY_OPERATOR(+, ULong);
312 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000313 IMPLEMENT_BINARY_OPERATOR(+, Float);
314 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000315 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000316 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000317 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000318 }
319 return Dest;
320}
321
322static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
323 const Type *Ty, ExecutionContext &SF) {
324 GenericValue Dest;
325 switch (Ty->getPrimitiveID()) {
326 IMPLEMENT_BINARY_OPERATOR(-, UByte);
327 IMPLEMENT_BINARY_OPERATOR(-, SByte);
328 IMPLEMENT_BINARY_OPERATOR(-, UShort);
329 IMPLEMENT_BINARY_OPERATOR(-, Short);
330 IMPLEMENT_BINARY_OPERATOR(-, UInt);
331 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000332 IMPLEMENT_BINARY_OPERATOR(-, ULong);
333 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000334 IMPLEMENT_BINARY_OPERATOR(-, Float);
335 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000336 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000337 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000338 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000339 }
340 return Dest;
341}
342
Chris Lattnerc2593162001-10-27 08:28:11 +0000343static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
344 const Type *Ty, ExecutionContext &SF) {
345 GenericValue Dest;
346 switch (Ty->getPrimitiveID()) {
347 IMPLEMENT_BINARY_OPERATOR(*, UByte);
348 IMPLEMENT_BINARY_OPERATOR(*, SByte);
349 IMPLEMENT_BINARY_OPERATOR(*, UShort);
350 IMPLEMENT_BINARY_OPERATOR(*, Short);
351 IMPLEMENT_BINARY_OPERATOR(*, UInt);
352 IMPLEMENT_BINARY_OPERATOR(*, Int);
353 IMPLEMENT_BINARY_OPERATOR(*, ULong);
354 IMPLEMENT_BINARY_OPERATOR(*, Long);
355 IMPLEMENT_BINARY_OPERATOR(*, Float);
356 IMPLEMENT_BINARY_OPERATOR(*, Double);
357 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
358 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000359 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000360 }
361 return Dest;
362}
363
364static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
365 const Type *Ty, ExecutionContext &SF) {
366 GenericValue Dest;
367 switch (Ty->getPrimitiveID()) {
368 IMPLEMENT_BINARY_OPERATOR(/, UByte);
369 IMPLEMENT_BINARY_OPERATOR(/, SByte);
370 IMPLEMENT_BINARY_OPERATOR(/, UShort);
371 IMPLEMENT_BINARY_OPERATOR(/, Short);
372 IMPLEMENT_BINARY_OPERATOR(/, UInt);
373 IMPLEMENT_BINARY_OPERATOR(/, Int);
374 IMPLEMENT_BINARY_OPERATOR(/, ULong);
375 IMPLEMENT_BINARY_OPERATOR(/, Long);
376 IMPLEMENT_BINARY_OPERATOR(/, Float);
377 IMPLEMENT_BINARY_OPERATOR(/, Double);
378 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
379 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000380 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000381 }
382 return Dest;
383}
384
385static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
386 const Type *Ty, ExecutionContext &SF) {
387 GenericValue Dest;
388 switch (Ty->getPrimitiveID()) {
389 IMPLEMENT_BINARY_OPERATOR(%, UByte);
390 IMPLEMENT_BINARY_OPERATOR(%, SByte);
391 IMPLEMENT_BINARY_OPERATOR(%, UShort);
392 IMPLEMENT_BINARY_OPERATOR(%, Short);
393 IMPLEMENT_BINARY_OPERATOR(%, UInt);
394 IMPLEMENT_BINARY_OPERATOR(%, Int);
395 IMPLEMENT_BINARY_OPERATOR(%, ULong);
396 IMPLEMENT_BINARY_OPERATOR(%, Long);
397 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
398 case Type::FloatTyID:
399 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
400 break;
401 case Type::DoubleTyID:
402 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
403 break;
404 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000405 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000406 }
407 return Dest;
408}
409
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000410static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000411 const Type *Ty, ExecutionContext &SF) {
412 GenericValue Dest;
413 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000414 IMPLEMENT_BINARY_OPERATOR(&, UByte);
415 IMPLEMENT_BINARY_OPERATOR(&, SByte);
416 IMPLEMENT_BINARY_OPERATOR(&, UShort);
417 IMPLEMENT_BINARY_OPERATOR(&, Short);
418 IMPLEMENT_BINARY_OPERATOR(&, UInt);
419 IMPLEMENT_BINARY_OPERATOR(&, Int);
420 IMPLEMENT_BINARY_OPERATOR(&, ULong);
421 IMPLEMENT_BINARY_OPERATOR(&, Long);
422 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
423 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000424 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000425 }
426 return Dest;
427}
428
429
430static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
431 const Type *Ty, ExecutionContext &SF) {
432 GenericValue Dest;
433 switch (Ty->getPrimitiveID()) {
434 IMPLEMENT_BINARY_OPERATOR(|, UByte);
435 IMPLEMENT_BINARY_OPERATOR(|, SByte);
436 IMPLEMENT_BINARY_OPERATOR(|, UShort);
437 IMPLEMENT_BINARY_OPERATOR(|, Short);
438 IMPLEMENT_BINARY_OPERATOR(|, UInt);
439 IMPLEMENT_BINARY_OPERATOR(|, Int);
440 IMPLEMENT_BINARY_OPERATOR(|, ULong);
441 IMPLEMENT_BINARY_OPERATOR(|, Long);
442 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
443 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000444 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000445 }
446 return Dest;
447}
448
449
450static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
451 const Type *Ty, ExecutionContext &SF) {
452 GenericValue Dest;
453 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000454 IMPLEMENT_BINARY_OPERATOR(^, UByte);
455 IMPLEMENT_BINARY_OPERATOR(^, SByte);
456 IMPLEMENT_BINARY_OPERATOR(^, UShort);
457 IMPLEMENT_BINARY_OPERATOR(^, Short);
458 IMPLEMENT_BINARY_OPERATOR(^, UInt);
459 IMPLEMENT_BINARY_OPERATOR(^, Int);
460 IMPLEMENT_BINARY_OPERATOR(^, ULong);
461 IMPLEMENT_BINARY_OPERATOR(^, Long);
462 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
463 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000464 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000465 }
466 return Dest;
467}
468
469
Chris Lattner92101ac2001-08-23 17:05:04 +0000470#define IMPLEMENT_SETCC(OP, TY) \
471 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
472
Chris Lattner92101ac2001-08-23 17:05:04 +0000473static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
474 const Type *Ty, ExecutionContext &SF) {
475 GenericValue Dest;
476 switch (Ty->getPrimitiveID()) {
477 IMPLEMENT_SETCC(==, UByte);
478 IMPLEMENT_SETCC(==, SByte);
479 IMPLEMENT_SETCC(==, UShort);
480 IMPLEMENT_SETCC(==, Short);
481 IMPLEMENT_SETCC(==, UInt);
482 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000483 IMPLEMENT_SETCC(==, ULong);
484 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 IMPLEMENT_SETCC(==, Float);
486 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000487 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000488 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000489 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000490 }
491 return Dest;
492}
493
494static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
495 const Type *Ty, ExecutionContext &SF) {
496 GenericValue Dest;
497 switch (Ty->getPrimitiveID()) {
498 IMPLEMENT_SETCC(!=, UByte);
499 IMPLEMENT_SETCC(!=, SByte);
500 IMPLEMENT_SETCC(!=, UShort);
501 IMPLEMENT_SETCC(!=, Short);
502 IMPLEMENT_SETCC(!=, UInt);
503 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000504 IMPLEMENT_SETCC(!=, ULong);
505 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000506 IMPLEMENT_SETCC(!=, Float);
507 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000508 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000509
Chris Lattner92101ac2001-08-23 17:05:04 +0000510 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000511 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000512 }
513 return Dest;
514}
515
516static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
517 const Type *Ty, ExecutionContext &SF) {
518 GenericValue Dest;
519 switch (Ty->getPrimitiveID()) {
520 IMPLEMENT_SETCC(<=, UByte);
521 IMPLEMENT_SETCC(<=, SByte);
522 IMPLEMENT_SETCC(<=, UShort);
523 IMPLEMENT_SETCC(<=, Short);
524 IMPLEMENT_SETCC(<=, UInt);
525 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000526 IMPLEMENT_SETCC(<=, ULong);
527 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000528 IMPLEMENT_SETCC(<=, Float);
529 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000530 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000532 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 }
534 return Dest;
535}
536
537static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
538 const Type *Ty, ExecutionContext &SF) {
539 GenericValue Dest;
540 switch (Ty->getPrimitiveID()) {
541 IMPLEMENT_SETCC(>=, UByte);
542 IMPLEMENT_SETCC(>=, SByte);
543 IMPLEMENT_SETCC(>=, UShort);
544 IMPLEMENT_SETCC(>=, Short);
545 IMPLEMENT_SETCC(>=, UInt);
546 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000547 IMPLEMENT_SETCC(>=, ULong);
548 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000549 IMPLEMENT_SETCC(>=, Float);
550 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000551 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000552 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000553 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000554 }
555 return Dest;
556}
557
558static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
559 const Type *Ty, ExecutionContext &SF) {
560 GenericValue Dest;
561 switch (Ty->getPrimitiveID()) {
562 IMPLEMENT_SETCC(<, UByte);
563 IMPLEMENT_SETCC(<, SByte);
564 IMPLEMENT_SETCC(<, UShort);
565 IMPLEMENT_SETCC(<, Short);
566 IMPLEMENT_SETCC(<, UInt);
567 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000568 IMPLEMENT_SETCC(<, ULong);
569 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000570 IMPLEMENT_SETCC(<, Float);
571 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000572 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000573 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000574 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000575 }
576 return Dest;
577}
578
579static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
580 const Type *Ty, ExecutionContext &SF) {
581 GenericValue Dest;
582 switch (Ty->getPrimitiveID()) {
583 IMPLEMENT_SETCC(>, UByte);
584 IMPLEMENT_SETCC(>, SByte);
585 IMPLEMENT_SETCC(>, UShort);
586 IMPLEMENT_SETCC(>, Short);
587 IMPLEMENT_SETCC(>, UInt);
588 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000589 IMPLEMENT_SETCC(>, ULong);
590 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000591 IMPLEMENT_SETCC(>, Float);
592 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000593 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000594 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000595 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000596 }
597 return Dest;
598}
599
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000600static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
601 const Type *Ty = I.getOperand(0)->getType();
602 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
603 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000604 GenericValue R; // Result
605
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000606 switch (I.getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000607 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
608 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
609 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
610 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
611 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000612 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
613 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000614 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000615 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
616 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
617 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
618 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
619 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
620 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
621 default:
622 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000623 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000624 }
625
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000626 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000627}
628
Chris Lattner92101ac2001-08-23 17:05:04 +0000629//===----------------------------------------------------------------------===//
630// Terminator Instruction Implementations
631//===----------------------------------------------------------------------===//
632
Chris Lattnera95c6992001-11-12 16:28:48 +0000633static void PerformExitStuff() {
634#ifdef PROFILE_STRUCTURE_FIELDS
635 // Print out structure field accounting information...
636 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000637 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000638 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000639 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
640 for (; I != E; ++I) {
641 vector<unsigned> &OfC = I->second;
642 CW << " '" << (Value*)I->first << "'\t- Sum=";
643
644 unsigned Sum = 0;
645 for (unsigned i = 0; i < OfC.size(); ++i)
646 Sum += OfC[i];
647 CW << Sum << " - ";
648
649 for (unsigned i = 0; i < OfC.size(); ++i) {
650 if (i) CW << ", ";
651 CW << OfC[i];
652 }
Chris Lattner697954c2002-01-20 22:54:45 +0000653 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000654 }
Chris Lattner697954c2002-01-20 22:54:45 +0000655 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000656
657 CW << "Profile Field Access Percentages:\n";
658 cout.precision(3);
659 for (I = FieldAccessCounts.begin(); I != E; ++I) {
660 vector<unsigned> &OfC = I->second;
661 unsigned Sum = 0;
662 for (unsigned i = 0; i < OfC.size(); ++i)
663 Sum += OfC[i];
664
665 CW << " '" << (Value*)I->first << "'\t- ";
666 for (unsigned i = 0; i < OfC.size(); ++i) {
667 if (i) CW << ", ";
668 CW << double(OfC[i])/Sum;
669 }
Chris Lattner697954c2002-01-20 22:54:45 +0000670 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000671 }
Chris Lattner697954c2002-01-20 22:54:45 +0000672 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000673
Chris Lattnera95c6992001-11-12 16:28:48 +0000674 FieldAccessCounts.clear();
675 }
676#endif
677}
678
Chris Lattnere43db882001-10-27 04:15:57 +0000679void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000680 if (!QuietMode) {
681 cout << "Program returned ";
682 print(Type::IntTy, GV);
683 cout << " via 'void exit(int)'\n";
684 }
Chris Lattnere43db882001-10-27 04:15:57 +0000685
686 ExitCode = GV.SByteVal;
687 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000688 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000689}
690
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000691void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000692 const Type *RetTy = 0;
693 GenericValue Result;
694
695 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000696 if (I.getNumOperands()) {
697 RetTy = I.getReturnValue()->getType();
698 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000699 }
700
701 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000702 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000703
704 // Pop the current stack frame... this invalidates SF
705 ECStack.pop_back();
706
707 if (ECStack.empty()) { // Finished main. Put result into exit code...
708 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000709 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000710 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000711 << "\" returned ";
712 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000713 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000714 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000715
716 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000717 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000718 } else {
719 ExitCode = 0;
720 }
Chris Lattnere2409062001-11-12 16:19:45 +0000721
Chris Lattnera95c6992001-11-12 16:28:48 +0000722 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000723 return;
724 }
725
726 // If we have a previous stack frame, and we have a previous call, fill in
727 // the return value...
728 //
729 ExecutionContext &NewSF = ECStack.back();
730 if (NewSF.Caller) {
731 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
732 SetValue(NewSF.Caller, Result, NewSF);
733
734 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000735 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000736 // This must be a function that is executing because of a user 'call'
737 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000738 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000739 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000740 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000741 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000742 }
743}
744
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000745void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000746 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
747 BasicBlock *Dest;
748
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000749 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
750 if (!I.isUnconditional()) {
751 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000752 GenericValue CondVal = getOperandValue(Cond, SF);
753 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000754 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000755 }
756 SF.CurBB = Dest; // Update CurBB to branch destination
757 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
758}
759
760//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000761// Memory Instruction Implementations
762//===----------------------------------------------------------------------===//
763
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000764void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
765 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000766
Chris Lattnercc82cc12002-04-28 21:57:33 +0000767 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000769
770 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000771 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000772 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
773
774 GenericValue Result;
775 Result.PointerVal = (PointerTy)Memory;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000776 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000777 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000778
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000779 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000780 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000781}
782
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000783static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
784 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
785 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000786 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000787 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000788}
789
Chris Lattner95c3af52001-10-29 19:32:19 +0000790
Chris Lattnera34c5682002-08-27 22:33:45 +0000791// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000792//
Chris Lattnera34c5682002-08-27 22:33:45 +0000793static GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
794 User::op_iterator E,
795 ExecutionContext &SF) {
796 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000797 "Cannot getElementOffset of a nonpointer type!");
798
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000799 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000800 const Type *Ty = Ptr->getType();
801
802 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000803 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
804 const StructLayout *SLO = TD.getStructLayout(STy);
805
806 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000807 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000808 assert(CPU->getType() == Type::UByteTy);
809 unsigned Index = CPU->getValue();
810
Chris Lattnere2409062001-11-12 16:19:45 +0000811#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000812 if (ProfileStructureFields) {
813 // Do accounting for this field...
814 vector<unsigned> &OfC = FieldAccessCounts[STy];
815 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
816 OfC[Index]++;
817 }
Chris Lattnere2409062001-11-12 16:19:45 +0000818#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000819
820 Total += SLO->MemberOffsets[Index];
821 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000822 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000823
Chris Lattner782b9392001-11-26 18:18:18 +0000824 // Get the index number for the array... which must be uint type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000825 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000826 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000827 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000828 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000829 cerr << "Out of range memory access to element #" << Idx
830 << " of a " << AT->getNumElements() << " element array."
Chris Lattnera34c5682002-08-27 22:33:45 +0000831 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000832 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000833 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000834 }
Chris Lattner782b9392001-11-26 18:18:18 +0000835
Chris Lattnerf23eb852001-12-14 16:49:29 +0000836 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000837 unsigned Size = TD.getTypeSize(Ty);
838 Total += Size*Idx;
839 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000840 }
841
Chris Lattnera34c5682002-08-27 22:33:45 +0000842 GenericValue Result;
843 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
844 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000845}
846
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000847static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000848 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
849 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000850}
851
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000852static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
853 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattner24ea74e2002-08-22 22:49:05 +0000854 GenericValue *Ptr = (GenericValue*)SRC.PointerVal;
Chris Lattner86660982001-08-27 05:16:50 +0000855 GenericValue Result;
856
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000857 switch (I.getType()->getPrimitiveID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000858 case Type::BoolTyID:
859 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000860 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000861 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000862 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000863 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000864 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000865 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000866 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
867 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
868 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
869 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000870 default:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000871 cout << "Cannot load value of type " << I.getType() << "!\n";
Chris Lattner86660982001-08-27 05:16:50 +0000872 }
873
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000874 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000875}
876
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000877static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
878 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattner24ea74e2002-08-22 22:49:05 +0000879 GenericValue *Ptr = (GenericValue *)SRC.PointerVal;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000880 GenericValue Val = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000881
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000882 switch (I.getOperand(0)->getType()->getPrimitiveID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000883 case Type::BoolTyID:
884 case Type::UByteTyID:
885 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
886 case Type::UShortTyID:
887 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
888 case Type::UIntTyID:
889 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000890 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000891 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
892 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000893 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
894 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000895 default:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000896 cout << "Cannot store value of type " << I.getType() << "!\n";
Chris Lattner86660982001-08-27 05:16:50 +0000897 }
898}
899
900
901//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000902// Miscellaneous Instruction Implementations
903//===----------------------------------------------------------------------===//
904
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000905void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
906 ECStack.back().Caller = &I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000907 vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000908 ArgVals.reserve(I.getNumOperands()-1);
909 for (unsigned i = 1; i < I.getNumOperands(); ++i)
910 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner365a76e2001-09-10 04:49:44 +0000911
Chris Lattner070cf5e2001-11-07 20:12:30 +0000912 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000913 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000914 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +0000915
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000916 callMethod((Function*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000917}
918
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000919static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000920 BasicBlock *PrevBB = SF.PrevBB;
921 Value *IncomingValue = 0;
922
923 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000924 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
925 if (I.getIncomingBlock(--i) == PrevBB) {
926 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +0000927 break;
928 }
929 }
930 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
931
932 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000933 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000934}
935
Chris Lattner86660982001-08-27 05:16:50 +0000936#define IMPLEMENT_SHIFT(OP, TY) \
937 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
938
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000939static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
940 const Type *Ty = I.getOperand(0)->getType();
941 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
942 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000943 GenericValue Dest;
944
945 switch (Ty->getPrimitiveID()) {
946 IMPLEMENT_SHIFT(<<, UByte);
947 IMPLEMENT_SHIFT(<<, SByte);
948 IMPLEMENT_SHIFT(<<, UShort);
949 IMPLEMENT_SHIFT(<<, Short);
950 IMPLEMENT_SHIFT(<<, UInt);
951 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000952 IMPLEMENT_SHIFT(<<, ULong);
953 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000954 IMPLEMENT_SHIFT(<<, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000955 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000956 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000957 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000958 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000959}
960
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000961static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
962 const Type *Ty = I.getOperand(0)->getType();
963 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
964 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000965 GenericValue Dest;
966
967 switch (Ty->getPrimitiveID()) {
968 IMPLEMENT_SHIFT(>>, UByte);
969 IMPLEMENT_SHIFT(>>, SByte);
970 IMPLEMENT_SHIFT(>>, UShort);
971 IMPLEMENT_SHIFT(>>, Short);
972 IMPLEMENT_SHIFT(>>, UInt);
973 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000974 IMPLEMENT_SHIFT(>>, ULong);
975 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000976 IMPLEMENT_SHIFT(>>, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000977 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000978 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000979 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000980 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000981}
982
983#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000984 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000985
986#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
987 case Type::DESTTY##TyID: \
988 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000989 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000990 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
991 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
992 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
993 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
994 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000995 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
996 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000997 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
998 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000999
1000#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1001 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1002 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1003
1004#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +00001005 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +00001006 break; \
1007 } \
1008 break
1009
1010#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1011 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1012 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001013 IMPLEMENT_CAST_CASE_END()
1014
Chris Lattnera34c5682002-08-27 22:33:45 +00001015static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
1016 ExecutionContext &SF) {
1017 const Type *SrcTy = SrcVal->getType();
1018 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001019
1020 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001021 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1022 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1023 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001024 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001025 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1026 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1027 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1028 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1029 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1030 IMPLEMENT_CAST_CASE(Float , (float));
1031 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001032 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001033 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001034 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001035
1036 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001037}
Chris Lattner92101ac2001-08-23 17:05:04 +00001038
1039
Chris Lattnera34c5682002-08-27 22:33:45 +00001040static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1041 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1042}
Chris Lattner92101ac2001-08-23 17:05:04 +00001043
1044
1045//===----------------------------------------------------------------------===//
1046// Dispatch and Execution Code
1047//===----------------------------------------------------------------------===//
1048
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001049MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001050 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001051 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1052 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001053
1054 // Iterate over all of the instructions...
1055 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001056 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1057 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1058 // For each instruction... Add Annote
1059 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001060}
1061
1062unsigned MethodInfo::getValueSlot(const Value *V) {
1063 unsigned Plane = V->getType()->getUniqueID();
1064 if (Plane >= NumPlaneElements.size())
1065 NumPlaneElements.resize(Plane+1, 0);
1066 return NumPlaneElements[Plane]++;
1067}
1068
1069
Chris Lattner92101ac2001-08-23 17:05:04 +00001070//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001071// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001072//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001073void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001074 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1075 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1076 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001077 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001078 GenericValue Result = callExternalMethod(M, ArgVals);
1079 const Type *RetTy = M->getReturnType();
1080
1081 // Copy the result back into the result variable if we are not returning
1082 // void.
1083 if (RetTy != Type::VoidTy) {
1084 if (!ECStack.empty() && ECStack.back().Caller) {
1085 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001086 SetValue(SF.Caller, Result, SF);
1087
1088 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001089 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001090 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001091 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001092 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001093 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001094 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001095
1096 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +00001097 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +00001098 }
1099 }
1100
Chris Lattner92101ac2001-08-23 17:05:04 +00001101 return;
1102 }
1103
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001104 // Process the function, assigning instruction numbers to the instructions in
1105 // the function. Also calculate the number of values for each type slot
1106 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001107 //
1108 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001109 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001110
Chris Lattner92101ac2001-08-23 17:05:04 +00001111 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1112 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001113 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001114 StackFrame.CurInst = StackFrame.CurBB->begin();
1115 StackFrame.MethInfo = MethInfo;
1116
1117 // Initialize the values to nothing...
1118 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001119 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001120 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1121
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001122 // Taint the initial values of stuff
1123 memset(&StackFrame.Values[i][0], 42,
1124 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1125 }
1126
Chris Lattner92101ac2001-08-23 17:05:04 +00001127 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1128
Chris Lattner92101ac2001-08-23 17:05:04 +00001129
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001130 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001131 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001132 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001133 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001134 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1135 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001136}
1137
1138// executeInstruction - Interpret a single instruction, increment the "PC", and
1139// return true if the next instruction is a breakpoint...
1140//
1141bool Interpreter::executeInstruction() {
1142 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1143
1144 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001145 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001146
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001147 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001148 CW << "Run:" << I;
1149
1150 // Set a sigsetjmp buffer so that we can recover if an error happens during
1151 // instruction execution...
1152 //
1153 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1154 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001155 if (SigNo != SIGINT) {
Chris Lattner8b77be22002-09-13 14:41:38 +00001156 cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001157 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001158 // If -abort-on-exception was specified, terminate LLI instead of trying
1159 // to debug it.
1160 //
1161 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001162 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001163 cout << "CTRL-C Detected, execution halted.\n";
1164 }
1165 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001166 return true;
1167 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001168
Chris Lattner461f02f2001-11-07 05:31:27 +00001169 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001170 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001171 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001172 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001173 switch (I.getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001174 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001175 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1176 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001177 // Memory Instructions
1178 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001179 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001180 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1181 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1182 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001183 case Instruction::GetElementPtr:
1184 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001185
1186 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001187 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1188 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1189 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1190 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1191 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001192 default:
1193 cout << "Don't know how to execute this instruction!\n-->" << I;
1194 }
1195 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001196 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001197
1198 // Reset the current frame location to the top of stack
1199 CurFrame = ECStack.size()-1;
1200
1201 if (CurFrame == -1) return false; // No breakpoint if no code
1202
1203 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001204 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001205}
1206
1207void Interpreter::stepInstruction() { // Do the 'step' command
1208 if (ECStack.empty()) {
1209 cout << "Error: no program running, cannot step!\n";
1210 return;
1211 }
1212
1213 // Run an instruction...
1214 executeInstruction();
1215
1216 // Print the next instruction to execute...
1217 printCurrentInstruction();
1218}
1219
1220// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001221void Interpreter::nextInstruction() { // Do the 'next' command
1222 if (ECStack.empty()) {
1223 cout << "Error: no program running, cannot 'next'!\n";
1224 return;
1225 }
1226
1227 // If this is a call instruction, step over the call instruction...
1228 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001229 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001230 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001231 // Step into the function...
1232 if (executeInstruction()) {
1233 // Hit a breakpoint, print current instruction, then return to user...
1234 cout << "Breakpoint hit!\n";
1235 printCurrentInstruction();
1236 return;
1237 }
1238
Chris Lattnera74a6b52001-10-29 14:08:33 +00001239 // If we we able to step into the function, finish it now. We might not be
1240 // able the step into a function, if it's external for example.
1241 if (ECStack.size() != StackSize)
1242 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001243 else
1244 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001245
Chris Lattner92101ac2001-08-23 17:05:04 +00001246 } else {
1247 // Normal instruction, just step...
1248 stepInstruction();
1249 }
1250}
1251
1252void Interpreter::run() {
1253 if (ECStack.empty()) {
1254 cout << "Error: no program running, cannot run!\n";
1255 return;
1256 }
1257
1258 bool HitBreakpoint = false;
1259 while (!ECStack.empty() && !HitBreakpoint) {
1260 // Run an instruction...
1261 HitBreakpoint = executeInstruction();
1262 }
1263
1264 if (HitBreakpoint) {
1265 cout << "Breakpoint hit!\n";
1266 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001267 // Print the next instruction to execute...
1268 printCurrentInstruction();
1269}
1270
1271void Interpreter::finish() {
1272 if (ECStack.empty()) {
1273 cout << "Error: no program running, cannot run!\n";
1274 return;
1275 }
1276
1277 unsigned StackSize = ECStack.size();
1278 bool HitBreakpoint = false;
1279 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1280 // Run an instruction...
1281 HitBreakpoint = executeInstruction();
1282 }
1283
1284 if (HitBreakpoint) {
1285 cout << "Breakpoint hit!\n";
1286 }
1287
1288 // Print the next instruction to execute...
1289 printCurrentInstruction();
1290}
1291
1292
1293
1294// printCurrentInstruction - Print out the instruction that the virtual PC is
1295// at, or fail silently if no program is running.
1296//
1297void Interpreter::printCurrentInstruction() {
1298 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001299 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1300 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1301
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001302 Instruction &I = *ECStack.back().CurInst;
1303 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001304 assert(IN && "Instruction has no numbering annotation!");
1305 cout << "#" << IN->InstNum << I;
1306 }
1307}
1308
1309void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001310 switch (Ty->getPrimitiveID()) {
1311 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001312 case Type::SByteTyID:
1313 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1314 case Type::UByteTyID:
1315 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001316 case Type::ShortTyID: cout << V.ShortVal; break;
1317 case Type::UShortTyID: cout << V.UShortVal; break;
1318 case Type::IntTyID: cout << V.IntVal; break;
1319 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001320 case Type::LongTyID: cout << (long)V.LongVal; break;
1321 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001322 case Type::FloatTyID: cout << V.FloatVal; break;
1323 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001324 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001325 default:
1326 cout << "- Don't know how to print value of this type!";
1327 break;
1328 }
1329}
1330
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001331void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001332 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001333 printValue(Ty, V);
1334}
1335
Chris Lattner697954c2002-01-20 22:54:45 +00001336void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001337 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1338 if (!PickedVal) return;
1339
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001340 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1341 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001342 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001343 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001344 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1345 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001346 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001347 print(PickedVal->getType(),
1348 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001349 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001350 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001351}
1352
Chris Lattner697954c2002-01-20 22:54:45 +00001353void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001354 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1355 if (!PickedVal) return;
1356
1357 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001358 print(PickedVal->getType(),
1359 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001360 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001361 printOperandInfo(PickedVal, ECStack[CurFrame]);
1362}
1363
Chris Lattner461f02f2001-11-07 05:31:27 +00001364// printStackFrame - Print information about the specified stack frame, or -1
1365// for the default one.
1366//
Chris Lattner601d7152002-07-25 17:37:05 +00001367void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001368 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001369 Function *F = ECStack[FrameNo].CurMethod;
1370 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001371
1372 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001373 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001374
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001375 unsigned i = 0;
1376 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001377 if (i != 0) cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001378 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001379
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001380 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001381 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001382
Chris Lattner697954c2002-01-20 22:54:45 +00001383 cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001384
1385 if (FrameNo != int(ECStack.size()-1)) {
1386 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1387 CW << --I;
1388 } else {
1389 CW << *ECStack[FrameNo].CurInst;
1390 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001391}
Chris Lattner461f02f2001-11-07 05:31:27 +00001392