blob: c10dbbd12369fc5b7ad7f942def7aa0f78cd24e9 [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 Spencerfe855262006-11-01 03:41:05 +0000257#define IMPLEMENT_SIGNLESS_BINOP(OP, TY1, TY2) \
258 case Type::TY2##TyID: IMPLEMENT_BINARY_OPERATOR(OP, TY1)
259
Reid Spencer1628cec2006-10-26 06:15:43 +0000260static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2,
261 const Type *Ty) {
262 GenericValue Dest;
263 if (Ty->isSigned())
264 Ty = Ty->getUnsignedVersion();
265 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000266 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
267 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
268 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
269 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000270 default:
271 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
272 abort();
273 }
274 return Dest;
275}
276
277static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
278 const Type *Ty) {
279 GenericValue Dest;
280 if (Ty->isUnsigned())
281 Ty = Ty->getSignedVersion();
282 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000283 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
284 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
285 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
286 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000287 default:
288 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
289 abort();
290 }
291 return Dest;
292}
293
294static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000295 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000296 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000297 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000298 IMPLEMENT_BINARY_OPERATOR(/, Float);
299 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000300 default:
Chris Lattner02868352003-04-22 21:22:33 +0000301 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
302 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000303 }
304 return Dest;
305}
306
Misha Brukmand1c881a2005-04-21 22:43:08 +0000307static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
308 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000309 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000310 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000311 IMPLEMENT_BINARY_OPERATOR(%, UByte);
312 IMPLEMENT_BINARY_OPERATOR(%, SByte);
313 IMPLEMENT_BINARY_OPERATOR(%, UShort);
314 IMPLEMENT_BINARY_OPERATOR(%, Short);
315 IMPLEMENT_BINARY_OPERATOR(%, UInt);
316 IMPLEMENT_BINARY_OPERATOR(%, Int);
317 IMPLEMENT_BINARY_OPERATOR(%, ULong);
318 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000319 case Type::FloatTyID:
320 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
321 break;
322 case Type::DoubleTyID:
323 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
324 break;
325 default:
Chris Lattner02868352003-04-22 21:22:33 +0000326 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
327 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000328 }
329 return Dest;
330}
331
Misha Brukmand1c881a2005-04-21 22:43:08 +0000332static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
333 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000334 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000335 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000336 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000337 IMPLEMENT_BINARY_OPERATOR(&, UByte);
338 IMPLEMENT_BINARY_OPERATOR(&, SByte);
339 IMPLEMENT_BINARY_OPERATOR(&, UShort);
340 IMPLEMENT_BINARY_OPERATOR(&, Short);
341 IMPLEMENT_BINARY_OPERATOR(&, UInt);
342 IMPLEMENT_BINARY_OPERATOR(&, Int);
343 IMPLEMENT_BINARY_OPERATOR(&, ULong);
344 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000345 default:
Chris Lattner02868352003-04-22 21:22:33 +0000346 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
347 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000348 }
349 return Dest;
350}
351
Misha Brukmand1c881a2005-04-21 22:43:08 +0000352static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000353 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000354 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000355 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000356 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000357 IMPLEMENT_BINARY_OPERATOR(|, UByte);
358 IMPLEMENT_BINARY_OPERATOR(|, SByte);
359 IMPLEMENT_BINARY_OPERATOR(|, UShort);
360 IMPLEMENT_BINARY_OPERATOR(|, Short);
361 IMPLEMENT_BINARY_OPERATOR(|, UInt);
362 IMPLEMENT_BINARY_OPERATOR(|, Int);
363 IMPLEMENT_BINARY_OPERATOR(|, ULong);
364 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000365 default:
Chris Lattner02868352003-04-22 21:22:33 +0000366 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
367 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000368 }
369 return Dest;
370}
371
Misha Brukmand1c881a2005-04-21 22:43:08 +0000372static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000373 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000374 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000375 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000376 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000377 IMPLEMENT_BINARY_OPERATOR(^, UByte);
378 IMPLEMENT_BINARY_OPERATOR(^, SByte);
379 IMPLEMENT_BINARY_OPERATOR(^, UShort);
380 IMPLEMENT_BINARY_OPERATOR(^, Short);
381 IMPLEMENT_BINARY_OPERATOR(^, UInt);
382 IMPLEMENT_BINARY_OPERATOR(^, Int);
383 IMPLEMENT_BINARY_OPERATOR(^, ULong);
384 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000385 default:
Chris Lattner02868352003-04-22 21:22:33 +0000386 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
387 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000388 }
389 return Dest;
390}
391
Chris Lattner92101ac2001-08-23 17:05:04 +0000392#define IMPLEMENT_SETCC(OP, TY) \
393 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
394
Chris Lattnerfd506f52003-04-23 19:55:35 +0000395// Handle pointers specially because they must be compared with only as much
396// width as the host has. We _do not_ want to be comparing 64 bit values when
397// running on a 32-bit target, otherwise the upper 32 bits might mess up
398// comparisons if they contain garbage.
399#define IMPLEMENT_POINTERSETCC(OP) \
400 case Type::PointerTyID: \
401 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
402 (void*)(intptr_t)Src2.PointerVal; break
403
Misha Brukmand1c881a2005-04-21 22:43:08 +0000404static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
405 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000406 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000407 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000408 IMPLEMENT_SETCC(==, UByte);
409 IMPLEMENT_SETCC(==, SByte);
410 IMPLEMENT_SETCC(==, UShort);
411 IMPLEMENT_SETCC(==, Short);
412 IMPLEMENT_SETCC(==, UInt);
413 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000414 IMPLEMENT_SETCC(==, ULong);
415 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000416 IMPLEMENT_SETCC(==, Float);
417 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000418 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000419 default:
Chris Lattner02868352003-04-22 21:22:33 +0000420 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
421 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000422 }
423 return Dest;
424}
425
Misha Brukmand1c881a2005-04-21 22:43:08 +0000426static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
427 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000428 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000429 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000430 IMPLEMENT_SETCC(!=, UByte);
431 IMPLEMENT_SETCC(!=, SByte);
432 IMPLEMENT_SETCC(!=, UShort);
433 IMPLEMENT_SETCC(!=, Short);
434 IMPLEMENT_SETCC(!=, UInt);
435 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000436 IMPLEMENT_SETCC(!=, ULong);
437 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 IMPLEMENT_SETCC(!=, Float);
439 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000440 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000441
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 default:
Chris Lattner02868352003-04-22 21:22:33 +0000443 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
444 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000445 }
446 return Dest;
447}
448
Misha Brukmand1c881a2005-04-21 22:43:08 +0000449static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
450 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000452 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 IMPLEMENT_SETCC(<=, UByte);
454 IMPLEMENT_SETCC(<=, SByte);
455 IMPLEMENT_SETCC(<=, UShort);
456 IMPLEMENT_SETCC(<=, Short);
457 IMPLEMENT_SETCC(<=, UInt);
458 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000459 IMPLEMENT_SETCC(<=, ULong);
460 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000461 IMPLEMENT_SETCC(<=, Float);
462 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000463 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000465 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000466 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 }
468 return Dest;
469}
470
Misha Brukmand1c881a2005-04-21 22:43:08 +0000471static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
472 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000474 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 IMPLEMENT_SETCC(>=, UByte);
476 IMPLEMENT_SETCC(>=, SByte);
477 IMPLEMENT_SETCC(>=, UShort);
478 IMPLEMENT_SETCC(>=, Short);
479 IMPLEMENT_SETCC(>=, UInt);
480 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000481 IMPLEMENT_SETCC(>=, ULong);
482 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000483 IMPLEMENT_SETCC(>=, Float);
484 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000485 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000486 default:
Chris Lattner02868352003-04-22 21:22:33 +0000487 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
488 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000489 }
490 return Dest;
491}
492
Misha Brukmand1c881a2005-04-21 22:43:08 +0000493static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
494 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000495 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000496 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000497 IMPLEMENT_SETCC(<, UByte);
498 IMPLEMENT_SETCC(<, SByte);
499 IMPLEMENT_SETCC(<, UShort);
500 IMPLEMENT_SETCC(<, Short);
501 IMPLEMENT_SETCC(<, UInt);
502 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000503 IMPLEMENT_SETCC(<, ULong);
504 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 IMPLEMENT_SETCC(<, Float);
506 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000507 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000508 default:
Chris Lattner02868352003-04-22 21:22:33 +0000509 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
510 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000511 }
512 return Dest;
513}
514
Misha Brukmand1c881a2005-04-21 22:43:08 +0000515static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
516 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000517 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000518 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000519 IMPLEMENT_SETCC(>, UByte);
520 IMPLEMENT_SETCC(>, SByte);
521 IMPLEMENT_SETCC(>, UShort);
522 IMPLEMENT_SETCC(>, Short);
523 IMPLEMENT_SETCC(>, UInt);
524 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000525 IMPLEMENT_SETCC(>, ULong);
526 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000527 IMPLEMENT_SETCC(>, Float);
528 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000529 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000530 default:
Chris Lattner02868352003-04-22 21:22:33 +0000531 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
532 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000533 }
534 return Dest;
535}
536
Chris Lattnerd7916e92003-05-10 21:22:39 +0000537void Interpreter::visitBinaryOperator(BinaryOperator &I) {
538 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000539 const Type *Ty = I.getOperand(0)->getType();
540 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
541 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000542 GenericValue R; // Result
543
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000544 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000545 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
546 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
547 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000548 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
549 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
550 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000551 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
552 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
553 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
554 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
555 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
556 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
557 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
558 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
559 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
560 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000561 default:
Chris Lattner02868352003-04-22 21:22:33 +0000562 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
563 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000564 }
565
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000566 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000567}
568
Misha Brukmand1c881a2005-04-21 22:43:08 +0000569static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000570 GenericValue Src3) {
571 return Src1.BoolVal ? Src2 : Src3;
572}
573
574void Interpreter::visitSelectInst(SelectInst &I) {
575 ExecutionContext &SF = ECStack.back();
576 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
577 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
578 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
579 GenericValue R = executeSelectInst(Src1, Src2, Src3);
580 SetValue(&I, R, SF);
581}
582
583
Chris Lattner92101ac2001-08-23 17:05:04 +0000584//===----------------------------------------------------------------------===//
585// Terminator Instruction Implementations
586//===----------------------------------------------------------------------===//
587
Chris Lattnere43db882001-10-27 04:15:57 +0000588void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000589 // runAtExitHandlers() assumes there are no stack frames, but
590 // if exit() was called, then it had a stack frame. Blow away
591 // the stack before interpreting atexit handlers.
592 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000593 runAtExitHandlers ();
594 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000595}
596
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000597/// Pop the last stack frame off of ECStack and then copy the result
598/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000599/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000600/// CallInst if there was a previous stack frame. This method may
601/// invalidate any ECStack iterators you have. This method also takes
602/// care of switching to the normal destination BB, if we are returning
603/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000604///
605void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
606 GenericValue Result) {
607 // Pop the current stack frame.
608 ECStack.pop_back();
609
Misha Brukmand1c881a2005-04-21 22:43:08 +0000610 if (ECStack.empty()) { // Finished main. Put result into exit code...
611 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000612 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000613 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000614 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000615 }
616 } else {
617 // If we have a previous stack frame, and we have a previous call,
618 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000619 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000620 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000621 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000622 SetValue(I, Result, CallingSF);
623 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
624 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000625 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000626 }
627 }
628}
629
Chris Lattnerd7916e92003-05-10 21:22:39 +0000630void Interpreter::visitReturnInst(ReturnInst &I) {
631 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000632 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000633 GenericValue Result;
634
635 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000636 if (I.getNumOperands()) {
637 RetTy = I.getReturnValue()->getType();
638 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000639 }
640
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000641 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000642}
643
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000644void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000645 // Unwind stack
646 Instruction *Inst;
647 do {
648 ECStack.pop_back ();
649 if (ECStack.empty ())
650 abort ();
651 Inst = ECStack.back ().Caller.getInstruction ();
652 } while (!(Inst && isa<InvokeInst> (Inst)));
653
654 // Return from invoke
655 ExecutionContext &InvokingSF = ECStack.back ();
656 InvokingSF.Caller = CallSite ();
657
658 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000659 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000660}
661
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000662void Interpreter::visitUnreachableInst(UnreachableInst &I) {
663 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
664 abort();
665}
666
Chris Lattnerd7916e92003-05-10 21:22:39 +0000667void Interpreter::visitBranchInst(BranchInst &I) {
668 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000669 BasicBlock *Dest;
670
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000671 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
672 if (!I.isUnconditional()) {
673 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000674 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000675 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000676 }
Chris Lattner77113b62003-05-10 20:21:16 +0000677 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000678}
679
Chris Lattnerd7916e92003-05-10 21:22:39 +0000680void Interpreter::visitSwitchInst(SwitchInst &I) {
681 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000682 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
683 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000684
685 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000686 BasicBlock *Dest = 0;
687 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000688 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000689 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000690 Dest = cast<BasicBlock>(I.getOperand(i+1));
691 break;
692 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000693
Chris Lattner09e93922003-04-22 20:34:47 +0000694 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000695 SwitchToNewBasicBlock(Dest, SF);
696}
697
698// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
699// This function handles the actual updating of block and instruction iterators
700// as well as execution of all of the PHI nodes in the destination block.
701//
702// This method does this because all of the PHI nodes must be executed
703// atomically, reading their inputs before any of the results are updated. Not
704// doing this can cause problems if the PHI nodes depend on other PHI nodes for
705// their inputs. If the input PHI node is updated before it is read, incorrect
706// results can happen. Thus we use a two phase approach.
707//
708void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
709 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
710 SF.CurBB = Dest; // Update CurBB to branch destination
711 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
712
713 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
714
715 // Loop over all of the PHI nodes in the current block, reading their inputs.
716 std::vector<GenericValue> ResultValues;
717
718 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
719 // Search for the value corresponding to this previous bb...
720 int i = PN->getBasicBlockIndex(PrevBB);
721 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
722 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000723
Chris Lattner77113b62003-05-10 20:21:16 +0000724 // Save the incoming value for this PHI node...
725 ResultValues.push_back(getOperandValue(IncomingValue, SF));
726 }
727
728 // Now loop over all of the PHI nodes setting their values...
729 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000730 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
731 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000732 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000733 }
Chris Lattner09e93922003-04-22 20:34:47 +0000734}
735
Chris Lattner92101ac2001-08-23 17:05:04 +0000736//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000737// Memory Instruction Implementations
738//===----------------------------------------------------------------------===//
739
Chris Lattnerd7916e92003-05-10 21:22:39 +0000740void Interpreter::visitAllocationInst(AllocationInst &I) {
741 ExecutionContext &SF = ECStack.back();
742
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000743 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000744
Chris Lattnercc82cc12002-04-28 21:57:33 +0000745 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000746 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000747
748 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000749 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000750
Chris Lattnerfe11a972002-12-23 23:59:41 +0000751 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000752 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000753 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000754
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000755 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000756 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000757}
758
Chris Lattnerd7916e92003-05-10 21:22:39 +0000759void Interpreter::visitFreeInst(FreeInst &I) {
760 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000761 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
762 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000763 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000764 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000765}
766
Chris Lattnera34c5682002-08-27 22:33:45 +0000767// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000768//
Chris Lattner4af6de82003-11-25 20:44:56 +0000769GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000770 gep_type_iterator E,
771 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000772 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000773 "Cannot getElementOffset of a nonpointer type!");
774
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000775 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000776
777 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000778 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000779 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000780
Reid Spencerb83eb642006-10-20 07:07:24 +0000781 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
782 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000783
Chris Lattnerd5644962005-01-08 20:05:34 +0000784 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000785 } else {
786 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000787 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000788 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
789
790 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000791 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000792 default: assert(0 && "Illegal getelementptr index for sequential type!");
793 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
794 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
795 case Type::IntTyID: Idx = IdxGV.IntVal; break;
796 case Type::LongTyID: Idx = IdxGV.LongVal; break;
797 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
798 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
799 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
800 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
801 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000802 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000803 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000804 }
805
Chris Lattnera34c5682002-08-27 22:33:45 +0000806 GenericValue Result;
807 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
808 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000809}
810
Chris Lattnerd7916e92003-05-10 21:22:39 +0000811void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
812 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000813 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000814 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000815}
816
Chris Lattnerd7916e92003-05-10 21:22:39 +0000817void Interpreter::visitLoadInst(LoadInst &I) {
818 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000819 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000820 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000821 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000822 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000823}
824
Chris Lattnerd7916e92003-05-10 21:22:39 +0000825void Interpreter::visitStoreInst(StoreInst &I) {
826 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000827 GenericValue Val = getOperandValue(I.getOperand(0), SF);
828 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000829 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000830 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000831}
832
Chris Lattner86660982001-08-27 05:16:50 +0000833//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000834// Miscellaneous Instruction Implementations
835//===----------------------------------------------------------------------===//
836
Brian Gaekefea483d2003-11-07 20:04:22 +0000837void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000838 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000839
840 // Check to see if this is an intrinsic function call...
841 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000842 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000843 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000844 case Intrinsic::not_intrinsic:
845 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000846 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000847 GenericValue ArgIndex;
848 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
849 ArgIndex.UIntPairVal.second = 0;
850 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000851 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000852 }
Chris Lattner317201d2004-03-13 00:24:00 +0000853 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000854 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000855 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000856 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
857 return;
858 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000859 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000860 // class to transform it into hopefully tasty LLVM code.
861 //
862 Instruction *Prev = CS.getInstruction()->getPrev();
863 BasicBlock *Parent = CS.getInstruction()->getParent();
864 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
865
866 // Restore the CurInst pointer to the first instruction newly inserted, if
867 // any.
868 if (!Prev) {
869 SF.CurInst = Parent->begin();
870 } else {
871 SF.CurInst = Prev;
872 ++SF.CurInst;
873 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000874 return;
Chris Lattner73011782003-12-28 09:44:37 +0000875 }
876
Brian Gaekefea483d2003-11-07 20:04:22 +0000877 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000878 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000879 const unsigned NumArgs = SF.Caller.arg_size();
880 ArgVals.reserve(NumArgs);
881 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
882 e = SF.Caller.arg_end(); i != e; ++i) {
883 Value *V = *i;
884 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000885 // Promote all integral types whose size is < sizeof(int) into ints. We do
886 // this by zero or sign extending the value as appropriate according to the
887 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000888 const Type *Ty = V->getType();
889 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000890 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000891 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000892 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000893 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000894 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000895 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000896 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000897 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000898 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000899 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000900 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000901 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000902 }
903 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000904
Misha Brukmand1c881a2005-04-21 22:43:08 +0000905 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000906 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000907 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000908 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000909}
910
Chris Lattner86660982001-08-27 05:16:50 +0000911#define IMPLEMENT_SHIFT(OP, TY) \
912 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
913
Brian Gaeke63438cc2003-12-11 00:22:59 +0000914static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
915 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000916 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000917 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000918 IMPLEMENT_SHIFT(<<, UByte);
919 IMPLEMENT_SHIFT(<<, SByte);
920 IMPLEMENT_SHIFT(<<, UShort);
921 IMPLEMENT_SHIFT(<<, Short);
922 IMPLEMENT_SHIFT(<<, UInt);
923 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000924 IMPLEMENT_SHIFT(<<, ULong);
925 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000926 default:
Chris Lattner02868352003-04-22 21:22:33 +0000927 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000928 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000929 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000930}
931
Brian Gaeke63438cc2003-12-11 00:22:59 +0000932static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
933 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000934 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000935 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000936 IMPLEMENT_SHIFT(>>, UByte);
937 IMPLEMENT_SHIFT(>>, SByte);
938 IMPLEMENT_SHIFT(>>, UShort);
939 IMPLEMENT_SHIFT(>>, Short);
940 IMPLEMENT_SHIFT(>>, UInt);
941 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000942 IMPLEMENT_SHIFT(>>, ULong);
943 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000944 default:
Chris Lattner02868352003-04-22 21:22:33 +0000945 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
946 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000947 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000948 return Dest;
949}
950
951void Interpreter::visitShl(ShiftInst &I) {
952 ExecutionContext &SF = ECStack.back();
953 const Type *Ty = I.getOperand(0)->getType();
954 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
955 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
956 GenericValue Dest;
957 Dest = executeShlInst (Src1, Src2, Ty);
958 SetValue(&I, Dest, SF);
959}
960
961void Interpreter::visitShr(ShiftInst &I) {
962 ExecutionContext &SF = ECStack.back();
963 const Type *Ty = I.getOperand(0)->getType();
964 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
965 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
966 GenericValue Dest;
967 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000968 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000969}
970
971#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000972 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000973
974#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
975 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000976 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000977 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000978 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
979 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
980 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000983 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
984 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000985 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
986 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000987
988#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
989 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
990 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
991
992#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +0000993 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +0000994 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000995 } \
996 break
997
998#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
999 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1000 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +00001001 IMPLEMENT_CAST_CASE_END()
1002
Brian Gaeke29794cb2003-09-05 18:55:03 +00001003GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001004 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001005 const Type *SrcTy = SrcVal->getType();
1006 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001007
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001008 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001009 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1010 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1011 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001012 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001013 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1014 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1015 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1016 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001017 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001018 IMPLEMENT_CAST_CASE(Float , (float));
1019 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001020 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001021 default:
Chris Lattner02868352003-04-22 21:22:33 +00001022 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001023 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001024 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001025
1026 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001027}
Chris Lattner92101ac2001-08-23 17:05:04 +00001028
Chris Lattnerd7916e92003-05-10 21:22:39 +00001029void Interpreter::visitCastInst(CastInst &I) {
1030 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001031 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1032}
Chris Lattner92101ac2001-08-23 17:05:04 +00001033
Brian Gaekec1a2be12003-11-07 21:20:47 +00001034#define IMPLEMENT_VAARG(TY) \
1035 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1036
1037void Interpreter::visitVAArgInst(VAArgInst &I) {
1038 ExecutionContext &SF = ECStack.back();
1039
Brian Gaeke9d20b712004-02-25 23:01:48 +00001040 // Get the incoming valist parameter. LLI treats the valist as a
1041 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001042 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001043 GenericValue Dest;
1044 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001045 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001046 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001047 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001048 IMPLEMENT_VAARG(UByte);
1049 IMPLEMENT_VAARG(SByte);
1050 IMPLEMENT_VAARG(UShort);
1051 IMPLEMENT_VAARG(Short);
1052 IMPLEMENT_VAARG(UInt);
1053 IMPLEMENT_VAARG(Int);
1054 IMPLEMENT_VAARG(ULong);
1055 IMPLEMENT_VAARG(Long);
1056 IMPLEMENT_VAARG(Pointer);
1057 IMPLEMENT_VAARG(Float);
1058 IMPLEMENT_VAARG(Double);
1059 IMPLEMENT_VAARG(Bool);
1060 default:
1061 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1062 abort();
1063 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001064
Brian Gaekec1a2be12003-11-07 21:20:47 +00001065 // Set the Value of this Instruction.
1066 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001067
1068 // Move the pointer to the next vararg.
1069 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001070}
1071
Chris Lattner92101ac2001-08-23 17:05:04 +00001072//===----------------------------------------------------------------------===//
1073// Dispatch and Execution Code
1074//===----------------------------------------------------------------------===//
1075
Chris Lattner92101ac2001-08-23 17:05:04 +00001076//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001077// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001078//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001079void Interpreter::callFunction(Function *F,
1080 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001081 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1082 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1083 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001084 // Make a new stack frame... and fill it in.
1085 ECStack.push_back(ExecutionContext());
1086 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001087 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001088
1089 // Special handling for external functions.
1090 if (F->isExternal()) {
1091 GenericValue Result = callExternalFunction (F, ArgVals);
1092 // Simulate a 'ret' instruction of the appropriate type.
1093 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1094 return;
1095 }
1096
1097 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001098 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001099 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001100
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001101 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001102 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001103 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001104 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001105
1106 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001107 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001108 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001109 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001110
1111 // Handle varargs arguments...
1112 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001113}
1114
Chris Lattner92101ac2001-08-23 17:05:04 +00001115void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001116 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001117 // Interpret a single instruction & increment the "PC".
1118 ExecutionContext &SF = ECStack.back(); // Current stack frame
1119 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001120
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001121 // Track the number of dynamic instructions executed.
1122 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001123
Brian Gaeke63438cc2003-12-11 00:22:59 +00001124 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001125 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001126 }
1127}