blob: 928a819cd00e96fdecad4fca960889149e66a2ab [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner73011782003-12-28 09:44:37 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Chris Lattner30483732004-06-20 07:49:54 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
Chris Lattner4af6de82003-11-25 20:44:56 +000023using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000024
Chris Lattnerbbdabce2002-12-08 05:51:08 +000025namespace {
26 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattnerbbdabce2002-12-08 05:51:08 +000027
Chris Lattner4af6de82003-11-25 20:44:56 +000028 Interpreter *TheEE = 0;
29}
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner73011782003-12-28 09:44:37 +000031
Chris Lattner2e42d3a2001-10-15 05:51:48 +000032//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000033// Value Manipulation code
34//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000035
Misha Brukmand1c881a2005-04-21 22:43:08 +000036static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
37 const Type *Ty);
38static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
39 const Type *Ty);
40static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
41 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000042static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
43 const Type *Ty);
44static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
45 const Type *Ty);
46static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
47 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000048static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
49 const Type *Ty);
50static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
51 const Type *Ty);
52static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
53 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000054static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
55 const Type *Ty);
56static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
57 const Type *Ty);
58static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
59 const Type *Ty);
60static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
61 const Type *Ty);
62static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
63 const Type *Ty);
64static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
65 const Type *Ty);
66static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
67 const Type *Ty);
68static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
69 const Type *Ty);
70static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
71 const Type *Ty);
72static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
73 const Type *Ty);
Reid Spencer3822ff52006-11-08 06:47:33 +000074static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
75 const Type *Ty);
76static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
77 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000078static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000079 GenericValue Src3);
80
Brian Gaeke63438cc2003-12-11 00:22:59 +000081GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
82 ExecutionContext &SF) {
83 switch (CE->getOpcode()) {
84 case Instruction::Cast:
85 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
86 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());
137 case Instruction::SetEQ:
138 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
139 getOperandValue(CE->getOperand(1), SF),
140 CE->getOperand(0)->getType());
141 case Instruction::SetNE:
142 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
143 getOperandValue(CE->getOperand(1), SF),
144 CE->getOperand(0)->getType());
145 case Instruction::SetLE:
146 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
147 getOperandValue(CE->getOperand(1), SF),
148 CE->getOperand(0)->getType());
149 case Instruction::SetGE:
150 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
151 getOperandValue(CE->getOperand(1), SF),
152 CE->getOperand(0)->getType());
153 case Instruction::SetLT:
154 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
155 getOperandValue(CE->getOperand(1), SF),
156 CE->getOperand(0)->getType());
157 case Instruction::SetGT:
158 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
159 getOperandValue(CE->getOperand(1), SF),
160 CE->getOperand(0)->getType());
161 case Instruction::Shl:
162 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
163 getOperandValue(CE->getOperand(1), SF),
164 CE->getOperand(0)->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +0000165 case Instruction::LShr:
166 return executeLShrInst(getOperandValue(CE->getOperand(0), SF),
167 getOperandValue(CE->getOperand(1), SF),
168 CE->getOperand(0)->getType());
169 case Instruction::AShr:
170 return executeAShrInst(getOperandValue(CE->getOperand(0), SF),
171 getOperandValue(CE->getOperand(1), SF),
172 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000173 case Instruction::Select:
174 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
175 getOperandValue(CE->getOperand(1), SF),
176 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000177 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000178 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000179 abort();
180 return GenericValue();
181 }
182}
Chris Lattnera34c5682002-08-27 22:33:45 +0000183
Brian Gaeke29794cb2003-09-05 18:55:03 +0000184GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000185 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000186 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000187 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000188 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000189 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000190 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000191 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000192 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000193 }
194}
195
Chris Lattner39bb5b42001-10-15 13:25:40 +0000196static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000197 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000198}
199
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000200void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000201 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000202}
203
Chris Lattner2adcd832002-05-03 19:52:30 +0000204//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000205// Binary Instruction Implementations
206//===----------------------------------------------------------------------===//
207
208#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
209 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
210
Misha Brukmand1c881a2005-04-21 22:43:08 +0000211static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
212 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000213 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000214 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000215 IMPLEMENT_BINARY_OPERATOR(+, UByte);
216 IMPLEMENT_BINARY_OPERATOR(+, SByte);
217 IMPLEMENT_BINARY_OPERATOR(+, UShort);
218 IMPLEMENT_BINARY_OPERATOR(+, Short);
219 IMPLEMENT_BINARY_OPERATOR(+, UInt);
220 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000221 IMPLEMENT_BINARY_OPERATOR(+, ULong);
222 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000223 IMPLEMENT_BINARY_OPERATOR(+, Float);
224 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000225 default:
Chris Lattner02868352003-04-22 21:22:33 +0000226 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
227 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000228 }
229 return Dest;
230}
231
Misha Brukmand1c881a2005-04-21 22:43:08 +0000232static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
233 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000234 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000235 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000236 IMPLEMENT_BINARY_OPERATOR(-, UByte);
237 IMPLEMENT_BINARY_OPERATOR(-, SByte);
238 IMPLEMENT_BINARY_OPERATOR(-, UShort);
239 IMPLEMENT_BINARY_OPERATOR(-, Short);
240 IMPLEMENT_BINARY_OPERATOR(-, UInt);
241 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000242 IMPLEMENT_BINARY_OPERATOR(-, ULong);
243 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000244 IMPLEMENT_BINARY_OPERATOR(-, Float);
245 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000246 default:
Chris Lattner02868352003-04-22 21:22:33 +0000247 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
248 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000249 }
250 return Dest;
251}
252
Misha Brukmand1c881a2005-04-21 22:43:08 +0000253static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
254 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000255 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000256 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000257 IMPLEMENT_BINARY_OPERATOR(*, UByte);
258 IMPLEMENT_BINARY_OPERATOR(*, SByte);
259 IMPLEMENT_BINARY_OPERATOR(*, UShort);
260 IMPLEMENT_BINARY_OPERATOR(*, Short);
261 IMPLEMENT_BINARY_OPERATOR(*, UInt);
262 IMPLEMENT_BINARY_OPERATOR(*, Int);
263 IMPLEMENT_BINARY_OPERATOR(*, ULong);
264 IMPLEMENT_BINARY_OPERATOR(*, Long);
265 IMPLEMENT_BINARY_OPERATOR(*, Float);
266 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000267 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000268 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000269 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000270 }
271 return Dest;
272}
273
Reid Spencerfe855262006-11-01 03:41:05 +0000274#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
275 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
276
Reid Spencer1628cec2006-10-26 06:15:43 +0000277static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
278 const Type *Ty) {
279 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000280 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000281 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
282 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
283 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
284 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000285 default:
286 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
287 abort();
288 }
289 return Dest;
290}
291
292static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
293 const Type *Ty) {
294 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000295 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000296 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
297 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
298 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
299 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000300 default:
301 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
302 abort();
303 }
304 return Dest;
305}
306
307static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000308 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000309 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000310 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000311 IMPLEMENT_BINARY_OPERATOR(/, Float);
312 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000313 default:
Chris Lattner02868352003-04-22 21:22:33 +0000314 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
315 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000316 }
317 return Dest;
318}
319
Reid Spencer0a783f72006-11-02 01:53:59 +0000320static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000321 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000322 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000323 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000324 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
325 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
326 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
327 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
328 default:
329 std::cout << "Unhandled type for URem instruction: " << *Ty << "\n";
330 abort();
331 }
332 return Dest;
333}
334
335static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
336 const Type *Ty) {
337 GenericValue Dest;
338 switch (Ty->getTypeID()) {
339 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
340 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
341 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
342 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
343 default:
344 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
345 abort();
346 }
347 return Dest;
348}
349
350static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
351 const Type *Ty) {
352 GenericValue Dest;
353 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000354 case Type::FloatTyID:
355 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
356 break;
357 case Type::DoubleTyID:
358 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
359 break;
360 default:
Chris Lattner02868352003-04-22 21:22:33 +0000361 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
362 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000363 }
364 return Dest;
365}
366
Misha Brukmand1c881a2005-04-21 22:43:08 +0000367static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
368 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000369 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000370 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000371 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000372 IMPLEMENT_BINARY_OPERATOR(&, UByte);
373 IMPLEMENT_BINARY_OPERATOR(&, SByte);
374 IMPLEMENT_BINARY_OPERATOR(&, UShort);
375 IMPLEMENT_BINARY_OPERATOR(&, Short);
376 IMPLEMENT_BINARY_OPERATOR(&, UInt);
377 IMPLEMENT_BINARY_OPERATOR(&, Int);
378 IMPLEMENT_BINARY_OPERATOR(&, ULong);
379 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000380 default:
Chris Lattner02868352003-04-22 21:22:33 +0000381 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
382 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000383 }
384 return Dest;
385}
386
Misha Brukmand1c881a2005-04-21 22:43:08 +0000387static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000388 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000389 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000390 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000391 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000392 IMPLEMENT_BINARY_OPERATOR(|, UByte);
393 IMPLEMENT_BINARY_OPERATOR(|, SByte);
394 IMPLEMENT_BINARY_OPERATOR(|, UShort);
395 IMPLEMENT_BINARY_OPERATOR(|, Short);
396 IMPLEMENT_BINARY_OPERATOR(|, UInt);
397 IMPLEMENT_BINARY_OPERATOR(|, Int);
398 IMPLEMENT_BINARY_OPERATOR(|, ULong);
399 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000400 default:
Chris Lattner02868352003-04-22 21:22:33 +0000401 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
402 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000403 }
404 return Dest;
405}
406
Misha Brukmand1c881a2005-04-21 22:43:08 +0000407static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000408 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000409 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000410 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000411 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000412 IMPLEMENT_BINARY_OPERATOR(^, UByte);
413 IMPLEMENT_BINARY_OPERATOR(^, SByte);
414 IMPLEMENT_BINARY_OPERATOR(^, UShort);
415 IMPLEMENT_BINARY_OPERATOR(^, Short);
416 IMPLEMENT_BINARY_OPERATOR(^, UInt);
417 IMPLEMENT_BINARY_OPERATOR(^, Int);
418 IMPLEMENT_BINARY_OPERATOR(^, ULong);
419 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000420 default:
Chris Lattner02868352003-04-22 21:22:33 +0000421 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
422 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000423 }
424 return Dest;
425}
426
Chris Lattner92101ac2001-08-23 17:05:04 +0000427#define IMPLEMENT_SETCC(OP, TY) \
428 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
429
Chris Lattnerfd506f52003-04-23 19:55:35 +0000430// Handle pointers specially because they must be compared with only as much
431// width as the host has. We _do not_ want to be comparing 64 bit values when
432// running on a 32-bit target, otherwise the upper 32 bits might mess up
433// comparisons if they contain garbage.
434#define IMPLEMENT_POINTERSETCC(OP) \
435 case Type::PointerTyID: \
436 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
437 (void*)(intptr_t)Src2.PointerVal; break
438
Misha Brukmand1c881a2005-04-21 22:43:08 +0000439static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
440 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000441 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000442 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000443 IMPLEMENT_SETCC(==, UByte);
444 IMPLEMENT_SETCC(==, SByte);
445 IMPLEMENT_SETCC(==, UShort);
446 IMPLEMENT_SETCC(==, Short);
447 IMPLEMENT_SETCC(==, UInt);
448 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000449 IMPLEMENT_SETCC(==, ULong);
450 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 IMPLEMENT_SETCC(==, Float);
452 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000453 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000454 default:
Chris Lattner02868352003-04-22 21:22:33 +0000455 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
456 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 }
458 return Dest;
459}
460
Misha Brukmand1c881a2005-04-21 22:43:08 +0000461static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
462 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000463 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000464 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000465 IMPLEMENT_SETCC(!=, UByte);
466 IMPLEMENT_SETCC(!=, SByte);
467 IMPLEMENT_SETCC(!=, UShort);
468 IMPLEMENT_SETCC(!=, Short);
469 IMPLEMENT_SETCC(!=, UInt);
470 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000471 IMPLEMENT_SETCC(!=, ULong);
472 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 IMPLEMENT_SETCC(!=, Float);
474 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000475 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000476
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 default:
Chris Lattner02868352003-04-22 21:22:33 +0000478 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
479 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 }
481 return Dest;
482}
483
Misha Brukmand1c881a2005-04-21 22:43:08 +0000484static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
485 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000486 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000487 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000488 IMPLEMENT_SETCC(<=, UByte);
489 IMPLEMENT_SETCC(<=, SByte);
490 IMPLEMENT_SETCC(<=, UShort);
491 IMPLEMENT_SETCC(<=, Short);
492 IMPLEMENT_SETCC(<=, UInt);
493 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000494 IMPLEMENT_SETCC(<=, ULong);
495 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 IMPLEMENT_SETCC(<=, Float);
497 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000498 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000500 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000501 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 }
503 return Dest;
504}
505
Misha Brukmand1c881a2005-04-21 22:43:08 +0000506static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
507 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000509 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000510 IMPLEMENT_SETCC(>=, UByte);
511 IMPLEMENT_SETCC(>=, SByte);
512 IMPLEMENT_SETCC(>=, UShort);
513 IMPLEMENT_SETCC(>=, Short);
514 IMPLEMENT_SETCC(>=, UInt);
515 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000516 IMPLEMENT_SETCC(>=, ULong);
517 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000518 IMPLEMENT_SETCC(>=, Float);
519 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000520 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 default:
Chris Lattner02868352003-04-22 21:22:33 +0000522 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
523 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000524 }
525 return Dest;
526}
527
Misha Brukmand1c881a2005-04-21 22:43:08 +0000528static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
529 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000530 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000531 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000532 IMPLEMENT_SETCC(<, UByte);
533 IMPLEMENT_SETCC(<, SByte);
534 IMPLEMENT_SETCC(<, UShort);
535 IMPLEMENT_SETCC(<, Short);
536 IMPLEMENT_SETCC(<, UInt);
537 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000538 IMPLEMENT_SETCC(<, ULong);
539 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000540 IMPLEMENT_SETCC(<, Float);
541 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000542 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000543 default:
Chris Lattner02868352003-04-22 21:22:33 +0000544 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
545 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000546 }
547 return Dest;
548}
549
Misha Brukmand1c881a2005-04-21 22:43:08 +0000550static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
551 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000552 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000553 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000554 IMPLEMENT_SETCC(>, UByte);
555 IMPLEMENT_SETCC(>, SByte);
556 IMPLEMENT_SETCC(>, UShort);
557 IMPLEMENT_SETCC(>, Short);
558 IMPLEMENT_SETCC(>, UInt);
559 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000560 IMPLEMENT_SETCC(>, ULong);
561 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 IMPLEMENT_SETCC(>, Float);
563 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000564 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000565 default:
Chris Lattner02868352003-04-22 21:22:33 +0000566 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
567 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000568 }
569 return Dest;
570}
571
Chris Lattnerd7916e92003-05-10 21:22:39 +0000572void Interpreter::visitBinaryOperator(BinaryOperator &I) {
573 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000574 const Type *Ty = I.getOperand(0)->getType();
575 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
576 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000577 GenericValue R; // Result
578
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000579 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000580 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
581 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
582 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000583 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
584 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
585 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000586 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
587 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
588 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000589 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
590 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
591 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
592 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
593 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
594 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
595 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
596 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
597 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000598 default:
Chris Lattner02868352003-04-22 21:22:33 +0000599 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
600 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000601 }
602
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000603 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000604}
605
Misha Brukmand1c881a2005-04-21 22:43:08 +0000606static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000607 GenericValue Src3) {
608 return Src1.BoolVal ? Src2 : Src3;
609}
610
611void Interpreter::visitSelectInst(SelectInst &I) {
612 ExecutionContext &SF = ECStack.back();
613 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
614 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
615 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
616 GenericValue R = executeSelectInst(Src1, Src2, Src3);
617 SetValue(&I, R, SF);
618}
619
620
Chris Lattner92101ac2001-08-23 17:05:04 +0000621//===----------------------------------------------------------------------===//
622// Terminator Instruction Implementations
623//===----------------------------------------------------------------------===//
624
Chris Lattnere43db882001-10-27 04:15:57 +0000625void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000626 // runAtExitHandlers() assumes there are no stack frames, but
627 // if exit() was called, then it had a stack frame. Blow away
628 // the stack before interpreting atexit handlers.
629 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000630 runAtExitHandlers ();
631 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000632}
633
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000634/// Pop the last stack frame off of ECStack and then copy the result
635/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000636/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000637/// CallInst if there was a previous stack frame. This method may
638/// invalidate any ECStack iterators you have. This method also takes
639/// care of switching to the normal destination BB, if we are returning
640/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000641///
642void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
643 GenericValue Result) {
644 // Pop the current stack frame.
645 ECStack.pop_back();
646
Misha Brukmand1c881a2005-04-21 22:43:08 +0000647 if (ECStack.empty()) { // Finished main. Put result into exit code...
648 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000649 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000650 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000651 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000652 }
653 } else {
654 // If we have a previous stack frame, and we have a previous call,
655 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000656 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000657 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000658 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000659 SetValue(I, Result, CallingSF);
660 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
661 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000662 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000663 }
664 }
665}
666
Chris Lattnerd7916e92003-05-10 21:22:39 +0000667void Interpreter::visitReturnInst(ReturnInst &I) {
668 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000669 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000670 GenericValue Result;
671
672 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000673 if (I.getNumOperands()) {
674 RetTy = I.getReturnValue()->getType();
675 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000676 }
677
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000678 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000679}
680
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000681void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000682 // Unwind stack
683 Instruction *Inst;
684 do {
685 ECStack.pop_back ();
686 if (ECStack.empty ())
687 abort ();
688 Inst = ECStack.back ().Caller.getInstruction ();
689 } while (!(Inst && isa<InvokeInst> (Inst)));
690
691 // Return from invoke
692 ExecutionContext &InvokingSF = ECStack.back ();
693 InvokingSF.Caller = CallSite ();
694
695 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000696 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000697}
698
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000699void Interpreter::visitUnreachableInst(UnreachableInst &I) {
700 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
701 abort();
702}
703
Chris Lattnerd7916e92003-05-10 21:22:39 +0000704void Interpreter::visitBranchInst(BranchInst &I) {
705 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000706 BasicBlock *Dest;
707
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000708 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
709 if (!I.isUnconditional()) {
710 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000711 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000712 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000713 }
Chris Lattner77113b62003-05-10 20:21:16 +0000714 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000715}
716
Chris Lattnerd7916e92003-05-10 21:22:39 +0000717void Interpreter::visitSwitchInst(SwitchInst &I) {
718 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000719 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
720 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000721
722 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000723 BasicBlock *Dest = 0;
724 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000725 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000726 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000727 Dest = cast<BasicBlock>(I.getOperand(i+1));
728 break;
729 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000730
Chris Lattner09e93922003-04-22 20:34:47 +0000731 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000732 SwitchToNewBasicBlock(Dest, SF);
733}
734
735// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
736// This function handles the actual updating of block and instruction iterators
737// as well as execution of all of the PHI nodes in the destination block.
738//
739// This method does this because all of the PHI nodes must be executed
740// atomically, reading their inputs before any of the results are updated. Not
741// doing this can cause problems if the PHI nodes depend on other PHI nodes for
742// their inputs. If the input PHI node is updated before it is read, incorrect
743// results can happen. Thus we use a two phase approach.
744//
745void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
746 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
747 SF.CurBB = Dest; // Update CurBB to branch destination
748 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
749
750 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
751
752 // Loop over all of the PHI nodes in the current block, reading their inputs.
753 std::vector<GenericValue> ResultValues;
754
755 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
756 // Search for the value corresponding to this previous bb...
757 int i = PN->getBasicBlockIndex(PrevBB);
758 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
759 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000760
Chris Lattner77113b62003-05-10 20:21:16 +0000761 // Save the incoming value for this PHI node...
762 ResultValues.push_back(getOperandValue(IncomingValue, SF));
763 }
764
765 // Now loop over all of the PHI nodes setting their values...
766 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000767 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
768 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000769 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000770 }
Chris Lattner09e93922003-04-22 20:34:47 +0000771}
772
Chris Lattner92101ac2001-08-23 17:05:04 +0000773//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000774// Memory Instruction Implementations
775//===----------------------------------------------------------------------===//
776
Chris Lattnerd7916e92003-05-10 21:22:39 +0000777void Interpreter::visitAllocationInst(AllocationInst &I) {
778 ExecutionContext &SF = ECStack.back();
779
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000780 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000781
Chris Lattnercc82cc12002-04-28 21:57:33 +0000782 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000783 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000784
785 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000786 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000787
Chris Lattnerfe11a972002-12-23 23:59:41 +0000788 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000789 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000790 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000791
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000792 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000793 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000794}
795
Chris Lattnerd7916e92003-05-10 21:22:39 +0000796void Interpreter::visitFreeInst(FreeInst &I) {
797 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000798 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
799 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000800 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000801 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000802}
803
Chris Lattnera34c5682002-08-27 22:33:45 +0000804// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000805//
Chris Lattner4af6de82003-11-25 20:44:56 +0000806GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000807 gep_type_iterator E,
808 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000809 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000810 "Cannot getElementOffset of a nonpointer type!");
811
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000812 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000813
814 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000815 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000816 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000817
Reid Spencerb83eb642006-10-20 07:07:24 +0000818 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
819 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000820
Chris Lattnerd5644962005-01-08 20:05:34 +0000821 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000822 } else {
823 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000824 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000825 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
826
827 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000828 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000829 default: assert(0 && "Illegal getelementptr index for sequential type!");
830 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
831 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
832 case Type::IntTyID: Idx = IdxGV.IntVal; break;
833 case Type::LongTyID: Idx = IdxGV.LongVal; break;
834 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
835 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
836 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
837 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
838 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000839 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000840 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000841 }
842
Chris Lattnera34c5682002-08-27 22:33:45 +0000843 GenericValue Result;
844 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
845 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000846}
847
Chris Lattnerd7916e92003-05-10 21:22:39 +0000848void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
849 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000850 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000851 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000852}
853
Chris Lattnerd7916e92003-05-10 21:22:39 +0000854void Interpreter::visitLoadInst(LoadInst &I) {
855 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000856 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000857 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000858 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000859 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000860}
861
Chris Lattnerd7916e92003-05-10 21:22:39 +0000862void Interpreter::visitStoreInst(StoreInst &I) {
863 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000864 GenericValue Val = getOperandValue(I.getOperand(0), SF);
865 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000866 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000867 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000868}
869
Chris Lattner86660982001-08-27 05:16:50 +0000870//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000871// Miscellaneous Instruction Implementations
872//===----------------------------------------------------------------------===//
873
Brian Gaekefea483d2003-11-07 20:04:22 +0000874void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000875 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000876
877 // Check to see if this is an intrinsic function call...
878 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000879 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000880 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000881 case Intrinsic::not_intrinsic:
882 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000883 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000884 GenericValue ArgIndex;
885 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
886 ArgIndex.UIntPairVal.second = 0;
887 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000888 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000889 }
Chris Lattner317201d2004-03-13 00:24:00 +0000890 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000891 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000892 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000893 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
894 return;
895 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000896 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000897 // class to transform it into hopefully tasty LLVM code.
898 //
899 Instruction *Prev = CS.getInstruction()->getPrev();
900 BasicBlock *Parent = CS.getInstruction()->getParent();
901 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
902
903 // Restore the CurInst pointer to the first instruction newly inserted, if
904 // any.
905 if (!Prev) {
906 SF.CurInst = Parent->begin();
907 } else {
908 SF.CurInst = Prev;
909 ++SF.CurInst;
910 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000911 return;
Chris Lattner73011782003-12-28 09:44:37 +0000912 }
913
Brian Gaekefea483d2003-11-07 20:04:22 +0000914 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000915 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000916 const unsigned NumArgs = SF.Caller.arg_size();
917 ArgVals.reserve(NumArgs);
918 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
919 e = SF.Caller.arg_end(); i != e; ++i) {
920 Value *V = *i;
921 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000922 // Promote all integral types whose size is < sizeof(int) into ints. We do
923 // this by zero or sign extending the value as appropriate according to the
924 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000925 const Type *Ty = V->getType();
926 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000927 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000928 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000929 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000930 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000931 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000932 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000933 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000934 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000935 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000936 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000937 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000938 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000939 }
940 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000941
Misha Brukmand1c881a2005-04-21 22:43:08 +0000942 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000943 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000944 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000945 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000946}
947
Chris Lattner86660982001-08-27 05:16:50 +0000948#define IMPLEMENT_SHIFT(OP, TY) \
949 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
950
Reid Spencer3822ff52006-11-08 06:47:33 +0000951#define IMPLEMENT_SIGNLESS_SHIFT(OP, TY1, TY2) \
952 case Type::TY2##TyID: \
953 IMPLEMENT_SHIFT(OP, TY1)
954
Brian Gaeke63438cc2003-12-11 00:22:59 +0000955static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
956 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000957 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000958 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000959 IMPLEMENT_SHIFT(<<, UByte);
960 IMPLEMENT_SHIFT(<<, SByte);
961 IMPLEMENT_SHIFT(<<, UShort);
962 IMPLEMENT_SHIFT(<<, Short);
963 IMPLEMENT_SHIFT(<<, UInt);
964 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000965 IMPLEMENT_SHIFT(<<, ULong);
966 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000967 default:
Chris Lattner02868352003-04-22 21:22:33 +0000968 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000969 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000970 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000971}
972
Reid Spencer3822ff52006-11-08 06:47:33 +0000973static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2,
974 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000975 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000976 switch (Ty->getTypeID()) {
Reid Spencer3822ff52006-11-08 06:47:33 +0000977 IMPLEMENT_SIGNLESS_SHIFT(>>, UByte, SByte);
978 IMPLEMENT_SIGNLESS_SHIFT(>>, UShort, Short);
979 IMPLEMENT_SIGNLESS_SHIFT(>>, UInt, Int);
980 IMPLEMENT_SIGNLESS_SHIFT(>>, ULong, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000981 default:
Reid Spencer3822ff52006-11-08 06:47:33 +0000982 std::cout << "Unhandled type for LShr instruction: " << *Ty << "\n";
983 abort();
984 }
985 return Dest;
986}
987
988static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2,
989 const Type *Ty) {
990 GenericValue Dest;
991 switch (Ty->getTypeID()) {
992 IMPLEMENT_SIGNLESS_SHIFT(>>, SByte, UByte);
993 IMPLEMENT_SIGNLESS_SHIFT(>>, Short, UShort);
994 IMPLEMENT_SIGNLESS_SHIFT(>>, Int, UInt);
995 IMPLEMENT_SIGNLESS_SHIFT(>>, Long, ULong);
996 default:
997 std::cout << "Unhandled type for AShr instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000998 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000999 }
Brian Gaeke63438cc2003-12-11 00:22:59 +00001000 return Dest;
1001}
1002
1003void Interpreter::visitShl(ShiftInst &I) {
1004 ExecutionContext &SF = ECStack.back();
1005 const Type *Ty = I.getOperand(0)->getType();
1006 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1007 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1008 GenericValue Dest;
1009 Dest = executeShlInst (Src1, Src2, Ty);
1010 SetValue(&I, Dest, SF);
1011}
1012
Reid Spencer3822ff52006-11-08 06:47:33 +00001013void Interpreter::visitLShr(ShiftInst &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001014 ExecutionContext &SF = ECStack.back();
1015 const Type *Ty = I.getOperand(0)->getType();
1016 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1017 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1018 GenericValue Dest;
Reid Spencer3822ff52006-11-08 06:47:33 +00001019 Dest = executeLShrInst (Src1, Src2, Ty);
1020 SetValue(&I, Dest, SF);
1021}
1022
1023void Interpreter::visitAShr(ShiftInst &I) {
1024 ExecutionContext &SF = ECStack.back();
1025 const Type *Ty = I.getOperand(0)->getType();
1026 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1027 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1028 GenericValue Dest;
1029 Dest = executeAShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001030 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001031}
1032
1033#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001034 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001035
1036#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1037 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001038 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001039 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001040 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1041 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1042 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1043 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1044 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001045 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1046 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001047 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1048 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001049
1050#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1051 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1052 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1053
1054#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +00001055 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001056 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001057 } \
1058 break
1059
1060#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1061 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1062 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001063 IMPLEMENT_CAST_CASE_END()
1064
Brian Gaeke29794cb2003-09-05 18:55:03 +00001065GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001066 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001067 const Type *SrcTy = SrcVal->getType();
1068 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001069
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001070 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001071 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1072 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1073 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001074 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001075 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1076 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1077 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1078 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001079 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001080 IMPLEMENT_CAST_CASE(Float , (float));
1081 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001082 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001083 default:
Chris Lattner02868352003-04-22 21:22:33 +00001084 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001085 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001086 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001087
1088 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001089}
Chris Lattner92101ac2001-08-23 17:05:04 +00001090
Chris Lattnerd7916e92003-05-10 21:22:39 +00001091void Interpreter::visitCastInst(CastInst &I) {
1092 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001093 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1094}
Chris Lattner92101ac2001-08-23 17:05:04 +00001095
Brian Gaekec1a2be12003-11-07 21:20:47 +00001096#define IMPLEMENT_VAARG(TY) \
1097 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1098
1099void Interpreter::visitVAArgInst(VAArgInst &I) {
1100 ExecutionContext &SF = ECStack.back();
1101
Brian Gaeke9d20b712004-02-25 23:01:48 +00001102 // Get the incoming valist parameter. LLI treats the valist as a
1103 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001104 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001105 GenericValue Dest;
1106 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001107 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001108 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001109 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001110 IMPLEMENT_VAARG(UByte);
1111 IMPLEMENT_VAARG(SByte);
1112 IMPLEMENT_VAARG(UShort);
1113 IMPLEMENT_VAARG(Short);
1114 IMPLEMENT_VAARG(UInt);
1115 IMPLEMENT_VAARG(Int);
1116 IMPLEMENT_VAARG(ULong);
1117 IMPLEMENT_VAARG(Long);
1118 IMPLEMENT_VAARG(Pointer);
1119 IMPLEMENT_VAARG(Float);
1120 IMPLEMENT_VAARG(Double);
1121 IMPLEMENT_VAARG(Bool);
1122 default:
1123 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1124 abort();
1125 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001126
Brian Gaekec1a2be12003-11-07 21:20:47 +00001127 // Set the Value of this Instruction.
1128 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001129
1130 // Move the pointer to the next vararg.
1131 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001132}
1133
Chris Lattner92101ac2001-08-23 17:05:04 +00001134//===----------------------------------------------------------------------===//
1135// Dispatch and Execution Code
1136//===----------------------------------------------------------------------===//
1137
Chris Lattner92101ac2001-08-23 17:05:04 +00001138//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001139// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001140//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001141void Interpreter::callFunction(Function *F,
1142 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001143 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1144 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1145 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001146 // Make a new stack frame... and fill it in.
1147 ECStack.push_back(ExecutionContext());
1148 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001149 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001150
1151 // Special handling for external functions.
1152 if (F->isExternal()) {
1153 GenericValue Result = callExternalFunction (F, ArgVals);
1154 // Simulate a 'ret' instruction of the appropriate type.
1155 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1156 return;
1157 }
1158
1159 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001160 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001161 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001162
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001163 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001164 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001165 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001166 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001167
1168 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001169 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001170 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001171 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001172
1173 // Handle varargs arguments...
1174 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001175}
1176
Chris Lattner92101ac2001-08-23 17:05:04 +00001177void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001178 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001179 // Interpret a single instruction & increment the "PC".
1180 ExecutionContext &SF = ECStack.back(); // Current stack frame
1181 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001182
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001183 // Track the number of dynamic instructions executed.
1184 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001185
Brian Gaeke63438cc2003-12-11 00:22:59 +00001186 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001187 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001188 }
1189}