blob: 3d6cf0bfb9cde10375e5a25cc25ad17b91ba867a [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 Lattnerbbdabce2002-12-08 05:51:08 +000018#include "Support/Statistic.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 +000022using std::vector;
23using std::cout;
24using std::cerr;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000025
Chris Lattnerbbdabce2002-12-08 05:51:08 +000026namespace {
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
28}
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030static cl::opt<bool>
31QuietMode("quiet", cl::desc("Do not emit any non-program output"));
32
33static cl::alias
34QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
35
36static cl::opt<bool>
37ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
38
39static cl::opt<bool>
40AbortOnExceptions("abort-on-exception",
41 cl::desc("Halt execution on a machine exception"));
Chris Lattnere9bb2df2001-12-03 22:26:30 +000042
Chris Lattner2e42d3a2001-10-15 05:51:48 +000043// Create a TargetData structure to handle memory addressing and size/alignment
44// computations
45//
Chris Lattnerdbaf74d2002-10-02 21:10:48 +000046TargetData TD("lli Interpreter");
Chris Lattnerea38c0e2001-11-07 19:46:27 +000047CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000048
49
Chris Lattnere2409062001-11-12 16:19:45 +000050#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner5ff62e92002-07-22 02:10:13 +000051static cl::opt<bool>
52ProfileStructureFields("profilestructfields",
53 cl::desc("Profile Structure Field Accesses"));
Chris Lattnere2409062001-11-12 16:19:45 +000054#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000055static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000056#endif
57
Chris Lattner5af0c482001-11-07 04:23:00 +000058sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000059static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000060
61extern "C" {
62static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000063 if (InInstruction)
64 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000065}
66}
67
68static void initializeSignalHandlers() {
69 struct sigaction Action;
70 Action.sa_handler = SigHandler;
71 Action.sa_flags = SA_SIGINFO;
72 sigemptyset(&Action.sa_mask);
73 sigaction(SIGSEGV, &Action, 0);
74 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000075 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000076 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000077}
78
Chris Lattner2e42d3a2001-10-15 05:51:48 +000079
80//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000081// Value Manipulation code
82//===----------------------------------------------------------------------===//
83
84static unsigned getOperandSlot(Value *V) {
85 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
86 assert(SN && "Operand does not have a slot number annotation!");
87 return SN->SlotNum;
88}
89
90#define GET_CONST_VAL(TY, CLASS) \
Chris Lattnerfddc7552002-10-15 20:34:05 +000091 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattner39bb5b42001-10-15 13:25:40 +000092
Chris Lattnera34c5682002-08-27 22:33:45 +000093// Operations used by constant expr implementations...
94static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
95 ExecutionContext &SF);
96static GenericValue executeGEPOperation(Value *Src, User::op_iterator IdxBegin,
97 User::op_iterator IdxEnd,
98 ExecutionContext &SF);
99static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
100 const Type *Ty, ExecutionContext &SF);
101
Chris Lattnerfddc7552002-10-15 20:34:05 +0000102static GenericValue getConstantValue(const Constant *C) {
103 GenericValue Result;
104 switch (C->getType()->getPrimitiveID()) {
105 GET_CONST_VAL(Bool , ConstantBool);
106 GET_CONST_VAL(UByte , ConstantUInt);
107 GET_CONST_VAL(SByte , ConstantSInt);
108 GET_CONST_VAL(UShort , ConstantUInt);
109 GET_CONST_VAL(Short , ConstantSInt);
110 GET_CONST_VAL(UInt , ConstantUInt);
111 GET_CONST_VAL(Int , ConstantSInt);
112 GET_CONST_VAL(ULong , ConstantUInt);
113 GET_CONST_VAL(Long , ConstantSInt);
114 GET_CONST_VAL(Float , ConstantFP);
115 GET_CONST_VAL(Double , ConstantFP);
116 case Type::PointerTyID:
117 if (isa<ConstantPointerNull>(C)) {
118 Result.PointerVal = 0;
119 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
120 GlobalAddress *Address =
121 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
122 Result.PointerVal = (PointerTy)Address->Ptr;
123 } else {
124 assert(0 && "Unknown constant pointer type!");
125 }
126 break;
127 default:
128 cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
129 }
130 return Result;
131}
132
Chris Lattner39bb5b42001-10-15 13:25:40 +0000133static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000134 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
135 switch (CE->getOpcode()) {
136 case Instruction::Cast:
137 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
138 case Instruction::GetElementPtr:
139 return executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
140 CE->op_end(), SF);
141 case Instruction::Add:
142 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
143 getOperandValue(CE->getOperand(1), SF),
144 CE->getType(), SF);
145 default:
146 cerr << "Unhandled ConstantExpr: " << CE << "\n";
147 abort();
148 { GenericValue V; return V; }
149 }
150 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfddc7552002-10-15 20:34:05 +0000151 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000152 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
153 GlobalAddress *Address =
154 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
155 GenericValue Result;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000156 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000157 return Result;
158 } else {
159 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000160 unsigned OpSlot = getOperandSlot(V);
161 assert(TyP < SF.Values.size() &&
162 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000163 return SF.Values[TyP][getOperandSlot(V)];
164 }
165}
166
167static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000168 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000169 cout << "Constant Pool Value\n";
170 } else if (isa<GlobalValue>(V)) {
171 cout << "Global Value\n";
172 } else {
173 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
174 unsigned Slot = getOperandSlot(V);
175 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000176 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
177 << " Contents=0x";
178
179 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
180 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
181 unsigned char Cur = Buf[i];
182 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
183 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
184 }
Chris Lattner697954c2002-01-20 22:54:45 +0000185 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000186 }
187}
188
189
190
191static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
192 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
193
Chris Lattner697954c2002-01-20 22:54:45 +0000194 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000195 SF.Values[TyP][getOperandSlot(V)] = Val;
196}
197
198
199//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000200// Annotation Wrangling code
201//===----------------------------------------------------------------------===//
202
203void Interpreter::initializeExecutionEngine() {
204 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
205 &MethodInfo::Create);
206 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
207 &GlobalAddress::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000208 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000209}
210
Chris Lattnerfddc7552002-10-15 20:34:05 +0000211static void StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
212 const Type *Ty);
213
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000214// InitializeMemory - Recursive function to apply a Constant value into the
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000215// specified memory location...
216//
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000217static void InitializeMemory(const Constant *Init, char *Addr) {
Chris Lattnerfddc7552002-10-15 20:34:05 +0000218
219 if (Init->getType()->isFirstClassType()) {
220 GenericValue Val = getConstantValue(Init);
221 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
222 return;
223 }
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000224
225 switch (Init->getType()->getPrimitiveID()) {
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000226 case Type::ArrayTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000227 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000228 const vector<Use> &Val = CPA->getValues();
229 unsigned ElementSize =
230 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
231 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000232 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000233 return;
234 }
Chris Lattner39bb5b42001-10-15 13:25:40 +0000235
236 case Type::StructTyID: {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000237 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000238 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
239 const vector<Use> &Val = CPS->getValues();
240 for (unsigned i = 0; i < Val.size(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000241 InitializeMemory(cast<Constant>(Val[i].get()),
Chris Lattner39bb5b42001-10-15 13:25:40 +0000242 Addr+SL->MemberOffsets[i]);
243 return;
244 }
245
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000246 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000247 CW << "Bad Type: " << Init->getType() << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000248 assert(0 && "Unknown constant type to initialize memory with!");
249 }
250}
251
252Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
253 assert(AID == GlobalAddressAID);
254
255 // This annotation will only be created on GlobalValue objects...
256 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
257
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000258 if (isa<Function>(GVal)) {
259 // The GlobalAddress object for a function is just a pointer to function
260 // itself. Don't delete it when the annotation is gone though!
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000261 return new GlobalAddress(GVal, false);
262 }
263
264 // Handle the case of a global variable...
265 assert(isa<GlobalVariable>(GVal) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000266 "Global value found that isn't a function or global variable!");
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000267 GlobalVariable *GV = cast<GlobalVariable>(GVal);
268
269 // First off, we must allocate space for the global variable to point at...
Chris Lattner7a176752001-12-04 00:03:30 +0000270 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000271
272 // Allocate enough memory to hold the type...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000273 void *Addr = calloc(1, TD.getTypeSize(Ty));
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000274 assert(Addr != 0 && "Null pointer returned by malloc!");
275
276 // Initialize the memory if there is an initializer...
277 if (GV->hasInitializer())
278 InitializeMemory(GV->getInitializer(), (char*)Addr);
279
280 return new GlobalAddress(Addr, true); // Simply invoke the ctor
281}
282
Chris Lattner2adcd832002-05-03 19:52:30 +0000283//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000284// Binary Instruction Implementations
285//===----------------------------------------------------------------------===//
286
287#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
288 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
289
290static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
291 const Type *Ty, ExecutionContext &SF) {
292 GenericValue Dest;
293 switch (Ty->getPrimitiveID()) {
294 IMPLEMENT_BINARY_OPERATOR(+, UByte);
295 IMPLEMENT_BINARY_OPERATOR(+, SByte);
296 IMPLEMENT_BINARY_OPERATOR(+, UShort);
297 IMPLEMENT_BINARY_OPERATOR(+, Short);
298 IMPLEMENT_BINARY_OPERATOR(+, UInt);
299 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000300 IMPLEMENT_BINARY_OPERATOR(+, ULong);
301 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 IMPLEMENT_BINARY_OPERATOR(+, Float);
303 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000304 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000305 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000306 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000307 }
308 return Dest;
309}
310
311static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
312 const Type *Ty, ExecutionContext &SF) {
313 GenericValue Dest;
314 switch (Ty->getPrimitiveID()) {
315 IMPLEMENT_BINARY_OPERATOR(-, UByte);
316 IMPLEMENT_BINARY_OPERATOR(-, SByte);
317 IMPLEMENT_BINARY_OPERATOR(-, UShort);
318 IMPLEMENT_BINARY_OPERATOR(-, Short);
319 IMPLEMENT_BINARY_OPERATOR(-, UInt);
320 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000321 IMPLEMENT_BINARY_OPERATOR(-, ULong);
322 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000323 IMPLEMENT_BINARY_OPERATOR(-, Float);
324 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000325 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000326 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000327 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000328 }
329 return Dest;
330}
331
Chris Lattnerc2593162001-10-27 08:28:11 +0000332static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
333 const Type *Ty, ExecutionContext &SF) {
334 GenericValue Dest;
335 switch (Ty->getPrimitiveID()) {
336 IMPLEMENT_BINARY_OPERATOR(*, UByte);
337 IMPLEMENT_BINARY_OPERATOR(*, SByte);
338 IMPLEMENT_BINARY_OPERATOR(*, UShort);
339 IMPLEMENT_BINARY_OPERATOR(*, Short);
340 IMPLEMENT_BINARY_OPERATOR(*, UInt);
341 IMPLEMENT_BINARY_OPERATOR(*, Int);
342 IMPLEMENT_BINARY_OPERATOR(*, ULong);
343 IMPLEMENT_BINARY_OPERATOR(*, Long);
344 IMPLEMENT_BINARY_OPERATOR(*, Float);
345 IMPLEMENT_BINARY_OPERATOR(*, Double);
346 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
347 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000348 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000349 }
350 return Dest;
351}
352
353static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
354 const Type *Ty, ExecutionContext &SF) {
355 GenericValue Dest;
356 switch (Ty->getPrimitiveID()) {
357 IMPLEMENT_BINARY_OPERATOR(/, UByte);
358 IMPLEMENT_BINARY_OPERATOR(/, SByte);
359 IMPLEMENT_BINARY_OPERATOR(/, UShort);
360 IMPLEMENT_BINARY_OPERATOR(/, Short);
361 IMPLEMENT_BINARY_OPERATOR(/, UInt);
362 IMPLEMENT_BINARY_OPERATOR(/, Int);
363 IMPLEMENT_BINARY_OPERATOR(/, ULong);
364 IMPLEMENT_BINARY_OPERATOR(/, Long);
365 IMPLEMENT_BINARY_OPERATOR(/, Float);
366 IMPLEMENT_BINARY_OPERATOR(/, Double);
367 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
368 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000369 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000370 }
371 return Dest;
372}
373
374static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
375 const Type *Ty, ExecutionContext &SF) {
376 GenericValue Dest;
377 switch (Ty->getPrimitiveID()) {
378 IMPLEMENT_BINARY_OPERATOR(%, UByte);
379 IMPLEMENT_BINARY_OPERATOR(%, SByte);
380 IMPLEMENT_BINARY_OPERATOR(%, UShort);
381 IMPLEMENT_BINARY_OPERATOR(%, Short);
382 IMPLEMENT_BINARY_OPERATOR(%, UInt);
383 IMPLEMENT_BINARY_OPERATOR(%, Int);
384 IMPLEMENT_BINARY_OPERATOR(%, ULong);
385 IMPLEMENT_BINARY_OPERATOR(%, Long);
386 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
387 case Type::FloatTyID:
388 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
389 break;
390 case Type::DoubleTyID:
391 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
392 break;
393 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000394 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000395 }
396 return Dest;
397}
398
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000399static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000400 const Type *Ty, ExecutionContext &SF) {
401 GenericValue Dest;
402 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000403 IMPLEMENT_BINARY_OPERATOR(&, UByte);
404 IMPLEMENT_BINARY_OPERATOR(&, SByte);
405 IMPLEMENT_BINARY_OPERATOR(&, UShort);
406 IMPLEMENT_BINARY_OPERATOR(&, Short);
407 IMPLEMENT_BINARY_OPERATOR(&, UInt);
408 IMPLEMENT_BINARY_OPERATOR(&, Int);
409 IMPLEMENT_BINARY_OPERATOR(&, ULong);
410 IMPLEMENT_BINARY_OPERATOR(&, Long);
411 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
412 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000413 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000414 }
415 return Dest;
416}
417
418
419static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
420 const Type *Ty, ExecutionContext &SF) {
421 GenericValue Dest;
422 switch (Ty->getPrimitiveID()) {
423 IMPLEMENT_BINARY_OPERATOR(|, UByte);
424 IMPLEMENT_BINARY_OPERATOR(|, SByte);
425 IMPLEMENT_BINARY_OPERATOR(|, UShort);
426 IMPLEMENT_BINARY_OPERATOR(|, Short);
427 IMPLEMENT_BINARY_OPERATOR(|, UInt);
428 IMPLEMENT_BINARY_OPERATOR(|, Int);
429 IMPLEMENT_BINARY_OPERATOR(|, ULong);
430 IMPLEMENT_BINARY_OPERATOR(|, Long);
431 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
432 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000433 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000434 }
435 return Dest;
436}
437
438
439static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
440 const Type *Ty, ExecutionContext &SF) {
441 GenericValue Dest;
442 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000443 IMPLEMENT_BINARY_OPERATOR(^, UByte);
444 IMPLEMENT_BINARY_OPERATOR(^, SByte);
445 IMPLEMENT_BINARY_OPERATOR(^, UShort);
446 IMPLEMENT_BINARY_OPERATOR(^, Short);
447 IMPLEMENT_BINARY_OPERATOR(^, UInt);
448 IMPLEMENT_BINARY_OPERATOR(^, Int);
449 IMPLEMENT_BINARY_OPERATOR(^, ULong);
450 IMPLEMENT_BINARY_OPERATOR(^, Long);
451 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
452 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000453 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000454 }
455 return Dest;
456}
457
458
Chris Lattner92101ac2001-08-23 17:05:04 +0000459#define IMPLEMENT_SETCC(OP, TY) \
460 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
461
Chris Lattner92101ac2001-08-23 17:05:04 +0000462static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
463 const Type *Ty, ExecutionContext &SF) {
464 GenericValue Dest;
465 switch (Ty->getPrimitiveID()) {
466 IMPLEMENT_SETCC(==, UByte);
467 IMPLEMENT_SETCC(==, SByte);
468 IMPLEMENT_SETCC(==, UShort);
469 IMPLEMENT_SETCC(==, Short);
470 IMPLEMENT_SETCC(==, UInt);
471 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000472 IMPLEMENT_SETCC(==, ULong);
473 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 IMPLEMENT_SETCC(==, Float);
475 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000476 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000478 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 }
480 return Dest;
481}
482
483static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
484 const Type *Ty, ExecutionContext &SF) {
485 GenericValue Dest;
486 switch (Ty->getPrimitiveID()) {
487 IMPLEMENT_SETCC(!=, UByte);
488 IMPLEMENT_SETCC(!=, SByte);
489 IMPLEMENT_SETCC(!=, UShort);
490 IMPLEMENT_SETCC(!=, Short);
491 IMPLEMENT_SETCC(!=, UInt);
492 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000493 IMPLEMENT_SETCC(!=, ULong);
494 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000495 IMPLEMENT_SETCC(!=, Float);
496 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000497 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000498
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000500 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 }
502 return Dest;
503}
504
505static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
506 const Type *Ty, ExecutionContext &SF) {
507 GenericValue Dest;
508 switch (Ty->getPrimitiveID()) {
509 IMPLEMENT_SETCC(<=, UByte);
510 IMPLEMENT_SETCC(<=, SByte);
511 IMPLEMENT_SETCC(<=, UShort);
512 IMPLEMENT_SETCC(<=, Short);
513 IMPLEMENT_SETCC(<=, UInt);
514 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000515 IMPLEMENT_SETCC(<=, ULong);
516 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 IMPLEMENT_SETCC(<=, Float);
518 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000519 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000520 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000521 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000522 }
523 return Dest;
524}
525
526static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
527 const Type *Ty, ExecutionContext &SF) {
528 GenericValue Dest;
529 switch (Ty->getPrimitiveID()) {
530 IMPLEMENT_SETCC(>=, UByte);
531 IMPLEMENT_SETCC(>=, SByte);
532 IMPLEMENT_SETCC(>=, UShort);
533 IMPLEMENT_SETCC(>=, Short);
534 IMPLEMENT_SETCC(>=, UInt);
535 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000536 IMPLEMENT_SETCC(>=, ULong);
537 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 IMPLEMENT_SETCC(>=, Float);
539 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000540 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000541 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000542 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000543 }
544 return Dest;
545}
546
547static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
548 const Type *Ty, ExecutionContext &SF) {
549 GenericValue Dest;
550 switch (Ty->getPrimitiveID()) {
551 IMPLEMENT_SETCC(<, UByte);
552 IMPLEMENT_SETCC(<, SByte);
553 IMPLEMENT_SETCC(<, UShort);
554 IMPLEMENT_SETCC(<, Short);
555 IMPLEMENT_SETCC(<, UInt);
556 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000557 IMPLEMENT_SETCC(<, ULong);
558 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 IMPLEMENT_SETCC(<, Float);
560 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000561 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000563 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000564 }
565 return Dest;
566}
567
568static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
569 const Type *Ty, ExecutionContext &SF) {
570 GenericValue Dest;
571 switch (Ty->getPrimitiveID()) {
572 IMPLEMENT_SETCC(>, UByte);
573 IMPLEMENT_SETCC(>, SByte);
574 IMPLEMENT_SETCC(>, UShort);
575 IMPLEMENT_SETCC(>, Short);
576 IMPLEMENT_SETCC(>, UInt);
577 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000578 IMPLEMENT_SETCC(>, ULong);
579 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000580 IMPLEMENT_SETCC(>, Float);
581 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000582 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000583 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000584 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000585 }
586 return Dest;
587}
588
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000589static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
590 const Type *Ty = I.getOperand(0)->getType();
591 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
592 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000593 GenericValue R; // Result
594
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000595 switch (I.getOpcode()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000596 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
597 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
598 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
599 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
600 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000601 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
602 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000603 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000604 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
605 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
606 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
607 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
608 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
609 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
610 default:
611 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000612 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000613 }
614
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000615 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000616}
617
Chris Lattner92101ac2001-08-23 17:05:04 +0000618//===----------------------------------------------------------------------===//
619// Terminator Instruction Implementations
620//===----------------------------------------------------------------------===//
621
Chris Lattnera95c6992001-11-12 16:28:48 +0000622static void PerformExitStuff() {
623#ifdef PROFILE_STRUCTURE_FIELDS
624 // Print out structure field accounting information...
625 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000626 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000627 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000628 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
629 for (; I != E; ++I) {
630 vector<unsigned> &OfC = I->second;
631 CW << " '" << (Value*)I->first << "'\t- Sum=";
632
633 unsigned Sum = 0;
634 for (unsigned i = 0; i < OfC.size(); ++i)
635 Sum += OfC[i];
636 CW << Sum << " - ";
637
638 for (unsigned i = 0; i < OfC.size(); ++i) {
639 if (i) CW << ", ";
640 CW << OfC[i];
641 }
Chris Lattner697954c2002-01-20 22:54:45 +0000642 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000643 }
Chris Lattner697954c2002-01-20 22:54:45 +0000644 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000645
646 CW << "Profile Field Access Percentages:\n";
647 cout.precision(3);
648 for (I = FieldAccessCounts.begin(); I != E; ++I) {
649 vector<unsigned> &OfC = I->second;
650 unsigned Sum = 0;
651 for (unsigned i = 0; i < OfC.size(); ++i)
652 Sum += OfC[i];
653
654 CW << " '" << (Value*)I->first << "'\t- ";
655 for (unsigned i = 0; i < OfC.size(); ++i) {
656 if (i) CW << ", ";
657 CW << double(OfC[i])/Sum;
658 }
Chris Lattner697954c2002-01-20 22:54:45 +0000659 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000660 }
Chris Lattner697954c2002-01-20 22:54:45 +0000661 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000662
Chris Lattnera95c6992001-11-12 16:28:48 +0000663 FieldAccessCounts.clear();
664 }
665#endif
666}
667
Chris Lattnere43db882001-10-27 04:15:57 +0000668void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000669 if (!QuietMode) {
670 cout << "Program returned ";
671 print(Type::IntTy, GV);
672 cout << " via 'void exit(int)'\n";
673 }
Chris Lattnere43db882001-10-27 04:15:57 +0000674
675 ExitCode = GV.SByteVal;
676 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000677 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000678}
679
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000680void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000681 const Type *RetTy = 0;
682 GenericValue Result;
683
684 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000685 if (I.getNumOperands()) {
686 RetTy = I.getReturnValue()->getType();
687 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000688 }
689
690 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000691 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000692
693 // Pop the current stack frame... this invalidates SF
694 ECStack.pop_back();
695
696 if (ECStack.empty()) { // Finished main. Put result into exit code...
697 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000698 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000699 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000700 << "\" returned ";
701 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000702 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000703 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000704
705 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000706 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000707 } else {
708 ExitCode = 0;
709 }
Chris Lattnere2409062001-11-12 16:19:45 +0000710
Chris Lattnera95c6992001-11-12 16:28:48 +0000711 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000712 return;
713 }
714
715 // If we have a previous stack frame, and we have a previous call, fill in
716 // the return value...
717 //
718 ExecutionContext &NewSF = ECStack.back();
719 if (NewSF.Caller) {
720 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
721 SetValue(NewSF.Caller, Result, NewSF);
722
723 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000724 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000725 // This must be a function that is executing because of a user 'call'
726 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000727 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000728 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000729 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000730 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000731 }
732}
733
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000734void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000735 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
736 BasicBlock *Dest;
737
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000738 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
739 if (!I.isUnconditional()) {
740 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000741 GenericValue CondVal = getOperandValue(Cond, SF);
742 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000743 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000744 }
745 SF.CurBB = Dest; // Update CurBB to branch destination
746 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
747}
748
749//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000750// Memory Instruction Implementations
751//===----------------------------------------------------------------------===//
752
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000753void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
754 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000755
Chris Lattnercc82cc12002-04-28 21:57:33 +0000756 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000757 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000758
759 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000760 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000761 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
762
763 GenericValue Result;
764 Result.PointerVal = (PointerTy)Memory;
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000765 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000766 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000767
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000769 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000770}
771
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000772static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
773 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
774 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000775 // TODO: Check to make sure memory is allocated
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000776 free((void*)Value.PointerVal); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000777}
778
Chris Lattner95c3af52001-10-29 19:32:19 +0000779
Chris Lattnera34c5682002-08-27 22:33:45 +0000780// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000781//
Chris Lattnera34c5682002-08-27 22:33:45 +0000782static GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
783 User::op_iterator E,
784 ExecutionContext &SF) {
785 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000786 "Cannot getElementOffset of a nonpointer type!");
787
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000788 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000789 const Type *Ty = Ptr->getType();
790
791 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000792 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
793 const StructLayout *SLO = TD.getStructLayout(STy);
794
795 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000796 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000797 assert(CPU->getType() == Type::UByteTy);
798 unsigned Index = CPU->getValue();
799
Chris Lattnere2409062001-11-12 16:19:45 +0000800#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000801 if (ProfileStructureFields) {
802 // Do accounting for this field...
803 vector<unsigned> &OfC = FieldAccessCounts[STy];
804 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
805 OfC[Index]++;
806 }
Chris Lattnere2409062001-11-12 16:19:45 +0000807#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000808
809 Total += SLO->MemberOffsets[Index];
810 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000811 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000812
Chris Lattner782b9392001-11-26 18:18:18 +0000813 // Get the index number for the array... which must be uint type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000814 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000815 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000816 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000817 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000818 cerr << "Out of range memory access to element #" << Idx
819 << " of a " << AT->getNumElements() << " element array."
Chris Lattnera34c5682002-08-27 22:33:45 +0000820 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000821 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000822 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000823 }
Chris Lattner782b9392001-11-26 18:18:18 +0000824
Chris Lattnerf23eb852001-12-14 16:49:29 +0000825 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000826 unsigned Size = TD.getTypeSize(Ty);
827 Total += Size*Idx;
828 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000829 }
830
Chris Lattnera34c5682002-08-27 22:33:45 +0000831 GenericValue Result;
832 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
833 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000834}
835
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000836static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000837 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
838 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000839}
840
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000841static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
842 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattner24ea74e2002-08-22 22:49:05 +0000843 GenericValue *Ptr = (GenericValue*)SRC.PointerVal;
Chris Lattner86660982001-08-27 05:16:50 +0000844 GenericValue Result;
845
Chris Lattnerfddc7552002-10-15 20:34:05 +0000846 if (TD.isLittleEndian()) {
847 switch (I.getType()->getPrimitiveID()) {
848 case Type::BoolTyID:
849 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000850 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000851 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000852 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
853 ((unsigned)Ptr->Untyped[1] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000854 break;
855 case Type::FloatTyID:
856 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000857 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
858 ((unsigned)Ptr->Untyped[1] << 8) |
859 ((unsigned)Ptr->Untyped[2] << 16) |
860 ((unsigned)Ptr->Untyped[3] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000861 break;
862 case Type::DoubleTyID:
863 case Type::ULongTyID:
864 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000865 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
866 ((uint64_t)Ptr->Untyped[1] << 8) |
867 ((uint64_t)Ptr->Untyped[2] << 16) |
868 ((uint64_t)Ptr->Untyped[3] << 24) |
869 ((uint64_t)Ptr->Untyped[4] << 32) |
870 ((uint64_t)Ptr->Untyped[5] << 40) |
871 ((uint64_t)Ptr->Untyped[6] << 48) |
872 ((uint64_t)Ptr->Untyped[7] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000873 break;
874 default:
875 cout << "Cannot load value of type " << I.getType() << "!\n";
876 }
877 } else {
878 switch (I.getType()->getPrimitiveID()) {
879 case Type::BoolTyID:
880 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000881 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000882 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000883 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
884 ((unsigned)Ptr->Untyped[0] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000885 break;
886 case Type::FloatTyID:
887 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000888 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
889 ((unsigned)Ptr->Untyped[2] << 8) |
890 ((unsigned)Ptr->Untyped[1] << 16) |
891 ((unsigned)Ptr->Untyped[0] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000892 break;
893 case Type::DoubleTyID:
894 case Type::ULongTyID:
895 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000896 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
897 ((uint64_t)Ptr->Untyped[6] << 8) |
898 ((uint64_t)Ptr->Untyped[5] << 16) |
899 ((uint64_t)Ptr->Untyped[4] << 24) |
900 ((uint64_t)Ptr->Untyped[3] << 32) |
901 ((uint64_t)Ptr->Untyped[2] << 40) |
902 ((uint64_t)Ptr->Untyped[1] << 48) |
903 ((uint64_t)Ptr->Untyped[0] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000904 break;
905 default:
906 cout << "Cannot load value of type " << I.getType() << "!\n";
907 }
Chris Lattner86660982001-08-27 05:16:50 +0000908 }
909
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000910 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000911}
912
Chris Lattnerfddc7552002-10-15 20:34:05 +0000913static void StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
914 const Type *Ty) {
915 if (TD.isLittleEndian()) {
916 switch (Ty->getPrimitiveID()) {
917 case Type::BoolTyID:
918 case Type::UByteTyID:
919 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
920 case Type::UShortTyID:
921 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
922 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
923 break;
924 case Type::FloatTyID:
925 case Type::UIntTyID:
926 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
927 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
928 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
929 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
930 break;
931 case Type::DoubleTyID:
932 case Type::ULongTyID:
933 case Type::LongTyID:
934 case Type::PointerTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
935 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
936 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
937 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
938 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
939 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
940 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
941 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
942 break;
943 default:
Chris Lattner683d5da92002-10-26 01:57:15 +0000944 cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerfddc7552002-10-15 20:34:05 +0000945 }
946 } else {
947 switch (Ty->getPrimitiveID()) {
948 case Type::BoolTyID:
949 case Type::UByteTyID:
950 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
951 case Type::UShortTyID:
952 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
953 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
954 break;
955 case Type::FloatTyID:
956 case Type::UIntTyID:
957 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
958 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
959 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
960 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
961 break;
962 case Type::DoubleTyID:
963 case Type::ULongTyID:
964 case Type::LongTyID:
965 case Type::PointerTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
966 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
967 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
968 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
969 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
970 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
971 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
972 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
973 break;
974 default:
Chris Lattner683d5da92002-10-26 01:57:15 +0000975 cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerfddc7552002-10-15 20:34:05 +0000976 }
Chris Lattner86660982001-08-27 05:16:50 +0000977 }
978}
979
Chris Lattnerfddc7552002-10-15 20:34:05 +0000980static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
981 GenericValue Val = getOperandValue(I.getOperand(0), SF);
982 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattner683d5da92002-10-26 01:57:15 +0000983 StoreValueToMemory(Val, (GenericValue *)SRC.PointerVal,
984 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000985}
986
Chris Lattner86660982001-08-27 05:16:50 +0000987
Chris Lattnerab2dea52002-11-07 19:29:31 +0000988GenericValue Interpreter::CreateArgv(const std::vector<std::string> &InputArgv){
989 // Pointers are 64 bits...
990 PointerTy *Result = new PointerTy[InputArgv.size()+1]; // 64 bit assumption
991
992 for (unsigned i = 0; i < InputArgv.size(); ++i) {
993 unsigned Size = InputArgv[i].size()+1;
994 char *Dest = new char[Size];
995 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
996 Dest[Size-1] = 0;
997
998 GenericValue GV; GV.PointerVal = (PointerTy)Dest;
999 // Endian safe: Result[i] = (PointerTy)Dest;
1000 StoreValueToMemory(GV, (GenericValue*)(Result+i),
1001 Type::LongTy); // 64 bit assumption
1002 }
1003
1004 Result[InputArgv.size()] = 0;
1005 GenericValue GV; GV.PointerVal = (PointerTy)Result;
1006 return GV;
1007}
1008
1009
Chris Lattner86660982001-08-27 05:16:50 +00001010//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001011// Miscellaneous Instruction Implementations
1012//===----------------------------------------------------------------------===//
1013
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001014void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
1015 ECStack.back().Caller = &I;
Chris Lattner365a76e2001-09-10 04:49:44 +00001016 vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001017 ArgVals.reserve(I.getNumOperands()-1);
1018 for (unsigned i = 1; i < I.getNumOperands(); ++i)
1019 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner365a76e2001-09-10 04:49:44 +00001020
Chris Lattner070cf5e2001-11-07 20:12:30 +00001021 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001022 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001023 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +00001024
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001025 callMethod((Function*)SRC.PointerVal, ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001026}
1027
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001028static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001029 BasicBlock *PrevBB = SF.PrevBB;
1030 Value *IncomingValue = 0;
1031
1032 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001033 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
1034 if (I.getIncomingBlock(--i) == PrevBB) {
1035 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +00001036 break;
1037 }
1038 }
1039 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
1040
1041 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001042 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001043}
1044
Chris Lattner86660982001-08-27 05:16:50 +00001045#define IMPLEMENT_SHIFT(OP, TY) \
1046 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
1047
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001048static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
1049 const Type *Ty = I.getOperand(0)->getType();
1050 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1051 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001052 GenericValue Dest;
1053
1054 switch (Ty->getPrimitiveID()) {
1055 IMPLEMENT_SHIFT(<<, UByte);
1056 IMPLEMENT_SHIFT(<<, SByte);
1057 IMPLEMENT_SHIFT(<<, UShort);
1058 IMPLEMENT_SHIFT(<<, Short);
1059 IMPLEMENT_SHIFT(<<, UInt);
1060 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +00001061 IMPLEMENT_SHIFT(<<, ULong);
1062 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +00001063 IMPLEMENT_SHIFT(<<, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001064 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001065 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001066 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001067 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001068}
1069
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001070static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
1071 const Type *Ty = I.getOperand(0)->getType();
1072 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1073 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001074 GenericValue Dest;
1075
1076 switch (Ty->getPrimitiveID()) {
1077 IMPLEMENT_SHIFT(>>, UByte);
1078 IMPLEMENT_SHIFT(>>, SByte);
1079 IMPLEMENT_SHIFT(>>, UShort);
1080 IMPLEMENT_SHIFT(>>, Short);
1081 IMPLEMENT_SHIFT(>>, UInt);
1082 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +00001083 IMPLEMENT_SHIFT(>>, ULong);
1084 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +00001085 IMPLEMENT_SHIFT(>>, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001086 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001087 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001088 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001089 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001090}
1091
1092#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001093 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001094
1095#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1096 case Type::DESTTY##TyID: \
1097 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001098 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001099 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1100 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1101 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1102 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1103 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001104 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1105 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001106 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1107 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001108
1109#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1110 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1111 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1112
1113#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +00001114 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +00001115 break; \
1116 } \
1117 break
1118
1119#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1120 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1121 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001122 IMPLEMENT_CAST_CASE_END()
1123
Chris Lattnera34c5682002-08-27 22:33:45 +00001124static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
1125 ExecutionContext &SF) {
1126 const Type *SrcTy = SrcVal->getType();
1127 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001128
1129 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001130 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1131 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1132 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001133 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001134 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1135 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1136 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1137 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001138 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001139 IMPLEMENT_CAST_CASE(Float , (float));
1140 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +00001141 default:
Chris Lattner697954c2002-01-20 22:54:45 +00001142 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001143 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001144
1145 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001146}
Chris Lattner92101ac2001-08-23 17:05:04 +00001147
1148
Chris Lattnera34c5682002-08-27 22:33:45 +00001149static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1150 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1151}
Chris Lattner92101ac2001-08-23 17:05:04 +00001152
1153
1154//===----------------------------------------------------------------------===//
1155// Dispatch and Execution Code
1156//===----------------------------------------------------------------------===//
1157
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001158MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001159 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001160 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1161 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001162
1163 // Iterate over all of the instructions...
1164 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001165 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1166 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1167 // For each instruction... Add Annote
1168 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001169}
1170
1171unsigned MethodInfo::getValueSlot(const Value *V) {
1172 unsigned Plane = V->getType()->getUniqueID();
1173 if (Plane >= NumPlaneElements.size())
1174 NumPlaneElements.resize(Plane+1, 0);
1175 return NumPlaneElements[Plane]++;
1176}
1177
1178
Chris Lattner92101ac2001-08-23 17:05:04 +00001179//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001180// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001181//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001182void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001183 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1184 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1185 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001186 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001187 GenericValue Result = callExternalMethod(M, ArgVals);
1188 const Type *RetTy = M->getReturnType();
1189
1190 // Copy the result back into the result variable if we are not returning
1191 // void.
1192 if (RetTy != Type::VoidTy) {
1193 if (!ECStack.empty() && ECStack.back().Caller) {
1194 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001195 SetValue(SF.Caller, Result, SF);
1196
1197 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001198 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001199 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001200 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001201 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001202 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001203 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001204
1205 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +00001206 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +00001207 }
1208 }
1209
Chris Lattner92101ac2001-08-23 17:05:04 +00001210 return;
1211 }
1212
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001213 // Process the function, assigning instruction numbers to the instructions in
1214 // the function. Also calculate the number of values for each type slot
1215 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001216 //
1217 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001218 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001219
Chris Lattner92101ac2001-08-23 17:05:04 +00001220 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1221 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001222 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001223 StackFrame.CurInst = StackFrame.CurBB->begin();
1224 StackFrame.MethInfo = MethInfo;
1225
1226 // Initialize the values to nothing...
1227 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001228 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001229 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1230
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001231 // Taint the initial values of stuff
1232 memset(&StackFrame.Values[i][0], 42,
1233 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1234 }
1235
Chris Lattner92101ac2001-08-23 17:05:04 +00001236 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1237
Chris Lattner92101ac2001-08-23 17:05:04 +00001238
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001239 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001240 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001241 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001242 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001243 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1244 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001245}
1246
1247// executeInstruction - Interpret a single instruction, increment the "PC", and
1248// return true if the next instruction is a breakpoint...
1249//
1250bool Interpreter::executeInstruction() {
1251 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1252
1253 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001254 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001255
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001256 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001257 CW << "Run:" << I;
1258
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001259 // Track the number of dynamic instructions executed.
1260 ++NumDynamicInsts;
1261
Chris Lattner5af0c482001-11-07 04:23:00 +00001262 // Set a sigsetjmp buffer so that we can recover if an error happens during
1263 // instruction execution...
1264 //
1265 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1266 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001267 if (SigNo != SIGINT) {
Chris Lattner8b77be22002-09-13 14:41:38 +00001268 cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001269 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001270 // If -abort-on-exception was specified, terminate LLI instead of trying
1271 // to debug it.
1272 //
1273 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001274 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001275 cout << "CTRL-C Detected, execution halted.\n";
1276 }
1277 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001278 return true;
1279 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001280
Chris Lattner461f02f2001-11-07 05:31:27 +00001281 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001282 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001283 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001284 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001285 switch (I.getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001286 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001287 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1288 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001289 // Memory Instructions
1290 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001291 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001292 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1293 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1294 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001295 case Instruction::GetElementPtr:
1296 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001297
1298 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001299 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1300 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1301 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1302 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1303 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001304 default:
1305 cout << "Don't know how to execute this instruction!\n-->" << I;
1306 }
1307 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001308 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001309
1310 // Reset the current frame location to the top of stack
1311 CurFrame = ECStack.size()-1;
1312
1313 if (CurFrame == -1) return false; // No breakpoint if no code
1314
1315 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001316 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001317}
1318
1319void Interpreter::stepInstruction() { // Do the 'step' command
1320 if (ECStack.empty()) {
1321 cout << "Error: no program running, cannot step!\n";
1322 return;
1323 }
1324
1325 // Run an instruction...
1326 executeInstruction();
1327
1328 // Print the next instruction to execute...
1329 printCurrentInstruction();
1330}
1331
1332// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001333void Interpreter::nextInstruction() { // Do the 'next' command
1334 if (ECStack.empty()) {
1335 cout << "Error: no program running, cannot 'next'!\n";
1336 return;
1337 }
1338
1339 // If this is a call instruction, step over the call instruction...
1340 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001341 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001342 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001343 // Step into the function...
1344 if (executeInstruction()) {
1345 // Hit a breakpoint, print current instruction, then return to user...
1346 cout << "Breakpoint hit!\n";
1347 printCurrentInstruction();
1348 return;
1349 }
1350
Chris Lattnera74a6b52001-10-29 14:08:33 +00001351 // If we we able to step into the function, finish it now. We might not be
1352 // able the step into a function, if it's external for example.
1353 if (ECStack.size() != StackSize)
1354 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001355 else
1356 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001357
Chris Lattner92101ac2001-08-23 17:05:04 +00001358 } else {
1359 // Normal instruction, just step...
1360 stepInstruction();
1361 }
1362}
1363
1364void Interpreter::run() {
1365 if (ECStack.empty()) {
1366 cout << "Error: no program running, cannot run!\n";
1367 return;
1368 }
1369
1370 bool HitBreakpoint = false;
1371 while (!ECStack.empty() && !HitBreakpoint) {
1372 // Run an instruction...
1373 HitBreakpoint = executeInstruction();
1374 }
1375
1376 if (HitBreakpoint) {
1377 cout << "Breakpoint hit!\n";
1378 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001379 // Print the next instruction to execute...
1380 printCurrentInstruction();
1381}
1382
1383void Interpreter::finish() {
1384 if (ECStack.empty()) {
1385 cout << "Error: no program running, cannot run!\n";
1386 return;
1387 }
1388
1389 unsigned StackSize = ECStack.size();
1390 bool HitBreakpoint = false;
1391 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1392 // Run an instruction...
1393 HitBreakpoint = executeInstruction();
1394 }
1395
1396 if (HitBreakpoint) {
1397 cout << "Breakpoint hit!\n";
1398 }
1399
1400 // Print the next instruction to execute...
1401 printCurrentInstruction();
1402}
1403
1404
1405
1406// printCurrentInstruction - Print out the instruction that the virtual PC is
1407// at, or fail silently if no program is running.
1408//
1409void Interpreter::printCurrentInstruction() {
1410 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001411 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1412 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1413
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001414 Instruction &I = *ECStack.back().CurInst;
1415 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001416 assert(IN && "Instruction has no numbering annotation!");
1417 cout << "#" << IN->InstNum << I;
1418 }
1419}
1420
1421void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001422 switch (Ty->getPrimitiveID()) {
1423 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001424 case Type::SByteTyID:
1425 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1426 case Type::UByteTyID:
1427 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001428 case Type::ShortTyID: cout << V.ShortVal; break;
1429 case Type::UShortTyID: cout << V.UShortVal; break;
1430 case Type::IntTyID: cout << V.IntVal; break;
1431 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001432 case Type::LongTyID: cout << (long)V.LongVal; break;
1433 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001434 case Type::FloatTyID: cout << V.FloatVal; break;
1435 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001436 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001437 default:
1438 cout << "- Don't know how to print value of this type!";
1439 break;
1440 }
1441}
1442
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001443void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001444 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001445 printValue(Ty, V);
1446}
1447
Chris Lattner697954c2002-01-20 22:54:45 +00001448void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001449 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1450 if (!PickedVal) return;
1451
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001452 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1453 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001454 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001455 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001456 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1457 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001458 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001459 print(PickedVal->getType(),
1460 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001461 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001462 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001463}
1464
Chris Lattner697954c2002-01-20 22:54:45 +00001465void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001466 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1467 if (!PickedVal) return;
1468
1469 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001470 print(PickedVal->getType(),
1471 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001472 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001473 printOperandInfo(PickedVal, ECStack[CurFrame]);
1474}
1475
Chris Lattner461f02f2001-11-07 05:31:27 +00001476// printStackFrame - Print information about the specified stack frame, or -1
1477// for the default one.
1478//
Chris Lattner601d7152002-07-25 17:37:05 +00001479void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001480 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001481 Function *F = ECStack[FrameNo].CurMethod;
1482 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001483
1484 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001485 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001486
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001487 unsigned i = 0;
1488 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001489 if (i != 0) cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001490 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001491
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001492 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001493 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001494
Chris Lattner697954c2002-01-20 22:54:45 +00001495 cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001496
1497 if (FrameNo != int(ECStack.size()-1)) {
1498 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1499 CW << --I;
1500 } else {
1501 CW << *ECStack[FrameNo].CurInst;
1502 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001503}
Chris Lattner461f02f2001-11-07 05:31:27 +00001504