blob: 7ceb8e8c54b98304a7307b0f8623b42e4c60d476 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "interpreter"
15#include "Interpreter.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/GetElementPtrTypeIterator.h"
21#include "llvm/ADT/APInt.h"
22#include "llvm/ADT/Statistic.h"
Chris Lattner001ea9d2008-07-08 17:25:49 +000023#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000025#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/MathExtras.h"
Gabor Greif7ee10f92007-10-11 19:40:35 +000027#include <algorithm>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000028#include <cmath>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
31STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032
Chris Lattner001ea9d2008-07-08 17:25:49 +000033static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
34 cl::desc("make the interpreter print every volatile load and store"));
35
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036//===----------------------------------------------------------------------===//
37// Various Helper Functions
38//===----------------------------------------------------------------------===//
39
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
41 SF.Values[V] = Val;
42}
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044//===----------------------------------------------------------------------===//
45// Binary Instruction Implementations
46//===----------------------------------------------------------------------===//
47
48#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
49 case Type::TY##TyID: \
50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
51 break
52
Dan Gohman7ce405e2009-06-04 22:49:04 +000053static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
54 GenericValue Src2, const Type *Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 switch (Ty->getTypeID()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 IMPLEMENT_BINARY_OPERATOR(+, Float);
57 IMPLEMENT_BINARY_OPERATOR(+, Double);
58 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +000059 errs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000060 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 }
62}
63
Dan Gohman7ce405e2009-06-04 22:49:04 +000064static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
65 GenericValue Src2, const Type *Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 switch (Ty->getTypeID()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 IMPLEMENT_BINARY_OPERATOR(-, Float);
68 IMPLEMENT_BINARY_OPERATOR(-, Double);
69 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +000070 errs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000071 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 }
73}
74
Dan Gohman7ce405e2009-06-04 22:49:04 +000075static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
76 GenericValue Src2, const Type *Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 switch (Ty->getTypeID()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 IMPLEMENT_BINARY_OPERATOR(*, Float);
79 IMPLEMENT_BINARY_OPERATOR(*, Double);
80 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +000081 errs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000082 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 }
84}
85
86static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
87 GenericValue Src2, const Type *Ty) {
88 switch (Ty->getTypeID()) {
89 IMPLEMENT_BINARY_OPERATOR(/, Float);
90 IMPLEMENT_BINARY_OPERATOR(/, Double);
91 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +000092 errs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +000093 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 }
95}
96
97static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
98 GenericValue Src2, const Type *Ty) {
99 switch (Ty->getTypeID()) {
100 case Type::FloatTyID:
101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
102 break;
103 case Type::DoubleTyID:
104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
105 break;
106 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000107 errs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000108 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 }
110}
111
112#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
113 case Type::IntegerTyID: \
114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
115 break;
116
117// Handle pointers specially because they must be compared with only as much
118// width as the host has. We _do not_ want to be comparing 64 bit values when
119// running on a 32-bit target, otherwise the upper 32 bits might mess up
120// comparisons if they contain garbage.
121#define IMPLEMENT_POINTER_ICMP(OP) \
122 case Type::PointerTyID: \
123 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
124 (void*)(intptr_t)Src2.PointerVal); \
125 break;
126
127static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
128 const Type *Ty) {
129 GenericValue Dest;
130 switch (Ty->getTypeID()) {
131 IMPLEMENT_INTEGER_ICMP(eq,Ty);
132 IMPLEMENT_POINTER_ICMP(==);
133 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000134 errs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000135 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 }
137 return Dest;
138}
139
140static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
141 const Type *Ty) {
142 GenericValue Dest;
143 switch (Ty->getTypeID()) {
144 IMPLEMENT_INTEGER_ICMP(ne,Ty);
145 IMPLEMENT_POINTER_ICMP(!=);
146 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000147 errs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000148 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 }
150 return Dest;
151}
152
153static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
154 const Type *Ty) {
155 GenericValue Dest;
156 switch (Ty->getTypeID()) {
157 IMPLEMENT_INTEGER_ICMP(ult,Ty);
158 IMPLEMENT_POINTER_ICMP(<);
159 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000160 errs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000161 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 }
163 return Dest;
164}
165
166static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
167 const Type *Ty) {
168 GenericValue Dest;
169 switch (Ty->getTypeID()) {
170 IMPLEMENT_INTEGER_ICMP(slt,Ty);
171 IMPLEMENT_POINTER_ICMP(<);
172 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000173 errs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000174 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 }
176 return Dest;
177}
178
179static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
180 const Type *Ty) {
181 GenericValue Dest;
182 switch (Ty->getTypeID()) {
183 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
184 IMPLEMENT_POINTER_ICMP(>);
185 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000186 errs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000187 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 }
189 return Dest;
190}
191
192static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
193 const Type *Ty) {
194 GenericValue Dest;
195 switch (Ty->getTypeID()) {
196 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
197 IMPLEMENT_POINTER_ICMP(>);
198 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000199 errs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000200 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 }
202 return Dest;
203}
204
205static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
206 const Type *Ty) {
207 GenericValue Dest;
208 switch (Ty->getTypeID()) {
209 IMPLEMENT_INTEGER_ICMP(ule,Ty);
210 IMPLEMENT_POINTER_ICMP(<=);
211 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000212 errs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000213 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 }
215 return Dest;
216}
217
218static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
219 const Type *Ty) {
220 GenericValue Dest;
221 switch (Ty->getTypeID()) {
222 IMPLEMENT_INTEGER_ICMP(sle,Ty);
223 IMPLEMENT_POINTER_ICMP(<=);
224 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000225 errs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000226 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 }
228 return Dest;
229}
230
231static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
232 const Type *Ty) {
233 GenericValue Dest;
234 switch (Ty->getTypeID()) {
235 IMPLEMENT_INTEGER_ICMP(uge,Ty);
236 IMPLEMENT_POINTER_ICMP(>=);
237 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000238 errs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000239 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 }
241 return Dest;
242}
243
244static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
245 const Type *Ty) {
246 GenericValue Dest;
247 switch (Ty->getTypeID()) {
248 IMPLEMENT_INTEGER_ICMP(sge,Ty);
249 IMPLEMENT_POINTER_ICMP(>=);
250 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000251 errs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000252 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 }
254 return Dest;
255}
256
257void Interpreter::visitICmpInst(ICmpInst &I) {
258 ExecutionContext &SF = ECStack.back();
259 const Type *Ty = I.getOperand(0)->getType();
260 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
261 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
262 GenericValue R; // Result
263
264 switch (I.getPredicate()) {
265 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
266 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
267 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
268 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
269 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
270 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
271 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
272 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
273 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
274 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
275 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000276 errs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000277 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 }
279
280 SetValue(&I, R, SF);
281}
282
283#define IMPLEMENT_FCMP(OP, TY) \
284 case Type::TY##TyID: \
285 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
286 break
287
288static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
289 const Type *Ty) {
290 GenericValue Dest;
291 switch (Ty->getTypeID()) {
292 IMPLEMENT_FCMP(==, Float);
293 IMPLEMENT_FCMP(==, Double);
294 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000295 errs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000296 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 }
298 return Dest;
299}
300
301static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
302 const Type *Ty) {
303 GenericValue Dest;
304 switch (Ty->getTypeID()) {
305 IMPLEMENT_FCMP(!=, Float);
306 IMPLEMENT_FCMP(!=, Double);
307
308 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000309 errs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000310 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 }
312 return Dest;
313}
314
315static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
316 const Type *Ty) {
317 GenericValue Dest;
318 switch (Ty->getTypeID()) {
319 IMPLEMENT_FCMP(<=, Float);
320 IMPLEMENT_FCMP(<=, Double);
321 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000322 errs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000323 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 }
325 return Dest;
326}
327
328static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
329 const Type *Ty) {
330 GenericValue Dest;
331 switch (Ty->getTypeID()) {
332 IMPLEMENT_FCMP(>=, Float);
333 IMPLEMENT_FCMP(>=, Double);
334 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000335 errs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000336 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 }
338 return Dest;
339}
340
341static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
342 const Type *Ty) {
343 GenericValue Dest;
344 switch (Ty->getTypeID()) {
345 IMPLEMENT_FCMP(<, Float);
346 IMPLEMENT_FCMP(<, Double);
347 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000348 errs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000349 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 }
351 return Dest;
352}
353
354static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
355 const Type *Ty) {
356 GenericValue Dest;
357 switch (Ty->getTypeID()) {
358 IMPLEMENT_FCMP(>, Float);
359 IMPLEMENT_FCMP(>, Double);
360 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000361 errs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000362 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 }
364 return Dest;
365}
366
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000367#define IMPLEMENT_UNORDERED(TY, X,Y) \
Chris Lattner82cdc062009-10-05 05:54:46 +0000368 if (TY->isFloatTy()) { \
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000369 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
370 Dest.IntVal = APInt(1,true); \
371 return Dest; \
372 } \
373 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
374 Dest.IntVal = APInt(1,true); \
375 return Dest; \
376 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377
378
379static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
380 const Type *Ty) {
381 GenericValue Dest;
382 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
383 return executeFCMP_OEQ(Src1, Src2, Ty);
384}
385
386static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
387 const Type *Ty) {
388 GenericValue Dest;
389 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
390 return executeFCMP_ONE(Src1, Src2, Ty);
391}
392
393static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
394 const Type *Ty) {
395 GenericValue Dest;
396 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
397 return executeFCMP_OLE(Src1, Src2, Ty);
398}
399
400static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
401 const Type *Ty) {
402 GenericValue Dest;
403 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
404 return executeFCMP_OGE(Src1, Src2, Ty);
405}
406
407static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
408 const Type *Ty) {
409 GenericValue Dest;
410 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
411 return executeFCMP_OLT(Src1, Src2, Ty);
412}
413
414static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
415 const Type *Ty) {
416 GenericValue Dest;
417 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
418 return executeFCMP_OGT(Src1, Src2, Ty);
419}
420
421static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
422 const Type *Ty) {
423 GenericValue Dest;
Chris Lattner82cdc062009-10-05 05:54:46 +0000424 if (Ty->isFloatTy())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
426 Src2.FloatVal == Src2.FloatVal));
427 else
428 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
429 Src2.DoubleVal == Src2.DoubleVal));
430 return Dest;
431}
432
433static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
434 const Type *Ty) {
435 GenericValue Dest;
Chris Lattner82cdc062009-10-05 05:54:46 +0000436 if (Ty->isFloatTy())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
438 Src2.FloatVal != Src2.FloatVal));
439 else
440 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
441 Src2.DoubleVal != Src2.DoubleVal));
442 return Dest;
443}
444
445void Interpreter::visitFCmpInst(FCmpInst &I) {
446 ExecutionContext &SF = ECStack.back();
447 const Type *Ty = I.getOperand(0)->getType();
448 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
449 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
450 GenericValue R; // Result
451
452 switch (I.getPredicate()) {
453 case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
454 case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
455 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
456 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
457 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
458 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
459 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
460 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
461 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
462 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
463 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
464 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
465 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
466 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
467 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
468 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
469 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000470 errs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000471 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 }
473
474 SetValue(&I, R, SF);
475}
476
477static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
478 GenericValue Src2, const Type *Ty) {
479 GenericValue Result;
480 switch (predicate) {
481 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
482 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
483 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
484 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
485 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
486 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
487 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
488 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
489 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
490 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
491 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
492 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
493 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
494 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
495 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
496 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
497 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
498 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
499 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
500 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
501 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
502 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
503 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
504 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
505 case FCmpInst::FCMP_FALSE: {
506 GenericValue Result;
507 Result.IntVal = APInt(1, false);
508 return Result;
509 }
510 case FCmpInst::FCMP_TRUE: {
511 GenericValue Result;
512 Result.IntVal = APInt(1, true);
513 return Result;
514 }
515 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000516 errs() << "Unhandled Cmp predicate\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000517 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 }
519}
520
521void Interpreter::visitBinaryOperator(BinaryOperator &I) {
522 ExecutionContext &SF = ECStack.back();
523 const Type *Ty = I.getOperand(0)->getType();
524 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
525 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
526 GenericValue R; // Result
527
528 switch (I.getOpcode()) {
Dan Gohman7ce405e2009-06-04 22:49:04 +0000529 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
530 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
531 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
532 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
533 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
534 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
535 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
536 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
538 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
539 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
540 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
541 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
542 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
543 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
544 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +0000545 errs() << "Don't know how to handle this binary operator!\n-->" << I;
Edwin Törökbd448e32009-07-14 16:55:14 +0000546 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 }
548
549 SetValue(&I, R, SF);
550}
551
552static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
553 GenericValue Src3) {
554 return Src1.IntVal == 0 ? Src3 : Src2;
555}
556
557void Interpreter::visitSelectInst(SelectInst &I) {
558 ExecutionContext &SF = ECStack.back();
559 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
560 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
561 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
562 GenericValue R = executeSelectInst(Src1, Src2, Src3);
563 SetValue(&I, R, SF);
564}
565
566
567//===----------------------------------------------------------------------===//
568// Terminator Instruction Implementations
569//===----------------------------------------------------------------------===//
570
571void Interpreter::exitCalled(GenericValue GV) {
572 // runAtExitHandlers() assumes there are no stack frames, but
573 // if exit() was called, then it had a stack frame. Blow away
574 // the stack before interpreting atexit handlers.
575 ECStack.clear ();
576 runAtExitHandlers ();
577 exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
578}
579
580/// Pop the last stack frame off of ECStack and then copy the result
581/// back into the result variable if we are not returning void. The
582/// result variable may be the ExitValue, or the Value of the calling
583/// CallInst if there was a previous stack frame. This method may
584/// invalidate any ECStack iterators you have. This method also takes
585/// care of switching to the normal destination BB, if we are returning
586/// from an invoke.
587///
588void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
589 GenericValue Result) {
590 // Pop the current stack frame.
591 ECStack.pop_back();
592
593 if (ECStack.empty()) { // Finished main. Put result into exit code...
594 if (RetTy && RetTy->isInteger()) { // Nonvoid return type?
595 ExitValue = Result; // Capture the exit value of the program
596 } else {
597 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
598 }
599 } else {
600 // If we have a previous stack frame, and we have a previous call,
601 // fill in the return value...
602 ExecutionContext &CallingSF = ECStack.back();
603 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Owen Anderson35b47072009-08-13 21:58:54 +0000604 // Save result...
605 if (CallingSF.Caller.getType() != Type::getVoidTy(RetTy->getContext()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 SetValue(I, Result, CallingSF);
607 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
608 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
609 CallingSF.Caller = CallSite(); // We returned from the call...
610 }
611 }
612}
613
614void Interpreter::visitReturnInst(ReturnInst &I) {
615 ExecutionContext &SF = ECStack.back();
Owen Anderson35b47072009-08-13 21:58:54 +0000616 const Type *RetTy = Type::getVoidTy(I.getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 GenericValue Result;
618
619 // Save away the return value... (if we are not 'ret void')
620 if (I.getNumOperands()) {
621 RetTy = I.getReturnValue()->getType();
622 Result = getOperandValue(I.getReturnValue(), SF);
623 }
624
625 popStackAndReturnValueToCaller(RetTy, Result);
626}
627
628void Interpreter::visitUnwindInst(UnwindInst &I) {
629 // Unwind stack
630 Instruction *Inst;
631 do {
632 ECStack.pop_back ();
633 if (ECStack.empty ())
Edwin Törökced9ff82009-07-11 13:10:19 +0000634 llvm_report_error("Empty stack during unwind!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 Inst = ECStack.back ().Caller.getInstruction ();
636 } while (!(Inst && isa<InvokeInst> (Inst)));
637
638 // Return from invoke
639 ExecutionContext &InvokingSF = ECStack.back ();
640 InvokingSF.Caller = CallSite ();
641
642 // Go to exceptional destination BB of invoke instruction
643 SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
644}
645
646void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Benjamin Kramer98c00ab2009-08-03 13:33:33 +0000647 llvm_report_error("Program executed an 'unreachable' instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648}
649
650void Interpreter::visitBranchInst(BranchInst &I) {
651 ExecutionContext &SF = ECStack.back();
652 BasicBlock *Dest;
653
654 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
655 if (!I.isUnconditional()) {
656 Value *Cond = I.getCondition();
657 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
658 Dest = I.getSuccessor(1);
659 }
660 SwitchToNewBasicBlock(Dest, SF);
661}
662
663void Interpreter::visitSwitchInst(SwitchInst &I) {
664 ExecutionContext &SF = ECStack.back();
665 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
666 const Type *ElTy = I.getOperand(0)->getType();
667
668 // Check to see if any of the cases match...
669 BasicBlock *Dest = 0;
670 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
671 if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
672 .IntVal != 0) {
673 Dest = cast<BasicBlock>(I.getOperand(i+1));
674 break;
675 }
676
677 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
678 SwitchToNewBasicBlock(Dest, SF);
679}
680
681// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
682// This function handles the actual updating of block and instruction iterators
683// as well as execution of all of the PHI nodes in the destination block.
684//
685// This method does this because all of the PHI nodes must be executed
686// atomically, reading their inputs before any of the results are updated. Not
687// doing this can cause problems if the PHI nodes depend on other PHI nodes for
688// their inputs. If the input PHI node is updated before it is read, incorrect
689// results can happen. Thus we use a two phase approach.
690//
691void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
692 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
693 SF.CurBB = Dest; // Update CurBB to branch destination
694 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
695
696 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
697
698 // Loop over all of the PHI nodes in the current block, reading their inputs.
699 std::vector<GenericValue> ResultValues;
700
701 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
702 // Search for the value corresponding to this previous bb...
703 int i = PN->getBasicBlockIndex(PrevBB);
704 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
705 Value *IncomingValue = PN->getIncomingValue(i);
706
707 // Save the incoming value for this PHI node...
708 ResultValues.push_back(getOperandValue(IncomingValue, SF));
709 }
710
711 // Now loop over all of the PHI nodes setting their values...
712 SF.CurInst = SF.CurBB->begin();
713 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
714 PHINode *PN = cast<PHINode>(SF.CurInst);
715 SetValue(PN, ResultValues[i], SF);
716 }
717}
718
719//===----------------------------------------------------------------------===//
720// Memory Instruction Implementations
721//===----------------------------------------------------------------------===//
722
Victor Hernandezb1687302009-10-23 21:09:37 +0000723void Interpreter::visitAllocaInst(AllocaInst &I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 ExecutionContext &SF = ECStack.back();
725
726 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
727
728 // Get the number of elements being allocated by the array...
729 unsigned NumElements =
730 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
731
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000732 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733
Gabor Greif7ee10f92007-10-11 19:40:35 +0000734 // Avoid malloc-ing zero bytes, use max()...
735 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736
737 // Allocate enough memory to hold the type...
738 void *Memory = malloc(MemToAlloc);
739
Chris Lattnerb0659d42009-08-23 04:52:46 +0000740 DEBUG(errs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
741 << NumElements << " (Total: " << MemToAlloc << ") at "
742 << uintptr_t(Memory) << '\n');
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743
744 GenericValue Result = PTOGV(Memory);
745 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
746 SetValue(&I, Result, SF);
747
748 if (I.getOpcode() == Instruction::Alloca)
749 ECStack.back().Allocas.add(Memory);
750}
751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752// getElementOffset - The workhorse for getelementptr.
753//
754GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
755 gep_type_iterator E,
756 ExecutionContext &SF) {
757 assert(isa<PointerType>(Ptr->getType()) &&
758 "Cannot getElementOffset of a nonpointer type!");
759
760 uint64_t Total = 0;
761
762 for (; I != E; ++I) {
763 if (const StructType *STy = dyn_cast<StructType>(*I)) {
764 const StructLayout *SLO = TD.getStructLayout(STy);
765
766 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
767 unsigned Index = unsigned(CPU->getZExtValue());
768
769 Total += SLO->getElementOffset(Index);
770 } else {
771 const SequentialType *ST = cast<SequentialType>(*I);
772 // Get the index number for the array... which must be long type...
773 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
774
775 int64_t Idx;
776 unsigned BitWidth =
777 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
778 if (BitWidth == 32)
779 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattner3203f192008-04-06 21:50:58 +0000780 else {
781 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattner3203f192008-04-06 21:50:58 +0000783 }
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000784 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 }
786 }
787
788 GenericValue Result;
789 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
Chris Lattnerb0659d42009-08-23 04:52:46 +0000790 DEBUG(errs() << "GEP Index " << Total << " bytes.\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 return Result;
792}
793
794void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
795 ExecutionContext &SF = ECStack.back();
Owen Anderson0e17f632009-06-26 16:46:15 +0000796 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 gep_type_begin(I), gep_type_end(I), SF), SF);
798}
799
800void Interpreter::visitLoadInst(LoadInst &I) {
801 ExecutionContext &SF = ECStack.back();
802 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
803 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
804 GenericValue Result;
805 LoadValueFromMemory(Result, Ptr, I.getType());
806 SetValue(&I, Result, SF);
Chris Lattner001ea9d2008-07-08 17:25:49 +0000807 if (I.isVolatile() && PrintVolatile)
Chris Lattner8a6411c2009-08-23 04:37:46 +0000808 errs() << "Volatile load " << I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809}
810
811void Interpreter::visitStoreInst(StoreInst &I) {
812 ExecutionContext &SF = ECStack.back();
813 GenericValue Val = getOperandValue(I.getOperand(0), SF);
814 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
815 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
816 I.getOperand(0)->getType());
Chris Lattner001ea9d2008-07-08 17:25:49 +0000817 if (I.isVolatile() && PrintVolatile)
Chris Lattner8a6411c2009-08-23 04:37:46 +0000818 errs() << "Volatile store: " << I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819}
820
821//===----------------------------------------------------------------------===//
822// Miscellaneous Instruction Implementations
823//===----------------------------------------------------------------------===//
824
825void Interpreter::visitCallSite(CallSite CS) {
826 ExecutionContext &SF = ECStack.back();
827
828 // Check to see if this is an intrinsic function call...
829 Function *F = CS.getCalledFunction();
830 if (F && F->isDeclaration ())
831 switch (F->getIntrinsicID()) {
832 case Intrinsic::not_intrinsic:
833 break;
834 case Intrinsic::vastart: { // va_start
835 GenericValue ArgIndex;
836 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
837 ArgIndex.UIntPairVal.second = 0;
838 SetValue(CS.getInstruction(), ArgIndex, SF);
839 return;
840 }
841 case Intrinsic::vaend: // va_end is a noop for the interpreter
842 return;
843 case Intrinsic::vacopy: // va_copy: dest = src
844 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
845 return;
846 default:
847 // If it is an unknown intrinsic function, use the intrinsic lowering
848 // class to transform it into hopefully tasty LLVM code.
849 //
850 BasicBlock::iterator me(CS.getInstruction());
851 BasicBlock *Parent = CS.getInstruction()->getParent();
852 bool atBegin(Parent->begin() == me);
853 if (!atBegin)
854 --me;
855 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
856
857 // Restore the CurInst pointer to the first instruction newly inserted, if
858 // any.
859 if (atBegin) {
860 SF.CurInst = Parent->begin();
861 } else {
862 SF.CurInst = me;
863 ++SF.CurInst;
864 }
865 return;
866 }
867
868
869 SF.Caller = CS;
870 std::vector<GenericValue> ArgVals;
871 const unsigned NumArgs = SF.Caller.arg_size();
872 ArgVals.reserve(NumArgs);
873 uint16_t pNum = 1;
874 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
875 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
876 Value *V = *i;
877 ArgVals.push_back(getOperandValue(V, SF));
Duncan Sands637ec552007-11-28 17:07:01 +0000878 // Promote all integral types whose size is < sizeof(i32) into i32.
879 // We do this by zero or sign extending the value as appropriate
880 // according to the parameter attributes
881 const Type *Ty = V->getType();
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000882 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
Devang Pateld222f862008-09-25 21:00:45 +0000883 if (CS.paramHasAttr(pNum, Attribute::ZExt))
Duncan Sands637ec552007-11-28 17:07:01 +0000884 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
Devang Pateld222f862008-09-25 21:00:45 +0000885 else if (CS.paramHasAttr(pNum, Attribute::SExt))
Duncan Sands637ec552007-11-28 17:07:01 +0000886 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000887 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 }
889
890 // To handle indirect calls, we must get the pointer value from the argument
891 // and treat it as a function pointer.
892 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
893 callFunction((Function*)GVTOP(SRC), ArgVals);
894}
895
896void Interpreter::visitShl(BinaryOperator &I) {
897 ExecutionContext &SF = ECStack.back();
898 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
899 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
900 GenericValue Dest;
Chris Lattner09484b42009-01-16 20:17:02 +0000901 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
902 Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
903 else
904 Dest.IntVal = Src1.IntVal;
905
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 SetValue(&I, Dest, SF);
907}
908
909void Interpreter::visitLShr(BinaryOperator &I) {
910 ExecutionContext &SF = ECStack.back();
911 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
912 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
913 GenericValue Dest;
Chris Lattner09484b42009-01-16 20:17:02 +0000914 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
915 Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
916 else
917 Dest.IntVal = Src1.IntVal;
918
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 SetValue(&I, Dest, SF);
920}
921
922void Interpreter::visitAShr(BinaryOperator &I) {
923 ExecutionContext &SF = ECStack.back();
924 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
925 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner09484b42009-01-16 20:17:02 +0000926 GenericValue Dest;
927 if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
928 Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
929 else
930 Dest.IntVal = Src1.IntVal;
931
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 SetValue(&I, Dest, SF);
933}
934
935GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
936 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
938 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
941 return Dest;
942}
943
944GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
945 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
947 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 Dest.IntVal = Src.IntVal.sext(DBitWidth);
950 return Dest;
951}
952
953GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
954 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
956 const IntegerType *DITy = cast<IntegerType>(DstTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 unsigned DBitWidth = DITy->getBitWidth();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 Dest.IntVal = Src.IntVal.zext(DBitWidth);
959 return Dest;
960}
961
962GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
963 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner82cdc062009-10-05 05:54:46 +0000965 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 "Invalid FPTrunc instruction");
967 Dest.FloatVal = (float) Src.DoubleVal;
968 return Dest;
969}
970
971GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
972 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner82cdc062009-10-05 05:54:46 +0000974 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 "Invalid FPTrunc instruction");
976 Dest.DoubleVal = (double) Src.FloatVal;
977 return Dest;
978}
979
980GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
981 ExecutionContext &SF) {
982 const Type *SrcTy = SrcVal->getType();
983 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
984 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
985 assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
986
987 if (SrcTy->getTypeID() == Type::FloatTyID)
988 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
989 else
990 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
991 return Dest;
992}
993
994GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
995 ExecutionContext &SF) {
996 const Type *SrcTy = SrcVal->getType();
997 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
998 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
999 assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
1000
1001 if (SrcTy->getTypeID() == Type::FloatTyID)
1002 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1003 else
1004 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1005 return Dest;
1006}
1007
1008GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
1009 ExecutionContext &SF) {
1010 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1011 assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
1012
1013 if (DstTy->getTypeID() == Type::FloatTyID)
1014 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1015 else
1016 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1017 return Dest;
1018}
1019
1020GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
1021 ExecutionContext &SF) {
1022 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1023 assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
1024
1025 if (DstTy->getTypeID() == Type::FloatTyID)
1026 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1027 else
1028 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1029 return Dest;
1030
1031}
1032
1033GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
1034 ExecutionContext &SF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1036 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Chris Lattner3203f192008-04-06 21:50:58 +00001037 assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038
1039 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1040 return Dest;
1041}
1042
1043GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
1044 ExecutionContext &SF) {
1045 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1046 assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
1047
1048 uint32_t PtrSize = TD.getPointerSizeInBits();
1049 if (PtrSize != Src.IntVal.getBitWidth())
1050 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1051
1052 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1053 return Dest;
1054}
1055
1056GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
1057 ExecutionContext &SF) {
1058
1059 const Type *SrcTy = SrcVal->getType();
1060 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1061 if (isa<PointerType>(DstTy)) {
1062 assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
1063 Dest.PointerVal = Src.PointerVal;
1064 } else if (DstTy->isInteger()) {
Chris Lattner82cdc062009-10-05 05:54:46 +00001065 if (SrcTy->isFloatTy()) {
Dan Gohmand06cad62009-04-01 18:45:54 +00001066 Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 Dest.IntVal.floatToBits(Src.FloatVal);
Chris Lattner82cdc062009-10-05 05:54:46 +00001068 } else if (SrcTy->isDoubleTy()) {
Dan Gohmand06cad62009-04-01 18:45:54 +00001069 Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070 Dest.IntVal.doubleToBits(Src.DoubleVal);
1071 } else if (SrcTy->isInteger()) {
1072 Dest.IntVal = Src.IntVal;
1073 } else
Edwin Törökbd448e32009-07-14 16:55:14 +00001074 llvm_unreachable("Invalid BitCast");
Chris Lattner82cdc062009-10-05 05:54:46 +00001075 } else if (DstTy->isFloatTy()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 if (SrcTy->isInteger())
1077 Dest.FloatVal = Src.IntVal.bitsToFloat();
1078 else
1079 Dest.FloatVal = Src.FloatVal;
Chris Lattner82cdc062009-10-05 05:54:46 +00001080 } else if (DstTy->isDoubleTy()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 if (SrcTy->isInteger())
1082 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1083 else
1084 Dest.DoubleVal = Src.DoubleVal;
1085 } else
Edwin Törökbd448e32009-07-14 16:55:14 +00001086 llvm_unreachable("Invalid Bitcast");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087
1088 return Dest;
1089}
1090
1091void Interpreter::visitTruncInst(TruncInst &I) {
1092 ExecutionContext &SF = ECStack.back();
1093 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1094}
1095
1096void Interpreter::visitSExtInst(SExtInst &I) {
1097 ExecutionContext &SF = ECStack.back();
1098 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1099}
1100
1101void Interpreter::visitZExtInst(ZExtInst &I) {
1102 ExecutionContext &SF = ECStack.back();
1103 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1104}
1105
1106void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1107 ExecutionContext &SF = ECStack.back();
1108 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1109}
1110
1111void Interpreter::visitFPExtInst(FPExtInst &I) {
1112 ExecutionContext &SF = ECStack.back();
1113 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1114}
1115
1116void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1117 ExecutionContext &SF = ECStack.back();
1118 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1119}
1120
1121void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1122 ExecutionContext &SF = ECStack.back();
1123 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1124}
1125
1126void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1127 ExecutionContext &SF = ECStack.back();
1128 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1129}
1130
1131void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1132 ExecutionContext &SF = ECStack.back();
1133 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1134}
1135
1136void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1137 ExecutionContext &SF = ECStack.back();
1138 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1139}
1140
1141void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1142 ExecutionContext &SF = ECStack.back();
1143 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1144}
1145
1146void Interpreter::visitBitCastInst(BitCastInst &I) {
1147 ExecutionContext &SF = ECStack.back();
1148 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1149}
1150
1151#define IMPLEMENT_VAARG(TY) \
1152 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1153
1154void Interpreter::visitVAArgInst(VAArgInst &I) {
1155 ExecutionContext &SF = ECStack.back();
1156
1157 // Get the incoming valist parameter. LLI treats the valist as a
1158 // (ec-stack-depth var-arg-index) pair.
1159 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1160 GenericValue Dest;
1161 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1162 .VarArgs[VAList.UIntPairVal.second];
1163 const Type *Ty = I.getType();
1164 switch (Ty->getTypeID()) {
1165 case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
1166 IMPLEMENT_VAARG(Pointer);
1167 IMPLEMENT_VAARG(Float);
1168 IMPLEMENT_VAARG(Double);
1169 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +00001170 errs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001171 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 }
1173
1174 // Set the Value of this Instruction.
1175 SetValue(&I, Dest, SF);
1176
1177 // Move the pointer to the next vararg.
1178 ++VAList.UIntPairVal.second;
1179}
1180
1181GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1182 ExecutionContext &SF) {
1183 switch (CE->getOpcode()) {
1184 case Instruction::Trunc:
1185 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1186 case Instruction::ZExt:
1187 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1188 case Instruction::SExt:
1189 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1190 case Instruction::FPTrunc:
1191 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1192 case Instruction::FPExt:
1193 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1194 case Instruction::UIToFP:
1195 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1196 case Instruction::SIToFP:
1197 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1198 case Instruction::FPToUI:
1199 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1200 case Instruction::FPToSI:
1201 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1202 case Instruction::PtrToInt:
1203 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1204 case Instruction::IntToPtr:
1205 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1206 case Instruction::BitCast:
1207 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1208 case Instruction::GetElementPtr:
1209 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1210 gep_type_end(CE), SF);
1211 case Instruction::FCmp:
1212 case Instruction::ICmp:
1213 return executeCmpInst(CE->getPredicate(),
1214 getOperandValue(CE->getOperand(0), SF),
1215 getOperandValue(CE->getOperand(1), SF),
1216 CE->getOperand(0)->getType());
1217 case Instruction::Select:
1218 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1219 getOperandValue(CE->getOperand(1), SF),
1220 getOperandValue(CE->getOperand(2), SF));
1221 default :
1222 break;
1223 }
1224
1225 // The cases below here require a GenericValue parameter for the result
1226 // so we initialize one, compute it and then return it.
1227 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1228 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
1229 GenericValue Dest;
1230 const Type * Ty = CE->getOperand(0)->getType();
1231 switch (CE->getOpcode()) {
Dan Gohman7ce405e2009-06-04 22:49:04 +00001232 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1233 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1234 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1235 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1236 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1237 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1239 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1240 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1241 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1242 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1243 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohman7ce405e2009-06-04 22:49:04 +00001244 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1245 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1246 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 case Instruction::Shl:
1248 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1249 break;
1250 case Instruction::LShr:
1251 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1252 break;
1253 case Instruction::AShr:
1254 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1255 break;
1256 default:
Chris Lattner8a6411c2009-08-23 04:37:46 +00001257 errs() << "Unhandled ConstantExpr: " << *CE << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001258 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 return GenericValue();
1260 }
1261 return Dest;
1262}
1263
1264GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1265 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1266 return getConstantExprValue(CE, SF);
1267 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1268 return getConstantValue(CPV);
1269 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1270 return PTOGV(getPointerToGlobal(GV));
1271 } else {
1272 return SF.Values[V];
1273 }
1274}
1275
1276//===----------------------------------------------------------------------===//
1277// Dispatch and Execution Code
1278//===----------------------------------------------------------------------===//
1279
1280//===----------------------------------------------------------------------===//
1281// callFunction - Execute the specified function...
1282//
1283void Interpreter::callFunction(Function *F,
1284 const std::vector<GenericValue> &ArgVals) {
1285 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
1286 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
1287 "Incorrect number of arguments passed into function call!");
1288 // Make a new stack frame... and fill it in.
1289 ECStack.push_back(ExecutionContext());
1290 ExecutionContext &StackFrame = ECStack.back();
1291 StackFrame.CurFunction = F;
1292
1293 // Special handling for external functions.
1294 if (F->isDeclaration()) {
1295 GenericValue Result = callExternalFunction (F, ArgVals);
1296 // Simulate a 'ret' instruction of the appropriate type.
1297 popStackAndReturnValueToCaller (F->getReturnType (), Result);
1298 return;
1299 }
1300
1301 // Get pointers to first LLVM BB & Instruction in function.
1302 StackFrame.CurBB = F->begin();
1303 StackFrame.CurInst = StackFrame.CurBB->begin();
1304
1305 // Run through the function arguments and initialize their values...
1306 assert((ArgVals.size() == F->arg_size() ||
1307 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
1308 "Invalid number of values passed to function invocation!");
1309
1310 // Handle non-varargs arguments...
1311 unsigned i = 0;
1312 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1313 AI != E; ++AI, ++i)
1314 SetValue(AI, ArgVals[i], StackFrame);
1315
1316 // Handle varargs arguments...
1317 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1318}
1319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320
1321void Interpreter::run() {
1322 while (!ECStack.empty()) {
1323 // Interpret a single instruction & increment the "PC".
1324 ExecutionContext &SF = ECStack.back(); // Current stack frame
1325 Instruction &I = *SF.CurInst++; // Increment before execute
1326
1327 // Track the number of dynamic instructions executed.
1328 ++NumDynamicInsts;
1329
Chris Lattner8a6411c2009-08-23 04:37:46 +00001330 DEBUG(errs() << "About to interpret: " << I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 visit(I); // Dispatch to one of the visit* methods...
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001332#if 0
1333 // This is not safe, as visiting the instruction could lower it and free I.
Chris Lattner8a6411c2009-08-23 04:37:46 +00001334DEBUG(
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
1336 I.getType() != Type::VoidTy) {
Chris Lattner8a6411c2009-08-23 04:37:46 +00001337 errs() << " --> ";
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001338 const GenericValue &Val = SF.Values[&I];
1339 switch (I.getType()->getTypeID()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001340 default: llvm_unreachable("Invalid GenericValue Type");
Chris Lattner8a6411c2009-08-23 04:37:46 +00001341 case Type::VoidTyID: errs() << "void"; break;
1342 case Type::FloatTyID: errs() << "float " << Val.FloatVal; break;
1343 case Type::DoubleTyID: errs() << "double " << Val.DoubleVal; break;
1344 case Type::PointerTyID: errs() << "void* " << intptr_t(Val.PointerVal);
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001345 break;
1346 case Type::IntegerTyID:
Chris Lattner8a6411c2009-08-23 04:37:46 +00001347 errs() << "i" << Val.IntVal.getBitWidth() << " "
1348 << Val.IntVal.toStringUnsigned(10)
1349 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001350 break;
1351 }
Chris Lattner8a6411c2009-08-23 04:37:46 +00001352 });
Chris Lattnerfc65a9f2007-09-21 18:30:39 +00001353#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 }
1355}