blob: 3d1db56637100f2e55e811b933ea18a5b0ed89d5 [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 Lattner4af6de82003-11-25 20:44:56 +000018#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerbbdabce2002-12-08 05:51:08 +000019#include "Support/Statistic.h"
Brian Gaeke235b2002003-10-10 17:03:22 +000020#include <cmath> // For fmod
Chris Lattner4af6de82003-11-25 20:44:56 +000021using namespace 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
Chris Lattner4af6de82003-11-25 20:44:56 +000027namespace llvm {
28 Interpreter *TheEE = 0;
29}
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner2e42d3a2001-10-15 05:51:48 +000031//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000032// Value Manipulation code
33//===----------------------------------------------------------------------===//
34
Chris Lattnera34c5682002-08-27 22:33:45 +000035// Operations used by constant expr implementations...
36static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
37 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000038static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000039 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000040
Brian Gaeke29794cb2003-09-05 18:55:03 +000041GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000042 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
43 switch (CE->getOpcode()) {
44 case Instruction::Cast:
45 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
46 case Instruction::GetElementPtr:
Chris Lattner4af6de82003-11-25 20:44:56 +000047 return TheEE->executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
48 gep_type_end(CE), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000049 case Instruction::Add:
50 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
51 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000052 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000053 default:
Chris Lattner02868352003-04-22 21:22:33 +000054 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000055 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000056 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000057 }
58 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000059 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000060 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000061 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +000062 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000063 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +000064 }
65}
66
Chris Lattner39bb5b42001-10-15 13:25:40 +000067static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000068 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000069}
70
Chris Lattner39bb5b42001-10-15 13:25:40 +000071//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +000072// Annotation Wrangling code
73//===----------------------------------------------------------------------===//
74
75void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000076 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000077}
78
Chris Lattner2adcd832002-05-03 19:52:30 +000079//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000080// Binary Instruction Implementations
81//===----------------------------------------------------------------------===//
82
83#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
84 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
85
86static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000087 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +000088 GenericValue Dest;
89 switch (Ty->getPrimitiveID()) {
90 IMPLEMENT_BINARY_OPERATOR(+, UByte);
91 IMPLEMENT_BINARY_OPERATOR(+, SByte);
92 IMPLEMENT_BINARY_OPERATOR(+, UShort);
93 IMPLEMENT_BINARY_OPERATOR(+, Short);
94 IMPLEMENT_BINARY_OPERATOR(+, UInt);
95 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +000096 IMPLEMENT_BINARY_OPERATOR(+, ULong);
97 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +000098 IMPLEMENT_BINARY_OPERATOR(+, Float);
99 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000100 default:
Chris Lattner02868352003-04-22 21:22:33 +0000101 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
102 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000103 }
104 return Dest;
105}
106
107static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000108 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000109 GenericValue Dest;
110 switch (Ty->getPrimitiveID()) {
111 IMPLEMENT_BINARY_OPERATOR(-, UByte);
112 IMPLEMENT_BINARY_OPERATOR(-, SByte);
113 IMPLEMENT_BINARY_OPERATOR(-, UShort);
114 IMPLEMENT_BINARY_OPERATOR(-, Short);
115 IMPLEMENT_BINARY_OPERATOR(-, UInt);
116 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000117 IMPLEMENT_BINARY_OPERATOR(-, ULong);
118 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000119 IMPLEMENT_BINARY_OPERATOR(-, Float);
120 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000121 default:
Chris Lattner02868352003-04-22 21:22:33 +0000122 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
123 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000124 }
125 return Dest;
126}
127
Chris Lattnerc2593162001-10-27 08:28:11 +0000128static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000129 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000130 GenericValue Dest;
131 switch (Ty->getPrimitiveID()) {
132 IMPLEMENT_BINARY_OPERATOR(*, UByte);
133 IMPLEMENT_BINARY_OPERATOR(*, SByte);
134 IMPLEMENT_BINARY_OPERATOR(*, UShort);
135 IMPLEMENT_BINARY_OPERATOR(*, Short);
136 IMPLEMENT_BINARY_OPERATOR(*, UInt);
137 IMPLEMENT_BINARY_OPERATOR(*, Int);
138 IMPLEMENT_BINARY_OPERATOR(*, ULong);
139 IMPLEMENT_BINARY_OPERATOR(*, Long);
140 IMPLEMENT_BINARY_OPERATOR(*, Float);
141 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000142 default:
Chris Lattner02868352003-04-22 21:22:33 +0000143 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
144 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000145 }
146 return Dest;
147}
148
149static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000150 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000151 GenericValue Dest;
152 switch (Ty->getPrimitiveID()) {
153 IMPLEMENT_BINARY_OPERATOR(/, UByte);
154 IMPLEMENT_BINARY_OPERATOR(/, SByte);
155 IMPLEMENT_BINARY_OPERATOR(/, UShort);
156 IMPLEMENT_BINARY_OPERATOR(/, Short);
157 IMPLEMENT_BINARY_OPERATOR(/, UInt);
158 IMPLEMENT_BINARY_OPERATOR(/, Int);
159 IMPLEMENT_BINARY_OPERATOR(/, ULong);
160 IMPLEMENT_BINARY_OPERATOR(/, Long);
161 IMPLEMENT_BINARY_OPERATOR(/, Float);
162 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000163 default:
Chris Lattner02868352003-04-22 21:22:33 +0000164 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
165 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000166 }
167 return Dest;
168}
169
170static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000171 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000172 GenericValue Dest;
173 switch (Ty->getPrimitiveID()) {
174 IMPLEMENT_BINARY_OPERATOR(%, UByte);
175 IMPLEMENT_BINARY_OPERATOR(%, SByte);
176 IMPLEMENT_BINARY_OPERATOR(%, UShort);
177 IMPLEMENT_BINARY_OPERATOR(%, Short);
178 IMPLEMENT_BINARY_OPERATOR(%, UInt);
179 IMPLEMENT_BINARY_OPERATOR(%, Int);
180 IMPLEMENT_BINARY_OPERATOR(%, ULong);
181 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000182 case Type::FloatTyID:
183 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
184 break;
185 case Type::DoubleTyID:
186 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
187 break;
188 default:
Chris Lattner02868352003-04-22 21:22:33 +0000189 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
190 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000191 }
192 return Dest;
193}
194
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000195static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000196 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000197 GenericValue Dest;
198 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000199 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000200 IMPLEMENT_BINARY_OPERATOR(&, UByte);
201 IMPLEMENT_BINARY_OPERATOR(&, SByte);
202 IMPLEMENT_BINARY_OPERATOR(&, UShort);
203 IMPLEMENT_BINARY_OPERATOR(&, Short);
204 IMPLEMENT_BINARY_OPERATOR(&, UInt);
205 IMPLEMENT_BINARY_OPERATOR(&, Int);
206 IMPLEMENT_BINARY_OPERATOR(&, ULong);
207 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000208 default:
Chris Lattner02868352003-04-22 21:22:33 +0000209 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
210 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000211 }
212 return Dest;
213}
214
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000215static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000216 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000217 GenericValue Dest;
218 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000219 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000220 IMPLEMENT_BINARY_OPERATOR(|, UByte);
221 IMPLEMENT_BINARY_OPERATOR(|, SByte);
222 IMPLEMENT_BINARY_OPERATOR(|, UShort);
223 IMPLEMENT_BINARY_OPERATOR(|, Short);
224 IMPLEMENT_BINARY_OPERATOR(|, UInt);
225 IMPLEMENT_BINARY_OPERATOR(|, Int);
226 IMPLEMENT_BINARY_OPERATOR(|, ULong);
227 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000228 default:
Chris Lattner02868352003-04-22 21:22:33 +0000229 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
230 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000231 }
232 return Dest;
233}
234
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000235static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000236 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000237 GenericValue Dest;
238 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000239 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000240 IMPLEMENT_BINARY_OPERATOR(^, UByte);
241 IMPLEMENT_BINARY_OPERATOR(^, SByte);
242 IMPLEMENT_BINARY_OPERATOR(^, UShort);
243 IMPLEMENT_BINARY_OPERATOR(^, Short);
244 IMPLEMENT_BINARY_OPERATOR(^, UInt);
245 IMPLEMENT_BINARY_OPERATOR(^, Int);
246 IMPLEMENT_BINARY_OPERATOR(^, ULong);
247 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000248 default:
Chris Lattner02868352003-04-22 21:22:33 +0000249 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
250 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000251 }
252 return Dest;
253}
254
Chris Lattner92101ac2001-08-23 17:05:04 +0000255#define IMPLEMENT_SETCC(OP, TY) \
256 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
257
Chris Lattnerfd506f52003-04-23 19:55:35 +0000258// Handle pointers specially because they must be compared with only as much
259// width as the host has. We _do not_ want to be comparing 64 bit values when
260// running on a 32-bit target, otherwise the upper 32 bits might mess up
261// comparisons if they contain garbage.
262#define IMPLEMENT_POINTERSETCC(OP) \
263 case Type::PointerTyID: \
264 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
265 (void*)(intptr_t)Src2.PointerVal; break
266
Chris Lattner92101ac2001-08-23 17:05:04 +0000267static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000268 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000269 GenericValue Dest;
270 switch (Ty->getPrimitiveID()) {
271 IMPLEMENT_SETCC(==, UByte);
272 IMPLEMENT_SETCC(==, SByte);
273 IMPLEMENT_SETCC(==, UShort);
274 IMPLEMENT_SETCC(==, Short);
275 IMPLEMENT_SETCC(==, UInt);
276 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000277 IMPLEMENT_SETCC(==, ULong);
278 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000279 IMPLEMENT_SETCC(==, Float);
280 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000281 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000282 default:
Chris Lattner02868352003-04-22 21:22:33 +0000283 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
284 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000285 }
286 return Dest;
287}
288
289static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000290 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000291 GenericValue Dest;
292 switch (Ty->getPrimitiveID()) {
293 IMPLEMENT_SETCC(!=, UByte);
294 IMPLEMENT_SETCC(!=, SByte);
295 IMPLEMENT_SETCC(!=, UShort);
296 IMPLEMENT_SETCC(!=, Short);
297 IMPLEMENT_SETCC(!=, UInt);
298 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000299 IMPLEMENT_SETCC(!=, ULong);
300 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000301 IMPLEMENT_SETCC(!=, Float);
302 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000303 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000304
Chris Lattner92101ac2001-08-23 17:05:04 +0000305 default:
Chris Lattner02868352003-04-22 21:22:33 +0000306 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
307 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000308 }
309 return Dest;
310}
311
312static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000313 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000314 GenericValue Dest;
315 switch (Ty->getPrimitiveID()) {
316 IMPLEMENT_SETCC(<=, UByte);
317 IMPLEMENT_SETCC(<=, SByte);
318 IMPLEMENT_SETCC(<=, UShort);
319 IMPLEMENT_SETCC(<=, Short);
320 IMPLEMENT_SETCC(<=, UInt);
321 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000322 IMPLEMENT_SETCC(<=, ULong);
323 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 IMPLEMENT_SETCC(<=, Float);
325 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000326 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000327 default:
Chris Lattner02868352003-04-22 21:22:33 +0000328 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
329 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000330 }
331 return Dest;
332}
333
334static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000335 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000336 GenericValue Dest;
337 switch (Ty->getPrimitiveID()) {
338 IMPLEMENT_SETCC(>=, UByte);
339 IMPLEMENT_SETCC(>=, SByte);
340 IMPLEMENT_SETCC(>=, UShort);
341 IMPLEMENT_SETCC(>=, Short);
342 IMPLEMENT_SETCC(>=, UInt);
343 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000344 IMPLEMENT_SETCC(>=, ULong);
345 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000346 IMPLEMENT_SETCC(>=, Float);
347 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000348 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000349 default:
Chris Lattner02868352003-04-22 21:22:33 +0000350 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
351 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000352 }
353 return Dest;
354}
355
356static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000357 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000358 GenericValue Dest;
359 switch (Ty->getPrimitiveID()) {
360 IMPLEMENT_SETCC(<, UByte);
361 IMPLEMENT_SETCC(<, SByte);
362 IMPLEMENT_SETCC(<, UShort);
363 IMPLEMENT_SETCC(<, Short);
364 IMPLEMENT_SETCC(<, UInt);
365 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000366 IMPLEMENT_SETCC(<, ULong);
367 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 IMPLEMENT_SETCC(<, Float);
369 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000370 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000371 default:
Chris Lattner02868352003-04-22 21:22:33 +0000372 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
373 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000374 }
375 return Dest;
376}
377
378static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000379 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000380 GenericValue Dest;
381 switch (Ty->getPrimitiveID()) {
382 IMPLEMENT_SETCC(>, UByte);
383 IMPLEMENT_SETCC(>, SByte);
384 IMPLEMENT_SETCC(>, UShort);
385 IMPLEMENT_SETCC(>, Short);
386 IMPLEMENT_SETCC(>, UInt);
387 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000388 IMPLEMENT_SETCC(>, ULong);
389 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000390 IMPLEMENT_SETCC(>, Float);
391 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000392 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000393 default:
Chris Lattner02868352003-04-22 21:22:33 +0000394 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
395 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000396 }
397 return Dest;
398}
399
Chris Lattnerd7916e92003-05-10 21:22:39 +0000400void Interpreter::visitBinaryOperator(BinaryOperator &I) {
401 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000402 const Type *Ty = I.getOperand(0)->getType();
403 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
404 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000405 GenericValue R; // Result
406
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000407 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000408 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
409 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
410 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
411 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
412 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
413 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
414 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
415 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
416 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
417 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
418 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
419 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
420 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
421 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000422 default:
Chris Lattner02868352003-04-22 21:22:33 +0000423 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
424 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000425 }
426
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000427 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000428}
429
Chris Lattner92101ac2001-08-23 17:05:04 +0000430//===----------------------------------------------------------------------===//
431// Terminator Instruction Implementations
432//===----------------------------------------------------------------------===//
433
Chris Lattnere43db882001-10-27 04:15:57 +0000434void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnere43db882001-10-27 04:15:57 +0000435 ExitCode = GV.SByteVal;
436 ECStack.clear();
437}
438
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000439/// Pop the last stack frame off of ECStack and then copy the result
440/// back into the result variable if we are not returning void. The
441/// result variable may be the ExitCode, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000442/// CallInst if there was a previous stack frame. This method may
443/// invalidate any ECStack iterators you have. This method also takes
444/// care of switching to the normal destination BB, if we are returning
445/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000446///
447void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
448 GenericValue Result) {
449 // Pop the current stack frame.
450 ECStack.pop_back();
451
452 if (ECStack.empty()) { // Finished main. Put result into exit code...
453 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
454 ExitCode = Result.IntVal; // Capture the exit code of the program
455 } else {
456 ExitCode = 0;
457 }
458 } else {
459 // If we have a previous stack frame, and we have a previous call,
460 // fill in the return value...
461 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000462 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000463 if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000464 SetValue(I, Result, CallingSF);
465 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
466 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000467 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000468 }
469 }
470}
471
Chris Lattnerd7916e92003-05-10 21:22:39 +0000472void Interpreter::visitReturnInst(ReturnInst &I) {
473 ExecutionContext &SF = ECStack.back();
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000474 const Type *RetTy = Type::VoidTy;
Chris Lattner92101ac2001-08-23 17:05:04 +0000475 GenericValue Result;
476
477 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000478 if (I.getNumOperands()) {
479 RetTy = I.getReturnValue()->getType();
480 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000481 }
482
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000483 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000484}
485
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000486void Interpreter::visitUnwindInst(UnwindInst &I) {
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000487 // Unwind stack
488 Instruction *Inst;
489 do {
490 ECStack.pop_back ();
491 if (ECStack.empty ())
492 abort ();
493 Inst = ECStack.back ().Caller.getInstruction ();
494 } while (!(Inst && isa<InvokeInst> (Inst)));
495
496 // Return from invoke
497 ExecutionContext &InvokingSF = ECStack.back ();
498 InvokingSF.Caller = CallSite ();
499
500 // Go to exceptional destination BB of invoke instruction
501 SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (),
502 InvokingSF);
Brian Gaeke9bf06b12003-11-07 20:07:06 +0000503}
504
Chris Lattnerd7916e92003-05-10 21:22:39 +0000505void Interpreter::visitBranchInst(BranchInst &I) {
506 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000507 BasicBlock *Dest;
508
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000509 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
510 if (!I.isUnconditional()) {
511 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000512 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000513 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000514 }
Chris Lattner77113b62003-05-10 20:21:16 +0000515 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000516}
517
Chris Lattnerd7916e92003-05-10 21:22:39 +0000518void Interpreter::visitSwitchInst(SwitchInst &I) {
519 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000520 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
521 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000522
523 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000524 BasicBlock *Dest = 0;
525 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000526 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000527 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000528 Dest = cast<BasicBlock>(I.getOperand(i+1));
529 break;
530 }
Chris Lattner09e93922003-04-22 20:34:47 +0000531
532 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000533 SwitchToNewBasicBlock(Dest, SF);
534}
535
536// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
537// This function handles the actual updating of block and instruction iterators
538// as well as execution of all of the PHI nodes in the destination block.
539//
540// This method does this because all of the PHI nodes must be executed
541// atomically, reading their inputs before any of the results are updated. Not
542// doing this can cause problems if the PHI nodes depend on other PHI nodes for
543// their inputs. If the input PHI node is updated before it is read, incorrect
544// results can happen. Thus we use a two phase approach.
545//
546void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
547 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
548 SF.CurBB = Dest; // Update CurBB to branch destination
549 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
550
551 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
552
553 // Loop over all of the PHI nodes in the current block, reading their inputs.
554 std::vector<GenericValue> ResultValues;
555
556 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
557 // Search for the value corresponding to this previous bb...
558 int i = PN->getBasicBlockIndex(PrevBB);
559 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
560 Value *IncomingValue = PN->getIncomingValue(i);
561
562 // Save the incoming value for this PHI node...
563 ResultValues.push_back(getOperandValue(IncomingValue, SF));
564 }
565
566 // Now loop over all of the PHI nodes setting their values...
567 SF.CurInst = SF.CurBB->begin();
568 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
569 ++SF.CurInst, ++i)
570 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000571}
572
Chris Lattner92101ac2001-08-23 17:05:04 +0000573//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000574// Memory Instruction Implementations
575//===----------------------------------------------------------------------===//
576
Chris Lattnerd7916e92003-05-10 21:22:39 +0000577void Interpreter::visitAllocationInst(AllocationInst &I) {
578 ExecutionContext &SF = ECStack.back();
579
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000580 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000581
Chris Lattnercc82cc12002-04-28 21:57:33 +0000582 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000583 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000584
585 // Allocate enough memory to hold the type...
Brian Gaeke2fa82122003-11-05 01:02:14 +0000586 void *Memory = malloc(NumElements * TD.getTypeSize(Ty));
Chris Lattner9bffa732002-02-19 18:50:09 +0000587
Chris Lattnerfe11a972002-12-23 23:59:41 +0000588 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000589 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000590 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000591
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000592 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000593 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000594}
595
Chris Lattnerd7916e92003-05-10 21:22:39 +0000596void Interpreter::visitFreeInst(FreeInst &I) {
597 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000598 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
599 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000600 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000601 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000602}
603
Chris Lattnera34c5682002-08-27 22:33:45 +0000604// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000605//
Chris Lattner4af6de82003-11-25 20:44:56 +0000606GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
607 gep_type_iterator E,
Chris Lattnerfe11a972002-12-23 23:59:41 +0000608 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000609 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000610 "Cannot getElementOffset of a nonpointer type!");
611
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000612 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000613
614 for (; I != E; ++I) {
Chris Lattner4af6de82003-11-25 20:44:56 +0000615 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +0000616 const StructLayout *SLO = TD.getStructLayout(STy);
617
Misha Brukmand5d96b92003-10-10 17:42:19 +0000618 // Indices must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000619 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000620 unsigned Index = CPU->getValue();
621
Chris Lattner782b9392001-11-26 18:18:18 +0000622 Total += SLO->MemberOffsets[Index];
Chris Lattner4af6de82003-11-25 20:44:56 +0000623 } else {
624 const SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +0000625 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +0000626 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
627
628 uint64_t Idx;
629 switch (I.getOperand()->getType()->getPrimitiveID()) {
630 default: assert(0 && "Illegal getelementptr index for sequential type!");
631 case Type::SByteTyID: Idx = IdxGV.SByteVal; break;
632 case Type::ShortTyID: Idx = IdxGV.ShortVal; break;
633 case Type::IntTyID: Idx = IdxGV.IntVal; break;
634 case Type::LongTyID: Idx = IdxGV.LongVal; break;
635 case Type::UByteTyID: Idx = IdxGV.UByteVal; break;
636 case Type::UShortTyID: Idx = IdxGV.UShortVal; break;
637 case Type::UIntTyID: Idx = IdxGV.UIntVal; break;
638 case Type::ULongTyID: Idx = IdxGV.ULongVal; break;
639 }
640 Total += TD.getTypeSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000641 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000642 }
643
Chris Lattnera34c5682002-08-27 22:33:45 +0000644 GenericValue Result;
645 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
646 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000647}
648
Chris Lattnerd7916e92003-05-10 21:22:39 +0000649void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
650 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000651 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +0000652 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000653}
654
Chris Lattnerd7916e92003-05-10 21:22:39 +0000655void Interpreter::visitLoadInst(LoadInst &I) {
656 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000657 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000658 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000659 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000660 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000661}
662
Chris Lattnerd7916e92003-05-10 21:22:39 +0000663void Interpreter::visitStoreInst(StoreInst &I) {
664 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000665 GenericValue Val = getOperandValue(I.getOperand(0), SF);
666 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000667 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000668 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000669}
670
Chris Lattner86660982001-08-27 05:16:50 +0000671//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000672// Miscellaneous Instruction Implementations
673//===----------------------------------------------------------------------===//
674
Brian Gaekefea483d2003-11-07 20:04:22 +0000675void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000676 ExecutionContext &SF = ECStack.back();
Brian Gaekefea483d2003-11-07 20:04:22 +0000677 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +0000678 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +0000679 const unsigned NumArgs = SF.Caller.arg_size();
680 ArgVals.reserve(NumArgs);
681 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
682 e = SF.Caller.arg_end(); i != e; ++i) {
683 Value *V = *i;
684 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +0000685 // Promote all integral types whose size is < sizeof(int) into ints. We do
686 // this by zero or sign extending the value as appropriate according to the
687 // source type.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000688 const Type *Ty = V->getType();
689 if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) {
Chris Lattner93780132003-01-13 00:58:52 +0000690 if (Ty == Type::ShortTy)
691 ArgVals.back().IntVal = ArgVals.back().ShortVal;
692 else if (Ty == Type::UShortTy)
693 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
694 else if (Ty == Type::SByteTy)
695 ArgVals.back().IntVal = ArgVals.back().SByteVal;
696 else if (Ty == Type::UByteTy)
697 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
698 else if (Ty == Type::BoolTy)
699 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
700 else
701 assert(0 && "Unknown type!");
702 }
703 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000704
Chris Lattner070cf5e2001-11-07 20:12:30 +0000705 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000706 // and treat it as a function pointer.
Brian Gaekeae2495a2003-11-07 19:59:08 +0000707 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000708 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000709}
710
Chris Lattner86660982001-08-27 05:16:50 +0000711#define IMPLEMENT_SHIFT(OP, TY) \
712 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
713
Chris Lattnerd7916e92003-05-10 21:22:39 +0000714void Interpreter::visitShl(ShiftInst &I) {
715 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000716 const Type *Ty = I.getOperand(0)->getType();
717 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
718 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000719 GenericValue Dest;
720
721 switch (Ty->getPrimitiveID()) {
722 IMPLEMENT_SHIFT(<<, UByte);
723 IMPLEMENT_SHIFT(<<, SByte);
724 IMPLEMENT_SHIFT(<<, UShort);
725 IMPLEMENT_SHIFT(<<, Short);
726 IMPLEMENT_SHIFT(<<, UInt);
727 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000728 IMPLEMENT_SHIFT(<<, ULong);
729 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000730 default:
Chris Lattner02868352003-04-22 21:22:33 +0000731 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000732 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000733 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000734}
735
Chris Lattnerd7916e92003-05-10 21:22:39 +0000736void Interpreter::visitShr(ShiftInst &I) {
737 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000738 const Type *Ty = I.getOperand(0)->getType();
739 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
740 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000741 GenericValue Dest;
742
743 switch (Ty->getPrimitiveID()) {
744 IMPLEMENT_SHIFT(>>, UByte);
745 IMPLEMENT_SHIFT(>>, SByte);
746 IMPLEMENT_SHIFT(>>, UShort);
747 IMPLEMENT_SHIFT(>>, Short);
748 IMPLEMENT_SHIFT(>>, UInt);
749 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000750 IMPLEMENT_SHIFT(>>, ULong);
751 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000752 default:
Chris Lattner02868352003-04-22 21:22:33 +0000753 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
754 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000755 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000756 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000757}
758
759#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000760 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000761
762#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
763 case Type::DESTTY##TyID: \
764 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000765 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000766 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
767 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
768 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
769 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
770 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000771 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
772 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000773 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
774 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000775
776#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
777 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
778 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
779
780#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000781 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
782 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000783 } \
784 break
785
786#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
787 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
788 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000789 IMPLEMENT_CAST_CASE_END()
790
Brian Gaeke29794cb2003-09-05 18:55:03 +0000791GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
792 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000793 const Type *SrcTy = SrcVal->getType();
794 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000795
796 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000797 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
798 IMPLEMENT_CAST_CASE(SByte , ( signed char));
799 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000800 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000801 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
802 IMPLEMENT_CAST_CASE(Int , ( signed int ));
803 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
804 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000805 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000806 IMPLEMENT_CAST_CASE(Float , (float));
807 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000808 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000809 default:
Chris Lattner02868352003-04-22 21:22:33 +0000810 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000811 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000812 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000813
814 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000815}
Chris Lattner92101ac2001-08-23 17:05:04 +0000816
Chris Lattnerd7916e92003-05-10 21:22:39 +0000817void Interpreter::visitCastInst(CastInst &I) {
818 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000819 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
820}
Chris Lattner92101ac2001-08-23 17:05:04 +0000821
Chris Lattner4c665492003-10-18 05:55:25 +0000822void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000823 ExecutionContext &SF = ECStack.back();
824
Brian Gaeke8da17482003-11-13 06:06:01 +0000825 // Get the incoming valist parameter. LLI treats the valist as a pointer
826 // to the next argument.
Chris Lattner4c665492003-10-18 05:55:25 +0000827 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
828
Brian Gaeke8da17482003-11-13 06:06:01 +0000829 // Move the pointer to the next vararg.
830 GenericValue *ArgPtr = (GenericValue *) GVTOP (VAList);
831 ++ArgPtr;
832 VAList = PTOGV (ArgPtr);
Chris Lattner4c665492003-10-18 05:55:25 +0000833 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000834}
Chris Lattner92101ac2001-08-23 17:05:04 +0000835
Brian Gaekec1a2be12003-11-07 21:20:47 +0000836#define IMPLEMENT_VAARG(TY) \
837 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
838
839void Interpreter::visitVAArgInst(VAArgInst &I) {
840 ExecutionContext &SF = ECStack.back();
841
Brian Gaeke8da17482003-11-13 06:06:01 +0000842 // Get the incoming valist parameter. LLI treats the valist as a pointer
843 // to the next argument.
Brian Gaekec1a2be12003-11-07 21:20:47 +0000844 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke8da17482003-11-13 06:06:01 +0000845 assert (GVTOP (VAList) != 0 && "VAList was null in vaarg instruction");
846 GenericValue Dest, Src = *(GenericValue *) GVTOP (VAList);
Brian Gaekec1a2be12003-11-07 21:20:47 +0000847 const Type *Ty = I.getType();
848 switch (Ty->getPrimitiveID()) {
849 IMPLEMENT_VAARG(UByte);
850 IMPLEMENT_VAARG(SByte);
851 IMPLEMENT_VAARG(UShort);
852 IMPLEMENT_VAARG(Short);
853 IMPLEMENT_VAARG(UInt);
854 IMPLEMENT_VAARG(Int);
855 IMPLEMENT_VAARG(ULong);
856 IMPLEMENT_VAARG(Long);
857 IMPLEMENT_VAARG(Pointer);
858 IMPLEMENT_VAARG(Float);
859 IMPLEMENT_VAARG(Double);
860 IMPLEMENT_VAARG(Bool);
861 default:
862 std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
863 abort();
864 }
865
866 // Set the Value of this Instruction.
867 SetValue(&I, Dest, SF);
868}
869
Chris Lattner92101ac2001-08-23 17:05:04 +0000870//===----------------------------------------------------------------------===//
871// Dispatch and Execution Code
872//===----------------------------------------------------------------------===//
873
Chris Lattner92101ac2001-08-23 17:05:04 +0000874//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000875// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000876//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000877void Interpreter::callFunction(Function *F,
878 const std::vector<GenericValue> &ArgVals) {
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000879 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
880 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
Chris Lattner365a76e2001-09-10 04:49:44 +0000881 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +0000882 // Make a new stack frame... and fill it in.
883 ECStack.push_back(ExecutionContext());
884 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000885 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000886
887 // Special handling for external functions.
888 if (F->isExternal()) {
889 GenericValue Result = callExternalFunction (F, ArgVals);
890 // Simulate a 'ret' instruction of the appropriate type.
891 popStackAndReturnValueToCaller (F->getReturnType (), Result);
892 return;
893 }
894
895 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +0000896 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000897 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000898
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000899 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000900 assert((ArgVals.size() == F->asize() ||
901 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000902 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000903
904 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000905 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000906 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000907 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000908
909 // Handle varargs arguments...
910 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000911}
912
Chris Lattner92101ac2001-08-23 17:05:04 +0000913void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000914 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000915 // Interpret a single instruction & increment the "PC".
916 ExecutionContext &SF = ECStack.back(); // Current stack frame
917 Instruction &I = *SF.CurInst++; // Increment before execute
918
919 // Track the number of dynamic instructions executed.
920 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +0000921
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000922 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +0000923 }
924}