blob: 22812c3adbddbd9bf37a88df9e72f5bff866df68 [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"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000021#include "Support/Statistic.h"
Brian Gaeke63438cc2003-12-11 00:22:59 +000022#include "Support/Debug.h"
Brian Gaeke235b2002003-10-10 17:03:22 +000023#include <cmath> // For fmod
Chris Lattner4af6de82003-11-25 20:44:56 +000024using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000025
Chris Lattnerbbdabce2002-12-08 05:51:08 +000026namespace {
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattnerbbdabce2002-12-08 05:51:08 +000028
Chris Lattner4af6de82003-11-25 20:44:56 +000029 Interpreter *TheEE = 0;
30}
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner73011782003-12-28 09:44:37 +000032
Chris Lattner2e42d3a2001-10-15 05:51:48 +000033//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000034// Value Manipulation code
35//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000036
Chris Lattnera34c5682002-08-27 22:33:45 +000037static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000038 const Type *Ty);
Brian Gaeke63438cc2003-12-11 00:22:59 +000039static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
40 const Type *Ty);
41static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
42 const Type *Ty);
43static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
44 const Type *Ty);
45static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
46 const Type *Ty);
47static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
48 const Type *Ty);
49static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
50 const Type *Ty);
51static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
53static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
55static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
57static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
59static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
60 const Type *Ty);
61static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
63static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
67static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
68 const Type *Ty);
69
70GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
71 ExecutionContext &SF) {
72 switch (CE->getOpcode()) {
73 case Instruction::Cast:
74 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
75 case Instruction::GetElementPtr:
76 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
77 gep_type_end(CE), SF);
78 case Instruction::Add:
79 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
80 getOperandValue(CE->getOperand(1), SF),
81 CE->getOperand(0)->getType());
82 case Instruction::Sub:
83 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
84 getOperandValue(CE->getOperand(1), SF),
85 CE->getOperand(0)->getType());
86 case Instruction::Mul:
87 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
88 getOperandValue(CE->getOperand(1), SF),
89 CE->getOperand(0)->getType());
90 case Instruction::Div:
91 return executeDivInst(getOperandValue(CE->getOperand(0), SF),
92 getOperandValue(CE->getOperand(1), SF),
93 CE->getOperand(0)->getType());
94 case Instruction::Rem:
95 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
96 getOperandValue(CE->getOperand(1), SF),
97 CE->getOperand(0)->getType());
98 case Instruction::And:
99 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
100 getOperandValue(CE->getOperand(1), SF),
101 CE->getOperand(0)->getType());
102 case Instruction::Or:
103 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
104 getOperandValue(CE->getOperand(1), SF),
105 CE->getOperand(0)->getType());
106 case Instruction::Xor:
107 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
108 getOperandValue(CE->getOperand(1), SF),
109 CE->getOperand(0)->getType());
110 case Instruction::SetEQ:
111 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
112 getOperandValue(CE->getOperand(1), SF),
113 CE->getOperand(0)->getType());
114 case Instruction::SetNE:
115 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
116 getOperandValue(CE->getOperand(1), SF),
117 CE->getOperand(0)->getType());
118 case Instruction::SetLE:
119 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
120 getOperandValue(CE->getOperand(1), SF),
121 CE->getOperand(0)->getType());
122 case Instruction::SetGE:
123 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
124 getOperandValue(CE->getOperand(1), SF),
125 CE->getOperand(0)->getType());
126 case Instruction::SetLT:
127 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
128 getOperandValue(CE->getOperand(1), SF),
129 CE->getOperand(0)->getType());
130 case Instruction::SetGT:
131 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
132 getOperandValue(CE->getOperand(1), SF),
133 CE->getOperand(0)->getType());
134 case Instruction::Shl:
135 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
136 getOperandValue(CE->getOperand(1), SF),
137 CE->getOperand(0)->getType());
138 case Instruction::Shr:
139 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
140 getOperandValue(CE->getOperand(1), SF),
141 CE->getOperand(0)->getType());
142
143 default:
144 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
145 abort();
146 return GenericValue();
147 }
148}
Chris Lattnera34c5682002-08-27 22:33:45 +0000149
Brian Gaeke29794cb2003-09-05 18:55:03 +0000150GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000151 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000152 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000153 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000154 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000155 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000156 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000157 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000158 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000159 }
160}
161
Chris Lattner39bb5b42001-10-15 13:25:40 +0000162static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000163 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000164}
165
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000167 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000168}
169
Chris Lattner2adcd832002-05-03 19:52:30 +0000170//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000171// Binary Instruction Implementations
172//===----------------------------------------------------------------------===//
173
174#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
175 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
176
177static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000178 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000179 GenericValue Dest;
180 switch (Ty->getPrimitiveID()) {
181 IMPLEMENT_BINARY_OPERATOR(+, UByte);
182 IMPLEMENT_BINARY_OPERATOR(+, SByte);
183 IMPLEMENT_BINARY_OPERATOR(+, UShort);
184 IMPLEMENT_BINARY_OPERATOR(+, Short);
185 IMPLEMENT_BINARY_OPERATOR(+, UInt);
186 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000187 IMPLEMENT_BINARY_OPERATOR(+, ULong);
188 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000189 IMPLEMENT_BINARY_OPERATOR(+, Float);
190 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000191 default:
Chris Lattner02868352003-04-22 21:22:33 +0000192 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
193 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000194 }
195 return Dest;
196}
197
198static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000199 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000200 GenericValue Dest;
201 switch (Ty->getPrimitiveID()) {
202 IMPLEMENT_BINARY_OPERATOR(-, UByte);
203 IMPLEMENT_BINARY_OPERATOR(-, SByte);
204 IMPLEMENT_BINARY_OPERATOR(-, UShort);
205 IMPLEMENT_BINARY_OPERATOR(-, Short);
206 IMPLEMENT_BINARY_OPERATOR(-, UInt);
207 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000208 IMPLEMENT_BINARY_OPERATOR(-, ULong);
209 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000210 IMPLEMENT_BINARY_OPERATOR(-, Float);
211 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000212 default:
Chris Lattner02868352003-04-22 21:22:33 +0000213 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
214 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000215 }
216 return Dest;
217}
218
Chris Lattnerc2593162001-10-27 08:28:11 +0000219static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000220 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000221 GenericValue Dest;
222 switch (Ty->getPrimitiveID()) {
223 IMPLEMENT_BINARY_OPERATOR(*, UByte);
224 IMPLEMENT_BINARY_OPERATOR(*, SByte);
225 IMPLEMENT_BINARY_OPERATOR(*, UShort);
226 IMPLEMENT_BINARY_OPERATOR(*, Short);
227 IMPLEMENT_BINARY_OPERATOR(*, UInt);
228 IMPLEMENT_BINARY_OPERATOR(*, Int);
229 IMPLEMENT_BINARY_OPERATOR(*, ULong);
230 IMPLEMENT_BINARY_OPERATOR(*, Long);
231 IMPLEMENT_BINARY_OPERATOR(*, Float);
232 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000233 default:
Chris Lattner02868352003-04-22 21:22:33 +0000234 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
235 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000236 }
237 return Dest;
238}
239
240static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000241 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000242 GenericValue Dest;
243 switch (Ty->getPrimitiveID()) {
244 IMPLEMENT_BINARY_OPERATOR(/, UByte);
245 IMPLEMENT_BINARY_OPERATOR(/, SByte);
246 IMPLEMENT_BINARY_OPERATOR(/, UShort);
247 IMPLEMENT_BINARY_OPERATOR(/, Short);
248 IMPLEMENT_BINARY_OPERATOR(/, UInt);
249 IMPLEMENT_BINARY_OPERATOR(/, Int);
250 IMPLEMENT_BINARY_OPERATOR(/, ULong);
251 IMPLEMENT_BINARY_OPERATOR(/, Long);
252 IMPLEMENT_BINARY_OPERATOR(/, Float);
253 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000254 default:
Chris Lattner02868352003-04-22 21:22:33 +0000255 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
256 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000257 }
258 return Dest;
259}
260
261static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000262 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000263 GenericValue Dest;
264 switch (Ty->getPrimitiveID()) {
265 IMPLEMENT_BINARY_OPERATOR(%, UByte);
266 IMPLEMENT_BINARY_OPERATOR(%, SByte);
267 IMPLEMENT_BINARY_OPERATOR(%, UShort);
268 IMPLEMENT_BINARY_OPERATOR(%, Short);
269 IMPLEMENT_BINARY_OPERATOR(%, UInt);
270 IMPLEMENT_BINARY_OPERATOR(%, Int);
271 IMPLEMENT_BINARY_OPERATOR(%, ULong);
272 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000273 case Type::FloatTyID:
274 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
275 break;
276 case Type::DoubleTyID:
277 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
278 break;
279 default:
Chris Lattner02868352003-04-22 21:22:33 +0000280 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
281 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000282 }
283 return Dest;
284}
285
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000286static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000287 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000288 GenericValue Dest;
289 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000290 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000291 IMPLEMENT_BINARY_OPERATOR(&, UByte);
292 IMPLEMENT_BINARY_OPERATOR(&, SByte);
293 IMPLEMENT_BINARY_OPERATOR(&, UShort);
294 IMPLEMENT_BINARY_OPERATOR(&, Short);
295 IMPLEMENT_BINARY_OPERATOR(&, UInt);
296 IMPLEMENT_BINARY_OPERATOR(&, Int);
297 IMPLEMENT_BINARY_OPERATOR(&, ULong);
298 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000299 default:
Chris Lattner02868352003-04-22 21:22:33 +0000300 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
301 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000302 }
303 return Dest;
304}
305
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000306static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000307 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000308 GenericValue Dest;
309 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000310 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000311 IMPLEMENT_BINARY_OPERATOR(|, UByte);
312 IMPLEMENT_BINARY_OPERATOR(|, SByte);
313 IMPLEMENT_BINARY_OPERATOR(|, UShort);
314 IMPLEMENT_BINARY_OPERATOR(|, Short);
315 IMPLEMENT_BINARY_OPERATOR(|, UInt);
316 IMPLEMENT_BINARY_OPERATOR(|, Int);
317 IMPLEMENT_BINARY_OPERATOR(|, ULong);
318 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000319 default:
Chris Lattner02868352003-04-22 21:22:33 +0000320 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
321 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000322 }
323 return Dest;
324}
325
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000326static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000327 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000328 GenericValue Dest;
329 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000330 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000331 IMPLEMENT_BINARY_OPERATOR(^, UByte);
332 IMPLEMENT_BINARY_OPERATOR(^, SByte);
333 IMPLEMENT_BINARY_OPERATOR(^, UShort);
334 IMPLEMENT_BINARY_OPERATOR(^, Short);
335 IMPLEMENT_BINARY_OPERATOR(^, UInt);
336 IMPLEMENT_BINARY_OPERATOR(^, Int);
337 IMPLEMENT_BINARY_OPERATOR(^, ULong);
338 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000339 default:
Chris Lattner02868352003-04-22 21:22:33 +0000340 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
341 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000342 }
343 return Dest;
344}
345
Chris Lattner92101ac2001-08-23 17:05:04 +0000346#define IMPLEMENT_SETCC(OP, TY) \
347 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
348
Chris Lattnerfd506f52003-04-23 19:55:35 +0000349// Handle pointers specially because they must be compared with only as much
350// width as the host has. We _do not_ want to be comparing 64 bit values when
351// running on a 32-bit target, otherwise the upper 32 bits might mess up
352// comparisons if they contain garbage.
353#define IMPLEMENT_POINTERSETCC(OP) \
354 case Type::PointerTyID: \
355 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
356 (void*)(intptr_t)Src2.PointerVal; break
357
Chris Lattner92101ac2001-08-23 17:05:04 +0000358static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000359 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000360 GenericValue Dest;
361 switch (Ty->getPrimitiveID()) {
362 IMPLEMENT_SETCC(==, UByte);
363 IMPLEMENT_SETCC(==, SByte);
364 IMPLEMENT_SETCC(==, UShort);
365 IMPLEMENT_SETCC(==, Short);
366 IMPLEMENT_SETCC(==, UInt);
367 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000368 IMPLEMENT_SETCC(==, ULong);
369 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000370 IMPLEMENT_SETCC(==, Float);
371 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000372 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000373 default:
Chris Lattner02868352003-04-22 21:22:33 +0000374 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
375 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000376 }
377 return Dest;
378}
379
380static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000381 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000382 GenericValue Dest;
383 switch (Ty->getPrimitiveID()) {
384 IMPLEMENT_SETCC(!=, UByte);
385 IMPLEMENT_SETCC(!=, SByte);
386 IMPLEMENT_SETCC(!=, UShort);
387 IMPLEMENT_SETCC(!=, Short);
388 IMPLEMENT_SETCC(!=, UInt);
389 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000390 IMPLEMENT_SETCC(!=, ULong);
391 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000392 IMPLEMENT_SETCC(!=, Float);
393 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000394 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000395
Chris Lattner92101ac2001-08-23 17:05:04 +0000396 default:
Chris Lattner02868352003-04-22 21:22:33 +0000397 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
398 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000399 }
400 return Dest;
401}
402
403static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000404 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000405 GenericValue Dest;
406 switch (Ty->getPrimitiveID()) {
407 IMPLEMENT_SETCC(<=, UByte);
408 IMPLEMENT_SETCC(<=, SByte);
409 IMPLEMENT_SETCC(<=, UShort);
410 IMPLEMENT_SETCC(<=, Short);
411 IMPLEMENT_SETCC(<=, UInt);
412 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000413 IMPLEMENT_SETCC(<=, ULong);
414 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000415 IMPLEMENT_SETCC(<=, Float);
416 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000417 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000418 default:
Chris Lattner02868352003-04-22 21:22:33 +0000419 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
420 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000421 }
422 return Dest;
423}
424
425static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000426 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000427 GenericValue Dest;
428 switch (Ty->getPrimitiveID()) {
429 IMPLEMENT_SETCC(>=, UByte);
430 IMPLEMENT_SETCC(>=, SByte);
431 IMPLEMENT_SETCC(>=, UShort);
432 IMPLEMENT_SETCC(>=, Short);
433 IMPLEMENT_SETCC(>=, UInt);
434 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000435 IMPLEMENT_SETCC(>=, ULong);
436 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000437 IMPLEMENT_SETCC(>=, Float);
438 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000439 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000440 default:
Chris Lattner02868352003-04-22 21:22:33 +0000441 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
442 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000443 }
444 return Dest;
445}
446
447static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000448 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000449 GenericValue Dest;
450 switch (Ty->getPrimitiveID()) {
451 IMPLEMENT_SETCC(<, UByte);
452 IMPLEMENT_SETCC(<, SByte);
453 IMPLEMENT_SETCC(<, UShort);
454 IMPLEMENT_SETCC(<, Short);
455 IMPLEMENT_SETCC(<, UInt);
456 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000457 IMPLEMENT_SETCC(<, ULong);
458 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000459 IMPLEMENT_SETCC(<, Float);
460 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000461 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000462 default:
Chris Lattner02868352003-04-22 21:22:33 +0000463 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
464 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000465 }
466 return Dest;
467}
468
469static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000470 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000471 GenericValue Dest;
472 switch (Ty->getPrimitiveID()) {
473 IMPLEMENT_SETCC(>, UByte);
474 IMPLEMENT_SETCC(>, SByte);
475 IMPLEMENT_SETCC(>, UShort);
476 IMPLEMENT_SETCC(>, Short);
477 IMPLEMENT_SETCC(>, UInt);
478 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000479 IMPLEMENT_SETCC(>, ULong);
480 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 IMPLEMENT_SETCC(>, Float);
482 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000483 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000484 default:
Chris Lattner02868352003-04-22 21:22:33 +0000485 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
486 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000487 }
488 return Dest;
489}
490
Chris Lattnerd7916e92003-05-10 21:22:39 +0000491void Interpreter::visitBinaryOperator(BinaryOperator &I) {
492 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000493 const Type *Ty = I.getOperand(0)->getType();
494 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
495 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 GenericValue R; // Result
497
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000498 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000499 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
500 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
501 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
502 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
503 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
504 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
505 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
506 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
507 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
508 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
509 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
510 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
511 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
512 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000513 default:
Chris Lattner02868352003-04-22 21:22:33 +0000514 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
515 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000516 }
517
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000518 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000519}
520
Chris Lattner92101ac2001-08-23 17:05:04 +0000521//===----------------------------------------------------------------------===//
522// Terminator Instruction Implementations
523//===----------------------------------------------------------------------===//
524
Chris Lattnere43db882001-10-27 04:15:57 +0000525void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000526 runAtExitHandlers ();
527 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000528}
529
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000530/// Pop the last stack frame off of ECStack and then copy the result
531/// back into the result variable if we are not returning void. The
532/// result variable may be the ExitCode, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000533/// CallInst if there was a previous stack frame. This method may
534/// invalidate any ECStack iterators you have. This method also takes
535/// care of switching to the normal destination BB, if we are returning
536/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000537///
538void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
539 GenericValue Result) {
540 // Pop the current stack frame.
541 ECStack.pop_back();
542
543 if (ECStack.empty()) { // Finished main. Put result into exit code...
544 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
545 ExitCode = Result.IntVal; // Capture the exit code of the program
546 } else {
547 ExitCode = 0;
548 }
549 } else {
550 // If we have a previous stack frame, and we have a previous call,
551 // fill in the return value...
552 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000553 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000554 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000555 SetValue(I, Result, CallingSF);
556 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
557 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000558 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000559 }
560 }
561}
562
Chris Lattnerd7916e92003-05-10 21:22:39 +0000563void Interpreter::visitReturnInst(ReturnInst &I) {
564 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000565 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000566 GenericValue Result;
567
568 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000569 if (I.getNumOperands()) {
570 RetTy = I.getReturnValue()->getType();
571 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000572 }
573
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000574 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000575}
576
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000577void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000578 // Unwind stack
579 Instruction *Inst;
580 do {
581 ECStack.pop_back ();
582 if (ECStack.empty ())
583 abort ();
584 Inst = ECStack.back ().Caller.getInstruction ();
585 } while (!(Inst && isa<InvokeInst> (Inst)));
586
587 // Return from invoke
588 ExecutionContext &InvokingSF = ECStack.back ();
589 InvokingSF.Caller = CallSite ();
590
591 // Go to exceptional destination BB of invoke instruction
592 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
593 InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000594}
595
Chris Lattnerd7916e92003-05-10 21:22:39 +0000596void Interpreter::visitBranchInst(BranchInst &I) {
597 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000598 BasicBlock *Dest;
599
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000600 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
601 if (!I.isUnconditional()) {
602 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000603 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000604 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000605 }
Chris Lattner77113b62003-05-10 20:21:16 +0000606 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000607}
608
Chris Lattnerd7916e92003-05-10 21:22:39 +0000609void Interpreter::visitSwitchInst(SwitchInst &I) {
610 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000611 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
612 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000613
614 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000615 BasicBlock *Dest = 0;
616 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000617 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000618 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000619 Dest = cast<BasicBlock>(I.getOperand(i+1));
620 break;
621 }
Chris Lattner09e93922003-04-22 20:34:47 +0000622
623 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000624 SwitchToNewBasicBlock(Dest, SF);
625}
626
627// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
628// This function handles the actual updating of block and instruction iterators
629// as well as execution of all of the PHI nodes in the destination block.
630//
631// This method does this because all of the PHI nodes must be executed
632// atomically, reading their inputs before any of the results are updated. Not
633// doing this can cause problems if the PHI nodes depend on other PHI nodes for
634// their inputs. If the input PHI node is updated before it is read, incorrect
635// results can happen. Thus we use a two phase approach.
636//
637void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
638 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
639 SF.CurBB = Dest; // Update CurBB to branch destination
640 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
641
642 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
643
644 // Loop over all of the PHI nodes in the current block, reading their inputs.
645 std::vector<GenericValue> ResultValues;
646
647 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
648 // Search for the value corresponding to this previous bb...
649 int i = PN->getBasicBlockIndex(PrevBB);
650 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
651 Value *IncomingValue = PN->getIncomingValue(i);
652
653 // Save the incoming value for this PHI node...
654 ResultValues.push_back(getOperandValue(IncomingValue, SF));
655 }
656
657 // Now loop over all of the PHI nodes setting their values...
658 SF.CurInst = SF.CurBB->begin();
659 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
660 ++SF.CurInst, ++i)
661 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000662}
663
Chris Lattner92101ac2001-08-23 17:05:04 +0000664//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000665// Memory Instruction Implementations
666//===----------------------------------------------------------------------===//
667
Chris Lattnerd7916e92003-05-10 21:22:39 +0000668void Interpreter::visitAllocationInst(AllocationInst &I) {
669 ExecutionContext &SF = ECStack.back();
670
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000671 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000672
Chris Lattnercc82cc12002-04-28 21:57:33 +0000673 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000674 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000675
676 // Allocate enough memory to hold the type...
Brian Gaeke2fa82122003-11-05 01:02:14 +0000677 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000678
Chris Lattnerfe11a972002-12-23 23:59:41 +0000679 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000680 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000681 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000682
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000683 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000684 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000685}
686
Chris Lattnerd7916e92003-05-10 21:22:39 +0000687void Interpreter::visitFreeInst(FreeInst &I) {
688 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000689 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
690 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000691 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000692 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000693}
694
Chris Lattnera34c5682002-08-27 22:33:45 +0000695// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000696//
Chris Lattner4af6de82003-11-25 20:44:56 +0000697GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
698 gep_type_iterator E,
Chris Lattnerfe11a972002-12-23 23:59:41 +0000699 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000700 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000701 "Cannot getElementOffset of a nonpointer type!");
702
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000703 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000704
705 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000706 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000707 const StructLayout *SLO = TD.getStructLayout(STy);
708
Brian Gaeke2798cd02003-12-12 05:13:05 +0000709 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
Chris Lattner782b9392001-11-26 18:18:18 +0000710 unsigned Index = CPU->getValue();
711
Chris Lattner782b9392001-11-26 18:18:18 +0000712 Total += SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000713 } else {
714 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000715 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000716 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
717
718 uint64_t Idx;
719 switch (I.getOperand()->getType()->getPrimitiveID()) {
720 default: assert(0 && "Illegal getelementptr index for sequential type!");
721 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
722 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
723 case Type::IntTyID: Idx = IdxGV.IntVal; break;
724 case Type::LongTyID: Idx = IdxGV.LongVal; break;
725 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
726 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
727 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
728 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
729 }
730 Total += TD.getTypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000731 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000732 }
733
Chris Lattnera34c5682002-08-27 22:33:45 +0000734 GenericValue Result;
735 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
736 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000737}
738
Chris Lattnerd7916e92003-05-10 21:22:39 +0000739void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
740 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000741 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000742 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000743}
744
Chris Lattnerd7916e92003-05-10 21:22:39 +0000745void Interpreter::visitLoadInst(LoadInst &I) {
746 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000747 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000748 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000749 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000750 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000751}
752
Chris Lattnerd7916e92003-05-10 21:22:39 +0000753void Interpreter::visitStoreInst(StoreInst &I) {
754 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000755 GenericValue Val = getOperandValue(I.getOperand(0), SF);
756 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000757 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000758 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000759}
760
Chris Lattner86660982001-08-27 05:16:50 +0000761//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000762// Miscellaneous Instruction Implementations
763//===----------------------------------------------------------------------===//
764
Brian Gaekefea483d2003-11-07 20:04:22 +0000765void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000766 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000767
768 // Check to see if this is an intrinsic function call...
769 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000770 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000771 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000772 case Intrinsic::not_intrinsic:
773 break;
Chris Lattner73011782003-12-28 09:44:37 +0000774 case Intrinsic::va_start: // va_start: implemented by getFirstVarArg()
775 SetValue(CS.getInstruction(), getFirstVarArg(), SF);
776 return;
777 case Intrinsic::va_end: // va_end is a noop for the interpreter
778 return;
779 case Intrinsic::va_copy: // va_copy: dest = src
780 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
781 return;
782 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000783 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000784 // class to transform it into hopefully tasty LLVM code.
785 //
786 Instruction *Prev = CS.getInstruction()->getPrev();
787 BasicBlock *Parent = CS.getInstruction()->getParent();
788 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
789
790 // Restore the CurInst pointer to the first instruction newly inserted, if
791 // any.
792 if (!Prev) {
793 SF.CurInst = Parent->begin();
794 } else {
795 SF.CurInst = Prev;
796 ++SF.CurInst;
797 }
798 }
799
Brian Gaekefea483d2003-11-07 20:04:22 +0000800 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000801 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000802 const unsigned NumArgs = SF.Caller.arg_size();
803 ArgVals.reserve(NumArgs);
804 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
805 e = SF.Caller.arg_end(); i != e; ++i) {
806 Value *V = *i;
807 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000808 // Promote all integral types whose size is < sizeof(int) into ints. We do
809 // this by zero or sign extending the value as appropriate according to the
810 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000811 const Type *Ty = V->getType();
812 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000813 if (Ty == Type::ShortTy)
814 ArgVals.back().IntVal = ArgVals.back().ShortVal;
815 else if (Ty == Type::UShortTy)
816 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
817 else if (Ty == Type::SByteTy)
818 ArgVals.back().IntVal = ArgVals.back().SByteVal;
819 else if (Ty == Type::UByteTy)
820 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
821 else if (Ty == Type::BoolTy)
822 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
823 else
824 assert(0 && "Unknown type!");
825 }
826 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000827
Chris Lattner070cf5e2001-11-07 20:12:30 +0000828 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000829 // and treat it as a function pointer.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000830 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000831 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000832}
833
Chris Lattner86660982001-08-27 05:16:50 +0000834#define IMPLEMENT_SHIFT(OP, TY) \
835 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
836
Brian Gaeke63438cc2003-12-11 00:22:59 +0000837static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
838 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000839 GenericValue Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000840 switch (Ty->getPrimitiveID()) {
841 IMPLEMENT_SHIFT(<<, UByte);
842 IMPLEMENT_SHIFT(<<, SByte);
843 IMPLEMENT_SHIFT(<<, UShort);
844 IMPLEMENT_SHIFT(<<, Short);
845 IMPLEMENT_SHIFT(<<, UInt);
846 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000847 IMPLEMENT_SHIFT(<<, ULong);
848 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000849 default:
Chris Lattner02868352003-04-22 21:22:33 +0000850 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000851 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000852 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000853}
854
Brian Gaeke63438cc2003-12-11 00:22:59 +0000855static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
856 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000857 GenericValue Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000858 switch (Ty->getPrimitiveID()) {
859 IMPLEMENT_SHIFT(>>, UByte);
860 IMPLEMENT_SHIFT(>>, SByte);
861 IMPLEMENT_SHIFT(>>, UShort);
862 IMPLEMENT_SHIFT(>>, Short);
863 IMPLEMENT_SHIFT(>>, UInt);
864 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000865 IMPLEMENT_SHIFT(>>, ULong);
866 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000867 default:
Chris Lattner02868352003-04-22 21:22:33 +0000868 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
869 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000870 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000871 return Dest;
872}
873
874void Interpreter::visitShl(ShiftInst &I) {
875 ExecutionContext &SF = ECStack.back();
876 const Type *Ty = I.getOperand(0)->getType();
877 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
878 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
879 GenericValue Dest;
880 Dest = executeShlInst (Src1, Src2, Ty);
881 SetValue(&I, Dest, SF);
882}
883
884void Interpreter::visitShr(ShiftInst &I) {
885 ExecutionContext &SF = ECStack.back();
886 const Type *Ty = I.getOperand(0)->getType();
887 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
888 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
889 GenericValue Dest;
890 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000891 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000892}
893
894#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000895 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000896
897#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
898 case Type::DESTTY##TyID: \
899 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000900 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000901 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
902 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
903 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
904 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
905 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000906 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
907 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000908 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
909 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000910
911#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
912 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
913 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
914
915#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000916 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
917 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000918 } \
919 break
920
921#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
922 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
923 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000924 IMPLEMENT_CAST_CASE_END()
925
Brian Gaeke29794cb2003-09-05 18:55:03 +0000926GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
927 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000928 const Type *SrcTy = SrcVal->getType();
929 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000930
931 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000932 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
933 IMPLEMENT_CAST_CASE(SByte , ( signed char));
934 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000935 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000936 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
937 IMPLEMENT_CAST_CASE(Int , ( signed int ));
938 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
939 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000940 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000941 IMPLEMENT_CAST_CASE(Float , (float));
942 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000943 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000944 default:
Chris Lattner02868352003-04-22 21:22:33 +0000945 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000946 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000947 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000948
949 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000950}
Chris Lattner92101ac2001-08-23 17:05:04 +0000951
Chris Lattnerd7916e92003-05-10 21:22:39 +0000952void Interpreter::visitCastInst(CastInst &I) {
953 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000954 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
955}
Chris Lattner92101ac2001-08-23 17:05:04 +0000956
Chris Lattner4c665492003-10-18 05:55:25 +0000957void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000958 ExecutionContext &SF = ECStack.back();
959
Brian Gaeke8da17482003-11-13 06:06:01 +0000960 // Get the incoming valist parameter. LLI treats the valist as a pointer
961 // to the next argument.
Chris Lattner4c665492003-10-18 05:55:25 +0000962 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
963
Brian Gaeke8da17482003-11-13 06:06:01 +0000964 // Move the pointer to the next vararg.
965 GenericValue *ArgPtr = (GenericValue *) GVTOP (VAList);
966 ++ArgPtr;
967 VAList = PTOGV (ArgPtr);
Chris Lattner4c665492003-10-18 05:55:25 +0000968 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000969}
Chris Lattner92101ac2001-08-23 17:05:04 +0000970
Brian Gaekec1a2be12003-11-07 21:20:47 +0000971#define IMPLEMENT_VAARG(TY) \
972 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
973
974void Interpreter::visitVAArgInst(VAArgInst &I) {
975 ExecutionContext &SF = ECStack.back();
976
Brian Gaeke8da17482003-11-13 06:06:01 +0000977 // Get the incoming valist parameter. LLI treats the valist as a pointer
978 // to the next argument.
Brian Gaekec1a2be12003-11-07 21:20:47 +0000979 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke8da17482003-11-13 06:06:01 +0000980 assert (GVTOP (VAList) != 0 && "VAList was null in vaarg instruction");
981 GenericValue Dest, Src = *(GenericValue *) GVTOP (VAList);
Brian Gaekec1a2be12003-11-07 21:20:47 +0000982 const Type *Ty = I.getType();
983 switch (Ty->getPrimitiveID()) {
984 IMPLEMENT_VAARG(UByte);
985 IMPLEMENT_VAARG(SByte);
986 IMPLEMENT_VAARG(UShort);
987 IMPLEMENT_VAARG(Short);
988 IMPLEMENT_VAARG(UInt);
989 IMPLEMENT_VAARG(Int);
990 IMPLEMENT_VAARG(ULong);
991 IMPLEMENT_VAARG(Long);
992 IMPLEMENT_VAARG(Pointer);
993 IMPLEMENT_VAARG(Float);
994 IMPLEMENT_VAARG(Double);
995 IMPLEMENT_VAARG(Bool);
996 default:
997 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
998 abort();
999 }
1000
1001 // Set the Value of this Instruction.
1002 SetValue(&I, Dest, SF);
1003}
1004
Chris Lattner92101ac2001-08-23 17:05:04 +00001005//===----------------------------------------------------------------------===//
1006// Dispatch and Execution Code
1007//===----------------------------------------------------------------------===//
1008
Chris Lattner92101ac2001-08-23 17:05:04 +00001009//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001010// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001011//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001012void Interpreter::callFunction(Function *F,
1013 const std::vector<GenericValue> &ArgVals) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +00001014 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1015 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
Chris Lattner365a76e2001-09-10 04:49:44 +00001016 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001017 // Make a new stack frame... and fill it in.
1018 ECStack.push_back(ExecutionContext());
1019 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001020 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001021
1022 // Special handling for external functions.
1023 if (F->isExternal()) {
1024 GenericValue Result = callExternalFunction (F, ArgVals);
1025 // Simulate a 'ret' instruction of the appropriate type.
1026 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1027 return;
1028 }
1029
1030 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001031 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001032 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001033
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001034 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +00001035 assert((ArgVals.size() == F->asize() ||
1036 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001037 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001038
1039 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001040 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +00001041 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001042 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001043
1044 // Handle varargs arguments...
1045 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001046}
1047
Chris Lattner92101ac2001-08-23 17:05:04 +00001048void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001049 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001050 // Interpret a single instruction & increment the "PC".
1051 ExecutionContext &SF = ECStack.back(); // Current stack frame
1052 Instruction &I = *SF.CurInst++; // Increment before execute
1053
1054 // Track the number of dynamic instructions executed.
1055 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001056
Brian Gaeke63438cc2003-12-11 00:22:59 +00001057 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001058 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001059 }
1060}