blob: 5a7181f83eb92515a8ea0ce5c1903cc213cd42d7 [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;
Reid Spencer1628cec2006-10-26 06:15:43 +0000263 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000264 IMPLEMENT_SIGNLESS_BINOP(/, UByte, SByte);
265 IMPLEMENT_SIGNLESS_BINOP(/, UShort, Short);
266 IMPLEMENT_SIGNLESS_BINOP(/, UInt, Int);
267 IMPLEMENT_SIGNLESS_BINOP(/, ULong, Long);
Reid Spencer1628cec2006-10-26 06:15:43 +0000268 default:
269 std::cout << "Unhandled type for UDiv instruction: " << *Ty << "\n";
270 abort();
271 }
272 return Dest;
273}
274
275static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2,
276 const Type *Ty) {
277 GenericValue Dest;
Reid Spencer1628cec2006-10-26 06:15:43 +0000278 switch (Ty->getTypeID()) {
Reid Spencerfe855262006-11-01 03:41:05 +0000279 IMPLEMENT_SIGNLESS_BINOP(/, SByte, UByte);
280 IMPLEMENT_SIGNLESS_BINOP(/, Short, UShort);
281 IMPLEMENT_SIGNLESS_BINOP(/, Int, UInt);
282 IMPLEMENT_SIGNLESS_BINOP(/, Long, ULong);
Reid Spencer1628cec2006-10-26 06:15:43 +0000283 default:
284 std::cout << "Unhandled type for SDiv instruction: " << *Ty << "\n";
285 abort();
286 }
287 return Dest;
288}
289
290static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000291 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000292 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000293 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000294 IMPLEMENT_BINARY_OPERATOR(/, Float);
295 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000296 default:
Chris Lattner02868352003-04-22 21:22:33 +0000297 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
298 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000299 }
300 return Dest;
301}
302
Misha Brukmand1c881a2005-04-21 22:43:08 +0000303static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
304 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000305 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000306 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000307 IMPLEMENT_BINARY_OPERATOR(%, UByte);
308 IMPLEMENT_BINARY_OPERATOR(%, SByte);
309 IMPLEMENT_BINARY_OPERATOR(%, UShort);
310 IMPLEMENT_BINARY_OPERATOR(%, Short);
311 IMPLEMENT_BINARY_OPERATOR(%, UInt);
312 IMPLEMENT_BINARY_OPERATOR(%, Int);
313 IMPLEMENT_BINARY_OPERATOR(%, ULong);
314 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000315 case Type::FloatTyID:
316 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
317 break;
318 case Type::DoubleTyID:
319 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
320 break;
321 default:
Chris Lattner02868352003-04-22 21:22:33 +0000322 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
323 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000324 }
325 return Dest;
326}
327
Misha Brukmand1c881a2005-04-21 22:43:08 +0000328static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
329 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000330 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000331 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000332 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000333 IMPLEMENT_BINARY_OPERATOR(&, UByte);
334 IMPLEMENT_BINARY_OPERATOR(&, SByte);
335 IMPLEMENT_BINARY_OPERATOR(&, UShort);
336 IMPLEMENT_BINARY_OPERATOR(&, Short);
337 IMPLEMENT_BINARY_OPERATOR(&, UInt);
338 IMPLEMENT_BINARY_OPERATOR(&, Int);
339 IMPLEMENT_BINARY_OPERATOR(&, ULong);
340 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000341 default:
Chris Lattner02868352003-04-22 21:22:33 +0000342 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
343 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000344 }
345 return Dest;
346}
347
Misha Brukmand1c881a2005-04-21 22:43:08 +0000348static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000349 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000350 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000351 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000352 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000353 IMPLEMENT_BINARY_OPERATOR(|, UByte);
354 IMPLEMENT_BINARY_OPERATOR(|, SByte);
355 IMPLEMENT_BINARY_OPERATOR(|, UShort);
356 IMPLEMENT_BINARY_OPERATOR(|, Short);
357 IMPLEMENT_BINARY_OPERATOR(|, UInt);
358 IMPLEMENT_BINARY_OPERATOR(|, Int);
359 IMPLEMENT_BINARY_OPERATOR(|, ULong);
360 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000361 default:
Chris Lattner02868352003-04-22 21:22:33 +0000362 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
363 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000364 }
365 return Dest;
366}
367
Misha Brukmand1c881a2005-04-21 22:43:08 +0000368static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000369 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000370 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000371 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000372 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000373 IMPLEMENT_BINARY_OPERATOR(^, UByte);
374 IMPLEMENT_BINARY_OPERATOR(^, SByte);
375 IMPLEMENT_BINARY_OPERATOR(^, UShort);
376 IMPLEMENT_BINARY_OPERATOR(^, Short);
377 IMPLEMENT_BINARY_OPERATOR(^, UInt);
378 IMPLEMENT_BINARY_OPERATOR(^, Int);
379 IMPLEMENT_BINARY_OPERATOR(^, ULong);
380 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000381 default:
Chris Lattner02868352003-04-22 21:22:33 +0000382 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
383 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000384 }
385 return Dest;
386}
387
Chris Lattner92101ac2001-08-23 17:05:04 +0000388#define IMPLEMENT_SETCC(OP, TY) \
389 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
390
Chris Lattnerfd506f52003-04-23 19:55:35 +0000391// Handle pointers specially because they must be compared with only as much
392// width as the host has. We _do not_ want to be comparing 64 bit values when
393// running on a 32-bit target, otherwise the upper 32 bits might mess up
394// comparisons if they contain garbage.
395#define IMPLEMENT_POINTERSETCC(OP) \
396 case Type::PointerTyID: \
397 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
398 (void*)(intptr_t)Src2.PointerVal; break
399
Misha Brukmand1c881a2005-04-21 22:43:08 +0000400static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
401 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000402 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000403 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000404 IMPLEMENT_SETCC(==, UByte);
405 IMPLEMENT_SETCC(==, SByte);
406 IMPLEMENT_SETCC(==, UShort);
407 IMPLEMENT_SETCC(==, Short);
408 IMPLEMENT_SETCC(==, UInt);
409 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000410 IMPLEMENT_SETCC(==, ULong);
411 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000412 IMPLEMENT_SETCC(==, Float);
413 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000414 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000415 default:
Chris Lattner02868352003-04-22 21:22:33 +0000416 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
417 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000418 }
419 return Dest;
420}
421
Misha Brukmand1c881a2005-04-21 22:43:08 +0000422static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
423 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000424 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000425 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 IMPLEMENT_SETCC(!=, UByte);
427 IMPLEMENT_SETCC(!=, SByte);
428 IMPLEMENT_SETCC(!=, UShort);
429 IMPLEMENT_SETCC(!=, Short);
430 IMPLEMENT_SETCC(!=, UInt);
431 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000432 IMPLEMENT_SETCC(!=, ULong);
433 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000434 IMPLEMENT_SETCC(!=, Float);
435 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000436 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000437
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 default:
Chris Lattner02868352003-04-22 21:22:33 +0000439 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
440 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000441 }
442 return Dest;
443}
444
Misha Brukmand1c881a2005-04-21 22:43:08 +0000445static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
446 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000447 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000448 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000449 IMPLEMENT_SETCC(<=, UByte);
450 IMPLEMENT_SETCC(<=, SByte);
451 IMPLEMENT_SETCC(<=, UShort);
452 IMPLEMENT_SETCC(<=, Short);
453 IMPLEMENT_SETCC(<=, UInt);
454 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000455 IMPLEMENT_SETCC(<=, ULong);
456 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000457 IMPLEMENT_SETCC(<=, Float);
458 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000459 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000460 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000461 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000462 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000463 }
464 return Dest;
465}
466
Misha Brukmand1c881a2005-04-21 22:43:08 +0000467static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
468 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000469 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000470 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000471 IMPLEMENT_SETCC(>=, UByte);
472 IMPLEMENT_SETCC(>=, SByte);
473 IMPLEMENT_SETCC(>=, UShort);
474 IMPLEMENT_SETCC(>=, Short);
475 IMPLEMENT_SETCC(>=, UInt);
476 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000477 IMPLEMENT_SETCC(>=, ULong);
478 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 IMPLEMENT_SETCC(>=, Float);
480 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000481 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000482 default:
Chris Lattner02868352003-04-22 21:22:33 +0000483 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
484 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000485 }
486 return Dest;
487}
488
Misha Brukmand1c881a2005-04-21 22:43:08 +0000489static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
490 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000491 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000492 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000493 IMPLEMENT_SETCC(<, UByte);
494 IMPLEMENT_SETCC(<, SByte);
495 IMPLEMENT_SETCC(<, UShort);
496 IMPLEMENT_SETCC(<, Short);
497 IMPLEMENT_SETCC(<, UInt);
498 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000499 IMPLEMENT_SETCC(<, ULong);
500 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 IMPLEMENT_SETCC(<, Float);
502 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000503 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000504 default:
Chris Lattner02868352003-04-22 21:22:33 +0000505 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
506 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000507 }
508 return Dest;
509}
510
Misha Brukmand1c881a2005-04-21 22:43:08 +0000511static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
512 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000513 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000514 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000515 IMPLEMENT_SETCC(>, UByte);
516 IMPLEMENT_SETCC(>, SByte);
517 IMPLEMENT_SETCC(>, UShort);
518 IMPLEMENT_SETCC(>, Short);
519 IMPLEMENT_SETCC(>, UInt);
520 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000521 IMPLEMENT_SETCC(>, ULong);
522 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000523 IMPLEMENT_SETCC(>, Float);
524 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000525 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000526 default:
Chris Lattner02868352003-04-22 21:22:33 +0000527 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
528 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000529 }
530 return Dest;
531}
532
Chris Lattnerd7916e92003-05-10 21:22:39 +0000533void Interpreter::visitBinaryOperator(BinaryOperator &I) {
534 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000535 const Type *Ty = I.getOperand(0)->getType();
536 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
537 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000538 GenericValue R; // Result
539
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000540 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000541 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
542 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
543 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000544 case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break;
545 case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break;
546 case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break;
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000547 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
548 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
549 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
550 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
551 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
552 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
553 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
554 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
555 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
556 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000557 default:
Chris Lattner02868352003-04-22 21:22:33 +0000558 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
559 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000560 }
561
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000562 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000563}
564
Misha Brukmand1c881a2005-04-21 22:43:08 +0000565static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Chris Lattner759d34f2004-04-20 16:43:21 +0000566 GenericValue Src3) {
567 return Src1.BoolVal ? Src2 : Src3;
568}
569
570void Interpreter::visitSelectInst(SelectInst &I) {
571 ExecutionContext &SF = ECStack.back();
572 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
573 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
574 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
575 GenericValue R = executeSelectInst(Src1, Src2, Src3);
576 SetValue(&I, R, SF);
577}
578
579
Chris Lattner92101ac2001-08-23 17:05:04 +0000580//===----------------------------------------------------------------------===//
581// Terminator Instruction Implementations
582//===----------------------------------------------------------------------===//
583
Chris Lattnere43db882001-10-27 04:15:57 +0000584void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000585 // runAtExitHandlers() assumes there are no stack frames, but
586 // if exit() was called, then it had a stack frame. Blow away
587 // the stack before interpreting atexit handlers.
588 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000589 runAtExitHandlers ();
590 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000591}
592
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000593/// Pop the last stack frame off of ECStack and then copy the result
594/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000595/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000596/// CallInst if there was a previous stack frame. This method may
597/// invalidate any ECStack iterators you have. This method also takes
598/// care of switching to the normal destination BB, if we are returning
599/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000600///
601void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
602 GenericValue Result) {
603 // Pop the current stack frame.
604 ECStack.pop_back();
605
Misha Brukmand1c881a2005-04-21 22:43:08 +0000606 if (ECStack.empty()) { // Finished main. Put result into exit code...
607 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000608 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000609 } else {
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000610 memset(&ExitValue, 0, sizeof(ExitValue));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000611 }
612 } else {
613 // If we have a previous stack frame, and we have a previous call,
614 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000615 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000616 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000617 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000618 SetValue(I, Result, CallingSF);
619 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
620 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000621 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000622 }
623 }
624}
625
Chris Lattnerd7916e92003-05-10 21:22:39 +0000626void Interpreter::visitReturnInst(ReturnInst &I) {
627 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000628 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000629 GenericValue Result;
630
631 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000632 if (I.getNumOperands()) {
633 RetTy = I.getReturnValue()->getType();
634 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000635 }
636
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000637 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000638}
639
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000640void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000641 // Unwind stack
642 Instruction *Inst;
643 do {
644 ECStack.pop_back ();
645 if (ECStack.empty ())
646 abort ();
647 Inst = ECStack.back ().Caller.getInstruction ();
648 } while (!(Inst && isa<InvokeInst> (Inst)));
649
650 // Return from invoke
651 ExecutionContext &InvokingSF = ECStack.back ();
652 InvokingSF.Caller = CallSite ();
653
654 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000655 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000656}
657
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000658void Interpreter::visitUnreachableInst(UnreachableInst &I) {
659 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
660 abort();
661}
662
Chris Lattnerd7916e92003-05-10 21:22:39 +0000663void Interpreter::visitBranchInst(BranchInst &I) {
664 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000665 BasicBlock *Dest;
666
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000667 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
668 if (!I.isUnconditional()) {
669 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000670 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000671 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000672 }
Chris Lattner77113b62003-05-10 20:21:16 +0000673 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000674}
675
Chris Lattnerd7916e92003-05-10 21:22:39 +0000676void Interpreter::visitSwitchInst(SwitchInst &I) {
677 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000678 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
679 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000680
681 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000682 BasicBlock *Dest = 0;
683 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000684 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000685 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000686 Dest = cast<BasicBlock>(I.getOperand(i+1));
687 break;
688 }
Misha Brukmand1c881a2005-04-21 22:43:08 +0000689
Chris Lattner09e93922003-04-22 20:34:47 +0000690 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000691 SwitchToNewBasicBlock(Dest, SF);
692}
693
694// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
695// This function handles the actual updating of block and instruction iterators
696// as well as execution of all of the PHI nodes in the destination block.
697//
698// This method does this because all of the PHI nodes must be executed
699// atomically, reading their inputs before any of the results are updated. Not
700// doing this can cause problems if the PHI nodes depend on other PHI nodes for
701// their inputs. If the input PHI node is updated before it is read, incorrect
702// results can happen. Thus we use a two phase approach.
703//
704void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
705 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
706 SF.CurBB = Dest; // Update CurBB to branch destination
707 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
708
709 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
710
711 // Loop over all of the PHI nodes in the current block, reading their inputs.
712 std::vector<GenericValue> ResultValues;
713
714 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
715 // Search for the value corresponding to this previous bb...
716 int i = PN->getBasicBlockIndex(PrevBB);
717 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
718 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000719
Chris Lattner77113b62003-05-10 20:21:16 +0000720 // Save the incoming value for this PHI node...
721 ResultValues.push_back(getOperandValue(IncomingValue, SF));
722 }
723
724 // Now loop over all of the PHI nodes setting their values...
725 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000726 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
727 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000728 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000729 }
Chris Lattner09e93922003-04-22 20:34:47 +0000730}
731
Chris Lattner92101ac2001-08-23 17:05:04 +0000732//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000733// Memory Instruction Implementations
734//===----------------------------------------------------------------------===//
735
Chris Lattnerd7916e92003-05-10 21:22:39 +0000736void Interpreter::visitAllocationInst(AllocationInst &I) {
737 ExecutionContext &SF = ECStack.back();
738
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000739 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000740
Chris Lattnercc82cc12002-04-28 21:57:33 +0000741 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000742 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000743
744 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000745 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000746
Chris Lattnerfe11a972002-12-23 23:59:41 +0000747 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000748 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000749 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000750
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000751 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000752 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000753}
754
Chris Lattnerd7916e92003-05-10 21:22:39 +0000755void Interpreter::visitFreeInst(FreeInst &I) {
756 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000757 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
758 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000759 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000760 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000761}
762
Chris Lattnera34c5682002-08-27 22:33:45 +0000763// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000764//
Chris Lattner4af6de82003-11-25 20:44:56 +0000765GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +0000766 gep_type_iterator E,
767 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000768 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000769 "Cannot getElementOffset of a nonpointer type!");
770
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000771 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000772
773 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000774 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000775 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000776
Reid Spencerb83eb642006-10-20 07:07:24 +0000777 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
778 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +0000779
Chris Lattnerd5644962005-01-08 20:05:34 +0000780 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000781 } else {
782 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000783 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000784 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
785
786 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000787 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000788 default: assert(0 && "Illegal getelementptr index for sequential type!");
789 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
790 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
791 case Type::IntTyID: Idx = IdxGV.IntVal; break;
792 case Type::LongTyID: Idx = IdxGV.LongVal; break;
793 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
794 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
795 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
796 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
797 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000798 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000799 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000800 }
801
Chris Lattnera34c5682002-08-27 22:33:45 +0000802 GenericValue Result;
803 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
804 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000805}
806
Chris Lattnerd7916e92003-05-10 21:22:39 +0000807void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
808 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000809 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000810 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000811}
812
Chris Lattnerd7916e92003-05-10 21:22:39 +0000813void Interpreter::visitLoadInst(LoadInst &I) {
814 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000815 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000816 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000817 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000818 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000819}
820
Chris Lattnerd7916e92003-05-10 21:22:39 +0000821void Interpreter::visitStoreInst(StoreInst &I) {
822 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000823 GenericValue Val = getOperandValue(I.getOperand(0), SF);
824 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000825 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000826 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000827}
828
Chris Lattner86660982001-08-27 05:16:50 +0000829//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000830// Miscellaneous Instruction Implementations
831//===----------------------------------------------------------------------===//
832
Brian Gaekefea483d2003-11-07 20:04:22 +0000833void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000834 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000835
836 // Check to see if this is an intrinsic function call...
837 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000838 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000839 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000840 case Intrinsic::not_intrinsic:
841 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000842 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000843 GenericValue ArgIndex;
844 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
845 ArgIndex.UIntPairVal.second = 0;
846 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000847 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000848 }
Chris Lattner317201d2004-03-13 00:24:00 +0000849 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000850 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000851 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000852 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
853 return;
854 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000855 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000856 // class to transform it into hopefully tasty LLVM code.
857 //
858 Instruction *Prev = CS.getInstruction()->getPrev();
859 BasicBlock *Parent = CS.getInstruction()->getParent();
860 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
861
862 // Restore the CurInst pointer to the first instruction newly inserted, if
863 // any.
864 if (!Prev) {
865 SF.CurInst = Parent->begin();
866 } else {
867 SF.CurInst = Prev;
868 ++SF.CurInst;
869 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000870 return;
Chris Lattner73011782003-12-28 09:44:37 +0000871 }
872
Brian Gaekefea483d2003-11-07 20:04:22 +0000873 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000874 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000875 const unsigned NumArgs = SF.Caller.arg_size();
876 ArgVals.reserve(NumArgs);
877 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
878 e = SF.Caller.arg_end(); i != e; ++i) {
879 Value *V = *i;
880 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000881 // Promote all integral types whose size is < sizeof(int) into ints. We do
882 // this by zero or sign extending the value as appropriate according to the
883 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000884 const Type *Ty = V->getType();
885 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000886 if (Ty == Type::ShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000887 ArgVals.back().IntVal = ArgVals.back().ShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000888 else if (Ty == Type::UShortTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000889 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
Chris Lattner93780132003-01-13 00:58:52 +0000890 else if (Ty == Type::SByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000891 ArgVals.back().IntVal = ArgVals.back().SByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000892 else if (Ty == Type::UByteTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000893 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
Chris Lattner93780132003-01-13 00:58:52 +0000894 else if (Ty == Type::BoolTy)
Misha Brukmand1c881a2005-04-21 22:43:08 +0000895 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
Chris Lattner93780132003-01-13 00:58:52 +0000896 else
Misha Brukmand1c881a2005-04-21 22:43:08 +0000897 assert(0 && "Unknown type!");
Chris Lattner93780132003-01-13 00:58:52 +0000898 }
899 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000900
Misha Brukmand1c881a2005-04-21 22:43:08 +0000901 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000902 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +0000903 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000904 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000905}
906
Chris Lattner86660982001-08-27 05:16:50 +0000907#define IMPLEMENT_SHIFT(OP, TY) \
908 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
909
Brian Gaeke63438cc2003-12-11 00:22:59 +0000910static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
911 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000912 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000913 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000914 IMPLEMENT_SHIFT(<<, UByte);
915 IMPLEMENT_SHIFT(<<, SByte);
916 IMPLEMENT_SHIFT(<<, UShort);
917 IMPLEMENT_SHIFT(<<, Short);
918 IMPLEMENT_SHIFT(<<, UInt);
919 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000920 IMPLEMENT_SHIFT(<<, ULong);
921 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000922 default:
Chris Lattner02868352003-04-22 21:22:33 +0000923 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000924 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000925 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000926}
927
Brian Gaeke63438cc2003-12-11 00:22:59 +0000928static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
929 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000930 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000931 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000932 IMPLEMENT_SHIFT(>>, UByte);
933 IMPLEMENT_SHIFT(>>, SByte);
934 IMPLEMENT_SHIFT(>>, UShort);
935 IMPLEMENT_SHIFT(>>, Short);
936 IMPLEMENT_SHIFT(>>, UInt);
937 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000938 IMPLEMENT_SHIFT(>>, ULong);
939 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000940 default:
Chris Lattner02868352003-04-22 21:22:33 +0000941 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
942 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000943 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000944 return Dest;
945}
946
947void Interpreter::visitShl(ShiftInst &I) {
948 ExecutionContext &SF = ECStack.back();
949 const Type *Ty = I.getOperand(0)->getType();
950 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
951 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
952 GenericValue Dest;
953 Dest = executeShlInst (Src1, Src2, Ty);
954 SetValue(&I, Dest, SF);
955}
956
957void Interpreter::visitShr(ShiftInst &I) {
958 ExecutionContext &SF = ECStack.back();
959 const Type *Ty = I.getOperand(0)->getType();
960 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
961 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
962 GenericValue Dest;
963 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000964 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000965}
966
967#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000968 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000969
970#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
971 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000972 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000973 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000974 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
975 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
976 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000979 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
980 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000981 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000983
984#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
985 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
986 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
987
988#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +0000989 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +0000990 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000991 } \
992 break
993
994#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
995 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
996 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000997 IMPLEMENT_CAST_CASE_END()
998
Brian Gaeke29794cb2003-09-05 18:55:03 +0000999GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001000 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001001 const Type *SrcTy = SrcVal->getType();
1002 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001003
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001004 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001005 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1006 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1007 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +00001008 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001009 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1010 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1011 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1012 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +00001013 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001014 IMPLEMENT_CAST_CASE(Float , (float));
1015 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +00001016 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +00001017 default:
Chris Lattner02868352003-04-22 21:22:33 +00001018 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +00001019 abort();
Chris Lattner86660982001-08-27 05:16:50 +00001020 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001021
1022 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001023}
Chris Lattner92101ac2001-08-23 17:05:04 +00001024
Chris Lattnerd7916e92003-05-10 21:22:39 +00001025void Interpreter::visitCastInst(CastInst &I) {
1026 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +00001027 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1028}
Chris Lattner92101ac2001-08-23 17:05:04 +00001029
Brian Gaekec1a2be12003-11-07 21:20:47 +00001030#define IMPLEMENT_VAARG(TY) \
1031 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1032
1033void Interpreter::visitVAArgInst(VAArgInst &I) {
1034 ExecutionContext &SF = ECStack.back();
1035
Brian Gaeke9d20b712004-02-25 23:01:48 +00001036 // Get the incoming valist parameter. LLI treats the valist as a
1037 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001038 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001039 GenericValue Dest;
1040 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Misha Brukmand1c881a2005-04-21 22:43:08 +00001041 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001042 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001043 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001044 IMPLEMENT_VAARG(UByte);
1045 IMPLEMENT_VAARG(SByte);
1046 IMPLEMENT_VAARG(UShort);
1047 IMPLEMENT_VAARG(Short);
1048 IMPLEMENT_VAARG(UInt);
1049 IMPLEMENT_VAARG(Int);
1050 IMPLEMENT_VAARG(ULong);
1051 IMPLEMENT_VAARG(Long);
1052 IMPLEMENT_VAARG(Pointer);
1053 IMPLEMENT_VAARG(Float);
1054 IMPLEMENT_VAARG(Double);
1055 IMPLEMENT_VAARG(Bool);
1056 default:
1057 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1058 abort();
1059 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001060
Brian Gaekec1a2be12003-11-07 21:20:47 +00001061 // Set the Value of this Instruction.
1062 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001063
1064 // Move the pointer to the next vararg.
1065 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001066}
1067
Chris Lattner92101ac2001-08-23 17:05:04 +00001068//===----------------------------------------------------------------------===//
1069// Dispatch and Execution Code
1070//===----------------------------------------------------------------------===//
1071
Chris Lattner92101ac2001-08-23 17:05:04 +00001072//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001073// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001074//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001075void Interpreter::callFunction(Function *F,
1076 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00001077 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1078 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1079 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001080 // Make a new stack frame... and fill it in.
1081 ECStack.push_back(ExecutionContext());
1082 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001083 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001084
1085 // Special handling for external functions.
1086 if (F->isExternal()) {
1087 GenericValue Result = callExternalFunction (F, ArgVals);
1088 // Simulate a 'ret' instruction of the appropriate type.
1089 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1090 return;
1091 }
1092
1093 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001094 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001095 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001096
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001097 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001098 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00001099 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001100 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001101
1102 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001103 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001104 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001105 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001106
1107 // Handle varargs arguments...
1108 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001109}
1110
Chris Lattner92101ac2001-08-23 17:05:04 +00001111void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001112 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001113 // Interpret a single instruction & increment the "PC".
1114 ExecutionContext &SF = ECStack.back(); // Current stack frame
1115 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00001116
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001117 // Track the number of dynamic instructions executed.
1118 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001119
Brian Gaeke63438cc2003-12-11 00:22:59 +00001120 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001121 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001122 }
1123}