blob: 6a224b97fdd2260b2317217df101a988520511bd [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
14#include "Interpreter.h"
Chris Lattnerd5bc41a2003-04-25 04:21:19 +000015#include "llvm/Instructions.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000016#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000018#include "Support/Statistic.h"
Brian Gaeke235b2002003-10-10 17:03:22 +000019#include <cmath> // For fmod
Chris Lattner2e42d3a2001-10-15 05:51:48 +000020
Chris Lattnerfe11a972002-12-23 23:59:41 +000021Interpreter *TheEE = 0;
22
Chris Lattnerbbdabce2002-12-08 05:51:08 +000023namespace {
24 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
25}
26
Chris Lattner2e42d3a2001-10-15 05:51:48 +000027//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000028// Value Manipulation code
29//===----------------------------------------------------------------------===//
30
Chris Lattnera34c5682002-08-27 22:33:45 +000031// Operations used by constant expr implementations...
32static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
33 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000034static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000035 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000036
Brian Gaeke29794cb2003-09-05 18:55:03 +000037GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000038 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
39 switch (CE->getOpcode()) {
40 case Instruction::Cast:
41 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
42 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000043 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
44 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000045 case Instruction::Add:
46 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
47 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000048 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000049 default:
Chris Lattner02868352003-04-22 21:22:33 +000050 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000051 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000052 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000053 }
54 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000055 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000056 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000057 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +000058 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000059 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +000060 }
61}
62
Chris Lattner39bb5b42001-10-15 13:25:40 +000063static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000064 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000065}
66
Chris Lattner39bb5b42001-10-15 13:25:40 +000067//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +000068// Annotation Wrangling code
69//===----------------------------------------------------------------------===//
70
71void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000072 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000073}
74
Chris Lattner2adcd832002-05-03 19:52:30 +000075//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000076// Binary Instruction Implementations
77//===----------------------------------------------------------------------===//
78
79#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
80 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
81
82static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000083 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +000084 GenericValue Dest;
85 switch (Ty->getPrimitiveID()) {
86 IMPLEMENT_BINARY_OPERATOR(+, UByte);
87 IMPLEMENT_BINARY_OPERATOR(+, SByte);
88 IMPLEMENT_BINARY_OPERATOR(+, UShort);
89 IMPLEMENT_BINARY_OPERATOR(+, Short);
90 IMPLEMENT_BINARY_OPERATOR(+, UInt);
91 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +000092 IMPLEMENT_BINARY_OPERATOR(+, ULong);
93 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +000094 IMPLEMENT_BINARY_OPERATOR(+, Float);
95 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000096 default:
Chris Lattner02868352003-04-22 21:22:33 +000097 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
98 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +000099 }
100 return Dest;
101}
102
103static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000104 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000105 GenericValue Dest;
106 switch (Ty->getPrimitiveID()) {
107 IMPLEMENT_BINARY_OPERATOR(-, UByte);
108 IMPLEMENT_BINARY_OPERATOR(-, SByte);
109 IMPLEMENT_BINARY_OPERATOR(-, UShort);
110 IMPLEMENT_BINARY_OPERATOR(-, Short);
111 IMPLEMENT_BINARY_OPERATOR(-, UInt);
112 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000113 IMPLEMENT_BINARY_OPERATOR(-, ULong);
114 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000115 IMPLEMENT_BINARY_OPERATOR(-, Float);
116 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000117 default:
Chris Lattner02868352003-04-22 21:22:33 +0000118 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
119 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000120 }
121 return Dest;
122}
123
Chris Lattnerc2593162001-10-27 08:28:11 +0000124static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000125 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000126 GenericValue Dest;
127 switch (Ty->getPrimitiveID()) {
128 IMPLEMENT_BINARY_OPERATOR(*, UByte);
129 IMPLEMENT_BINARY_OPERATOR(*, SByte);
130 IMPLEMENT_BINARY_OPERATOR(*, UShort);
131 IMPLEMENT_BINARY_OPERATOR(*, Short);
132 IMPLEMENT_BINARY_OPERATOR(*, UInt);
133 IMPLEMENT_BINARY_OPERATOR(*, Int);
134 IMPLEMENT_BINARY_OPERATOR(*, ULong);
135 IMPLEMENT_BINARY_OPERATOR(*, Long);
136 IMPLEMENT_BINARY_OPERATOR(*, Float);
137 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000138 default:
Chris Lattner02868352003-04-22 21:22:33 +0000139 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
140 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000141 }
142 return Dest;
143}
144
145static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000146 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000147 GenericValue Dest;
148 switch (Ty->getPrimitiveID()) {
149 IMPLEMENT_BINARY_OPERATOR(/, UByte);
150 IMPLEMENT_BINARY_OPERATOR(/, SByte);
151 IMPLEMENT_BINARY_OPERATOR(/, UShort);
152 IMPLEMENT_BINARY_OPERATOR(/, Short);
153 IMPLEMENT_BINARY_OPERATOR(/, UInt);
154 IMPLEMENT_BINARY_OPERATOR(/, Int);
155 IMPLEMENT_BINARY_OPERATOR(/, ULong);
156 IMPLEMENT_BINARY_OPERATOR(/, Long);
157 IMPLEMENT_BINARY_OPERATOR(/, Float);
158 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000159 default:
Chris Lattner02868352003-04-22 21:22:33 +0000160 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
161 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000162 }
163 return Dest;
164}
165
166static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000167 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000168 GenericValue Dest;
169 switch (Ty->getPrimitiveID()) {
170 IMPLEMENT_BINARY_OPERATOR(%, UByte);
171 IMPLEMENT_BINARY_OPERATOR(%, SByte);
172 IMPLEMENT_BINARY_OPERATOR(%, UShort);
173 IMPLEMENT_BINARY_OPERATOR(%, Short);
174 IMPLEMENT_BINARY_OPERATOR(%, UInt);
175 IMPLEMENT_BINARY_OPERATOR(%, Int);
176 IMPLEMENT_BINARY_OPERATOR(%, ULong);
177 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000178 case Type::FloatTyID:
179 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
180 break;
181 case Type::DoubleTyID:
182 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
183 break;
184 default:
Chris Lattner02868352003-04-22 21:22:33 +0000185 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
186 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000187 }
188 return Dest;
189}
190
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000191static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000192 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000193 GenericValue Dest;
194 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000195 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000196 IMPLEMENT_BINARY_OPERATOR(&, UByte);
197 IMPLEMENT_BINARY_OPERATOR(&, SByte);
198 IMPLEMENT_BINARY_OPERATOR(&, UShort);
199 IMPLEMENT_BINARY_OPERATOR(&, Short);
200 IMPLEMENT_BINARY_OPERATOR(&, UInt);
201 IMPLEMENT_BINARY_OPERATOR(&, Int);
202 IMPLEMENT_BINARY_OPERATOR(&, ULong);
203 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000204 default:
Chris Lattner02868352003-04-22 21:22:33 +0000205 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
206 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000207 }
208 return Dest;
209}
210
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000211static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000212 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000213 GenericValue Dest;
214 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000215 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000216 IMPLEMENT_BINARY_OPERATOR(|, UByte);
217 IMPLEMENT_BINARY_OPERATOR(|, SByte);
218 IMPLEMENT_BINARY_OPERATOR(|, UShort);
219 IMPLEMENT_BINARY_OPERATOR(|, Short);
220 IMPLEMENT_BINARY_OPERATOR(|, UInt);
221 IMPLEMENT_BINARY_OPERATOR(|, Int);
222 IMPLEMENT_BINARY_OPERATOR(|, ULong);
223 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000224 default:
Chris Lattner02868352003-04-22 21:22:33 +0000225 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
226 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000227 }
228 return Dest;
229}
230
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000231static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000232 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000233 GenericValue Dest;
234 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000235 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000236 IMPLEMENT_BINARY_OPERATOR(^, UByte);
237 IMPLEMENT_BINARY_OPERATOR(^, SByte);
238 IMPLEMENT_BINARY_OPERATOR(^, UShort);
239 IMPLEMENT_BINARY_OPERATOR(^, Short);
240 IMPLEMENT_BINARY_OPERATOR(^, UInt);
241 IMPLEMENT_BINARY_OPERATOR(^, Int);
242 IMPLEMENT_BINARY_OPERATOR(^, ULong);
243 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000244 default:
Chris Lattner02868352003-04-22 21:22:33 +0000245 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
246 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000247 }
248 return Dest;
249}
250
Chris Lattner92101ac2001-08-23 17:05:04 +0000251#define IMPLEMENT_SETCC(OP, TY) \
252 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
253
Chris Lattnerfd506f52003-04-23 19:55:35 +0000254// Handle pointers specially because they must be compared with only as much
255// width as the host has. We _do not_ want to be comparing 64 bit values when
256// running on a 32-bit target, otherwise the upper 32 bits might mess up
257// comparisons if they contain garbage.
258#define IMPLEMENT_POINTERSETCC(OP) \
259 case Type::PointerTyID: \
260 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
261 (void*)(intptr_t)Src2.PointerVal; break
262
Chris Lattner92101ac2001-08-23 17:05:04 +0000263static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000264 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000265 GenericValue Dest;
266 switch (Ty->getPrimitiveID()) {
267 IMPLEMENT_SETCC(==, UByte);
268 IMPLEMENT_SETCC(==, SByte);
269 IMPLEMENT_SETCC(==, UShort);
270 IMPLEMENT_SETCC(==, Short);
271 IMPLEMENT_SETCC(==, UInt);
272 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000273 IMPLEMENT_SETCC(==, ULong);
274 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000275 IMPLEMENT_SETCC(==, Float);
276 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000277 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000278 default:
Chris Lattner02868352003-04-22 21:22:33 +0000279 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
280 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000281 }
282 return Dest;
283}
284
285static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000286 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000287 GenericValue Dest;
288 switch (Ty->getPrimitiveID()) {
289 IMPLEMENT_SETCC(!=, UByte);
290 IMPLEMENT_SETCC(!=, SByte);
291 IMPLEMENT_SETCC(!=, UShort);
292 IMPLEMENT_SETCC(!=, Short);
293 IMPLEMENT_SETCC(!=, UInt);
294 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000295 IMPLEMENT_SETCC(!=, ULong);
296 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000297 IMPLEMENT_SETCC(!=, Float);
298 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000299 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000300
Chris Lattner92101ac2001-08-23 17:05:04 +0000301 default:
Chris Lattner02868352003-04-22 21:22:33 +0000302 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
303 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000304 }
305 return Dest;
306}
307
308static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000309 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000310 GenericValue Dest;
311 switch (Ty->getPrimitiveID()) {
312 IMPLEMENT_SETCC(<=, UByte);
313 IMPLEMENT_SETCC(<=, SByte);
314 IMPLEMENT_SETCC(<=, UShort);
315 IMPLEMENT_SETCC(<=, Short);
316 IMPLEMENT_SETCC(<=, UInt);
317 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000318 IMPLEMENT_SETCC(<=, ULong);
319 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000320 IMPLEMENT_SETCC(<=, Float);
321 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000322 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000323 default:
Chris Lattner02868352003-04-22 21:22:33 +0000324 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
325 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000326 }
327 return Dest;
328}
329
330static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000331 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000332 GenericValue Dest;
333 switch (Ty->getPrimitiveID()) {
334 IMPLEMENT_SETCC(>=, UByte);
335 IMPLEMENT_SETCC(>=, SByte);
336 IMPLEMENT_SETCC(>=, UShort);
337 IMPLEMENT_SETCC(>=, Short);
338 IMPLEMENT_SETCC(>=, UInt);
339 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000340 IMPLEMENT_SETCC(>=, ULong);
341 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000342 IMPLEMENT_SETCC(>=, Float);
343 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000344 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000345 default:
Chris Lattner02868352003-04-22 21:22:33 +0000346 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
347 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000348 }
349 return Dest;
350}
351
352static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000353 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000354 GenericValue Dest;
355 switch (Ty->getPrimitiveID()) {
356 IMPLEMENT_SETCC(<, UByte);
357 IMPLEMENT_SETCC(<, SByte);
358 IMPLEMENT_SETCC(<, UShort);
359 IMPLEMENT_SETCC(<, Short);
360 IMPLEMENT_SETCC(<, UInt);
361 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000362 IMPLEMENT_SETCC(<, ULong);
363 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000364 IMPLEMENT_SETCC(<, Float);
365 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000366 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000367 default:
Chris Lattner02868352003-04-22 21:22:33 +0000368 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
369 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000370 }
371 return Dest;
372}
373
374static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000375 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000376 GenericValue Dest;
377 switch (Ty->getPrimitiveID()) {
378 IMPLEMENT_SETCC(>, UByte);
379 IMPLEMENT_SETCC(>, SByte);
380 IMPLEMENT_SETCC(>, UShort);
381 IMPLEMENT_SETCC(>, Short);
382 IMPLEMENT_SETCC(>, UInt);
383 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000384 IMPLEMENT_SETCC(>, ULong);
385 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000386 IMPLEMENT_SETCC(>, Float);
387 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000388 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000389 default:
Chris Lattner02868352003-04-22 21:22:33 +0000390 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
391 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000392 }
393 return Dest;
394}
395
Chris Lattnerd7916e92003-05-10 21:22:39 +0000396void Interpreter::visitBinaryOperator(BinaryOperator &I) {
397 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000398 const Type *Ty = I.getOperand(0)->getType();
399 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
400 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 GenericValue R; // Result
402
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000403 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000404 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
405 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
406 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
407 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
408 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
409 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
410 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
411 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
412 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
413 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
414 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
415 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
416 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
417 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000418 default:
Chris Lattner02868352003-04-22 21:22:33 +0000419 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
420 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000421 }
422
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000423 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000424}
425
Chris Lattner92101ac2001-08-23 17:05:04 +0000426//===----------------------------------------------------------------------===//
427// Terminator Instruction Implementations
428//===----------------------------------------------------------------------===//
429
Chris Lattnere43db882001-10-27 04:15:57 +0000430void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnere43db882001-10-27 04:15:57 +0000431 ExitCode = GV.SByteVal;
432 ECStack.clear();
433}
434
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000435/// Pop the last stack frame off of ECStack and then copy the result
436/// back into the result variable if we are not returning void. The
437/// result variable may be the ExitCode, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000438/// CallInst if there was a previous stack frame. This method may
439/// invalidate any ECStack iterators you have. This method also takes
440/// care of switching to the normal destination BB, if we are returning
441/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000442///
443void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
444 GenericValue Result) {
445 // Pop the current stack frame.
446 ECStack.pop_back();
447
448 if (ECStack.empty()) { // Finished main. Put result into exit code...
449 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
450 ExitCode = Result.IntVal; // Capture the exit code of the program
451 } else {
452 ExitCode = 0;
453 }
454 } else {
455 // If we have a previous stack frame, and we have a previous call,
456 // fill in the return value...
457 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000458 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000459 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000460 SetValue(I, Result, CallingSF);
461 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
462 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000463 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000464 }
465 }
466}
467
Chris Lattnerd7916e92003-05-10 21:22:39 +0000468void Interpreter::visitReturnInst(ReturnInst &I) {
469 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000470 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000471 GenericValue Result;
472
473 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000474 if (I.getNumOperands()) {
475 RetTy = I.getReturnValue()->getType();
476 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 }
478
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000479 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000480}
481
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000482void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000483 // Unwind stack
484 Instruction *Inst;
485 do {
486 ECStack.pop_back ();
487 if (ECStack.empty ())
488 abort ();
489 Inst = ECStack.back ().Caller.getInstruction ();
490 } while (!(Inst && isa<InvokeInst> (Inst)));
491
492 // Return from invoke
493 ExecutionContext &InvokingSF = ECStack.back ();
494 InvokingSF.Caller = CallSite ();
495
496 // Go to exceptional destination BB of invoke instruction
497 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
498 InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000499}
500
Chris Lattnerd7916e92003-05-10 21:22:39 +0000501void Interpreter::visitBranchInst(BranchInst &I) {
502 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000503 BasicBlock *Dest;
504
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000505 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
506 if (!I.isUnconditional()) {
507 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000508 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000509 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000510 }
Chris Lattner77113b62003-05-10 20:21:16 +0000511 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000512}
513
Chris Lattnerd7916e92003-05-10 21:22:39 +0000514void Interpreter::visitSwitchInst(SwitchInst &I) {
515 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000516 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
517 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000518
519 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000520 BasicBlock *Dest = 0;
521 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000522 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000523 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000524 Dest = cast<BasicBlock>(I.getOperand(i+1));
525 break;
526 }
Chris Lattner09e93922003-04-22 20:34:47 +0000527
528 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000529 SwitchToNewBasicBlock(Dest, SF);
530}
531
532// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
533// This function handles the actual updating of block and instruction iterators
534// as well as execution of all of the PHI nodes in the destination block.
535//
536// This method does this because all of the PHI nodes must be executed
537// atomically, reading their inputs before any of the results are updated. Not
538// doing this can cause problems if the PHI nodes depend on other PHI nodes for
539// their inputs. If the input PHI node is updated before it is read, incorrect
540// results can happen. Thus we use a two phase approach.
541//
542void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
543 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
544 SF.CurBB = Dest; // Update CurBB to branch destination
545 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
546
547 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
548
549 // Loop over all of the PHI nodes in the current block, reading their inputs.
550 std::vector<GenericValue> ResultValues;
551
552 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
553 // Search for the value corresponding to this previous bb...
554 int i = PN->getBasicBlockIndex(PrevBB);
555 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
556 Value *IncomingValue = PN->getIncomingValue(i);
557
558 // Save the incoming value for this PHI node...
559 ResultValues.push_back(getOperandValue(IncomingValue, SF));
560 }
561
562 // Now loop over all of the PHI nodes setting their values...
563 SF.CurInst = SF.CurBB->begin();
564 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
565 ++SF.CurInst, ++i)
566 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000567}
568
Chris Lattner92101ac2001-08-23 17:05:04 +0000569//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000570// Memory Instruction Implementations
571//===----------------------------------------------------------------------===//
572
Chris Lattnerd7916e92003-05-10 21:22:39 +0000573void Interpreter::visitAllocationInst(AllocationInst &I) {
574 ExecutionContext &SF = ECStack.back();
575
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000576 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000577
Chris Lattnercc82cc12002-04-28 21:57:33 +0000578 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000579 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000580
581 // Allocate enough memory to hold the type...
Brian Gaeke2fa82122003-11-05 01:02:14 +0000582 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000583
Chris Lattnerfe11a972002-12-23 23:59:41 +0000584 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000585 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000586 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000587
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000588 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000589 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000590}
591
Chris Lattnerd7916e92003-05-10 21:22:39 +0000592void Interpreter::visitFreeInst(FreeInst &I) {
593 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000594 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
595 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000596 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000597 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000598}
599
Chris Lattnera34c5682002-08-27 22:33:45 +0000600// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000601//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000602GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
603 User::op_iterator E,
604 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000605 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000606 "Cannot getElementOffset of a nonpointer type!");
607
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000608 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000609 const Type *Ty = Ptr->getType();
610
611 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000612 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
613 const StructLayout *SLO = TD.getStructLayout(STy);
614
Misha Brukmand5d96b92003-10-10 17:42:19 +0000615 // Indices must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000616 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000617 assert(CPU->getType() == Type::UByteTy);
618 unsigned Index = CPU->getValue();
619
Chris Lattner782b9392001-11-26 18:18:18 +0000620 Total += SLO->MemberOffsets[Index];
621 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000622 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattner006a4a52003-02-25 21:14:59 +0000623 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000624 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000625 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000626 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000627 unsigned Size = TD.getTypeSize(Ty);
628 Total += Size*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000629 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000630 }
631
Chris Lattnera34c5682002-08-27 22:33:45 +0000632 GenericValue Result;
633 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
634 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000635}
636
Chris Lattnerd7916e92003-05-10 21:22:39 +0000637void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
638 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000639 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000640 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000641}
642
Chris Lattnerd7916e92003-05-10 21:22:39 +0000643void Interpreter::visitLoadInst(LoadInst &I) {
644 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000645 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000646 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000647 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000648 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000649}
650
Chris Lattnerd7916e92003-05-10 21:22:39 +0000651void Interpreter::visitStoreInst(StoreInst &I) {
652 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000653 GenericValue Val = getOperandValue(I.getOperand(0), SF);
654 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000655 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000656 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000657}
658
Chris Lattner86660982001-08-27 05:16:50 +0000659//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000660// Miscellaneous Instruction Implementations
661//===----------------------------------------------------------------------===//
662
Brian Gaekefea483d2003-11-07 20:04:22 +0000663void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000664 ExecutionContext &SF = ECStack.back();
Brian Gaekefea483d2003-11-07 20:04:22 +0000665 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000666 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000667 const unsigned NumArgs = SF.Caller.arg_size();
668 ArgVals.reserve(NumArgs);
669 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
670 e = SF.Caller.arg_end(); i != e; ++i) {
671 Value *V = *i;
672 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000673 // Promote all integral types whose size is < sizeof(int) into ints. We do
674 // this by zero or sign extending the value as appropriate according to the
675 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000676 const Type *Ty = V->getType();
677 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000678 if (Ty == Type::ShortTy)
679 ArgVals.back().IntVal = ArgVals.back().ShortVal;
680 else if (Ty == Type::UShortTy)
681 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
682 else if (Ty == Type::SByteTy)
683 ArgVals.back().IntVal = ArgVals.back().SByteVal;
684 else if (Ty == Type::UByteTy)
685 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
686 else if (Ty == Type::BoolTy)
687 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
688 else
689 assert(0 && "Unknown type!");
690 }
691 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000692
Chris Lattner070cf5e2001-11-07 20:12:30 +0000693 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000694 // and treat it as a function pointer.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000695 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000696 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000697}
698
Chris Lattner86660982001-08-27 05:16:50 +0000699#define IMPLEMENT_SHIFT(OP, TY) \
700 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
701
Chris Lattnerd7916e92003-05-10 21:22:39 +0000702void Interpreter::visitShl(ShiftInst &I) {
703 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000704 const Type *Ty = I.getOperand(0)->getType();
705 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
706 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000707 GenericValue Dest;
708
709 switch (Ty->getPrimitiveID()) {
710 IMPLEMENT_SHIFT(<<, UByte);
711 IMPLEMENT_SHIFT(<<, SByte);
712 IMPLEMENT_SHIFT(<<, UShort);
713 IMPLEMENT_SHIFT(<<, Short);
714 IMPLEMENT_SHIFT(<<, UInt);
715 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000716 IMPLEMENT_SHIFT(<<, ULong);
717 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000718 default:
Chris Lattner02868352003-04-22 21:22:33 +0000719 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000720 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000721 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000722}
723
Chris Lattnerd7916e92003-05-10 21:22:39 +0000724void Interpreter::visitShr(ShiftInst &I) {
725 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000726 const Type *Ty = I.getOperand(0)->getType();
727 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
728 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000729 GenericValue Dest;
730
731 switch (Ty->getPrimitiveID()) {
732 IMPLEMENT_SHIFT(>>, UByte);
733 IMPLEMENT_SHIFT(>>, SByte);
734 IMPLEMENT_SHIFT(>>, UShort);
735 IMPLEMENT_SHIFT(>>, Short);
736 IMPLEMENT_SHIFT(>>, UInt);
737 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000738 IMPLEMENT_SHIFT(>>, ULong);
739 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000740 default:
Chris Lattner02868352003-04-22 21:22:33 +0000741 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
742 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000743 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000744 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000745}
746
747#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000748 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000749
750#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
751 case Type::DESTTY##TyID: \
752 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000753 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000754 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
755 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
756 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
757 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
758 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000759 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
760 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000761 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
762 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000763
764#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
765 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
766 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
767
768#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000769 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
770 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000771 } \
772 break
773
774#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
775 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
776 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000777 IMPLEMENT_CAST_CASE_END()
778
Brian Gaeke29794cb2003-09-05 18:55:03 +0000779GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
780 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000781 const Type *SrcTy = SrcVal->getType();
782 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000783
784 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000785 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
786 IMPLEMENT_CAST_CASE(SByte , ( signed char));
787 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000788 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000789 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
790 IMPLEMENT_CAST_CASE(Int , ( signed int ));
791 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
792 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000793 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000794 IMPLEMENT_CAST_CASE(Float , (float));
795 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000796 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000797 default:
Chris Lattner02868352003-04-22 21:22:33 +0000798 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000799 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000800 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000801
802 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000803}
Chris Lattner92101ac2001-08-23 17:05:04 +0000804
Chris Lattnerd7916e92003-05-10 21:22:39 +0000805void Interpreter::visitCastInst(CastInst &I) {
806 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000807 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
808}
Chris Lattner92101ac2001-08-23 17:05:04 +0000809
Chris Lattner4c665492003-10-18 05:55:25 +0000810void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000811 ExecutionContext &SF = ECStack.back();
812
Chris Lattner4c665492003-10-18 05:55:25 +0000813 // Get the incoming valist element. LLI treats the valist as an integer.
814 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
815
816 // Move to the next operand.
Chris Lattner374344c2003-05-08 16:52:43 +0000817 unsigned Argument = VAList.IntVal++;
Chris Lattner374344c2003-05-08 16:52:43 +0000818 assert(Argument < SF.VarArgs.size() &&
819 "Accessing past the last vararg argument!");
Chris Lattner4c665492003-10-18 05:55:25 +0000820 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000821}
Chris Lattner92101ac2001-08-23 17:05:04 +0000822
823//===----------------------------------------------------------------------===//
824// Dispatch and Execution Code
825//===----------------------------------------------------------------------===//
826
Chris Lattner92101ac2001-08-23 17:05:04 +0000827//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000828// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000829//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000830void Interpreter::callFunction(Function *F,
831 const std::vector<GenericValue> &ArgVals) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000832 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
833 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
Chris Lattner365a76e2001-09-10 04:49:44 +0000834 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +0000835 // Make a new stack frame... and fill it in.
836 ECStack.push_back(ExecutionContext());
837 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000838 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000839
840 // Special handling for external functions.
841 if (F->isExternal()) {
842 GenericValue Result = callExternalFunction (F, ArgVals);
843 // Simulate a 'ret' instruction of the appropriate type.
844 popStackAndReturnValueToCaller (F->getReturnType (), Result);
845 return;
846 }
847
848 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +0000849 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000850 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000851
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000852 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000853 assert((ArgVals.size() == F->asize() ||
854 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000855 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000856
857 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000858 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000859 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000860 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000861
862 // Handle varargs arguments...
863 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000864}
865
Chris Lattner92101ac2001-08-23 17:05:04 +0000866void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000867 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000868 // Interpret a single instruction & increment the "PC".
869 ExecutionContext &SF = ECStack.back(); // Current stack frame
870 Instruction &I = *SF.CurInst++; // Increment before execute
871
872 // Track the number of dynamic instructions executed.
873 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +0000874
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000875 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +0000876 }
877}