blob: aa32983cc38dce8565c2fb502ed184bed820076c [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
Brian Gaeked0fde302003-11-11 22:41:34 +000021namespace llvm {
Chris Lattnerfe11a972002-12-23 23:59:41 +000022
Chris Lattnerbbdabce2002-12-08 05:51:08 +000023namespace {
24 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
25}
26
Brian Gaeked0fde302003-11-11 22:41:34 +000027Interpreter *TheEE = 0;
28
Chris Lattner2e42d3a2001-10-15 05:51:48 +000029//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000030// Value Manipulation code
31//===----------------------------------------------------------------------===//
32
Chris Lattnera34c5682002-08-27 22:33:45 +000033// Operations used by constant expr implementations...
34static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
35 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000036static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000037 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000038
Brian Gaeke29794cb2003-09-05 18:55:03 +000039GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000040 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
41 switch (CE->getOpcode()) {
42 case Instruction::Cast:
43 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
44 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000045 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
46 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000047 case Instruction::Add:
48 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
49 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000050 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000051 default:
Chris Lattner02868352003-04-22 21:22:33 +000052 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000053 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000054 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000055 }
56 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000057 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000058 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000059 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +000060 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000061 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +000062 }
63}
64
Chris Lattner39bb5b42001-10-15 13:25:40 +000065static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000066 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000067}
68
Chris Lattner39bb5b42001-10-15 13:25:40 +000069//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +000070// Annotation Wrangling code
71//===----------------------------------------------------------------------===//
72
73void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000074 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000075}
76
Chris Lattner2adcd832002-05-03 19:52:30 +000077//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000078// Binary Instruction Implementations
79//===----------------------------------------------------------------------===//
80
81#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
82 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
83
84static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000085 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +000086 GenericValue Dest;
87 switch (Ty->getPrimitiveID()) {
88 IMPLEMENT_BINARY_OPERATOR(+, UByte);
89 IMPLEMENT_BINARY_OPERATOR(+, SByte);
90 IMPLEMENT_BINARY_OPERATOR(+, UShort);
91 IMPLEMENT_BINARY_OPERATOR(+, Short);
92 IMPLEMENT_BINARY_OPERATOR(+, UInt);
93 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +000094 IMPLEMENT_BINARY_OPERATOR(+, ULong);
95 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +000096 IMPLEMENT_BINARY_OPERATOR(+, Float);
97 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000098 default:
Chris Lattner02868352003-04-22 21:22:33 +000099 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
100 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000101 }
102 return Dest;
103}
104
105static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000106 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000107 GenericValue Dest;
108 switch (Ty->getPrimitiveID()) {
109 IMPLEMENT_BINARY_OPERATOR(-, UByte);
110 IMPLEMENT_BINARY_OPERATOR(-, SByte);
111 IMPLEMENT_BINARY_OPERATOR(-, UShort);
112 IMPLEMENT_BINARY_OPERATOR(-, Short);
113 IMPLEMENT_BINARY_OPERATOR(-, UInt);
114 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000115 IMPLEMENT_BINARY_OPERATOR(-, ULong);
116 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000117 IMPLEMENT_BINARY_OPERATOR(-, Float);
118 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000119 default:
Chris Lattner02868352003-04-22 21:22:33 +0000120 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
121 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000122 }
123 return Dest;
124}
125
Chris Lattnerc2593162001-10-27 08:28:11 +0000126static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000127 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000128 GenericValue Dest;
129 switch (Ty->getPrimitiveID()) {
130 IMPLEMENT_BINARY_OPERATOR(*, UByte);
131 IMPLEMENT_BINARY_OPERATOR(*, SByte);
132 IMPLEMENT_BINARY_OPERATOR(*, UShort);
133 IMPLEMENT_BINARY_OPERATOR(*, Short);
134 IMPLEMENT_BINARY_OPERATOR(*, UInt);
135 IMPLEMENT_BINARY_OPERATOR(*, Int);
136 IMPLEMENT_BINARY_OPERATOR(*, ULong);
137 IMPLEMENT_BINARY_OPERATOR(*, Long);
138 IMPLEMENT_BINARY_OPERATOR(*, Float);
139 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000140 default:
Chris Lattner02868352003-04-22 21:22:33 +0000141 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
142 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000143 }
144 return Dest;
145}
146
147static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000148 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000149 GenericValue Dest;
150 switch (Ty->getPrimitiveID()) {
151 IMPLEMENT_BINARY_OPERATOR(/, UByte);
152 IMPLEMENT_BINARY_OPERATOR(/, SByte);
153 IMPLEMENT_BINARY_OPERATOR(/, UShort);
154 IMPLEMENT_BINARY_OPERATOR(/, Short);
155 IMPLEMENT_BINARY_OPERATOR(/, UInt);
156 IMPLEMENT_BINARY_OPERATOR(/, Int);
157 IMPLEMENT_BINARY_OPERATOR(/, ULong);
158 IMPLEMENT_BINARY_OPERATOR(/, Long);
159 IMPLEMENT_BINARY_OPERATOR(/, Float);
160 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000161 default:
Chris Lattner02868352003-04-22 21:22:33 +0000162 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
163 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000164 }
165 return Dest;
166}
167
168static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000169 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000170 GenericValue Dest;
171 switch (Ty->getPrimitiveID()) {
172 IMPLEMENT_BINARY_OPERATOR(%, UByte);
173 IMPLEMENT_BINARY_OPERATOR(%, SByte);
174 IMPLEMENT_BINARY_OPERATOR(%, UShort);
175 IMPLEMENT_BINARY_OPERATOR(%, Short);
176 IMPLEMENT_BINARY_OPERATOR(%, UInt);
177 IMPLEMENT_BINARY_OPERATOR(%, Int);
178 IMPLEMENT_BINARY_OPERATOR(%, ULong);
179 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000180 case Type::FloatTyID:
181 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
182 break;
183 case Type::DoubleTyID:
184 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
185 break;
186 default:
Chris Lattner02868352003-04-22 21:22:33 +0000187 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
188 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000189 }
190 return Dest;
191}
192
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000193static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000194 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000195 GenericValue Dest;
196 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000197 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000198 IMPLEMENT_BINARY_OPERATOR(&, UByte);
199 IMPLEMENT_BINARY_OPERATOR(&, SByte);
200 IMPLEMENT_BINARY_OPERATOR(&, UShort);
201 IMPLEMENT_BINARY_OPERATOR(&, Short);
202 IMPLEMENT_BINARY_OPERATOR(&, UInt);
203 IMPLEMENT_BINARY_OPERATOR(&, Int);
204 IMPLEMENT_BINARY_OPERATOR(&, ULong);
205 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000206 default:
Chris Lattner02868352003-04-22 21:22:33 +0000207 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
208 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000209 }
210 return Dest;
211}
212
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000213static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000214 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000215 GenericValue Dest;
216 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000217 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000218 IMPLEMENT_BINARY_OPERATOR(|, UByte);
219 IMPLEMENT_BINARY_OPERATOR(|, SByte);
220 IMPLEMENT_BINARY_OPERATOR(|, UShort);
221 IMPLEMENT_BINARY_OPERATOR(|, Short);
222 IMPLEMENT_BINARY_OPERATOR(|, UInt);
223 IMPLEMENT_BINARY_OPERATOR(|, Int);
224 IMPLEMENT_BINARY_OPERATOR(|, ULong);
225 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000226 default:
Chris Lattner02868352003-04-22 21:22:33 +0000227 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
228 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000229 }
230 return Dest;
231}
232
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000233static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000234 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000235 GenericValue Dest;
236 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000237 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000238 IMPLEMENT_BINARY_OPERATOR(^, UByte);
239 IMPLEMENT_BINARY_OPERATOR(^, SByte);
240 IMPLEMENT_BINARY_OPERATOR(^, UShort);
241 IMPLEMENT_BINARY_OPERATOR(^, Short);
242 IMPLEMENT_BINARY_OPERATOR(^, UInt);
243 IMPLEMENT_BINARY_OPERATOR(^, Int);
244 IMPLEMENT_BINARY_OPERATOR(^, ULong);
245 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000246 default:
Chris Lattner02868352003-04-22 21:22:33 +0000247 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
248 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000249 }
250 return Dest;
251}
252
Chris Lattner92101ac2001-08-23 17:05:04 +0000253#define IMPLEMENT_SETCC(OP, TY) \
254 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
255
Chris Lattnerfd506f52003-04-23 19:55:35 +0000256// Handle pointers specially because they must be compared with only as much
257// width as the host has. We _do not_ want to be comparing 64 bit values when
258// running on a 32-bit target, otherwise the upper 32 bits might mess up
259// comparisons if they contain garbage.
260#define IMPLEMENT_POINTERSETCC(OP) \
261 case Type::PointerTyID: \
262 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
263 (void*)(intptr_t)Src2.PointerVal; break
264
Chris Lattner92101ac2001-08-23 17:05:04 +0000265static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000266 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000267 GenericValue Dest;
268 switch (Ty->getPrimitiveID()) {
269 IMPLEMENT_SETCC(==, UByte);
270 IMPLEMENT_SETCC(==, SByte);
271 IMPLEMENT_SETCC(==, UShort);
272 IMPLEMENT_SETCC(==, Short);
273 IMPLEMENT_SETCC(==, UInt);
274 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000275 IMPLEMENT_SETCC(==, ULong);
276 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000277 IMPLEMENT_SETCC(==, Float);
278 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000279 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000280 default:
Chris Lattner02868352003-04-22 21:22:33 +0000281 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
282 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000283 }
284 return Dest;
285}
286
287static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000288 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000289 GenericValue Dest;
290 switch (Ty->getPrimitiveID()) {
291 IMPLEMENT_SETCC(!=, UByte);
292 IMPLEMENT_SETCC(!=, SByte);
293 IMPLEMENT_SETCC(!=, UShort);
294 IMPLEMENT_SETCC(!=, Short);
295 IMPLEMENT_SETCC(!=, UInt);
296 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000297 IMPLEMENT_SETCC(!=, ULong);
298 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000299 IMPLEMENT_SETCC(!=, Float);
300 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000301 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000302
Chris Lattner92101ac2001-08-23 17:05:04 +0000303 default:
Chris Lattner02868352003-04-22 21:22:33 +0000304 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
305 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000306 }
307 return Dest;
308}
309
310static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000311 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000312 GenericValue Dest;
313 switch (Ty->getPrimitiveID()) {
314 IMPLEMENT_SETCC(<=, UByte);
315 IMPLEMENT_SETCC(<=, SByte);
316 IMPLEMENT_SETCC(<=, UShort);
317 IMPLEMENT_SETCC(<=, Short);
318 IMPLEMENT_SETCC(<=, UInt);
319 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000320 IMPLEMENT_SETCC(<=, ULong);
321 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000322 IMPLEMENT_SETCC(<=, Float);
323 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000324 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000325 default:
Chris Lattner02868352003-04-22 21:22:33 +0000326 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
327 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000328 }
329 return Dest;
330}
331
332static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000333 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000334 GenericValue Dest;
335 switch (Ty->getPrimitiveID()) {
336 IMPLEMENT_SETCC(>=, UByte);
337 IMPLEMENT_SETCC(>=, SByte);
338 IMPLEMENT_SETCC(>=, UShort);
339 IMPLEMENT_SETCC(>=, Short);
340 IMPLEMENT_SETCC(>=, UInt);
341 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000342 IMPLEMENT_SETCC(>=, ULong);
343 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000344 IMPLEMENT_SETCC(>=, Float);
345 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000346 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000347 default:
Chris Lattner02868352003-04-22 21:22:33 +0000348 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
349 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000350 }
351 return Dest;
352}
353
354static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000355 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000356 GenericValue Dest;
357 switch (Ty->getPrimitiveID()) {
358 IMPLEMENT_SETCC(<, UByte);
359 IMPLEMENT_SETCC(<, SByte);
360 IMPLEMENT_SETCC(<, UShort);
361 IMPLEMENT_SETCC(<, Short);
362 IMPLEMENT_SETCC(<, UInt);
363 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000364 IMPLEMENT_SETCC(<, ULong);
365 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000366 IMPLEMENT_SETCC(<, Float);
367 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000368 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000369 default:
Chris Lattner02868352003-04-22 21:22:33 +0000370 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
371 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000372 }
373 return Dest;
374}
375
376static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000377 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000378 GenericValue Dest;
379 switch (Ty->getPrimitiveID()) {
380 IMPLEMENT_SETCC(>, UByte);
381 IMPLEMENT_SETCC(>, SByte);
382 IMPLEMENT_SETCC(>, UShort);
383 IMPLEMENT_SETCC(>, Short);
384 IMPLEMENT_SETCC(>, UInt);
385 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000386 IMPLEMENT_SETCC(>, ULong);
387 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000388 IMPLEMENT_SETCC(>, Float);
389 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000390 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000391 default:
Chris Lattner02868352003-04-22 21:22:33 +0000392 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
393 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000394 }
395 return Dest;
396}
397
Chris Lattnerd7916e92003-05-10 21:22:39 +0000398void Interpreter::visitBinaryOperator(BinaryOperator &I) {
399 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000400 const Type *Ty = I.getOperand(0)->getType();
401 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
402 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000403 GenericValue R; // Result
404
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000405 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000406 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
407 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
408 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
409 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
410 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
411 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
412 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
413 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
414 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
415 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
416 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
417 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
418 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
419 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000420 default:
Chris Lattner02868352003-04-22 21:22:33 +0000421 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
422 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 }
424
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000425 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000426}
427
Chris Lattner92101ac2001-08-23 17:05:04 +0000428//===----------------------------------------------------------------------===//
429// Terminator Instruction Implementations
430//===----------------------------------------------------------------------===//
431
Chris Lattnere43db882001-10-27 04:15:57 +0000432void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnere43db882001-10-27 04:15:57 +0000433 ExitCode = GV.SByteVal;
434 ECStack.clear();
435}
436
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000437/// Pop the last stack frame off of ECStack and then copy the result
438/// back into the result variable if we are not returning void. The
439/// result variable may be the ExitCode, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000440/// CallInst if there was a previous stack frame. This method may
441/// invalidate any ECStack iterators you have. This method also takes
442/// care of switching to the normal destination BB, if we are returning
443/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000444///
445void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
446 GenericValue Result) {
447 // Pop the current stack frame.
448 ECStack.pop_back();
449
450 if (ECStack.empty()) { // Finished main. Put result into exit code...
451 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
452 ExitCode = Result.IntVal; // Capture the exit code of the program
453 } else {
454 ExitCode = 0;
455 }
456 } else {
457 // If we have a previous stack frame, and we have a previous call,
458 // fill in the return value...
459 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000460 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000461 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000462 SetValue(I, Result, CallingSF);
463 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
464 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000465 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000466 }
467 }
468}
469
Chris Lattnerd7916e92003-05-10 21:22:39 +0000470void Interpreter::visitReturnInst(ReturnInst &I) {
471 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000472 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000473 GenericValue Result;
474
475 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000476 if (I.getNumOperands()) {
477 RetTy = I.getReturnValue()->getType();
478 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000479 }
480
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000481 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000482}
483
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000484void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000485 // Unwind stack
486 Instruction *Inst;
487 do {
488 ECStack.pop_back ();
489 if (ECStack.empty ())
490 abort ();
491 Inst = ECStack.back ().Caller.getInstruction ();
492 } while (!(Inst && isa<InvokeInst> (Inst)));
493
494 // Return from invoke
495 ExecutionContext &InvokingSF = ECStack.back ();
496 InvokingSF.Caller = CallSite ();
497
498 // Go to exceptional destination BB of invoke instruction
499 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
500 InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000501}
502
Chris Lattnerd7916e92003-05-10 21:22:39 +0000503void Interpreter::visitBranchInst(BranchInst &I) {
504 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000505 BasicBlock *Dest;
506
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000507 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
508 if (!I.isUnconditional()) {
509 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000510 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000511 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000512 }
Chris Lattner77113b62003-05-10 20:21:16 +0000513 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000514}
515
Chris Lattnerd7916e92003-05-10 21:22:39 +0000516void Interpreter::visitSwitchInst(SwitchInst &I) {
517 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000518 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
519 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000520
521 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000522 BasicBlock *Dest = 0;
523 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000524 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000525 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000526 Dest = cast<BasicBlock>(I.getOperand(i+1));
527 break;
528 }
Chris Lattner09e93922003-04-22 20:34:47 +0000529
530 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000531 SwitchToNewBasicBlock(Dest, SF);
532}
533
534// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
535// This function handles the actual updating of block and instruction iterators
536// as well as execution of all of the PHI nodes in the destination block.
537//
538// This method does this because all of the PHI nodes must be executed
539// atomically, reading their inputs before any of the results are updated. Not
540// doing this can cause problems if the PHI nodes depend on other PHI nodes for
541// their inputs. If the input PHI node is updated before it is read, incorrect
542// results can happen. Thus we use a two phase approach.
543//
544void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
545 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
546 SF.CurBB = Dest; // Update CurBB to branch destination
547 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
548
549 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
550
551 // Loop over all of the PHI nodes in the current block, reading their inputs.
552 std::vector<GenericValue> ResultValues;
553
554 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
555 // Search for the value corresponding to this previous bb...
556 int i = PN->getBasicBlockIndex(PrevBB);
557 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
558 Value *IncomingValue = PN->getIncomingValue(i);
559
560 // Save the incoming value for this PHI node...
561 ResultValues.push_back(getOperandValue(IncomingValue, SF));
562 }
563
564 // Now loop over all of the PHI nodes setting their values...
565 SF.CurInst = SF.CurBB->begin();
566 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
567 ++SF.CurInst, ++i)
568 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000569}
570
Chris Lattner92101ac2001-08-23 17:05:04 +0000571//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000572// Memory Instruction Implementations
573//===----------------------------------------------------------------------===//
574
Chris Lattnerd7916e92003-05-10 21:22:39 +0000575void Interpreter::visitAllocationInst(AllocationInst &I) {
576 ExecutionContext &SF = ECStack.back();
577
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000578 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000579
Chris Lattnercc82cc12002-04-28 21:57:33 +0000580 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000581 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000582
583 // Allocate enough memory to hold the type...
Brian Gaeke2fa82122003-11-05 01:02:14 +0000584 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000585
Chris Lattnerfe11a972002-12-23 23:59:41 +0000586 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000587 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000588 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000589
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000590 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000591 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000592}
593
Chris Lattnerd7916e92003-05-10 21:22:39 +0000594void Interpreter::visitFreeInst(FreeInst &I) {
595 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000596 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
597 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000598 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000599 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000600}
601
Chris Lattnera34c5682002-08-27 22:33:45 +0000602// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000603//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000604GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
605 User::op_iterator E,
606 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000607 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000608 "Cannot getElementOffset of a nonpointer type!");
609
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000610 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000611 const Type *Ty = Ptr->getType();
612
613 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000614 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
615 const StructLayout *SLO = TD.getStructLayout(STy);
616
Misha Brukmand5d96b92003-10-10 17:42:19 +0000617 // Indices must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000618 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000619 assert(CPU->getType() == Type::UByteTy);
620 unsigned Index = CPU->getValue();
621
Chris Lattner782b9392001-11-26 18:18:18 +0000622 Total += SLO->MemberOffsets[Index];
623 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000624 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattner006a4a52003-02-25 21:14:59 +0000625 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000626 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000627 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000628 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000629 unsigned Size = TD.getTypeSize(Ty);
630 Total += Size*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000631 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000632 }
633
Chris Lattnera34c5682002-08-27 22:33:45 +0000634 GenericValue Result;
635 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
636 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000637}
638
Chris Lattnerd7916e92003-05-10 21:22:39 +0000639void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
640 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000641 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000642 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000643}
644
Chris Lattnerd7916e92003-05-10 21:22:39 +0000645void Interpreter::visitLoadInst(LoadInst &I) {
646 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000647 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000648 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000649 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000650 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000651}
652
Chris Lattnerd7916e92003-05-10 21:22:39 +0000653void Interpreter::visitStoreInst(StoreInst &I) {
654 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000655 GenericValue Val = getOperandValue(I.getOperand(0), SF);
656 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000657 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000658 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000659}
660
Chris Lattner86660982001-08-27 05:16:50 +0000661//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000662// Miscellaneous Instruction Implementations
663//===----------------------------------------------------------------------===//
664
Brian Gaekefea483d2003-11-07 20:04:22 +0000665void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000666 ExecutionContext &SF = ECStack.back();
Brian Gaekefea483d2003-11-07 20:04:22 +0000667 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000668 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000669 const unsigned NumArgs = SF.Caller.arg_size();
670 ArgVals.reserve(NumArgs);
671 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
672 e = SF.Caller.arg_end(); i != e; ++i) {
673 Value *V = *i;
674 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000675 // Promote all integral types whose size is < sizeof(int) into ints. We do
676 // this by zero or sign extending the value as appropriate according to the
677 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000678 const Type *Ty = V->getType();
679 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000680 if (Ty == Type::ShortTy)
681 ArgVals.back().IntVal = ArgVals.back().ShortVal;
682 else if (Ty == Type::UShortTy)
683 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
684 else if (Ty == Type::SByteTy)
685 ArgVals.back().IntVal = ArgVals.back().SByteVal;
686 else if (Ty == Type::UByteTy)
687 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
688 else if (Ty == Type::BoolTy)
689 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
690 else
691 assert(0 && "Unknown type!");
692 }
693 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000694
Chris Lattner070cf5e2001-11-07 20:12:30 +0000695 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000696 // and treat it as a function pointer.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000697 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000698 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000699}
700
Chris Lattner86660982001-08-27 05:16:50 +0000701#define IMPLEMENT_SHIFT(OP, TY) \
702 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
703
Chris Lattnerd7916e92003-05-10 21:22:39 +0000704void Interpreter::visitShl(ShiftInst &I) {
705 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000706 const Type *Ty = I.getOperand(0)->getType();
707 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
708 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000709 GenericValue Dest;
710
711 switch (Ty->getPrimitiveID()) {
712 IMPLEMENT_SHIFT(<<, UByte);
713 IMPLEMENT_SHIFT(<<, SByte);
714 IMPLEMENT_SHIFT(<<, UShort);
715 IMPLEMENT_SHIFT(<<, Short);
716 IMPLEMENT_SHIFT(<<, UInt);
717 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000718 IMPLEMENT_SHIFT(<<, ULong);
719 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000720 default:
Chris Lattner02868352003-04-22 21:22:33 +0000721 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000722 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000723 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000724}
725
Chris Lattnerd7916e92003-05-10 21:22:39 +0000726void Interpreter::visitShr(ShiftInst &I) {
727 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000728 const Type *Ty = I.getOperand(0)->getType();
729 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
730 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000731 GenericValue Dest;
732
733 switch (Ty->getPrimitiveID()) {
734 IMPLEMENT_SHIFT(>>, UByte);
735 IMPLEMENT_SHIFT(>>, SByte);
736 IMPLEMENT_SHIFT(>>, UShort);
737 IMPLEMENT_SHIFT(>>, Short);
738 IMPLEMENT_SHIFT(>>, UInt);
739 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000740 IMPLEMENT_SHIFT(>>, ULong);
741 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000742 default:
Chris Lattner02868352003-04-22 21:22:33 +0000743 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
744 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000745 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000746 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000747}
748
749#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000750 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000751
752#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
753 case Type::DESTTY##TyID: \
754 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000755 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000756 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
757 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
758 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
759 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
760 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000761 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
762 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000763 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
764 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000765
766#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
767 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
768 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
769
770#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000771 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
772 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000773 } \
774 break
775
776#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
777 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
778 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000779 IMPLEMENT_CAST_CASE_END()
780
Brian Gaeke29794cb2003-09-05 18:55:03 +0000781GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
782 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000783 const Type *SrcTy = SrcVal->getType();
784 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000785
786 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000787 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
788 IMPLEMENT_CAST_CASE(SByte , ( signed char));
789 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000790 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000791 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
792 IMPLEMENT_CAST_CASE(Int , ( signed int ));
793 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
794 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000795 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000796 IMPLEMENT_CAST_CASE(Float , (float));
797 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000798 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000799 default:
Chris Lattner02868352003-04-22 21:22:33 +0000800 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000801 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000802 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000803
804 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000805}
Chris Lattner92101ac2001-08-23 17:05:04 +0000806
Chris Lattnerd7916e92003-05-10 21:22:39 +0000807void Interpreter::visitCastInst(CastInst &I) {
808 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000809 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
810}
Chris Lattner92101ac2001-08-23 17:05:04 +0000811
Chris Lattner4c665492003-10-18 05:55:25 +0000812void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000813 ExecutionContext &SF = ECStack.back();
814
Chris Lattner4c665492003-10-18 05:55:25 +0000815 // Get the incoming valist element. LLI treats the valist as an integer.
816 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
817
818 // Move to the next operand.
Chris Lattner374344c2003-05-08 16:52:43 +0000819 unsigned Argument = VAList.IntVal++;
Chris Lattner374344c2003-05-08 16:52:43 +0000820 assert(Argument < SF.VarArgs.size() &&
821 "Accessing past the last vararg argument!");
Chris Lattner4c665492003-10-18 05:55:25 +0000822 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000823}
Chris Lattner92101ac2001-08-23 17:05:04 +0000824
Brian Gaekec1a2be12003-11-07 21:20:47 +0000825#define IMPLEMENT_VAARG(TY) \
826 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
827
828void Interpreter::visitVAArgInst(VAArgInst &I) {
829 ExecutionContext &SF = ECStack.back();
830
831 // Get the incoming valist element. LLI treats the valist as an integer.
832 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
833 unsigned Argument = VAList.IntVal;
834 assert(Argument < SF.VarArgs.size() &&
835 "Accessing past the last vararg argument!");
836 GenericValue Dest, Src = SF.VarArgs[Argument];
837 const Type *Ty = I.getType();
838 switch (Ty->getPrimitiveID()) {
839 IMPLEMENT_VAARG(UByte);
840 IMPLEMENT_VAARG(SByte);
841 IMPLEMENT_VAARG(UShort);
842 IMPLEMENT_VAARG(Short);
843 IMPLEMENT_VAARG(UInt);
844 IMPLEMENT_VAARG(Int);
845 IMPLEMENT_VAARG(ULong);
846 IMPLEMENT_VAARG(Long);
847 IMPLEMENT_VAARG(Pointer);
848 IMPLEMENT_VAARG(Float);
849 IMPLEMENT_VAARG(Double);
850 IMPLEMENT_VAARG(Bool);
851 default:
852 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
853 abort();
854 }
855
856 // Set the Value of this Instruction.
857 SetValue(&I, Dest, SF);
858}
859
Chris Lattner92101ac2001-08-23 17:05:04 +0000860//===----------------------------------------------------------------------===//
861// Dispatch and Execution Code
862//===----------------------------------------------------------------------===//
863
Chris Lattner92101ac2001-08-23 17:05:04 +0000864//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000865// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000866//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000867void Interpreter::callFunction(Function *F,
868 const std::vector<GenericValue> &ArgVals) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000869 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
870 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
Chris Lattner365a76e2001-09-10 04:49:44 +0000871 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +0000872 // Make a new stack frame... and fill it in.
873 ECStack.push_back(ExecutionContext());
874 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000875 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000876
877 // Special handling for external functions.
878 if (F->isExternal()) {
879 GenericValue Result = callExternalFunction (F, ArgVals);
880 // Simulate a 'ret' instruction of the appropriate type.
881 popStackAndReturnValueToCaller (F->getReturnType (), Result);
882 return;
883 }
884
885 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +0000886 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000887 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000888
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000889 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000890 assert((ArgVals.size() == F->asize() ||
891 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000892 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000893
894 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000895 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000896 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000897 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000898
899 // Handle varargs arguments...
900 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000901}
902
Chris Lattner92101ac2001-08-23 17:05:04 +0000903void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000904 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000905 // Interpret a single instruction & increment the "PC".
906 ExecutionContext &SF = ECStack.back(); // Current stack frame
907 Instruction &I = *SF.CurInst++; // Increment before execute
908
909 // Track the number of dynamic instructions executed.
910 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +0000911
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000912 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +0000913 }
914}
Brian Gaeked0fde302003-11-11 22:41:34 +0000915
916} // End llvm namespace