blob: fd5fb6f0bd39e741b0b440792f0d7009993a58c1 [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"
Jeff Cohen97af7512006-12-02 02:22:01 +000023#include <cmath>
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
Misha Brukmand1c881a2005-04-21 22:43:08 +000037static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
38 const Type *Ty);
39static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
40 const Type *Ty);
41static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
42 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000043static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
44 const Type *Ty);
45static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
46 const Type *Ty);
47static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
48 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000049static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
50 const Type *Ty);
51static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
53static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000055static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
57static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
59static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
60 const Type *Ty);
61static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
63static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
67static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
68 const Type *Ty);
69static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
70 const Type *Ty);
71static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
72 const Type *Ty);
73static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
74 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000075static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
76 const Type *Ty);
77static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
78 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000079static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000080 GenericValue Src3);
81
Brian Gaeke63438cc2003-12-11 00:22:59 +000082GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
83 ExecutionContext &SF) {
84 switch (CE->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +000085 case Instruction::Trunc:
86 case Instruction::ZExt:
87 case Instruction::SExt:
88 case Instruction::FPTrunc:
89 case Instruction::FPExt:
90 case Instruction::UIToFP:
91 case Instruction::SIToFP:
92 case Instruction::FPToUI:
93 case Instruction::FPToSI:
94 case Instruction::PtrToInt:
95 case Instruction::IntToPtr:
96 case Instruction::BitCast:
97 return executeCastOperation(Instruction::CastOps(CE->getOpcode()),
98 CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +000099 case Instruction::GetElementPtr:
100 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
101 gep_type_end(CE), SF);
102 case Instruction::Add:
103 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
104 getOperandValue(CE->getOperand(1), SF),
105 CE->getOperand(0)->getType());
106 case Instruction::Sub:
107 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
108 getOperandValue(CE->getOperand(1), SF),
109 CE->getOperand(0)->getType());
110 case Instruction::Mul:
111 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
112 getOperandValue(CE->getOperand(1), SF),
113 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000114 case Instruction::SDiv:
115 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
116 getOperandValue(CE->getOperand(1), SF),
117 CE->getOperand(0)->getType());
118 case Instruction::UDiv:
119 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
120 getOperandValue(CE->getOperand(1), SF),
121 CE->getOperand(0)->getType());
122 case Instruction::FDiv:
123 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
124 getOperandValue(CE->getOperand(1), SF),
125 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000126 case Instruction::URem:
127 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000128 getOperandValue(CE->getOperand(1), SF),
129 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000130 case Instruction::SRem:
131 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
132 getOperandValue(CE->getOperand(1), SF),
133 CE->getOperand(0)->getType());
134 case Instruction::FRem:
135 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
136 getOperandValue(CE->getOperand(1), SF),
137 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000138 case Instruction::And:
139 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
140 getOperandValue(CE->getOperand(1), SF),
141 CE->getOperand(0)->getType());
142 case Instruction::Or:
143 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
144 getOperandValue(CE->getOperand(1), SF),
145 CE->getOperand(0)->getType());
146 case Instruction::Xor:
147 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
148 getOperandValue(CE->getOperand(1), SF),
149 CE->getOperand(0)->getType());
150 case Instruction::SetEQ:
151 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
152 getOperandValue(CE->getOperand(1), SF),
153 CE->getOperand(0)->getType());
154 case Instruction::SetNE:
155 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
156 getOperandValue(CE->getOperand(1), SF),
157 CE->getOperand(0)->getType());
158 case Instruction::SetLE:
159 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
160 getOperandValue(CE->getOperand(1), SF),
161 CE->getOperand(0)->getType());
162 case Instruction::SetGE:
163 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
164 getOperandValue(CE->getOperand(1), SF),
165 CE->getOperand(0)->getType());
166 case Instruction::SetLT:
167 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
168 getOperandValue(CE->getOperand(1), SF),
169 CE->getOperand(0)->getType());
170 case Instruction::SetGT:
171 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
172 getOperandValue(CE->getOperand(1), SF),
173 CE->getOperand(0)->getType());
174 case Instruction::Shl:
175 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
176 getOperandValue(CE->getOperand(1), SF),
177 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000178 case Instruction::LShr:
179 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
180 getOperandValue(CE->getOperand(1), SF),
181 CE->getOperand(0)->getType());
182 case Instruction::AShr:
183 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
184 getOperandValue(CE->getOperand(1), SF),
185 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000186 case Instruction::Select:
187 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
188 getOperandValue(CE->getOperand(1), SF),
189 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000190 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000191 llvm_cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000192 abort();
193 return GenericValue();
194 }
195}
Chris Lattnera34c5682002-08-27 22:33:45 +0000196
Brian Gaeke29794cb2003-09-05 18:55:03 +0000197GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000198 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000199 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000200 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000201 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000202 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000203 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000204 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000205 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000206 }
207}
208
Chris Lattner39bb5b42001-10-15 13:25:40 +0000209static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000210 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000211}
212
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000213void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000214 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000215}
216
Chris Lattner2adcd832002-05-03 19:52:30 +0000217//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000218// Binary Instruction Implementations
219//===----------------------------------------------------------------------===//
220
221#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
222 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
223
Misha Brukmand1c881a2005-04-21 22:43:08 +0000224static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
225 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000226 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000227 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000228 IMPLEMENT_BINARY_OPERATOR(+, UByte);
229 IMPLEMENT_BINARY_OPERATOR(+, SByte);
230 IMPLEMENT_BINARY_OPERATOR(+, UShort);
231 IMPLEMENT_BINARY_OPERATOR(+, Short);
232 IMPLEMENT_BINARY_OPERATOR(+, UInt);
233 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000234 IMPLEMENT_BINARY_OPERATOR(+, ULong);
235 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000236 IMPLEMENT_BINARY_OPERATOR(+, Float);
237 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000238 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000239 llvm_cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000240 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000241 }
242 return Dest;
243}
244
Misha Brukmand1c881a2005-04-21 22:43:08 +0000245static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
246 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000247 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000248 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000249 IMPLEMENT_BINARY_OPERATOR(-, UByte);
250 IMPLEMENT_BINARY_OPERATOR(-, SByte);
251 IMPLEMENT_BINARY_OPERATOR(-, UShort);
252 IMPLEMENT_BINARY_OPERATOR(-, Short);
253 IMPLEMENT_BINARY_OPERATOR(-, UInt);
254 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000255 IMPLEMENT_BINARY_OPERATOR(-, ULong);
256 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000257 IMPLEMENT_BINARY_OPERATOR(-, Float);
258 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000259 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000260 llvm_cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000261 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000262 }
263 return Dest;
264}
265
Misha Brukmand1c881a2005-04-21 22:43:08 +0000266static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
267 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000268 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000269 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000270 IMPLEMENT_BINARY_OPERATOR(*, UByte);
271 IMPLEMENT_BINARY_OPERATOR(*, SByte);
272 IMPLEMENT_BINARY_OPERATOR(*, UShort);
273 IMPLEMENT_BINARY_OPERATOR(*, Short);
274 IMPLEMENT_BINARY_OPERATOR(*, UInt);
275 IMPLEMENT_BINARY_OPERATOR(*, Int);
276 IMPLEMENT_BINARY_OPERATOR(*, ULong);
277 IMPLEMENT_BINARY_OPERATOR(*, Long);
278 IMPLEMENT_BINARY_OPERATOR(*, Float);
279 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000280 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000281 llvm_cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000282 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000283 }
284 return Dest;
285}
286
Reid Spencerfe855262006-11-01 03:41:05 +0000287#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
288 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
289
Reid Spencer1628cec2006-10-26 06:15:43 +0000290static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
291 const Type *Ty) {
292 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000293 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000294 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
295 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
296 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
297 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000298 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000299 llvm_cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000300 abort();
301 }
302 return Dest;
303}
304
305static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
306 const Type *Ty) {
307 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000308 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000309 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
310 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
311 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
312 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000313 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000314 llvm_cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000315 abort();
316 }
317 return Dest;
318}
319
320static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000321 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000322 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000323 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000324 IMPLEMENT_BINARY_OPERATOR(/, Float);
325 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000326 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000327 llvm_cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000328 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000329 }
330 return Dest;
331}
332
Reid Spencer0a783f72006-11-02 01:53:59 +0000333static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000334 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000335 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000336 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000337 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
338 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
339 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
340 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
341 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000342 llvm_cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000343 abort();
344 }
345 return Dest;
346}
347
348static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
349 const Type *Ty) {
350 GenericValue Dest;
351 switch (Ty->getTypeID()) {
352 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
353 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
354 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
355 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
356 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000357 llvm_cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000358 abort();
359 }
360 return Dest;
361}
362
363static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
364 const Type *Ty) {
365 GenericValue Dest;
366 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000367 case Type::FloatTyID:
368 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
369 break;
370 case Type::DoubleTyID:
371 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
372 break;
373 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000374 llvm_cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000375 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000376 }
377 return Dest;
378}
379
Misha Brukmand1c881a2005-04-21 22:43:08 +0000380static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
381 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000382 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000383 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000384 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000385 IMPLEMENT_BINARY_OPERATOR(&, UByte);
386 IMPLEMENT_BINARY_OPERATOR(&, SByte);
387 IMPLEMENT_BINARY_OPERATOR(&, UShort);
388 IMPLEMENT_BINARY_OPERATOR(&, Short);
389 IMPLEMENT_BINARY_OPERATOR(&, UInt);
390 IMPLEMENT_BINARY_OPERATOR(&, Int);
391 IMPLEMENT_BINARY_OPERATOR(&, ULong);
392 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000393 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000394 llvm_cerr << "Unhandled type for And instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000395 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000396 }
397 return Dest;
398}
399
Misha Brukmand1c881a2005-04-21 22:43:08 +0000400static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000401 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000402 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000403 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000404 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000405 IMPLEMENT_BINARY_OPERATOR(|, UByte);
406 IMPLEMENT_BINARY_OPERATOR(|, SByte);
407 IMPLEMENT_BINARY_OPERATOR(|, UShort);
408 IMPLEMENT_BINARY_OPERATOR(|, Short);
409 IMPLEMENT_BINARY_OPERATOR(|, UInt);
410 IMPLEMENT_BINARY_OPERATOR(|, Int);
411 IMPLEMENT_BINARY_OPERATOR(|, ULong);
412 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000413 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000414 llvm_cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000415 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000416 }
417 return Dest;
418}
419
Misha Brukmand1c881a2005-04-21 22:43:08 +0000420static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000421 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000422 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000423 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000424 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000425 IMPLEMENT_BINARY_OPERATOR(^, UByte);
426 IMPLEMENT_BINARY_OPERATOR(^, SByte);
427 IMPLEMENT_BINARY_OPERATOR(^, UShort);
428 IMPLEMENT_BINARY_OPERATOR(^, Short);
429 IMPLEMENT_BINARY_OPERATOR(^, UInt);
430 IMPLEMENT_BINARY_OPERATOR(^, Int);
431 IMPLEMENT_BINARY_OPERATOR(^, ULong);
432 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000433 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000434 llvm_cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000435 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000436 }
437 return Dest;
438}
439
Chris Lattner92101ac2001-08-23 17:05:04 +0000440#define IMPLEMENT_SETCC(OP, TY) \
441 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
442
Chris Lattnerfd506f52003-04-23 19:55:35 +0000443// Handle pointers specially because they must be compared with only as much
444// width as the host has. We _do not_ want to be comparing 64 bit values when
445// running on a 32-bit target, otherwise the upper 32 bits might mess up
446// comparisons if they contain garbage.
447#define IMPLEMENT_POINTERSETCC(OP) \
448 case Type::PointerTyID: \
449 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
450 (void*)(intptr_t)Src2.PointerVal; break
451
Misha Brukmand1c881a2005-04-21 22:43:08 +0000452static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
453 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000454 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000455 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000456 IMPLEMENT_SETCC(==, UByte);
457 IMPLEMENT_SETCC(==, SByte);
458 IMPLEMENT_SETCC(==, UShort);
459 IMPLEMENT_SETCC(==, Short);
460 IMPLEMENT_SETCC(==, UInt);
461 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000462 IMPLEMENT_SETCC(==, ULong);
463 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 IMPLEMENT_SETCC(==, Float);
465 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000466 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000468 llvm_cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000469 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 }
471 return Dest;
472}
473
Misha Brukmand1c881a2005-04-21 22:43:08 +0000474static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
475 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000476 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000477 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 IMPLEMENT_SETCC(!=, UByte);
479 IMPLEMENT_SETCC(!=, SByte);
480 IMPLEMENT_SETCC(!=, UShort);
481 IMPLEMENT_SETCC(!=, Short);
482 IMPLEMENT_SETCC(!=, UInt);
483 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000484 IMPLEMENT_SETCC(!=, ULong);
485 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000486 IMPLEMENT_SETCC(!=, Float);
487 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000488 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000489
Chris Lattner92101ac2001-08-23 17:05:04 +0000490 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000491 llvm_cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000492 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000493 }
494 return Dest;
495}
496
Misha Brukmand1c881a2005-04-21 22:43:08 +0000497static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
498 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000500 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 IMPLEMENT_SETCC(<=, UByte);
502 IMPLEMENT_SETCC(<=, SByte);
503 IMPLEMENT_SETCC(<=, UShort);
504 IMPLEMENT_SETCC(<=, Short);
505 IMPLEMENT_SETCC(<=, UInt);
506 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000507 IMPLEMENT_SETCC(<=, ULong);
508 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000509 IMPLEMENT_SETCC(<=, Float);
510 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000511 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000512 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000513 llvm_cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000514 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000515 }
516 return Dest;
517}
518
Misha Brukmand1c881a2005-04-21 22:43:08 +0000519static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
520 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000522 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000523 IMPLEMENT_SETCC(>=, UByte);
524 IMPLEMENT_SETCC(>=, SByte);
525 IMPLEMENT_SETCC(>=, UShort);
526 IMPLEMENT_SETCC(>=, Short);
527 IMPLEMENT_SETCC(>=, UInt);
528 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000529 IMPLEMENT_SETCC(>=, ULong);
530 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000531 IMPLEMENT_SETCC(>=, Float);
532 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000533 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000534 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000535 llvm_cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000536 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000537 }
538 return Dest;
539}
540
Misha Brukmand1c881a2005-04-21 22:43:08 +0000541static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
542 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000543 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000544 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000545 IMPLEMENT_SETCC(<, UByte);
546 IMPLEMENT_SETCC(<, SByte);
547 IMPLEMENT_SETCC(<, UShort);
548 IMPLEMENT_SETCC(<, Short);
549 IMPLEMENT_SETCC(<, UInt);
550 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000551 IMPLEMENT_SETCC(<, ULong);
552 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000553 IMPLEMENT_SETCC(<, Float);
554 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000555 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000556 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000557 llvm_cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000558 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 }
560 return Dest;
561}
562
Misha Brukmand1c881a2005-04-21 22:43:08 +0000563static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
564 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000565 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000566 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000567 IMPLEMENT_SETCC(>, UByte);
568 IMPLEMENT_SETCC(>, SByte);
569 IMPLEMENT_SETCC(>, UShort);
570 IMPLEMENT_SETCC(>, Short);
571 IMPLEMENT_SETCC(>, UInt);
572 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000573 IMPLEMENT_SETCC(>, ULong);
574 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000575 IMPLEMENT_SETCC(>, Float);
576 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000577 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000578 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000579 llvm_cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000580 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000581 }
582 return Dest;
583}
584
Chris Lattnerd7916e92003-05-10 21:22:39 +0000585void Interpreter::visitBinaryOperator(BinaryOperator &I) {
586 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000587 const Type *Ty = I.getOperand(0)->getType();
588 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
589 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000590 GenericValue R; // Result
591
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000592 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000593 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
594 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
595 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000596 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
597 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
598 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000599 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
600 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
601 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000602 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
603 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
604 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
605 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
606 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
607 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
608 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
609 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
610 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000611 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000612 llvm_cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000613 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000614 }
615
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000616 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000617}
618
Misha Brukmand1c881a2005-04-21 22:43:08 +0000619static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000620 GenericValue Src3) {
621 return Src1.BoolVal ? Src2 : Src3;
622}
623
624void Interpreter::visitSelectInst(SelectInst &I) {
625 ExecutionContext &SF = ECStack.back();
626 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
627 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
628 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
629 GenericValue R = executeSelectInst(Src1, Src2, Src3);
630 SetValue(&I, R, SF);
631}
632
633
Chris Lattner92101ac2001-08-23 17:05:04 +0000634//===----------------------------------------------------------------------===//
635// Terminator Instruction Implementations
636//===----------------------------------------------------------------------===//
637
Chris Lattnere43db882001-10-27 04:15:57 +0000638void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000639 // runAtExitHandlers() assumes there are no stack frames, but
640 // if exit() was called, then it had a stack frame. Blow away
641 // the stack before interpreting atexit handlers.
642 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000643 runAtExitHandlers ();
644 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000645}
646
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000647/// Pop the last stack frame off of ECStack and then copy the result
648/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000649/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000650/// CallInst if there was a previous stack frame. This method may
651/// invalidate any ECStack iterators you have. This method also takes
652/// care of switching to the normal destination BB, if we are returning
653/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000654///
655void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
656 GenericValue Result) {
657 // Pop the current stack frame.
658 ECStack.pop_back();
659
Misha Brukmand1c881a2005-04-21 22:43:08 +0000660 if (ECStack.empty()) { // Finished main. Put result into exit code...
661 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000662 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000663 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000664 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000665 }
666 } else {
667 // If we have a previous stack frame, and we have a previous call,
668 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000669 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000670 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000671 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000672 SetValue(I, Result, CallingSF);
673 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
674 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000675 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000676 }
677 }
678}
679
Chris Lattnerd7916e92003-05-10 21:22:39 +0000680void Interpreter::visitReturnInst(ReturnInst &I) {
681 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000682 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000683 GenericValue Result;
684
685 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000686 if (I.getNumOperands()) {
687 RetTy = I.getReturnValue()->getType();
688 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000689 }
690
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000691 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000692}
693
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000694void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000695 // Unwind stack
696 Instruction *Inst;
697 do {
698 ECStack.pop_back ();
699 if (ECStack.empty ())
700 abort ();
701 Inst = ECStack.back ().Caller.getInstruction ();
702 } while (!(Inst && isa<InvokeInst> (Inst)));
703
704 // Return from invoke
705 ExecutionContext &InvokingSF = ECStack.back ();
706 InvokingSF.Caller = CallSite ();
707
708 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000709 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000710}
711
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000712void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendling480f0932006-11-27 23:54:50 +0000713 llvm_cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000714 abort();
715}
716
Chris Lattnerd7916e92003-05-10 21:22:39 +0000717void Interpreter::visitBranchInst(BranchInst &I) {
718 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000719 BasicBlock *Dest;
720
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000721 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
722 if (!I.isUnconditional()) {
723 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000724 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000725 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000726 }
Chris Lattner77113b62003-05-10 20:21:16 +0000727 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000728}
729
Chris Lattnerd7916e92003-05-10 21:22:39 +0000730void Interpreter::visitSwitchInst(SwitchInst &I) {
731 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000732 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
733 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000734
735 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000736 BasicBlock *Dest = 0;
737 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000738 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000739 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000740 Dest = cast<BasicBlock>(I.getOperand(i+1));
741 break;
742 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000743
Chris Lattner09e93922003-04-22 20:34:47 +0000744 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000745 SwitchToNewBasicBlock(Dest, SF);
746}
747
748// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
749// This function handles the actual updating of block and instruction iterators
750// as well as execution of all of the PHI nodes in the destination block.
751//
752// This method does this because all of the PHI nodes must be executed
753// atomically, reading their inputs before any of the results are updated. Not
754// doing this can cause problems if the PHI nodes depend on other PHI nodes for
755// their inputs. If the input PHI node is updated before it is read, incorrect
756// results can happen. Thus we use a two phase approach.
757//
758void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
759 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
760 SF.CurBB = Dest; // Update CurBB to branch destination
761 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
762
763 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
764
765 // Loop over all of the PHI nodes in the current block, reading their inputs.
766 std::vector<GenericValue> ResultValues;
767
768 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
769 // Search for the value corresponding to this previous bb...
770 int i = PN->getBasicBlockIndex(PrevBB);
771 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
772 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000773
Chris Lattner77113b62003-05-10 20:21:16 +0000774 // Save the incoming value for this PHI node...
775 ResultValues.push_back(getOperandValue(IncomingValue, SF));
776 }
777
778 // Now loop over all of the PHI nodes setting their values...
779 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000780 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
781 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000782 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000783 }
Chris Lattner09e93922003-04-22 20:34:47 +0000784}
785
Chris Lattner92101ac2001-08-23 17:05:04 +0000786//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000787// Memory Instruction Implementations
788//===----------------------------------------------------------------------===//
789
Chris Lattnerd7916e92003-05-10 21:22:39 +0000790void Interpreter::visitAllocationInst(AllocationInst &I) {
791 ExecutionContext &SF = ECStack.back();
792
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000793 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000794
Chris Lattnercc82cc12002-04-28 21:57:33 +0000795 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000796 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000797
798 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000799 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000800
Chris Lattnerfe11a972002-12-23 23:59:41 +0000801 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000802 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000803 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000804
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000805 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000806 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000807}
808
Chris Lattnerd7916e92003-05-10 21:22:39 +0000809void Interpreter::visitFreeInst(FreeInst &I) {
810 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000811 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
812 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000813 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000814 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000815}
816
Chris Lattnera34c5682002-08-27 22:33:45 +0000817// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000818//
Chris Lattner4af6de82003-11-25 20:44:56 +0000819GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000820 gep_type_iterator E,
821 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000822 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000823 "Cannot getElementOffset of a nonpointer type!");
824
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000825 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000826
827 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000828 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000829 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000830
Reid Spencerb83eb642006-10-20 07:07:24 +0000831 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
832 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000833
Chris Lattnerd5644962005-01-08 20:05:34 +0000834 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000835 } else {
836 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000837 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000838 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
839
840 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000841 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000842 default: assert(0 && "Illegal getelementptr index for sequential type!");
843 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
844 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
845 case Type::IntTyID: Idx = IdxGV.IntVal; break;
846 case Type::LongTyID: Idx = IdxGV.LongVal; break;
847 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
848 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
849 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
850 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
851 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000852 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000853 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000854 }
855
Chris Lattnera34c5682002-08-27 22:33:45 +0000856 GenericValue Result;
857 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
858 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000859}
860
Chris Lattnerd7916e92003-05-10 21:22:39 +0000861void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
862 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000863 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000864 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000865}
866
Chris Lattnerd7916e92003-05-10 21:22:39 +0000867void Interpreter::visitLoadInst(LoadInst &I) {
868 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000869 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000870 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000871 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000872 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000873}
874
Chris Lattnerd7916e92003-05-10 21:22:39 +0000875void Interpreter::visitStoreInst(StoreInst &I) {
876 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000877 GenericValue Val = getOperandValue(I.getOperand(0), SF);
878 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000879 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000880 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000881}
882
Chris Lattner86660982001-08-27 05:16:50 +0000883//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000884// Miscellaneous Instruction Implementations
885//===----------------------------------------------------------------------===//
886
Brian Gaekefea483d2003-11-07 20:04:22 +0000887void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000888 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000889
890 // Check to see if this is an intrinsic function call...
891 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000892 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000893 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000894 case Intrinsic::not_intrinsic:
895 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000896 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000897 GenericValue ArgIndex;
898 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
899 ArgIndex.UIntPairVal.second = 0;
900 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000901 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000902 }
Chris Lattner317201d2004-03-13 00:24:00 +0000903 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000904 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000905 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000906 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
907 return;
908 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000909 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000910 // class to transform it into hopefully tasty LLVM code.
911 //
912 Instruction *Prev = CS.getInstruction()->getPrev();
913 BasicBlock *Parent = CS.getInstruction()->getParent();
914 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
915
916 // Restore the CurInst pointer to the first instruction newly inserted, if
917 // any.
918 if (!Prev) {
919 SF.CurInst = Parent->begin();
920 } else {
921 SF.CurInst = Prev;
922 ++SF.CurInst;
923 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000924 return;
Chris Lattner73011782003-12-28 09:44:37 +0000925 }
926
Brian Gaekefea483d2003-11-07 20:04:22 +0000927 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000928 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000929 const unsigned NumArgs = SF.Caller.arg_size();
930 ArgVals.reserve(NumArgs);
931 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
932 e = SF.Caller.arg_end(); i != e; ++i) {
933 Value *V = *i;
934 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000935 // Promote all integral types whose size is < sizeof(int) into ints. We do
936 // this by zero or sign extending the value as appropriate according to the
937 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000938 const Type *Ty = V->getType();
939 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000940 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000941 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000942 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000943 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000944 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000945 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000946 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000947 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000948 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000949 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000950 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000951 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000952 }
953 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000954
Misha Brukmand1c881a2005-04-21 22:43:08 +0000955 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000956 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000957 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000958 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000959}
960
Chris Lattner86660982001-08-27 05:16:50 +0000961#define IMPLEMENT_SHIFT(OP, TY) \
962 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
963
Reid Spencer3822ff52006-11-08 06:47:33 +0000964#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
965 case Type::TY2##TyID: \
966 IMPLEMENT_SHIFT(OP, TY1)
967
Brian Gaeke63438cc2003-12-11 00:22:59 +0000968static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
969 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000970 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000971 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000972 IMPLEMENT_SHIFT(<<, UByte);
973 IMPLEMENT_SHIFT(<<, SByte);
974 IMPLEMENT_SHIFT(<<, UShort);
975 IMPLEMENT_SHIFT(<<, Short);
976 IMPLEMENT_SHIFT(<<, UInt);
977 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000978 IMPLEMENT_SHIFT(<<, ULong);
979 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000980 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000981 llvm_cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000982 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000983 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000984}
985
Reid Spencer3822ff52006-11-08 06:47:33 +0000986static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
987 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000988 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000989 switch (Ty->getTypeID()) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000990 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
991 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
992 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
993 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000994 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000995 llvm_cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +0000996 abort();
997 }
998 return Dest;
999}
1000
1001static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1002 const Type *Ty) {
1003 GenericValue Dest;
1004 switch (Ty->getTypeID()) {
1005 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
1006 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
1007 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
1008 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
1009 default:
Bill Wendling480f0932006-11-27 23:54:50 +00001010 llvm_cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001011 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001012 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001013 return Dest;
1014}
1015
1016void Interpreter::visitShl(ShiftInst &I) {
1017 ExecutionContext &SF = ECStack.back();
1018 const Type *Ty = I.getOperand(0)->getType();
1019 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1020 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1021 GenericValue Dest;
1022 Dest = executeShlInst (Src1, Src2, Ty);
1023 SetValue(&I, Dest, SF);
1024}
1025
Reid Spencer3822ff52006-11-08 06:47:33 +00001026void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001027 ExecutionContext &SF = ECStack.back();
1028 const Type *Ty = I.getOperand(0)->getType();
1029 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1030 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1031 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001032 Dest = executeLShrInst (Src1, Src2, Ty);
1033 SetValue(&I, Dest, SF);
1034}
1035
1036void Interpreter::visitAShr(ShiftInst &I) {
1037 ExecutionContext &SF = ECStack.back();
1038 const Type *Ty = I.getOperand(0)->getType();
1039 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1040 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1041 GenericValue Dest;
1042 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001043 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001044}
1045
Reid Spencer3da59db2006-11-27 01:05:10 +00001046#define IMPLEMENT_CAST_START \
1047 switch (DstTy->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +00001048
Reid Spencer3da59db2006-11-27 01:05:10 +00001049#define IMPLEMENT_CAST(DTY, DCTY, STY) \
1050 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1051
1052#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
Chris Lattner86660982001-08-27 05:16:50 +00001053 case Type::DESTTY##TyID: \
Reid Spencer3da59db2006-11-27 01:05:10 +00001054 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001055 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001056 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1057 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1058 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1059 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1060 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001061 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1062 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001063 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001064 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); \
Chris Lattner86660982001-08-27 05:16:50 +00001065 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001066 IMPLEMENT_CAST(DESTTY, DESTCTY, Double) \
1067 default: \
Bill Wendling480f0932006-11-27 23:54:50 +00001068 llvm_cerr << "Unhandled cast: " \
Reid Spencer3da59db2006-11-27 01:05:10 +00001069 << *SrcTy << " to " << *DstTy << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001070 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001071 } \
1072 break
1073
Reid Spencer3da59db2006-11-27 01:05:10 +00001074#define IMPLEMENT_CAST_END \
Bill Wendling480f0932006-11-27 23:54:50 +00001075 default: llvm_cerr \
Reid Spencer3da59db2006-11-27 01:05:10 +00001076 << "Unhandled dest type for cast instruction: " \
1077 << *DstTy << "\n"; \
1078 abort(); \
1079 }
Chris Lattner86660982001-08-27 05:16:50 +00001080
Reid Spencer3da59db2006-11-27 01:05:10 +00001081GenericValue Interpreter::executeCastOperation(Instruction::CastOps opcode,
1082 Value *SrcVal, const Type *DstTy,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001083 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001084 const Type *SrcTy = SrcVal->getType();
1085 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001086
Reid Spencer3da59db2006-11-27 01:05:10 +00001087 if (opcode == Instruction::Trunc && DstTy->getTypeID() == Type::BoolTyID) {
1088 // For truncations to bool, we must clear the high order bits of the source
1089 switch (SrcTy->getTypeID()) {
1090 case Type::BoolTyID: Src.BoolVal &= 1; break;
1091 case Type::SByteTyID: Src.SByteVal &= 1; break;
1092 case Type::UByteTyID: Src.UByteVal &= 1; break;
1093 case Type::ShortTyID: Src.ShortVal &= 1; break;
1094 case Type::UShortTyID: Src.UShortVal &= 1; break;
1095 case Type::IntTyID: Src.IntVal &= 1; break;
1096 case Type::UIntTyID: Src.UIntVal &= 1; break;
1097 case Type::LongTyID: Src.LongVal &= 1; break;
1098 case Type::ULongTyID: Src.ULongVal &= 1; break;
1099 default:
1100 assert(0 && "Can't trunc a non-integer!");
1101 break;
1102 }
1103 } else if (opcode == Instruction::SExt &&
1104 SrcTy->getTypeID() == Type::BoolTyID) {
1105 // For sign extension from bool, we must extend the source bits.
1106 SrcTy = Type::LongTy;
1107 Src.LongVal = 0 - Src.BoolVal;
Chris Lattner86660982001-08-27 05:16:50 +00001108 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001109
Reid Spencer3da59db2006-11-27 01:05:10 +00001110 switch (opcode) {
1111 case Instruction::Trunc: // src integer, dest integral (can't be long)
1112 IMPLEMENT_CAST_START
1113 IMPLEMENT_CAST_CASE(Bool , (bool));
1114 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1115 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1116 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1117 IMPLEMENT_CAST_CASE(Short , ( signed short));
1118 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1119 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1120 IMPLEMENT_CAST_END
1121 break;
1122 case Instruction::ZExt: // src integral (can't be long), dest integer
1123 IMPLEMENT_CAST_START
1124 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1125 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1126 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1127 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1128 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1129 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1130 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1131 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1132 IMPLEMENT_CAST_END
1133 break;
1134 case Instruction::SExt: // src integral (can't be long), dest integer
1135 IMPLEMENT_CAST_START
1136 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1137 IMPLEMENT_CAST_CASE(SByte , (signed char));
1138 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1139 IMPLEMENT_CAST_CASE(Short , (signed short));
1140 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1141 IMPLEMENT_CAST_CASE(Int , (signed int));
1142 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1143 IMPLEMENT_CAST_CASE(Long , (int64_t));
1144 IMPLEMENT_CAST_END
1145 break;
1146 case Instruction::FPTrunc: // src double, dest float
1147 IMPLEMENT_CAST_START
1148 IMPLEMENT_CAST_CASE(Float , (float));
1149 IMPLEMENT_CAST_END
1150 break;
1151 case Instruction::FPExt: // src float, dest double
1152 IMPLEMENT_CAST_START
1153 IMPLEMENT_CAST_CASE(Double , (double));
1154 IMPLEMENT_CAST_END
1155 break;
1156 case Instruction::UIToFP: // src integral, dest floating
1157 IMPLEMENT_CAST_START
1158 IMPLEMENT_CAST_CASE(Float , (float)(uint64_t));
1159 IMPLEMENT_CAST_CASE(Double , (double)(uint64_t));
1160 IMPLEMENT_CAST_END
1161 break;
1162 case Instruction::SIToFP: // src integeral, dest floating
1163 IMPLEMENT_CAST_START
1164 IMPLEMENT_CAST_CASE(Float , (float)(int64_t));
1165 IMPLEMENT_CAST_CASE(Double , (double)(int64_t));
1166 IMPLEMENT_CAST_END
1167 break;
1168 case Instruction::FPToUI: // src floating, dest integral
1169 IMPLEMENT_CAST_START
1170 IMPLEMENT_CAST_CASE(Bool , (bool));
1171 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1172 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1173 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1174 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1175 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1176 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1177 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1178 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1179 IMPLEMENT_CAST_END
1180 break;
1181 case Instruction::FPToSI: // src floating, dest integral
1182 IMPLEMENT_CAST_START
1183 IMPLEMENT_CAST_CASE(Bool , (bool));
1184 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1185 IMPLEMENT_CAST_CASE(SByte , (signed char));
1186 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1187 IMPLEMENT_CAST_CASE(Short , (signed short));
1188 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1189 IMPLEMENT_CAST_CASE(Int , (signed int));
1190 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1191 IMPLEMENT_CAST_CASE(Long , (int64_t));
1192 IMPLEMENT_CAST_END
1193 break;
1194 case Instruction::PtrToInt: // src pointer, dest integral
1195 IMPLEMENT_CAST_START
1196 IMPLEMENT_CAST_CASE(Bool , (bool));
1197 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1198 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1199 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1200 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1201 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1202 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int));
1203 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1204 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1205 IMPLEMENT_CAST_END
1206 break;
1207 case Instruction::IntToPtr: // src integral, dest pointer
1208 IMPLEMENT_CAST_START
1209 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1210 IMPLEMENT_CAST_END
1211 break;
1212 case Instruction::BitCast: // src any, dest any (same size)
1213 IMPLEMENT_CAST_START
1214 IMPLEMENT_CAST_CASE(Bool , (bool));
1215 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1216 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1217 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1218 IMPLEMENT_CAST_CASE(Short , ( signed short));
1219 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1220 IMPLEMENT_CAST_CASE(Int , ( signed int));
1221 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1222 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1223 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1224 IMPLEMENT_CAST_CASE(Float , (float));
1225 IMPLEMENT_CAST_CASE(Double , (double));
1226 IMPLEMENT_CAST_END
1227 break;
1228 default:
Bill Wendling480f0932006-11-27 23:54:50 +00001229 llvm_cerr
Reid Spencer3da59db2006-11-27 01:05:10 +00001230 << "Invalid cast opcode for cast instruction: " << opcode << "\n";
1231 abort();
1232 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001233 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001234}
Chris Lattner92101ac2001-08-23 17:05:04 +00001235
Chris Lattnerd7916e92003-05-10 21:22:39 +00001236void Interpreter::visitCastInst(CastInst &I) {
1237 ExecutionContext &SF = ECStack.back();
Reid Spencer3da59db2006-11-27 01:05:10 +00001238 SetValue(&I, executeCastOperation(I.getOpcode(), I.getOperand(0),
1239 I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001240}
Chris Lattner92101ac2001-08-23 17:05:04 +00001241
Brian Gaekec1a2be12003-11-07 21:20:47 +00001242#define IMPLEMENT_VAARG(TY) \
1243 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1244
1245void Interpreter::visitVAArgInst(VAArgInst &I) {
1246 ExecutionContext &SF = ECStack.back();
1247
Brian Gaeke9d20b712004-02-25 23:01:48 +00001248 // Get the incoming valist parameter. LLI treats the valist as a
1249 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001250 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001251 GenericValue Dest;
1252 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001253 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001254 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001255 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001256 IMPLEMENT_VAARG(UByte);
1257 IMPLEMENT_VAARG(SByte);
1258 IMPLEMENT_VAARG(UShort);
1259 IMPLEMENT_VAARG(Short);
1260 IMPLEMENT_VAARG(UInt);
1261 IMPLEMENT_VAARG(Int);
1262 IMPLEMENT_VAARG(ULong);
1263 IMPLEMENT_VAARG(Long);
1264 IMPLEMENT_VAARG(Pointer);
1265 IMPLEMENT_VAARG(Float);
1266 IMPLEMENT_VAARG(Double);
1267 IMPLEMENT_VAARG(Bool);
1268 default:
Bill Wendling480f0932006-11-27 23:54:50 +00001269 llvm_cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001270 abort();
1271 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001272
Brian Gaekec1a2be12003-11-07 21:20:47 +00001273 // Set the Value of this Instruction.
1274 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001275
1276 // Move the pointer to the next vararg.
1277 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001278}
1279
Chris Lattner92101ac2001-08-23 17:05:04 +00001280//===----------------------------------------------------------------------===//
1281// Dispatch and Execution Code
1282//===----------------------------------------------------------------------===//
1283
Chris Lattner92101ac2001-08-23 17:05:04 +00001284//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001285// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001286//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001287void Interpreter::callFunction(Function *F,
1288 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001289 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1290 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1291 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001292 // Make a new stack frame... and fill it in.
1293 ECStack.push_back(ExecutionContext());
1294 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001295 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001296
1297 // Special handling for external functions.
1298 if (F->isExternal()) {
1299 GenericValue Result = callExternalFunction (F, ArgVals);
1300 // Simulate a 'ret' instruction of the appropriate type.
1301 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1302 return;
1303 }
1304
1305 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001306 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001307 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001308
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001309 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001310 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001311 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001312 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001313
1314 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001315 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001316 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001317 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001318
1319 // Handle varargs arguments...
1320 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001321}
1322
Chris Lattner92101ac2001-08-23 17:05:04 +00001323void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001324 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001325 // Interpret a single instruction & increment the "PC".
1326 ExecutionContext &SF = ECStack.back(); // Current stack frame
1327 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001328
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001329 // Track the number of dynamic instructions executed.
1330 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001331
Bill Wendling480f0932006-11-27 23:54:50 +00001332 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001333 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001334 }
1335}