blob: eec018ac010903d5d507e33ed36b0ec87320589b [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"
Brian Gaeke235b2002003-10-10 17:03:22 +000023#include <cmath> // For fmod
Chris Lattner4af6de82003-11-25 20:44:56 +000024using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000025
Chris Lattnerbbdabce2002-12-08 05:51:08 +000026namespace {
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
Chris Lattnerbbdabce2002-12-08 05:51:08 +000028
Chris Lattner4af6de82003-11-25 20:44:56 +000029 Interpreter *TheEE = 0;
30}
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner73011782003-12-28 09:44:37 +000032
Chris Lattner2e42d3a2001-10-15 05:51:48 +000033//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000034// Value Manipulation code
35//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000036
Misha Brukmand1c881a2005-04-21 22:43:08 +000037static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
38 const Type *Ty);
39static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
40 const Type *Ty);
41static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
42 const Type *Ty);
43static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
44 const Type *Ty);
Reid Spencer1628cec2006-10-26 06:15:43 +000045static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
46 const Type *Ty);
47static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
48 const Type *Ty);
49static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
50 const Type *Ty);
Reid Spencer0a783f72006-11-02 01:53:59 +000051static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
53static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
55static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
Misha Brukmand1c881a2005-04-21 22:43:08 +000057static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
59static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
60 const Type *Ty);
61static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
63static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
67static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
68 const Type *Ty);
69static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
70 const Type *Ty);
71static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
72 const Type *Ty);
73static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
74 const Type *Ty);
75static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
76 const Type *Ty);
77static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
78 const Type *Ty);
79static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000080 GenericValue Src3);
81
Brian Gaeke63438cc2003-12-11 00:22:59 +000082GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
83 ExecutionContext &SF) {
84 switch (CE->getOpcode()) {
85 case Instruction::Cast:
86 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
87 case Instruction::GetElementPtr:
88 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
89 gep_type_end(CE), SF);
90 case Instruction::Add:
91 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
92 getOperandValue(CE->getOperand(1), SF),
93 CE->getOperand(0)->getType());
94 case Instruction::Sub:
95 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
96 getOperandValue(CE->getOperand(1), SF),
97 CE->getOperand(0)->getType());
98 case Instruction::Mul:
99 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
100 getOperandValue(CE->getOperand(1), SF),
101 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +0000102 case Instruction::SDiv:
103 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
104 getOperandValue(CE->getOperand(1), SF),
105 CE->getOperand(0)->getType());
106 case Instruction::UDiv:
107 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
108 getOperandValue(CE->getOperand(1), SF),
109 CE->getOperand(0)->getType());
110 case Instruction::FDiv:
111 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
112 getOperandValue(CE->getOperand(1), SF),
113 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000114 case Instruction::URem:
115 return executeURemInst(getOperandValue(CE->getOperand(0), SF),
Brian Gaeke63438cc2003-12-11 00:22:59 +0000116 getOperandValue(CE->getOperand(1), SF),
117 CE->getOperand(0)->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +0000118 case Instruction::SRem:
119 return executeSRemInst(getOperandValue(CE->getOperand(0), SF),
120 getOperandValue(CE->getOperand(1), SF),
121 CE->getOperand(0)->getType());
122 case Instruction::FRem:
123 return executeFRemInst(getOperandValue(CE->getOperand(0), SF),
124 getOperandValue(CE->getOperand(1), SF),
125 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000126 case Instruction::And:
127 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
128 getOperandValue(CE->getOperand(1), SF),
129 CE->getOperand(0)->getType());
130 case Instruction::Or:
131 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
132 getOperandValue(CE->getOperand(1), SF),
133 CE->getOperand(0)->getType());
134 case Instruction::Xor:
135 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
136 getOperandValue(CE->getOperand(1), SF),
137 CE->getOperand(0)->getType());
138 case Instruction::SetEQ:
139 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
140 getOperandValue(CE->getOperand(1), SF),
141 CE->getOperand(0)->getType());
142 case Instruction::SetNE:
143 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
144 getOperandValue(CE->getOperand(1), SF),
145 CE->getOperand(0)->getType());
146 case Instruction::SetLE:
147 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
148 getOperandValue(CE->getOperand(1), SF),
149 CE->getOperand(0)->getType());
150 case Instruction::SetGE:
151 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
152 getOperandValue(CE->getOperand(1), SF),
153 CE->getOperand(0)->getType());
154 case Instruction::SetLT:
155 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
156 getOperandValue(CE->getOperand(1), SF),
157 CE->getOperand(0)->getType());
158 case Instruction::SetGT:
159 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
160 getOperandValue(CE->getOperand(1), SF),
161 CE->getOperand(0)->getType());
162 case Instruction::Shl:
163 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
164 getOperandValue(CE->getOperand(1), SF),
165 CE->getOperand(0)->getType());
166 case Instruction::Shr:
167 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
168 getOperandValue(CE->getOperand(1), SF),
169 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000170 case Instruction::Select:
171 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
172 getOperandValue(CE->getOperand(1), SF),
173 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000174 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000175 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000176 abort();
177 return GenericValue();
178 }
179}
Chris Lattnera34c5682002-08-27 22:33:45 +0000180
Brian Gaeke29794cb2003-09-05 18:55:03 +0000181GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000182 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000183 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000184 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000185 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000186 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000187 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000188 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000189 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000190 }
191}
192
Chris Lattner39bb5b42001-10-15 13:25:40 +0000193static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000194 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000195}
196
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000197void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000198 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000199}
200
Chris Lattner2adcd832002-05-03 19:52:30 +0000201//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000202// Binary Instruction Implementations
203//===----------------------------------------------------------------------===//
204
205#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
206 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
207
Misha Brukmand1c881a2005-04-21 22:43:08 +0000208static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
209 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000210 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000211 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000212 IMPLEMENT_BINARY_OPERATOR(+, UByte);
213 IMPLEMENT_BINARY_OPERATOR(+, SByte);
214 IMPLEMENT_BINARY_OPERATOR(+, UShort);
215 IMPLEMENT_BINARY_OPERATOR(+, Short);
216 IMPLEMENT_BINARY_OPERATOR(+, UInt);
217 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000218 IMPLEMENT_BINARY_OPERATOR(+, ULong);
219 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000220 IMPLEMENT_BINARY_OPERATOR(+, Float);
221 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000222 default:
Chris Lattner02868352003-04-22 21:22:33 +0000223 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
224 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000225 }
226 return Dest;
227}
228
Misha Brukmand1c881a2005-04-21 22:43:08 +0000229static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
230 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000231 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000232 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000233 IMPLEMENT_BINARY_OPERATOR(-, UByte);
234 IMPLEMENT_BINARY_OPERATOR(-, SByte);
235 IMPLEMENT_BINARY_OPERATOR(-, UShort);
236 IMPLEMENT_BINARY_OPERATOR(-, Short);
237 IMPLEMENT_BINARY_OPERATOR(-, UInt);
238 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000239 IMPLEMENT_BINARY_OPERATOR(-, ULong);
240 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000241 IMPLEMENT_BINARY_OPERATOR(-, Float);
242 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000243 default:
Chris Lattner02868352003-04-22 21:22:33 +0000244 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
245 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000246 }
247 return Dest;
248}
249
Misha Brukmand1c881a2005-04-21 22:43:08 +0000250static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
251 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000252 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000253 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000254 IMPLEMENT_BINARY_OPERATOR(*, UByte);
255 IMPLEMENT_BINARY_OPERATOR(*, SByte);
256 IMPLEMENT_BINARY_OPERATOR(*, UShort);
257 IMPLEMENT_BINARY_OPERATOR(*, Short);
258 IMPLEMENT_BINARY_OPERATOR(*, UInt);
259 IMPLEMENT_BINARY_OPERATOR(*, Int);
260 IMPLEMENT_BINARY_OPERATOR(*, ULong);
261 IMPLEMENT_BINARY_OPERATOR(*, Long);
262 IMPLEMENT_BINARY_OPERATOR(*, Float);
263 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000264 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000265 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000266 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000267 }
268 return Dest;
269}
270
Reid Spencerfe855262006-11-01 03:41:05 +0000271#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
272 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
273
Reid Spencer1628cec2006-10-26 06:15:43 +0000274static GenericValue executeUDivInst(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(/, UByte, SByte);
279 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
280 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
281 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000282 default:
283 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
284 abort();
285 }
286 return Dest;
287}
288
289static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
290 const Type *Ty) {
291 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000292 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000293 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
294 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
295 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
296 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000297 default:
298 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
299 abort();
300 }
301 return Dest;
302}
303
304static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000305 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000306 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000307 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000308 IMPLEMENT_BINARY_OPERATOR(/, Float);
309 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000310 default:
Chris Lattner02868352003-04-22 21:22:33 +0000311 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
312 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000313 }
314 return Dest;
315}
316
Reid Spencer0a783f72006-11-02 01:53:59 +0000317static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000318 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000319 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000320 switch (Ty->getTypeID()) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000321 IMPLEMENT_SIGNLESS_BINOP(%, UByte, SByte);
322 IMPLEMENT_SIGNLESS_BINOP(%, UShort, Short);
323 IMPLEMENT_SIGNLESS_BINOP(%, UInt, Int);
324 IMPLEMENT_SIGNLESS_BINOP(%, ULong, Long);
325 default:
326 std::cout << "Unhandled type for URem instruction: " << *Ty << "\n";
327 abort();
328 }
329 return Dest;
330}
331
332static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2,
333 const Type *Ty) {
334 GenericValue Dest;
335 switch (Ty->getTypeID()) {
336 IMPLEMENT_SIGNLESS_BINOP(%, SByte, UByte);
337 IMPLEMENT_SIGNLESS_BINOP(%, Short, UShort);
338 IMPLEMENT_SIGNLESS_BINOP(%, Int, UInt);
339 IMPLEMENT_SIGNLESS_BINOP(%, Long, ULong);
340 default:
341 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
342 abort();
343 }
344 return Dest;
345}
346
347static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2,
348 const Type *Ty) {
349 GenericValue Dest;
350 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000351 case Type::FloatTyID:
352 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
353 break;
354 case Type::DoubleTyID:
355 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
356 break;
357 default:
Chris Lattner02868352003-04-22 21:22:33 +0000358 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
359 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000360 }
361 return Dest;
362}
363
Misha Brukmand1c881a2005-04-21 22:43:08 +0000364static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
365 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000366 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000367 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000368 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000369 IMPLEMENT_BINARY_OPERATOR(&, UByte);
370 IMPLEMENT_BINARY_OPERATOR(&, SByte);
371 IMPLEMENT_BINARY_OPERATOR(&, UShort);
372 IMPLEMENT_BINARY_OPERATOR(&, Short);
373 IMPLEMENT_BINARY_OPERATOR(&, UInt);
374 IMPLEMENT_BINARY_OPERATOR(&, Int);
375 IMPLEMENT_BINARY_OPERATOR(&, ULong);
376 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000377 default:
Chris Lattner02868352003-04-22 21:22:33 +0000378 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
379 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000380 }
381 return Dest;
382}
383
Misha Brukmand1c881a2005-04-21 22:43:08 +0000384static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000385 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000386 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000387 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000388 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000389 IMPLEMENT_BINARY_OPERATOR(|, UByte);
390 IMPLEMENT_BINARY_OPERATOR(|, SByte);
391 IMPLEMENT_BINARY_OPERATOR(|, UShort);
392 IMPLEMENT_BINARY_OPERATOR(|, Short);
393 IMPLEMENT_BINARY_OPERATOR(|, UInt);
394 IMPLEMENT_BINARY_OPERATOR(|, Int);
395 IMPLEMENT_BINARY_OPERATOR(|, ULong);
396 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000397 default:
Chris Lattner02868352003-04-22 21:22:33 +0000398 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
399 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000400 }
401 return Dest;
402}
403
Misha Brukmand1c881a2005-04-21 22:43:08 +0000404static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000405 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000406 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000407 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000408 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000409 IMPLEMENT_BINARY_OPERATOR(^, UByte);
410 IMPLEMENT_BINARY_OPERATOR(^, SByte);
411 IMPLEMENT_BINARY_OPERATOR(^, UShort);
412 IMPLEMENT_BINARY_OPERATOR(^, Short);
413 IMPLEMENT_BINARY_OPERATOR(^, UInt);
414 IMPLEMENT_BINARY_OPERATOR(^, Int);
415 IMPLEMENT_BINARY_OPERATOR(^, ULong);
416 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000417 default:
Chris Lattner02868352003-04-22 21:22:33 +0000418 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
419 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000420 }
421 return Dest;
422}
423
Chris Lattner92101ac2001-08-23 17:05:04 +0000424#define IMPLEMENT_SETCC(OP, TY) \
425 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
426
Chris Lattnerfd506f52003-04-23 19:55:35 +0000427// Handle pointers specially because they must be compared with only as much
428// width as the host has. We _do not_ want to be comparing 64 bit values when
429// running on a 32-bit target, otherwise the upper 32 bits might mess up
430// comparisons if they contain garbage.
431#define IMPLEMENT_POINTERSETCC(OP) \
432 case Type::PointerTyID: \
433 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
434 (void*)(intptr_t)Src2.PointerVal; break
435
Misha Brukmand1c881a2005-04-21 22:43:08 +0000436static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
437 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000439 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000440 IMPLEMENT_SETCC(==, UByte);
441 IMPLEMENT_SETCC(==, SByte);
442 IMPLEMENT_SETCC(==, UShort);
443 IMPLEMENT_SETCC(==, Short);
444 IMPLEMENT_SETCC(==, UInt);
445 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000446 IMPLEMENT_SETCC(==, ULong);
447 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 IMPLEMENT_SETCC(==, Float);
449 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000450 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 default:
Chris Lattner02868352003-04-22 21:22:33 +0000452 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
453 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000454 }
455 return Dest;
456}
457
Misha Brukmand1c881a2005-04-21 22:43:08 +0000458static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
459 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000461 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000462 IMPLEMENT_SETCC(!=, UByte);
463 IMPLEMENT_SETCC(!=, SByte);
464 IMPLEMENT_SETCC(!=, UShort);
465 IMPLEMENT_SETCC(!=, Short);
466 IMPLEMENT_SETCC(!=, UInt);
467 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000468 IMPLEMENT_SETCC(!=, ULong);
469 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 IMPLEMENT_SETCC(!=, Float);
471 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000472 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000473
Chris Lattner92101ac2001-08-23 17:05:04 +0000474 default:
Chris Lattner02868352003-04-22 21:22:33 +0000475 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
476 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 }
478 return Dest;
479}
480
Misha Brukmand1c881a2005-04-21 22:43:08 +0000481static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
482 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000483 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000484 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 IMPLEMENT_SETCC(<=, UByte);
486 IMPLEMENT_SETCC(<=, SByte);
487 IMPLEMENT_SETCC(<=, UShort);
488 IMPLEMENT_SETCC(<=, Short);
489 IMPLEMENT_SETCC(<=, UInt);
490 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000491 IMPLEMENT_SETCC(<=, ULong);
492 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000493 IMPLEMENT_SETCC(<=, Float);
494 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000495 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000496 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000497 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000498 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000499 }
500 return Dest;
501}
502
Misha Brukmand1c881a2005-04-21 22:43:08 +0000503static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
504 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000506 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000507 IMPLEMENT_SETCC(>=, UByte);
508 IMPLEMENT_SETCC(>=, SByte);
509 IMPLEMENT_SETCC(>=, UShort);
510 IMPLEMENT_SETCC(>=, Short);
511 IMPLEMENT_SETCC(>=, UInt);
512 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000513 IMPLEMENT_SETCC(>=, ULong);
514 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000515 IMPLEMENT_SETCC(>=, Float);
516 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000517 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000518 default:
Chris Lattner02868352003-04-22 21:22:33 +0000519 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
520 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 }
522 return Dest;
523}
524
Misha Brukmand1c881a2005-04-21 22:43:08 +0000525static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
526 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000528 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000529 IMPLEMENT_SETCC(<, UByte);
530 IMPLEMENT_SETCC(<, SByte);
531 IMPLEMENT_SETCC(<, UShort);
532 IMPLEMENT_SETCC(<, Short);
533 IMPLEMENT_SETCC(<, UInt);
534 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000535 IMPLEMENT_SETCC(<, ULong);
536 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000537 IMPLEMENT_SETCC(<, Float);
538 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000539 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000540 default:
Chris Lattner02868352003-04-22 21:22:33 +0000541 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
542 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000543 }
544 return Dest;
545}
546
Misha Brukmand1c881a2005-04-21 22:43:08 +0000547static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
548 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000549 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000550 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000551 IMPLEMENT_SETCC(>, UByte);
552 IMPLEMENT_SETCC(>, SByte);
553 IMPLEMENT_SETCC(>, UShort);
554 IMPLEMENT_SETCC(>, Short);
555 IMPLEMENT_SETCC(>, UInt);
556 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000557 IMPLEMENT_SETCC(>, ULong);
558 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000559 IMPLEMENT_SETCC(>, Float);
560 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000561 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000562 default:
Chris Lattner02868352003-04-22 21:22:33 +0000563 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
564 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000565 }
566 return Dest;
567}
568
Chris Lattnerd7916e92003-05-10 21:22:39 +0000569void Interpreter::visitBinaryOperator(BinaryOperator &I) {
570 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000571 const Type *Ty = I.getOperand(0)->getType();
572 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
573 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000574 GenericValue R; // Result
575
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000576 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000577 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
578 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
579 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000580 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
581 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
582 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Reid Spencer0a783f72006-11-02 01:53:59 +0000583 case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break;
584 case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break;
585 case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000586 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
587 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
588 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
589 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
590 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
591 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
592 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
593 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
594 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000595 default:
Chris Lattner02868352003-04-22 21:22:33 +0000596 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
597 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000598 }
599
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000600 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000601}
602
Misha Brukmand1c881a2005-04-21 22:43:08 +0000603static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000604 GenericValue Src3) {
605 return Src1.BoolVal ? Src2 : Src3;
606}
607
608void Interpreter::visitSelectInst(SelectInst &I) {
609 ExecutionContext &SF = ECStack.back();
610 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
611 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
612 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
613 GenericValue R = executeSelectInst(Src1, Src2, Src3);
614 SetValue(&I, R, SF);
615}
616
617
Chris Lattner92101ac2001-08-23 17:05:04 +0000618//===----------------------------------------------------------------------===//
619// Terminator Instruction Implementations
620//===----------------------------------------------------------------------===//
621
Chris Lattnere43db882001-10-27 04:15:57 +0000622void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000623 // runAtExitHandlers() assumes there are no stack frames, but
624 // if exit() was called, then it had a stack frame. Blow away
625 // the stack before interpreting atexit handlers.
626 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000627 runAtExitHandlers ();
628 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000629}
630
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000631/// Pop the last stack frame off of ECStack and then copy the result
632/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000633/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000634/// CallInst if there was a previous stack frame. This method may
635/// invalidate any ECStack iterators you have. This method also takes
636/// care of switching to the normal destination BB, if we are returning
637/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000638///
639void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
640 GenericValue Result) {
641 // Pop the current stack frame.
642 ECStack.pop_back();
643
Misha Brukmand1c881a2005-04-21 22:43:08 +0000644 if (ECStack.empty()) { // Finished main. Put result into exit code...
645 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000646 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000647 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000648 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000649 }
650 } else {
651 // If we have a previous stack frame, and we have a previous call,
652 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000653 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000654 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000655 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000656 SetValue(I, Result, CallingSF);
657 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
658 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000659 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000660 }
661 }
662}
663
Chris Lattnerd7916e92003-05-10 21:22:39 +0000664void Interpreter::visitReturnInst(ReturnInst &I) {
665 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000666 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000667 GenericValue Result;
668
669 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000670 if (I.getNumOperands()) {
671 RetTy = I.getReturnValue()->getType();
672 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000673 }
674
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000675 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000676}
677
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000678void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000679 // Unwind stack
680 Instruction *Inst;
681 do {
682 ECStack.pop_back ();
683 if (ECStack.empty ())
684 abort ();
685 Inst = ECStack.back ().Caller.getInstruction ();
686 } while (!(Inst && isa<InvokeInst> (Inst)));
687
688 // Return from invoke
689 ExecutionContext &InvokingSF = ECStack.back ();
690 InvokingSF.Caller = CallSite ();
691
692 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000693 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000694}
695
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000696void Interpreter::visitUnreachableInst(UnreachableInst &I) {
697 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
698 abort();
699}
700
Chris Lattnerd7916e92003-05-10 21:22:39 +0000701void Interpreter::visitBranchInst(BranchInst &I) {
702 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000703 BasicBlock *Dest;
704
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000705 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
706 if (!I.isUnconditional()) {
707 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000708 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000709 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000710 }
Chris Lattner77113b62003-05-10 20:21:16 +0000711 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000712}
713
Chris Lattnerd7916e92003-05-10 21:22:39 +0000714void Interpreter::visitSwitchInst(SwitchInst &I) {
715 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000716 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
717 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000718
719 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000720 BasicBlock *Dest = 0;
721 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000722 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000723 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000724 Dest = cast<BasicBlock>(I.getOperand(i+1));
725 break;
726 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000727
Chris Lattner09e93922003-04-22 20:34:47 +0000728 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000729 SwitchToNewBasicBlock(Dest, SF);
730}
731
732// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
733// This function handles the actual updating of block and instruction iterators
734// as well as execution of all of the PHI nodes in the destination block.
735//
736// This method does this because all of the PHI nodes must be executed
737// atomically, reading their inputs before any of the results are updated. Not
738// doing this can cause problems if the PHI nodes depend on other PHI nodes for
739// their inputs. If the input PHI node is updated before it is read, incorrect
740// results can happen. Thus we use a two phase approach.
741//
742void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
743 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
744 SF.CurBB = Dest; // Update CurBB to branch destination
745 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
746
747 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
748
749 // Loop over all of the PHI nodes in the current block, reading their inputs.
750 std::vector<GenericValue> ResultValues;
751
752 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
753 // Search for the value corresponding to this previous bb...
754 int i = PN->getBasicBlockIndex(PrevBB);
755 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
756 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000757
Chris Lattner77113b62003-05-10 20:21:16 +0000758 // Save the incoming value for this PHI node...
759 ResultValues.push_back(getOperandValue(IncomingValue, SF));
760 }
761
762 // Now loop over all of the PHI nodes setting their values...
763 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000764 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
765 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000766 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000767 }
Chris Lattner09e93922003-04-22 20:34:47 +0000768}
769
Chris Lattner92101ac2001-08-23 17:05:04 +0000770//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000771// Memory Instruction Implementations
772//===----------------------------------------------------------------------===//
773
Chris Lattnerd7916e92003-05-10 21:22:39 +0000774void Interpreter::visitAllocationInst(AllocationInst &I) {
775 ExecutionContext &SF = ECStack.back();
776
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000777 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000778
Chris Lattnercc82cc12002-04-28 21:57:33 +0000779 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000780 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000781
782 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000783 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000784
Chris Lattnerfe11a972002-12-23 23:59:41 +0000785 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000786 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000787 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000788
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000789 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000790 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000791}
792
Chris Lattnerd7916e92003-05-10 21:22:39 +0000793void Interpreter::visitFreeInst(FreeInst &I) {
794 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000795 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
796 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000797 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000798 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000799}
800
Chris Lattnera34c5682002-08-27 22:33:45 +0000801// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000802//
Chris Lattner4af6de82003-11-25 20:44:56 +0000803GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000804 gep_type_iterator E,
805 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000806 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000807 "Cannot getElementOffset of a nonpointer type!");
808
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000809 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000810
811 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000812 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000813 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000814
Reid Spencerb83eb642006-10-20 07:07:24 +0000815 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
816 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000817
Chris Lattnerd5644962005-01-08 20:05:34 +0000818 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000819 } else {
820 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000821 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000822 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
823
824 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000825 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000826 default: assert(0 && "Illegal getelementptr index for sequential type!");
827 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
828 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
829 case Type::IntTyID: Idx = IdxGV.IntVal; break;
830 case Type::LongTyID: Idx = IdxGV.LongVal; break;
831 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
832 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
833 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
834 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
835 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000836 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000837 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000838 }
839
Chris Lattnera34c5682002-08-27 22:33:45 +0000840 GenericValue Result;
841 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
842 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000843}
844
Chris Lattnerd7916e92003-05-10 21:22:39 +0000845void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
846 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000847 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000848 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000849}
850
Chris Lattnerd7916e92003-05-10 21:22:39 +0000851void Interpreter::visitLoadInst(LoadInst &I) {
852 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000853 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000854 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000855 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000856 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000857}
858
Chris Lattnerd7916e92003-05-10 21:22:39 +0000859void Interpreter::visitStoreInst(StoreInst &I) {
860 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000861 GenericValue Val = getOperandValue(I.getOperand(0), SF);
862 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000863 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000864 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000865}
866
Chris Lattner86660982001-08-27 05:16:50 +0000867//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000868// Miscellaneous Instruction Implementations
869//===----------------------------------------------------------------------===//
870
Brian Gaekefea483d2003-11-07 20:04:22 +0000871void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000872 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000873
874 // Check to see if this is an intrinsic function call...
875 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000876 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000877 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000878 case Intrinsic::not_intrinsic:
879 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000880 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000881 GenericValue ArgIndex;
882 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
883 ArgIndex.UIntPairVal.second = 0;
884 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000885 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000886 }
Chris Lattner317201d2004-03-13 00:24:00 +0000887 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000888 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000889 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000890 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
891 return;
892 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000893 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000894 // class to transform it into hopefully tasty LLVM code.
895 //
896 Instruction *Prev = CS.getInstruction()->getPrev();
897 BasicBlock *Parent = CS.getInstruction()->getParent();
898 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
899
900 // Restore the CurInst pointer to the first instruction newly inserted, if
901 // any.
902 if (!Prev) {
903 SF.CurInst = Parent->begin();
904 } else {
905 SF.CurInst = Prev;
906 ++SF.CurInst;
907 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000908 return;
Chris Lattner73011782003-12-28 09:44:37 +0000909 }
910
Brian Gaekefea483d2003-11-07 20:04:22 +0000911 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000912 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000913 const unsigned NumArgs = SF.Caller.arg_size();
914 ArgVals.reserve(NumArgs);
915 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
916 e = SF.Caller.arg_end(); i != e; ++i) {
917 Value *V = *i;
918 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000919 // Promote all integral types whose size is < sizeof(int) into ints. We do
920 // this by zero or sign extending the value as appropriate according to the
921 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000922 const Type *Ty = V->getType();
923 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000924 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000925 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000926 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000927 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000928 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000929 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000930 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000931 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000932 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000933 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000934 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000935 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000936 }
937 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000938
Misha Brukmand1c881a2005-04-21 22:43:08 +0000939 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000940 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000941 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000942 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000943}
944
Chris Lattner86660982001-08-27 05:16:50 +0000945#define IMPLEMENT_SHIFT(OP, TY) \
946 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
947
Brian Gaeke63438cc2003-12-11 00:22:59 +0000948static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
949 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000950 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000951 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000952 IMPLEMENT_SHIFT(<<, UByte);
953 IMPLEMENT_SHIFT(<<, SByte);
954 IMPLEMENT_SHIFT(<<, UShort);
955 IMPLEMENT_SHIFT(<<, Short);
956 IMPLEMENT_SHIFT(<<, UInt);
957 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000958 IMPLEMENT_SHIFT(<<, ULong);
959 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000960 default:
Chris Lattner02868352003-04-22 21:22:33 +0000961 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000962 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000963 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000964}
965
Brian Gaeke63438cc2003-12-11 00:22:59 +0000966static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
967 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000968 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000969 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000970 IMPLEMENT_SHIFT(>>, UByte);
971 IMPLEMENT_SHIFT(>>, SByte);
972 IMPLEMENT_SHIFT(>>, UShort);
973 IMPLEMENT_SHIFT(>>, Short);
974 IMPLEMENT_SHIFT(>>, UInt);
975 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000976 IMPLEMENT_SHIFT(>>, ULong);
977 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000978 default:
Chris Lattner02868352003-04-22 21:22:33 +0000979 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
980 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000981 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000982 return Dest;
983}
984
985void Interpreter::visitShl(ShiftInst &I) {
986 ExecutionContext &SF = ECStack.back();
987 const Type *Ty = I.getOperand(0)->getType();
988 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
989 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
990 GenericValue Dest;
991 Dest = executeShlInst (Src1, Src2, Ty);
992 SetValue(&I, Dest, SF);
993}
994
995void Interpreter::visitShr(ShiftInst &I) {
996 ExecutionContext &SF = ECStack.back();
997 const Type *Ty = I.getOperand(0)->getType();
998 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
999 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1000 GenericValue Dest;
1001 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001002 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001003}
1004
1005#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001006 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +00001007
1008#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
1009 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001010 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +00001011 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +00001012 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
1013 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
1014 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
1015 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
1016 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +00001017 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1018 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +00001019 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1020 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +00001021
1022#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1023 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1024 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1025
1026#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +00001027 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +00001028 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +00001029 } \
1030 break
1031
1032#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1033 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1034 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001035 IMPLEMENT_CAST_CASE_END()
1036
Brian Gaeke29794cb2003-09-05 18:55:03 +00001037GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001038 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001039 const Type *SrcTy = SrcVal->getType();
1040 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001041
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001042 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001043 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1044 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1045 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001046 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001047 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1048 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1049 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1050 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001051 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001052 IMPLEMENT_CAST_CASE(Float , (float));
1053 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001054 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001055 default:
Chris Lattner02868352003-04-22 21:22:33 +00001056 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001057 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001058 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001059
1060 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001061}
Chris Lattner92101ac2001-08-23 17:05:04 +00001062
Chris Lattnerd7916e92003-05-10 21:22:39 +00001063void Interpreter::visitCastInst(CastInst &I) {
1064 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001065 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1066}
Chris Lattner92101ac2001-08-23 17:05:04 +00001067
Brian Gaekec1a2be12003-11-07 21:20:47 +00001068#define IMPLEMENT_VAARG(TY) \
1069 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1070
1071void Interpreter::visitVAArgInst(VAArgInst &I) {
1072 ExecutionContext &SF = ECStack.back();
1073
Brian Gaeke9d20b712004-02-25 23:01:48 +00001074 // Get the incoming valist parameter. LLI treats the valist as a
1075 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001076 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001077 GenericValue Dest;
1078 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001079 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001080 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001081 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001082 IMPLEMENT_VAARG(UByte);
1083 IMPLEMENT_VAARG(SByte);
1084 IMPLEMENT_VAARG(UShort);
1085 IMPLEMENT_VAARG(Short);
1086 IMPLEMENT_VAARG(UInt);
1087 IMPLEMENT_VAARG(Int);
1088 IMPLEMENT_VAARG(ULong);
1089 IMPLEMENT_VAARG(Long);
1090 IMPLEMENT_VAARG(Pointer);
1091 IMPLEMENT_VAARG(Float);
1092 IMPLEMENT_VAARG(Double);
1093 IMPLEMENT_VAARG(Bool);
1094 default:
1095 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1096 abort();
1097 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001098
Brian Gaekec1a2be12003-11-07 21:20:47 +00001099 // Set the Value of this Instruction.
1100 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001101
1102 // Move the pointer to the next vararg.
1103 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001104}
1105
Chris Lattner92101ac2001-08-23 17:05:04 +00001106//===----------------------------------------------------------------------===//
1107// Dispatch and Execution Code
1108//===----------------------------------------------------------------------===//
1109
Chris Lattner92101ac2001-08-23 17:05:04 +00001110//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001111// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001112//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001113void Interpreter::callFunction(Function *F,
1114 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001115 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1116 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1117 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001118 // Make a new stack frame... and fill it in.
1119 ECStack.push_back(ExecutionContext());
1120 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001121 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001122
1123 // Special handling for external functions.
1124 if (F->isExternal()) {
1125 GenericValue Result = callExternalFunction (F, ArgVals);
1126 // Simulate a 'ret' instruction of the appropriate type.
1127 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1128 return;
1129 }
1130
1131 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001132 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001133 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001134
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001135 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001136 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001137 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001138 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001139
1140 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001141 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001142 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001143 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001144
1145 // Handle varargs arguments...
1146 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001147}
1148
Chris Lattner92101ac2001-08-23 17:05:04 +00001149void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001150 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001151 // Interpret a single instruction & increment the "PC".
1152 ExecutionContext &SF = ECStack.back(); // Current stack frame
1153 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001154
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001155 // Track the number of dynamic instructions executed.
1156 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001157
Brian Gaeke63438cc2003-12-11 00:22:59 +00001158 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001159 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001160 }
1161}