blob: b9f8030a468b6c751de0c5d088b8131e2b6473bb [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// This file contains the actual instruction interpreter.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Interpreter.h"
8#include "ExecutionAnnotations.h"
Chris Lattner7061dc52001-12-03 18:02:31 +00009#include "llvm/iPHINode.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000010#include "llvm/iOther.h"
11#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000012#include "llvm/iMemory.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000013#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "llvm/Assembly/Writer.h"
Chris Lattner41c2e5c2001-09-28 22:56:43 +000016#include "llvm/Target/TargetData.h"
Chris Lattner2e42d3a2001-10-15 05:51:48 +000017#include "llvm/GlobalVariable.h"
Chris Lattnerf23eb852001-12-14 16:49:29 +000018#include "Support/CommandLine.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000019#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000020#include <signal.h>
21#include <setjmp.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <iostream>
23using std::vector;
24using std::cout;
25using std::cerr;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000026
Chris Lattnerf23eb852001-12-14 16:49:29 +000027cl::Flag QuietMode ("quiet" , "Do not emit any non-program output");
28cl::Alias QuietModeA("q" , "Alias for -quiet", cl::NoFlags, QuietMode);
Chris Lattnerc0fbd572002-02-11 20:19:16 +000029cl::Flag ArrayChecksEnabled("array-checks", "Enable array bound checks");
Chris Lattner74030252002-02-12 15:47:23 +000030cl::Flag AbortOnExceptions("abort-on-exception", "Halt execution on a machine exception");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000031
Chris Lattner2e42d3a2001-10-15 05:51:48 +000032// Create a TargetData structure to handle memory addressing and size/alignment
33// computations
34//
35static TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000036CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000037
38
Chris Lattnere2409062001-11-12 16:19:45 +000039#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattnere2409062001-11-12 16:19:45 +000040static cl::Flag ProfileStructureFields("profilestructfields",
41 "Profile Structure Field Accesses");
42#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000043static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000044#endif
45
Chris Lattner5af0c482001-11-07 04:23:00 +000046sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000047static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000048
49extern "C" {
50static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000051 if (InInstruction)
52 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000053}
54}
55
56static void initializeSignalHandlers() {
57 struct sigaction Action;
58 Action.sa_handler = SigHandler;
59 Action.sa_flags = SA_SIGINFO;
60 sigemptyset(&Action.sa_mask);
61 sigaction(SIGSEGV, &Action, 0);
62 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000063 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000064 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000065}
66
Chris Lattner2e42d3a2001-10-15 05:51:48 +000067
68//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000069// Value Manipulation code
70//===----------------------------------------------------------------------===//
71
72static unsigned getOperandSlot(Value *V) {
73 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
74 assert(SN && "Operand does not have a slot number annotation!");
75 return SN->SlotNum;
76}
77
78#define GET_CONST_VAL(TY, CLASS) \
79 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
80
81static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +000083 GenericValue Result;
84 switch (CPV->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000085 GET_CONST_VAL(Bool , ConstantBool);
86 GET_CONST_VAL(UByte , ConstantUInt);
87 GET_CONST_VAL(SByte , ConstantSInt);
88 GET_CONST_VAL(UShort , ConstantUInt);
89 GET_CONST_VAL(Short , ConstantSInt);
90 GET_CONST_VAL(UInt , ConstantUInt);
91 GET_CONST_VAL(Int , ConstantSInt);
92 GET_CONST_VAL(ULong , ConstantUInt);
93 GET_CONST_VAL(Long , ConstantSInt);
94 GET_CONST_VAL(Float , ConstantFP);
95 GET_CONST_VAL(Double , ConstantFP);
Chris Lattner39bb5b42001-10-15 13:25:40 +000096 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000097 if (isa<ConstantPointerNull>(CPV)) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +000098 Result.PointerVal = 0;
Chris Lattner697954c2002-01-20 22:54:45 +000099 } else if (isa<ConstantPointerRef>(CPV)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000100 assert(0 && "Not implemented!");
101 } else {
102 assert(0 && "Unknown constant pointer type!");
103 }
104 break;
105 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000106 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000107 }
108 return Result;
109 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
110 GlobalAddress *Address =
111 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
112 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000113 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000114 return Result;
115 } else {
116 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000117 unsigned OpSlot = getOperandSlot(V);
118 assert(TyP < SF.Values.size() &&
119 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000120 return SF.Values[TyP][getOperandSlot(V)];
121 }
122}
123
124static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000125 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000126 cout << "Constant Pool Value\n";
127 } else if (isa<GlobalValue>(V)) {
128 cout << "Global Value\n";
129 } else {
130 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
131 unsigned Slot = getOperandSlot(V);
132 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000133 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
134 << " Contents=0x";
135
136 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
137 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
138 unsigned char Cur = Buf[i];
139 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
140 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
141 }
Chris Lattner697954c2002-01-20 22:54:45 +0000142 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000143 }
144}
145
146
147
148static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
149 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
150
Chris Lattner697954c2002-01-20 22:54:45 +0000151 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000152 SF.Values[TyP][getOperandSlot(V)] = Val;
153}
154
155
156//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000157// Annotation Wrangling code
158//===----------------------------------------------------------------------===//
159
160void Interpreter::initializeExecutionEngine() {
161 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
162 &MethodInfo::Create);
163 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
164 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000165 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166}
167
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000168// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000169// specified memory location...
170//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171static void InitializeMemory(Constant *Init, char *Addr) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000172#define INITIALIZE_MEMORY(TYID, CLASS, TY) \
173 case Type::TYID##TyID: { \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000174 TY Tmp = cast<CLASS>(Init)->getValue(); \
Chris Lattner39bb5b42001-10-15 13:25:40 +0000175 memcpy(Addr, &Tmp, sizeof(TY)); \
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000176 } return
177
178 switch (Init->getType()->getPrimitiveID()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000179 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
180 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
181 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
182 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
183 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
184 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
185 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
186 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
187 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
188 INITIALIZE_MEMORY(Float , ConstantFP , float);
189 INITIALIZE_MEMORY(Double , ConstantFP , double);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000190#undef INITIALIZE_MEMORY
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000192 case Type::ArrayTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000193 ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000194 const vector<Use> &Val = CPA->getValues();
195 unsigned ElementSize =
196 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
197 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000198 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000199 return;
200 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000201
202 case Type::StructTyID: {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000203 ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000204 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
205 const vector<Use> &Val = CPS->getValues();
206 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000207 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000208 Addr+SL->MemberOffsets[i]);
209 return;
210 }
211
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000212 case Type::PointerTyID:
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000213 if (isa<ConstantPointerNull>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000214 *(void**)Addr = 0;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000215 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Init)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000216 GlobalAddress *Address =
217 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
218 *(void**)Addr = (GenericValue*)Address->Ptr;
219 } else {
220 assert(0 && "Unknown Constant pointer type!");
221 }
222 return;
223
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000224 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000225 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000226 assert(0 && "Unknown constant type to initialize memory with!");
227 }
228}
229
230Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
231 assert(AID == GlobalAddressAID);
232
233 // This annotation will only be created on GlobalValue objects...
234 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
235
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000236 if (isa<Function>(GVal)) {
237 // The GlobalAddress object for a function is just a pointer to function
238 // itself. Don't delete it when the annotation is gone though!
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000239 return new GlobalAddress(GVal, false);
240 }
241
242 // Handle the case of a global variable...
243 assert(isa<GlobalVariable>(GVal) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000244 "Global value found that isn't a function or global variable!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000245 GlobalVariable *GV = cast<GlobalVariable>(GVal);
246
247 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000248 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000249
250 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000251 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000252 assert(Addr != 0 && "Null pointer returned by malloc!");
253
254 // Initialize the memory if there is an initializer...
255 if (GV->hasInitializer())
256 InitializeMemory(GV->getInitializer(), (char*)Addr);
257
258 return new GlobalAddress(Addr, true); // Simply invoke the ctor
259}
260
Chris Lattner92101ac2001-08-23 17:05:04 +0000261
262//===----------------------------------------------------------------------===//
Chris Lattner2adcd832002-05-03 19:52:30 +0000263// Unary Instruction Implementations
264//===----------------------------------------------------------------------===//
265
266#define IMPLEMENT_UNARY_OPERATOR(OP, TY) \
267 case Type::TY##TyID: Dest.TY##Val = OP Src.TY##Val; break
268
269static void executeNotInst(UnaryOperator *I, ExecutionContext &SF) {
270 const Type *Ty = I->getOperand(0)->getType();
271 GenericValue Src = getOperandValue(I->getOperand(0), SF);
272 GenericValue Dest;
273 switch (Ty->getPrimitiveID()) {
274 IMPLEMENT_UNARY_OPERATOR(~, UByte);
275 IMPLEMENT_UNARY_OPERATOR(~, SByte);
276 IMPLEMENT_UNARY_OPERATOR(~, UShort);
277 IMPLEMENT_UNARY_OPERATOR(~, Short);
278 IMPLEMENT_UNARY_OPERATOR(~, UInt);
279 IMPLEMENT_UNARY_OPERATOR(~, Int);
280 IMPLEMENT_UNARY_OPERATOR(~, ULong);
281 IMPLEMENT_UNARY_OPERATOR(~, Long);
282 IMPLEMENT_UNARY_OPERATOR(~, Pointer);
283 default:
284 cout << "Unhandled type for Not instruction: " << Ty << "\n";
285 }
286 SetValue(I, Dest, SF);
287}
288
289//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000290// Binary Instruction Implementations
291//===----------------------------------------------------------------------===//
292
293#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
294 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
295
296static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
297 const Type *Ty, ExecutionContext &SF) {
298 GenericValue Dest;
299 switch (Ty->getPrimitiveID()) {
300 IMPLEMENT_BINARY_OPERATOR(+, UByte);
301 IMPLEMENT_BINARY_OPERATOR(+, SByte);
302 IMPLEMENT_BINARY_OPERATOR(+, UShort);
303 IMPLEMENT_BINARY_OPERATOR(+, Short);
304 IMPLEMENT_BINARY_OPERATOR(+, UInt);
305 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000306 IMPLEMENT_BINARY_OPERATOR(+, ULong);
307 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000308 IMPLEMENT_BINARY_OPERATOR(+, Float);
309 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000310 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000311 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000312 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000313 }
314 return Dest;
315}
316
317static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
318 const Type *Ty, ExecutionContext &SF) {
319 GenericValue Dest;
320 switch (Ty->getPrimitiveID()) {
321 IMPLEMENT_BINARY_OPERATOR(-, UByte);
322 IMPLEMENT_BINARY_OPERATOR(-, SByte);
323 IMPLEMENT_BINARY_OPERATOR(-, UShort);
324 IMPLEMENT_BINARY_OPERATOR(-, Short);
325 IMPLEMENT_BINARY_OPERATOR(-, UInt);
326 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000327 IMPLEMENT_BINARY_OPERATOR(-, ULong);
328 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000329 IMPLEMENT_BINARY_OPERATOR(-, Float);
330 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000331 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000332 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000333 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000334 }
335 return Dest;
336}
337
Chris Lattnerc2593162001-10-27 08:28:11 +0000338static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
339 const Type *Ty, ExecutionContext &SF) {
340 GenericValue Dest;
341 switch (Ty->getPrimitiveID()) {
342 IMPLEMENT_BINARY_OPERATOR(*, UByte);
343 IMPLEMENT_BINARY_OPERATOR(*, SByte);
344 IMPLEMENT_BINARY_OPERATOR(*, UShort);
345 IMPLEMENT_BINARY_OPERATOR(*, Short);
346 IMPLEMENT_BINARY_OPERATOR(*, UInt);
347 IMPLEMENT_BINARY_OPERATOR(*, Int);
348 IMPLEMENT_BINARY_OPERATOR(*, ULong);
349 IMPLEMENT_BINARY_OPERATOR(*, Long);
350 IMPLEMENT_BINARY_OPERATOR(*, Float);
351 IMPLEMENT_BINARY_OPERATOR(*, Double);
352 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
353 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000354 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000355 }
356 return Dest;
357}
358
359static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
360 const Type *Ty, ExecutionContext &SF) {
361 GenericValue Dest;
362 switch (Ty->getPrimitiveID()) {
363 IMPLEMENT_BINARY_OPERATOR(/, UByte);
364 IMPLEMENT_BINARY_OPERATOR(/, SByte);
365 IMPLEMENT_BINARY_OPERATOR(/, UShort);
366 IMPLEMENT_BINARY_OPERATOR(/, Short);
367 IMPLEMENT_BINARY_OPERATOR(/, UInt);
368 IMPLEMENT_BINARY_OPERATOR(/, Int);
369 IMPLEMENT_BINARY_OPERATOR(/, ULong);
370 IMPLEMENT_BINARY_OPERATOR(/, Long);
371 IMPLEMENT_BINARY_OPERATOR(/, Float);
372 IMPLEMENT_BINARY_OPERATOR(/, Double);
373 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
374 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000375 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000376 }
377 return Dest;
378}
379
380static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
381 const Type *Ty, ExecutionContext &SF) {
382 GenericValue Dest;
383 switch (Ty->getPrimitiveID()) {
384 IMPLEMENT_BINARY_OPERATOR(%, UByte);
385 IMPLEMENT_BINARY_OPERATOR(%, SByte);
386 IMPLEMENT_BINARY_OPERATOR(%, UShort);
387 IMPLEMENT_BINARY_OPERATOR(%, Short);
388 IMPLEMENT_BINARY_OPERATOR(%, UInt);
389 IMPLEMENT_BINARY_OPERATOR(%, Int);
390 IMPLEMENT_BINARY_OPERATOR(%, ULong);
391 IMPLEMENT_BINARY_OPERATOR(%, Long);
392 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
393 case Type::FloatTyID:
394 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
395 break;
396 case Type::DoubleTyID:
397 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
398 break;
399 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000400 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000401 }
402 return Dest;
403}
404
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000405static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000406 const Type *Ty, ExecutionContext &SF) {
407 GenericValue Dest;
408 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000409 IMPLEMENT_BINARY_OPERATOR(&, UByte);
410 IMPLEMENT_BINARY_OPERATOR(&, SByte);
411 IMPLEMENT_BINARY_OPERATOR(&, UShort);
412 IMPLEMENT_BINARY_OPERATOR(&, Short);
413 IMPLEMENT_BINARY_OPERATOR(&, UInt);
414 IMPLEMENT_BINARY_OPERATOR(&, Int);
415 IMPLEMENT_BINARY_OPERATOR(&, ULong);
416 IMPLEMENT_BINARY_OPERATOR(&, Long);
417 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
418 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000419 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000420 }
421 return Dest;
422}
423
424
425static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
426 const Type *Ty, ExecutionContext &SF) {
427 GenericValue Dest;
428 switch (Ty->getPrimitiveID()) {
429 IMPLEMENT_BINARY_OPERATOR(|, UByte);
430 IMPLEMENT_BINARY_OPERATOR(|, SByte);
431 IMPLEMENT_BINARY_OPERATOR(|, UShort);
432 IMPLEMENT_BINARY_OPERATOR(|, Short);
433 IMPLEMENT_BINARY_OPERATOR(|, UInt);
434 IMPLEMENT_BINARY_OPERATOR(|, Int);
435 IMPLEMENT_BINARY_OPERATOR(|, ULong);
436 IMPLEMENT_BINARY_OPERATOR(|, Long);
437 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
438 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000439 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000440 }
441 return Dest;
442}
443
444
445static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
446 const Type *Ty, ExecutionContext &SF) {
447 GenericValue Dest;
448 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000449 IMPLEMENT_BINARY_OPERATOR(^, UByte);
450 IMPLEMENT_BINARY_OPERATOR(^, SByte);
451 IMPLEMENT_BINARY_OPERATOR(^, UShort);
452 IMPLEMENT_BINARY_OPERATOR(^, Short);
453 IMPLEMENT_BINARY_OPERATOR(^, UInt);
454 IMPLEMENT_BINARY_OPERATOR(^, Int);
455 IMPLEMENT_BINARY_OPERATOR(^, ULong);
456 IMPLEMENT_BINARY_OPERATOR(^, Long);
457 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
458 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000459 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000460 }
461 return Dest;
462}
463
464
Chris Lattner92101ac2001-08-23 17:05:04 +0000465#define IMPLEMENT_SETCC(OP, TY) \
466 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
467
Chris Lattner92101ac2001-08-23 17:05:04 +0000468static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
469 const Type *Ty, ExecutionContext &SF) {
470 GenericValue Dest;
471 switch (Ty->getPrimitiveID()) {
472 IMPLEMENT_SETCC(==, UByte);
473 IMPLEMENT_SETCC(==, SByte);
474 IMPLEMENT_SETCC(==, UShort);
475 IMPLEMENT_SETCC(==, Short);
476 IMPLEMENT_SETCC(==, UInt);
477 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000478 IMPLEMENT_SETCC(==, ULong);
479 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 IMPLEMENT_SETCC(==, Float);
481 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000482 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000483 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000484 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 }
486 return Dest;
487}
488
489static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
490 const Type *Ty, ExecutionContext &SF) {
491 GenericValue Dest;
492 switch (Ty->getPrimitiveID()) {
493 IMPLEMENT_SETCC(!=, UByte);
494 IMPLEMENT_SETCC(!=, SByte);
495 IMPLEMENT_SETCC(!=, UShort);
496 IMPLEMENT_SETCC(!=, Short);
497 IMPLEMENT_SETCC(!=, UInt);
498 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000499 IMPLEMENT_SETCC(!=, ULong);
500 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 IMPLEMENT_SETCC(!=, Float);
502 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000503 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000504
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000506 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000507 }
508 return Dest;
509}
510
511static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
512 const Type *Ty, ExecutionContext &SF) {
513 GenericValue Dest;
514 switch (Ty->getPrimitiveID()) {
515 IMPLEMENT_SETCC(<=, UByte);
516 IMPLEMENT_SETCC(<=, SByte);
517 IMPLEMENT_SETCC(<=, UShort);
518 IMPLEMENT_SETCC(<=, Short);
519 IMPLEMENT_SETCC(<=, UInt);
520 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000521 IMPLEMENT_SETCC(<=, ULong);
522 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000523 IMPLEMENT_SETCC(<=, Float);
524 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000525 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000526 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000527 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000528 }
529 return Dest;
530}
531
532static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
533 const Type *Ty, ExecutionContext &SF) {
534 GenericValue Dest;
535 switch (Ty->getPrimitiveID()) {
536 IMPLEMENT_SETCC(>=, UByte);
537 IMPLEMENT_SETCC(>=, SByte);
538 IMPLEMENT_SETCC(>=, UShort);
539 IMPLEMENT_SETCC(>=, Short);
540 IMPLEMENT_SETCC(>=, UInt);
541 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000542 IMPLEMENT_SETCC(>=, ULong);
543 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000544 IMPLEMENT_SETCC(>=, Float);
545 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000546 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000547 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000548 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000549 }
550 return Dest;
551}
552
553static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
554 const Type *Ty, ExecutionContext &SF) {
555 GenericValue Dest;
556 switch (Ty->getPrimitiveID()) {
557 IMPLEMENT_SETCC(<, UByte);
558 IMPLEMENT_SETCC(<, SByte);
559 IMPLEMENT_SETCC(<, UShort);
560 IMPLEMENT_SETCC(<, Short);
561 IMPLEMENT_SETCC(<, UInt);
562 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000563 IMPLEMENT_SETCC(<, ULong);
564 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000565 IMPLEMENT_SETCC(<, Float);
566 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000567 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000568 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000569 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000570 }
571 return Dest;
572}
573
574static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
575 const Type *Ty, ExecutionContext &SF) {
576 GenericValue Dest;
577 switch (Ty->getPrimitiveID()) {
578 IMPLEMENT_SETCC(>, UByte);
579 IMPLEMENT_SETCC(>, SByte);
580 IMPLEMENT_SETCC(>, UShort);
581 IMPLEMENT_SETCC(>, Short);
582 IMPLEMENT_SETCC(>, UInt);
583 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000584 IMPLEMENT_SETCC(>, ULong);
585 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000586 IMPLEMENT_SETCC(>, Float);
587 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000588 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000589 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000590 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000591 }
592 return Dest;
593}
594
595static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
596 const Type *Ty = I->getOperand(0)->getType();
597 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
598 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
599 GenericValue R; // Result
600
601 switch (I->getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000602 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
603 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
604 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
605 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
606 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000607 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
608 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000609 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000610 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
611 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
612 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
613 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
614 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
615 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
616 default:
617 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000618 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000619 }
620
621 SetValue(I, R, SF);
622}
623
Chris Lattner92101ac2001-08-23 17:05:04 +0000624//===----------------------------------------------------------------------===//
625// Terminator Instruction Implementations
626//===----------------------------------------------------------------------===//
627
Chris Lattnera95c6992001-11-12 16:28:48 +0000628static void PerformExitStuff() {
629#ifdef PROFILE_STRUCTURE_FIELDS
630 // Print out structure field accounting information...
631 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000632 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000633 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000634 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
635 for (; I != E; ++I) {
636 vector<unsigned> &OfC = I->second;
637 CW << " '" << (Value*)I->first << "'\t- Sum=";
638
639 unsigned Sum = 0;
640 for (unsigned i = 0; i < OfC.size(); ++i)
641 Sum += OfC[i];
642 CW << Sum << " - ";
643
644 for (unsigned i = 0; i < OfC.size(); ++i) {
645 if (i) CW << ", ";
646 CW << OfC[i];
647 }
Chris Lattner697954c2002-01-20 22:54:45 +0000648 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000649 }
Chris Lattner697954c2002-01-20 22:54:45 +0000650 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000651
652 CW << "Profile Field Access Percentages:\n";
653 cout.precision(3);
654 for (I = FieldAccessCounts.begin(); I != E; ++I) {
655 vector<unsigned> &OfC = I->second;
656 unsigned Sum = 0;
657 for (unsigned i = 0; i < OfC.size(); ++i)
658 Sum += OfC[i];
659
660 CW << " '" << (Value*)I->first << "'\t- ";
661 for (unsigned i = 0; i < OfC.size(); ++i) {
662 if (i) CW << ", ";
663 CW << double(OfC[i])/Sum;
664 }
Chris Lattner697954c2002-01-20 22:54:45 +0000665 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000666 }
Chris Lattner697954c2002-01-20 22:54:45 +0000667 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000668
Chris Lattnera95c6992001-11-12 16:28:48 +0000669 FieldAccessCounts.clear();
670 }
671#endif
672}
673
Chris Lattnere43db882001-10-27 04:15:57 +0000674void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000675 if (!QuietMode) {
676 cout << "Program returned ";
677 print(Type::IntTy, GV);
678 cout << " via 'void exit(int)'\n";
679 }
Chris Lattnere43db882001-10-27 04:15:57 +0000680
681 ExitCode = GV.SByteVal;
682 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000683 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000684}
685
Chris Lattner92101ac2001-08-23 17:05:04 +0000686void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
687 const Type *RetTy = 0;
688 GenericValue Result;
689
690 // Save away the return value... (if we are not 'ret void')
691 if (I->getNumOperands()) {
692 RetTy = I->getReturnValue()->getType();
693 Result = getOperandValue(I->getReturnValue(), SF);
694 }
695
696 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000697 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000698
699 // Pop the current stack frame... this invalidates SF
700 ECStack.pop_back();
701
702 if (ECStack.empty()) { // Finished main. Put result into exit code...
703 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000704 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000705 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000706 << "\" returned ";
707 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000708 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000709 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000710
711 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000712 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000713 } else {
714 ExitCode = 0;
715 }
Chris Lattnere2409062001-11-12 16:19:45 +0000716
Chris Lattnera95c6992001-11-12 16:28:48 +0000717 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000718 return;
719 }
720
721 // If we have a previous stack frame, and we have a previous call, fill in
722 // the return value...
723 //
724 ExecutionContext &NewSF = ECStack.back();
725 if (NewSF.Caller) {
726 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
727 SetValue(NewSF.Caller, Result, NewSF);
728
729 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000730 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000731 // This must be a function that is executing because of a user 'call'
732 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000733 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000734 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000735 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000736 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000737 }
738}
739
740void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
741 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
742 BasicBlock *Dest;
743
744 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
745 if (!I->isUnconditional()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000746 Value *Cond = I->getCondition();
747 GenericValue CondVal = getOperandValue(Cond, SF);
748 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner92101ac2001-08-23 17:05:04 +0000749 Dest = I->getSuccessor(1);
750 }
751 SF.CurBB = Dest; // Update CurBB to branch destination
752 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
753}
754
755//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000756// Memory Instruction Implementations
757//===----------------------------------------------------------------------===//
758
Chris Lattner86660982001-08-27 05:16:50 +0000759void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
Chris Lattner7a176752001-12-04 00:03:30 +0000760 const Type *Ty = I->getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000761
Chris Lattnercc82cc12002-04-28 21:57:33 +0000762 // Get the number of elements being allocated by the array...
763 unsigned NumElements = getOperandValue(I->getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000764
765 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000766 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000767 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
768
769 GenericValue Result;
770 Result.PointerVal = (PointerTy)Memory;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000771 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner86660982001-08-27 05:16:50 +0000772 SetValue(I, Result, SF);
773
Chris Lattner9bffa732002-02-19 18:50:09 +0000774 if (I->getOpcode() == Instruction::Alloca)
775 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000776}
777
778static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
779 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
780 GenericValue Value = getOperandValue(I->getOperand(0), SF);
781 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000782 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000783}
784
Chris Lattner95c3af52001-10-29 19:32:19 +0000785
786// getElementOffset - The workhorse for getelementptr, load and store. This
787// function returns the offset that arguments ArgOff+1 -> NumArgs specify for
788// the pointer type specified by argument Arg.
789//
Chris Lattner782b9392001-11-26 18:18:18 +0000790static PointerTy getElementOffset(MemAccessInst *I, ExecutionContext &SF) {
791 assert(isa<PointerType>(I->getPointerOperand()->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000792 "Cannot getElementOffset of a nonpointer type!");
793
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000794 PointerTy Total = 0;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000795 const Type *Ty = I->getPointerOperand()->getType();
Chris Lattner95c3af52001-10-29 19:32:19 +0000796
Chris Lattner782b9392001-11-26 18:18:18 +0000797 unsigned ArgOff = I->getFirstIndexOperandNumber();
Chris Lattner95c3af52001-10-29 19:32:19 +0000798 while (ArgOff < I->getNumOperands()) {
Chris Lattner782b9392001-11-26 18:18:18 +0000799 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
800 const StructLayout *SLO = TD.getStructLayout(STy);
801
802 // Indicies must be ubyte constants...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000803 const ConstantUInt *CPU = cast<ConstantUInt>(I->getOperand(ArgOff++));
Chris Lattner782b9392001-11-26 18:18:18 +0000804 assert(CPU->getType() == Type::UByteTy);
805 unsigned Index = CPU->getValue();
806
Chris Lattnere2409062001-11-12 16:19:45 +0000807#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000808 if (ProfileStructureFields) {
809 // Do accounting for this field...
810 vector<unsigned> &OfC = FieldAccessCounts[STy];
811 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
812 OfC[Index]++;
813 }
Chris Lattnere2409062001-11-12 16:19:45 +0000814#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000815
816 Total += SLO->MemberOffsets[Index];
817 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000818 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000819
Chris Lattner782b9392001-11-26 18:18:18 +0000820 // Get the index number for the array... which must be uint type...
821 assert(I->getOperand(ArgOff)->getType() == Type::UIntTy);
822 unsigned Idx = getOperandValue(I->getOperand(ArgOff++), SF).UIntVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000823 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000824 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000825 cerr << "Out of range memory access to element #" << Idx
826 << " of a " << AT->getNumElements() << " element array."
827 << " Subscript #" << (ArgOff-I->getFirstIndexOperandNumber())
828 << "\n";
829 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000830 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000831 }
Chris Lattner782b9392001-11-26 18:18:18 +0000832
Chris Lattnerf23eb852001-12-14 16:49:29 +0000833 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000834 unsigned Size = TD.getTypeSize(Ty);
835 Total += Size*Idx;
836 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000837 }
838
839 return Total;
840}
841
842static void executeGEPInst(GetElementPtrInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000843 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000844 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner95c3af52001-10-29 19:32:19 +0000845
846 GenericValue Result;
Chris Lattner782b9392001-11-26 18:18:18 +0000847 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000848 SetValue(I, Result, SF);
849}
850
Chris Lattner86660982001-08-27 05:16:50 +0000851static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000852 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000853 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000854 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
Chris Lattnerbb76f022001-10-30 20:27:31 +0000855 SrcPtr += Offset;
Chris Lattner95c3af52001-10-29 19:32:19 +0000856
857 GenericValue *Ptr = (GenericValue*)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000858 GenericValue Result;
859
860 switch (I->getType()->getPrimitiveID()) {
861 case Type::BoolTyID:
862 case Type::UByteTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000863 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000864 case Type::UShortTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000865 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000866 case Type::UIntTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000867 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000868 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000869 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
870 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
871 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
872 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000873 default:
874 cout << "Cannot load value of type " << I->getType() << "!\n";
875 }
876
877 SetValue(I, Result, SF);
878}
879
880static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
Chris Lattner3bcce722001-11-14 11:28:18 +0000881 GenericValue SRC = getOperandValue(I->getPointerOperand(), SF);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000882 PointerTy SrcPtr = SRC.PointerVal;
Chris Lattner782b9392001-11-26 18:18:18 +0000883 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
Chris Lattner95c3af52001-10-29 19:32:19 +0000884
885 GenericValue *Ptr = (GenericValue *)SrcPtr;
Chris Lattner86660982001-08-27 05:16:50 +0000886 GenericValue Val = getOperandValue(I->getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000887
888 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
889 case Type::BoolTyID:
890 case Type::UByteTyID:
891 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
892 case Type::UShortTyID:
893 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
894 case Type::UIntTyID:
895 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
Chris Lattner7b851ab2001-10-15 19:18:26 +0000896 case Type::ULongTyID:
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000897 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
898 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000899 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
900 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
Chris Lattner86660982001-08-27 05:16:50 +0000901 default:
902 cout << "Cannot store value of type " << I->getType() << "!\n";
903 }
904}
905
906
907//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000908// Miscellaneous Instruction Implementations
909//===----------------------------------------------------------------------===//
910
911void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
912 ECStack.back().Caller = I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000913 vector<GenericValue> ArgVals;
914 ArgVals.reserve(I->getNumOperands()-1);
915 for (unsigned i = 1; i < I->getNumOperands(); ++i)
916 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
917
Chris Lattner070cf5e2001-11-07 20:12:30 +0000918 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000919 // and treat it as a function pointer.
Chris Lattner070cf5e2001-11-07 20:12:30 +0000920 GenericValue SRC = getOperandValue(I->getCalledValue(), SF);
921
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000922 callMethod((Function*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000923}
924
925static void executePHINode(PHINode *I, ExecutionContext &SF) {
926 BasicBlock *PrevBB = SF.PrevBB;
927 Value *IncomingValue = 0;
928
929 // Search for the value corresponding to this previous bb...
930 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
931 if (I->getIncomingBlock(--i) == PrevBB) {
932 IncomingValue = I->getIncomingValue(i);
933 break;
934 }
935 }
936 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
937
938 // Found the value, set as the result...
939 SetValue(I, getOperandValue(IncomingValue, SF), SF);
940}
941
Chris Lattner86660982001-08-27 05:16:50 +0000942#define IMPLEMENT_SHIFT(OP, TY) \
943 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
944
945static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
946 const Type *Ty = I->getOperand(0)->getType();
947 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
948 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
949 GenericValue Dest;
950
951 switch (Ty->getPrimitiveID()) {
952 IMPLEMENT_SHIFT(<<, UByte);
953 IMPLEMENT_SHIFT(<<, SByte);
954 IMPLEMENT_SHIFT(<<, UShort);
955 IMPLEMENT_SHIFT(<<, Short);
956 IMPLEMENT_SHIFT(<<, UInt);
957 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000958 IMPLEMENT_SHIFT(<<, ULong);
959 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000960 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000961 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000962 }
963 SetValue(I, Dest, SF);
964}
965
966static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
967 const Type *Ty = I->getOperand(0)->getType();
968 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
969 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
970 GenericValue Dest;
971
972 switch (Ty->getPrimitiveID()) {
973 IMPLEMENT_SHIFT(>>, UByte);
974 IMPLEMENT_SHIFT(>>, SByte);
975 IMPLEMENT_SHIFT(>>, UShort);
976 IMPLEMENT_SHIFT(>>, Short);
977 IMPLEMENT_SHIFT(>>, UInt);
978 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000979 IMPLEMENT_SHIFT(>>, ULong);
980 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000981 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000982 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000983 }
984 SetValue(I, Dest, SF);
985}
986
987#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000988 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000989
990#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
991 case Type::DESTTY##TyID: \
992 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000993 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000994 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
995 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
996 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
997 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
998 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000999 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1000 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001001 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1002 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001003
1004#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1005 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1006 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1007
1008#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +00001009 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +00001010 break; \
1011 } \
1012 break
1013
1014#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1015 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1016 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001017 IMPLEMENT_CAST_CASE_END()
1018
1019static void executeCastInst(CastInst *I, ExecutionContext &SF) {
1020 const Type *Ty = I->getType();
1021 const Type *SrcTy = I->getOperand(0)->getType();
1022 GenericValue Src = getOperandValue(I->getOperand(0), SF);
1023 GenericValue Dest;
1024
1025 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001026 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1027 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1028 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1029 IMPLEMENT_CAST_CASE(Short , ( signed char));
1030 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1031 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1032 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1033 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1034 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1035 IMPLEMENT_CAST_CASE(Float , (float));
1036 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001037 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001038 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001039 }
1040 SetValue(I, Dest, SF);
1041}
Chris Lattner92101ac2001-08-23 17:05:04 +00001042
1043
1044
1045
1046//===----------------------------------------------------------------------===//
1047// Dispatch and Execution Code
1048//===----------------------------------------------------------------------===//
1049
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001050MethodInfo::MethodInfo(Function *M) : Annotation(MethodInfoAID) {
1051 // Assign slot numbers to the function arguments...
1052 const Function::ArgumentListType &ArgList = M->getArgumentList();
1053 for (Function::ArgumentListType::const_iterator AI = ArgList.begin(),
Chris Lattner9a3d6962002-03-26 18:02:30 +00001054 AE = ArgList.end(); AI != AE; ++AI)
Chris Lattnere06e9142002-04-09 19:59:31 +00001055 ((Value*)(*AI))->addAnnotation(new SlotNumber(getValueSlot((Value*)*AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001056
1057 // Iterate over all of the instructions...
1058 unsigned InstNum = 0;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001059 for (Function::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
Chris Lattner221d6882002-02-12 21:07:25 +00001060 BasicBlock *BB = *MI;
1061 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
1062 Instruction *I = *II; // For each instruction... Add Annote
1063 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I)));
1064 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001065 }
1066}
1067
1068unsigned MethodInfo::getValueSlot(const Value *V) {
1069 unsigned Plane = V->getType()->getUniqueID();
1070 if (Plane >= NumPlaneElements.size())
1071 NumPlaneElements.resize(Plane+1, 0);
1072 return NumPlaneElements[Plane]++;
1073}
1074
1075
Chris Lattner92101ac2001-08-23 17:05:04 +00001076//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001077// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001078//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001079void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001080 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1081 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1082 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001083 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001084 GenericValue Result = callExternalMethod(M, ArgVals);
1085 const Type *RetTy = M->getReturnType();
1086
1087 // Copy the result back into the result variable if we are not returning
1088 // void.
1089 if (RetTy != Type::VoidTy) {
1090 if (!ECStack.empty() && ECStack.back().Caller) {
1091 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001092 SetValue(SF.Caller, Result, SF);
1093
1094 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001095 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001096 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001097 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001098 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001099 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001100 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001101
1102 if (RetTy->isIntegral())
1103 ExitCode = Result.SByteVal; // Capture the exit code of the program
1104 }
1105 }
1106
Chris Lattner92101ac2001-08-23 17:05:04 +00001107 return;
1108 }
1109
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001110 // Process the function, assigning instruction numbers to the instructions in
1111 // the function. Also calculate the number of values for each type slot
1112 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001113 //
1114 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001115 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001116
Chris Lattner92101ac2001-08-23 17:05:04 +00001117 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1118 StackFrame.CurMethod = M;
1119 StackFrame.CurBB = M->front();
1120 StackFrame.CurInst = StackFrame.CurBB->begin();
1121 StackFrame.MethInfo = MethInfo;
1122
1123 // Initialize the values to nothing...
1124 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001125 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001126 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1127
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001128 // Taint the initial values of stuff
1129 memset(&StackFrame.Values[i][0], 42,
1130 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1131 }
1132
Chris Lattner92101ac2001-08-23 17:05:04 +00001133 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1134
Chris Lattner92101ac2001-08-23 17:05:04 +00001135
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001136 // Run through the function arguments and initialize their values...
Chris Lattnerf8f2afb2001-10-18 21:55:32 +00001137 assert(ArgVals.size() == M->getArgumentList().size() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001138 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001139 unsigned i = 0;
Chris Lattnere06e9142002-04-09 19:59:31 +00001140 for (Function::ArgumentListType::iterator AI = M->getArgumentList().begin(),
1141 AE = M->getArgumentList().end(); AI != AE; ++AI, ++i) {
1142 SetValue((Value*)*AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001143 }
1144}
1145
1146// executeInstruction - Interpret a single instruction, increment the "PC", and
1147// return true if the next instruction is a breakpoint...
1148//
1149bool Interpreter::executeInstruction() {
1150 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1151
1152 ExecutionContext &SF = ECStack.back(); // Current stack frame
1153 Instruction *I = *SF.CurInst++; // Increment before execute
1154
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001155 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001156 CW << "Run:" << I;
1157
1158 // Set a sigsetjmp buffer so that we can recover if an error happens during
1159 // instruction execution...
1160 //
1161 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1162 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001163 if (SigNo != SIGINT) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001164 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001165 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001166 // If -abort-on-exception was specified, terminate LLI instead of trying
1167 // to debug it.
1168 //
1169 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001170 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001171 cout << "CTRL-C Detected, execution halted.\n";
1172 }
1173 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001174 return true;
1175 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001176
Chris Lattner461f02f2001-11-07 05:31:27 +00001177 InInstruction = true;
Chris Lattner92101ac2001-08-23 17:05:04 +00001178 if (I->isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001179 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001180 } else {
1181 switch (I->getOpcode()) {
Chris Lattner2adcd832002-05-03 19:52:30 +00001182 case Instruction::Not: executeNotInst(cast<UnaryOperator>(I),SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001183 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001184 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1185 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001186 // Memory Instructions
1187 case Instruction::Alloca:
Chris Lattnerbb76f022001-10-30 20:27:31 +00001188 case Instruction::Malloc: executeAllocInst((AllocationInst*)I, SF); break;
1189 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1190 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1191 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001192 case Instruction::GetElementPtr:
1193 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001194
1195 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001196 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1197 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1198 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1199 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1200 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001201 default:
1202 cout << "Don't know how to execute this instruction!\n-->" << I;
1203 }
1204 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001205 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001206
1207 // Reset the current frame location to the top of stack
1208 CurFrame = ECStack.size()-1;
1209
1210 if (CurFrame == -1) return false; // No breakpoint if no code
1211
1212 // Return true if there is a breakpoint annotation on the instruction...
1213 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
1214}
1215
1216void Interpreter::stepInstruction() { // Do the 'step' command
1217 if (ECStack.empty()) {
1218 cout << "Error: no program running, cannot step!\n";
1219 return;
1220 }
1221
1222 // Run an instruction...
1223 executeInstruction();
1224
1225 // Print the next instruction to execute...
1226 printCurrentInstruction();
1227}
1228
1229// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001230void Interpreter::nextInstruction() { // Do the 'next' command
1231 if (ECStack.empty()) {
1232 cout << "Error: no program running, cannot 'next'!\n";
1233 return;
1234 }
1235
1236 // If this is a call instruction, step over the call instruction...
1237 // TODO: ICALL, CALL WITH, ...
1238 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001239 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001240 // Step into the function...
1241 if (executeInstruction()) {
1242 // Hit a breakpoint, print current instruction, then return to user...
1243 cout << "Breakpoint hit!\n";
1244 printCurrentInstruction();
1245 return;
1246 }
1247
Chris Lattnera74a6b52001-10-29 14:08:33 +00001248 // If we we able to step into the function, finish it now. We might not be
1249 // able the step into a function, if it's external for example.
1250 if (ECStack.size() != StackSize)
1251 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001252 else
1253 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001254
Chris Lattner92101ac2001-08-23 17:05:04 +00001255 } else {
1256 // Normal instruction, just step...
1257 stepInstruction();
1258 }
1259}
1260
1261void Interpreter::run() {
1262 if (ECStack.empty()) {
1263 cout << "Error: no program running, cannot run!\n";
1264 return;
1265 }
1266
1267 bool HitBreakpoint = false;
1268 while (!ECStack.empty() && !HitBreakpoint) {
1269 // Run an instruction...
1270 HitBreakpoint = executeInstruction();
1271 }
1272
1273 if (HitBreakpoint) {
1274 cout << "Breakpoint hit!\n";
1275 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001276 // Print the next instruction to execute...
1277 printCurrentInstruction();
1278}
1279
1280void Interpreter::finish() {
1281 if (ECStack.empty()) {
1282 cout << "Error: no program running, cannot run!\n";
1283 return;
1284 }
1285
1286 unsigned StackSize = ECStack.size();
1287 bool HitBreakpoint = false;
1288 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1289 // Run an instruction...
1290 HitBreakpoint = executeInstruction();
1291 }
1292
1293 if (HitBreakpoint) {
1294 cout << "Breakpoint hit!\n";
1295 }
1296
1297 // Print the next instruction to execute...
1298 printCurrentInstruction();
1299}
1300
1301
1302
1303// printCurrentInstruction - Print out the instruction that the virtual PC is
1304// at, or fail silently if no program is running.
1305//
1306void Interpreter::printCurrentInstruction() {
1307 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001308 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1309 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1310
Chris Lattner92101ac2001-08-23 17:05:04 +00001311 Instruction *I = *ECStack.back().CurInst;
1312 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
1313 assert(IN && "Instruction has no numbering annotation!");
1314 cout << "#" << IN->InstNum << I;
1315 }
1316}
1317
1318void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001319 switch (Ty->getPrimitiveID()) {
1320 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1321 case Type::SByteTyID: cout << V.SByteVal; break;
1322 case Type::UByteTyID: cout << V.UByteVal; break;
1323 case Type::ShortTyID: cout << V.ShortVal; break;
1324 case Type::UShortTyID: cout << V.UShortVal; break;
1325 case Type::IntTyID: cout << V.IntVal; break;
1326 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001327 case Type::LongTyID: cout << (long)V.LongVal; break;
1328 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001329 case Type::FloatTyID: cout << V.FloatVal; break;
1330 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001331 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001332 default:
1333 cout << "- Don't know how to print value of this type!";
1334 break;
1335 }
1336}
1337
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001338void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001339 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001340 printValue(Ty, V);
1341}
1342
Chris Lattner697954c2002-01-20 22:54:45 +00001343void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001344 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1345 if (!PickedVal) return;
1346
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001347 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1348 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001349 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001350 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001351 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1352 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001353 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001354 print(PickedVal->getType(),
1355 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001356 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001357 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001358}
1359
Chris Lattner697954c2002-01-20 22:54:45 +00001360void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001361 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1362 if (!PickedVal) return;
1363
1364 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001365 print(PickedVal->getType(),
1366 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001367 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001368 printOperandInfo(PickedVal, ECStack[CurFrame]);
1369}
1370
Chris Lattner461f02f2001-11-07 05:31:27 +00001371// printStackFrame - Print information about the specified stack frame, or -1
1372// for the default one.
1373//
1374void Interpreter::printStackFrame(int FrameNo = -1) {
1375 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001376 Function *Func = ECStack[FrameNo].CurMethod;
1377 const Type *RetTy = Func->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001378
1379 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001380 << (Value*)RetTy << " \"" << Func->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001381
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001382 Function::ArgumentListType &Args = Func->getArgumentList();
Chris Lattner461f02f2001-11-07 05:31:27 +00001383 for (unsigned i = 0; i < Args.size(); ++i) {
1384 if (i != 0) cout << ", ";
1385 CW << (Value*)Args[i] << "=";
1386
Chris Lattnere06e9142002-04-09 19:59:31 +00001387 printValue(((Value*)Args[i])->getType(),
1388 getOperandValue((Value*)Args[i], ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001389 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001390
Chris Lattner697954c2002-01-20 22:54:45 +00001391 cout << ")\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001392 CW << *(ECStack[FrameNo].CurInst-(FrameNo != int(ECStack.size()-1)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001393}
Chris Lattner461f02f2001-11-07 05:31:27 +00001394