blob: 5b287b2cc42a0abeb52a1da1bfa70d062cdf62b2 [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
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.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
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"
Chris Lattner30483732004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000023using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000024
Chris Lattnerbbdabce2002-12-08 05:51:08 +000025namespace {
26 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattnerbbdabce2002-12-08 05:51:08 +000027
Chris Lattner4af6de82003-11-25 20:44:56 +000028 Interpreter *TheEE = 0;
29}
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner73011782003-12-28 09:44:37 +000031
Chris Lattner2e42d3a2001-10-15 05:51:48 +000032//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000033// Value Manipulation code
34//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000035
Misha Brukmand1c881a2005-04-21 22:43:08 +000036static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
37 const Type *Ty);
38static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
39 const Type *Ty);
40static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
41 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000042static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
43 const Type *Ty);
44static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
45 const Type *Ty);
46static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
47 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000048static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
49 const Type *Ty);
50static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
51 const Type *Ty);
52static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
53 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000054static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
55 const Type *Ty);
56static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
57 const Type *Ty);
58static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
59 const Type *Ty);
60static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
61 const Type *Ty);
62static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
63 const Type *Ty);
64static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
65 const Type *Ty);
66static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
67 const Type *Ty);
68static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
69 const Type *Ty);
70static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
71 const Type *Ty);
72static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
73 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000074static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
75 const Type *Ty);
76static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
77 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000078static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000079 GenericValue Src3);
80
Brian Gaeke63438cc2003-12-11 00:22:59 +000081GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
82 ExecutionContext &SF) {
83 switch (CE->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +000084 case Instruction::Trunc:
85 case Instruction::ZExt:
86 case Instruction::SExt:
87 case Instruction::FPTrunc:
88 case Instruction::FPExt:
89 case Instruction::UIToFP:
90 case Instruction::SIToFP:
91 case Instruction::FPToUI:
92 case Instruction::FPToSI:
93 case Instruction::PtrToInt:
94 case Instruction::IntToPtr:
95 case Instruction::BitCast:
96 return executeCastOperation(Instruction::CastOps(CE->getOpcode()),
97 CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +000098 case Instruction::GetElementPtr:
99 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
100 gep_type_end(CE), SF);
101 case Instruction::Add:
102 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
103 getOperandValue(CE->getOperand(1), SF),
104 CE->getOperand(0)->getType());
105 case Instruction::Sub:
106 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
107 getOperandValue(CE->getOperand(1), SF),
108 CE->getOperand(0)->getType());
109 case Instruction::Mul:
110 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
111 getOperandValue(CE->getOperand(1), SF),
112 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000113 case Instruction::SDiv:
114 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
115 getOperandValue(CE->getOperand(1), SF),
116 CE->getOperand(0)->getType());
117 case Instruction::UDiv:
118 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
119 getOperandValue(CE->getOperand(1), SF),
120 CE->getOperand(0)->getType());
121 case Instruction::FDiv:
122 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
123 getOperandValue(CE->getOperand(1), SF),
124 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000125 case Instruction::URem:
126 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000127 getOperandValue(CE->getOperand(1), SF),
128 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000129 case Instruction::SRem:
130 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
131 getOperandValue(CE->getOperand(1), SF),
132 CE->getOperand(0)->getType());
133 case Instruction::FRem:
134 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
135 getOperandValue(CE->getOperand(1), SF),
136 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000137 case Instruction::And:
138 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
139 getOperandValue(CE->getOperand(1), SF),
140 CE->getOperand(0)->getType());
141 case Instruction::Or:
142 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
143 getOperandValue(CE->getOperand(1), SF),
144 CE->getOperand(0)->getType());
145 case Instruction::Xor:
146 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
147 getOperandValue(CE->getOperand(1), SF),
148 CE->getOperand(0)->getType());
149 case Instruction::SetEQ:
150 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
151 getOperandValue(CE->getOperand(1), SF),
152 CE->getOperand(0)->getType());
153 case Instruction::SetNE:
154 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
155 getOperandValue(CE->getOperand(1), SF),
156 CE->getOperand(0)->getType());
157 case Instruction::SetLE:
158 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
159 getOperandValue(CE->getOperand(1), SF),
160 CE->getOperand(0)->getType());
161 case Instruction::SetGE:
162 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
163 getOperandValue(CE->getOperand(1), SF),
164 CE->getOperand(0)->getType());
165 case Instruction::SetLT:
166 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
167 getOperandValue(CE->getOperand(1), SF),
168 CE->getOperand(0)->getType());
169 case Instruction::SetGT:
170 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
171 getOperandValue(CE->getOperand(1), SF),
172 CE->getOperand(0)->getType());
173 case Instruction::Shl:
174 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
175 getOperandValue(CE->getOperand(1), SF),
176 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000177 case Instruction::LShr:
178 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
179 getOperandValue(CE->getOperand(1), SF),
180 CE->getOperand(0)->getType());
181 case Instruction::AShr:
182 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
183 getOperandValue(CE->getOperand(1), SF),
184 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000185 case Instruction::Select:
186 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
187 getOperandValue(CE->getOperand(1), SF),
188 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000189 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000190 llvm_cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000191 abort();
192 return GenericValue();
193 }
194}
Chris Lattnera34c5682002-08-27 22:33:45 +0000195
Brian Gaeke29794cb2003-09-05 18:55:03 +0000196GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000197 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000198 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000199 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000200 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000201 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000202 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000203 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000204 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000205 }
206}
207
Chris Lattner39bb5b42001-10-15 13:25:40 +0000208static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000209 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000210}
211
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000212void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000213 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000214}
215
Chris Lattner2adcd832002-05-03 19:52:30 +0000216//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000217// Binary Instruction Implementations
218//===----------------------------------------------------------------------===//
219
220#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
221 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
222
Misha Brukmand1c881a2005-04-21 22:43:08 +0000223static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
224 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000225 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000226 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000227 IMPLEMENT_BINARY_OPERATOR(+, UByte);
228 IMPLEMENT_BINARY_OPERATOR(+, SByte);
229 IMPLEMENT_BINARY_OPERATOR(+, UShort);
230 IMPLEMENT_BINARY_OPERATOR(+, Short);
231 IMPLEMENT_BINARY_OPERATOR(+, UInt);
232 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000233 IMPLEMENT_BINARY_OPERATOR(+, ULong);
234 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000235 IMPLEMENT_BINARY_OPERATOR(+, Float);
236 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000237 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000238 llvm_cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000239 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000240 }
241 return Dest;
242}
243
Misha Brukmand1c881a2005-04-21 22:43:08 +0000244static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
245 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000246 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000247 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000248 IMPLEMENT_BINARY_OPERATOR(-, UByte);
249 IMPLEMENT_BINARY_OPERATOR(-, SByte);
250 IMPLEMENT_BINARY_OPERATOR(-, UShort);
251 IMPLEMENT_BINARY_OPERATOR(-, Short);
252 IMPLEMENT_BINARY_OPERATOR(-, UInt);
253 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000254 IMPLEMENT_BINARY_OPERATOR(-, ULong);
255 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000256 IMPLEMENT_BINARY_OPERATOR(-, Float);
257 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000258 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000259 llvm_cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000260 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000261 }
262 return Dest;
263}
264
Misha Brukmand1c881a2005-04-21 22:43:08 +0000265static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
266 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000267 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000268 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000269 IMPLEMENT_BINARY_OPERATOR(*, UByte);
270 IMPLEMENT_BINARY_OPERATOR(*, SByte);
271 IMPLEMENT_BINARY_OPERATOR(*, UShort);
272 IMPLEMENT_BINARY_OPERATOR(*, Short);
273 IMPLEMENT_BINARY_OPERATOR(*, UInt);
274 IMPLEMENT_BINARY_OPERATOR(*, Int);
275 IMPLEMENT_BINARY_OPERATOR(*, ULong);
276 IMPLEMENT_BINARY_OPERATOR(*, Long);
277 IMPLEMENT_BINARY_OPERATOR(*, Float);
278 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000279 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000280 llvm_cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000281 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000282 }
283 return Dest;
284}
285
Reid Spencerfe855262006-11-01 03:41:05 +0000286#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
287 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
288
Reid Spencer1628cec2006-10-26 06:15:43 +0000289static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
290 const Type *Ty) {
291 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000292 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000293 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
294 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
295 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
296 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000297 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000298 llvm_cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000299 abort();
300 }
301 return Dest;
302}
303
304static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
305 const Type *Ty) {
306 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000307 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000308 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
309 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
310 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
311 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000312 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000313 llvm_cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000314 abort();
315 }
316 return Dest;
317}
318
319static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000320 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000321 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000322 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000323 IMPLEMENT_BINARY_OPERATOR(/, Float);
324 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000325 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000326 llvm_cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000327 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000328 }
329 return Dest;
330}
331
Reid Spencer0a783f72006-11-02 01:53:59 +0000332static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000333 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000334 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000335 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000336 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
337 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
338 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
339 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
340 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000341 llvm_cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000342 abort();
343 }
344 return Dest;
345}
346
347static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
348 const Type *Ty) {
349 GenericValue Dest;
350 switch (Ty->getTypeID()) {
351 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
352 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
353 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
354 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
355 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000356 llvm_cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000357 abort();
358 }
359 return Dest;
360}
361
362static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
363 const Type *Ty) {
364 GenericValue Dest;
365 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000366 case Type::FloatTyID:
367 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
368 break;
369 case Type::DoubleTyID:
370 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
371 break;
372 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000373 llvm_cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000374 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000375 }
376 return Dest;
377}
378
Misha Brukmand1c881a2005-04-21 22:43:08 +0000379static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
380 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000381 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000382 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000383 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000384 IMPLEMENT_BINARY_OPERATOR(&, UByte);
385 IMPLEMENT_BINARY_OPERATOR(&, SByte);
386 IMPLEMENT_BINARY_OPERATOR(&, UShort);
387 IMPLEMENT_BINARY_OPERATOR(&, Short);
388 IMPLEMENT_BINARY_OPERATOR(&, UInt);
389 IMPLEMENT_BINARY_OPERATOR(&, Int);
390 IMPLEMENT_BINARY_OPERATOR(&, ULong);
391 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000392 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000393 llvm_cerr << "Unhandled type for And instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000394 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000395 }
396 return Dest;
397}
398
Misha Brukmand1c881a2005-04-21 22:43:08 +0000399static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000400 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000401 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000402 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000403 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000404 IMPLEMENT_BINARY_OPERATOR(|, UByte);
405 IMPLEMENT_BINARY_OPERATOR(|, SByte);
406 IMPLEMENT_BINARY_OPERATOR(|, UShort);
407 IMPLEMENT_BINARY_OPERATOR(|, Short);
408 IMPLEMENT_BINARY_OPERATOR(|, UInt);
409 IMPLEMENT_BINARY_OPERATOR(|, Int);
410 IMPLEMENT_BINARY_OPERATOR(|, ULong);
411 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000412 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000413 llvm_cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000414 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000415 }
416 return Dest;
417}
418
Misha Brukmand1c881a2005-04-21 22:43:08 +0000419static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000420 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000421 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000422 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000423 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000424 IMPLEMENT_BINARY_OPERATOR(^, UByte);
425 IMPLEMENT_BINARY_OPERATOR(^, SByte);
426 IMPLEMENT_BINARY_OPERATOR(^, UShort);
427 IMPLEMENT_BINARY_OPERATOR(^, Short);
428 IMPLEMENT_BINARY_OPERATOR(^, UInt);
429 IMPLEMENT_BINARY_OPERATOR(^, Int);
430 IMPLEMENT_BINARY_OPERATOR(^, ULong);
431 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000432 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000433 llvm_cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000434 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000435 }
436 return Dest;
437}
438
Chris Lattner92101ac2001-08-23 17:05:04 +0000439#define IMPLEMENT_SETCC(OP, TY) \
440 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
441
Chris Lattnerfd506f52003-04-23 19:55:35 +0000442// Handle pointers specially because they must be compared with only as much
443// width as the host has. We _do not_ want to be comparing 64 bit values when
444// running on a 32-bit target, otherwise the upper 32 bits might mess up
445// comparisons if they contain garbage.
446#define IMPLEMENT_POINTERSETCC(OP) \
447 case Type::PointerTyID: \
448 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
449 (void*)(intptr_t)Src2.PointerVal; break
450
Misha Brukmand1c881a2005-04-21 22:43:08 +0000451static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
452 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000454 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000455 IMPLEMENT_SETCC(==, UByte);
456 IMPLEMENT_SETCC(==, SByte);
457 IMPLEMENT_SETCC(==, UShort);
458 IMPLEMENT_SETCC(==, Short);
459 IMPLEMENT_SETCC(==, UInt);
460 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000461 IMPLEMENT_SETCC(==, ULong);
462 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000463 IMPLEMENT_SETCC(==, Float);
464 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000465 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000466 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000467 llvm_cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000468 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 }
470 return Dest;
471}
472
Misha Brukmand1c881a2005-04-21 22:43:08 +0000473static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
474 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000476 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 IMPLEMENT_SETCC(!=, UByte);
478 IMPLEMENT_SETCC(!=, SByte);
479 IMPLEMENT_SETCC(!=, UShort);
480 IMPLEMENT_SETCC(!=, Short);
481 IMPLEMENT_SETCC(!=, UInt);
482 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000483 IMPLEMENT_SETCC(!=, ULong);
484 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 IMPLEMENT_SETCC(!=, Float);
486 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000487 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000488
Chris Lattner92101ac2001-08-23 17:05:04 +0000489 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000490 llvm_cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000491 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000492 }
493 return Dest;
494}
495
Misha Brukmand1c881a2005-04-21 22:43:08 +0000496static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
497 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000498 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000499 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000500 IMPLEMENT_SETCC(<=, UByte);
501 IMPLEMENT_SETCC(<=, SByte);
502 IMPLEMENT_SETCC(<=, UShort);
503 IMPLEMENT_SETCC(<=, Short);
504 IMPLEMENT_SETCC(<=, UInt);
505 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000506 IMPLEMENT_SETCC(<=, ULong);
507 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 IMPLEMENT_SETCC(<=, Float);
509 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000510 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000511 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000512 llvm_cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000513 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000514 }
515 return Dest;
516}
517
Misha Brukmand1c881a2005-04-21 22:43:08 +0000518static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
519 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000520 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000521 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000522 IMPLEMENT_SETCC(>=, UByte);
523 IMPLEMENT_SETCC(>=, SByte);
524 IMPLEMENT_SETCC(>=, UShort);
525 IMPLEMENT_SETCC(>=, Short);
526 IMPLEMENT_SETCC(>=, UInt);
527 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000528 IMPLEMENT_SETCC(>=, ULong);
529 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000530 IMPLEMENT_SETCC(>=, Float);
531 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000532 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000534 llvm_cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000535 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000536 }
537 return Dest;
538}
539
Misha Brukmand1c881a2005-04-21 22:43:08 +0000540static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
541 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000542 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000543 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000544 IMPLEMENT_SETCC(<, UByte);
545 IMPLEMENT_SETCC(<, SByte);
546 IMPLEMENT_SETCC(<, UShort);
547 IMPLEMENT_SETCC(<, Short);
548 IMPLEMENT_SETCC(<, UInt);
549 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000550 IMPLEMENT_SETCC(<, ULong);
551 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000552 IMPLEMENT_SETCC(<, Float);
553 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000554 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000555 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000556 llvm_cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000557 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000558 }
559 return Dest;
560}
561
Misha Brukmand1c881a2005-04-21 22:43:08 +0000562static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
563 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000564 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000565 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000566 IMPLEMENT_SETCC(>, UByte);
567 IMPLEMENT_SETCC(>, SByte);
568 IMPLEMENT_SETCC(>, UShort);
569 IMPLEMENT_SETCC(>, Short);
570 IMPLEMENT_SETCC(>, UInt);
571 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000572 IMPLEMENT_SETCC(>, ULong);
573 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000574 IMPLEMENT_SETCC(>, Float);
575 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000576 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000577 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000578 llvm_cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000579 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000580 }
581 return Dest;
582}
583
Chris Lattnerd7916e92003-05-10 21:22:39 +0000584void Interpreter::visitBinaryOperator(BinaryOperator &I) {
585 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000586 const Type *Ty = I.getOperand(0)->getType();
587 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
588 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000589 GenericValue R; // Result
590
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000591 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000592 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
593 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
594 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000595 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
596 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
597 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000598 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
599 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
600 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000601 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
602 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
603 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
604 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
605 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
606 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
607 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
608 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
609 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000610 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000611 llvm_cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000612 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000613 }
614
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000615 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000616}
617
Misha Brukmand1c881a2005-04-21 22:43:08 +0000618static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000619 GenericValue Src3) {
620 return Src1.BoolVal ? Src2 : Src3;
621}
622
623void Interpreter::visitSelectInst(SelectInst &I) {
624 ExecutionContext &SF = ECStack.back();
625 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
626 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
627 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
628 GenericValue R = executeSelectInst(Src1, Src2, Src3);
629 SetValue(&I, R, SF);
630}
631
632
Chris Lattner92101ac2001-08-23 17:05:04 +0000633//===----------------------------------------------------------------------===//
634// Terminator Instruction Implementations
635//===----------------------------------------------------------------------===//
636
Chris Lattnere43db882001-10-27 04:15:57 +0000637void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000638 // runAtExitHandlers() assumes there are no stack frames, but
639 // if exit() was called, then it had a stack frame. Blow away
640 // the stack before interpreting atexit handlers.
641 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000642 runAtExitHandlers ();
643 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000644}
645
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000646/// Pop the last stack frame off of ECStack and then copy the result
647/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000648/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000649/// CallInst if there was a previous stack frame. This method may
650/// invalidate any ECStack iterators you have. This method also takes
651/// care of switching to the normal destination BB, if we are returning
652/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000653///
654void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
655 GenericValue Result) {
656 // Pop the current stack frame.
657 ECStack.pop_back();
658
Misha Brukmand1c881a2005-04-21 22:43:08 +0000659 if (ECStack.empty()) { // Finished main. Put result into exit code...
660 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000661 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000662 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000663 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000664 }
665 } else {
666 // If we have a previous stack frame, and we have a previous call,
667 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000668 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000669 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000670 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000671 SetValue(I, Result, CallingSF);
672 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
673 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000674 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000675 }
676 }
677}
678
Chris Lattnerd7916e92003-05-10 21:22:39 +0000679void Interpreter::visitReturnInst(ReturnInst &I) {
680 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000681 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000682 GenericValue Result;
683
684 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000685 if (I.getNumOperands()) {
686 RetTy = I.getReturnValue()->getType();
687 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000688 }
689
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000690 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000691}
692
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000693void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000694 // Unwind stack
695 Instruction *Inst;
696 do {
697 ECStack.pop_back ();
698 if (ECStack.empty ())
699 abort ();
700 Inst = ECStack.back ().Caller.getInstruction ();
701 } while (!(Inst && isa<InvokeInst> (Inst)));
702
703 // Return from invoke
704 ExecutionContext &InvokingSF = ECStack.back ();
705 InvokingSF.Caller = CallSite ();
706
707 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000708 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000709}
710
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000711void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendling480f0932006-11-27 23:54:50 +0000712 llvm_cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000713 abort();
714}
715
Chris Lattnerd7916e92003-05-10 21:22:39 +0000716void Interpreter::visitBranchInst(BranchInst &I) {
717 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000718 BasicBlock *Dest;
719
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000720 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
721 if (!I.isUnconditional()) {
722 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000723 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000724 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000725 }
Chris Lattner77113b62003-05-10 20:21:16 +0000726 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000727}
728
Chris Lattnerd7916e92003-05-10 21:22:39 +0000729void Interpreter::visitSwitchInst(SwitchInst &I) {
730 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000731 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
732 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000733
734 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000735 BasicBlock *Dest = 0;
736 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000737 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000738 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000739 Dest = cast<BasicBlock>(I.getOperand(i+1));
740 break;
741 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000742
Chris Lattner09e93922003-04-22 20:34:47 +0000743 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000744 SwitchToNewBasicBlock(Dest, SF);
745}
746
747// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
748// This function handles the actual updating of block and instruction iterators
749// as well as execution of all of the PHI nodes in the destination block.
750//
751// This method does this because all of the PHI nodes must be executed
752// atomically, reading their inputs before any of the results are updated. Not
753// doing this can cause problems if the PHI nodes depend on other PHI nodes for
754// their inputs. If the input PHI node is updated before it is read, incorrect
755// results can happen. Thus we use a two phase approach.
756//
757void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
758 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
759 SF.CurBB = Dest; // Update CurBB to branch destination
760 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
761
762 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
763
764 // Loop over all of the PHI nodes in the current block, reading their inputs.
765 std::vector<GenericValue> ResultValues;
766
767 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
768 // Search for the value corresponding to this previous bb...
769 int i = PN->getBasicBlockIndex(PrevBB);
770 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
771 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000772
Chris Lattner77113b62003-05-10 20:21:16 +0000773 // Save the incoming value for this PHI node...
774 ResultValues.push_back(getOperandValue(IncomingValue, SF));
775 }
776
777 // Now loop over all of the PHI nodes setting their values...
778 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000779 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
780 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000781 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000782 }
Chris Lattner09e93922003-04-22 20:34:47 +0000783}
784
Chris Lattner92101ac2001-08-23 17:05:04 +0000785//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000786// Memory Instruction Implementations
787//===----------------------------------------------------------------------===//
788
Chris Lattnerd7916e92003-05-10 21:22:39 +0000789void Interpreter::visitAllocationInst(AllocationInst &I) {
790 ExecutionContext &SF = ECStack.back();
791
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000792 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000793
Chris Lattnercc82cc12002-04-28 21:57:33 +0000794 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000795 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000796
797 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000798 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000799
Chris Lattnerfe11a972002-12-23 23:59:41 +0000800 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000801 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000802 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000803
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000804 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000805 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000806}
807
Chris Lattnerd7916e92003-05-10 21:22:39 +0000808void Interpreter::visitFreeInst(FreeInst &I) {
809 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000810 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
811 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000812 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000813 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000814}
815
Chris Lattnera34c5682002-08-27 22:33:45 +0000816// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000817//
Chris Lattner4af6de82003-11-25 20:44:56 +0000818GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000819 gep_type_iterator E,
820 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000821 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000822 "Cannot getElementOffset of a nonpointer type!");
823
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000824 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000825
826 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000827 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000828 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000829
Reid Spencerb83eb642006-10-20 07:07:24 +0000830 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
831 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000832
Chris Lattnerd5644962005-01-08 20:05:34 +0000833 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000834 } else {
835 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000836 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000837 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
838
839 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000840 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000841 default: assert(0 && "Illegal getelementptr index for sequential type!");
842 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
843 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
844 case Type::IntTyID: Idx = IdxGV.IntVal; break;
845 case Type::LongTyID: Idx = IdxGV.LongVal; break;
846 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
847 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
848 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
849 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
850 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000851 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000852 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000853 }
854
Chris Lattnera34c5682002-08-27 22:33:45 +0000855 GenericValue Result;
856 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
857 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000858}
859
Chris Lattnerd7916e92003-05-10 21:22:39 +0000860void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
861 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000862 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000863 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000864}
865
Chris Lattnerd7916e92003-05-10 21:22:39 +0000866void Interpreter::visitLoadInst(LoadInst &I) {
867 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000868 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000869 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000870 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000871 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000872}
873
Chris Lattnerd7916e92003-05-10 21:22:39 +0000874void Interpreter::visitStoreInst(StoreInst &I) {
875 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000876 GenericValue Val = getOperandValue(I.getOperand(0), SF);
877 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000878 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000879 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000880}
881
Chris Lattner86660982001-08-27 05:16:50 +0000882//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000883// Miscellaneous Instruction Implementations
884//===----------------------------------------------------------------------===//
885
Brian Gaekefea483d2003-11-07 20:04:22 +0000886void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000887 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000888
889 // Check to see if this is an intrinsic function call...
890 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000891 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000892 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000893 case Intrinsic::not_intrinsic:
894 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000895 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000896 GenericValue ArgIndex;
897 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
898 ArgIndex.UIntPairVal.second = 0;
899 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000900 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000901 }
Chris Lattner317201d2004-03-13 00:24:00 +0000902 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000903 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000904 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000905 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
906 return;
907 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000908 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000909 // class to transform it into hopefully tasty LLVM code.
910 //
911 Instruction *Prev = CS.getInstruction()->getPrev();
912 BasicBlock *Parent = CS.getInstruction()->getParent();
913 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
914
915 // Restore the CurInst pointer to the first instruction newly inserted, if
916 // any.
917 if (!Prev) {
918 SF.CurInst = Parent->begin();
919 } else {
920 SF.CurInst = Prev;
921 ++SF.CurInst;
922 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000923 return;
Chris Lattner73011782003-12-28 09:44:37 +0000924 }
925
Brian Gaekefea483d2003-11-07 20:04:22 +0000926 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000927 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000928 const unsigned NumArgs = SF.Caller.arg_size();
929 ArgVals.reserve(NumArgs);
930 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
931 e = SF.Caller.arg_end(); i != e; ++i) {
932 Value *V = *i;
933 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000934 // Promote all integral types whose size is < sizeof(int) into ints. We do
935 // this by zero or sign extending the value as appropriate according to the
936 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000937 const Type *Ty = V->getType();
938 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000939 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000940 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000941 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000942 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000943 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000944 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000945 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000946 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000947 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000948 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000949 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000950 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000951 }
952 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000953
Misha Brukmand1c881a2005-04-21 22:43:08 +0000954 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000955 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000956 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000957 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000958}
959
Chris Lattner86660982001-08-27 05:16:50 +0000960#define IMPLEMENT_SHIFT(OP, TY) \
961 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
962
Reid Spencer3822ff52006-11-08 06:47:33 +0000963#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
964 case Type::TY2##TyID: \
965 IMPLEMENT_SHIFT(OP, TY1)
966
Brian Gaeke63438cc2003-12-11 00:22:59 +0000967static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
968 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000969 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000970 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000971 IMPLEMENT_SHIFT(<<, UByte);
972 IMPLEMENT_SHIFT(<<, SByte);
973 IMPLEMENT_SHIFT(<<, UShort);
974 IMPLEMENT_SHIFT(<<, Short);
975 IMPLEMENT_SHIFT(<<, UInt);
976 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000977 IMPLEMENT_SHIFT(<<, ULong);
978 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000979 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000980 llvm_cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000981 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000982 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000983}
984
Reid Spencer3822ff52006-11-08 06:47:33 +0000985static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
986 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000987 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000988 switch (Ty->getTypeID()) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000989 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
990 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
991 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
992 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000993 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000994 llvm_cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +0000995 abort();
996 }
997 return Dest;
998}
999
1000static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1001 const Type *Ty) {
1002 GenericValue Dest;
1003 switch (Ty->getTypeID()) {
1004 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
1005 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
1006 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
1007 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
1008 default:
Bill Wendling480f0932006-11-27 23:54:50 +00001009 llvm_cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001010 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001011 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001012 return Dest;
1013}
1014
1015void Interpreter::visitShl(ShiftInst &I) {
1016 ExecutionContext &SF = ECStack.back();
1017 const Type *Ty = I.getOperand(0)->getType();
1018 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1019 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1020 GenericValue Dest;
1021 Dest = executeShlInst (Src1, Src2, Ty);
1022 SetValue(&I, Dest, SF);
1023}
1024
Reid Spencer3822ff52006-11-08 06:47:33 +00001025void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001026 ExecutionContext &SF = ECStack.back();
1027 const Type *Ty = I.getOperand(0)->getType();
1028 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1029 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1030 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001031 Dest = executeLShrInst (Src1, Src2, Ty);
1032 SetValue(&I, Dest, SF);
1033}
1034
1035void Interpreter::visitAShr(ShiftInst &I) {
1036 ExecutionContext &SF = ECStack.back();
1037 const Type *Ty = I.getOperand(0)->getType();
1038 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1039 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1040 GenericValue Dest;
1041 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001042 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001043}
1044
Reid Spencer3da59db2006-11-27 01:05:10 +00001045#define IMPLEMENT_CAST_START \
1046 switch (DstTy->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +00001047
Reid Spencer3da59db2006-11-27 01:05:10 +00001048#define IMPLEMENT_CAST(DTY, DCTY, STY) \
1049 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1050
1051#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
Chris Lattner86660982001-08-27 05:16:50 +00001052 case Type::DESTTY##TyID: \
Reid Spencer3da59db2006-11-27 01:05:10 +00001053 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001054 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001055 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1056 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1057 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1058 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1059 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001060 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1061 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001062 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001063 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); \
Chris Lattner86660982001-08-27 05:16:50 +00001064 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001065 IMPLEMENT_CAST(DESTTY, DESTCTY, Double) \
1066 default: \
Bill Wendling480f0932006-11-27 23:54:50 +00001067 llvm_cerr << "Unhandled cast: " \
Reid Spencer3da59db2006-11-27 01:05:10 +00001068 << *SrcTy << " to " << *DstTy << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001069 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001070 } \
1071 break
1072
Reid Spencer3da59db2006-11-27 01:05:10 +00001073#define IMPLEMENT_CAST_END \
Bill Wendling480f0932006-11-27 23:54:50 +00001074 default: llvm_cerr \
Reid Spencer3da59db2006-11-27 01:05:10 +00001075 << "Unhandled dest type for cast instruction: " \
1076 << *DstTy << "\n"; \
1077 abort(); \
1078 }
Chris Lattner86660982001-08-27 05:16:50 +00001079
Reid Spencer3da59db2006-11-27 01:05:10 +00001080GenericValue Interpreter::executeCastOperation(Instruction::CastOps opcode,
1081 Value *SrcVal, const Type *DstTy,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001082 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001083 const Type *SrcTy = SrcVal->getType();
1084 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001085
Reid Spencer3da59db2006-11-27 01:05:10 +00001086 if (opcode == Instruction::Trunc && DstTy->getTypeID() == Type::BoolTyID) {
1087 // For truncations to bool, we must clear the high order bits of the source
1088 switch (SrcTy->getTypeID()) {
1089 case Type::BoolTyID: Src.BoolVal &= 1; break;
1090 case Type::SByteTyID: Src.SByteVal &= 1; break;
1091 case Type::UByteTyID: Src.UByteVal &= 1; break;
1092 case Type::ShortTyID: Src.ShortVal &= 1; break;
1093 case Type::UShortTyID: Src.UShortVal &= 1; break;
1094 case Type::IntTyID: Src.IntVal &= 1; break;
1095 case Type::UIntTyID: Src.UIntVal &= 1; break;
1096 case Type::LongTyID: Src.LongVal &= 1; break;
1097 case Type::ULongTyID: Src.ULongVal &= 1; break;
1098 default:
1099 assert(0 && "Can't trunc a non-integer!");
1100 break;
1101 }
1102 } else if (opcode == Instruction::SExt &&
1103 SrcTy->getTypeID() == Type::BoolTyID) {
1104 // For sign extension from bool, we must extend the source bits.
1105 SrcTy = Type::LongTy;
1106 Src.LongVal = 0 - Src.BoolVal;
Chris Lattner86660982001-08-27 05:16:50 +00001107 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001108
Reid Spencer3da59db2006-11-27 01:05:10 +00001109 switch (opcode) {
1110 case Instruction::Trunc: // src integer, dest integral (can't be long)
1111 IMPLEMENT_CAST_START
1112 IMPLEMENT_CAST_CASE(Bool , (bool));
1113 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1114 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1115 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1116 IMPLEMENT_CAST_CASE(Short , ( signed short));
1117 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1118 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1119 IMPLEMENT_CAST_END
1120 break;
1121 case Instruction::ZExt: // src integral (can't be long), dest integer
1122 IMPLEMENT_CAST_START
1123 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1124 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1125 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1126 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1127 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1128 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1129 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1130 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1131 IMPLEMENT_CAST_END
1132 break;
1133 case Instruction::SExt: // src integral (can't be long), dest integer
1134 IMPLEMENT_CAST_START
1135 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1136 IMPLEMENT_CAST_CASE(SByte , (signed char));
1137 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1138 IMPLEMENT_CAST_CASE(Short , (signed short));
1139 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1140 IMPLEMENT_CAST_CASE(Int , (signed int));
1141 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1142 IMPLEMENT_CAST_CASE(Long , (int64_t));
1143 IMPLEMENT_CAST_END
1144 break;
1145 case Instruction::FPTrunc: // src double, dest float
1146 IMPLEMENT_CAST_START
1147 IMPLEMENT_CAST_CASE(Float , (float));
1148 IMPLEMENT_CAST_END
1149 break;
1150 case Instruction::FPExt: // src float, dest double
1151 IMPLEMENT_CAST_START
1152 IMPLEMENT_CAST_CASE(Double , (double));
1153 IMPLEMENT_CAST_END
1154 break;
1155 case Instruction::UIToFP: // src integral, dest floating
1156 IMPLEMENT_CAST_START
1157 IMPLEMENT_CAST_CASE(Float , (float)(uint64_t));
1158 IMPLEMENT_CAST_CASE(Double , (double)(uint64_t));
1159 IMPLEMENT_CAST_END
1160 break;
1161 case Instruction::SIToFP: // src integeral, dest floating
1162 IMPLEMENT_CAST_START
1163 IMPLEMENT_CAST_CASE(Float , (float)(int64_t));
1164 IMPLEMENT_CAST_CASE(Double , (double)(int64_t));
1165 IMPLEMENT_CAST_END
1166 break;
1167 case Instruction::FPToUI: // src floating, dest integral
1168 IMPLEMENT_CAST_START
1169 IMPLEMENT_CAST_CASE(Bool , (bool));
1170 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1171 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1172 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1173 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1174 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1175 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1176 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1177 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1178 IMPLEMENT_CAST_END
1179 break;
1180 case Instruction::FPToSI: // src floating, dest integral
1181 IMPLEMENT_CAST_START
1182 IMPLEMENT_CAST_CASE(Bool , (bool));
1183 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1184 IMPLEMENT_CAST_CASE(SByte , (signed char));
1185 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1186 IMPLEMENT_CAST_CASE(Short , (signed short));
1187 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1188 IMPLEMENT_CAST_CASE(Int , (signed int));
1189 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1190 IMPLEMENT_CAST_CASE(Long , (int64_t));
1191 IMPLEMENT_CAST_END
1192 break;
1193 case Instruction::PtrToInt: // src pointer, dest integral
1194 IMPLEMENT_CAST_START
1195 IMPLEMENT_CAST_CASE(Bool , (bool));
1196 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1197 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1198 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1199 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1200 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1201 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int));
1202 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1203 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1204 IMPLEMENT_CAST_END
1205 break;
1206 case Instruction::IntToPtr: // src integral, dest pointer
1207 IMPLEMENT_CAST_START
1208 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1209 IMPLEMENT_CAST_END
1210 break;
1211 case Instruction::BitCast: // src any, dest any (same size)
1212 IMPLEMENT_CAST_START
1213 IMPLEMENT_CAST_CASE(Bool , (bool));
1214 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1215 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1216 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1217 IMPLEMENT_CAST_CASE(Short , ( signed short));
1218 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1219 IMPLEMENT_CAST_CASE(Int , ( signed int));
1220 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1221 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1222 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1223 IMPLEMENT_CAST_CASE(Float , (float));
1224 IMPLEMENT_CAST_CASE(Double , (double));
1225 IMPLEMENT_CAST_END
1226 break;
1227 default:
Bill Wendling480f0932006-11-27 23:54:50 +00001228 llvm_cerr
Reid Spencer3da59db2006-11-27 01:05:10 +00001229 << "Invalid cast opcode for cast instruction: " << opcode << "\n";
1230 abort();
1231 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001232 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001233}
Chris Lattner92101ac2001-08-23 17:05:04 +00001234
Chris Lattnerd7916e92003-05-10 21:22:39 +00001235void Interpreter::visitCastInst(CastInst &I) {
1236 ExecutionContext &SF = ECStack.back();
Reid Spencer3da59db2006-11-27 01:05:10 +00001237 SetValue(&I, executeCastOperation(I.getOpcode(), I.getOperand(0),
1238 I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001239}
Chris Lattner92101ac2001-08-23 17:05:04 +00001240
Brian Gaekec1a2be12003-11-07 21:20:47 +00001241#define IMPLEMENT_VAARG(TY) \
1242 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1243
1244void Interpreter::visitVAArgInst(VAArgInst &I) {
1245 ExecutionContext &SF = ECStack.back();
1246
Brian Gaeke9d20b712004-02-25 23:01:48 +00001247 // Get the incoming valist parameter. LLI treats the valist as a
1248 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001249 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001250 GenericValue Dest;
1251 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001252 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001253 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001254 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001255 IMPLEMENT_VAARG(UByte);
1256 IMPLEMENT_VAARG(SByte);
1257 IMPLEMENT_VAARG(UShort);
1258 IMPLEMENT_VAARG(Short);
1259 IMPLEMENT_VAARG(UInt);
1260 IMPLEMENT_VAARG(Int);
1261 IMPLEMENT_VAARG(ULong);
1262 IMPLEMENT_VAARG(Long);
1263 IMPLEMENT_VAARG(Pointer);
1264 IMPLEMENT_VAARG(Float);
1265 IMPLEMENT_VAARG(Double);
1266 IMPLEMENT_VAARG(Bool);
1267 default:
Bill Wendling480f0932006-11-27 23:54:50 +00001268 llvm_cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001269 abort();
1270 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001271
Brian Gaekec1a2be12003-11-07 21:20:47 +00001272 // Set the Value of this Instruction.
1273 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001274
1275 // Move the pointer to the next vararg.
1276 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001277}
1278
Chris Lattner92101ac2001-08-23 17:05:04 +00001279//===----------------------------------------------------------------------===//
1280// Dispatch and Execution Code
1281//===----------------------------------------------------------------------===//
1282
Chris Lattner92101ac2001-08-23 17:05:04 +00001283//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001284// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001285//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001286void Interpreter::callFunction(Function *F,
1287 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001288 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1289 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1290 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001291 // Make a new stack frame... and fill it in.
1292 ECStack.push_back(ExecutionContext());
1293 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001294 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001295
1296 // Special handling for external functions.
1297 if (F->isExternal()) {
1298 GenericValue Result = callExternalFunction (F, ArgVals);
1299 // Simulate a 'ret' instruction of the appropriate type.
1300 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1301 return;
1302 }
1303
1304 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001305 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001306 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001307
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001308 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001309 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001310 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001311 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001312
1313 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001314 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001315 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001316 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001317
1318 // Handle varargs arguments...
1319 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001320}
1321
Chris Lattner92101ac2001-08-23 17:05:04 +00001322void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001323 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001324 // Interpret a single instruction & increment the "PC".
1325 ExecutionContext &SF = ECStack.back(); // Current stack frame
1326 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001327
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001328 // Track the number of dynamic instructions executed.
1329 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001330
Bill Wendling480f0932006-11-27 23:54:50 +00001331 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001332 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001333 }
1334}