blob: ff356796f3a27b166fc2590c734c1d587345e1bb [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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
Chris Lattnera34c5682002-08-27 22:33:45 +000037static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000038 const Type *Ty);
Brian Gaeke63438cc2003-12-11 00:22:59 +000039static 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);
45static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
46 const Type *Ty);
47static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
48 const Type *Ty);
49static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
50 const Type *Ty);
51static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
52 const Type *Ty);
53static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
54 const Type *Ty);
55static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
56 const Type *Ty);
57static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
58 const Type *Ty);
59static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
60 const Type *Ty);
61static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
62 const Type *Ty);
63static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
64 const Type *Ty);
65static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
66 const Type *Ty);
67static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
68 const Type *Ty);
Chris Lattner759d34f2004-04-20 16:43:21 +000069static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
70 GenericValue Src3);
71
Brian Gaeke63438cc2003-12-11 00:22:59 +000072GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
73 ExecutionContext &SF) {
74 switch (CE->getOpcode()) {
75 case Instruction::Cast:
76 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
77 case Instruction::GetElementPtr:
78 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
79 gep_type_end(CE), SF);
80 case Instruction::Add:
81 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
82 getOperandValue(CE->getOperand(1), SF),
83 CE->getOperand(0)->getType());
84 case Instruction::Sub:
85 return executeSubInst(getOperandValue(CE->getOperand(0), SF),
86 getOperandValue(CE->getOperand(1), SF),
87 CE->getOperand(0)->getType());
88 case Instruction::Mul:
89 return executeMulInst(getOperandValue(CE->getOperand(0), SF),
90 getOperandValue(CE->getOperand(1), SF),
91 CE->getOperand(0)->getType());
92 case Instruction::Div:
93 return executeDivInst(getOperandValue(CE->getOperand(0), SF),
94 getOperandValue(CE->getOperand(1), SF),
95 CE->getOperand(0)->getType());
96 case Instruction::Rem:
97 return executeRemInst(getOperandValue(CE->getOperand(0), SF),
98 getOperandValue(CE->getOperand(1), SF),
99 CE->getOperand(0)->getType());
100 case Instruction::And:
101 return executeAndInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
103 CE->getOperand(0)->getType());
104 case Instruction::Or:
105 return executeOrInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
107 CE->getOperand(0)->getType());
108 case Instruction::Xor:
109 return executeXorInst(getOperandValue(CE->getOperand(0), SF),
110 getOperandValue(CE->getOperand(1), SF),
111 CE->getOperand(0)->getType());
112 case Instruction::SetEQ:
113 return executeSetEQInst(getOperandValue(CE->getOperand(0), SF),
114 getOperandValue(CE->getOperand(1), SF),
115 CE->getOperand(0)->getType());
116 case Instruction::SetNE:
117 return executeSetNEInst(getOperandValue(CE->getOperand(0), SF),
118 getOperandValue(CE->getOperand(1), SF),
119 CE->getOperand(0)->getType());
120 case Instruction::SetLE:
121 return executeSetLEInst(getOperandValue(CE->getOperand(0), SF),
122 getOperandValue(CE->getOperand(1), SF),
123 CE->getOperand(0)->getType());
124 case Instruction::SetGE:
125 return executeSetGEInst(getOperandValue(CE->getOperand(0), SF),
126 getOperandValue(CE->getOperand(1), SF),
127 CE->getOperand(0)->getType());
128 case Instruction::SetLT:
129 return executeSetLTInst(getOperandValue(CE->getOperand(0), SF),
130 getOperandValue(CE->getOperand(1), SF),
131 CE->getOperand(0)->getType());
132 case Instruction::SetGT:
133 return executeSetGTInst(getOperandValue(CE->getOperand(0), SF),
134 getOperandValue(CE->getOperand(1), SF),
135 CE->getOperand(0)->getType());
136 case Instruction::Shl:
137 return executeShlInst(getOperandValue(CE->getOperand(0), SF),
138 getOperandValue(CE->getOperand(1), SF),
139 CE->getOperand(0)->getType());
140 case Instruction::Shr:
141 return executeShrInst(getOperandValue(CE->getOperand(0), SF),
142 getOperandValue(CE->getOperand(1), SF),
143 CE->getOperand(0)->getType());
Chris Lattner759d34f2004-04-20 16:43:21 +0000144 case Instruction::Select:
145 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
146 getOperandValue(CE->getOperand(1), SF),
147 getOperandValue(CE->getOperand(2), SF));
Brian Gaeke63438cc2003-12-11 00:22:59 +0000148 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000149 std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
Brian Gaeke63438cc2003-12-11 00:22:59 +0000150 abort();
151 return GenericValue();
152 }
153}
Chris Lattnera34c5682002-08-27 22:33:45 +0000154
Brian Gaeke29794cb2003-09-05 18:55:03 +0000155GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000156 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000157 return getConstantExprValue(CE, SF);
Chris Lattnera34c5682002-08-27 22:33:45 +0000158 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000159 return getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +0000160 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeke63438cc2003-12-11 00:22:59 +0000161 return PTOGV(getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +0000162 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000163 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +0000164 }
165}
166
Chris Lattner39bb5b42001-10-15 13:25:40 +0000167static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000168 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +0000169}
170
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000171void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +0000172 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000173}
174
Chris Lattner2adcd832002-05-03 19:52:30 +0000175//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000176// Binary Instruction Implementations
177//===----------------------------------------------------------------------===//
178
179#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
180 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
181
182static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000183 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000184 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000185 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000186 IMPLEMENT_BINARY_OPERATOR(+, UByte);
187 IMPLEMENT_BINARY_OPERATOR(+, SByte);
188 IMPLEMENT_BINARY_OPERATOR(+, UShort);
189 IMPLEMENT_BINARY_OPERATOR(+, Short);
190 IMPLEMENT_BINARY_OPERATOR(+, UInt);
191 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000192 IMPLEMENT_BINARY_OPERATOR(+, ULong);
193 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000194 IMPLEMENT_BINARY_OPERATOR(+, Float);
195 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000196 default:
Chris Lattner02868352003-04-22 21:22:33 +0000197 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
198 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000199 }
200 return Dest;
201}
202
203static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000204 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000205 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000206 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000207 IMPLEMENT_BINARY_OPERATOR(-, UByte);
208 IMPLEMENT_BINARY_OPERATOR(-, SByte);
209 IMPLEMENT_BINARY_OPERATOR(-, UShort);
210 IMPLEMENT_BINARY_OPERATOR(-, Short);
211 IMPLEMENT_BINARY_OPERATOR(-, UInt);
212 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000213 IMPLEMENT_BINARY_OPERATOR(-, ULong);
214 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000215 IMPLEMENT_BINARY_OPERATOR(-, Float);
216 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000217 default:
Chris Lattner02868352003-04-22 21:22:33 +0000218 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
219 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000220 }
221 return Dest;
222}
223
Chris Lattnerc2593162001-10-27 08:28:11 +0000224static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000225 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000226 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000227 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000228 IMPLEMENT_BINARY_OPERATOR(*, UByte);
229 IMPLEMENT_BINARY_OPERATOR(*, SByte);
230 IMPLEMENT_BINARY_OPERATOR(*, UShort);
231 IMPLEMENT_BINARY_OPERATOR(*, Short);
232 IMPLEMENT_BINARY_OPERATOR(*, UInt);
233 IMPLEMENT_BINARY_OPERATOR(*, Int);
234 IMPLEMENT_BINARY_OPERATOR(*, ULong);
235 IMPLEMENT_BINARY_OPERATOR(*, Long);
236 IMPLEMENT_BINARY_OPERATOR(*, Float);
237 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000238 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000239 std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000240 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000241 }
242 return Dest;
243}
244
245static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000246 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000247 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000248 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000249 IMPLEMENT_BINARY_OPERATOR(/, UByte);
250 IMPLEMENT_BINARY_OPERATOR(/, SByte);
251 IMPLEMENT_BINARY_OPERATOR(/, UShort);
252 IMPLEMENT_BINARY_OPERATOR(/, Short);
253 IMPLEMENT_BINARY_OPERATOR(/, UInt);
254 IMPLEMENT_BINARY_OPERATOR(/, Int);
255 IMPLEMENT_BINARY_OPERATOR(/, ULong);
256 IMPLEMENT_BINARY_OPERATOR(/, Long);
257 IMPLEMENT_BINARY_OPERATOR(/, Float);
258 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000259 default:
Chris Lattner02868352003-04-22 21:22:33 +0000260 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
261 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000262 }
263 return Dest;
264}
265
266static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000267 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000268 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000269 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000270 IMPLEMENT_BINARY_OPERATOR(%, UByte);
271 IMPLEMENT_BINARY_OPERATOR(%, SByte);
272 IMPLEMENT_BINARY_OPERATOR(%, UShort);
273 IMPLEMENT_BINARY_OPERATOR(%, Short);
274 IMPLEMENT_BINARY_OPERATOR(%, UInt);
275 IMPLEMENT_BINARY_OPERATOR(%, Int);
276 IMPLEMENT_BINARY_OPERATOR(%, ULong);
277 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000278 case Type::FloatTyID:
279 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
280 break;
281 case Type::DoubleTyID:
282 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
283 break;
284 default:
Chris Lattner02868352003-04-22 21:22:33 +0000285 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
286 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000287 }
288 return Dest;
289}
290
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000291static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000292 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000293 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000294 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000295 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000296 IMPLEMENT_BINARY_OPERATOR(&, UByte);
297 IMPLEMENT_BINARY_OPERATOR(&, SByte);
298 IMPLEMENT_BINARY_OPERATOR(&, UShort);
299 IMPLEMENT_BINARY_OPERATOR(&, Short);
300 IMPLEMENT_BINARY_OPERATOR(&, UInt);
301 IMPLEMENT_BINARY_OPERATOR(&, Int);
302 IMPLEMENT_BINARY_OPERATOR(&, ULong);
303 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000304 default:
Chris Lattner02868352003-04-22 21:22:33 +0000305 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
306 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000307 }
308 return Dest;
309}
310
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000311static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000312 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000313 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000314 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000315 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000316 IMPLEMENT_BINARY_OPERATOR(|, UByte);
317 IMPLEMENT_BINARY_OPERATOR(|, SByte);
318 IMPLEMENT_BINARY_OPERATOR(|, UShort);
319 IMPLEMENT_BINARY_OPERATOR(|, Short);
320 IMPLEMENT_BINARY_OPERATOR(|, UInt);
321 IMPLEMENT_BINARY_OPERATOR(|, Int);
322 IMPLEMENT_BINARY_OPERATOR(|, ULong);
323 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000324 default:
Chris Lattner02868352003-04-22 21:22:33 +0000325 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
326 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000327 }
328 return Dest;
329}
330
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000331static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000332 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000333 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000334 switch (Ty->getTypeID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000335 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000336 IMPLEMENT_BINARY_OPERATOR(^, UByte);
337 IMPLEMENT_BINARY_OPERATOR(^, SByte);
338 IMPLEMENT_BINARY_OPERATOR(^, UShort);
339 IMPLEMENT_BINARY_OPERATOR(^, Short);
340 IMPLEMENT_BINARY_OPERATOR(^, UInt);
341 IMPLEMENT_BINARY_OPERATOR(^, Int);
342 IMPLEMENT_BINARY_OPERATOR(^, ULong);
343 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000344 default:
Chris Lattner02868352003-04-22 21:22:33 +0000345 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
346 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000347 }
348 return Dest;
349}
350
Chris Lattner92101ac2001-08-23 17:05:04 +0000351#define IMPLEMENT_SETCC(OP, TY) \
352 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
353
Chris Lattnerfd506f52003-04-23 19:55:35 +0000354// Handle pointers specially because they must be compared with only as much
355// width as the host has. We _do not_ want to be comparing 64 bit values when
356// running on a 32-bit target, otherwise the upper 32 bits might mess up
357// comparisons if they contain garbage.
358#define IMPLEMENT_POINTERSETCC(OP) \
359 case Type::PointerTyID: \
360 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
361 (void*)(intptr_t)Src2.PointerVal; break
362
Chris Lattner92101ac2001-08-23 17:05:04 +0000363static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000364 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000365 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000366 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000367 IMPLEMENT_SETCC(==, UByte);
368 IMPLEMENT_SETCC(==, SByte);
369 IMPLEMENT_SETCC(==, UShort);
370 IMPLEMENT_SETCC(==, Short);
371 IMPLEMENT_SETCC(==, UInt);
372 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000373 IMPLEMENT_SETCC(==, ULong);
374 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000375 IMPLEMENT_SETCC(==, Float);
376 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000377 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000378 default:
Chris Lattner02868352003-04-22 21:22:33 +0000379 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
380 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000381 }
382 return Dest;
383}
384
385static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000386 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000387 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000388 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000389 IMPLEMENT_SETCC(!=, UByte);
390 IMPLEMENT_SETCC(!=, SByte);
391 IMPLEMENT_SETCC(!=, UShort);
392 IMPLEMENT_SETCC(!=, Short);
393 IMPLEMENT_SETCC(!=, UInt);
394 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000395 IMPLEMENT_SETCC(!=, ULong);
396 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000397 IMPLEMENT_SETCC(!=, Float);
398 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000399 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000400
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 default:
Chris Lattner02868352003-04-22 21:22:33 +0000402 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
403 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000404 }
405 return Dest;
406}
407
408static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000409 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000410 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000411 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000412 IMPLEMENT_SETCC(<=, UByte);
413 IMPLEMENT_SETCC(<=, SByte);
414 IMPLEMENT_SETCC(<=, UShort);
415 IMPLEMENT_SETCC(<=, Short);
416 IMPLEMENT_SETCC(<=, UInt);
417 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000418 IMPLEMENT_SETCC(<=, ULong);
419 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000420 IMPLEMENT_SETCC(<=, Float);
421 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000422 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 default:
Chris Lattnered6c0732004-07-15 02:51:32 +0000424 std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
Chris Lattner02868352003-04-22 21:22:33 +0000425 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000426 }
427 return Dest;
428}
429
430static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000431 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000432 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000433 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000434 IMPLEMENT_SETCC(>=, UByte);
435 IMPLEMENT_SETCC(>=, SByte);
436 IMPLEMENT_SETCC(>=, UShort);
437 IMPLEMENT_SETCC(>=, Short);
438 IMPLEMENT_SETCC(>=, UInt);
439 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000440 IMPLEMENT_SETCC(>=, ULong);
441 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000442 IMPLEMENT_SETCC(>=, Float);
443 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000444 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000445 default:
Chris Lattner02868352003-04-22 21:22:33 +0000446 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
447 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 }
449 return Dest;
450}
451
452static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000453 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000454 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000455 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000456 IMPLEMENT_SETCC(<, UByte);
457 IMPLEMENT_SETCC(<, SByte);
458 IMPLEMENT_SETCC(<, UShort);
459 IMPLEMENT_SETCC(<, Short);
460 IMPLEMENT_SETCC(<, UInt);
461 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000462 IMPLEMENT_SETCC(<, ULong);
463 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000464 IMPLEMENT_SETCC(<, Float);
465 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000466 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000467 default:
Chris Lattner02868352003-04-22 21:22:33 +0000468 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
469 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 }
471 return Dest;
472}
473
474static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000475 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000476 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000477 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000478 IMPLEMENT_SETCC(>, UByte);
479 IMPLEMENT_SETCC(>, SByte);
480 IMPLEMENT_SETCC(>, UShort);
481 IMPLEMENT_SETCC(>, Short);
482 IMPLEMENT_SETCC(>, UInt);
483 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000484 IMPLEMENT_SETCC(>, ULong);
485 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000486 IMPLEMENT_SETCC(>, Float);
487 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000488 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000489 default:
Chris Lattner02868352003-04-22 21:22:33 +0000490 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
491 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000492 }
493 return Dest;
494}
495
Chris Lattnerd7916e92003-05-10 21:22:39 +0000496void Interpreter::visitBinaryOperator(BinaryOperator &I) {
497 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000498 const Type *Ty = I.getOperand(0)->getType();
499 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
500 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000501 GenericValue R; // Result
502
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000503 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000504 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
505 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
506 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
507 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
508 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
509 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
510 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
511 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
512 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
513 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
514 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
515 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
516 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
517 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000518 default:
Chris Lattner02868352003-04-22 21:22:33 +0000519 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
520 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000521 }
522
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000523 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000524}
525
Chris Lattner759d34f2004-04-20 16:43:21 +0000526static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
527 GenericValue Src3) {
528 return Src1.BoolVal ? Src2 : Src3;
529}
530
531void Interpreter::visitSelectInst(SelectInst &I) {
532 ExecutionContext &SF = ECStack.back();
533 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
534 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
535 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
536 GenericValue R = executeSelectInst(Src1, Src2, Src3);
537 SetValue(&I, R, SF);
538}
539
540
Chris Lattner92101ac2001-08-23 17:05:04 +0000541//===----------------------------------------------------------------------===//
542// Terminator Instruction Implementations
543//===----------------------------------------------------------------------===//
544
Chris Lattnere43db882001-10-27 04:15:57 +0000545void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000546 // runAtExitHandlers() assumes there are no stack frames, but
547 // if exit() was called, then it had a stack frame. Blow away
548 // the stack before interpreting atexit handlers.
549 ECStack.clear ();
Brian Gaeke63438cc2003-12-11 00:22:59 +0000550 runAtExitHandlers ();
551 exit (GV.IntVal);
Chris Lattnere43db882001-10-27 04:15:57 +0000552}
553
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000554/// Pop the last stack frame off of ECStack and then copy the result
555/// back into the result variable if we are not returning void. The
556/// result variable may be the ExitCode, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000557/// CallInst if there was a previous stack frame. This method may
558/// invalidate any ECStack iterators you have. This method also takes
559/// care of switching to the normal destination BB, if we are returning
560/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000561///
562void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
563 GenericValue Result) {
564 // Pop the current stack frame.
565 ECStack.pop_back();
566
567 if (ECStack.empty()) { // Finished main. Put result into exit code...
568 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
569 ExitCode = Result.IntVal; // Capture the exit code of the program
570 } else {
571 ExitCode = 0;
572 }
573 } else {
574 // If we have a previous stack frame, and we have a previous call,
575 // fill in the return value...
576 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000577 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000578 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000579 SetValue(I, Result, CallingSF);
580 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
581 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000582 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000583 }
584 }
585}
586
Chris Lattnerd7916e92003-05-10 21:22:39 +0000587void Interpreter::visitReturnInst(ReturnInst &I) {
588 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000589 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000590 GenericValue Result;
591
592 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000593 if (I.getNumOperands()) {
594 RetTy = I.getReturnValue()->getType();
595 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000596 }
597
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000598 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000599}
600
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000601void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000602 // Unwind stack
603 Instruction *Inst;
604 do {
605 ECStack.pop_back ();
606 if (ECStack.empty ())
607 abort ();
608 Inst = ECStack.back ().Caller.getInstruction ();
609 } while (!(Inst && isa<InvokeInst> (Inst)));
610
611 // Return from invoke
612 ExecutionContext &InvokingSF = ECStack.back ();
613 InvokingSF.Caller = CallSite ();
614
615 // Go to exceptional destination BB of invoke instruction
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000616 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000617}
618
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000619void Interpreter::visitUnreachableInst(UnreachableInst &I) {
620 std::cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
621 abort();
622}
623
Chris Lattnerd7916e92003-05-10 21:22:39 +0000624void Interpreter::visitBranchInst(BranchInst &I) {
625 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000626 BasicBlock *Dest;
627
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000628 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
629 if (!I.isUnconditional()) {
630 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000631 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000632 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000633 }
Chris Lattner77113b62003-05-10 20:21:16 +0000634 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000635}
636
Chris Lattnerd7916e92003-05-10 21:22:39 +0000637void Interpreter::visitSwitchInst(SwitchInst &I) {
638 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000639 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
640 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000641
642 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000643 BasicBlock *Dest = 0;
644 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000645 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000646 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000647 Dest = cast<BasicBlock>(I.getOperand(i+1));
648 break;
649 }
Chris Lattner09e93922003-04-22 20:34:47 +0000650
651 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000652 SwitchToNewBasicBlock(Dest, SF);
653}
654
655// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
656// This function handles the actual updating of block and instruction iterators
657// as well as execution of all of the PHI nodes in the destination block.
658//
659// This method does this because all of the PHI nodes must be executed
660// atomically, reading their inputs before any of the results are updated. Not
661// doing this can cause problems if the PHI nodes depend on other PHI nodes for
662// their inputs. If the input PHI node is updated before it is read, incorrect
663// results can happen. Thus we use a two phase approach.
664//
665void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
666 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
667 SF.CurBB = Dest; // Update CurBB to branch destination
668 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
669
670 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
671
672 // Loop over all of the PHI nodes in the current block, reading their inputs.
673 std::vector<GenericValue> ResultValues;
674
675 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
676 // Search for the value corresponding to this previous bb...
677 int i = PN->getBasicBlockIndex(PrevBB);
678 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
679 Value *IncomingValue = PN->getIncomingValue(i);
680
681 // Save the incoming value for this PHI node...
682 ResultValues.push_back(getOperandValue(IncomingValue, SF));
683 }
684
685 // Now loop over all of the PHI nodes setting their values...
686 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000687 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
688 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000689 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000690 }
Chris Lattner09e93922003-04-22 20:34:47 +0000691}
692
Chris Lattner92101ac2001-08-23 17:05:04 +0000693//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000694// Memory Instruction Implementations
695//===----------------------------------------------------------------------===//
696
Chris Lattnerd7916e92003-05-10 21:22:39 +0000697void Interpreter::visitAllocationInst(AllocationInst &I) {
698 ExecutionContext &SF = ECStack.back();
699
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000700 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000701
Chris Lattnercc82cc12002-04-28 21:57:33 +0000702 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000703 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000704
705 // Allocate enough memory to hold the type...
Chris Lattnerd5644962005-01-08 20:05:34 +0000706 void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000707
Chris Lattnerfe11a972002-12-23 23:59:41 +0000708 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000709 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000710 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000711
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000712 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000713 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000714}
715
Chris Lattnerd7916e92003-05-10 21:22:39 +0000716void Interpreter::visitFreeInst(FreeInst &I) {
717 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000718 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
719 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000720 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000721 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000722}
723
Chris Lattnera34c5682002-08-27 22:33:45 +0000724// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000725//
Chris Lattner4af6de82003-11-25 20:44:56 +0000726GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
727 gep_type_iterator E,
Chris Lattnerfe11a972002-12-23 23:59:41 +0000728 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000729 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000730 "Cannot getElementOffset of a nonpointer type!");
731
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000732 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000733
734 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000735 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000736 const StructLayout *SLO = TD.getStructLayout(STy);
737
Brian Gaeke2798cd02003-12-12 05:13:05 +0000738 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand());
Chris Lattnerd5644962005-01-08 20:05:34 +0000739 unsigned Index = unsigned(CPU->getValue());
Chris Lattner782b9392001-11-26 18:18:18 +0000740
Chris Lattnerd5644962005-01-08 20:05:34 +0000741 Total += (PointerTy)SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000742 } else {
743 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000744 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000745 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
746
747 uint64_t Idx;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000748 switch (I.getOperand()->getType()->getTypeID()) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000749 default: assert(0 && "Illegal getelementptr index for sequential type!");
750 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
751 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
752 case Type::IntTyID: Idx = IdxGV.IntVal; break;
753 case Type::LongTyID: Idx = IdxGV.LongVal; break;
754 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
755 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
756 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
757 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
758 }
Chris Lattnerd5644962005-01-08 20:05:34 +0000759 Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx);
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000760 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000761 }
762
Chris Lattnera34c5682002-08-27 22:33:45 +0000763 GenericValue Result;
764 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
765 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000766}
767
Chris Lattnerd7916e92003-05-10 21:22:39 +0000768void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
769 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000770 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000771 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000772}
773
Chris Lattnerd7916e92003-05-10 21:22:39 +0000774void Interpreter::visitLoadInst(LoadInst &I) {
775 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000776 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000777 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000778 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000779 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000780}
781
Chris Lattnerd7916e92003-05-10 21:22:39 +0000782void Interpreter::visitStoreInst(StoreInst &I) {
783 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000784 GenericValue Val = getOperandValue(I.getOperand(0), SF);
785 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000786 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000787 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000788}
789
Chris Lattner86660982001-08-27 05:16:50 +0000790//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000791// Miscellaneous Instruction Implementations
792//===----------------------------------------------------------------------===//
793
Brian Gaekefea483d2003-11-07 20:04:22 +0000794void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000795 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +0000796
797 // Check to see if this is an intrinsic function call...
798 if (Function *F = CS.getCalledFunction())
Brian Gaeke34562ba2004-01-14 06:02:53 +0000799 if (F->isExternal ())
Chris Lattner73011782003-12-28 09:44:37 +0000800 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +0000801 case Intrinsic::not_intrinsic:
802 break;
Chris Lattner317201d2004-03-13 00:24:00 +0000803 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +0000804 GenericValue ArgIndex;
805 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
806 ArgIndex.UIntPairVal.second = 0;
807 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +0000808 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +0000809 }
Chris Lattner317201d2004-03-13 00:24:00 +0000810 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +0000811 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000812 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +0000813 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
814 return;
815 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +0000816 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +0000817 // class to transform it into hopefully tasty LLVM code.
818 //
819 Instruction *Prev = CS.getInstruction()->getPrev();
820 BasicBlock *Parent = CS.getInstruction()->getParent();
821 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
822
823 // Restore the CurInst pointer to the first instruction newly inserted, if
824 // any.
825 if (!Prev) {
826 SF.CurInst = Parent->begin();
827 } else {
828 SF.CurInst = Prev;
829 ++SF.CurInst;
830 }
Brian Gaekeb440dea2004-04-23 18:05:28 +0000831 return;
Chris Lattner73011782003-12-28 09:44:37 +0000832 }
833
Brian Gaekefea483d2003-11-07 20:04:22 +0000834 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000835 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000836 const unsigned NumArgs = SF.Caller.arg_size();
837 ArgVals.reserve(NumArgs);
838 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
839 e = SF.Caller.arg_end(); i != e; ++i) {
840 Value *V = *i;
841 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000842 // Promote all integral types whose size is < sizeof(int) into ints. We do
843 // this by zero or sign extending the value as appropriate according to the
844 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000845 const Type *Ty = V->getType();
846 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000847 if (Ty == Type::ShortTy)
848 ArgVals.back().IntVal = ArgVals.back().ShortVal;
849 else if (Ty == Type::UShortTy)
850 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
851 else if (Ty == Type::SByteTy)
852 ArgVals.back().IntVal = ArgVals.back().SByteVal;
853 else if (Ty == Type::UByteTy)
854 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
855 else if (Ty == Type::BoolTy)
856 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
857 else
858 assert(0 && "Unknown type!");
859 }
860 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000861
Chris Lattner070cf5e2001-11-07 20:12:30 +0000862 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000863 // and treat it as a function pointer.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000864 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000865 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000866}
867
Chris Lattner86660982001-08-27 05:16:50 +0000868#define IMPLEMENT_SHIFT(OP, TY) \
869 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
870
Brian Gaeke63438cc2003-12-11 00:22:59 +0000871static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
872 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000873 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000874 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000875 IMPLEMENT_SHIFT(<<, UByte);
876 IMPLEMENT_SHIFT(<<, SByte);
877 IMPLEMENT_SHIFT(<<, UShort);
878 IMPLEMENT_SHIFT(<<, Short);
879 IMPLEMENT_SHIFT(<<, UInt);
880 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000881 IMPLEMENT_SHIFT(<<, ULong);
882 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000883 default:
Chris Lattner02868352003-04-22 21:22:33 +0000884 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000885 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000886 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000887}
888
Brian Gaeke63438cc2003-12-11 00:22:59 +0000889static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
890 const Type *Ty) {
Chris Lattner86660982001-08-27 05:16:50 +0000891 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000892 switch (Ty->getTypeID()) {
Chris Lattner86660982001-08-27 05:16:50 +0000893 IMPLEMENT_SHIFT(>>, UByte);
894 IMPLEMENT_SHIFT(>>, SByte);
895 IMPLEMENT_SHIFT(>>, UShort);
896 IMPLEMENT_SHIFT(>>, Short);
897 IMPLEMENT_SHIFT(>>, UInt);
898 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000899 IMPLEMENT_SHIFT(>>, ULong);
900 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000901 default:
Chris Lattner02868352003-04-22 21:22:33 +0000902 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
903 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000904 }
Brian Gaeke63438cc2003-12-11 00:22:59 +0000905 return Dest;
906}
907
908void Interpreter::visitShl(ShiftInst &I) {
909 ExecutionContext &SF = ECStack.back();
910 const Type *Ty = I.getOperand(0)->getType();
911 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
912 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
913 GenericValue Dest;
914 Dest = executeShlInst (Src1, Src2, Ty);
915 SetValue(&I, Dest, SF);
916}
917
918void Interpreter::visitShr(ShiftInst &I) {
919 ExecutionContext &SF = ECStack.back();
920 const Type *Ty = I.getOperand(0)->getType();
921 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
922 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
923 GenericValue Dest;
924 Dest = executeShrInst (Src1, Src2, Ty);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000925 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000926}
927
928#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000929 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000930
931#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
932 case Type::DESTTY##TyID: \
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000933 switch (SrcTy->getTypeID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000934 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000935 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
936 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
937 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
938 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
939 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000940 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000942 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
943 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000944
945#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
946 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
947 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
948
949#define IMPLEMENT_CAST_CASE_END() \
Chris Lattnered6c0732004-07-15 02:51:32 +0000950 default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
Chris Lattner02868352003-04-22 21:22:33 +0000951 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000952 } \
953 break
954
955#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
956 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
957 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000958 IMPLEMENT_CAST_CASE_END()
959
Brian Gaeke29794cb2003-09-05 18:55:03 +0000960GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
961 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000962 const Type *SrcTy = SrcVal->getType();
963 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000964
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000965 switch (Ty->getTypeID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000966 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
967 IMPLEMENT_CAST_CASE(SByte , ( signed char));
968 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000969 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000970 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
971 IMPLEMENT_CAST_CASE(Int , ( signed int ));
972 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
973 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000974 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000975 IMPLEMENT_CAST_CASE(Float , (float));
976 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000977 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000978 default:
Chris Lattner02868352003-04-22 21:22:33 +0000979 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000980 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000981 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000982
983 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000984}
Chris Lattner92101ac2001-08-23 17:05:04 +0000985
Chris Lattnerd7916e92003-05-10 21:22:39 +0000986void Interpreter::visitCastInst(CastInst &I) {
987 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000988 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
989}
Chris Lattner92101ac2001-08-23 17:05:04 +0000990
Chris Lattner4c665492003-10-18 05:55:25 +0000991void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000992 ExecutionContext &SF = ECStack.back();
993
Brian Gaeke9d20b712004-02-25 23:01:48 +0000994 // Get the incoming valist parameter. LLI treats the valist as a
995 // (ec-stack-depth var-arg-index) pair.
Chris Lattner4c665492003-10-18 05:55:25 +0000996 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
997
Brian Gaeke8da17482003-11-13 06:06:01 +0000998 // Move the pointer to the next vararg.
Brian Gaeke9d20b712004-02-25 23:01:48 +0000999 ++VAList.UIntPairVal.second;
Chris Lattner4c665492003-10-18 05:55:25 +00001000 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +00001001}
Chris Lattner92101ac2001-08-23 17:05:04 +00001002
Brian Gaekec1a2be12003-11-07 21:20:47 +00001003#define IMPLEMENT_VAARG(TY) \
1004 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1005
1006void Interpreter::visitVAArgInst(VAArgInst &I) {
1007 ExecutionContext &SF = ECStack.back();
1008
Brian Gaeke9d20b712004-02-25 23:01:48 +00001009 // Get the incoming valist parameter. LLI treats the valist as a
1010 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001011 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001012 GenericValue Dest;
1013 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1014 .VarArgs[VAList.UIntPairVal.second];
Brian Gaekec1a2be12003-11-07 21:20:47 +00001015 const Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001016 switch (Ty->getTypeID()) {
Brian Gaekec1a2be12003-11-07 21:20:47 +00001017 IMPLEMENT_VAARG(UByte);
1018 IMPLEMENT_VAARG(SByte);
1019 IMPLEMENT_VAARG(UShort);
1020 IMPLEMENT_VAARG(Short);
1021 IMPLEMENT_VAARG(UInt);
1022 IMPLEMENT_VAARG(Int);
1023 IMPLEMENT_VAARG(ULong);
1024 IMPLEMENT_VAARG(Long);
1025 IMPLEMENT_VAARG(Pointer);
1026 IMPLEMENT_VAARG(Float);
1027 IMPLEMENT_VAARG(Double);
1028 IMPLEMENT_VAARG(Bool);
1029 default:
1030 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1031 abort();
1032 }
1033
1034 // Set the Value of this Instruction.
1035 SetValue(&I, Dest, SF);
1036}
1037
Chris Lattner92101ac2001-08-23 17:05:04 +00001038//===----------------------------------------------------------------------===//
1039// Dispatch and Execution Code
1040//===----------------------------------------------------------------------===//
1041
Chris Lattner92101ac2001-08-23 17:05:04 +00001042//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001043// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00001044//
Chris Lattnerda82ed52003-05-08 16:18:31 +00001045void Interpreter::callFunction(Function *F,
1046 const std::vector<GenericValue> &ArgVals) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +00001047 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1048 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
Chris Lattner365a76e2001-09-10 04:49:44 +00001049 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00001050 // Make a new stack frame... and fill it in.
1051 ECStack.push_back(ExecutionContext());
1052 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00001053 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00001054
1055 // Special handling for external functions.
1056 if (F->isExternal()) {
1057 GenericValue Result = callExternalFunction (F, ArgVals);
1058 // Simulate a 'ret' instruction of the appropriate type.
1059 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1060 return;
1061 }
1062
1063 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00001064 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001065 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00001066
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001067 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00001068 assert((ArgVals.size() == F->arg_size() ||
1069 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001070 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00001071
1072 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00001073 unsigned i = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +00001074 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001075 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00001076
1077 // Handle varargs arguments...
1078 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00001079}
1080
Chris Lattner92101ac2001-08-23 17:05:04 +00001081void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00001082 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001083 // Interpret a single instruction & increment the "PC".
1084 ExecutionContext &SF = ECStack.back(); // Current stack frame
1085 Instruction &I = *SF.CurInst++; // Increment before execute
1086
1087 // Track the number of dynamic instructions executed.
1088 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00001089
Brian Gaeke63438cc2003-12-11 00:22:59 +00001090 DEBUG(std::cerr << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001091 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +00001092 }
1093}