blob: a17093ac86e86d3a44ae46953bc347c22b39be6c [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"
Brian Gaeke03e43dc2003-10-24 19:59:01 +000015#include "llvm/Function.h"
Chris Lattnerd5bc41a2003-04-25 04:21:19 +000016#include "llvm/Instructions.h"
Chris Lattnere2cbbce2002-04-29 18:56:45 +000017#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.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 Lattner2e42d3a2001-10-15 05:51:48 +000021
Chris Lattnerfe11a972002-12-23 23:59:41 +000022Interpreter *TheEE = 0;
23
Chris Lattnerbbdabce2002-12-08 05:51:08 +000024namespace {
25 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
26}
27
Chris Lattner2e42d3a2001-10-15 05:51:48 +000028//===----------------------------------------------------------------------===//
Chris Lattner39bb5b42001-10-15 13:25:40 +000029// Value Manipulation code
30//===----------------------------------------------------------------------===//
31
Chris Lattnera34c5682002-08-27 22:33:45 +000032// Operations used by constant expr implementations...
33static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
34 ExecutionContext &SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000035static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000036 const Type *Ty);
Chris Lattnera34c5682002-08-27 22:33:45 +000037
Brian Gaeke29794cb2003-09-05 18:55:03 +000038GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +000039 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
40 switch (CE->getOpcode()) {
41 case Instruction::Cast:
42 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
43 case Instruction::GetElementPtr:
Chris Lattnerfe11a972002-12-23 23:59:41 +000044 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
45 CE->op_end(), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +000046 case Instruction::Add:
47 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
48 getOperandValue(CE->getOperand(1), SF),
Chris Lattnerb945e4d2003-04-22 20:37:39 +000049 CE->getType());
Chris Lattnera34c5682002-08-27 22:33:45 +000050 default:
Chris Lattner02868352003-04-22 21:22:33 +000051 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
Chris Lattnera34c5682002-08-27 22:33:45 +000052 abort();
Chris Lattner04e2ad72003-04-21 22:43:32 +000053 return GenericValue();
Chris Lattnera34c5682002-08-27 22:33:45 +000054 }
55 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000056 return TheEE->getConstantValue(CPV);
Chris Lattner39bb5b42001-10-15 13:25:40 +000057 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000058 return PTOGV(TheEE->getPointerToGlobal(GV));
Chris Lattner39bb5b42001-10-15 13:25:40 +000059 } else {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000060 return SF.Values[V];
Chris Lattner39bb5b42001-10-15 13:25:40 +000061 }
62}
63
Chris Lattner39bb5b42001-10-15 13:25:40 +000064static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000065 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000066}
67
Chris Lattner39bb5b42001-10-15 13:25:40 +000068//===----------------------------------------------------------------------===//
Chris Lattner2e42d3a2001-10-15 05:51:48 +000069// Annotation Wrangling code
70//===----------------------------------------------------------------------===//
71
72void Interpreter::initializeExecutionEngine() {
Chris Lattnerfe11a972002-12-23 23:59:41 +000073 TheEE = this;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000074}
75
Chris Lattner2adcd832002-05-03 19:52:30 +000076//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000077// Binary Instruction Implementations
78//===----------------------------------------------------------------------===//
79
80#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
81 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
82
83static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +000084 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +000085 GenericValue Dest;
86 switch (Ty->getPrimitiveID()) {
87 IMPLEMENT_BINARY_OPERATOR(+, UByte);
88 IMPLEMENT_BINARY_OPERATOR(+, SByte);
89 IMPLEMENT_BINARY_OPERATOR(+, UShort);
90 IMPLEMENT_BINARY_OPERATOR(+, Short);
91 IMPLEMENT_BINARY_OPERATOR(+, UInt);
92 IMPLEMENT_BINARY_OPERATOR(+, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +000093 IMPLEMENT_BINARY_OPERATOR(+, ULong);
94 IMPLEMENT_BINARY_OPERATOR(+, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +000095 IMPLEMENT_BINARY_OPERATOR(+, Float);
96 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000097 default:
Chris Lattner02868352003-04-22 21:22:33 +000098 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
99 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000100 }
101 return Dest;
102}
103
104static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000105 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000106 GenericValue Dest;
107 switch (Ty->getPrimitiveID()) {
108 IMPLEMENT_BINARY_OPERATOR(-, UByte);
109 IMPLEMENT_BINARY_OPERATOR(-, SByte);
110 IMPLEMENT_BINARY_OPERATOR(-, UShort);
111 IMPLEMENT_BINARY_OPERATOR(-, Short);
112 IMPLEMENT_BINARY_OPERATOR(-, UInt);
113 IMPLEMENT_BINARY_OPERATOR(-, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000114 IMPLEMENT_BINARY_OPERATOR(-, ULong);
115 IMPLEMENT_BINARY_OPERATOR(-, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000116 IMPLEMENT_BINARY_OPERATOR(-, Float);
117 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +0000118 default:
Chris Lattner02868352003-04-22 21:22:33 +0000119 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
120 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000121 }
122 return Dest;
123}
124
Chris Lattnerc2593162001-10-27 08:28:11 +0000125static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000126 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000127 GenericValue Dest;
128 switch (Ty->getPrimitiveID()) {
129 IMPLEMENT_BINARY_OPERATOR(*, UByte);
130 IMPLEMENT_BINARY_OPERATOR(*, SByte);
131 IMPLEMENT_BINARY_OPERATOR(*, UShort);
132 IMPLEMENT_BINARY_OPERATOR(*, Short);
133 IMPLEMENT_BINARY_OPERATOR(*, UInt);
134 IMPLEMENT_BINARY_OPERATOR(*, Int);
135 IMPLEMENT_BINARY_OPERATOR(*, ULong);
136 IMPLEMENT_BINARY_OPERATOR(*, Long);
137 IMPLEMENT_BINARY_OPERATOR(*, Float);
138 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000139 default:
Chris Lattner02868352003-04-22 21:22:33 +0000140 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
141 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000142 }
143 return Dest;
144}
145
146static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000147 const Type *Ty) {
Chris Lattnerc2593162001-10-27 08:28:11 +0000148 GenericValue Dest;
149 switch (Ty->getPrimitiveID()) {
150 IMPLEMENT_BINARY_OPERATOR(/, UByte);
151 IMPLEMENT_BINARY_OPERATOR(/, SByte);
152 IMPLEMENT_BINARY_OPERATOR(/, UShort);
153 IMPLEMENT_BINARY_OPERATOR(/, Short);
154 IMPLEMENT_BINARY_OPERATOR(/, UInt);
155 IMPLEMENT_BINARY_OPERATOR(/, Int);
156 IMPLEMENT_BINARY_OPERATOR(/, ULong);
157 IMPLEMENT_BINARY_OPERATOR(/, Long);
158 IMPLEMENT_BINARY_OPERATOR(/, Float);
159 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +0000160 default:
Chris Lattner02868352003-04-22 21:22:33 +0000161 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
162 abort();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000163 }
164 return Dest;
165}
166
167static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000168 const Type *Ty) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000169 GenericValue Dest;
170 switch (Ty->getPrimitiveID()) {
171 IMPLEMENT_BINARY_OPERATOR(%, UByte);
172 IMPLEMENT_BINARY_OPERATOR(%, SByte);
173 IMPLEMENT_BINARY_OPERATOR(%, UShort);
174 IMPLEMENT_BINARY_OPERATOR(%, Short);
175 IMPLEMENT_BINARY_OPERATOR(%, UInt);
176 IMPLEMENT_BINARY_OPERATOR(%, Int);
177 IMPLEMENT_BINARY_OPERATOR(%, ULong);
178 IMPLEMENT_BINARY_OPERATOR(%, Long);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000179 case Type::FloatTyID:
180 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
181 break;
182 case Type::DoubleTyID:
183 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
184 break;
185 default:
Chris Lattner02868352003-04-22 21:22:33 +0000186 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
187 abort();
Chris Lattnerc2593162001-10-27 08:28:11 +0000188 }
189 return Dest;
190}
191
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000192static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000193 const Type *Ty) {
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000194 GenericValue Dest;
195 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000196 IMPLEMENT_BINARY_OPERATOR(&, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000197 IMPLEMENT_BINARY_OPERATOR(&, UByte);
198 IMPLEMENT_BINARY_OPERATOR(&, SByte);
199 IMPLEMENT_BINARY_OPERATOR(&, UShort);
200 IMPLEMENT_BINARY_OPERATOR(&, Short);
201 IMPLEMENT_BINARY_OPERATOR(&, UInt);
202 IMPLEMENT_BINARY_OPERATOR(&, Int);
203 IMPLEMENT_BINARY_OPERATOR(&, ULong);
204 IMPLEMENT_BINARY_OPERATOR(&, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000205 default:
Chris Lattner02868352003-04-22 21:22:33 +0000206 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
207 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000208 }
209 return Dest;
210}
211
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000212static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000213 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000214 GenericValue Dest;
215 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000216 IMPLEMENT_BINARY_OPERATOR(|, Bool);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000217 IMPLEMENT_BINARY_OPERATOR(|, UByte);
218 IMPLEMENT_BINARY_OPERATOR(|, SByte);
219 IMPLEMENT_BINARY_OPERATOR(|, UShort);
220 IMPLEMENT_BINARY_OPERATOR(|, Short);
221 IMPLEMENT_BINARY_OPERATOR(|, UInt);
222 IMPLEMENT_BINARY_OPERATOR(|, Int);
223 IMPLEMENT_BINARY_OPERATOR(|, ULong);
224 IMPLEMENT_BINARY_OPERATOR(|, Long);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000225 default:
Chris Lattner02868352003-04-22 21:22:33 +0000226 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
227 abort();
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000228 }
229 return Dest;
230}
231
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000232static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000233 const Type *Ty) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000234 GenericValue Dest;
235 switch (Ty->getPrimitiveID()) {
Chris Lattner669b76a2003-04-23 19:21:00 +0000236 IMPLEMENT_BINARY_OPERATOR(^, Bool);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000237 IMPLEMENT_BINARY_OPERATOR(^, UByte);
238 IMPLEMENT_BINARY_OPERATOR(^, SByte);
239 IMPLEMENT_BINARY_OPERATOR(^, UShort);
240 IMPLEMENT_BINARY_OPERATOR(^, Short);
241 IMPLEMENT_BINARY_OPERATOR(^, UInt);
242 IMPLEMENT_BINARY_OPERATOR(^, Int);
243 IMPLEMENT_BINARY_OPERATOR(^, ULong);
244 IMPLEMENT_BINARY_OPERATOR(^, Long);
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000245 default:
Chris Lattner02868352003-04-22 21:22:33 +0000246 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
247 abort();
Chris Lattner4d0e1f92001-10-30 20:54:36 +0000248 }
249 return Dest;
250}
251
Chris Lattner92101ac2001-08-23 17:05:04 +0000252#define IMPLEMENT_SETCC(OP, TY) \
253 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
254
Chris Lattnerfd506f52003-04-23 19:55:35 +0000255// Handle pointers specially because they must be compared with only as much
256// width as the host has. We _do not_ want to be comparing 64 bit values when
257// running on a 32-bit target, otherwise the upper 32 bits might mess up
258// comparisons if they contain garbage.
259#define IMPLEMENT_POINTERSETCC(OP) \
260 case Type::PointerTyID: \
261 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
262 (void*)(intptr_t)Src2.PointerVal; break
263
Chris Lattner92101ac2001-08-23 17:05:04 +0000264static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000265 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000266 GenericValue Dest;
267 switch (Ty->getPrimitiveID()) {
268 IMPLEMENT_SETCC(==, UByte);
269 IMPLEMENT_SETCC(==, SByte);
270 IMPLEMENT_SETCC(==, UShort);
271 IMPLEMENT_SETCC(==, Short);
272 IMPLEMENT_SETCC(==, UInt);
273 IMPLEMENT_SETCC(==, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000274 IMPLEMENT_SETCC(==, ULong);
275 IMPLEMENT_SETCC(==, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000276 IMPLEMENT_SETCC(==, Float);
277 IMPLEMENT_SETCC(==, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000278 IMPLEMENT_POINTERSETCC(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000279 default:
Chris Lattner02868352003-04-22 21:22:33 +0000280 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
281 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000282 }
283 return Dest;
284}
285
286static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000287 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000288 GenericValue Dest;
289 switch (Ty->getPrimitiveID()) {
290 IMPLEMENT_SETCC(!=, UByte);
291 IMPLEMENT_SETCC(!=, SByte);
292 IMPLEMENT_SETCC(!=, UShort);
293 IMPLEMENT_SETCC(!=, Short);
294 IMPLEMENT_SETCC(!=, UInt);
295 IMPLEMENT_SETCC(!=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000296 IMPLEMENT_SETCC(!=, ULong);
297 IMPLEMENT_SETCC(!=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000298 IMPLEMENT_SETCC(!=, Float);
299 IMPLEMENT_SETCC(!=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000300 IMPLEMENT_POINTERSETCC(!=);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000301
Chris Lattner92101ac2001-08-23 17:05:04 +0000302 default:
Chris Lattner02868352003-04-22 21:22:33 +0000303 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
304 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000305 }
306 return Dest;
307}
308
309static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000310 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000311 GenericValue Dest;
312 switch (Ty->getPrimitiveID()) {
313 IMPLEMENT_SETCC(<=, UByte);
314 IMPLEMENT_SETCC(<=, SByte);
315 IMPLEMENT_SETCC(<=, UShort);
316 IMPLEMENT_SETCC(<=, Short);
317 IMPLEMENT_SETCC(<=, UInt);
318 IMPLEMENT_SETCC(<=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000319 IMPLEMENT_SETCC(<=, ULong);
320 IMPLEMENT_SETCC(<=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000321 IMPLEMENT_SETCC(<=, Float);
322 IMPLEMENT_SETCC(<=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000323 IMPLEMENT_POINTERSETCC(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000324 default:
Chris Lattner02868352003-04-22 21:22:33 +0000325 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
326 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000327 }
328 return Dest;
329}
330
331static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000332 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000333 GenericValue Dest;
334 switch (Ty->getPrimitiveID()) {
335 IMPLEMENT_SETCC(>=, UByte);
336 IMPLEMENT_SETCC(>=, SByte);
337 IMPLEMENT_SETCC(>=, UShort);
338 IMPLEMENT_SETCC(>=, Short);
339 IMPLEMENT_SETCC(>=, UInt);
340 IMPLEMENT_SETCC(>=, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000341 IMPLEMENT_SETCC(>=, ULong);
342 IMPLEMENT_SETCC(>=, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000343 IMPLEMENT_SETCC(>=, Float);
344 IMPLEMENT_SETCC(>=, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000345 IMPLEMENT_POINTERSETCC(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000346 default:
Chris Lattner02868352003-04-22 21:22:33 +0000347 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
348 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000349 }
350 return Dest;
351}
352
353static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000354 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000355 GenericValue Dest;
356 switch (Ty->getPrimitiveID()) {
357 IMPLEMENT_SETCC(<, UByte);
358 IMPLEMENT_SETCC(<, SByte);
359 IMPLEMENT_SETCC(<, UShort);
360 IMPLEMENT_SETCC(<, Short);
361 IMPLEMENT_SETCC(<, UInt);
362 IMPLEMENT_SETCC(<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000363 IMPLEMENT_SETCC(<, ULong);
364 IMPLEMENT_SETCC(<, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000365 IMPLEMENT_SETCC(<, Float);
366 IMPLEMENT_SETCC(<, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000367 IMPLEMENT_POINTERSETCC(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000368 default:
Chris Lattner02868352003-04-22 21:22:33 +0000369 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
370 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000371 }
372 return Dest;
373}
374
375static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000376 const Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000377 GenericValue Dest;
378 switch (Ty->getPrimitiveID()) {
379 IMPLEMENT_SETCC(>, UByte);
380 IMPLEMENT_SETCC(>, SByte);
381 IMPLEMENT_SETCC(>, UShort);
382 IMPLEMENT_SETCC(>, Short);
383 IMPLEMENT_SETCC(>, UInt);
384 IMPLEMENT_SETCC(>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000385 IMPLEMENT_SETCC(>, ULong);
386 IMPLEMENT_SETCC(>, Long);
Chris Lattner92101ac2001-08-23 17:05:04 +0000387 IMPLEMENT_SETCC(>, Float);
388 IMPLEMENT_SETCC(>, Double);
Chris Lattnerfd506f52003-04-23 19:55:35 +0000389 IMPLEMENT_POINTERSETCC(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000390 default:
Chris Lattner02868352003-04-22 21:22:33 +0000391 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
392 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000393 }
394 return Dest;
395}
396
Chris Lattnerd7916e92003-05-10 21:22:39 +0000397void Interpreter::visitBinaryOperator(BinaryOperator &I) {
398 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000399 const Type *Ty = I.getOperand(0)->getType();
400 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
401 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000402 GenericValue R; // Result
403
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000404 switch (I.getOpcode()) {
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000405 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
406 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
407 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
408 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
409 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
410 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
411 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
412 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
413 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
414 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
415 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
416 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
417 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
418 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000419 default:
Chris Lattner02868352003-04-22 21:22:33 +0000420 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
421 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000422 }
423
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000424 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000425}
426
Chris Lattner92101ac2001-08-23 17:05:04 +0000427//===----------------------------------------------------------------------===//
428// Terminator Instruction Implementations
429//===----------------------------------------------------------------------===//
430
Chris Lattnere43db882001-10-27 04:15:57 +0000431void Interpreter::exitCalled(GenericValue GV) {
Chris Lattnere43db882001-10-27 04:15:57 +0000432 ExitCode = GV.SByteVal;
433 ECStack.clear();
434}
435
Chris Lattnerd7916e92003-05-10 21:22:39 +0000436void Interpreter::visitReturnInst(ReturnInst &I) {
437 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000438 const Type *RetTy = 0;
439 GenericValue Result;
440
441 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000442 if (I.getNumOperands()) {
443 RetTy = I.getReturnValue()->getType();
444 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000445 }
446
Chris Lattner92101ac2001-08-23 17:05:04 +0000447 // Pop the current stack frame... this invalidates SF
448 ECStack.pop_back();
449
450 if (ECStack.empty()) { // Finished main. Put result into exit code...
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000451 if (RetTy && RetTy->isIntegral()) { // Nonvoid return type?
452 ExitCode = Result.IntVal; // Capture the exit code of the program
Chris Lattner92101ac2001-08-23 17:05:04 +0000453 } else {
454 ExitCode = 0;
455 }
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000456 } else {
457 // If we have a previous stack frame, and we have a previous call,
458 // fill in the return value...
459 ExecutionContext &NewSF = ECStack.back();
460 if (NewSF.Caller) {
461 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
462 SetValue(NewSF.Caller, Result, NewSF);
463 NewSF.Caller = 0; // We returned from the call...
464 }
Chris Lattner92101ac2001-08-23 17:05:04 +0000465 }
466}
467
Chris Lattnerd7916e92003-05-10 21:22:39 +0000468void Interpreter::visitBranchInst(BranchInst &I) {
469 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000470 BasicBlock *Dest;
471
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000472 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
473 if (!I.isUnconditional()) {
474 Value *Cond = I.getCondition();
Chris Lattner77113b62003-05-10 20:21:16 +0000475 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000476 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000477 }
Chris Lattner77113b62003-05-10 20:21:16 +0000478 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000479}
480
Chris Lattnerd7916e92003-05-10 21:22:39 +0000481void Interpreter::visitSwitchInst(SwitchInst &I) {
482 ExecutionContext &SF = ECStack.back();
Chris Lattner09e93922003-04-22 20:34:47 +0000483 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
484 const Type *ElTy = I.getOperand(0)->getType();
Chris Lattner09e93922003-04-22 20:34:47 +0000485
486 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000487 BasicBlock *Dest = 0;
488 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
Chris Lattner09e93922003-04-22 20:34:47 +0000489 if (executeSetEQInst(CondVal,
Chris Lattnerb945e4d2003-04-22 20:37:39 +0000490 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
Chris Lattner09e93922003-04-22 20:34:47 +0000491 Dest = cast<BasicBlock>(I.getOperand(i+1));
492 break;
493 }
Chris Lattner09e93922003-04-22 20:34:47 +0000494
495 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000496 SwitchToNewBasicBlock(Dest, SF);
497}
498
499// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
500// This function handles the actual updating of block and instruction iterators
501// as well as execution of all of the PHI nodes in the destination block.
502//
503// This method does this because all of the PHI nodes must be executed
504// atomically, reading their inputs before any of the results are updated. Not
505// doing this can cause problems if the PHI nodes depend on other PHI nodes for
506// their inputs. If the input PHI node is updated before it is read, incorrect
507// results can happen. Thus we use a two phase approach.
508//
509void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
510 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
511 SF.CurBB = Dest; // Update CurBB to branch destination
512 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
513
514 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
515
516 // Loop over all of the PHI nodes in the current block, reading their inputs.
517 std::vector<GenericValue> ResultValues;
518
519 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
520 // Search for the value corresponding to this previous bb...
521 int i = PN->getBasicBlockIndex(PrevBB);
522 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
523 Value *IncomingValue = PN->getIncomingValue(i);
524
525 // Save the incoming value for this PHI node...
526 ResultValues.push_back(getOperandValue(IncomingValue, SF));
527 }
528
529 // Now loop over all of the PHI nodes setting their values...
530 SF.CurInst = SF.CurBB->begin();
531 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
532 ++SF.CurInst, ++i)
533 SetValue(PN, ResultValues[i], SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000534}
535
Chris Lattner92101ac2001-08-23 17:05:04 +0000536//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000537// Memory Instruction Implementations
538//===----------------------------------------------------------------------===//
539
Chris Lattnerd7916e92003-05-10 21:22:39 +0000540void Interpreter::visitAllocationInst(AllocationInst &I) {
541 ExecutionContext &SF = ECStack.back();
542
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000543 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000544
Chris Lattnercc82cc12002-04-28 21:57:33 +0000545 // Get the number of elements being allocated by the array...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000546 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
Chris Lattner86660982001-08-27 05:16:50 +0000547
548 // Allocate enough memory to hold the type...
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000549 // FIXME: Don't use CALLOC, use a tainted malloc.
Chris Lattner9bffa732002-02-19 18:50:09 +0000550 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
551
Chris Lattnerfe11a972002-12-23 23:59:41 +0000552 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000553 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000554 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000555
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000556 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +0000557 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +0000558}
559
Chris Lattnerd7916e92003-05-10 21:22:39 +0000560void Interpreter::visitFreeInst(FreeInst &I) {
561 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000562 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
563 GenericValue Value = getOperandValue(I.getOperand(0), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000564 // TODO: Check to make sure memory is allocated
Chris Lattnerfe11a972002-12-23 23:59:41 +0000565 free(GVTOP(Value)); // Free memory
Chris Lattner86660982001-08-27 05:16:50 +0000566}
567
Chris Lattnera34c5682002-08-27 22:33:45 +0000568// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +0000569//
Chris Lattnerfe11a972002-12-23 23:59:41 +0000570GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
571 User::op_iterator E,
572 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000573 assert(isa<PointerType>(Ptr->getType()) &&
Chris Lattner95c3af52001-10-29 19:32:19 +0000574 "Cannot getElementOffset of a nonpointer type!");
575
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000576 PointerTy Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +0000577 const Type *Ty = Ptr->getType();
578
579 for (; I != E; ++I) {
Chris Lattner782b9392001-11-26 18:18:18 +0000580 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
581 const StructLayout *SLO = TD.getStructLayout(STy);
582
Misha Brukmand5d96b92003-10-10 17:42:19 +0000583 // Indices must be ubyte constants...
Chris Lattnera34c5682002-08-27 22:33:45 +0000584 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
Chris Lattner782b9392001-11-26 18:18:18 +0000585 assert(CPU->getType() == Type::UByteTy);
586 unsigned Index = CPU->getValue();
587
Chris Lattner782b9392001-11-26 18:18:18 +0000588 Total += SLO->MemberOffsets[Index];
589 Ty = STy->getElementTypes()[Index];
Chris Lattnerf23eb852001-12-14 16:49:29 +0000590 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
Chris Lattner006a4a52003-02-25 21:14:59 +0000591 // Get the index number for the array... which must be long type...
Chris Lattner0374b8d2002-09-11 01:21:35 +0000592 assert((*I)->getType() == Type::LongTy);
Chris Lattnere8b3e9b2002-09-13 23:30:42 +0000593 unsigned Idx = getOperandValue(*I, SF).LongVal;
Chris Lattnerf23eb852001-12-14 16:49:29 +0000594 Ty = ST->getElementType();
Chris Lattner782b9392001-11-26 18:18:18 +0000595 unsigned Size = TD.getTypeSize(Ty);
596 Total += Size*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000597 }
Chris Lattner95c3af52001-10-29 19:32:19 +0000598 }
599
Chris Lattnera34c5682002-08-27 22:33:45 +0000600 GenericValue Result;
601 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
602 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +0000603}
604
Chris Lattnerd7916e92003-05-10 21:22:39 +0000605void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
606 ExecutionContext &SF = ECStack.back();
Chris Lattnerfe11a972002-12-23 23:59:41 +0000607 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
Chris Lattnera34c5682002-08-27 22:33:45 +0000608 I.idx_begin(), I.idx_end(), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +0000609}
610
Chris Lattnerd7916e92003-05-10 21:22:39 +0000611void Interpreter::visitLoadInst(LoadInst &I) {
612 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000613 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000614 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Chris Lattner374344c2003-05-08 16:52:43 +0000615 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000616 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000617}
618
Chris Lattnerd7916e92003-05-10 21:22:39 +0000619void Interpreter::visitStoreInst(StoreInst &I) {
620 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +0000621 GenericValue Val = getOperandValue(I.getOperand(0), SF);
622 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +0000623 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +0000624 I.getOperand(0)->getType());
Chris Lattnerfddc7552002-10-15 20:34:05 +0000625}
626
Chris Lattner86660982001-08-27 05:16:50 +0000627//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +0000628// Miscellaneous Instruction Implementations
629//===----------------------------------------------------------------------===//
630
Chris Lattnerd7916e92003-05-10 21:22:39 +0000631void Interpreter::visitCallInst(CallInst &I) {
632 ExecutionContext &SF = ECStack.back();
633 SF.Caller = &I;
Chris Lattner02868352003-04-22 21:22:33 +0000634 std::vector<GenericValue> ArgVals;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000635 ArgVals.reserve(I.getNumOperands()-1);
Chris Lattner93780132003-01-13 00:58:52 +0000636 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000637 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
Chris Lattner93780132003-01-13 00:58:52 +0000638 // Promote all integral types whose size is < sizeof(int) into ints. We do
639 // this by zero or sign extending the value as appropriate according to the
640 // source type.
641 if (I.getOperand(i)->getType()->isIntegral() &&
642 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
643 const Type *Ty = I.getOperand(i)->getType();
644 if (Ty == Type::ShortTy)
645 ArgVals.back().IntVal = ArgVals.back().ShortVal;
646 else if (Ty == Type::UShortTy)
647 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
648 else if (Ty == Type::SByteTy)
649 ArgVals.back().IntVal = ArgVals.back().SByteVal;
650 else if (Ty == Type::UByteTy)
651 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
652 else if (Ty == Type::BoolTy)
653 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
654 else
655 assert(0 && "Unknown type!");
656 }
657 }
Chris Lattner365a76e2001-09-10 04:49:44 +0000658
Chris Lattner070cf5e2001-11-07 20:12:30 +0000659 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000660 // and treat it as a function pointer.
Chris Lattnerd7916e92003-05-10 21:22:39 +0000661 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +0000662 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +0000663}
664
Chris Lattner86660982001-08-27 05:16:50 +0000665#define IMPLEMENT_SHIFT(OP, TY) \
666 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
667
Chris Lattnerd7916e92003-05-10 21:22:39 +0000668void Interpreter::visitShl(ShiftInst &I) {
669 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000670 const Type *Ty = I.getOperand(0)->getType();
671 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
672 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000673 GenericValue Dest;
674
675 switch (Ty->getPrimitiveID()) {
676 IMPLEMENT_SHIFT(<<, UByte);
677 IMPLEMENT_SHIFT(<<, SByte);
678 IMPLEMENT_SHIFT(<<, UShort);
679 IMPLEMENT_SHIFT(<<, Short);
680 IMPLEMENT_SHIFT(<<, UInt);
681 IMPLEMENT_SHIFT(<<, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000682 IMPLEMENT_SHIFT(<<, ULong);
683 IMPLEMENT_SHIFT(<<, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000684 default:
Chris Lattner02868352003-04-22 21:22:33 +0000685 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
Chris Lattner86660982001-08-27 05:16:50 +0000686 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000687 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000688}
689
Chris Lattnerd7916e92003-05-10 21:22:39 +0000690void Interpreter::visitShr(ShiftInst &I) {
691 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000692 const Type *Ty = I.getOperand(0)->getType();
693 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
694 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner86660982001-08-27 05:16:50 +0000695 GenericValue Dest;
696
697 switch (Ty->getPrimitiveID()) {
698 IMPLEMENT_SHIFT(>>, UByte);
699 IMPLEMENT_SHIFT(>>, SByte);
700 IMPLEMENT_SHIFT(>>, UShort);
701 IMPLEMENT_SHIFT(>>, Short);
702 IMPLEMENT_SHIFT(>>, UInt);
703 IMPLEMENT_SHIFT(>>, Int);
Chris Lattner7b851ab2001-10-15 19:18:26 +0000704 IMPLEMENT_SHIFT(>>, ULong);
705 IMPLEMENT_SHIFT(>>, Long);
Chris Lattner86660982001-08-27 05:16:50 +0000706 default:
Chris Lattner02868352003-04-22 21:22:33 +0000707 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
708 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000709 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000710 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000711}
712
713#define IMPLEMENT_CAST(DTY, DCTY, STY) \
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000714 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
Chris Lattner86660982001-08-27 05:16:50 +0000715
716#define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
717 case Type::DESTTY##TyID: \
718 switch (SrcTy->getPrimitiveID()) { \
Chris Lattnerf4dca802002-05-02 19:28:45 +0000719 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
Chris Lattner86660982001-08-27 05:16:50 +0000720 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
721 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
722 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
723 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
724 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
Chris Lattner7b851ab2001-10-15 19:18:26 +0000725 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
726 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
Chris Lattnerc2593162001-10-27 08:28:11 +0000727 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
728 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
Chris Lattner86660982001-08-27 05:16:50 +0000729
730#define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
731 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
732 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
733
734#define IMPLEMENT_CAST_CASE_END() \
Chris Lattner02868352003-04-22 21:22:33 +0000735 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
736 abort(); \
Chris Lattner86660982001-08-27 05:16:50 +0000737 } \
738 break
739
740#define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
741 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
742 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
Chris Lattner86660982001-08-27 05:16:50 +0000743 IMPLEMENT_CAST_CASE_END()
744
Brian Gaeke29794cb2003-09-05 18:55:03 +0000745GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
746 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +0000747 const Type *SrcTy = SrcVal->getType();
748 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner86660982001-08-27 05:16:50 +0000749
750 switch (Ty->getPrimitiveID()) {
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000751 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
752 IMPLEMENT_CAST_CASE(SByte , ( signed char));
753 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
Chris Lattner1bbd3612002-08-02 22:06:04 +0000754 IMPLEMENT_CAST_CASE(Short , ( signed short));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000755 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
756 IMPLEMENT_CAST_CASE(Int , ( signed int ));
757 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
758 IMPLEMENT_CAST_CASE(Long , ( int64_t));
Chris Lattner2fdaddf2002-10-30 21:47:57 +0000759 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
Chris Lattnerea38c0e2001-11-07 19:46:27 +0000760 IMPLEMENT_CAST_CASE(Float , (float));
761 IMPLEMENT_CAST_CASE(Double , (double));
Chris Lattner5bff50d2003-04-22 21:15:56 +0000762 IMPLEMENT_CAST_CASE(Bool , (bool));
Chris Lattner86660982001-08-27 05:16:50 +0000763 default:
Chris Lattner02868352003-04-22 21:22:33 +0000764 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
Chris Lattner5bff50d2003-04-22 21:15:56 +0000765 abort();
Chris Lattner86660982001-08-27 05:16:50 +0000766 }
Chris Lattnera34c5682002-08-27 22:33:45 +0000767
768 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +0000769}
Chris Lattner92101ac2001-08-23 17:05:04 +0000770
Chris Lattnerd7916e92003-05-10 21:22:39 +0000771void Interpreter::visitCastInst(CastInst &I) {
772 ExecutionContext &SF = ECStack.back();
Chris Lattnera34c5682002-08-27 22:33:45 +0000773 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
774}
Chris Lattner92101ac2001-08-23 17:05:04 +0000775
Chris Lattner4c665492003-10-18 05:55:25 +0000776void Interpreter::visitVANextInst(VANextInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000777 ExecutionContext &SF = ECStack.back();
778
Chris Lattner4c665492003-10-18 05:55:25 +0000779 // Get the incoming valist element. LLI treats the valist as an integer.
780 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
781
782 // Move to the next operand.
Chris Lattner374344c2003-05-08 16:52:43 +0000783 unsigned Argument = VAList.IntVal++;
Chris Lattner374344c2003-05-08 16:52:43 +0000784 assert(Argument < SF.VarArgs.size() &&
785 "Accessing past the last vararg argument!");
Chris Lattner4c665492003-10-18 05:55:25 +0000786 SetValue(&I, VAList, SF);
Chris Lattner374344c2003-05-08 16:52:43 +0000787}
Chris Lattner92101ac2001-08-23 17:05:04 +0000788
789//===----------------------------------------------------------------------===//
790// Dispatch and Execution Code
791//===----------------------------------------------------------------------===//
792
Chris Lattner92101ac2001-08-23 17:05:04 +0000793//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000794// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +0000795//
Chris Lattnerda82ed52003-05-08 16:18:31 +0000796void Interpreter::callFunction(Function *F,
797 const std::vector<GenericValue> &ArgVals) {
Chris Lattner365a76e2001-09-10 04:49:44 +0000798 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
799 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
800 "Incorrect number of arguments passed into function call!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000801 if (F->isExternal()) {
Chris Lattnerda82ed52003-05-08 16:18:31 +0000802 GenericValue Result = callExternalFunction(F, ArgVals);
Chris Lattnercdf51782003-05-08 16:06:52 +0000803 const Type *RetTy = F->getReturnType();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000804
805 // Copy the result back into the result variable if we are not returning
806 // void.
807 if (RetTy != Type::VoidTy) {
808 if (!ECStack.empty() && ECStack.back().Caller) {
809 ExecutionContext &SF = ECStack.back();
Chris Lattnerbb76f022001-10-30 20:27:31 +0000810 SetValue(SF.Caller, Result, SF);
Chris Lattnerbb76f022001-10-30 20:27:31 +0000811 SF.Caller = 0; // We returned from the call...
Chris Lattnerbb76f022001-10-30 20:27:31 +0000812 }
813 }
814
Chris Lattner92101ac2001-08-23 17:05:04 +0000815 return;
816 }
817
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000818 // Process the function, assigning instruction numbers to the instructions in
819 // the function. Also calculate the number of values for each type slot
820 // active.
Chris Lattner92101ac2001-08-23 17:05:04 +0000821 //
Chris Lattner86660982001-08-27 05:16:50 +0000822
Chris Lattner63bd6132003-09-17 17:26:22 +0000823 // Make a new stack frame... and fill it in.
824 ECStack.push_back(ExecutionContext());
825 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +0000826 StackFrame.CurFunction = F;
Chris Lattnercdf51782003-05-08 16:06:52 +0000827 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000828 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +0000829
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000830 // Run through the function arguments and initialize their values...
Chris Lattnercdf51782003-05-08 16:06:52 +0000831 assert((ArgVals.size() == F->asize() ||
832 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000833 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +0000834
835 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +0000836 unsigned i = 0;
Chris Lattnercdf51782003-05-08 16:06:52 +0000837 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000838 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +0000839
840 // Handle varargs arguments...
841 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +0000842}
843
Chris Lattner92101ac2001-08-23 17:05:04 +0000844void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +0000845 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000846 // Interpret a single instruction & increment the "PC".
847 ExecutionContext &SF = ECStack.back(); // Current stack frame
848 Instruction &I = *SF.CurInst++; // Increment before execute
849
850 // Track the number of dynamic instructions executed.
851 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +0000852
Brian Gaeke03e43dc2003-10-24 19:59:01 +0000853 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner92101ac2001-08-23 17:05:04 +0000854 }
855}