blob: 41f07507d469e5d7834ba3d4fbabbc7e643da803 [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);
Misha Brukmand1c881a2005-04-21 22:43:08 +000051static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
53static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
55static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
57static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
59static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
60 const Type *Ty);
61static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
63static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
67static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
68 const Type *Ty);
69static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
70 const Type *Ty);
71static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
72 const Type *Ty);
73static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +000074 GenericValue Src3);
75
Brian Gaeke63438cc2003-12-11 00:22:59 +000076GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
77 ExecutionContext &SF) {
78 switch (CE->getOpcode()) {
79 case Instruction::Cast:
80 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
81 case Instruction::GetElementPtr:
82 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
83 gep_type_end(CE), SF);
84 case Instruction::Add:
85 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
86 getOperandValue(CE->getOperand(1), SF),
87 CE->getOperand(0)->getType());
88 case Instruction::Sub:
89 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Mul:
93 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +000096 case Instruction::SDiv:
97 return executeSDivInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
100 case Instruction::UDiv:
101 return executeUDivInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::FDiv:
105 return executeFDivInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
Brian Gaeke63438cc2003-12-11 00:22:59 +0000108 case Instruction::Rem:
109 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::And:
113 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::Or:
117 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::Xor:
121 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::SetEQ:
125 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::SetNE:
129 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::SetLE:
133 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::SetGE:
137 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::SetLT:
141 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
144 case Instruction::SetGT:
145 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 CE->getOperand(0)->getType());
148 case Instruction::Shl:
149 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
150 getOperandValue(CE->getOperand(1), SF),
151 CE->getOperand(0)->getType());
152 case Instruction::Shr:
153 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
154 getOperandValue(CE->getOperand(1), SF),
155 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000156 case Instruction::Select:
157 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
158 getOperandValue(CE->getOperand(1), SF),
159 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000160 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000161 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000162 abort();
163 return GenericValue();
164 }
165}
Chris Lattnera34c5682002-08-27 22:33:45 +0000166
Brian Gaeke29794cb2003-09-05 18:55:03 +0000167GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000168 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000169 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000170 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000171 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000172 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000173 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000174 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000175 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000176 }
177}
178
Chris Lattner39bb5b42001-10-15 13:25:40 +0000179static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000180 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000181}
182
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000183void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000184 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000185}
186
Chris Lattner2adcd832002-05-03 19:52:30 +0000187//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000188// Binary Instruction Implementations
189//===----------------------------------------------------------------------===//
190
191#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
192 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
193
Misha Brukmand1c881a2005-04-21 22:43:08 +0000194static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
195 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000196 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000197 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000198 IMPLEMENT_BINARY_OPERATOR(+, UByte);
199 IMPLEMENT_BINARY_OPERATOR(+, SByte);
200 IMPLEMENT_BINARY_OPERATOR(+, UShort);
201 IMPLEMENT_BINARY_OPERATOR(+, Short);
202 IMPLEMENT_BINARY_OPERATOR(+, UInt);
203 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000204 IMPLEMENT_BINARY_OPERATOR(+, ULong);
205 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000206 IMPLEMENT_BINARY_OPERATOR(+, Float);
207 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000208 default:
Chris Lattner02868352003-04-22 21:22:33 +0000209 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
210 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000211 }
212 return Dest;
213}
214
Misha Brukmand1c881a2005-04-21 22:43:08 +0000215static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
216 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000217 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000218 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000219 IMPLEMENT_BINARY_OPERATOR(-, UByte);
220 IMPLEMENT_BINARY_OPERATOR(-, SByte);
221 IMPLEMENT_BINARY_OPERATOR(-, UShort);
222 IMPLEMENT_BINARY_OPERATOR(-, Short);
223 IMPLEMENT_BINARY_OPERATOR(-, UInt);
224 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000225 IMPLEMENT_BINARY_OPERATOR(-, ULong);
226 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000227 IMPLEMENT_BINARY_OPERATOR(-, Float);
228 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000229 default:
Chris Lattner02868352003-04-22 21:22:33 +0000230 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
231 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000232 }
233 return Dest;
234}
235
Misha Brukmand1c881a2005-04-21 22:43:08 +0000236static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
237 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000238 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000239 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000240 IMPLEMENT_BINARY_OPERATOR(*, UByte);
241 IMPLEMENT_BINARY_OPERATOR(*, SByte);
242 IMPLEMENT_BINARY_OPERATOR(*, UShort);
243 IMPLEMENT_BINARY_OPERATOR(*, Short);
244 IMPLEMENT_BINARY_OPERATOR(*, UInt);
245 IMPLEMENT_BINARY_OPERATOR(*, Int);
246 IMPLEMENT_BINARY_OPERATOR(*, ULong);
247 IMPLEMENT_BINARY_OPERATOR(*, Long);
248 IMPLEMENT_BINARY_OPERATOR(*, Float);
249 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000250 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000251 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000252 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000253 }
254 return Dest;
255}
256
Reid Spencer1628cec2006-10-26 06:15:43 +0000257static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
258 const Type *Ty) {
259 GenericValue Dest;
260 if (Ty->isSigned())
261 Ty = Ty->getUnsignedVersion();
262 switch (Ty->getTypeID()) {
263 IMPLEMENT_BINARY_OPERATOR(/, UByte);
264 IMPLEMENT_BINARY_OPERATOR(/, UShort);
265 IMPLEMENT_BINARY_OPERATOR(/, UInt);
266 IMPLEMENT_BINARY_OPERATOR(/, ULong);
267 default:
268 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
269 abort();
270 }
271 return Dest;
272}
273
274static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
275 const Type *Ty) {
276 GenericValue Dest;
277 if (Ty->isUnsigned())
278 Ty = Ty->getSignedVersion();
279 switch (Ty->getTypeID()) {
280 IMPLEMENT_BINARY_OPERATOR(/, SByte);
281 IMPLEMENT_BINARY_OPERATOR(/, Short);
282 IMPLEMENT_BINARY_OPERATOR(/, Int);
283 IMPLEMENT_BINARY_OPERATOR(/, Long);
284 default:
285 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
286 abort();
287 }
288 return Dest;
289}
290
291static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000292 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000293 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000294 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000295 IMPLEMENT_BINARY_OPERATOR(/, Float);
296 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000297 default:
Chris Lattner02868352003-04-22 21:22:33 +0000298 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
299 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000300 }
301 return Dest;
302}
303
Misha Brukmand1c881a2005-04-21 22:43:08 +0000304static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
305 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000306 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000307 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000308 IMPLEMENT_BINARY_OPERATOR(%, UByte);
309 IMPLEMENT_BINARY_OPERATOR(%, SByte);
310 IMPLEMENT_BINARY_OPERATOR(%, UShort);
311 IMPLEMENT_BINARY_OPERATOR(%, Short);
312 IMPLEMENT_BINARY_OPERATOR(%, UInt);
313 IMPLEMENT_BINARY_OPERATOR(%, Int);
314 IMPLEMENT_BINARY_OPERATOR(%, ULong);
315 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000316 case Type::FloatTyID:
317 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
318 break;
319 case Type::DoubleTyID:
320 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
321 break;
322 default:
Chris Lattner02868352003-04-22 21:22:33 +0000323 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
324 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000325 }
326 return Dest;
327}
328
Misha Brukmand1c881a2005-04-21 22:43:08 +0000329static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
330 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000331 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000332 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000333 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000334 IMPLEMENT_BINARY_OPERATOR(&, UByte);
335 IMPLEMENT_BINARY_OPERATOR(&, SByte);
336 IMPLEMENT_BINARY_OPERATOR(&, UShort);
337 IMPLEMENT_BINARY_OPERATOR(&, Short);
338 IMPLEMENT_BINARY_OPERATOR(&, UInt);
339 IMPLEMENT_BINARY_OPERATOR(&, Int);
340 IMPLEMENT_BINARY_OPERATOR(&, ULong);
341 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000342 default:
Chris Lattner02868352003-04-22 21:22:33 +0000343 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
344 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000345 }
346 return Dest;
347}
348
Misha Brukmand1c881a2005-04-21 22:43:08 +0000349static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000350 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000351 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000352 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000353 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000354 IMPLEMENT_BINARY_OPERATOR(|, UByte);
355 IMPLEMENT_BINARY_OPERATOR(|, SByte);
356 IMPLEMENT_BINARY_OPERATOR(|, UShort);
357 IMPLEMENT_BINARY_OPERATOR(|, Short);
358 IMPLEMENT_BINARY_OPERATOR(|, UInt);
359 IMPLEMENT_BINARY_OPERATOR(|, Int);
360 IMPLEMENT_BINARY_OPERATOR(|, ULong);
361 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000362 default:
Chris Lattner02868352003-04-22 21:22:33 +0000363 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
364 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000365 }
366 return Dest;
367}
368
Misha Brukmand1c881a2005-04-21 22:43:08 +0000369static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000370 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000371 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000372 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000373 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000374 IMPLEMENT_BINARY_OPERATOR(^, UByte);
375 IMPLEMENT_BINARY_OPERATOR(^, SByte);
376 IMPLEMENT_BINARY_OPERATOR(^, UShort);
377 IMPLEMENT_BINARY_OPERATOR(^, Short);
378 IMPLEMENT_BINARY_OPERATOR(^, UInt);
379 IMPLEMENT_BINARY_OPERATOR(^, Int);
380 IMPLEMENT_BINARY_OPERATOR(^, ULong);
381 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000382 default:
Chris Lattner02868352003-04-22 21:22:33 +0000383 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
384 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000385 }
386 return Dest;
387}
388
Chris Lattner92101ac2001-08-23 17:05:04 +0000389#define IMPLEMENT_SETCC(OP, TY) \
390 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
391
Chris Lattnerfd506f52003-04-23 19:55:35 +0000392// Handle pointers specially because they must be compared with only as much
393// width as the host has. We _do not_ want to be comparing 64 bit values when
394// running on a 32-bit target, otherwise the upper 32 bits might mess up
395// comparisons if they contain garbage.
396#define IMPLEMENT_POINTERSETCC(OP) \
397 case Type::PointerTyID: \
398 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
399 (void*)(intptr_t)Src2.PointerVal; break
400
Misha Brukmand1c881a2005-04-21 22:43:08 +0000401static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
402 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000403 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000404 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000405 IMPLEMENT_SETCC(==, UByte);
406 IMPLEMENT_SETCC(==, SByte);
407 IMPLEMENT_SETCC(==, UShort);
408 IMPLEMENT_SETCC(==, Short);
409 IMPLEMENT_SETCC(==, UInt);
410 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000411 IMPLEMENT_SETCC(==, ULong);
412 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000413 IMPLEMENT_SETCC(==, Float);
414 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000415 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000416 default:
Chris Lattner02868352003-04-22 21:22:33 +0000417 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
418 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000419 }
420 return Dest;
421}
422
Misha Brukmand1c881a2005-04-21 22:43:08 +0000423static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
424 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000425 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000426 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000427 IMPLEMENT_SETCC(!=, UByte);
428 IMPLEMENT_SETCC(!=, SByte);
429 IMPLEMENT_SETCC(!=, UShort);
430 IMPLEMENT_SETCC(!=, Short);
431 IMPLEMENT_SETCC(!=, UInt);
432 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000433 IMPLEMENT_SETCC(!=, ULong);
434 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000435 IMPLEMENT_SETCC(!=, Float);
436 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000437 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000438
Chris Lattner92101ac2001-08-23 17:05:04 +0000439 default:
Chris Lattner02868352003-04-22 21:22:33 +0000440 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
441 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 }
443 return Dest;
444}
445
Misha Brukmand1c881a2005-04-21 22:43:08 +0000446static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
447 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000449 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000450 IMPLEMENT_SETCC(<=, UByte);
451 IMPLEMENT_SETCC(<=, SByte);
452 IMPLEMENT_SETCC(<=, UShort);
453 IMPLEMENT_SETCC(<=, Short);
454 IMPLEMENT_SETCC(<=, UInt);
455 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000456 IMPLEMENT_SETCC(<=, ULong);
457 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000458 IMPLEMENT_SETCC(<=, Float);
459 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000460 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000461 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000462 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000463 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 }
465 return Dest;
466}
467
Misha Brukmand1c881a2005-04-21 22:43:08 +0000468static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
469 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000471 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000472 IMPLEMENT_SETCC(>=, UByte);
473 IMPLEMENT_SETCC(>=, SByte);
474 IMPLEMENT_SETCC(>=, UShort);
475 IMPLEMENT_SETCC(>=, Short);
476 IMPLEMENT_SETCC(>=, UInt);
477 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000478 IMPLEMENT_SETCC(>=, ULong);
479 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000480 IMPLEMENT_SETCC(>=, Float);
481 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000482 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000483 default:
Chris Lattner02868352003-04-22 21:22:33 +0000484 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
485 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000486 }
487 return Dest;
488}
489
Misha Brukmand1c881a2005-04-21 22:43:08 +0000490static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
491 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000492 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000493 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000494 IMPLEMENT_SETCC(<, UByte);
495 IMPLEMENT_SETCC(<, SByte);
496 IMPLEMENT_SETCC(<, UShort);
497 IMPLEMENT_SETCC(<, Short);
498 IMPLEMENT_SETCC(<, UInt);
499 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000500 IMPLEMENT_SETCC(<, ULong);
501 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000502 IMPLEMENT_SETCC(<, Float);
503 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000504 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 default:
Chris Lattner02868352003-04-22 21:22:33 +0000506 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
507 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 }
509 return Dest;
510}
511
Misha Brukmand1c881a2005-04-21 22:43:08 +0000512static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
513 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000514 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000515 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000516 IMPLEMENT_SETCC(>, UByte);
517 IMPLEMENT_SETCC(>, SByte);
518 IMPLEMENT_SETCC(>, UShort);
519 IMPLEMENT_SETCC(>, Short);
520 IMPLEMENT_SETCC(>, UInt);
521 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000522 IMPLEMENT_SETCC(>, ULong);
523 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000524 IMPLEMENT_SETCC(>, Float);
525 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000526 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 default:
Chris Lattner02868352003-04-22 21:22:33 +0000528 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
529 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000530 }
531 return Dest;
532}
533
Chris Lattnerd7916e92003-05-10 21:22:39 +0000534void Interpreter::visitBinaryOperator(BinaryOperator &I) {
535 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000536 const Type *Ty = I.getOperand(0)->getType();
537 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
538 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000539 GenericValue R; // Result
540
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000541 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000542 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
543 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
544 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000545 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
546 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
547 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000548 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
549 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
550 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
551 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
552 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
553 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
554 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
555 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
556 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
557 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000558 default:
Chris Lattner02868352003-04-22 21:22:33 +0000559 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
560 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000561 }
562
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000563 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000564}
565
Misha Brukmand1c881a2005-04-21 22:43:08 +0000566static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000567 GenericValue Src3) {
568 return Src1.BoolVal ? Src2 : Src3;
569}
570
571void Interpreter::visitSelectInst(SelectInst &I) {
572 ExecutionContext &SF = ECStack.back();
573 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
574 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
575 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
576 GenericValue R = executeSelectInst(Src1, Src2, Src3);
577 SetValue(&I, R, SF);
578}
579
580
Chris Lattner92101ac2001-08-23 17:05:04 +0000581//===----------------------------------------------------------------------===//
582// Terminator Instruction Implementations
583//===----------------------------------------------------------------------===//
584
Chris Lattnere43db882001-10-27 04:15:57 +0000585void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000586 // runAtExitHandlers() assumes there are no stack frames, but
587 // if exit() was called, then it had a stack frame. Blow away
588 // the stack before interpreting atexit handlers.
589 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000590 runAtExitHandlers ();
591 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000592}
593
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000594/// Pop the last stack frame off of ECStack and then copy the result
595/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000596/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000597/// CallInst if there was a previous stack frame. This method may
598/// invalidate any ECStack iterators you have. This method also takes
599/// care of switching to the normal destination BB, if we are returning
600/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000601///
602void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
603 GenericValue Result) {
604 // Pop the current stack frame.
605 ECStack.pop_back();
606
Misha Brukmand1c881a2005-04-21 22:43:08 +0000607 if (ECStack.empty()) { // Finished main. Put result into exit code...
608 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000609 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000610 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000611 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000612 }
613 } else {
614 // If we have a previous stack frame, and we have a previous call,
615 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000616 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000617 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000618 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000619 SetValue(I, Result, CallingSF);
620 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
621 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000622 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000623 }
624 }
625}
626
Chris Lattnerd7916e92003-05-10 21:22:39 +0000627void Interpreter::visitReturnInst(ReturnInst &I) {
628 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000629 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000630 GenericValue Result;
631
632 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000633 if (I.getNumOperands()) {
634 RetTy = I.getReturnValue()->getType();
635 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000636 }
637
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000638 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000639}
640
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000641void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000642 // Unwind stack
643 Instruction *Inst;
644 do {
645 ECStack.pop_back ();
646 if (ECStack.empty ())
647 abort ();
648 Inst = ECStack.back ().Caller.getInstruction ();
649 } while (!(Inst && isa<InvokeInst> (Inst)));
650
651 // Return from invoke
652 ExecutionContext &InvokingSF = ECStack.back ();
653 InvokingSF.Caller = CallSite ();
654
655 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000656 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000657}
658
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000659void Interpreter::visitUnreachableInst(UnreachableInst &I) {
660 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
661 abort();
662}
663
Chris Lattnerd7916e92003-05-10 21:22:39 +0000664void Interpreter::visitBranchInst(BranchInst &I) {
665 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000666 BasicBlock *Dest;
667
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000668 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
669 if (!I.isUnconditional()) {
670 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000671 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000672 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000673 }
Chris Lattner77113b62003-05-10 20:21:16 +0000674 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000675}
676
Chris Lattnerd7916e92003-05-10 21:22:39 +0000677void Interpreter::visitSwitchInst(SwitchInst &I) {
678 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000679 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
680 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000681
682 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000683 BasicBlock *Dest = 0;
684 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000685 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000686 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000687 Dest = cast<BasicBlock>(I.getOperand(i+1));
688 break;
689 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000690
Chris Lattner09e93922003-04-22 20:34:47 +0000691 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000692 SwitchToNewBasicBlock(Dest, SF);
693}
694
695// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
696// This function handles the actual updating of block and instruction iterators
697// as well as execution of all of the PHI nodes in the destination block.
698//
699// This method does this because all of the PHI nodes must be executed
700// atomically, reading their inputs before any of the results are updated. Not
701// doing this can cause problems if the PHI nodes depend on other PHI nodes for
702// their inputs. If the input PHI node is updated before it is read, incorrect
703// results can happen. Thus we use a two phase approach.
704//
705void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
706 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
707 SF.CurBB = Dest; // Update CurBB to branch destination
708 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
709
710 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
711
712 // Loop over all of the PHI nodes in the current block, reading their inputs.
713 std::vector<GenericValue> ResultValues;
714
715 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
716 // Search for the value corresponding to this previous bb...
717 int i = PN->getBasicBlockIndex(PrevBB);
718 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
719 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000720
Chris Lattner77113b62003-05-10 20:21:16 +0000721 // Save the incoming value for this PHI node...
722 ResultValues.push_back(getOperandValue(IncomingValue, SF));
723 }
724
725 // Now loop over all of the PHI nodes setting their values...
726 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000727 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
728 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000729 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000730 }
Chris Lattner09e93922003-04-22 20:34:47 +0000731}
732
Chris Lattner92101ac2001-08-23 17:05:04 +0000733//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000734// Memory Instruction Implementations
735//===----------------------------------------------------------------------===//
736
Chris Lattnerd7916e92003-05-10 21:22:39 +0000737void Interpreter::visitAllocationInst(AllocationInst &I) {
738 ExecutionContext &SF = ECStack.back();
739
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000740 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000741
Chris Lattnercc82cc12002-04-28 21:57:33 +0000742 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000743 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000744
745 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000746 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000747
Chris Lattnerfe11a972002-12-23 23:59:41 +0000748 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000749 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000750 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000751
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000752 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000753 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000754}
755
Chris Lattnerd7916e92003-05-10 21:22:39 +0000756void Interpreter::visitFreeInst(FreeInst &I) {
757 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000758 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
759 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000760 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000761 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000762}
763
Chris Lattnera34c5682002-08-27 22:33:45 +0000764// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000765//
Chris Lattner4af6de82003-11-25 20:44:56 +0000766GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000767 gep_type_iterator E,
768 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000769 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000770 "Cannot getElementOffset of a nonpointer type!");
771
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000772 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000773
774 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000775 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000776 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000777
Reid Spencerb83eb642006-10-20 07:07:24 +0000778 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
779 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000780
Chris Lattnerd5644962005-01-08 20:05:34 +0000781 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000782 } else {
783 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000784 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000785 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
786
787 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000788 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000789 default: assert(0 && "Illegal getelementptr index for sequential type!");
790 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
791 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
792 case Type::IntTyID: Idx = IdxGV.IntVal; break;
793 case Type::LongTyID: Idx = IdxGV.LongVal; break;
794 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
795 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
796 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
797 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
798 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000799 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000800 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000801 }
802
Chris Lattnera34c5682002-08-27 22:33:45 +0000803 GenericValue Result;
804 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
805 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000806}
807
Chris Lattnerd7916e92003-05-10 21:22:39 +0000808void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
809 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000810 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000811 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000812}
813
Chris Lattnerd7916e92003-05-10 21:22:39 +0000814void Interpreter::visitLoadInst(LoadInst &I) {
815 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000816 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000817 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000818 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000819 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000820}
821
Chris Lattnerd7916e92003-05-10 21:22:39 +0000822void Interpreter::visitStoreInst(StoreInst &I) {
823 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000824 GenericValue Val = getOperandValue(I.getOperand(0), SF);
825 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000826 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000827 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000828}
829
Chris Lattner86660982001-08-27 05:16:50 +0000830//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000831// Miscellaneous Instruction Implementations
832//===----------------------------------------------------------------------===//
833
Brian Gaekefea483d2003-11-07 20:04:22 +0000834void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000835 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000836
837 // Check to see if this is an intrinsic function call...
838 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000839 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000840 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000841 case Intrinsic::not_intrinsic:
842 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000843 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000844 GenericValue ArgIndex;
845 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
846 ArgIndex.UIntPairVal.second = 0;
847 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000848 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000849 }
Chris Lattner317201d2004-03-13 00:24:00 +0000850 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000851 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000852 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000853 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
854 return;
855 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000856 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000857 // class to transform it into hopefully tasty LLVM code.
858 //
859 Instruction *Prev = CS.getInstruction()->getPrev();
860 BasicBlock *Parent = CS.getInstruction()->getParent();
861 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
862
863 // Restore the CurInst pointer to the first instruction newly inserted, if
864 // any.
865 if (!Prev) {
866 SF.CurInst = Parent->begin();
867 } else {
868 SF.CurInst = Prev;
869 ++SF.CurInst;
870 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000871 return;
Chris Lattner73011782003-12-28 09:44:37 +0000872 }
873
Brian Gaekefea483d2003-11-07 20:04:22 +0000874 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000875 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000876 const unsigned NumArgs = SF.Caller.arg_size();
877 ArgVals.reserve(NumArgs);
878 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
879 e = SF.Caller.arg_end(); i != e; ++i) {
880 Value *V = *i;
881 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000882 // Promote all integral types whose size is < sizeof(int) into ints. We do
883 // this by zero or sign extending the value as appropriate according to the
884 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000885 const Type *Ty = V->getType();
886 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000887 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000888 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000889 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000890 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000891 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000892 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000893 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000894 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000895 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000896 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000897 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000898 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000899 }
900 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000901
Misha Brukmand1c881a2005-04-21 22:43:08 +0000902 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000903 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000904 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000905 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000906}
907
Chris Lattner86660982001-08-27 05:16:50 +0000908#define IMPLEMENT_SHIFT(OP, TY) \
909 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
910
Brian Gaeke63438cc2003-12-11 00:22:59 +0000911static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
912 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000913 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000914 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000915 IMPLEMENT_SHIFT(<<, UByte);
916 IMPLEMENT_SHIFT(<<, SByte);
917 IMPLEMENT_SHIFT(<<, UShort);
918 IMPLEMENT_SHIFT(<<, Short);
919 IMPLEMENT_SHIFT(<<, UInt);
920 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000921 IMPLEMENT_SHIFT(<<, ULong);
922 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000923 default:
Chris Lattner02868352003-04-22 21:22:33 +0000924 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000925 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000926 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000927}
928
Brian Gaeke63438cc2003-12-11 00:22:59 +0000929static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
930 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000931 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000932 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000933 IMPLEMENT_SHIFT(>>, UByte);
934 IMPLEMENT_SHIFT(>>, SByte);
935 IMPLEMENT_SHIFT(>>, UShort);
936 IMPLEMENT_SHIFT(>>, Short);
937 IMPLEMENT_SHIFT(>>, UInt);
938 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000939 IMPLEMENT_SHIFT(>>, ULong);
940 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000941 default:
Chris Lattner02868352003-04-22 21:22:33 +0000942 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
943 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000944 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000945 return Dest;
946}
947
948void Interpreter::visitShl(ShiftInst &I) {
949 ExecutionContext &SF = ECStack.back();
950 const Type *Ty = I.getOperand(0)->getType();
951 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
952 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
953 GenericValue Dest;
954 Dest = executeShlInst (Src1, Src2, Ty);
955 SetValue(&I, Dest, SF);
956}
957
958void Interpreter::visitShr(ShiftInst &I) {
959 ExecutionContext &SF = ECStack.back();
960 const Type *Ty = I.getOperand(0)->getType();
961 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
962 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
963 GenericValue Dest;
964 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000965 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000966}
967
968#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000969 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000970
971#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
972 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000973 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000974 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000975 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
976 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
979 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000980 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000982 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
983 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000984
985#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
986 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
987 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
988
989#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +0000990 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +0000991 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000992 } \
993 break
994
995#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
996 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
997 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000998 IMPLEMENT_CAST_CASE_END()
999
Brian Gaeke29794cb2003-09-05 18:55:03 +00001000GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001001 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001002 const Type *SrcTy = SrcVal->getType();
1003 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001004
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001005 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001006 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1007 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1008 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001009 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001010 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1011 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1012 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1013 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001014 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001015 IMPLEMENT_CAST_CASE(Float , (float));
1016 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001017 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001018 default:
Chris Lattner02868352003-04-22 21:22:33 +00001019 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001020 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001021 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001022
1023 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001024}
Chris Lattner92101ac2001-08-23 17:05:04 +00001025
Chris Lattnerd7916e92003-05-10 21:22:39 +00001026void Interpreter::visitCastInst(CastInst &I) {
1027 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001028 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1029}
Chris Lattner92101ac2001-08-23 17:05:04 +00001030
Brian Gaekec1a2be12003-11-07 21:20:47 +00001031#define IMPLEMENT_VAARG(TY) \
1032 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1033
1034void Interpreter::visitVAArgInst(VAArgInst &I) {
1035 ExecutionContext &SF = ECStack.back();
1036
Brian Gaeke9d20b712004-02-25 23:01:48 +00001037 // Get the incoming valist parameter. LLI treats the valist as a
1038 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001039 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001040 GenericValue Dest;
1041 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001042 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001043 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001044 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001045 IMPLEMENT_VAARG(UByte);
1046 IMPLEMENT_VAARG(SByte);
1047 IMPLEMENT_VAARG(UShort);
1048 IMPLEMENT_VAARG(Short);
1049 IMPLEMENT_VAARG(UInt);
1050 IMPLEMENT_VAARG(Int);
1051 IMPLEMENT_VAARG(ULong);
1052 IMPLEMENT_VAARG(Long);
1053 IMPLEMENT_VAARG(Pointer);
1054 IMPLEMENT_VAARG(Float);
1055 IMPLEMENT_VAARG(Double);
1056 IMPLEMENT_VAARG(Bool);
1057 default:
1058 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1059 abort();
1060 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001061
Brian Gaekec1a2be12003-11-07 21:20:47 +00001062 // Set the Value of this Instruction.
1063 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001064
1065 // Move the pointer to the next vararg.
1066 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001067}
1068
Chris Lattner92101ac2001-08-23 17:05:04 +00001069//===----------------------------------------------------------------------===//
1070// Dispatch and Execution Code
1071//===----------------------------------------------------------------------===//
1072
Chris Lattner92101ac2001-08-23 17:05:04 +00001073//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001074// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001075//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001076void Interpreter::callFunction(Function *F,
1077 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001078 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1079 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1080 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001081 // Make a new stack frame... and fill it in.
1082 ECStack.push_back(ExecutionContext());
1083 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001084 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001085
1086 // Special handling for external functions.
1087 if (F->isExternal()) {
1088 GenericValue Result = callExternalFunction (F, ArgVals);
1089 // Simulate a 'ret' instruction of the appropriate type.
1090 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1091 return;
1092 }
1093
1094 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001095 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001096 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001097
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001098 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001099 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001100 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001101 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001102
1103 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001104 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001105 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001106 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001107
1108 // Handle varargs arguments...
1109 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001110}
1111
Chris Lattner92101ac2001-08-23 17:05:04 +00001112void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001113 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001114 // Interpret a single instruction & increment the "PC".
1115 ExecutionContext &SF = ECStack.back(); // Current stack frame
1116 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001117
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001118 // Track the number of dynamic instructions executed.
1119 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001120
Brian Gaeke63438cc2003-12-11 00:22:59 +00001121 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001122 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001123 }
1124}