blob: 3ca5682c059e60f7d8a344ae2e6a6ac0f0195d98 [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 Lattnercecf56b2006-12-19 22:56:53 +000026STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
27static Interpreter *TheEE = 0;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner73011782003-12-28 09:44:37 +000029
Chris Lattner2e42d3a2001-10-15 05:51:48 +000030//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000031// Value Manipulation code
32//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000033
Misha Brukmand1c881a2005-04-21 22:43:08 +000034static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
35 const Type *Ty);
36static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
37 const Type *Ty);
38static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
39 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000040static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
41 const Type *Ty);
42static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
43 const Type *Ty);
44static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
45 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000046static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
47 const Type *Ty);
48static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
49 const Type *Ty);
50static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
51 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000052static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
53 const Type *Ty);
54static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
55 const Type *Ty);
56static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
57 const Type *Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +000058static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
59 GenericValue Src2, const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000060static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
61 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000062static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
63 const Type *Ty);
64static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
65 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000066static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000067 GenericValue Src3);
68
Brian Gaeke63438cc2003-12-11 00:22:59 +000069GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
70 ExecutionContext &SF) {
71 switch (CE->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +000072 case Instruction::Trunc:
73 case Instruction::ZExt:
74 case Instruction::SExt:
75 case Instruction::FPTrunc:
76 case Instruction::FPExt:
77 case Instruction::UIToFP:
78 case Instruction::SIToFP:
79 case Instruction::FPToUI:
80 case Instruction::FPToSI:
81 case Instruction::PtrToInt:
82 case Instruction::IntToPtr:
83 case Instruction::BitCast:
84 return executeCastOperation(Instruction::CastOps(CE->getOpcode()),
85 CE->getOperand(0), CE->getType(), SF);
Brian Gaeke63438cc2003-12-11 00:22:59 +000086 case Instruction::GetElementPtr:
87 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
88 gep_type_end(CE), SF);
89 case Instruction::Add:
90 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
91 getOperandValue(CE->getOperand(1), SF),
92 CE->getOperand(0)->getType());
93 case Instruction::Sub:
94 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
95 getOperandValue(CE->getOperand(1), SF),
96 CE->getOperand(0)->getType());
97 case Instruction::Mul:
98 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
99 getOperandValue(CE->getOperand(1), SF),
100 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000101 case Instruction::SDiv:
102 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
103 getOperandValue(CE->getOperand(1), SF),
104 CE->getOperand(0)->getType());
105 case Instruction::UDiv:
106 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
107 getOperandValue(CE->getOperand(1), SF),
108 CE->getOperand(0)->getType());
109 case Instruction::FDiv:
110 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
111 getOperandValue(CE->getOperand(1), SF),
112 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000113 case Instruction::URem:
114 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000115 getOperandValue(CE->getOperand(1), SF),
116 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000117 case Instruction::SRem:
118 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
119 getOperandValue(CE->getOperand(1), SF),
120 CE->getOperand(0)->getType());
121 case Instruction::FRem:
122 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
123 getOperandValue(CE->getOperand(1), SF),
124 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000125 case Instruction::And:
126 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
127 getOperandValue(CE->getOperand(1), SF),
128 CE->getOperand(0)->getType());
129 case Instruction::Or:
130 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
131 getOperandValue(CE->getOperand(1), SF),
132 CE->getOperand(0)->getType());
133 case Instruction::Xor:
134 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
135 getOperandValue(CE->getOperand(1), SF),
136 CE->getOperand(0)->getType());
Reid Spencere4d87aa2006-12-23 06:05:41 +0000137 case Instruction::FCmp:
138 case Instruction::ICmp:
139 return executeCmpInst(CE->getPredicate(),
140 getOperandValue(CE->getOperand(0), SF),
141 getOperandValue(CE->getOperand(1), SF),
142 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000143 case Instruction::Shl:
144 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
145 getOperandValue(CE->getOperand(1), SF),
146 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000147 case Instruction::LShr:
148 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
149 getOperandValue(CE->getOperand(1), SF),
150 CE->getOperand(0)->getType());
151 case Instruction::AShr:
152 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
153 getOperandValue(CE->getOperand(1), SF),
154 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000155 case Instruction::Select:
156 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
157 getOperandValue(CE->getOperand(1), SF),
158 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000159 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000160 cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000161 abort();
162 return GenericValue();
163 }
164}
Chris Lattnera34c5682002-08-27 22:33:45 +0000165
Brian Gaeke29794cb2003-09-05 18:55:03 +0000166GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000167 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000168 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000169 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000170 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000171 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000172 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000173 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000174 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000175 }
176}
177
Chris Lattner39bb5b42001-10-15 13:25:40 +0000178static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000179 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000180}
181
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000182void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000183 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000184}
185
Chris Lattner2adcd832002-05-03 19:52:30 +0000186//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000187// Binary Instruction Implementations
188//===----------------------------------------------------------------------===//
189
190#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
191 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
192
Misha Brukmand1c881a2005-04-21 22:43:08 +0000193static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
194 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000195 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000196 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000197 IMPLEMENT_BINARY_OPERATOR(+, UByte);
198 IMPLEMENT_BINARY_OPERATOR(+, SByte);
199 IMPLEMENT_BINARY_OPERATOR(+, UShort);
200 IMPLEMENT_BINARY_OPERATOR(+, Short);
201 IMPLEMENT_BINARY_OPERATOR(+, UInt);
202 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000203 IMPLEMENT_BINARY_OPERATOR(+, ULong);
204 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000205 IMPLEMENT_BINARY_OPERATOR(+, Float);
206 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000207 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000208 cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000209 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000210 }
211 return Dest;
212}
213
Misha Brukmand1c881a2005-04-21 22:43:08 +0000214static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
215 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000216 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000217 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000218 IMPLEMENT_BINARY_OPERATOR(-, UByte);
219 IMPLEMENT_BINARY_OPERATOR(-, SByte);
220 IMPLEMENT_BINARY_OPERATOR(-, UShort);
221 IMPLEMENT_BINARY_OPERATOR(-, Short);
222 IMPLEMENT_BINARY_OPERATOR(-, UInt);
223 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000224 IMPLEMENT_BINARY_OPERATOR(-, ULong);
225 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000226 IMPLEMENT_BINARY_OPERATOR(-, Float);
227 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000228 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000229 cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000230 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000231 }
232 return Dest;
233}
234
Misha Brukmand1c881a2005-04-21 22:43:08 +0000235static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
236 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000237 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000238 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000239 IMPLEMENT_BINARY_OPERATOR(*, UByte);
240 IMPLEMENT_BINARY_OPERATOR(*, SByte);
241 IMPLEMENT_BINARY_OPERATOR(*, UShort);
242 IMPLEMENT_BINARY_OPERATOR(*, Short);
243 IMPLEMENT_BINARY_OPERATOR(*, UInt);
244 IMPLEMENT_BINARY_OPERATOR(*, Int);
245 IMPLEMENT_BINARY_OPERATOR(*, ULong);
246 IMPLEMENT_BINARY_OPERATOR(*, Long);
247 IMPLEMENT_BINARY_OPERATOR(*, Float);
248 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000249 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000250 cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000251 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000252 }
253 return Dest;
254}
255
Reid Spencerfe855262006-11-01 03:41:05 +0000256#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
257 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
258
Reid Spencer1628cec2006-10-26 06:15:43 +0000259static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
260 const Type *Ty) {
261 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000262 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000263 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
264 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
265 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
266 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000267 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000268 cerr << "Unhandled type for UDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000269 abort();
270 }
271 return Dest;
272}
273
274static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
275 const Type *Ty) {
276 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000277 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000278 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
279 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
280 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
281 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000282 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000283 cerr << "Unhandled type for SDiv instruction: " << *Ty << "\n";
Reid Spencer1628cec2006-10-26 06:15:43 +0000284 abort();
285 }
286 return Dest;
287}
288
289static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000290 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000291 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000292 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000293 IMPLEMENT_BINARY_OPERATOR(/, Float);
294 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000295 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000296 cerr << "Unhandled type for Div instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000297 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000298 }
299 return Dest;
300}
301
Reid Spencer0a783f72006-11-02 01:53:59 +0000302static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000303 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000304 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000305 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000306 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
307 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
308 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
309 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
310 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000311 cerr << "Unhandled type for URem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000312 abort();
313 }
314 return Dest;
315}
316
317static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
318 const Type *Ty) {
319 GenericValue Dest;
320 switch (Ty->getTypeID()) {
321 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
322 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
323 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
324 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
325 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000326 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Reid Spencer0a783f72006-11-02 01:53:59 +0000327 abort();
328 }
329 return Dest;
330}
331
332static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
333 const Type *Ty) {
334 GenericValue Dest;
335 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000336 case Type::FloatTyID:
337 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
338 break;
339 case Type::DoubleTyID:
340 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
341 break;
342 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000343 cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000344 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000345 }
346 return Dest;
347}
348
Misha Brukmand1c881a2005-04-21 22:43:08 +0000349static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
350 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000351 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000352 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000353 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000354 IMPLEMENT_BINARY_OPERATOR(&, UByte);
355 IMPLEMENT_BINARY_OPERATOR(&, SByte);
356 IMPLEMENT_BINARY_OPERATOR(&, UShort);
357 IMPLEMENT_BINARY_OPERATOR(&, Short);
358 IMPLEMENT_BINARY_OPERATOR(&, UInt);
359 IMPLEMENT_BINARY_OPERATOR(&, Int);
360 IMPLEMENT_BINARY_OPERATOR(&, ULong);
361 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000362 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000363 cerr << "Unhandled type for And instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000364 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000365 }
366 return Dest;
367}
368
Misha Brukmand1c881a2005-04-21 22:43:08 +0000369static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000370 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000371 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000372 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000373 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000374 IMPLEMENT_BINARY_OPERATOR(|, UByte);
375 IMPLEMENT_BINARY_OPERATOR(|, SByte);
376 IMPLEMENT_BINARY_OPERATOR(|, UShort);
377 IMPLEMENT_BINARY_OPERATOR(|, Short);
378 IMPLEMENT_BINARY_OPERATOR(|, UInt);
379 IMPLEMENT_BINARY_OPERATOR(|, Int);
380 IMPLEMENT_BINARY_OPERATOR(|, ULong);
381 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000382 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000383 cerr << "Unhandled type for Or instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000384 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000385 }
386 return Dest;
387}
388
Misha Brukmand1c881a2005-04-21 22:43:08 +0000389static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000390 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000391 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000392 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000393 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000394 IMPLEMENT_BINARY_OPERATOR(^, UByte);
395 IMPLEMENT_BINARY_OPERATOR(^, SByte);
396 IMPLEMENT_BINARY_OPERATOR(^, UShort);
397 IMPLEMENT_BINARY_OPERATOR(^, Short);
398 IMPLEMENT_BINARY_OPERATOR(^, UInt);
399 IMPLEMENT_BINARY_OPERATOR(^, Int);
400 IMPLEMENT_BINARY_OPERATOR(^, ULong);
401 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000402 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000403 cerr << "Unhandled type for Xor instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000404 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000405 }
406 return Dest;
407}
408
Reid Spencere4d87aa2006-12-23 06:05:41 +0000409#define IMPLEMENT_CMP(OP, TY1, TY2) \
410 case Type::TY1##TyID: Dest.BoolVal = Src1.TY2##Val OP Src2.TY2##Val; break
Chris Lattner92101ac2001-08-23 17:05:04 +0000411
Chris Lattnerfd506f52003-04-23 19:55:35 +0000412// Handle pointers specially because they must be compared with only as much
413// width as the host has. We _do not_ want to be comparing 64 bit values when
414// running on a 32-bit target, otherwise the upper 32 bits might mess up
415// comparisons if they contain garbage.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000416#define IMPLEMENT_POINTERCMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000417 case Type::PointerTyID: \
418 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
419 (void*)(intptr_t)Src2.PointerVal; break
420
Reid Spencere4d87aa2006-12-23 06:05:41 +0000421static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
422 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000424 switch (Ty->getTypeID()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000425 IMPLEMENT_CMP(==, UByte, UByte);
426 IMPLEMENT_CMP(==, SByte, SByte);
427 IMPLEMENT_CMP(==, UShort, UShort);
428 IMPLEMENT_CMP(==, Short, Short);
429 IMPLEMENT_CMP(==, UInt, UInt);
430 IMPLEMENT_CMP(==, Int, Int);
431 IMPLEMENT_CMP(==, ULong, ULong);
432 IMPLEMENT_CMP(==, Long, Long);
433 IMPLEMENT_POINTERCMP(==);
434 default:
435 cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
436 abort();
437 }
438 return Dest;
439}
440
441static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
442 const Type *Ty) {
443 GenericValue Dest;
444 switch (Ty->getTypeID()) {
445 IMPLEMENT_CMP(!=, UByte, UByte);
446 IMPLEMENT_CMP(!=, SByte, SByte);
447 IMPLEMENT_CMP(!=, UShort,UShort);
448 IMPLEMENT_CMP(!=, Short, Short);
449 IMPLEMENT_CMP(!=, UInt, UInt);
450 IMPLEMENT_CMP(!=, Int, Int);
451 IMPLEMENT_CMP(!=, ULong, ULong);
452 IMPLEMENT_CMP(!=, Long, Long);
453 IMPLEMENT_POINTERCMP(!=);
454 default:
455 cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
456 abort();
457 }
458 return Dest;
459}
460
461static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
462 const Type *Ty) {
463 GenericValue Dest;
464 switch (Ty->getTypeID()) {
465 IMPLEMENT_CMP(<, SByte, UByte);
466 IMPLEMENT_CMP(<, Short, UShort);
467 IMPLEMENT_CMP(<, Int, UInt);
468 IMPLEMENT_CMP(<, Long, ULong);
469 IMPLEMENT_CMP(<, UByte, UByte);
470 IMPLEMENT_CMP(<, UShort, UShort);
471 IMPLEMENT_CMP(<, UInt, UInt);
472 IMPLEMENT_CMP(<, ULong, ULong);
473 IMPLEMENT_POINTERCMP(<);
474 default:
475 cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
476 abort();
477 }
478 return Dest;
479}
480
481static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
482 const Type *Ty) {
483 GenericValue Dest;
484 switch (Ty->getTypeID()) {
485 IMPLEMENT_CMP(<, SByte, SByte);
486 IMPLEMENT_CMP(<, Short, Short);
487 IMPLEMENT_CMP(<, Int, Int);
488 IMPLEMENT_CMP(<, Long, Long);
489 IMPLEMENT_CMP(<, UByte, SByte);
490 IMPLEMENT_CMP(<, UShort, Short);
491 IMPLEMENT_CMP(<, UInt, Int);
492 IMPLEMENT_CMP(<, ULong, Long);
493 IMPLEMENT_POINTERCMP(<);
494 default:
495 cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
496 abort();
497 }
498 return Dest;
499}
500
501static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
502 const Type *Ty) {
503 GenericValue Dest;
504 switch (Ty->getTypeID()) {
505 IMPLEMENT_CMP(>, SByte, UByte);
506 IMPLEMENT_CMP(>, Short, UShort);
507 IMPLEMENT_CMP(>, Int, UInt);
508 IMPLEMENT_CMP(>, Long, ULong);
509 IMPLEMENT_CMP(>, UByte, UByte);
510 IMPLEMENT_CMP(>, UShort, UShort);
511 IMPLEMENT_CMP(>, UInt, UInt);
512 IMPLEMENT_CMP(>, ULong, ULong);
513 IMPLEMENT_POINTERCMP(>);
514 default:
515 cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
516 abort();
517 }
518 return Dest;
519}
520
521static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
522 const Type *Ty) {
523 GenericValue Dest;
524 switch (Ty->getTypeID()) {
525 IMPLEMENT_CMP(>, SByte, SByte);
526 IMPLEMENT_CMP(>, Short, Short);
527 IMPLEMENT_CMP(>, Int, Int);
528 IMPLEMENT_CMP(>, Long, Long);
529 IMPLEMENT_CMP(>, UByte, SByte);
530 IMPLEMENT_CMP(>, UShort, Short);
531 IMPLEMENT_CMP(>, UInt, Int);
532 IMPLEMENT_CMP(>, ULong, Long);
533 IMPLEMENT_POINTERCMP(>);
534 default:
535 cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
536 abort();
537 }
538 return Dest;
539}
540
541static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
542 const Type *Ty) {
543 GenericValue Dest;
544 switch (Ty->getTypeID()) {
545 IMPLEMENT_CMP(<=, SByte, UByte);
546 IMPLEMENT_CMP(<=, Short, UShort);
547 IMPLEMENT_CMP(<=, Int, UInt);
548 IMPLEMENT_CMP(<=, Long, ULong);
549 IMPLEMENT_CMP(<=, UByte, UByte);
550 IMPLEMENT_CMP(<=, UShort, UShort);
551 IMPLEMENT_CMP(<=, UInt, UInt);
552 IMPLEMENT_CMP(<=, ULong, ULong);
553 IMPLEMENT_POINTERCMP(<=);
554 default:
555 cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
556 abort();
557 }
558 return Dest;
559}
560
561static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
562 const Type *Ty) {
563 GenericValue Dest;
564 switch (Ty->getTypeID()) {
565 IMPLEMENT_CMP(<=, SByte, SByte);
566 IMPLEMENT_CMP(<=, Short, Short);
567 IMPLEMENT_CMP(<=, Int, Int);
568 IMPLEMENT_CMP(<=, Long, Long);
569 IMPLEMENT_CMP(<=, UByte, SByte);
570 IMPLEMENT_CMP(<=, UShort, Short);
571 IMPLEMENT_CMP(<=, UInt, Int);
572 IMPLEMENT_CMP(<=, ULong, Long);
573 IMPLEMENT_POINTERCMP(<=);
574 default:
575 cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
576 abort();
577 }
578 return Dest;
579}
580
581static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
582 const Type *Ty) {
583 GenericValue Dest;
584 switch (Ty->getTypeID()) {
585 IMPLEMENT_CMP(>=, SByte, UByte);
586 IMPLEMENT_CMP(>=, Short, UShort);
587 IMPLEMENT_CMP(>=, Int, UInt);
588 IMPLEMENT_CMP(>=, Long, ULong);
589 IMPLEMENT_CMP(>=, UByte, UByte);
590 IMPLEMENT_CMP(>=, UShort, UShort);
591 IMPLEMENT_CMP(>=, UInt, UInt);
592 IMPLEMENT_CMP(>=, ULong, ULong);
593 IMPLEMENT_POINTERCMP(>=);
594 default:
595 cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
596 abort();
597 }
598 return Dest;
599}
600
601static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
602 const Type *Ty) {
603 GenericValue Dest;
604 switch (Ty->getTypeID()) {
605 IMPLEMENT_CMP(>=, SByte, SByte);
606 IMPLEMENT_CMP(>=, Short, Short);
607 IMPLEMENT_CMP(>=, Int, Int);
608 IMPLEMENT_CMP(>=, Long, Long);
609 IMPLEMENT_CMP(>=, UByte, SByte);
610 IMPLEMENT_CMP(>=, UShort, Short);
611 IMPLEMENT_CMP(>=, UInt, Int);
612 IMPLEMENT_CMP(>=, ULong, Long);
613 IMPLEMENT_POINTERCMP(>=);
614 default:
615 cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
616 abort();
617 }
618 return Dest;
619}
620
621#define IMPLEMENT_SETCC(OP, TY) \
622 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
623
624static GenericValue executeFCMP_EQ(GenericValue Src1, GenericValue Src2,
625 const Type *Ty) {
626 GenericValue Dest;
627 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000628 IMPLEMENT_SETCC(==, Float);
629 IMPLEMENT_SETCC(==, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000630 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000631 cerr << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000632 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000633 }
634 return Dest;
635}
636
Reid Spencere4d87aa2006-12-23 06:05:41 +0000637static GenericValue executeFCMP_NE(GenericValue Src1, GenericValue Src2,
638 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000639 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000640 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000641 IMPLEMENT_SETCC(!=, Float);
642 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000643
Chris Lattner92101ac2001-08-23 17:05:04 +0000644 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000645 cerr << "Unhandled type for SetNE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000646 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000647 }
648 return Dest;
649}
650
Reid Spencere4d87aa2006-12-23 06:05:41 +0000651static GenericValue executeFCMP_LE(GenericValue Src1, GenericValue Src2,
652 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000653 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000654 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000655 IMPLEMENT_SETCC(<=, Float);
656 IMPLEMENT_SETCC(<=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000657 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000658 cerr << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000659 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000660 }
661 return Dest;
662}
663
Reid Spencere4d87aa2006-12-23 06:05:41 +0000664static GenericValue executeFCMP_GE(GenericValue Src1, GenericValue Src2,
665 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000666 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000667 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000668 IMPLEMENT_SETCC(>=, Float);
669 IMPLEMENT_SETCC(>=, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000670 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000671 cerr << "Unhandled type for SetGE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000672 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000673 }
674 return Dest;
675}
676
Reid Spencere4d87aa2006-12-23 06:05:41 +0000677static GenericValue executeFCMP_LT(GenericValue Src1, GenericValue Src2,
678 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000679 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000680 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000681 IMPLEMENT_SETCC(<, Float);
682 IMPLEMENT_SETCC(<, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000683 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000684 cerr << "Unhandled type for SetLT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000685 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000686 }
687 return Dest;
688}
689
Reid Spencere4d87aa2006-12-23 06:05:41 +0000690static GenericValue executeFCMP_GT(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000691 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000692 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000693 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000694 IMPLEMENT_SETCC(>, Float);
695 IMPLEMENT_SETCC(>, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000696 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000697 cerr << "Unhandled type for SetGT instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000698 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000699 }
700 return Dest;
701}
702
Reid Spencere4d87aa2006-12-23 06:05:41 +0000703void Interpreter::visitFCmpInst(FCmpInst &I) {
704 ExecutionContext &SF = ECStack.back();
705 const Type *Ty = I.getOperand(0)->getType();
706 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
707 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
708 GenericValue R; // Result
709
710 switch (I.getPredicate()) {
711 case FCmpInst::FCMP_FALSE: R.BoolVal = false;
712 case FCmpInst::FCMP_ORD: R = executeFCMP_EQ(Src1, Src2, Ty); break; ///???
713 case FCmpInst::FCMP_UNO: R = executeFCMP_NE(Src1, Src2, Ty); break; ///???
714 case FCmpInst::FCMP_OEQ:
715 case FCmpInst::FCMP_UEQ: R = executeFCMP_EQ(Src1, Src2, Ty); break;
716 case FCmpInst::FCMP_ONE:
717 case FCmpInst::FCMP_UNE: R = executeFCMP_NE(Src1, Src2, Ty); break;
718 case FCmpInst::FCMP_OLT:
719 case FCmpInst::FCMP_ULT: R = executeFCMP_LT(Src1, Src2, Ty); break;
720 case FCmpInst::FCMP_OGT:
721 case FCmpInst::FCMP_UGT: R = executeFCMP_GT(Src1, Src2, Ty); break;
722 case FCmpInst::FCMP_OLE:
723 case FCmpInst::FCMP_ULE: R = executeFCMP_LE(Src1, Src2, Ty); break;
724 case FCmpInst::FCMP_OGE:
725 case FCmpInst::FCMP_UGE: R = executeFCMP_GE(Src1, Src2, Ty); break;
726 case FCmpInst::FCMP_TRUE: R.BoolVal = true;
727 default:
728 cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
729 abort();
730 }
731
732 SetValue(&I, R, SF);
733}
734
735void Interpreter::visitICmpInst(ICmpInst &I) {
736 ExecutionContext &SF = ECStack.back();
737 const Type *Ty = I.getOperand(0)->getType();
738 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
739 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
740 GenericValue R; // Result
741
742 switch (I.getPredicate()) {
743 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
744 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
745 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
746 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
747 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
748 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
749 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
750 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
751 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
752 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
753 default:
754 cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
755 abort();
756 }
757
758 SetValue(&I, R, SF);
759}
760
761static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
762 GenericValue Src2, const Type *Ty) {
763 GenericValue Result;
764 switch (predicate) {
765 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
766 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
767 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
768 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
769 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
770 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
771 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
772 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
773 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
774 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
775 case FCmpInst::FCMP_ORD: return executeFCMP_EQ(Src1, Src2, Ty); break;
776 case FCmpInst::FCMP_UNO: return executeFCMP_NE(Src1, Src2, Ty); break;
777 case FCmpInst::FCMP_OEQ:
778 case FCmpInst::FCMP_UEQ: return executeFCMP_EQ(Src1, Src2, Ty); break;
779 case FCmpInst::FCMP_ONE:
780 case FCmpInst::FCMP_UNE: return executeFCMP_NE(Src1, Src2, Ty); break;
781 case FCmpInst::FCMP_OLT:
782 case FCmpInst::FCMP_ULT: return executeFCMP_LT(Src1, Src2, Ty); break;
783 case FCmpInst::FCMP_OGT:
784 case FCmpInst::FCMP_UGT: return executeFCMP_GT(Src1, Src2, Ty); break;
785 case FCmpInst::FCMP_OLE:
786 case FCmpInst::FCMP_ULE: return executeFCMP_LE(Src1, Src2, Ty); break;
787 case FCmpInst::FCMP_OGE:
788 case FCmpInst::FCMP_UGE: return executeFCMP_GE(Src1, Src2, Ty); break;
789 case FCmpInst::FCMP_FALSE: {
790 GenericValue Result;
791 Result.BoolVal = false;
792 return Result;
793 }
794 case FCmpInst::FCMP_TRUE: {
795 GenericValue Result;
796 Result.BoolVal = true;
797 return Result;
798 }
799 default:
800 cerr << "Unhandled Cmp predicate\n";
801 abort();
802 }
803}
804
Chris Lattnerd7916e92003-05-10 21:22:39 +0000805void Interpreter::visitBinaryOperator(BinaryOperator &I) {
806 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000807 const Type *Ty = I.getOperand(0)->getType();
808 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
809 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000810 GenericValue R; // Result
811
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000812 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000813 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
814 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
815 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000816 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
817 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
818 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000819 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
820 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
821 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000822 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
823 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
824 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000825 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000826 cerr << "Don't know how to handle this binary operator!\n-->" << I;
Chris Lattner02868352003-04-22 21:22:33 +0000827 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000828 }
829
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000830 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000831}
832
Misha Brukmand1c881a2005-04-21 22:43:08 +0000833static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000834 GenericValue Src3) {
835 return Src1.BoolVal ? Src2 : Src3;
836}
837
838void Interpreter::visitSelectInst(SelectInst &I) {
839 ExecutionContext &SF = ECStack.back();
840 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
841 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
842 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
843 GenericValue R = executeSelectInst(Src1, Src2, Src3);
844 SetValue(&I, R, SF);
845}
846
847
Chris Lattner92101ac2001-08-23 17:05:04 +0000848//===----------------------------------------------------------------------===//
849// Terminator Instruction Implementations
850//===----------------------------------------------------------------------===//
851
Chris Lattnere43db882001-10-27 04:15:57 +0000852void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000853 // runAtExitHandlers() assumes there are no stack frames, but
854 // if exit() was called, then it had a stack frame. Blow away
855 // the stack before interpreting atexit handlers.
856 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000857 runAtExitHandlers ();
858 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000859}
860
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000861/// Pop the last stack frame off of ECStack and then copy the result
862/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000863/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000864/// CallInst if there was a previous stack frame. This method may
865/// invalidate any ECStack iterators you have. This method also takes
866/// care of switching to the normal destination BB, if we are returning
867/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000868///
869void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
870 GenericValue Result) {
871 // Pop the current stack frame.
872 ECStack.pop_back();
873
Misha Brukmand1c881a2005-04-21 22:43:08 +0000874 if (ECStack.empty()) { // Finished main. Put result into exit code...
875 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000876 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000877 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000878 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000879 }
880 } else {
881 // If we have a previous stack frame, and we have a previous call,
882 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000883 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000884 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000885 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000886 SetValue(I, Result, CallingSF);
887 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
888 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000889 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000890 }
891 }
892}
893
Chris Lattnerd7916e92003-05-10 21:22:39 +0000894void Interpreter::visitReturnInst(ReturnInst &I) {
895 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000896 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000897 GenericValue Result;
898
899 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000900 if (I.getNumOperands()) {
901 RetTy = I.getReturnValue()->getType();
902 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000903 }
904
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000905 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000906}
907
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000908void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000909 // Unwind stack
910 Instruction *Inst;
911 do {
912 ECStack.pop_back ();
913 if (ECStack.empty ())
914 abort ();
915 Inst = ECStack.back ().Caller.getInstruction ();
916 } while (!(Inst && isa<InvokeInst> (Inst)));
917
918 // Return from invoke
919 ExecutionContext &InvokingSF = ECStack.back ();
920 InvokingSF.Caller = CallSite ();
921
922 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000923 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000924}
925
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000926void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000927 cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000928 abort();
929}
930
Chris Lattnerd7916e92003-05-10 21:22:39 +0000931void Interpreter::visitBranchInst(BranchInst &I) {
932 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000933 BasicBlock *Dest;
934
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000935 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
936 if (!I.isUnconditional()) {
937 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000938 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000939 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000940 }
Chris Lattner77113b62003-05-10 20:21:16 +0000941 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000942}
943
Chris Lattnerd7916e92003-05-10 21:22:39 +0000944void Interpreter::visitSwitchInst(SwitchInst &I) {
945 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000946 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
947 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000948
949 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000950 BasicBlock *Dest = 0;
951 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Reid Spencere4d87aa2006-12-23 06:05:41 +0000952 if (executeICMP_EQ(CondVal,
953 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000954 Dest = cast<BasicBlock>(I.getOperand(i+1));
955 break;
956 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000957
Chris Lattner09e93922003-04-22 20:34:47 +0000958 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000959 SwitchToNewBasicBlock(Dest, SF);
960}
961
962// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
963// This function handles the actual updating of block and instruction iterators
964// as well as execution of all of the PHI nodes in the destination block.
965//
966// This method does this because all of the PHI nodes must be executed
967// atomically, reading their inputs before any of the results are updated. Not
968// doing this can cause problems if the PHI nodes depend on other PHI nodes for
969// their inputs. If the input PHI node is updated before it is read, incorrect
970// results can happen. Thus we use a two phase approach.
971//
972void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
973 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
974 SF.CurBB = Dest; // Update CurBB to branch destination
975 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
976
977 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
978
979 // Loop over all of the PHI nodes in the current block, reading their inputs.
980 std::vector<GenericValue> ResultValues;
981
982 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
983 // Search for the value corresponding to this previous bb...
984 int i = PN->getBasicBlockIndex(PrevBB);
985 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
986 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000987
Chris Lattner77113b62003-05-10 20:21:16 +0000988 // Save the incoming value for this PHI node...
989 ResultValues.push_back(getOperandValue(IncomingValue, SF));
990 }
991
992 // Now loop over all of the PHI nodes setting their values...
993 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000994 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
995 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000996 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000997 }
Chris Lattner09e93922003-04-22 20:34:47 +0000998}
999
Chris Lattner92101ac2001-08-23 17:05:04 +00001000//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +00001001// Memory Instruction Implementations
1002//===----------------------------------------------------------------------===//
1003
Chris Lattnerd7916e92003-05-10 21:22:39 +00001004void Interpreter::visitAllocationInst(AllocationInst &I) {
1005 ExecutionContext &SF = ECStack.back();
1006
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001007 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +00001008
Chris Lattnercc82cc12002-04-28 21:57:33 +00001009 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001010 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +00001011
1012 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +00001013 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +00001014
Chris Lattnerfe11a972002-12-23 23:59:41 +00001015 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001016 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001017 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001018
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001019 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +00001020 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +00001021}
1022
Chris Lattnerd7916e92003-05-10 21:22:39 +00001023void Interpreter::visitFreeInst(FreeInst &I) {
1024 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001025 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
1026 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +00001027 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +00001028 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +00001029}
1030
Chris Lattnera34c5682002-08-27 22:33:45 +00001031// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +00001032//
Chris Lattner4af6de82003-11-25 20:44:56 +00001033GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001034 gep_type_iterator E,
1035 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001036 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +00001037 "Cannot getElementOffset of a nonpointer type!");
1038
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001039 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +00001040
1041 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +00001042 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +00001043 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001044
Reid Spencerb83eb642006-10-20 07:07:24 +00001045 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1046 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +00001047
Chris Lattnerd5644962005-01-08 20:05:34 +00001048 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +00001049 } else {
1050 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +00001051 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +00001052 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1053
1054 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001055 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +00001056 default: assert(0 && "Illegal getelementptr index for sequential type!");
1057 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
1058 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
1059 case Type::IntTyID: Idx = IdxGV.IntVal; break;
1060 case Type::LongTyID: Idx = IdxGV.LongVal; break;
1061 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
1062 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
1063 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
1064 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
1065 }
Chris Lattnerd5644962005-01-08 20:05:34 +00001066 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001067 }
Chris Lattner95c3af52001-10-29 19:32:19 +00001068 }
1069
Chris Lattnera34c5682002-08-27 22:33:45 +00001070 GenericValue Result;
1071 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
1072 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +00001073}
1074
Chris Lattnerd7916e92003-05-10 21:22:39 +00001075void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1076 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +00001077 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +00001078 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +00001079}
1080
Chris Lattnerd7916e92003-05-10 21:22:39 +00001081void Interpreter::visitLoadInst(LoadInst &I) {
1082 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001083 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001084 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +00001085 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001086 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001087}
1088
Chris Lattnerd7916e92003-05-10 21:22:39 +00001089void Interpreter::visitStoreInst(StoreInst &I) {
1090 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +00001091 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1092 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001093 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +00001094 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +00001095}
1096
Chris Lattner86660982001-08-27 05:16:50 +00001097//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001098// Miscellaneous Instruction Implementations
1099//===----------------------------------------------------------------------===//
1100
Brian Gaekefea483d2003-11-07 20:04:22 +00001101void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001102 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +00001103
1104 // Check to see if this is an intrinsic function call...
1105 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +00001106 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +00001107 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001108 case Intrinsic::not_intrinsic:
1109 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001110 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001111 GenericValue ArgIndex;
1112 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1113 ArgIndex.UIntPairVal.second = 0;
1114 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001115 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001116 }
Chris Lattner317201d2004-03-13 00:24:00 +00001117 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001118 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001119 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001120 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1121 return;
1122 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001123 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001124 // class to transform it into hopefully tasty LLVM code.
1125 //
1126 Instruction *Prev = CS.getInstruction()->getPrev();
1127 BasicBlock *Parent = CS.getInstruction()->getParent();
1128 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1129
1130 // Restore the CurInst pointer to the first instruction newly inserted, if
1131 // any.
1132 if (!Prev) {
1133 SF.CurInst = Parent->begin();
1134 } else {
1135 SF.CurInst = Prev;
1136 ++SF.CurInst;
1137 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001138 return;
Chris Lattner73011782003-12-28 09:44:37 +00001139 }
1140
Brian Gaekefea483d2003-11-07 20:04:22 +00001141 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001142 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001143 const unsigned NumArgs = SF.Caller.arg_size();
1144 ArgVals.reserve(NumArgs);
1145 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1146 e = SF.Caller.arg_end(); i != e; ++i) {
1147 Value *V = *i;
1148 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001149 // Promote all integral types whose size is < sizeof(int) into ints. We do
1150 // this by zero or sign extending the value as appropriate according to the
1151 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +00001152 const Type *Ty = V->getType();
1153 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +00001154 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +00001155 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +00001156 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +00001157 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +00001158 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +00001159 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +00001160 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +00001161 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +00001162 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +00001163 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +00001164 else
Misha Brukmand1c881a2005-04-21 22:43:08 +00001165 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +00001166 }
1167 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001168
Misha Brukmand1c881a2005-04-21 22:43:08 +00001169 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001170 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001171 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001172 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001173}
1174
Chris Lattner86660982001-08-27 05:16:50 +00001175#define IMPLEMENT_SHIFT(OP, TY) \
1176 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
1177
Reid Spencer3822ff52006-11-08 06:47:33 +00001178#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
1179 case Type::TY2##TyID: \
1180 IMPLEMENT_SHIFT(OP, TY1)
1181
Brian Gaeke63438cc2003-12-11 00:22:59 +00001182static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
1183 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001184 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001185 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +00001186 IMPLEMENT_SHIFT(<<, UByte);
1187 IMPLEMENT_SHIFT(<<, SByte);
1188 IMPLEMENT_SHIFT(<<, UShort);
1189 IMPLEMENT_SHIFT(<<, Short);
1190 IMPLEMENT_SHIFT(<<, UInt);
1191 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +00001192 IMPLEMENT_SHIFT(<<, ULong);
1193 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +00001194 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001195 cerr << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +00001196 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001197 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001198}
1199
Reid Spencer3822ff52006-11-08 06:47:33 +00001200static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
1201 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +00001202 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001203 switch (Ty->getTypeID()) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001204 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
1205 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
1206 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
1207 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
Chris Lattner86660982001-08-27 05:16:50 +00001208 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001209 cerr << "Unhandled type for LShr instruction: " << *Ty << "\n";
Reid Spencer3822ff52006-11-08 06:47:33 +00001210 abort();
1211 }
1212 return Dest;
1213}
1214
1215static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
1216 const Type *Ty) {
1217 GenericValue Dest;
1218 switch (Ty->getTypeID()) {
1219 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
1220 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
1221 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
1222 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
1223 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001224 cerr << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +00001225 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001226 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001227 return Dest;
1228}
1229
1230void Interpreter::visitShl(ShiftInst &I) {
1231 ExecutionContext &SF = ECStack.back();
1232 const Type *Ty = I.getOperand(0)->getType();
1233 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1234 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1235 GenericValue Dest;
1236 Dest = executeShlInst (Src1, Src2, Ty);
1237 SetValue(&I, Dest, SF);
1238}
1239
Reid Spencer3822ff52006-11-08 06:47:33 +00001240void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001241 ExecutionContext &SF = ECStack.back();
1242 const Type *Ty = I.getOperand(0)->getType();
1243 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1244 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1245 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001246 Dest = executeLShrInst (Src1, Src2, Ty);
1247 SetValue(&I, Dest, SF);
1248}
1249
1250void Interpreter::visitAShr(ShiftInst &I) {
1251 ExecutionContext &SF = ECStack.back();
1252 const Type *Ty = I.getOperand(0)->getType();
1253 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1254 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1255 GenericValue Dest;
1256 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001257 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001258}
1259
Reid Spencer3da59db2006-11-27 01:05:10 +00001260#define IMPLEMENT_CAST_START \
1261 switch (DstTy->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +00001262
Reid Spencer3da59db2006-11-27 01:05:10 +00001263#define IMPLEMENT_CAST(DTY, DCTY, STY) \
1264 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
1265
1266#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
Chris Lattner86660982001-08-27 05:16:50 +00001267 case Type::DESTTY##TyID: \
Reid Spencer3da59db2006-11-27 01:05:10 +00001268 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001269 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001270 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1271 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1272 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1273 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1274 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001275 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1276 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001277 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001278 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); \
Chris Lattner86660982001-08-27 05:16:50 +00001279 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
Reid Spencer3da59db2006-11-27 01:05:10 +00001280 IMPLEMENT_CAST(DESTTY, DESTCTY, Double) \
1281 default: \
Bill Wendlinge8156192006-12-07 01:30:32 +00001282 cerr << "Unhandled cast: " \
Reid Spencer3da59db2006-11-27 01:05:10 +00001283 << *SrcTy << " to " << *DstTy << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001284 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001285 } \
1286 break
1287
Reid Spencer3da59db2006-11-27 01:05:10 +00001288#define IMPLEMENT_CAST_END \
Bill Wendlinge8156192006-12-07 01:30:32 +00001289 default: cerr \
Reid Spencer3da59db2006-11-27 01:05:10 +00001290 << "Unhandled dest type for cast instruction: " \
1291 << *DstTy << "\n"; \
1292 abort(); \
1293 }
Chris Lattner86660982001-08-27 05:16:50 +00001294
Reid Spencer3da59db2006-11-27 01:05:10 +00001295GenericValue Interpreter::executeCastOperation(Instruction::CastOps opcode,
1296 Value *SrcVal, const Type *DstTy,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001297 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001298 const Type *SrcTy = SrcVal->getType();
1299 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001300
Reid Spencer3da59db2006-11-27 01:05:10 +00001301 if (opcode == Instruction::Trunc && DstTy->getTypeID() == Type::BoolTyID) {
1302 // For truncations to bool, we must clear the high order bits of the source
1303 switch (SrcTy->getTypeID()) {
1304 case Type::BoolTyID: Src.BoolVal &= 1; break;
1305 case Type::SByteTyID: Src.SByteVal &= 1; break;
1306 case Type::UByteTyID: Src.UByteVal &= 1; break;
1307 case Type::ShortTyID: Src.ShortVal &= 1; break;
1308 case Type::UShortTyID: Src.UShortVal &= 1; break;
1309 case Type::IntTyID: Src.IntVal &= 1; break;
1310 case Type::UIntTyID: Src.UIntVal &= 1; break;
1311 case Type::LongTyID: Src.LongVal &= 1; break;
1312 case Type::ULongTyID: Src.ULongVal &= 1; break;
1313 default:
1314 assert(0 && "Can't trunc a non-integer!");
1315 break;
1316 }
1317 } else if (opcode == Instruction::SExt &&
1318 SrcTy->getTypeID() == Type::BoolTyID) {
1319 // For sign extension from bool, we must extend the source bits.
1320 SrcTy = Type::LongTy;
1321 Src.LongVal = 0 - Src.BoolVal;
Chris Lattner86660982001-08-27 05:16:50 +00001322 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001323
Reid Spencer3da59db2006-11-27 01:05:10 +00001324 switch (opcode) {
1325 case Instruction::Trunc: // src integer, dest integral (can't be long)
1326 IMPLEMENT_CAST_START
1327 IMPLEMENT_CAST_CASE(Bool , (bool));
1328 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1329 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1330 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1331 IMPLEMENT_CAST_CASE(Short , ( signed short));
1332 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1333 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1334 IMPLEMENT_CAST_END
1335 break;
1336 case Instruction::ZExt: // src integral (can't be long), dest integer
1337 IMPLEMENT_CAST_START
1338 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1339 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1340 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1341 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1342 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1343 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1344 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1345 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1346 IMPLEMENT_CAST_END
1347 break;
1348 case Instruction::SExt: // src integral (can't be long), dest integer
1349 IMPLEMENT_CAST_START
1350 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1351 IMPLEMENT_CAST_CASE(SByte , (signed char));
1352 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1353 IMPLEMENT_CAST_CASE(Short , (signed short));
1354 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1355 IMPLEMENT_CAST_CASE(Int , (signed int));
1356 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1357 IMPLEMENT_CAST_CASE(Long , (int64_t));
1358 IMPLEMENT_CAST_END
1359 break;
1360 case Instruction::FPTrunc: // src double, dest float
1361 IMPLEMENT_CAST_START
1362 IMPLEMENT_CAST_CASE(Float , (float));
1363 IMPLEMENT_CAST_END
1364 break;
1365 case Instruction::FPExt: // src float, dest double
1366 IMPLEMENT_CAST_START
1367 IMPLEMENT_CAST_CASE(Double , (double));
1368 IMPLEMENT_CAST_END
1369 break;
1370 case Instruction::UIToFP: // src integral, dest floating
1371 IMPLEMENT_CAST_START
1372 IMPLEMENT_CAST_CASE(Float , (float)(uint64_t));
1373 IMPLEMENT_CAST_CASE(Double , (double)(uint64_t));
1374 IMPLEMENT_CAST_END
1375 break;
1376 case Instruction::SIToFP: // src integeral, dest floating
1377 IMPLEMENT_CAST_START
1378 IMPLEMENT_CAST_CASE(Float , (float)(int64_t));
1379 IMPLEMENT_CAST_CASE(Double , (double)(int64_t));
1380 IMPLEMENT_CAST_END
1381 break;
1382 case Instruction::FPToUI: // src floating, dest integral
1383 IMPLEMENT_CAST_START
1384 IMPLEMENT_CAST_CASE(Bool , (bool));
1385 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1386 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1387 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1388 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1389 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1390 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int ));
1391 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1392 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1393 IMPLEMENT_CAST_END
1394 break;
1395 case Instruction::FPToSI: // src floating, dest integral
1396 IMPLEMENT_CAST_START
1397 IMPLEMENT_CAST_CASE(Bool , (bool));
1398 IMPLEMENT_CAST_CASE(UByte , (unsigned char)(signed char));
1399 IMPLEMENT_CAST_CASE(SByte , (signed char));
1400 IMPLEMENT_CAST_CASE(UShort , (unsigned short)(signed short));
1401 IMPLEMENT_CAST_CASE(Short , (signed short));
1402 IMPLEMENT_CAST_CASE(UInt , (unsigned int )(signed int));
1403 IMPLEMENT_CAST_CASE(Int , (signed int));
1404 IMPLEMENT_CAST_CASE(ULong , (uint64_t)(int64_t));
1405 IMPLEMENT_CAST_CASE(Long , (int64_t));
1406 IMPLEMENT_CAST_END
1407 break;
1408 case Instruction::PtrToInt: // src pointer, dest integral
1409 IMPLEMENT_CAST_START
1410 IMPLEMENT_CAST_CASE(Bool , (bool));
1411 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1412 IMPLEMENT_CAST_CASE(SByte , (signed char)(unsigned char));
1413 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1414 IMPLEMENT_CAST_CASE(Short , (signed short)(unsigned short));
1415 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1416 IMPLEMENT_CAST_CASE(Int , (signed int)(unsigned int));
1417 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1418 IMPLEMENT_CAST_CASE(Long , (int64_t)(uint64_t));
1419 IMPLEMENT_CAST_END
1420 break;
1421 case Instruction::IntToPtr: // src integral, dest pointer
1422 IMPLEMENT_CAST_START
1423 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1424 IMPLEMENT_CAST_END
1425 break;
1426 case Instruction::BitCast: // src any, dest any (same size)
1427 IMPLEMENT_CAST_START
1428 IMPLEMENT_CAST_CASE(Bool , (bool));
1429 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1430 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1431 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1432 IMPLEMENT_CAST_CASE(Short , ( signed short));
1433 IMPLEMENT_CAST_CASE(UInt , (unsigned int));
1434 IMPLEMENT_CAST_CASE(Int , ( signed int));
1435 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1436 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1437 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
1438 IMPLEMENT_CAST_CASE(Float , (float));
1439 IMPLEMENT_CAST_CASE(Double , (double));
1440 IMPLEMENT_CAST_END
1441 break;
1442 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001443 cerr << "Invalid cast opcode for cast instruction: " << opcode << "\n";
Reid Spencer3da59db2006-11-27 01:05:10 +00001444 abort();
1445 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001446 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001447}
Chris Lattner92101ac2001-08-23 17:05:04 +00001448
Chris Lattnerd7916e92003-05-10 21:22:39 +00001449void Interpreter::visitCastInst(CastInst &I) {
1450 ExecutionContext &SF = ECStack.back();
Reid Spencer3da59db2006-11-27 01:05:10 +00001451 SetValue(&I, executeCastOperation(I.getOpcode(), I.getOperand(0),
1452 I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001453}
Chris Lattner92101ac2001-08-23 17:05:04 +00001454
Brian Gaekec1a2be12003-11-07 21:20:47 +00001455#define IMPLEMENT_VAARG(TY) \
1456 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1457
1458void Interpreter::visitVAArgInst(VAArgInst &I) {
1459 ExecutionContext &SF = ECStack.back();
1460
Brian Gaeke9d20b712004-02-25 23:01:48 +00001461 // Get the incoming valist parameter. LLI treats the valist as a
1462 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001463 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001464 GenericValue Dest;
1465 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001466 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001467 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001468 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001469 IMPLEMENT_VAARG(UByte);
1470 IMPLEMENT_VAARG(SByte);
1471 IMPLEMENT_VAARG(UShort);
1472 IMPLEMENT_VAARG(Short);
1473 IMPLEMENT_VAARG(UInt);
1474 IMPLEMENT_VAARG(Int);
1475 IMPLEMENT_VAARG(ULong);
1476 IMPLEMENT_VAARG(Long);
1477 IMPLEMENT_VAARG(Pointer);
1478 IMPLEMENT_VAARG(Float);
1479 IMPLEMENT_VAARG(Double);
1480 IMPLEMENT_VAARG(Bool);
1481 default:
Bill Wendlinge8156192006-12-07 01:30:32 +00001482 cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Brian Gaekec1a2be12003-11-07 21:20:47 +00001483 abort();
1484 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001485
Brian Gaekec1a2be12003-11-07 21:20:47 +00001486 // Set the Value of this Instruction.
1487 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001488
1489 // Move the pointer to the next vararg.
1490 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001491}
1492
Chris Lattner92101ac2001-08-23 17:05:04 +00001493//===----------------------------------------------------------------------===//
1494// Dispatch and Execution Code
1495//===----------------------------------------------------------------------===//
1496
Chris Lattner92101ac2001-08-23 17:05:04 +00001497//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001498// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001499//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001500void Interpreter::callFunction(Function *F,
1501 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001502 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1503 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1504 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001505 // Make a new stack frame... and fill it in.
1506 ECStack.push_back(ExecutionContext());
1507 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001508 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001509
1510 // Special handling for external functions.
1511 if (F->isExternal()) {
1512 GenericValue Result = callExternalFunction (F, ArgVals);
1513 // Simulate a 'ret' instruction of the appropriate type.
1514 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1515 return;
1516 }
1517
1518 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001519 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001520 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001521
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001522 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001523 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001524 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001525 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001526
1527 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001528 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001529 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001530 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001531
1532 // Handle varargs arguments...
1533 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001534}
1535
Chris Lattner92101ac2001-08-23 17:05:04 +00001536void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001537 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001538 // Interpret a single instruction & increment the "PC".
1539 ExecutionContext &SF = ECStack.back(); // Current stack frame
1540 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001541
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001542 // Track the number of dynamic instructions executed.
1543 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001544
Bill Wendling480f0932006-11-27 23:54:50 +00001545 DOUT << "About to interpret: " << I;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001546 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001547 }
1548}