blob: 055a4256eddb2696df866f5bbb288ec69c0c58a2 [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 Lattnerfe11a972002-12-23 23:59:41 +00009#include "llvm/GlobalVariable.h"
10#include "llvm/Function.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000011#include "llvm/iPHINode.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000012#include "llvm/iOther.h"
13#include "llvm/iTerminators.h"
Chris Lattner86660982001-08-27 05:16:50 +000014#include "llvm/iMemory.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000015#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf23eb852001-12-14 16:49:29 +000018#include "Support/CommandLine.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000019#include "Support/Statistic.h"
Chris Lattnerbb76f022001-10-30 20:27:31 +000020#include <math.h> // For fmod
Chris Lattner5af0c482001-11-07 04:23:00 +000021#include <signal.h>
22#include <setjmp.h>
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::vector;
24using std::cout;
25using std::cerr;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000026
Chris Lattnerfe11a972002-12-23 23:59:41 +000027Interpreter *TheEE = 0;
28
Chris Lattnerbbdabce2002-12-08 05:51:08 +000029namespace {
30 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattner138b0cd2002-12-08 06:01:34 +000031
32 cl::opt<bool>
Chris Lattnerfe11a972002-12-23 23:59:41 +000033 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
34 cl::init(true));
Chris Lattner138b0cd2002-12-08 06:01:34 +000035
36 cl::alias
37 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
38
39 cl::opt<bool>
40 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
41
42 cl::opt<bool>
43 AbortOnExceptions("abort-on-exception",
44 cl::desc("Halt execution on a machine exception"));
Chris Lattnerbbdabce2002-12-08 05:51:08 +000045}
46
Chris Lattner2e42d3a2001-10-15 05:51:48 +000047// Create a TargetData structure to handle memory addressing and size/alignment
48// computations
49//
Chris Lattnerea38c0e2001-11-07 19:46:27 +000050CachedWriter CW; // Object to accelerate printing of LLVM
Chris Lattner5af0c482001-11-07 04:23:00 +000051
Chris Lattnere2409062001-11-12 16:19:45 +000052#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner5ff62e92002-07-22 02:10:13 +000053static cl::opt<bool>
54ProfileStructureFields("profilestructfields",
55 cl::desc("Profile Structure Field Accesses"));
Chris Lattnere2409062001-11-12 16:19:45 +000056#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000057static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
Chris Lattnere2409062001-11-12 16:19:45 +000058#endif
59
Chris Lattner5af0c482001-11-07 04:23:00 +000060sigjmp_buf SignalRecoverBuffer;
Chris Lattner461f02f2001-11-07 05:31:27 +000061static bool InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +000062
63extern "C" {
64static void SigHandler(int Signal) {
Chris Lattner461f02f2001-11-07 05:31:27 +000065 if (InInstruction)
66 siglongjmp(SignalRecoverBuffer, Signal);
Chris Lattner5af0c482001-11-07 04:23:00 +000067}
68}
69
70static void initializeSignalHandlers() {
71 struct sigaction Action;
72 Action.sa_handler = SigHandler;
73 Action.sa_flags = SA_SIGINFO;
74 sigemptyset(&Action.sa_mask);
75 sigaction(SIGSEGV, &Action, 0);
76 sigaction(SIGBUS, &Action, 0);
Chris Lattner461f02f2001-11-07 05:31:27 +000077 sigaction(SIGINT, &Action, 0);
Chris Lattnerea38c0e2001-11-07 19:46:27 +000078 sigaction(SIGFPE, &Action, 0);
Chris Lattner5af0c482001-11-07 04:23:00 +000079}
80
Chris Lattner2e42d3a2001-10-15 05:51:48 +000081
82//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000083// Value Manipulation code
84//===----------------------------------------------------------------------===//
85
86static unsigned getOperandSlot(Value *V) {
87 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
88 assert(SN && "Operand does not have a slot number annotation!");
89 return SN->SlotNum;
90}
91
Chris Lattnera34c5682002-08-27 22:33:45 +000092// Operations used by constant expr implementations...
93static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
94 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000095static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000096 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000097
Chris Lattnerfddc7552002-10-15 20:34:05 +000098
Chris Lattner39bb5b42001-10-15 13:25:40 +000099static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000100 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
101 switch (CE->getOpcode()) {
102 case Instruction::Cast:
103 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
104 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +0000105 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
106 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000107 case Instruction::Add:
108 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
109 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000110 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +0000111 default:
112 cerr << "Unhandled ConstantExpr: " << CE << "\n";
113 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +0000114 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +0000115 }
116 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000117 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000118 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000119 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000120 } else {
121 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
Chris Lattnerbb76f022001-10-30 20:27:31 +0000122 unsigned OpSlot = getOperandSlot(V);
123 assert(TyP < SF.Values.size() &&
124 OpSlot < SF.Values[TyP].size() && "Value out of range!");
Chris Lattner39bb5b42001-10-15 13:25:40 +0000125 return SF.Values[TyP][getOperandSlot(V)];
126 }
127}
128
129static void printOperandInfo(Value *V, ExecutionContext &SF) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000130 if (isa<Constant>(V)) {
Chris Lattner39bb5b42001-10-15 13:25:40 +0000131 cout << "Constant Pool Value\n";
132 } else if (isa<GlobalValue>(V)) {
133 cout << "Global Value\n";
134 } else {
135 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
136 unsigned Slot = getOperandSlot(V);
137 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000138 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
139 << " Contents=0x";
140
141 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
142 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
143 unsigned char Cur = Buf[i];
144 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
145 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
146 }
Chris Lattner697954c2002-01-20 22:54:45 +0000147 cout << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000148 }
149}
150
151
152
153static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
154 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
155
Chris Lattner697954c2002-01-20 22:54:45 +0000156 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
Chris Lattner39bb5b42001-10-15 13:25:40 +0000157 SF.Values[TyP][getOperandSlot(V)] = Val;
158}
159
160
161//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000162// Annotation Wrangling code
163//===----------------------------------------------------------------------===//
164
165void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000166 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000167 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
168 &MethodInfo::Create);
Chris Lattner5af0c482001-11-07 04:23:00 +0000169 initializeSignalHandlers();
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000170}
171
Chris Lattner2adcd832002-05-03 19:52:30 +0000172//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000173// Binary Instruction Implementations
174//===----------------------------------------------------------------------===//
175
176#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
177 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
178
179static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000180 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000181 GenericValue Dest;
182 switch (Ty->getPrimitiveID()) {
183 IMPLEMENT_BINARY_OPERATOR(+, UByte);
184 IMPLEMENT_BINARY_OPERATOR(+, SByte);
185 IMPLEMENT_BINARY_OPERATOR(+, UShort);
186 IMPLEMENT_BINARY_OPERATOR(+, Short);
187 IMPLEMENT_BINARY_OPERATOR(+, UInt);
188 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000189 IMPLEMENT_BINARY_OPERATOR(+, ULong);
190 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000191 IMPLEMENT_BINARY_OPERATOR(+, Float);
192 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000193 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000194 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000195 cout << "Unhandled type for Add instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000196 }
197 return Dest;
198}
199
200static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000201 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000202 GenericValue Dest;
203 switch (Ty->getPrimitiveID()) {
204 IMPLEMENT_BINARY_OPERATOR(-, UByte);
205 IMPLEMENT_BINARY_OPERATOR(-, SByte);
206 IMPLEMENT_BINARY_OPERATOR(-, UShort);
207 IMPLEMENT_BINARY_OPERATOR(-, Short);
208 IMPLEMENT_BINARY_OPERATOR(-, UInt);
209 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000210 IMPLEMENT_BINARY_OPERATOR(-, ULong);
211 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000212 IMPLEMENT_BINARY_OPERATOR(-, Float);
213 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000214 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000215 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000216 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000217 }
218 return Dest;
219}
220
Chris Lattnerc2593162001-10-27 08:28:11 +0000221static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000222 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000223 GenericValue Dest;
224 switch (Ty->getPrimitiveID()) {
225 IMPLEMENT_BINARY_OPERATOR(*, UByte);
226 IMPLEMENT_BINARY_OPERATOR(*, SByte);
227 IMPLEMENT_BINARY_OPERATOR(*, UShort);
228 IMPLEMENT_BINARY_OPERATOR(*, Short);
229 IMPLEMENT_BINARY_OPERATOR(*, UInt);
230 IMPLEMENT_BINARY_OPERATOR(*, Int);
231 IMPLEMENT_BINARY_OPERATOR(*, ULong);
232 IMPLEMENT_BINARY_OPERATOR(*, Long);
233 IMPLEMENT_BINARY_OPERATOR(*, Float);
234 IMPLEMENT_BINARY_OPERATOR(*, Double);
235 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
236 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000237 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000238 }
239 return Dest;
240}
241
242static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000243 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000244 GenericValue Dest;
245 switch (Ty->getPrimitiveID()) {
246 IMPLEMENT_BINARY_OPERATOR(/, UByte);
247 IMPLEMENT_BINARY_OPERATOR(/, SByte);
248 IMPLEMENT_BINARY_OPERATOR(/, UShort);
249 IMPLEMENT_BINARY_OPERATOR(/, Short);
250 IMPLEMENT_BINARY_OPERATOR(/, UInt);
251 IMPLEMENT_BINARY_OPERATOR(/, Int);
252 IMPLEMENT_BINARY_OPERATOR(/, ULong);
253 IMPLEMENT_BINARY_OPERATOR(/, Long);
254 IMPLEMENT_BINARY_OPERATOR(/, Float);
255 IMPLEMENT_BINARY_OPERATOR(/, Double);
256 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
257 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000258 cout << "Unhandled type for Div instruction: " << Ty << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +0000259 }
260 return Dest;
261}
262
263static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000264 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000265 GenericValue Dest;
266 switch (Ty->getPrimitiveID()) {
267 IMPLEMENT_BINARY_OPERATOR(%, UByte);
268 IMPLEMENT_BINARY_OPERATOR(%, SByte);
269 IMPLEMENT_BINARY_OPERATOR(%, UShort);
270 IMPLEMENT_BINARY_OPERATOR(%, Short);
271 IMPLEMENT_BINARY_OPERATOR(%, UInt);
272 IMPLEMENT_BINARY_OPERATOR(%, Int);
273 IMPLEMENT_BINARY_OPERATOR(%, ULong);
274 IMPLEMENT_BINARY_OPERATOR(%, Long);
275 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
276 case Type::FloatTyID:
277 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
278 break;
279 case Type::DoubleTyID:
280 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
281 break;
282 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000283 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
Chris Lattnerc2593162001-10-27 08:28:11 +0000284 }
285 return Dest;
286}
287
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000288static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000289 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000290 GenericValue Dest;
291 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000292 IMPLEMENT_BINARY_OPERATOR(&, UByte);
293 IMPLEMENT_BINARY_OPERATOR(&, SByte);
294 IMPLEMENT_BINARY_OPERATOR(&, UShort);
295 IMPLEMENT_BINARY_OPERATOR(&, Short);
296 IMPLEMENT_BINARY_OPERATOR(&, UInt);
297 IMPLEMENT_BINARY_OPERATOR(&, Int);
298 IMPLEMENT_BINARY_OPERATOR(&, ULong);
299 IMPLEMENT_BINARY_OPERATOR(&, Long);
300 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
301 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000302 cout << "Unhandled type for And instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000303 }
304 return Dest;
305}
306
307
308static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000309 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000310 GenericValue Dest;
311 switch (Ty->getPrimitiveID()) {
312 IMPLEMENT_BINARY_OPERATOR(|, UByte);
313 IMPLEMENT_BINARY_OPERATOR(|, SByte);
314 IMPLEMENT_BINARY_OPERATOR(|, UShort);
315 IMPLEMENT_BINARY_OPERATOR(|, Short);
316 IMPLEMENT_BINARY_OPERATOR(|, UInt);
317 IMPLEMENT_BINARY_OPERATOR(|, Int);
318 IMPLEMENT_BINARY_OPERATOR(|, ULong);
319 IMPLEMENT_BINARY_OPERATOR(|, Long);
320 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
321 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000322 cout << "Unhandled type for Or instruction: " << Ty << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000323 }
324 return Dest;
325}
326
327
328static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000329 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000330 GenericValue Dest;
331 switch (Ty->getPrimitiveID()) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000332 IMPLEMENT_BINARY_OPERATOR(^, UByte);
333 IMPLEMENT_BINARY_OPERATOR(^, SByte);
334 IMPLEMENT_BINARY_OPERATOR(^, UShort);
335 IMPLEMENT_BINARY_OPERATOR(^, Short);
336 IMPLEMENT_BINARY_OPERATOR(^, UInt);
337 IMPLEMENT_BINARY_OPERATOR(^, Int);
338 IMPLEMENT_BINARY_OPERATOR(^, ULong);
339 IMPLEMENT_BINARY_OPERATOR(^, Long);
340 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
341 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000342 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000343 }
344 return Dest;
345}
346
347
Chris Lattner92101ac2001-08-23 17:05:04 +0000348#define IMPLEMENT_SETCC(OP, TY) \
349 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
350
Chris Lattner92101ac2001-08-23 17:05:04 +0000351static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000352 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000353 GenericValue Dest;
354 switch (Ty->getPrimitiveID()) {
355 IMPLEMENT_SETCC(==, UByte);
356 IMPLEMENT_SETCC(==, SByte);
357 IMPLEMENT_SETCC(==, UShort);
358 IMPLEMENT_SETCC(==, Short);
359 IMPLEMENT_SETCC(==, UInt);
360 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000361 IMPLEMENT_SETCC(==, ULong);
362 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000363 IMPLEMENT_SETCC(==, Float);
364 IMPLEMENT_SETCC(==, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000365 IMPLEMENT_SETCC(==, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000366 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000367 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 }
369 return Dest;
370}
371
372static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000373 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 GenericValue Dest;
375 switch (Ty->getPrimitiveID()) {
376 IMPLEMENT_SETCC(!=, UByte);
377 IMPLEMENT_SETCC(!=, SByte);
378 IMPLEMENT_SETCC(!=, UShort);
379 IMPLEMENT_SETCC(!=, Short);
380 IMPLEMENT_SETCC(!=, UInt);
381 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000382 IMPLEMENT_SETCC(!=, ULong);
383 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000384 IMPLEMENT_SETCC(!=, Float);
385 IMPLEMENT_SETCC(!=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000386 IMPLEMENT_SETCC(!=, Pointer);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000387
Chris Lattner92101ac2001-08-23 17:05:04 +0000388 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000389 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000390 }
391 return Dest;
392}
393
394static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000395 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000396 GenericValue Dest;
397 switch (Ty->getPrimitiveID()) {
398 IMPLEMENT_SETCC(<=, UByte);
399 IMPLEMENT_SETCC(<=, SByte);
400 IMPLEMENT_SETCC(<=, UShort);
401 IMPLEMENT_SETCC(<=, Short);
402 IMPLEMENT_SETCC(<=, UInt);
403 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000404 IMPLEMENT_SETCC(<=, ULong);
405 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000406 IMPLEMENT_SETCC(<=, Float);
407 IMPLEMENT_SETCC(<=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000408 IMPLEMENT_SETCC(<=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000409 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000410 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000411 }
412 return Dest;
413}
414
415static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000416 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000417 GenericValue Dest;
418 switch (Ty->getPrimitiveID()) {
419 IMPLEMENT_SETCC(>=, UByte);
420 IMPLEMENT_SETCC(>=, SByte);
421 IMPLEMENT_SETCC(>=, UShort);
422 IMPLEMENT_SETCC(>=, Short);
423 IMPLEMENT_SETCC(>=, UInt);
424 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000425 IMPLEMENT_SETCC(>=, ULong);
426 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000427 IMPLEMENT_SETCC(>=, Float);
428 IMPLEMENT_SETCC(>=, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000429 IMPLEMENT_SETCC(>=, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000430 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000431 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000432 }
433 return Dest;
434}
435
436static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000437 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 GenericValue Dest;
439 switch (Ty->getPrimitiveID()) {
440 IMPLEMENT_SETCC(<, UByte);
441 IMPLEMENT_SETCC(<, SByte);
442 IMPLEMENT_SETCC(<, UShort);
443 IMPLEMENT_SETCC(<, Short);
444 IMPLEMENT_SETCC(<, UInt);
445 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000446 IMPLEMENT_SETCC(<, ULong);
447 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 IMPLEMENT_SETCC(<, Float);
449 IMPLEMENT_SETCC(<, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000450 IMPLEMENT_SETCC(<, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000452 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 }
454 return Dest;
455}
456
457static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000458 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000459 GenericValue Dest;
460 switch (Ty->getPrimitiveID()) {
461 IMPLEMENT_SETCC(>, UByte);
462 IMPLEMENT_SETCC(>, SByte);
463 IMPLEMENT_SETCC(>, UShort);
464 IMPLEMENT_SETCC(>, Short);
465 IMPLEMENT_SETCC(>, UInt);
466 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000467 IMPLEMENT_SETCC(>, ULong);
468 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 IMPLEMENT_SETCC(>, Float);
470 IMPLEMENT_SETCC(>, Double);
Chris Lattner86660982001-08-27 05:16:50 +0000471 IMPLEMENT_SETCC(>, Pointer);
Chris Lattner92101ac2001-08-23 17:05:04 +0000472 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000473 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 }
475 return Dest;
476}
477
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000478static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
479 const Type *Ty = I.getOperand(0)->getType();
480 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
481 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000482 GenericValue R; // Result
483
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000484 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000485 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
486 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
487 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
488 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
489 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
490 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
491 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
492 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
493 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
494 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
495 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
496 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
497 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
498 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 default:
500 cout << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000501 R = Src1;
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 }
503
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000504 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000505}
506
Chris Lattner92101ac2001-08-23 17:05:04 +0000507//===----------------------------------------------------------------------===//
508// Terminator Instruction Implementations
509//===----------------------------------------------------------------------===//
510
Chris Lattnera95c6992001-11-12 16:28:48 +0000511static void PerformExitStuff() {
512#ifdef PROFILE_STRUCTURE_FIELDS
513 // Print out structure field accounting information...
514 if (!FieldAccessCounts.empty()) {
Chris Lattner84efe092001-11-12 20:13:14 +0000515 CW << "Profile Field Access Counts:\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000516 std::map<const StructType *, vector<unsigned> >::iterator
Chris Lattnera95c6992001-11-12 16:28:48 +0000517 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
518 for (; I != E; ++I) {
519 vector<unsigned> &OfC = I->second;
520 CW << " '" << (Value*)I->first << "'\t- Sum=";
521
522 unsigned Sum = 0;
523 for (unsigned i = 0; i < OfC.size(); ++i)
524 Sum += OfC[i];
525 CW << Sum << " - ";
526
527 for (unsigned i = 0; i < OfC.size(); ++i) {
528 if (i) CW << ", ";
529 CW << OfC[i];
530 }
Chris Lattner697954c2002-01-20 22:54:45 +0000531 CW << "\n";
Chris Lattnera95c6992001-11-12 16:28:48 +0000532 }
Chris Lattner697954c2002-01-20 22:54:45 +0000533 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000534
535 CW << "Profile Field Access Percentages:\n";
536 cout.precision(3);
537 for (I = FieldAccessCounts.begin(); I != E; ++I) {
538 vector<unsigned> &OfC = I->second;
539 unsigned Sum = 0;
540 for (unsigned i = 0; i < OfC.size(); ++i)
541 Sum += OfC[i];
542
543 CW << " '" << (Value*)I->first << "'\t- ";
544 for (unsigned i = 0; i < OfC.size(); ++i) {
545 if (i) CW << ", ";
546 CW << double(OfC[i])/Sum;
547 }
Chris Lattner697954c2002-01-20 22:54:45 +0000548 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000549 }
Chris Lattner697954c2002-01-20 22:54:45 +0000550 CW << "\n";
Chris Lattner84efe092001-11-12 20:13:14 +0000551
Chris Lattnera95c6992001-11-12 16:28:48 +0000552 FieldAccessCounts.clear();
553 }
554#endif
555}
556
Chris Lattnere43db882001-10-27 04:15:57 +0000557void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000558 if (!QuietMode) {
559 cout << "Program returned ";
560 print(Type::IntTy, GV);
561 cout << " via 'void exit(int)'\n";
562 }
Chris Lattnere43db882001-10-27 04:15:57 +0000563
564 ExitCode = GV.SByteVal;
565 ECStack.clear();
Chris Lattnera95c6992001-11-12 16:28:48 +0000566 PerformExitStuff();
Chris Lattnere43db882001-10-27 04:15:57 +0000567}
568
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000569void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000570 const Type *RetTy = 0;
571 GenericValue Result;
572
573 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000574 if (I.getNumOperands()) {
575 RetTy = I.getReturnValue()->getType();
576 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000577 }
578
579 // Save previously executing meth
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000580 const Function *M = ECStack.back().CurMethod;
Chris Lattner92101ac2001-08-23 17:05:04 +0000581
582 // Pop the current stack frame... this invalidates SF
583 ECStack.pop_back();
584
585 if (ECStack.empty()) { // Finished main. Put result into exit code...
586 if (RetTy) { // Nonvoid return type?
Chris Lattnerf23eb852001-12-14 16:49:29 +0000587 if (!QuietMode) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000588 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattnerf23eb852001-12-14 16:49:29 +0000589 << "\" returned ";
590 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000591 cout << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000592 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000593
594 if (RetTy->isIntegral())
Chris Lattnerf4dca802002-05-02 19:28:45 +0000595 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000596 } else {
597 ExitCode = 0;
598 }
Chris Lattnere2409062001-11-12 16:19:45 +0000599
Chris Lattnera95c6992001-11-12 16:28:48 +0000600 PerformExitStuff();
Chris Lattner92101ac2001-08-23 17:05:04 +0000601 return;
602 }
603
604 // If we have a previous stack frame, and we have a previous call, fill in
605 // the return value...
606 //
607 ExecutionContext &NewSF = ECStack.back();
608 if (NewSF.Caller) {
609 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
610 SetValue(NewSF.Caller, Result, NewSF);
611
612 NewSF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +0000613 } else if (!QuietMode) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000614 // This must be a function that is executing because of a user 'call'
615 // instruction.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000616 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +0000617 << "\" returned ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000618 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +0000619 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000620 }
621}
622
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000623void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000624 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
625 BasicBlock *Dest;
626
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000627 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
628 if (!I.isUnconditional()) {
629 Value *Cond = I.getCondition();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000630 GenericValue CondVal = getOperandValue(Cond, SF);
631 if (CondVal.BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000632 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000633 }
634 SF.CurBB = Dest; // Update CurBB to branch destination
635 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
636}
637
Chris Lattner09e93922003-04-22 20:34:47 +0000638static void executeSwitch(SwitchInst &I, ExecutionContext &SF) {
639 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
640 const Type *ElTy = I.getOperand(0)->getType();
641 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
642 BasicBlock *Dest = 0;
643
644 // Check to see if any of the cases match...
645 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) {
646 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000647 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000648 Dest = cast<BasicBlock>(I.getOperand(i+1));
649 break;
650 }
651 }
652
653 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
654 SF.CurBB = Dest; // Update CurBB to branch destination
655 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
656}
657
658
Chris Lattner92101ac2001-08-23 17:05:04 +0000659//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000660// Memory Instruction Implementations
661//===----------------------------------------------------------------------===//
662
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000663void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
664 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000665
Chris Lattnercc82cc12002-04-28 21:57:33 +0000666 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000667 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000668
669 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000670 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000671 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
672
Chris Lattnerfe11a972002-12-23 23:59:41 +0000673 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000674 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000675 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000676
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000677 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000678 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000679}
680
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000681static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
682 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
683 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000684 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000685 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000686}
687
Chris Lattner95c3af52001-10-29 19:32:19 +0000688
Chris Lattnera34c5682002-08-27 22:33:45 +0000689// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000690//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000691GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
692 User::op_iterator E,
693 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000694 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000695 "Cannot getElementOffset of a nonpointer type!");
696
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000697 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000698 const Type *Ty = Ptr->getType();
699
700 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000701 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
702 const StructLayout *SLO = TD.getStructLayout(STy);
703
704 // Indicies must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000705 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000706 assert(CPU->getType() == Type::UByteTy);
707 unsigned Index = CPU->getValue();
708
Chris Lattnere2409062001-11-12 16:19:45 +0000709#ifdef PROFILE_STRUCTURE_FIELDS
Chris Lattner782b9392001-11-26 18:18:18 +0000710 if (ProfileStructureFields) {
711 // Do accounting for this field...
712 vector<unsigned> &OfC = FieldAccessCounts[STy];
713 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
714 OfC[Index]++;
715 }
Chris Lattnere2409062001-11-12 16:19:45 +0000716#endif
Chris Lattner782b9392001-11-26 18:18:18 +0000717
718 Total += SLO->MemberOffsets[Index];
719 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000720 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattnere2409062001-11-12 16:19:45 +0000721
Chris Lattner006a4a52003-02-25 21:14:59 +0000722 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000723 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000724 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000725 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
Chris Lattnerc0fbd572002-02-11 20:19:16 +0000726 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
Chris Lattnerf23eb852001-12-14 16:49:29 +0000727 cerr << "Out of range memory access to element #" << Idx
728 << " of a " << AT->getNumElements() << " element array."
Chris Lattnera34c5682002-08-27 22:33:45 +0000729 << " Subscript #" << *I << "\n";
Chris Lattnerf23eb852001-12-14 16:49:29 +0000730 // Get outta here!!!
Chris Lattner74030252002-02-12 15:47:23 +0000731 siglongjmp(SignalRecoverBuffer, SIGTRAP);
Chris Lattnerf23eb852001-12-14 16:49:29 +0000732 }
Chris Lattner782b9392001-11-26 18:18:18 +0000733
Chris Lattnerf23eb852001-12-14 16:49:29 +0000734 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000735 unsigned Size = TD.getTypeSize(Ty);
736 Total += Size*Idx;
737 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000738 }
739
Chris Lattnera34c5682002-08-27 22:33:45 +0000740 GenericValue Result;
741 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
742 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000743}
744
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000745static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000746 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000747 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000748}
749
Chris Lattnerfe11a972002-12-23 23:59:41 +0000750void Interpreter::executeLoadInst(LoadInst &I, ExecutionContext &SF) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000751 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000752 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner86660982001-08-27 05:16:50 +0000753 GenericValue Result;
754
Chris Lattnerfddc7552002-10-15 20:34:05 +0000755 if (TD.isLittleEndian()) {
756 switch (I.getType()->getPrimitiveID()) {
757 case Type::BoolTyID:
758 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000759 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000760 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000761 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
762 ((unsigned)Ptr->Untyped[1] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000763 break;
764 case Type::FloatTyID:
765 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000766 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
767 ((unsigned)Ptr->Untyped[1] << 8) |
768 ((unsigned)Ptr->Untyped[2] << 16) |
769 ((unsigned)Ptr->Untyped[3] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000770 break;
771 case Type::DoubleTyID:
772 case Type::ULongTyID:
773 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000774 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
775 ((uint64_t)Ptr->Untyped[1] << 8) |
776 ((uint64_t)Ptr->Untyped[2] << 16) |
777 ((uint64_t)Ptr->Untyped[3] << 24) |
778 ((uint64_t)Ptr->Untyped[4] << 32) |
779 ((uint64_t)Ptr->Untyped[5] << 40) |
780 ((uint64_t)Ptr->Untyped[6] << 48) |
781 ((uint64_t)Ptr->Untyped[7] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000782 break;
783 default:
784 cout << "Cannot load value of type " << I.getType() << "!\n";
785 }
786 } else {
787 switch (I.getType()->getPrimitiveID()) {
788 case Type::BoolTyID:
789 case Type::UByteTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000790 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
Chris Lattnerfddc7552002-10-15 20:34:05 +0000791 case Type::UShortTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000792 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
793 ((unsigned)Ptr->Untyped[0] << 8);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000794 break;
795 case Type::FloatTyID:
796 case Type::UIntTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000797 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
798 ((unsigned)Ptr->Untyped[2] << 8) |
799 ((unsigned)Ptr->Untyped[1] << 16) |
800 ((unsigned)Ptr->Untyped[0] << 24);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000801 break;
802 case Type::DoubleTyID:
803 case Type::ULongTyID:
804 case Type::LongTyID:
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000805 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
806 ((uint64_t)Ptr->Untyped[6] << 8) |
807 ((uint64_t)Ptr->Untyped[5] << 16) |
808 ((uint64_t)Ptr->Untyped[4] << 24) |
809 ((uint64_t)Ptr->Untyped[3] << 32) |
810 ((uint64_t)Ptr->Untyped[2] << 40) |
811 ((uint64_t)Ptr->Untyped[1] << 48) |
812 ((uint64_t)Ptr->Untyped[0] << 56);
Chris Lattnerfddc7552002-10-15 20:34:05 +0000813 break;
814 default:
815 cout << "Cannot load value of type " << I.getType() << "!\n";
816 }
Chris Lattner86660982001-08-27 05:16:50 +0000817 }
818
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000819 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000820}
821
Chris Lattnerfe11a972002-12-23 23:59:41 +0000822void Interpreter::executeStoreInst(StoreInst &I, ExecutionContext &SF) {
Chris Lattnerfddc7552002-10-15 20:34:05 +0000823 GenericValue Val = getOperandValue(I.getOperand(0), SF);
824 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000825 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000826 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000827}
828
Chris Lattner86660982001-08-27 05:16:50 +0000829
Chris Lattnerab2dea52002-11-07 19:29:31 +0000830
Chris Lattner86660982001-08-27 05:16:50 +0000831//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000832// Miscellaneous Instruction Implementations
833//===----------------------------------------------------------------------===//
834
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000835void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
836 ECStack.back().Caller = &I;
Chris Lattner365a76e2001-09-10 04:49:44 +0000837 vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000838 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000839 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000840 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000841 // Promote all integral types whose size is < sizeof(int) into ints. We do
842 // this by zero or sign extending the value as appropriate according to the
843 // source type.
844 if (I.getOperand(i)->getType()->isIntegral() &&
845 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
846 const Type *Ty = I.getOperand(i)->getType();
847 if (Ty == Type::ShortTy)
848 ArgVals.back().IntVal = ArgVals.back().ShortVal;
849 else if (Ty == Type::UShortTy)
850 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
851 else if (Ty == Type::SByteTy)
852 ArgVals.back().IntVal = ArgVals.back().SByteVal;
853 else if (Ty == Type::UByteTy)
854 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
855 else if (Ty == Type::BoolTy)
856 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
857 else
858 assert(0 && "Unknown type!");
859 }
860 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000861
Chris Lattner070cf5e2001-11-07 20:12:30 +0000862 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000863 // and treat it as a function pointer.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000864 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattner070cf5e2001-11-07 20:12:30 +0000865
Chris Lattnerfe11a972002-12-23 23:59:41 +0000866 callMethod((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000867}
868
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000869static void executePHINode(PHINode &I, ExecutionContext &SF) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000870 BasicBlock *PrevBB = SF.PrevBB;
871 Value *IncomingValue = 0;
872
873 // Search for the value corresponding to this previous bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000874 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
875 if (I.getIncomingBlock(--i) == PrevBB) {
876 IncomingValue = I.getIncomingValue(i);
Chris Lattner92101ac2001-08-23 17:05:04 +0000877 break;
878 }
879 }
880 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
881
882 // Found the value, set as the result...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000883 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000884}
885
Chris Lattner86660982001-08-27 05:16:50 +0000886#define IMPLEMENT_SHIFT(OP, TY) \
887 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
888
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000889static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
890 const Type *Ty = I.getOperand(0)->getType();
891 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
892 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000893 GenericValue Dest;
894
895 switch (Ty->getPrimitiveID()) {
896 IMPLEMENT_SHIFT(<<, UByte);
897 IMPLEMENT_SHIFT(<<, SByte);
898 IMPLEMENT_SHIFT(<<, UShort);
899 IMPLEMENT_SHIFT(<<, Short);
900 IMPLEMENT_SHIFT(<<, UInt);
901 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000902 IMPLEMENT_SHIFT(<<, ULong);
903 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000904 IMPLEMENT_SHIFT(<<, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000905 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000906 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000907 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000908 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000909}
910
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000911static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
912 const Type *Ty = I.getOperand(0)->getType();
913 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
914 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000915 GenericValue Dest;
916
917 switch (Ty->getPrimitiveID()) {
918 IMPLEMENT_SHIFT(>>, UByte);
919 IMPLEMENT_SHIFT(>>, SByte);
920 IMPLEMENT_SHIFT(>>, UShort);
921 IMPLEMENT_SHIFT(>>, Short);
922 IMPLEMENT_SHIFT(>>, UInt);
923 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000924 IMPLEMENT_SHIFT(>>, ULong);
925 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner743cd3e2002-07-09 18:42:36 +0000926 IMPLEMENT_SHIFT(>>, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000927 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000928 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000929 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000930 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000931}
932
933#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000934 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000935
936#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
937 case Type::DESTTY##TyID: \
938 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000939 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000940 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
942 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
943 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
944 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000945 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
946 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000947 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
948 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000949
950#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
951 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
952 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
953
954#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner697954c2002-01-20 22:54:45 +0000955 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
Chris Lattner86660982001-08-27 05:16:50 +0000956 break; \
957 } \
958 break
959
960#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
961 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
962 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000963 IMPLEMENT_CAST_CASE_END()
964
Chris Lattnera34c5682002-08-27 22:33:45 +0000965static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
966 ExecutionContext &SF) {
967 const Type *SrcTy = SrcVal->getType();
968 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000969
970 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000971 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
972 IMPLEMENT_CAST_CASE(SByte , ( signed char));
973 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000974 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000975 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
976 IMPLEMENT_CAST_CASE(Int , ( signed int ));
977 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
978 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000979 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000980 IMPLEMENT_CAST_CASE(Float , (float));
981 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner86660982001-08-27 05:16:50 +0000982 default:
Chris Lattner697954c2002-01-20 22:54:45 +0000983 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000984 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000985
986 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000987}
Chris Lattner92101ac2001-08-23 17:05:04 +0000988
989
Chris Lattnera34c5682002-08-27 22:33:45 +0000990static void executeCastInst(CastInst &I, ExecutionContext &SF) {
991 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
992}
Chris Lattner92101ac2001-08-23 17:05:04 +0000993
994
995//===----------------------------------------------------------------------===//
996// Dispatch and Execution Code
997//===----------------------------------------------------------------------===//
998
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000999MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001000 // Assign slot numbers to the function arguments...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001001 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1002 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001003
1004 // Iterate over all of the instructions...
1005 unsigned InstNum = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001006 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1007 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1008 // For each instruction... Add Annote
1009 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
Chris Lattner92101ac2001-08-23 17:05:04 +00001010}
1011
1012unsigned MethodInfo::getValueSlot(const Value *V) {
1013 unsigned Plane = V->getType()->getUniqueID();
1014 if (Plane >= NumPlaneElements.size())
1015 NumPlaneElements.resize(Plane+1, 0);
1016 return NumPlaneElements[Plane]++;
1017}
1018
1019
Chris Lattner92101ac2001-08-23 17:05:04 +00001020//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001021// callMethod - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001022//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001023void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +00001024 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1025 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1026 "Incorrect number of arguments passed into function call!");
Chris Lattner92101ac2001-08-23 17:05:04 +00001027 if (M->isExternal()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001028 GenericValue Result = callExternalMethod(M, ArgVals);
1029 const Type *RetTy = M->getReturnType();
1030
1031 // Copy the result back into the result variable if we are not returning
1032 // void.
1033 if (RetTy != Type::VoidTy) {
1034 if (!ECStack.empty() && ECStack.back().Caller) {
1035 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +00001036 SetValue(SF.Caller, Result, SF);
1037
1038 SF.Caller = 0; // We returned from the call...
Chris Lattnerf23eb852001-12-14 16:49:29 +00001039 } else if (!QuietMode) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001040 // print it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001041 CW << "Function " << M->getType() << " \"" << M->getName()
Chris Lattner5af0c482001-11-07 04:23:00 +00001042 << "\" returned ";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001043 print(RetTy, Result);
Chris Lattner697954c2002-01-20 22:54:45 +00001044 cout << "\n";
Chris Lattnerbb76f022001-10-30 20:27:31 +00001045
1046 if (RetTy->isIntegral())
Chris Lattner0c4e8862002-09-03 01:08:28 +00001047 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattnerbb76f022001-10-30 20:27:31 +00001048 }
1049 }
1050
Chris Lattner92101ac2001-08-23 17:05:04 +00001051 return;
1052 }
1053
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001054 // Process the function, assigning instruction numbers to the instructions in
1055 // the function. Also calculate the number of values for each type slot
1056 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +00001057 //
1058 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001059 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
Chris Lattner86660982001-08-27 05:16:50 +00001060
Chris Lattner92101ac2001-08-23 17:05:04 +00001061 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1062 StackFrame.CurMethod = M;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001063 StackFrame.CurBB = M->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001064 StackFrame.CurInst = StackFrame.CurBB->begin();
1065 StackFrame.MethInfo = MethInfo;
1066
1067 // Initialize the values to nothing...
1068 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001069 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001070 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1071
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001072 // Taint the initial values of stuff
1073 memset(&StackFrame.Values[i][0], 42,
1074 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1075 }
1076
Chris Lattner92101ac2001-08-23 17:05:04 +00001077 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1078
Chris Lattner92101ac2001-08-23 17:05:04 +00001079
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001080 // Run through the function arguments and initialize their values...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001081 assert(ArgVals.size() == M->asize() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001082 "Invalid number of values passed to function invocation!");
Chris Lattner365a76e2001-09-10 04:49:44 +00001083 unsigned i = 0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001084 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1085 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattner92101ac2001-08-23 17:05:04 +00001086}
1087
1088// executeInstruction - Interpret a single instruction, increment the "PC", and
1089// return true if the next instruction is a breakpoint...
1090//
1091bool Interpreter::executeInstruction() {
1092 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1093
1094 ExecutionContext &SF = ECStack.back(); // Current stack frame
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001095 Instruction &I = *SF.CurInst++; // Increment before execute
Chris Lattner92101ac2001-08-23 17:05:04 +00001096
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001097 if (Trace)
Chris Lattner5af0c482001-11-07 04:23:00 +00001098 CW << "Run:" << I;
1099
Chris Lattnerbbdabce2002-12-08 05:51:08 +00001100 // Track the number of dynamic instructions executed.
1101 ++NumDynamicInsts;
1102
Chris Lattner5af0c482001-11-07 04:23:00 +00001103 // Set a sigsetjmp buffer so that we can recover if an error happens during
1104 // instruction execution...
1105 //
1106 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1107 --SF.CurInst; // Back up to erroring instruction
Chris Lattner74030252002-02-12 15:47:23 +00001108 if (SigNo != SIGINT) {
Chris Lattner8b77be22002-09-13 14:41:38 +00001109 cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
Chris Lattner461f02f2001-11-07 05:31:27 +00001110 printStackTrace();
Chris Lattner74030252002-02-12 15:47:23 +00001111 // If -abort-on-exception was specified, terminate LLI instead of trying
1112 // to debug it.
1113 //
1114 if (AbortOnExceptions) exit(1);
Chris Lattner782b9392001-11-26 18:18:18 +00001115 } else if (SigNo == SIGINT) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001116 cout << "CTRL-C Detected, execution halted.\n";
1117 }
1118 InInstruction = false;
Chris Lattner5af0c482001-11-07 04:23:00 +00001119 return true;
1120 }
Chris Lattner43e3f7c2001-10-27 08:43:52 +00001121
Chris Lattner461f02f2001-11-07 05:31:27 +00001122 InInstruction = true;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001123 if (I.isBinaryOp()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +00001124 executeBinaryInst(cast<BinaryOperator>(I), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +00001125 } else {
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001126 switch (I.getOpcode()) {
Chris Lattner86660982001-08-27 05:16:50 +00001127 // Terminators
Chris Lattnerbb76f022001-10-30 20:27:31 +00001128 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1129 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
Chris Lattner09e93922003-04-22 20:34:47 +00001130 case Instruction::Switch: executeSwitch (cast<SwitchInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001131 // Memory Instructions
1132 case Instruction::Alloca:
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001133 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
Chris Lattnerbb76f022001-10-30 20:27:31 +00001134 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1135 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1136 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
Chris Lattner95c3af52001-10-29 19:32:19 +00001137 case Instruction::GetElementPtr:
1138 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
Chris Lattner86660982001-08-27 05:16:50 +00001139
1140 // Miscellaneous Instructions
Chris Lattnerbb76f022001-10-30 20:27:31 +00001141 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1142 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1143 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1144 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1145 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001146 default:
1147 cout << "Don't know how to execute this instruction!\n-->" << I;
1148 }
1149 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001150 InInstruction = false;
Chris Lattner92101ac2001-08-23 17:05:04 +00001151
1152 // Reset the current frame location to the top of stack
1153 CurFrame = ECStack.size()-1;
1154
1155 if (CurFrame == -1) return false; // No breakpoint if no code
1156
1157 // Return true if there is a breakpoint annotation on the instruction...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001158 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
Chris Lattner92101ac2001-08-23 17:05:04 +00001159}
1160
1161void Interpreter::stepInstruction() { // Do the 'step' command
1162 if (ECStack.empty()) {
1163 cout << "Error: no program running, cannot step!\n";
1164 return;
1165 }
1166
1167 // Run an instruction...
1168 executeInstruction();
1169
1170 // Print the next instruction to execute...
1171 printCurrentInstruction();
1172}
1173
1174// --- UI Stuff...
Chris Lattner92101ac2001-08-23 17:05:04 +00001175void Interpreter::nextInstruction() { // Do the 'next' command
1176 if (ECStack.empty()) {
1177 cout << "Error: no program running, cannot 'next'!\n";
1178 return;
1179 }
1180
1181 // If this is a call instruction, step over the call instruction...
1182 // TODO: ICALL, CALL WITH, ...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001183 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
Chris Lattnera74a6b52001-10-29 14:08:33 +00001184 unsigned StackSize = ECStack.size();
Chris Lattner92101ac2001-08-23 17:05:04 +00001185 // Step into the function...
1186 if (executeInstruction()) {
1187 // Hit a breakpoint, print current instruction, then return to user...
1188 cout << "Breakpoint hit!\n";
1189 printCurrentInstruction();
1190 return;
1191 }
1192
Chris Lattnera74a6b52001-10-29 14:08:33 +00001193 // If we we able to step into the function, finish it now. We might not be
1194 // able the step into a function, if it's external for example.
1195 if (ECStack.size() != StackSize)
1196 finish(); // Finish executing the function...
Chris Lattner069aa252001-10-29 16:05:19 +00001197 else
1198 printCurrentInstruction();
Chris Lattnera74a6b52001-10-29 14:08:33 +00001199
Chris Lattner92101ac2001-08-23 17:05:04 +00001200 } else {
1201 // Normal instruction, just step...
1202 stepInstruction();
1203 }
1204}
1205
1206void Interpreter::run() {
1207 if (ECStack.empty()) {
1208 cout << "Error: no program running, cannot run!\n";
1209 return;
1210 }
1211
1212 bool HitBreakpoint = false;
1213 while (!ECStack.empty() && !HitBreakpoint) {
1214 // Run an instruction...
1215 HitBreakpoint = executeInstruction();
1216 }
1217
1218 if (HitBreakpoint) {
1219 cout << "Breakpoint hit!\n";
1220 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001221 // Print the next instruction to execute...
1222 printCurrentInstruction();
1223}
1224
1225void Interpreter::finish() {
1226 if (ECStack.empty()) {
1227 cout << "Error: no program running, cannot run!\n";
1228 return;
1229 }
1230
1231 unsigned StackSize = ECStack.size();
1232 bool HitBreakpoint = false;
1233 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1234 // Run an instruction...
1235 HitBreakpoint = executeInstruction();
1236 }
1237
1238 if (HitBreakpoint) {
1239 cout << "Breakpoint hit!\n";
1240 }
1241
1242 // Print the next instruction to execute...
1243 printCurrentInstruction();
1244}
1245
1246
1247
1248// printCurrentInstruction - Print out the instruction that the virtual PC is
1249// at, or fail silently if no program is running.
1250//
1251void Interpreter::printCurrentInstruction() {
1252 if (!ECStack.empty()) {
Chris Lattnerf5b2ec12001-10-29 20:44:34 +00001253 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1254 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1255
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001256 Instruction &I = *ECStack.back().CurInst;
1257 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
Chris Lattner92101ac2001-08-23 17:05:04 +00001258 assert(IN && "Instruction has no numbering annotation!");
1259 cout << "#" << IN->InstNum << I;
1260 }
1261}
1262
1263void Interpreter::printValue(const Type *Ty, GenericValue V) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001264 switch (Ty->getPrimitiveID()) {
1265 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
Chris Lattner65629d52002-08-13 20:45:11 +00001266 case Type::SByteTyID:
1267 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1268 case Type::UByteTyID:
1269 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001270 case Type::ShortTyID: cout << V.ShortVal; break;
1271 case Type::UShortTyID: cout << V.UShortVal; break;
1272 case Type::IntTyID: cout << V.IntVal; break;
1273 case Type::UIntTyID: cout << V.UIntVal; break;
Chris Lattner697954c2002-01-20 22:54:45 +00001274 case Type::LongTyID: cout << (long)V.LongVal; break;
1275 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001276 case Type::FloatTyID: cout << V.FloatVal; break;
1277 case Type::DoubleTyID: cout << V.DoubleVal; break;
Chris Lattnerfe11a972002-12-23 23:59:41 +00001278 case Type::PointerTyID:cout << (void*)GVTOP(V); break;
Chris Lattner92101ac2001-08-23 17:05:04 +00001279 default:
1280 cout << "- Don't know how to print value of this type!";
1281 break;
1282 }
1283}
1284
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001285void Interpreter::print(const Type *Ty, GenericValue V) {
Chris Lattner5af0c482001-11-07 04:23:00 +00001286 CW << Ty << " ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001287 printValue(Ty, V);
1288}
1289
Chris Lattner697954c2002-01-20 22:54:45 +00001290void Interpreter::print(const std::string &Name) {
Chris Lattner92101ac2001-08-23 17:05:04 +00001291 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1292 if (!PickedVal) return;
1293
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001294 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1295 CW << F; // Print the function
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001296 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001297 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001298 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1299 CW << BB; // Print the basic block
Chris Lattner92101ac2001-08-23 17:05:04 +00001300 } else { // Otherwise there should be an annotation for the slot#
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001301 print(PickedVal->getType(),
1302 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001303 cout << "\n";
Chris Lattner92101ac2001-08-23 17:05:04 +00001304 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001305}
1306
Chris Lattner697954c2002-01-20 22:54:45 +00001307void Interpreter::infoValue(const std::string &Name) {
Chris Lattner86660982001-08-27 05:16:50 +00001308 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1309 if (!PickedVal) return;
1310
1311 cout << "Value: ";
Chris Lattner2e42d3a2001-10-15 05:51:48 +00001312 print(PickedVal->getType(),
1313 getOperandValue(PickedVal, ECStack[CurFrame]));
Chris Lattner697954c2002-01-20 22:54:45 +00001314 cout << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001315 printOperandInfo(PickedVal, ECStack[CurFrame]);
1316}
1317
Chris Lattner461f02f2001-11-07 05:31:27 +00001318// printStackFrame - Print information about the specified stack frame, or -1
1319// for the default one.
1320//
Chris Lattner601d7152002-07-25 17:37:05 +00001321void Interpreter::printStackFrame(int FrameNo) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001322 if (FrameNo == -1) FrameNo = CurFrame;
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001323 Function *F = ECStack[FrameNo].CurMethod;
1324 const Type *RetTy = F->getReturnType();
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001325
1326 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001327 << (Value*)RetTy << " \"" << F->getName() << "\"(";
Chris Lattner461f02f2001-11-07 05:31:27 +00001328
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001329 unsigned i = 0;
1330 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
Chris Lattner461f02f2001-11-07 05:31:27 +00001331 if (i != 0) cout << ", ";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001332 CW << *I << "=";
Chris Lattner461f02f2001-11-07 05:31:27 +00001333
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001334 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
Chris Lattner92101ac2001-08-23 17:05:04 +00001335 }
Chris Lattner461f02f2001-11-07 05:31:27 +00001336
Chris Lattner697954c2002-01-20 22:54:45 +00001337 cout << ")\n";
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001338
1339 if (FrameNo != int(ECStack.size()-1)) {
1340 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1341 CW << --I;
1342 } else {
1343 CW << *ECStack[FrameNo].CurInst;
1344 }
Chris Lattner92101ac2001-08-23 17:05:04 +00001345}
Chris Lattner461f02f2001-11-07 05:31:27 +00001346