blob: bec4b7b9056b8bb3afaf8ee436b826ad92523bb5 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner73011782003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
19#include "llvm/IntrinsicLowering.h"
20#include "llvm/Intrinsics.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000022#include "Support/Statistic.h"
Brian Gaeke63438cc2003-12-11 00:22:59 +000023#include "Support/Debug.h"
Brian Gaeke235b2002003-10-10 17:03:22 +000024#include <cmath> // For fmod
Chris Lattner4af6de82003-11-25 20:44:56 +000025using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000026
Chris Lattnerbbdabce2002-12-08 05:51:08 +000027namespace {
28 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattnerbbdabce2002-12-08 05:51:08 +000029
Chris Lattner4af6de82003-11-25 20:44:56 +000030 Interpreter *TheEE = 0;
31}
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner73011782003-12-28 09:44:37 +000033
Chris Lattner2e42d3a2001-10-15 05:51:48 +000034//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000035// Value Manipulation code
36//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000037
Chris Lattnera34c5682002-08-27 22:33:45 +000038static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000039 const Type *Ty);
Brian Gaeke63438cc2003-12-11 00:22:59 +000040static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
41 const Type *Ty);
42static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
43 const Type *Ty);
44static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
45 const Type *Ty);
46static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
47 const Type *Ty);
48static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
49 const Type *Ty);
50static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
51 const Type *Ty);
52static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
53 const Type *Ty);
54static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
55 const Type *Ty);
56static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
57 const Type *Ty);
58static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
59 const Type *Ty);
60static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
61 const Type *Ty);
62static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
63 const Type *Ty);
64static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
65 const Type *Ty);
66static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
67 const Type *Ty);
68static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
69 const Type *Ty);
70
71GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
72 ExecutionContext &SF) {
73 switch (CE->getOpcode()) {
74 case Instruction::Cast:
75 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
76 case Instruction::GetElementPtr:
77 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
78 gep_type_end(CE), SF);
79 case Instruction::Add:
80 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
81 getOperandValue(CE->getOperand(1), SF),
82 CE->getOperand(0)->getType());
83 case Instruction::Sub:
84 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
85 getOperandValue(CE->getOperand(1), SF),
86 CE->getOperand(0)->getType());
87 case Instruction::Mul:
88 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
89 getOperandValue(CE->getOperand(1), SF),
90 CE->getOperand(0)->getType());
91 case Instruction::Div:
92 return executeDivInst(getOperandValue(CE->getOperand(0), SF),
93 getOperandValue(CE->getOperand(1), SF),
94 CE->getOperand(0)->getType());
95 case Instruction::Rem:
96 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
97 getOperandValue(CE->getOperand(1), SF),
98 CE->getOperand(0)->getType());
99 case Instruction::And:
100 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
101 getOperandValue(CE->getOperand(1), SF),
102 CE->getOperand(0)->getType());
103 case Instruction::Or:
104 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
105 getOperandValue(CE->getOperand(1), SF),
106 CE->getOperand(0)->getType());
107 case Instruction::Xor:
108 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
109 getOperandValue(CE->getOperand(1), SF),
110 CE->getOperand(0)->getType());
111 case Instruction::SetEQ:
112 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
113 getOperandValue(CE->getOperand(1), SF),
114 CE->getOperand(0)->getType());
115 case Instruction::SetNE:
116 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
117 getOperandValue(CE->getOperand(1), SF),
118 CE->getOperand(0)->getType());
119 case Instruction::SetLE:
120 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
121 getOperandValue(CE->getOperand(1), SF),
122 CE->getOperand(0)->getType());
123 case Instruction::SetGE:
124 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
125 getOperandValue(CE->getOperand(1), SF),
126 CE->getOperand(0)->getType());
127 case Instruction::SetLT:
128 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
129 getOperandValue(CE->getOperand(1), SF),
130 CE->getOperand(0)->getType());
131 case Instruction::SetGT:
132 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
133 getOperandValue(CE->getOperand(1), SF),
134 CE->getOperand(0)->getType());
135 case Instruction::Shl:
136 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
137 getOperandValue(CE->getOperand(1), SF),
138 CE->getOperand(0)->getType());
139 case Instruction::Shr:
140 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
141 getOperandValue(CE->getOperand(1), SF),
142 CE->getOperand(0)->getType());
143
144 default:
145 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
146 abort();
147 return GenericValue();
148 }
149}
Chris Lattnera34c5682002-08-27 22:33:45 +0000150
Brian Gaeke29794cb2003-09-05 18:55:03 +0000151GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000152 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000153 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000154 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000155 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000156 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000157 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000158 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000159 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000160 }
161}
162
Chris Lattner39bb5b42001-10-15 13:25:40 +0000163static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000164 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000165}
166
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000167void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000168 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000169}
170
Chris Lattner2adcd832002-05-03 19:52:30 +0000171//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000172// Binary Instruction Implementations
173//===----------------------------------------------------------------------===//
174
175#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
176 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
177
178static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000179 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000180 GenericValue Dest;
181 switch (Ty->getPrimitiveID()) {
182 IMPLEMENT_BINARY_OPERATOR(+, UByte);
183 IMPLEMENT_BINARY_OPERATOR(+, SByte);
184 IMPLEMENT_BINARY_OPERATOR(+, UShort);
185 IMPLEMENT_BINARY_OPERATOR(+, Short);
186 IMPLEMENT_BINARY_OPERATOR(+, UInt);
187 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000188 IMPLEMENT_BINARY_OPERATOR(+, ULong);
189 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000190 IMPLEMENT_BINARY_OPERATOR(+, Float);
191 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000192 default:
Chris Lattner02868352003-04-22 21:22:33 +0000193 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
194 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000195 }
196 return Dest;
197}
198
199static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000200 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000201 GenericValue Dest;
202 switch (Ty->getPrimitiveID()) {
203 IMPLEMENT_BINARY_OPERATOR(-, UByte);
204 IMPLEMENT_BINARY_OPERATOR(-, SByte);
205 IMPLEMENT_BINARY_OPERATOR(-, UShort);
206 IMPLEMENT_BINARY_OPERATOR(-, Short);
207 IMPLEMENT_BINARY_OPERATOR(-, UInt);
208 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000209 IMPLEMENT_BINARY_OPERATOR(-, ULong);
210 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000211 IMPLEMENT_BINARY_OPERATOR(-, Float);
212 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000213 default:
Chris Lattner02868352003-04-22 21:22:33 +0000214 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
215 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000216 }
217 return Dest;
218}
219
Chris Lattnerc2593162001-10-27 08:28:11 +0000220static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000221 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000222 GenericValue Dest;
223 switch (Ty->getPrimitiveID()) {
224 IMPLEMENT_BINARY_OPERATOR(*, UByte);
225 IMPLEMENT_BINARY_OPERATOR(*, SByte);
226 IMPLEMENT_BINARY_OPERATOR(*, UShort);
227 IMPLEMENT_BINARY_OPERATOR(*, Short);
228 IMPLEMENT_BINARY_OPERATOR(*, UInt);
229 IMPLEMENT_BINARY_OPERATOR(*, Int);
230 IMPLEMENT_BINARY_OPERATOR(*, ULong);
231 IMPLEMENT_BINARY_OPERATOR(*, Long);
232 IMPLEMENT_BINARY_OPERATOR(*, Float);
233 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000234 default:
Chris Lattner02868352003-04-22 21:22:33 +0000235 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
236 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000237 }
238 return Dest;
239}
240
241static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000242 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000243 GenericValue Dest;
244 switch (Ty->getPrimitiveID()) {
245 IMPLEMENT_BINARY_OPERATOR(/, UByte);
246 IMPLEMENT_BINARY_OPERATOR(/, SByte);
247 IMPLEMENT_BINARY_OPERATOR(/, UShort);
248 IMPLEMENT_BINARY_OPERATOR(/, Short);
249 IMPLEMENT_BINARY_OPERATOR(/, UInt);
250 IMPLEMENT_BINARY_OPERATOR(/, Int);
251 IMPLEMENT_BINARY_OPERATOR(/, ULong);
252 IMPLEMENT_BINARY_OPERATOR(/, Long);
253 IMPLEMENT_BINARY_OPERATOR(/, Float);
254 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000255 default:
Chris Lattner02868352003-04-22 21:22:33 +0000256 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
257 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000258 }
259 return Dest;
260}
261
262static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000263 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000264 GenericValue Dest;
265 switch (Ty->getPrimitiveID()) {
266 IMPLEMENT_BINARY_OPERATOR(%, UByte);
267 IMPLEMENT_BINARY_OPERATOR(%, SByte);
268 IMPLEMENT_BINARY_OPERATOR(%, UShort);
269 IMPLEMENT_BINARY_OPERATOR(%, Short);
270 IMPLEMENT_BINARY_OPERATOR(%, UInt);
271 IMPLEMENT_BINARY_OPERATOR(%, Int);
272 IMPLEMENT_BINARY_OPERATOR(%, ULong);
273 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000274 case Type::FloatTyID:
275 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
276 break;
277 case Type::DoubleTyID:
278 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
279 break;
280 default:
Chris Lattner02868352003-04-22 21:22:33 +0000281 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
282 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000283 }
284 return Dest;
285}
286
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000287static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000288 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000289 GenericValue Dest;
290 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000291 IMPLEMENT_BINARY_OPERATOR(&, Bool);
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);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000300 default:
Chris Lattner02868352003-04-22 21:22:33 +0000301 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
302 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000303 }
304 return Dest;
305}
306
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000307static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000308 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000309 GenericValue Dest;
310 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000311 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000312 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);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000320 default:
Chris Lattner02868352003-04-22 21:22:33 +0000321 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
322 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000323 }
324 return Dest;
325}
326
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000327static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000328 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000329 GenericValue Dest;
330 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000331 IMPLEMENT_BINARY_OPERATOR(^, Bool);
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);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000340 default:
Chris Lattner02868352003-04-22 21:22:33 +0000341 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
342 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000343 }
344 return Dest;
345}
346
Chris Lattner92101ac2001-08-23 17:05:04 +0000347#define IMPLEMENT_SETCC(OP, TY) \
348 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
349
Chris Lattnerfd506f52003-04-23 19:55:35 +0000350// Handle pointers specially because they must be compared with only as much
351// width as the host has. We _do not_ want to be comparing 64 bit values when
352// running on a 32-bit target, otherwise the upper 32 bits might mess up
353// comparisons if they contain garbage.
354#define IMPLEMENT_POINTERSETCC(OP) \
355 case Type::PointerTyID: \
356 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
357 (void*)(intptr_t)Src2.PointerVal; break
358
Chris Lattner92101ac2001-08-23 17:05:04 +0000359static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000360 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000361 GenericValue Dest;
362 switch (Ty->getPrimitiveID()) {
363 IMPLEMENT_SETCC(==, UByte);
364 IMPLEMENT_SETCC(==, SByte);
365 IMPLEMENT_SETCC(==, UShort);
366 IMPLEMENT_SETCC(==, Short);
367 IMPLEMENT_SETCC(==, UInt);
368 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000369 IMPLEMENT_SETCC(==, ULong);
370 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000371 IMPLEMENT_SETCC(==, Float);
372 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000373 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 default:
Chris Lattner02868352003-04-22 21:22:33 +0000375 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
376 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000377 }
378 return Dest;
379}
380
381static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000382 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000383 GenericValue Dest;
384 switch (Ty->getPrimitiveID()) {
385 IMPLEMENT_SETCC(!=, UByte);
386 IMPLEMENT_SETCC(!=, SByte);
387 IMPLEMENT_SETCC(!=, UShort);
388 IMPLEMENT_SETCC(!=, Short);
389 IMPLEMENT_SETCC(!=, UInt);
390 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000391 IMPLEMENT_SETCC(!=, ULong);
392 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000393 IMPLEMENT_SETCC(!=, Float);
394 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000395 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000396
Chris Lattner92101ac2001-08-23 17:05:04 +0000397 default:
Chris Lattner02868352003-04-22 21:22:33 +0000398 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
399 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000400 }
401 return Dest;
402}
403
404static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000405 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000406 GenericValue Dest;
407 switch (Ty->getPrimitiveID()) {
408 IMPLEMENT_SETCC(<=, UByte);
409 IMPLEMENT_SETCC(<=, SByte);
410 IMPLEMENT_SETCC(<=, UShort);
411 IMPLEMENT_SETCC(<=, Short);
412 IMPLEMENT_SETCC(<=, UInt);
413 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000414 IMPLEMENT_SETCC(<=, ULong);
415 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000416 IMPLEMENT_SETCC(<=, Float);
417 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000418 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000419 default:
Chris Lattner02868352003-04-22 21:22:33 +0000420 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
421 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000422 }
423 return Dest;
424}
425
426static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000427 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000428 GenericValue Dest;
429 switch (Ty->getPrimitiveID()) {
430 IMPLEMENT_SETCC(>=, UByte);
431 IMPLEMENT_SETCC(>=, SByte);
432 IMPLEMENT_SETCC(>=, UShort);
433 IMPLEMENT_SETCC(>=, Short);
434 IMPLEMENT_SETCC(>=, UInt);
435 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000436 IMPLEMENT_SETCC(>=, ULong);
437 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 IMPLEMENT_SETCC(>=, Float);
439 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000440 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000441 default:
Chris Lattner02868352003-04-22 21:22:33 +0000442 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
443 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000444 }
445 return Dest;
446}
447
448static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000449 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000450 GenericValue Dest;
451 switch (Ty->getPrimitiveID()) {
452 IMPLEMENT_SETCC(<, UByte);
453 IMPLEMENT_SETCC(<, SByte);
454 IMPLEMENT_SETCC(<, UShort);
455 IMPLEMENT_SETCC(<, Short);
456 IMPLEMENT_SETCC(<, UInt);
457 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000458 IMPLEMENT_SETCC(<, ULong);
459 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 IMPLEMENT_SETCC(<, Float);
461 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000462 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000463 default:
Chris Lattner02868352003-04-22 21:22:33 +0000464 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
465 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000466 }
467 return Dest;
468}
469
470static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000471 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000472 GenericValue Dest;
473 switch (Ty->getPrimitiveID()) {
474 IMPLEMENT_SETCC(>, UByte);
475 IMPLEMENT_SETCC(>, SByte);
476 IMPLEMENT_SETCC(>, UShort);
477 IMPLEMENT_SETCC(>, Short);
478 IMPLEMENT_SETCC(>, UInt);
479 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000480 IMPLEMENT_SETCC(>, ULong);
481 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000482 IMPLEMENT_SETCC(>, Float);
483 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000484 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 default:
Chris Lattner02868352003-04-22 21:22:33 +0000486 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
487 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000488 }
489 return Dest;
490}
491
Chris Lattnerd7916e92003-05-10 21:22:39 +0000492void Interpreter::visitBinaryOperator(BinaryOperator &I) {
493 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000494 const Type *Ty = I.getOperand(0)->getType();
495 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
496 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 GenericValue R; // Result
498
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000499 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000500 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
501 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
502 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
503 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
504 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
505 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
506 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
507 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
508 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
509 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
510 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
511 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
512 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
513 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000514 default:
Chris Lattner02868352003-04-22 21:22:33 +0000515 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
516 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 }
518
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000519 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000520}
521
Chris Lattner92101ac2001-08-23 17:05:04 +0000522//===----------------------------------------------------------------------===//
523// Terminator Instruction Implementations
524//===----------------------------------------------------------------------===//
525
Chris Lattnere43db882001-10-27 04:15:57 +0000526void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000527 runAtExitHandlers ();
528 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000529}
530
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000531/// Pop the last stack frame off of ECStack and then copy the result
532/// back into the result variable if we are not returning void. The
533/// result variable may be the ExitCode, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000534/// CallInst if there was a previous stack frame. This method may
535/// invalidate any ECStack iterators you have. This method also takes
536/// care of switching to the normal destination BB, if we are returning
537/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000538///
539void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
540 GenericValue Result) {
541 // Pop the current stack frame.
542 ECStack.pop_back();
543
544 if (ECStack.empty()) { // Finished main. Put result into exit code...
545 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
546 ExitCode = Result.IntVal; // Capture the exit code of the program
547 } else {
548 ExitCode = 0;
549 }
550 } else {
551 // If we have a previous stack frame, and we have a previous call,
552 // fill in the return value...
553 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000554 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000555 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000556 SetValue(I, Result, CallingSF);
557 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
558 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000559 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000560 }
561 }
562}
563
Chris Lattnerd7916e92003-05-10 21:22:39 +0000564void Interpreter::visitReturnInst(ReturnInst &I) {
565 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000566 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000567 GenericValue Result;
568
569 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000570 if (I.getNumOperands()) {
571 RetTy = I.getReturnValue()->getType();
572 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000573 }
574
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000575 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000576}
577
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000578void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000579 // Unwind stack
580 Instruction *Inst;
581 do {
582 ECStack.pop_back ();
583 if (ECStack.empty ())
584 abort ();
585 Inst = ECStack.back ().Caller.getInstruction ();
586 } while (!(Inst && isa<InvokeInst> (Inst)));
587
588 // Return from invoke
589 ExecutionContext &InvokingSF = ECStack.back ();
590 InvokingSF.Caller = CallSite ();
591
592 // Go to exceptional destination BB of invoke instruction
593 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
594 InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000595}
596
Chris Lattnerd7916e92003-05-10 21:22:39 +0000597void Interpreter::visitBranchInst(BranchInst &I) {
598 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000599 BasicBlock *Dest;
600
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000601 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
602 if (!I.isUnconditional()) {
603 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000604 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000605 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000606 }
Chris Lattner77113b62003-05-10 20:21:16 +0000607 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000608}
609
Chris Lattnerd7916e92003-05-10 21:22:39 +0000610void Interpreter::visitSwitchInst(SwitchInst &I) {
611 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000612 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
613 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000614
615 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000616 BasicBlock *Dest = 0;
617 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000618 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000619 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000620 Dest = cast<BasicBlock>(I.getOperand(i+1));
621 break;
622 }
Chris Lattner09e93922003-04-22 20:34:47 +0000623
624 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000625 SwitchToNewBasicBlock(Dest, SF);
626}
627
628// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
629// This function handles the actual updating of block and instruction iterators
630// as well as execution of all of the PHI nodes in the destination block.
631//
632// This method does this because all of the PHI nodes must be executed
633// atomically, reading their inputs before any of the results are updated. Not
634// doing this can cause problems if the PHI nodes depend on other PHI nodes for
635// their inputs. If the input PHI node is updated before it is read, incorrect
636// results can happen. Thus we use a two phase approach.
637//
638void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
639 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
640 SF.CurBB = Dest; // Update CurBB to branch destination
641 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
642
643 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
644
645 // Loop over all of the PHI nodes in the current block, reading their inputs.
646 std::vector<GenericValue> ResultValues;
647
648 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
649 // Search for the value corresponding to this previous bb...
650 int i = PN->getBasicBlockIndex(PrevBB);
651 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
652 Value *IncomingValue = PN->getIncomingValue(i);
653
654 // Save the incoming value for this PHI node...
655 ResultValues.push_back(getOperandValue(IncomingValue, SF));
656 }
657
658 // Now loop over all of the PHI nodes setting their values...
659 SF.CurInst = SF.CurBB->begin();
660 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
661 ++SF.CurInst, ++i)
662 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000663}
664
Chris Lattner92101ac2001-08-23 17:05:04 +0000665//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000666// Memory Instruction Implementations
667//===----------------------------------------------------------------------===//
668
Chris Lattnerd7916e92003-05-10 21:22:39 +0000669void Interpreter::visitAllocationInst(AllocationInst &I) {
670 ExecutionContext &SF = ECStack.back();
671
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000672 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000673
Chris Lattnercc82cc12002-04-28 21:57:33 +0000674 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000675 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000676
677 // Allocate enough memory to hold the type...
Brian Gaeke2fa82122003-11-05 01:02:14 +0000678 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000679
Chris Lattnerfe11a972002-12-23 23:59:41 +0000680 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000681 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000682 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000683
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000684 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000685 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000686}
687
Chris Lattnerd7916e92003-05-10 21:22:39 +0000688void Interpreter::visitFreeInst(FreeInst &I) {
689 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000690 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
691 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000692 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000693 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000694}
695
Chris Lattnera34c5682002-08-27 22:33:45 +0000696// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000697//
Chris Lattner4af6de82003-11-25 20:44:56 +0000698GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
699 gep_type_iterator E,
Chris Lattnerfe11a972002-12-23 23:59:41 +0000700 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000701 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000702 "Cannot getElementOffset of a nonpointer type!");
703
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000704 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000705
706 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000707 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000708 const StructLayout *SLO = TD.getStructLayout(STy);
709
Brian Gaeke2798cd02003-12-12 05:13:05 +0000710 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
Chris Lattner782b9392001-11-26 18:18:18 +0000711 unsigned Index = CPU->getValue();
712
Chris Lattner782b9392001-11-26 18:18:18 +0000713 Total += SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000714 } else {
715 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000716 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000717 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
718
719 uint64_t Idx;
720 switch (I.getOperand()->getType()->getPrimitiveID()) {
721 default: assert(0 && "Illegal getelementptr index for sequential type!");
722 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
723 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
724 case Type::IntTyID: Idx = IdxGV.IntVal; break;
725 case Type::LongTyID: Idx = IdxGV.LongVal; break;
726 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
727 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
728 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
729 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
730 }
731 Total += TD.getTypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000732 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000733 }
734
Chris Lattnera34c5682002-08-27 22:33:45 +0000735 GenericValue Result;
736 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
737 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000738}
739
Chris Lattnerd7916e92003-05-10 21:22:39 +0000740void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
741 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000742 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000743 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000744}
745
Chris Lattnerd7916e92003-05-10 21:22:39 +0000746void Interpreter::visitLoadInst(LoadInst &I) {
747 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000748 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000749 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000750 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000751 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000752}
753
Chris Lattnerd7916e92003-05-10 21:22:39 +0000754void Interpreter::visitStoreInst(StoreInst &I) {
755 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000756 GenericValue Val = getOperandValue(I.getOperand(0), SF);
757 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000758 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000759 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000760}
761
Chris Lattner86660982001-08-27 05:16:50 +0000762//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000763// Miscellaneous Instruction Implementations
764//===----------------------------------------------------------------------===//
765
Brian Gaekefea483d2003-11-07 20:04:22 +0000766void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000767 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000768
769 // Check to see if this is an intrinsic function call...
770 if (Function *F = CS.getCalledFunction())
771 switch (F->getIntrinsicID()) {
772 case Intrinsic::va_start: // va_start: implemented by getFirstVarArg()
773 SetValue(CS.getInstruction(), getFirstVarArg(), SF);
774 return;
775 case Intrinsic::va_end: // va_end is a noop for the interpreter
776 return;
777 case Intrinsic::va_copy: // va_copy: dest = src
778 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
779 return;
780 default:
781 // If it is an unknown intrinsic function, using the intrinsic lowering
782 // class to transform it into hopefully tasty LLVM code.
783 //
784 Instruction *Prev = CS.getInstruction()->getPrev();
785 BasicBlock *Parent = CS.getInstruction()->getParent();
786 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
787
788 // Restore the CurInst pointer to the first instruction newly inserted, if
789 // any.
790 if (!Prev) {
791 SF.CurInst = Parent->begin();
792 } else {
793 SF.CurInst = Prev;
794 ++SF.CurInst;
795 }
796 }
797
Brian Gaekefea483d2003-11-07 20:04:22 +0000798 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000799 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000800 const unsigned NumArgs = SF.Caller.arg_size();
801 ArgVals.reserve(NumArgs);
802 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
803 e = SF.Caller.arg_end(); i != e; ++i) {
804 Value *V = *i;
805 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000806 // Promote all integral types whose size is < sizeof(int) into ints. We do
807 // this by zero or sign extending the value as appropriate according to the
808 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000809 const Type *Ty = V->getType();
810 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000811 if (Ty == Type::ShortTy)
812 ArgVals.back().IntVal = ArgVals.back().ShortVal;
813 else if (Ty == Type::UShortTy)
814 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
815 else if (Ty == Type::SByteTy)
816 ArgVals.back().IntVal = ArgVals.back().SByteVal;
817 else if (Ty == Type::UByteTy)
818 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
819 else if (Ty == Type::BoolTy)
820 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
821 else
822 assert(0 && "Unknown type!");
823 }
824 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000825
Chris Lattner070cf5e2001-11-07 20:12:30 +0000826 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000827 // and treat it as a function pointer.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000828 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000829 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000830}
831
Chris Lattner86660982001-08-27 05:16:50 +0000832#define IMPLEMENT_SHIFT(OP, TY) \
833 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
834
Brian Gaeke63438cc2003-12-11 00:22:59 +0000835static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
836 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000837 GenericValue Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000838 switch (Ty->getPrimitiveID()) {
839 IMPLEMENT_SHIFT(<<, UByte);
840 IMPLEMENT_SHIFT(<<, SByte);
841 IMPLEMENT_SHIFT(<<, UShort);
842 IMPLEMENT_SHIFT(<<, Short);
843 IMPLEMENT_SHIFT(<<, UInt);
844 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000845 IMPLEMENT_SHIFT(<<, ULong);
846 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000847 default:
Chris Lattner02868352003-04-22 21:22:33 +0000848 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000849 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000850 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000851}
852
Brian Gaeke63438cc2003-12-11 00:22:59 +0000853static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
854 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000855 GenericValue Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000856 switch (Ty->getPrimitiveID()) {
857 IMPLEMENT_SHIFT(>>, UByte);
858 IMPLEMENT_SHIFT(>>, SByte);
859 IMPLEMENT_SHIFT(>>, UShort);
860 IMPLEMENT_SHIFT(>>, Short);
861 IMPLEMENT_SHIFT(>>, UInt);
862 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000863 IMPLEMENT_SHIFT(>>, ULong);
864 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000865 default:
Chris Lattner02868352003-04-22 21:22:33 +0000866 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
867 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000868 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000869 return Dest;
870}
871
872void Interpreter::visitShl(ShiftInst &I) {
873 ExecutionContext &SF = ECStack.back();
874 const Type *Ty = I.getOperand(0)->getType();
875 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
876 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
877 GenericValue Dest;
878 Dest = executeShlInst (Src1, Src2, Ty);
879 SetValue(&I, Dest, SF);
880}
881
882void Interpreter::visitShr(ShiftInst &I) {
883 ExecutionContext &SF = ECStack.back();
884 const Type *Ty = I.getOperand(0)->getType();
885 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
886 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
887 GenericValue Dest;
888 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000889 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000890}
891
892#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000893 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000894
895#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
896 case Type::DESTTY##TyID: \
897 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000898 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000899 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
900 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
901 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
902 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
903 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000904 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
905 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000906 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
907 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000908
909#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
910 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
911 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
912
913#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000914 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
915 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000916 } \
917 break
918
919#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
920 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
921 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000922 IMPLEMENT_CAST_CASE_END()
923
Brian Gaeke29794cb2003-09-05 18:55:03 +0000924GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
925 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000926 const Type *SrcTy = SrcVal->getType();
927 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000928
929 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000930 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
931 IMPLEMENT_CAST_CASE(SByte , ( signed char));
932 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000933 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000934 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
935 IMPLEMENT_CAST_CASE(Int , ( signed int ));
936 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
937 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000938 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000939 IMPLEMENT_CAST_CASE(Float , (float));
940 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000941 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000942 default:
Chris Lattner02868352003-04-22 21:22:33 +0000943 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000944 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000945 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000946
947 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000948}
Chris Lattner92101ac2001-08-23 17:05:04 +0000949
Chris Lattnerd7916e92003-05-10 21:22:39 +0000950void Interpreter::visitCastInst(CastInst &I) {
951 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000952 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
953}
Chris Lattner92101ac2001-08-23 17:05:04 +0000954
Chris Lattner4c665492003-10-18 05:55:25 +0000955void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000956 ExecutionContext &SF = ECStack.back();
957
Brian Gaeke8da17482003-11-13 06:06:01 +0000958 // Get the incoming valist parameter. LLI treats the valist as a pointer
959 // to the next argument.
Chris Lattner4c665492003-10-18 05:55:25 +0000960 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
961
Brian Gaeke8da17482003-11-13 06:06:01 +0000962 // Move the pointer to the next vararg.
963 GenericValue *ArgPtr = (GenericValue *) GVTOP (VAList);
964 ++ArgPtr;
965 VAList = PTOGV (ArgPtr);
Chris Lattner4c665492003-10-18 05:55:25 +0000966 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000967}
Chris Lattner92101ac2001-08-23 17:05:04 +0000968
Brian Gaekec1a2be12003-11-07 21:20:47 +0000969#define IMPLEMENT_VAARG(TY) \
970 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
971
972void Interpreter::visitVAArgInst(VAArgInst &I) {
973 ExecutionContext &SF = ECStack.back();
974
Brian Gaeke8da17482003-11-13 06:06:01 +0000975 // Get the incoming valist parameter. LLI treats the valist as a pointer
976 // to the next argument.
Brian Gaekec1a2be12003-11-07 21:20:47 +0000977 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke8da17482003-11-13 06:06:01 +0000978 assert (GVTOP (VAList) != 0 && "VAList was null in vaarg instruction");
979 GenericValue Dest, Src = *(GenericValue *) GVTOP (VAList);
Brian Gaekec1a2be12003-11-07 21:20:47 +0000980 const Type *Ty = I.getType();
981 switch (Ty->getPrimitiveID()) {
982 IMPLEMENT_VAARG(UByte);
983 IMPLEMENT_VAARG(SByte);
984 IMPLEMENT_VAARG(UShort);
985 IMPLEMENT_VAARG(Short);
986 IMPLEMENT_VAARG(UInt);
987 IMPLEMENT_VAARG(Int);
988 IMPLEMENT_VAARG(ULong);
989 IMPLEMENT_VAARG(Long);
990 IMPLEMENT_VAARG(Pointer);
991 IMPLEMENT_VAARG(Float);
992 IMPLEMENT_VAARG(Double);
993 IMPLEMENT_VAARG(Bool);
994 default:
995 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
996 abort();
997 }
998
999 // Set the Value of this Instruction.
1000 SetValue(&I, Dest, SF);
1001}
1002
Chris Lattner92101ac2001-08-23 17:05:04 +00001003//===----------------------------------------------------------------------===//
1004// Dispatch and Execution Code
1005//===----------------------------------------------------------------------===//
1006
Chris Lattner92101ac2001-08-23 17:05:04 +00001007//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001008// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001009//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001010void Interpreter::callFunction(Function *F,
1011 const std::vector<GenericValue> &ArgVals) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +00001012 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1013 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
Chris Lattner365a76e2001-09-10 04:49:44 +00001014 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001015 // Make a new stack frame... and fill it in.
1016 ECStack.push_back(ExecutionContext());
1017 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001018 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001019
1020 // Special handling for external functions.
1021 if (F->isExternal()) {
1022 GenericValue Result = callExternalFunction (F, ArgVals);
1023 // Simulate a 'ret' instruction of the appropriate type.
1024 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1025 return;
1026 }
1027
1028 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001029 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001030 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001031
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001032 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +00001033 assert((ArgVals.size() == F->asize() ||
1034 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001035 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001036
1037 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001038 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +00001039 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001040 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001041
1042 // Handle varargs arguments...
1043 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001044}
1045
Chris Lattner92101ac2001-08-23 17:05:04 +00001046void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001047 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001048 // Interpret a single instruction & increment the "PC".
1049 ExecutionContext &SF = ECStack.back(); // Current stack frame
1050 Instruction &I = *SF.CurInst++; // Increment before execute
1051
1052 // Track the number of dynamic instructions executed.
1053 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001054
Brian Gaeke63438cc2003-12-11 00:22:59 +00001055 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001056 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001057 }
1058}