1//===-- Execution.cpp - Implement code to simulate the program ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the actual instruction interpreter.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Interpreter.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/Statistic.h"
16#include "llvm/CodeGen/IntrinsicLowering.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/GetElementPtrTypeIterator.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/raw_ostream.h"
26#include <algorithm>
27#include <cmath>
28using namespace llvm;
29
30#define DEBUG_TYPE "interpreter"
31
32STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
33
34static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35 cl::desc("make the interpreter print every volatile load and store"));
36
37//===----------------------------------------------------------------------===//
38// Various Helper Functions
39//===----------------------------------------------------------------------===//
40
41static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
42 SF.Values[V] = Val;
43}
44
45//===----------------------------------------------------------------------===//
46// Unary Instruction Implementations
47//===----------------------------------------------------------------------===//
48
49static void executeFNegInst(GenericValue &Dest, GenericValue Src, Type *Ty) {
50 switch (Ty->getTypeID()) {
51 case Type::FloatTyID:
52 Dest.FloatVal = -Src.FloatVal;
53 break;
54 case Type::DoubleTyID:
55 Dest.DoubleVal = -Src.DoubleVal;
56 break;
57 default:
58 llvm_unreachable("Unhandled type for FNeg instruction");
59 }
60}
61
62void Interpreter::visitUnaryOperator(UnaryOperator &I) {
63 ExecutionContext &SF = ECStack.back();
64 Type *Ty = I.getOperand(i_nocapture: 0)->getType();
65 GenericValue Src = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
66 GenericValue R; // Result
67
68 // First process vector operation
69 if (Ty->isVectorTy()) {
70 R.AggregateVal.resize(new_size: Src.AggregateVal.size());
71
72 switch(I.getOpcode()) {
73 default:
74 llvm_unreachable("Don't know how to handle this unary operator");
75 break;
76 case Instruction::FNeg:
77 if (cast<VectorType>(Val: Ty)->getElementType()->isFloatTy()) {
78 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
79 R.AggregateVal[i].FloatVal = -Src.AggregateVal[i].FloatVal;
80 } else if (cast<VectorType>(Val: Ty)->getElementType()->isDoubleTy()) {
81 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
82 R.AggregateVal[i].DoubleVal = -Src.AggregateVal[i].DoubleVal;
83 } else {
84 llvm_unreachable("Unhandled type for FNeg instruction");
85 }
86 break;
87 }
88 } else {
89 switch (I.getOpcode()) {
90 default:
91 llvm_unreachable("Don't know how to handle this unary operator");
92 break;
93 case Instruction::FNeg: executeFNegInst(Dest&: R, Src, Ty); break;
94 }
95 }
96 SetValue(V: &I, Val: R, SF);
97}
98
99//===----------------------------------------------------------------------===//
100// Binary Instruction Implementations
101//===----------------------------------------------------------------------===//
102
103#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
104 case Type::TY##TyID: \
105 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
106 break
107
108static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
109 GenericValue Src2, Type *Ty) {
110 switch (Ty->getTypeID()) {
111 IMPLEMENT_BINARY_OPERATOR(+, Float);
112 IMPLEMENT_BINARY_OPERATOR(+, Double);
113 default:
114 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
115 llvm_unreachable(nullptr);
116 }
117}
118
119static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
120 GenericValue Src2, Type *Ty) {
121 switch (Ty->getTypeID()) {
122 IMPLEMENT_BINARY_OPERATOR(-, Float);
123 IMPLEMENT_BINARY_OPERATOR(-, Double);
124 default:
125 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
126 llvm_unreachable(nullptr);
127 }
128}
129
130static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
131 GenericValue Src2, Type *Ty) {
132 switch (Ty->getTypeID()) {
133 IMPLEMENT_BINARY_OPERATOR(*, Float);
134 IMPLEMENT_BINARY_OPERATOR(*, Double);
135 default:
136 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
137 llvm_unreachable(nullptr);
138 }
139}
140
141static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
142 GenericValue Src2, Type *Ty) {
143 switch (Ty->getTypeID()) {
144 IMPLEMENT_BINARY_OPERATOR(/, Float);
145 IMPLEMENT_BINARY_OPERATOR(/, Double);
146 default:
147 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
148 llvm_unreachable(nullptr);
149 }
150}
151
152static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
153 GenericValue Src2, Type *Ty) {
154 switch (Ty->getTypeID()) {
155 case Type::FloatTyID:
156 Dest.FloatVal = fmod(x: Src1.FloatVal, y: Src2.FloatVal);
157 break;
158 case Type::DoubleTyID:
159 Dest.DoubleVal = fmod(x: Src1.DoubleVal, y: Src2.DoubleVal);
160 break;
161 default:
162 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
163 llvm_unreachable(nullptr);
164 }
165}
166
167#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
168 case Type::IntegerTyID: \
169 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
170 break;
171
172#define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \
173 case Type::FixedVectorTyID: \
174 case Type::ScalableVectorTyID: { \
175 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
176 Dest.AggregateVal.resize(Src1.AggregateVal.size()); \
177 for (uint32_t _i = 0; _i < Src1.AggregateVal.size(); _i++) \
178 Dest.AggregateVal[_i].IntVal = APInt( \
179 1, Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal)); \
180 } break;
181
182// Handle pointers specially because they must be compared with only as much
183// width as the host has. We _do not_ want to be comparing 64 bit values when
184// running on a 32-bit target, otherwise the upper 32 bits might mess up
185// comparisons if they contain garbage.
186#define IMPLEMENT_POINTER_ICMP(OP) \
187 case Type::PointerTyID: \
188 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
189 (void*)(intptr_t)Src2.PointerVal); \
190 break;
191
192static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
193 Type *Ty) {
194 GenericValue Dest;
195 switch (Ty->getTypeID()) {
196 IMPLEMENT_INTEGER_ICMP(eq,Ty);
197 IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
198 IMPLEMENT_POINTER_ICMP(==);
199 default:
200 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
201 llvm_unreachable(nullptr);
202 }
203 return Dest;
204}
205
206static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
207 Type *Ty) {
208 GenericValue Dest;
209 switch (Ty->getTypeID()) {
210 IMPLEMENT_INTEGER_ICMP(ne,Ty);
211 IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
212 IMPLEMENT_POINTER_ICMP(!=);
213 default:
214 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
215 llvm_unreachable(nullptr);
216 }
217 return Dest;
218}
219
220static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
221 Type *Ty) {
222 GenericValue Dest;
223 switch (Ty->getTypeID()) {
224 IMPLEMENT_INTEGER_ICMP(ult,Ty);
225 IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
226 IMPLEMENT_POINTER_ICMP(<);
227 default:
228 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
229 llvm_unreachable(nullptr);
230 }
231 return Dest;
232}
233
234static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
235 Type *Ty) {
236 GenericValue Dest;
237 switch (Ty->getTypeID()) {
238 IMPLEMENT_INTEGER_ICMP(slt,Ty);
239 IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
240 IMPLEMENT_POINTER_ICMP(<);
241 default:
242 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
243 llvm_unreachable(nullptr);
244 }
245 return Dest;
246}
247
248static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
249 Type *Ty) {
250 GenericValue Dest;
251 switch (Ty->getTypeID()) {
252 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
253 IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
254 IMPLEMENT_POINTER_ICMP(>);
255 default:
256 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
257 llvm_unreachable(nullptr);
258 }
259 return Dest;
260}
261
262static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
263 Type *Ty) {
264 GenericValue Dest;
265 switch (Ty->getTypeID()) {
266 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
267 IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
268 IMPLEMENT_POINTER_ICMP(>);
269 default:
270 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
271 llvm_unreachable(nullptr);
272 }
273 return Dest;
274}
275
276static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
277 Type *Ty) {
278 GenericValue Dest;
279 switch (Ty->getTypeID()) {
280 IMPLEMENT_INTEGER_ICMP(ule,Ty);
281 IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
282 IMPLEMENT_POINTER_ICMP(<=);
283 default:
284 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
285 llvm_unreachable(nullptr);
286 }
287 return Dest;
288}
289
290static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
291 Type *Ty) {
292 GenericValue Dest;
293 switch (Ty->getTypeID()) {
294 IMPLEMENT_INTEGER_ICMP(sle,Ty);
295 IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
296 IMPLEMENT_POINTER_ICMP(<=);
297 default:
298 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
299 llvm_unreachable(nullptr);
300 }
301 return Dest;
302}
303
304static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
305 Type *Ty) {
306 GenericValue Dest;
307 switch (Ty->getTypeID()) {
308 IMPLEMENT_INTEGER_ICMP(uge,Ty);
309 IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
310 IMPLEMENT_POINTER_ICMP(>=);
311 default:
312 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
313 llvm_unreachable(nullptr);
314 }
315 return Dest;
316}
317
318static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
319 Type *Ty) {
320 GenericValue Dest;
321 switch (Ty->getTypeID()) {
322 IMPLEMENT_INTEGER_ICMP(sge,Ty);
323 IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
324 IMPLEMENT_POINTER_ICMP(>=);
325 default:
326 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
327 llvm_unreachable(nullptr);
328 }
329 return Dest;
330}
331
332void Interpreter::visitICmpInst(ICmpInst &I) {
333 ExecutionContext &SF = ECStack.back();
334 Type *Ty = I.getOperand(i_nocapture: 0)->getType();
335 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
336 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
337 GenericValue R; // Result
338
339 switch (I.getPredicate()) {
340 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
341 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
342 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
343 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
344 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
345 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
346 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
347 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
348 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
349 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
350 default:
351 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
352 llvm_unreachable(nullptr);
353 }
354
355 SetValue(V: &I, Val: R, SF);
356}
357
358#define IMPLEMENT_FCMP(OP, TY) \
359 case Type::TY##TyID: \
360 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
361 break
362
363#define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \
364 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
365 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
366 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
367 Dest.AggregateVal[_i].IntVal = APInt(1, \
368 Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
369 break;
370
371#define IMPLEMENT_VECTOR_FCMP(OP) \
372 case Type::FixedVectorTyID: \
373 case Type::ScalableVectorTyID: \
374 if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \
375 IMPLEMENT_VECTOR_FCMP_T(OP, Float); \
376 } else { \
377 IMPLEMENT_VECTOR_FCMP_T(OP, Double); \
378 }
379
380static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
381 Type *Ty) {
382 GenericValue Dest;
383 switch (Ty->getTypeID()) {
384 IMPLEMENT_FCMP(==, Float);
385 IMPLEMENT_FCMP(==, Double);
386 IMPLEMENT_VECTOR_FCMP(==);
387 default:
388 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
389 llvm_unreachable(nullptr);
390 }
391 return Dest;
392}
393
394#define IMPLEMENT_SCALAR_NANS(TY, X,Y) \
395 if (TY->isFloatTy()) { \
396 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
397 Dest.IntVal = APInt(1,false); \
398 return Dest; \
399 } \
400 } else { \
401 if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
402 Dest.IntVal = APInt(1,false); \
403 return Dest; \
404 } \
405 }
406
407#define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
408 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
409 Dest.AggregateVal.resize( X.AggregateVal.size() ); \
410 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
411 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
412 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
413 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
414 else { \
415 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
416 } \
417 }
418
419#define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
420 if (TY->isVectorTy()) { \
421 if (cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
422 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
423 } else { \
424 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
425 } \
426 } \
427
428
429
430static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
431 Type *Ty)
432{
433 GenericValue Dest;
434 // if input is scalar value and Src1 or Src2 is NaN return false
435 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
436 // if vector input detect NaNs and fill mask
437 MASK_VECTOR_NANS(Ty, Src1, Src2, false)
438 GenericValue DestMask = Dest;
439 switch (Ty->getTypeID()) {
440 IMPLEMENT_FCMP(!=, Float);
441 IMPLEMENT_FCMP(!=, Double);
442 IMPLEMENT_VECTOR_FCMP(!=);
443 default:
444 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
445 llvm_unreachable(nullptr);
446 }
447 // in vector case mask out NaN elements
448 if (Ty->isVectorTy())
449 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
450 if (DestMask.AggregateVal[_i].IntVal == false)
451 Dest.AggregateVal[_i].IntVal = APInt(1,false);
452
453 return Dest;
454}
455
456static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
457 Type *Ty) {
458 GenericValue Dest;
459 switch (Ty->getTypeID()) {
460 IMPLEMENT_FCMP(<=, Float);
461 IMPLEMENT_FCMP(<=, Double);
462 IMPLEMENT_VECTOR_FCMP(<=);
463 default:
464 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
465 llvm_unreachable(nullptr);
466 }
467 return Dest;
468}
469
470static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
471 Type *Ty) {
472 GenericValue Dest;
473 switch (Ty->getTypeID()) {
474 IMPLEMENT_FCMP(>=, Float);
475 IMPLEMENT_FCMP(>=, Double);
476 IMPLEMENT_VECTOR_FCMP(>=);
477 default:
478 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
479 llvm_unreachable(nullptr);
480 }
481 return Dest;
482}
483
484static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
485 Type *Ty) {
486 GenericValue Dest;
487 switch (Ty->getTypeID()) {
488 IMPLEMENT_FCMP(<, Float);
489 IMPLEMENT_FCMP(<, Double);
490 IMPLEMENT_VECTOR_FCMP(<);
491 default:
492 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
493 llvm_unreachable(nullptr);
494 }
495 return Dest;
496}
497
498static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
499 Type *Ty) {
500 GenericValue Dest;
501 switch (Ty->getTypeID()) {
502 IMPLEMENT_FCMP(>, Float);
503 IMPLEMENT_FCMP(>, Double);
504 IMPLEMENT_VECTOR_FCMP(>);
505 default:
506 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
507 llvm_unreachable(nullptr);
508 }
509 return Dest;
510}
511
512#define IMPLEMENT_UNORDERED(TY, X,Y) \
513 if (TY->isFloatTy()) { \
514 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
515 Dest.IntVal = APInt(1,true); \
516 return Dest; \
517 } \
518 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
519 Dest.IntVal = APInt(1,true); \
520 return Dest; \
521 }
522
523#define IMPLEMENT_VECTOR_UNORDERED(TY, X, Y, FUNC) \
524 if (TY->isVectorTy()) { \
525 GenericValue DestMask = Dest; \
526 Dest = FUNC(Src1, Src2, Ty); \
527 for (size_t _i = 0; _i < Src1.AggregateVal.size(); _i++) \
528 if (DestMask.AggregateVal[_i].IntVal == true) \
529 Dest.AggregateVal[_i].IntVal = APInt(1, true); \
530 return Dest; \
531 }
532
533static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
534 Type *Ty) {
535 GenericValue Dest;
536 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
537 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
538 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
539 return executeFCMP_OEQ(Src1, Src2, Ty);
540
541}
542
543static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
544 Type *Ty) {
545 GenericValue Dest;
546 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
547 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
548 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
549 return executeFCMP_ONE(Src1, Src2, Ty);
550}
551
552static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
553 Type *Ty) {
554 GenericValue Dest;
555 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
556 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
557 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
558 return executeFCMP_OLE(Src1, Src2, Ty);
559}
560
561static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
562 Type *Ty) {
563 GenericValue Dest;
564 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
565 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
566 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
567 return executeFCMP_OGE(Src1, Src2, Ty);
568}
569
570static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
571 Type *Ty) {
572 GenericValue Dest;
573 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
574 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
575 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
576 return executeFCMP_OLT(Src1, Src2, Ty);
577}
578
579static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
580 Type *Ty) {
581 GenericValue Dest;
582 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
583 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
584 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
585 return executeFCMP_OGT(Src1, Src2, Ty);
586}
587
588static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
589 Type *Ty) {
590 GenericValue Dest;
591 if(Ty->isVectorTy()) {
592 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
593 Dest.AggregateVal.resize( new_size: Src1.AggregateVal.size() );
594 if (cast<VectorType>(Val: Ty)->getElementType()->isFloatTy()) {
595 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
596 Dest.AggregateVal[_i].IntVal = APInt(1,
597 ( (Src1.AggregateVal[_i].FloatVal ==
598 Src1.AggregateVal[_i].FloatVal) &&
599 (Src2.AggregateVal[_i].FloatVal ==
600 Src2.AggregateVal[_i].FloatVal)));
601 } else {
602 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
603 Dest.AggregateVal[_i].IntVal = APInt(1,
604 ( (Src1.AggregateVal[_i].DoubleVal ==
605 Src1.AggregateVal[_i].DoubleVal) &&
606 (Src2.AggregateVal[_i].DoubleVal ==
607 Src2.AggregateVal[_i].DoubleVal)));
608 }
609 } else if (Ty->isFloatTy())
610 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
611 Src2.FloatVal == Src2.FloatVal));
612 else {
613 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
614 Src2.DoubleVal == Src2.DoubleVal));
615 }
616 return Dest;
617}
618
619static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
620 Type *Ty) {
621 GenericValue Dest;
622 if(Ty->isVectorTy()) {
623 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
624 Dest.AggregateVal.resize( new_size: Src1.AggregateVal.size() );
625 if (cast<VectorType>(Val: Ty)->getElementType()->isFloatTy()) {
626 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
627 Dest.AggregateVal[_i].IntVal = APInt(1,
628 ( (Src1.AggregateVal[_i].FloatVal !=
629 Src1.AggregateVal[_i].FloatVal) ||
630 (Src2.AggregateVal[_i].FloatVal !=
631 Src2.AggregateVal[_i].FloatVal)));
632 } else {
633 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
634 Dest.AggregateVal[_i].IntVal = APInt(1,
635 ( (Src1.AggregateVal[_i].DoubleVal !=
636 Src1.AggregateVal[_i].DoubleVal) ||
637 (Src2.AggregateVal[_i].DoubleVal !=
638 Src2.AggregateVal[_i].DoubleVal)));
639 }
640 } else if (Ty->isFloatTy())
641 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
642 Src2.FloatVal != Src2.FloatVal));
643 else {
644 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
645 Src2.DoubleVal != Src2.DoubleVal));
646 }
647 return Dest;
648}
649
650static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
651 Type *Ty, const bool val) {
652 GenericValue Dest;
653 if(Ty->isVectorTy()) {
654 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
655 Dest.AggregateVal.resize( new_size: Src1.AggregateVal.size() );
656 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
657 Dest.AggregateVal[_i].IntVal = APInt(1,val);
658 } else {
659 Dest.IntVal = APInt(1, val);
660 }
661
662 return Dest;
663}
664
665void Interpreter::visitFCmpInst(FCmpInst &I) {
666 ExecutionContext &SF = ECStack.back();
667 Type *Ty = I.getOperand(i_nocapture: 0)->getType();
668 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
669 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
670 GenericValue R; // Result
671
672 switch (I.getPredicate()) {
673 default:
674 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
675 llvm_unreachable(nullptr);
676 break;
677 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, val: false);
678 break;
679 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, val: true);
680 break;
681 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
682 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
683 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
684 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
685 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
686 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
687 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
688 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
689 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
690 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
691 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
692 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
693 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
694 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
695 }
696
697 SetValue(V: &I, Val: R, SF);
698}
699
700void Interpreter::visitBinaryOperator(BinaryOperator &I) {
701 ExecutionContext &SF = ECStack.back();
702 Type *Ty = I.getOperand(i_nocapture: 0)->getType();
703 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
704 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
705 GenericValue R; // Result
706
707 // First process vector operation
708 if (Ty->isVectorTy()) {
709 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
710 R.AggregateVal.resize(new_size: Src1.AggregateVal.size());
711
712 // Macros to execute binary operation 'OP' over integer vectors
713#define INTEGER_VECTOR_OPERATION(OP) \
714 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
715 R.AggregateVal[i].IntVal = \
716 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
717
718 // Additional macros to execute binary operations udiv/sdiv/urem/srem since
719 // they have different notation.
720#define INTEGER_VECTOR_FUNCTION(OP) \
721 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
722 R.AggregateVal[i].IntVal = \
723 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
724
725 // Macros to execute binary operation 'OP' over floating point type TY
726 // (float or double) vectors
727#define FLOAT_VECTOR_FUNCTION(OP, TY) \
728 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
729 R.AggregateVal[i].TY = \
730 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
731
732 // Macros to choose appropriate TY: float or double and run operation
733 // execution
734#define FLOAT_VECTOR_OP(OP) { \
735 if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
736 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
737 else { \
738 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
739 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
740 else { \
741 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
742 llvm_unreachable(0); \
743 } \
744 } \
745}
746
747 switch(I.getOpcode()){
748 default:
749 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
750 llvm_unreachable(nullptr);
751 break;
752 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;
753 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
754 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
755 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
756 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
757 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
758 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
759 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
760 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
761 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
762 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
763 case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
764 case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
765 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
766 case Instruction::FRem:
767 if (cast<VectorType>(Val: Ty)->getElementType()->isFloatTy())
768 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
769 R.AggregateVal[i].FloatVal =
770 fmod(x: Src1.AggregateVal[i].FloatVal, y: Src2.AggregateVal[i].FloatVal);
771 else {
772 if (cast<VectorType>(Val: Ty)->getElementType()->isDoubleTy())
773 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
774 R.AggregateVal[i].DoubleVal =
775 fmod(x: Src1.AggregateVal[i].DoubleVal, y: Src2.AggregateVal[i].DoubleVal);
776 else {
777 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
778 llvm_unreachable(nullptr);
779 }
780 }
781 break;
782 }
783 } else {
784 switch (I.getOpcode()) {
785 default:
786 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
787 llvm_unreachable(nullptr);
788 break;
789 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
790 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
791 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
792 case Instruction::FAdd: executeFAddInst(Dest&: R, Src1, Src2, Ty); break;
793 case Instruction::FSub: executeFSubInst(Dest&: R, Src1, Src2, Ty); break;
794 case Instruction::FMul: executeFMulInst(Dest&: R, Src1, Src2, Ty); break;
795 case Instruction::FDiv: executeFDivInst(Dest&: R, Src1, Src2, Ty); break;
796 case Instruction::FRem: executeFRemInst(Dest&: R, Src1, Src2, Ty); break;
797 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(RHS: Src2.IntVal); break;
798 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(RHS: Src2.IntVal); break;
799 case Instruction::URem: R.IntVal = Src1.IntVal.urem(RHS: Src2.IntVal); break;
800 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(RHS: Src2.IntVal); break;
801 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
802 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
803 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
804 }
805 }
806 SetValue(V: &I, Val: R, SF);
807}
808
809static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
810 GenericValue Src3, Type *Ty) {
811 GenericValue Dest;
812 if(Ty->isVectorTy()) {
813 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
814 assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());
815 Dest.AggregateVal.resize( new_size: Src1.AggregateVal.size() );
816 for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)
817 Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?
818 Src3.AggregateVal[i] : Src2.AggregateVal[i];
819 } else {
820 Dest = (Src1.IntVal == 0) ? Src3 : Src2;
821 }
822 return Dest;
823}
824
825void Interpreter::visitSelectInst(SelectInst &I) {
826 ExecutionContext &SF = ECStack.back();
827 Type * Ty = I.getOperand(i_nocapture: 0)->getType();
828 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
829 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
830 GenericValue Src3 = getOperandValue(V: I.getOperand(i_nocapture: 2), SF);
831 GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);
832 SetValue(V: &I, Val: R, SF);
833}
834
835//===----------------------------------------------------------------------===//
836// Terminator Instruction Implementations
837//===----------------------------------------------------------------------===//
838
839void Interpreter::exitCalled(GenericValue GV) {
840 // runAtExitHandlers() assumes there are no stack frames, but
841 // if exit() was called, then it had a stack frame. Blow away
842 // the stack before interpreting atexit handlers.
843 ECStack.clear();
844 runAtExitHandlers();
845 exit(status: GV.IntVal.zextOrTrunc(width: 32).getZExtValue());
846}
847
848/// Pop the last stack frame off of ECStack and then copy the result
849/// back into the result variable if we are not returning void. The
850/// result variable may be the ExitValue, or the Value of the calling
851/// CallInst if there was a previous stack frame. This method may
852/// invalidate any ECStack iterators you have. This method also takes
853/// care of switching to the normal destination BB, if we are returning
854/// from an invoke.
855///
856void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
857 GenericValue Result) {
858 // Pop the current stack frame.
859 ECStack.pop_back();
860
861 if (ECStack.empty()) { // Finished main. Put result into exit code...
862 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
863 ExitValue = std::move(Result); // Capture the exit value of the program
864 } else {
865 memset(s: &ExitValue.Untyped, c: 0, n: sizeof(ExitValue.Untyped));
866 }
867 } else {
868 // If we have a previous stack frame, and we have a previous call,
869 // fill in the return value...
870 ExecutionContext &CallingSF = ECStack.back();
871 if (CallingSF.Caller) {
872 // Save result...
873 if (!CallingSF.Caller->getType()->isVoidTy())
874 SetValue(V: CallingSF.Caller, Val: Result, SF&: CallingSF);
875 if (InvokeInst *II = dyn_cast<InvokeInst>(Val: CallingSF.Caller))
876 SwitchToNewBasicBlock (Dest: II->getNormalDest (), SF&: CallingSF);
877 CallingSF.Caller = nullptr; // We returned from the call...
878 }
879 }
880}
881
882void Interpreter::visitReturnInst(ReturnInst &I) {
883 ExecutionContext &SF = ECStack.back();
884 Type *RetTy = Type::getVoidTy(C&: I.getContext());
885 GenericValue Result;
886
887 // Save away the return value... (if we are not 'ret void')
888 if (I.getNumOperands()) {
889 RetTy = I.getReturnValue()->getType();
890 Result = getOperandValue(V: I.getReturnValue(), SF);
891 }
892
893 popStackAndReturnValueToCaller(RetTy, Result);
894}
895
896void Interpreter::visitUnreachableInst(UnreachableInst &I) {
897 report_fatal_error(reason: "Program executed an 'unreachable' instruction!");
898}
899
900void Interpreter::visitUncondBrInst(UncondBrInst &I) {
901 ExecutionContext &SF = ECStack.back();
902 SwitchToNewBasicBlock(Dest: I.getSuccessor(), SF);
903}
904
905void Interpreter::visitCondBrInst(CondBrInst &I) {
906 ExecutionContext &SF = ECStack.back();
907 bool Cond = getOperandValue(V: I.getCondition(), SF).IntVal != 0;
908 SwitchToNewBasicBlock(Dest: I.getSuccessor(i: Cond ? 0 : 1), SF);
909}
910
911void Interpreter::visitSwitchInst(SwitchInst &I) {
912 ExecutionContext &SF = ECStack.back();
913 Value* Cond = I.getCondition();
914 Type *ElTy = Cond->getType();
915 GenericValue CondVal = getOperandValue(V: Cond, SF);
916
917 // Check to see if any of the cases match...
918 BasicBlock *Dest = nullptr;
919 for (auto Case : I.cases()) {
920 GenericValue CaseVal = getOperandValue(V: Case.getCaseValue(), SF);
921 if (executeICMP_EQ(Src1: CondVal, Src2: CaseVal, Ty: ElTy).IntVal != 0) {
922 Dest = cast<BasicBlock>(Val: Case.getCaseSuccessor());
923 break;
924 }
925 }
926 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
927 SwitchToNewBasicBlock(Dest, SF);
928}
929
930void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
931 ExecutionContext &SF = ECStack.back();
932 void *Dest = GVTOP(GV: getOperandValue(V: I.getAddress(), SF));
933 SwitchToNewBasicBlock(Dest: (BasicBlock*)Dest, SF);
934}
935
936
937// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
938// This function handles the actual updating of block and instruction iterators
939// as well as execution of all of the PHI nodes in the destination block.
940//
941// This method does this because all of the PHI nodes must be executed
942// atomically, reading their inputs before any of the results are updated. Not
943// doing this can cause problems if the PHI nodes depend on other PHI nodes for
944// their inputs. If the input PHI node is updated before it is read, incorrect
945// results can happen. Thus we use a two phase approach.
946//
947void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
948 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
949 SF.CurBB = Dest; // Update CurBB to branch destination
950 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
951
952 if (!isa<PHINode>(Val: SF.CurInst)) return; // Nothing fancy to do
953
954 // Loop over all of the PHI nodes in the current block, reading their inputs.
955 std::vector<GenericValue> ResultValues;
956
957 for (; PHINode *PN = dyn_cast<PHINode>(Val&: SF.CurInst); ++SF.CurInst) {
958 // Search for the value corresponding to this previous bb...
959 int i = PN->getBasicBlockIndex(BB: PrevBB);
960 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
961 Value *IncomingValue = PN->getIncomingValue(i);
962
963 // Save the incoming value for this PHI node...
964 ResultValues.push_back(x: getOperandValue(V: IncomingValue, SF));
965 }
966
967 // Now loop over all of the PHI nodes setting their values...
968 SF.CurInst = SF.CurBB->begin();
969 for (unsigned i = 0; isa<PHINode>(Val: SF.CurInst); ++SF.CurInst, ++i) {
970 PHINode *PN = cast<PHINode>(Val&: SF.CurInst);
971 SetValue(V: PN, Val: ResultValues[i], SF);
972 }
973}
974
975//===----------------------------------------------------------------------===//
976// Memory Instruction Implementations
977//===----------------------------------------------------------------------===//
978
979void Interpreter::visitAllocaInst(AllocaInst &I) {
980 ExecutionContext &SF = ECStack.back();
981
982 Type *Ty = I.getAllocatedType(); // Type to be allocated
983
984 // Get the number of elements being allocated by the array...
985 unsigned NumElements =
986 getOperandValue(V: I.getOperand(i_nocapture: 0), SF).IntVal.getZExtValue();
987
988 unsigned TypeSize = (size_t)getDataLayout().getTypeAllocSize(Ty);
989
990 // Avoid malloc-ing zero bytes, use max()...
991 unsigned MemToAlloc = std::max(a: 1U, b: NumElements * TypeSize);
992
993 // Allocate enough memory to hold the type...
994 void *Memory = safe_malloc(Sz: MemToAlloc);
995
996 LLVM_DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize
997 << " bytes) x " << NumElements << " (Total: " << MemToAlloc
998 << ") at " << uintptr_t(Memory) << '\n');
999
1000 GenericValue Result = PTOGV(P: Memory);
1001 assert(Result.PointerVal && "Null pointer returned by malloc!");
1002 SetValue(V: &I, Val: Result, SF);
1003
1004 if (I.getOpcode() == Instruction::Alloca)
1005 ECStack.back().Allocas.add(Mem: Memory);
1006}
1007
1008// getElementOffset - The workhorse for getelementptr.
1009//
1010GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1011 gep_type_iterator E,
1012 ExecutionContext &SF) {
1013 assert(Ptr->getType()->isPointerTy() &&
1014 "Cannot getElementOffset of a nonpointer type!");
1015
1016 uint64_t Total = 0;
1017
1018 for (; I != E; ++I) {
1019 if (StructType *STy = I.getStructTypeOrNull()) {
1020 const StructLayout *SLO = getDataLayout().getStructLayout(Ty: STy);
1021
1022 const ConstantInt *CPU = cast<ConstantInt>(Val: I.getOperand());
1023 unsigned Index = unsigned(CPU->getZExtValue());
1024
1025 Total += SLO->getElementOffset(Idx: Index);
1026 } else {
1027 // Get the index number for the array... which must be long type...
1028 GenericValue IdxGV = getOperandValue(V: I.getOperand(), SF);
1029
1030 int64_t Idx;
1031 unsigned BitWidth =
1032 cast<IntegerType>(Val: I.getOperand()->getType())->getBitWidth();
1033 if (BitWidth == 32)
1034 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
1035 else {
1036 assert(BitWidth == 64 && "Invalid index type for getelementptr");
1037 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
1038 }
1039 Total += I.getSequentialElementStride(DL: getDataLayout()) * Idx;
1040 }
1041 }
1042
1043 GenericValue Result;
1044 Result.PointerVal = ((char*)getOperandValue(V: Ptr, SF).PointerVal) + Total;
1045 LLVM_DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
1046 return Result;
1047}
1048
1049void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1050 ExecutionContext &SF = ECStack.back();
1051 SetValue(V: &I, Val: executeGEPOperation(Ptr: I.getPointerOperand(),
1052 I: gep_type_begin(GEP: I), E: gep_type_end(GEP: I), SF), SF);
1053}
1054
1055void Interpreter::visitLoadInst(LoadInst &I) {
1056 ExecutionContext &SF = ECStack.back();
1057 GenericValue SRC = getOperandValue(V: I.getPointerOperand(), SF);
1058 GenericValue *Ptr = (GenericValue*)GVTOP(GV: SRC);
1059 GenericValue Result;
1060 LoadValueFromMemory(Result, Ptr, Ty: I.getType());
1061 SetValue(V: &I, Val: Result, SF);
1062 if (I.isVolatile() && PrintVolatile)
1063 dbgs() << "Volatile load " << I;
1064}
1065
1066void Interpreter::visitStoreInst(StoreInst &I) {
1067 ExecutionContext &SF = ECStack.back();
1068 GenericValue Val = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1069 GenericValue SRC = getOperandValue(V: I.getPointerOperand(), SF);
1070 StoreValueToMemory(Val, Ptr: (GenericValue *)GVTOP(GV: SRC),
1071 Ty: I.getOperand(i_nocapture: 0)->getType());
1072 if (I.isVolatile() && PrintVolatile)
1073 dbgs() << "Volatile store: " << I;
1074}
1075
1076//===----------------------------------------------------------------------===//
1077// Miscellaneous Instruction Implementations
1078//===----------------------------------------------------------------------===//
1079
1080void Interpreter::visitVAStartInst(VAStartInst &I) {
1081 ExecutionContext &SF = ECStack.back();
1082 GenericValue ArgIndex;
1083 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1084 ArgIndex.UIntPairVal.second = 0;
1085 SetValue(V: &I, Val: ArgIndex, SF);
1086}
1087
1088void Interpreter::visitVAEndInst(VAEndInst &I) {
1089 // va_end is a noop for the interpreter
1090}
1091
1092void Interpreter::visitVACopyInst(VACopyInst &I) {
1093 ExecutionContext &SF = ECStack.back();
1094 SetValue(V: &I, Val: getOperandValue(V: *I.arg_begin(), SF), SF);
1095}
1096
1097void Interpreter::visitIntrinsicInst(IntrinsicInst &I) {
1098 ExecutionContext &SF = ECStack.back();
1099
1100 // If it is an unknown intrinsic function, use the intrinsic lowering
1101 // class to transform it into hopefully tasty LLVM code.
1102 //
1103 BasicBlock::iterator Me(&I);
1104 BasicBlock *Parent = I.getParent();
1105 bool atBegin(Parent->begin() == Me);
1106 if (!atBegin)
1107 --Me;
1108 IL->LowerIntrinsicCall(CI: &I);
1109
1110 // Restore the CurInst pointer to the first instruction newly inserted, if
1111 // any.
1112 if (atBegin) {
1113 SF.CurInst = Parent->begin();
1114 } else {
1115 SF.CurInst = Me;
1116 ++SF.CurInst;
1117 }
1118}
1119
1120void Interpreter::visitCallBase(CallBase &I) {
1121 ExecutionContext &SF = ECStack.back();
1122
1123 SF.Caller = &I;
1124 std::vector<GenericValue> ArgVals;
1125 const unsigned NumArgs = SF.Caller->arg_size();
1126 ArgVals.reserve(n: NumArgs);
1127 for (Value *V : SF.Caller->args())
1128 ArgVals.push_back(x: getOperandValue(V, SF));
1129
1130 // To handle indirect calls, we must get the pointer value from the argument
1131 // and treat it as a function pointer.
1132 GenericValue SRC = getOperandValue(V: SF.Caller->getCalledOperand(), SF);
1133 callFunction(F: (Function*)GVTOP(GV: SRC), ArgVals);
1134}
1135
1136// auxiliary function for shift operations
1137static unsigned getShiftAmount(uint64_t orgShiftAmount,
1138 llvm::APInt valueToShift) {
1139 unsigned valueWidth = valueToShift.getBitWidth();
1140 if (orgShiftAmount < (uint64_t)valueWidth)
1141 return orgShiftAmount;
1142 // according to the llvm documentation, if orgShiftAmount > valueWidth,
1143 // the result is undfeined. but we do shift by this rule:
1144 return (NextPowerOf2(A: valueWidth-1) - 1) & orgShiftAmount;
1145}
1146
1147
1148void Interpreter::visitShl(BinaryOperator &I) {
1149 ExecutionContext &SF = ECStack.back();
1150 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1151 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1152 GenericValue Dest;
1153 Type *Ty = I.getType();
1154
1155 if (Ty->isVectorTy()) {
1156 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1157 assert(src1Size == Src2.AggregateVal.size());
1158 for (unsigned i = 0; i < src1Size; i++) {
1159 GenericValue Result;
1160 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1161 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1162 Result.IntVal = valueToShift.shl(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1163 Dest.AggregateVal.push_back(x: Result);
1164 }
1165 } else {
1166 // scalar
1167 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1168 llvm::APInt valueToShift = Src1.IntVal;
1169 Dest.IntVal = valueToShift.shl(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1170 }
1171
1172 SetValue(V: &I, Val: Dest, SF);
1173}
1174
1175void Interpreter::visitLShr(BinaryOperator &I) {
1176 ExecutionContext &SF = ECStack.back();
1177 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1178 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1179 GenericValue Dest;
1180 Type *Ty = I.getType();
1181
1182 if (Ty->isVectorTy()) {
1183 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1184 assert(src1Size == Src2.AggregateVal.size());
1185 for (unsigned i = 0; i < src1Size; i++) {
1186 GenericValue Result;
1187 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1188 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1189 Result.IntVal = valueToShift.lshr(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1190 Dest.AggregateVal.push_back(x: Result);
1191 }
1192 } else {
1193 // scalar
1194 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1195 llvm::APInt valueToShift = Src1.IntVal;
1196 Dest.IntVal = valueToShift.lshr(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1197 }
1198
1199 SetValue(V: &I, Val: Dest, SF);
1200}
1201
1202void Interpreter::visitAShr(BinaryOperator &I) {
1203 ExecutionContext &SF = ECStack.back();
1204 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1205 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1206 GenericValue Dest;
1207 Type *Ty = I.getType();
1208
1209 if (Ty->isVectorTy()) {
1210 size_t src1Size = Src1.AggregateVal.size();
1211 assert(src1Size == Src2.AggregateVal.size());
1212 for (unsigned i = 0; i < src1Size; i++) {
1213 GenericValue Result;
1214 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1215 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1216 Result.IntVal = valueToShift.ashr(ShiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1217 Dest.AggregateVal.push_back(x: Result);
1218 }
1219 } else {
1220 // scalar
1221 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1222 llvm::APInt valueToShift = Src1.IntVal;
1223 Dest.IntVal = valueToShift.ashr(ShiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1224 }
1225
1226 SetValue(V: &I, Val: Dest, SF);
1227}
1228
1229GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
1230 ExecutionContext &SF) {
1231 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1232 Type *SrcTy = SrcVal->getType();
1233 if (SrcTy->isVectorTy()) {
1234 Type *DstVecTy = DstTy->getScalarType();
1235 unsigned DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1236 unsigned NumElts = Src.AggregateVal.size();
1237 // the sizes of src and dst vectors must be equal
1238 Dest.AggregateVal.resize(new_size: NumElts);
1239 for (unsigned i = 0; i < NumElts; i++)
1240 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(width: DBitWidth);
1241 } else {
1242 IntegerType *DITy = cast<IntegerType>(Val: DstTy);
1243 unsigned DBitWidth = DITy->getBitWidth();
1244 Dest.IntVal = Src.IntVal.trunc(width: DBitWidth);
1245 }
1246 return Dest;
1247}
1248
1249GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
1250 ExecutionContext &SF) {
1251 Type *SrcTy = SrcVal->getType();
1252 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1253 if (SrcTy->isVectorTy()) {
1254 Type *DstVecTy = DstTy->getScalarType();
1255 unsigned DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1256 unsigned size = Src.AggregateVal.size();
1257 // the sizes of src and dst vectors must be equal.
1258 Dest.AggregateVal.resize(new_size: size);
1259 for (unsigned i = 0; i < size; i++)
1260 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(width: DBitWidth);
1261 } else {
1262 auto *DITy = cast<IntegerType>(Val: DstTy);
1263 unsigned DBitWidth = DITy->getBitWidth();
1264 Dest.IntVal = Src.IntVal.sext(width: DBitWidth);
1265 }
1266 return Dest;
1267}
1268
1269GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
1270 ExecutionContext &SF) {
1271 Type *SrcTy = SrcVal->getType();
1272 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1273 if (SrcTy->isVectorTy()) {
1274 Type *DstVecTy = DstTy->getScalarType();
1275 unsigned DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1276
1277 unsigned size = Src.AggregateVal.size();
1278 // the sizes of src and dst vectors must be equal.
1279 Dest.AggregateVal.resize(new_size: size);
1280 for (unsigned i = 0; i < size; i++)
1281 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(width: DBitWidth);
1282 } else {
1283 auto *DITy = cast<IntegerType>(Val: DstTy);
1284 unsigned DBitWidth = DITy->getBitWidth();
1285 Dest.IntVal = Src.IntVal.zext(width: DBitWidth);
1286 }
1287 return Dest;
1288}
1289
1290GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
1291 ExecutionContext &SF) {
1292 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1293
1294 if (isa<VectorType>(Val: SrcVal->getType())) {
1295 assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
1296 DstTy->getScalarType()->isFloatTy() &&
1297 "Invalid FPTrunc instruction");
1298
1299 unsigned size = Src.AggregateVal.size();
1300 // the sizes of src and dst vectors must be equal.
1301 Dest.AggregateVal.resize(new_size: size);
1302 for (unsigned i = 0; i < size; i++)
1303 Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
1304 } else {
1305 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1306 "Invalid FPTrunc instruction");
1307 Dest.FloatVal = (float)Src.DoubleVal;
1308 }
1309
1310 return Dest;
1311}
1312
1313GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
1314 ExecutionContext &SF) {
1315 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1316
1317 if (isa<VectorType>(Val: SrcVal->getType())) {
1318 assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
1319 DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
1320
1321 unsigned size = Src.AggregateVal.size();
1322 // the sizes of src and dst vectors must be equal.
1323 Dest.AggregateVal.resize(new_size: size);
1324 for (unsigned i = 0; i < size; i++)
1325 Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
1326 } else {
1327 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1328 "Invalid FPExt instruction");
1329 Dest.DoubleVal = (double)Src.FloatVal;
1330 }
1331
1332 return Dest;
1333}
1334
1335GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
1336 ExecutionContext &SF) {
1337 Type *SrcTy = SrcVal->getType();
1338 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1339
1340 if (isa<VectorType>(Val: SrcTy)) {
1341 Type *DstVecTy = DstTy->getScalarType();
1342 Type *SrcVecTy = SrcTy->getScalarType();
1343 uint32_t DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1344 unsigned size = Src.AggregateVal.size();
1345 // the sizes of src and dst vectors must be equal.
1346 Dest.AggregateVal.resize(new_size: size);
1347
1348 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1349 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1350 for (unsigned i = 0; i < size; i++)
1351 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1352 Float: Src.AggregateVal[i].FloatVal, width: DBitWidth);
1353 } else {
1354 for (unsigned i = 0; i < size; i++)
1355 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1356 Double: Src.AggregateVal[i].DoubleVal, width: DBitWidth);
1357 }
1358 } else {
1359 // scalar
1360 uint32_t DBitWidth = cast<IntegerType>(Val: DstTy)->getBitWidth();
1361 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1362
1363 if (SrcTy->getTypeID() == Type::FloatTyID)
1364 Dest.IntVal = APIntOps::RoundFloatToAPInt(Float: Src.FloatVal, width: DBitWidth);
1365 else {
1366 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Double: Src.DoubleVal, width: DBitWidth);
1367 }
1368 }
1369
1370 return Dest;
1371}
1372
1373GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1374 ExecutionContext &SF) {
1375 Type *SrcTy = SrcVal->getType();
1376 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1377
1378 if (isa<VectorType>(Val: SrcTy)) {
1379 Type *DstVecTy = DstTy->getScalarType();
1380 Type *SrcVecTy = SrcTy->getScalarType();
1381 uint32_t DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1382 unsigned size = Src.AggregateVal.size();
1383 // the sizes of src and dst vectors must be equal
1384 Dest.AggregateVal.resize(new_size: size);
1385
1386 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1387 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1388 for (unsigned i = 0; i < size; i++)
1389 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1390 Float: Src.AggregateVal[i].FloatVal, width: DBitWidth);
1391 } else {
1392 for (unsigned i = 0; i < size; i++)
1393 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1394 Double: Src.AggregateVal[i].DoubleVal, width: DBitWidth);
1395 }
1396 } else {
1397 // scalar
1398 unsigned DBitWidth = cast<IntegerType>(Val: DstTy)->getBitWidth();
1399 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1400
1401 if (SrcTy->getTypeID() == Type::FloatTyID)
1402 Dest.IntVal = APIntOps::RoundFloatToAPInt(Float: Src.FloatVal, width: DBitWidth);
1403 else {
1404 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Double: Src.DoubleVal, width: DBitWidth);
1405 }
1406 }
1407 return Dest;
1408}
1409
1410GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1411 ExecutionContext &SF) {
1412 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1413
1414 if (isa<VectorType>(Val: SrcVal->getType())) {
1415 Type *DstVecTy = DstTy->getScalarType();
1416 unsigned size = Src.AggregateVal.size();
1417 // the sizes of src and dst vectors must be equal
1418 Dest.AggregateVal.resize(new_size: size);
1419
1420 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1421 assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1422 for (unsigned i = 0; i < size; i++)
1423 Dest.AggregateVal[i].FloatVal =
1424 APIntOps::RoundAPIntToFloat(APIVal: Src.AggregateVal[i].IntVal);
1425 } else {
1426 for (unsigned i = 0; i < size; i++)
1427 Dest.AggregateVal[i].DoubleVal =
1428 APIntOps::RoundAPIntToDouble(APIVal: Src.AggregateVal[i].IntVal);
1429 }
1430 } else {
1431 // scalar
1432 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1433 if (DstTy->getTypeID() == Type::FloatTyID)
1434 Dest.FloatVal = APIntOps::RoundAPIntToFloat(APIVal: Src.IntVal);
1435 else {
1436 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(APIVal: Src.IntVal);
1437 }
1438 }
1439 return Dest;
1440}
1441
1442GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1443 ExecutionContext &SF) {
1444 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1445
1446 if (isa<VectorType>(Val: SrcVal->getType())) {
1447 Type *DstVecTy = DstTy->getScalarType();
1448 unsigned size = Src.AggregateVal.size();
1449 // the sizes of src and dst vectors must be equal
1450 Dest.AggregateVal.resize(new_size: size);
1451
1452 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1453 assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1454 for (unsigned i = 0; i < size; i++)
1455 Dest.AggregateVal[i].FloatVal =
1456 APIntOps::RoundSignedAPIntToFloat(APIVal: Src.AggregateVal[i].IntVal);
1457 } else {
1458 for (unsigned i = 0; i < size; i++)
1459 Dest.AggregateVal[i].DoubleVal =
1460 APIntOps::RoundSignedAPIntToDouble(APIVal: Src.AggregateVal[i].IntVal);
1461 }
1462 } else {
1463 // scalar
1464 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1465
1466 if (DstTy->getTypeID() == Type::FloatTyID)
1467 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(APIVal: Src.IntVal);
1468 else {
1469 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(APIVal: Src.IntVal);
1470 }
1471 }
1472
1473 return Dest;
1474}
1475
1476GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1477 ExecutionContext &SF) {
1478 uint32_t DBitWidth = cast<IntegerType>(Val: DstTy)->getBitWidth();
1479 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1480 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1481
1482 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1483 return Dest;
1484}
1485
1486GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1487 ExecutionContext &SF) {
1488 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1489 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1490
1491 uint32_t PtrSize = getDataLayout().getPointerSizeInBits();
1492 if (PtrSize != Src.IntVal.getBitWidth())
1493 Src.IntVal = Src.IntVal.zextOrTrunc(width: PtrSize);
1494
1495 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1496 return Dest;
1497}
1498
1499GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1500 ExecutionContext &SF) {
1501
1502 // This instruction supports bitwise conversion of vectors to integers and
1503 // to vectors of other types (as long as they have the same size)
1504 Type *SrcTy = SrcVal->getType();
1505 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1506
1507 if (isa<VectorType>(Val: SrcTy) || isa<VectorType>(Val: DstTy)) {
1508 // vector src bitcast to vector dst or vector src bitcast to scalar dst or
1509 // scalar src bitcast to vector dst
1510 bool isLittleEndian = getDataLayout().isLittleEndian();
1511 GenericValue TempDst, TempSrc, SrcVec;
1512 Type *SrcElemTy;
1513 Type *DstElemTy;
1514 unsigned SrcBitSize;
1515 unsigned DstBitSize;
1516 unsigned SrcNum;
1517 unsigned DstNum;
1518
1519 if (isa<VectorType>(Val: SrcTy)) {
1520 SrcElemTy = SrcTy->getScalarType();
1521 SrcBitSize = SrcTy->getScalarSizeInBits();
1522 SrcNum = Src.AggregateVal.size();
1523 SrcVec = Src;
1524 } else {
1525 // if src is scalar value, make it vector <1 x type>
1526 SrcElemTy = SrcTy;
1527 SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1528 SrcNum = 1;
1529 SrcVec.AggregateVal.push_back(x: Src);
1530 }
1531
1532 if (isa<VectorType>(Val: DstTy)) {
1533 DstElemTy = DstTy->getScalarType();
1534 DstBitSize = DstTy->getScalarSizeInBits();
1535 DstNum = (SrcNum * SrcBitSize) / DstBitSize;
1536 } else {
1537 DstElemTy = DstTy;
1538 DstBitSize = DstTy->getPrimitiveSizeInBits();
1539 DstNum = 1;
1540 }
1541
1542 if (SrcNum * SrcBitSize != DstNum * DstBitSize)
1543 llvm_unreachable("Invalid BitCast");
1544
1545 // If src is floating point, cast to integer first.
1546 TempSrc.AggregateVal.resize(new_size: SrcNum);
1547 if (SrcElemTy->isFloatTy()) {
1548 for (unsigned i = 0; i < SrcNum; i++)
1549 TempSrc.AggregateVal[i].IntVal =
1550 APInt::floatToBits(V: SrcVec.AggregateVal[i].FloatVal);
1551
1552 } else if (SrcElemTy->isDoubleTy()) {
1553 for (unsigned i = 0; i < SrcNum; i++)
1554 TempSrc.AggregateVal[i].IntVal =
1555 APInt::doubleToBits(V: SrcVec.AggregateVal[i].DoubleVal);
1556 } else if (SrcElemTy->isIntegerTy()) {
1557 for (unsigned i = 0; i < SrcNum; i++)
1558 TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
1559 } else {
1560 // Pointers are not allowed as the element type of vector.
1561 llvm_unreachable("Invalid Bitcast");
1562 }
1563
1564 // now TempSrc is integer type vector
1565 if (DstNum < SrcNum) {
1566 // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
1567 unsigned Ratio = SrcNum / DstNum;
1568 unsigned SrcElt = 0;
1569 for (unsigned i = 0; i < DstNum; i++) {
1570 GenericValue Elt;
1571 Elt.IntVal = 0;
1572 Elt.IntVal = Elt.IntVal.zext(width: DstBitSize);
1573 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
1574 for (unsigned j = 0; j < Ratio; j++) {
1575 APInt Tmp;
1576 Tmp = Tmp.zext(width: SrcBitSize);
1577 Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
1578 Tmp = Tmp.zext(width: DstBitSize);
1579 Tmp <<= ShiftAmt;
1580 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
1581 Elt.IntVal |= Tmp;
1582 }
1583 TempDst.AggregateVal.push_back(x: Elt);
1584 }
1585 } else {
1586 // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
1587 unsigned Ratio = DstNum / SrcNum;
1588 for (unsigned i = 0; i < SrcNum; i++) {
1589 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
1590 for (unsigned j = 0; j < Ratio; j++) {
1591 GenericValue Elt;
1592 Elt.IntVal = Elt.IntVal.zext(width: SrcBitSize);
1593 Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
1594 Elt.IntVal.lshrInPlace(ShiftAmt);
1595 // it could be DstBitSize == SrcBitSize, so check it
1596 if (DstBitSize < SrcBitSize)
1597 Elt.IntVal = Elt.IntVal.trunc(width: DstBitSize);
1598 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
1599 TempDst.AggregateVal.push_back(x: Elt);
1600 }
1601 }
1602 }
1603
1604 // convert result from integer to specified type
1605 if (isa<VectorType>(Val: DstTy)) {
1606 if (DstElemTy->isDoubleTy()) {
1607 Dest.AggregateVal.resize(new_size: DstNum);
1608 for (unsigned i = 0; i < DstNum; i++)
1609 Dest.AggregateVal[i].DoubleVal =
1610 TempDst.AggregateVal[i].IntVal.bitsToDouble();
1611 } else if (DstElemTy->isFloatTy()) {
1612 Dest.AggregateVal.resize(new_size: DstNum);
1613 for (unsigned i = 0; i < DstNum; i++)
1614 Dest.AggregateVal[i].FloatVal =
1615 TempDst.AggregateVal[i].IntVal.bitsToFloat();
1616 } else {
1617 Dest = TempDst;
1618 }
1619 } else {
1620 if (DstElemTy->isDoubleTy())
1621 Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
1622 else if (DstElemTy->isFloatTy()) {
1623 Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
1624 } else {
1625 Dest.IntVal = TempDst.AggregateVal[0].IntVal;
1626 }
1627 }
1628 } else { // if (isa<VectorType>(SrcTy)) || isa<VectorType>(DstTy))
1629
1630 // scalar src bitcast to scalar dst
1631 if (DstTy->isPointerTy()) {
1632 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1633 Dest.PointerVal = Src.PointerVal;
1634 } else if (DstTy->isIntegerTy()) {
1635 if (SrcTy->isFloatTy())
1636 Dest.IntVal = APInt::floatToBits(V: Src.FloatVal);
1637 else if (SrcTy->isDoubleTy()) {
1638 Dest.IntVal = APInt::doubleToBits(V: Src.DoubleVal);
1639 } else if (SrcTy->isIntegerTy()) {
1640 Dest.IntVal = Src.IntVal;
1641 } else {
1642 llvm_unreachable("Invalid BitCast");
1643 }
1644 } else if (DstTy->isFloatTy()) {
1645 if (SrcTy->isIntegerTy())
1646 Dest.FloatVal = Src.IntVal.bitsToFloat();
1647 else {
1648 Dest.FloatVal = Src.FloatVal;
1649 }
1650 } else if (DstTy->isDoubleTy()) {
1651 if (SrcTy->isIntegerTy())
1652 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1653 else {
1654 Dest.DoubleVal = Src.DoubleVal;
1655 }
1656 } else {
1657 llvm_unreachable("Invalid Bitcast");
1658 }
1659 }
1660
1661 return Dest;
1662}
1663
1664void Interpreter::visitTruncInst(TruncInst &I) {
1665 ExecutionContext &SF = ECStack.back();
1666 SetValue(V: &I, Val: executeTruncInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1667}
1668
1669void Interpreter::visitSExtInst(SExtInst &I) {
1670 ExecutionContext &SF = ECStack.back();
1671 SetValue(V: &I, Val: executeSExtInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1672}
1673
1674void Interpreter::visitZExtInst(ZExtInst &I) {
1675 ExecutionContext &SF = ECStack.back();
1676 SetValue(V: &I, Val: executeZExtInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1677}
1678
1679void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1680 ExecutionContext &SF = ECStack.back();
1681 SetValue(V: &I, Val: executeFPTruncInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1682}
1683
1684void Interpreter::visitFPExtInst(FPExtInst &I) {
1685 ExecutionContext &SF = ECStack.back();
1686 SetValue(V: &I, Val: executeFPExtInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1687}
1688
1689void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1690 ExecutionContext &SF = ECStack.back();
1691 SetValue(V: &I, Val: executeUIToFPInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1692}
1693
1694void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1695 ExecutionContext &SF = ECStack.back();
1696 SetValue(V: &I, Val: executeSIToFPInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1697}
1698
1699void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1700 ExecutionContext &SF = ECStack.back();
1701 SetValue(V: &I, Val: executeFPToUIInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1702}
1703
1704void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1705 ExecutionContext &SF = ECStack.back();
1706 SetValue(V: &I, Val: executeFPToSIInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1707}
1708
1709void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1710 ExecutionContext &SF = ECStack.back();
1711 SetValue(V: &I, Val: executePtrToIntInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1712}
1713
1714void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1715 ExecutionContext &SF = ECStack.back();
1716 SetValue(V: &I, Val: executeIntToPtrInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1717}
1718
1719void Interpreter::visitBitCastInst(BitCastInst &I) {
1720 ExecutionContext &SF = ECStack.back();
1721 SetValue(V: &I, Val: executeBitCastInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1722}
1723
1724#define IMPLEMENT_VAARG(TY) \
1725 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1726
1727void Interpreter::visitVAArgInst(VAArgInst &I) {
1728 ExecutionContext &SF = ECStack.back();
1729
1730 // Get the incoming valist parameter. LLI treats the valist as a
1731 // (ec-stack-depth var-arg-index) pair.
1732 GenericValue VAList = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1733 GenericValue Dest;
1734 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1735 .VarArgs[VAList.UIntPairVal.second];
1736 Type *Ty = I.getType();
1737 switch (Ty->getTypeID()) {
1738 case Type::IntegerTyID:
1739 Dest.IntVal = Src.IntVal;
1740 break;
1741 IMPLEMENT_VAARG(Pointer);
1742 IMPLEMENT_VAARG(Float);
1743 IMPLEMENT_VAARG(Double);
1744 default:
1745 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1746 llvm_unreachable(nullptr);
1747 }
1748
1749 // Set the Value of this Instruction.
1750 SetValue(V: &I, Val: Dest, SF);
1751
1752 // Move the pointer to the next vararg.
1753 ++VAList.UIntPairVal.second;
1754}
1755
1756void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1757 ExecutionContext &SF = ECStack.back();
1758 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1759 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1760 GenericValue Dest;
1761
1762 Type *Ty = I.getType();
1763 const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1764
1765 if(Src1.AggregateVal.size() > indx) {
1766 switch (Ty->getTypeID()) {
1767 default:
1768 dbgs() << "Unhandled destination type for extractelement instruction: "
1769 << *Ty << "\n";
1770 llvm_unreachable(nullptr);
1771 break;
1772 case Type::IntegerTyID:
1773 Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1774 break;
1775 case Type::FloatTyID:
1776 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1777 break;
1778 case Type::DoubleTyID:
1779 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1780 break;
1781 }
1782 } else {
1783 dbgs() << "Invalid index in extractelement instruction\n";
1784 }
1785
1786 SetValue(V: &I, Val: Dest, SF);
1787}
1788
1789void Interpreter::visitInsertElementInst(InsertElementInst &I) {
1790 ExecutionContext &SF = ECStack.back();
1791 VectorType *Ty = cast<VectorType>(Val: I.getType());
1792
1793 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1794 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1795 GenericValue Src3 = getOperandValue(V: I.getOperand(i_nocapture: 2), SF);
1796 GenericValue Dest;
1797
1798 Type *TyContained = Ty->getElementType();
1799
1800 const unsigned indx = unsigned(Src3.IntVal.getZExtValue());
1801 Dest.AggregateVal = Src1.AggregateVal;
1802
1803 if(Src1.AggregateVal.size() <= indx)
1804 llvm_unreachable("Invalid index in insertelement instruction");
1805 switch (TyContained->getTypeID()) {
1806 default:
1807 llvm_unreachable("Unhandled dest type for insertelement instruction");
1808 case Type::IntegerTyID:
1809 Dest.AggregateVal[indx].IntVal = Src2.IntVal;
1810 break;
1811 case Type::FloatTyID:
1812 Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;
1813 break;
1814 case Type::DoubleTyID:
1815 Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;
1816 break;
1817 }
1818 SetValue(V: &I, Val: Dest, SF);
1819}
1820
1821void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){
1822 ExecutionContext &SF = ECStack.back();
1823
1824 VectorType *Ty = cast<VectorType>(Val: I.getType());
1825
1826 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1827 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1828 GenericValue Dest;
1829
1830 // There is no need to check types of src1 and src2, because the compiled
1831 // bytecode can't contain different types for src1 and src2 for a
1832 // shufflevector instruction.
1833
1834 Type *TyContained = Ty->getElementType();
1835 unsigned src1Size = (unsigned)Src1.AggregateVal.size();
1836 unsigned src2Size = (unsigned)Src2.AggregateVal.size();
1837 unsigned src3Size = I.getShuffleMask().size();
1838
1839 Dest.AggregateVal.resize(new_size: src3Size);
1840
1841 switch (TyContained->getTypeID()) {
1842 default:
1843 llvm_unreachable("Unhandled dest type for insertelement instruction");
1844 break;
1845 case Type::IntegerTyID:
1846 for( unsigned i=0; i<src3Size; i++) {
1847 unsigned j = std::max(a: 0, b: I.getMaskValue(Elt: i));
1848 if(j < src1Size)
1849 Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;
1850 else if(j < src1Size + src2Size)
1851 Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;
1852 else
1853 // The selector may not be greater than sum of lengths of first and
1854 // second operands and llasm should not allow situation like
1855 // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,
1856 // <2 x i32> < i32 0, i32 5 >,
1857 // where i32 5 is invalid, but let it be additional check here:
1858 llvm_unreachable("Invalid mask in shufflevector instruction");
1859 }
1860 break;
1861 case Type::FloatTyID:
1862 for( unsigned i=0; i<src3Size; i++) {
1863 unsigned j = std::max(a: 0, b: I.getMaskValue(Elt: i));
1864 if(j < src1Size)
1865 Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;
1866 else if(j < src1Size + src2Size)
1867 Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;
1868 else
1869 llvm_unreachable("Invalid mask in shufflevector instruction");
1870 }
1871 break;
1872 case Type::DoubleTyID:
1873 for( unsigned i=0; i<src3Size; i++) {
1874 unsigned j = std::max(a: 0, b: I.getMaskValue(Elt: i));
1875 if(j < src1Size)
1876 Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;
1877 else if(j < src1Size + src2Size)
1878 Dest.AggregateVal[i].DoubleVal =
1879 Src2.AggregateVal[j-src1Size].DoubleVal;
1880 else
1881 llvm_unreachable("Invalid mask in shufflevector instruction");
1882 }
1883 break;
1884 }
1885 SetValue(V: &I, Val: Dest, SF);
1886}
1887
1888void Interpreter::visitExtractValueInst(ExtractValueInst &I) {
1889 ExecutionContext &SF = ECStack.back();
1890 Value *Agg = I.getAggregateOperand();
1891 GenericValue Dest;
1892 GenericValue Src = getOperandValue(V: Agg, SF);
1893
1894 ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
1895 unsigned Num = I.getNumIndices();
1896 GenericValue *pSrc = &Src;
1897
1898 for (unsigned i = 0 ; i < Num; ++i) {
1899 pSrc = &pSrc->AggregateVal[*IdxBegin];
1900 ++IdxBegin;
1901 }
1902
1903 Type *IndexedType = ExtractValueInst::getIndexedType(Agg: Agg->getType(), Idxs: I.getIndices());
1904 switch (IndexedType->getTypeID()) {
1905 default:
1906 llvm_unreachable("Unhandled dest type for extractelement instruction");
1907 break;
1908 case Type::IntegerTyID:
1909 Dest.IntVal = pSrc->IntVal;
1910 break;
1911 case Type::FloatTyID:
1912 Dest.FloatVal = pSrc->FloatVal;
1913 break;
1914 case Type::DoubleTyID:
1915 Dest.DoubleVal = pSrc->DoubleVal;
1916 break;
1917 case Type::ArrayTyID:
1918 case Type::StructTyID:
1919 case Type::FixedVectorTyID:
1920 case Type::ScalableVectorTyID:
1921 Dest.AggregateVal = pSrc->AggregateVal;
1922 break;
1923 case Type::PointerTyID:
1924 Dest.PointerVal = pSrc->PointerVal;
1925 break;
1926 }
1927
1928 SetValue(V: &I, Val: Dest, SF);
1929}
1930
1931void Interpreter::visitInsertValueInst(InsertValueInst &I) {
1932
1933 ExecutionContext &SF = ECStack.back();
1934 Value *Agg = I.getAggregateOperand();
1935
1936 GenericValue Src1 = getOperandValue(V: Agg, SF);
1937 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1938 GenericValue Dest = Src1; // Dest is a slightly changed Src1
1939
1940 ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
1941 unsigned Num = I.getNumIndices();
1942
1943 GenericValue *pDest = &Dest;
1944 for (unsigned i = 0 ; i < Num; ++i) {
1945 pDest = &pDest->AggregateVal[*IdxBegin];
1946 ++IdxBegin;
1947 }
1948 // pDest points to the target value in the Dest now
1949
1950 Type *IndexedType = ExtractValueInst::getIndexedType(Agg: Agg->getType(), Idxs: I.getIndices());
1951
1952 switch (IndexedType->getTypeID()) {
1953 default:
1954 llvm_unreachable("Unhandled dest type for insertelement instruction");
1955 break;
1956 case Type::IntegerTyID:
1957 pDest->IntVal = Src2.IntVal;
1958 break;
1959 case Type::FloatTyID:
1960 pDest->FloatVal = Src2.FloatVal;
1961 break;
1962 case Type::DoubleTyID:
1963 pDest->DoubleVal = Src2.DoubleVal;
1964 break;
1965 case Type::ArrayTyID:
1966 case Type::StructTyID:
1967 case Type::FixedVectorTyID:
1968 case Type::ScalableVectorTyID:
1969 pDest->AggregateVal = Src2.AggregateVal;
1970 break;
1971 case Type::PointerTyID:
1972 pDest->PointerVal = Src2.PointerVal;
1973 break;
1974 }
1975
1976 SetValue(V: &I, Val: Dest, SF);
1977}
1978
1979GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1980 ExecutionContext &SF) {
1981 switch (CE->getOpcode()) {
1982 case Instruction::Trunc:
1983 return executeTruncInst(SrcVal: CE->getOperand(i_nocapture: 0), DstTy: CE->getType(), SF);
1984 case Instruction::PtrToInt:
1985 return executePtrToIntInst(SrcVal: CE->getOperand(i_nocapture: 0), DstTy: CE->getType(), SF);
1986 case Instruction::IntToPtr:
1987 return executeIntToPtrInst(SrcVal: CE->getOperand(i_nocapture: 0), DstTy: CE->getType(), SF);
1988 case Instruction::BitCast:
1989 return executeBitCastInst(SrcVal: CE->getOperand(i_nocapture: 0), DstTy: CE->getType(), SF);
1990 case Instruction::GetElementPtr:
1991 return executeGEPOperation(Ptr: CE->getOperand(i_nocapture: 0), I: gep_type_begin(GEP: CE),
1992 E: gep_type_end(GEP: CE), SF);
1993 break;
1994 }
1995
1996 // The cases below here require a GenericValue parameter for the result
1997 // so we initialize one, compute it and then return it.
1998 GenericValue Op0 = getOperandValue(V: CE->getOperand(i_nocapture: 0), SF);
1999 GenericValue Op1 = getOperandValue(V: CE->getOperand(i_nocapture: 1), SF);
2000 GenericValue Dest;
2001 switch (CE->getOpcode()) {
2002 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
2003 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
2004 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
2005 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
2006 case Instruction::Shl:
2007 Dest.IntVal = Op0.IntVal.shl(shiftAmt: Op1.IntVal.getZExtValue());
2008 break;
2009 default:
2010 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
2011 llvm_unreachable("Unhandled ConstantExpr");
2012 }
2013 return Dest;
2014}
2015
2016GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
2017 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: V)) {
2018 return getConstantExprValue(CE, SF);
2019 } else if (Constant *CPV = dyn_cast<Constant>(Val: V)) {
2020 return getConstantValue(C: CPV);
2021 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val: V)) {
2022 return PTOGV(P: getPointerToGlobal(GV));
2023 } else {
2024 return SF.Values[V];
2025 }
2026}
2027
2028//===----------------------------------------------------------------------===//
2029// Dispatch and Execution Code
2030//===----------------------------------------------------------------------===//
2031
2032//===----------------------------------------------------------------------===//
2033// callFunction - Execute the specified function...
2034//
2035void Interpreter::callFunction(Function *F, ArrayRef<GenericValue> ArgVals) {
2036 assert((ECStack.empty() || !ECStack.back().Caller ||
2037 ECStack.back().Caller->arg_size() == ArgVals.size()) &&
2038 "Incorrect number of arguments passed into function call!");
2039 // Make a new stack frame... and fill it in.
2040 ECStack.emplace_back();
2041 ExecutionContext &StackFrame = ECStack.back();
2042 StackFrame.CurFunction = F;
2043
2044 // Special handling for external functions.
2045 if (F->isDeclaration()) {
2046 GenericValue Result = callExternalFunction (F, ArgVals);
2047 // Simulate a 'ret' instruction of the appropriate type.
2048 popStackAndReturnValueToCaller (RetTy: F->getReturnType (), Result);
2049 return;
2050 }
2051
2052 // Get pointers to first LLVM BB & Instruction in function.
2053 StackFrame.CurBB = &F->front();
2054 StackFrame.CurInst = StackFrame.CurBB->begin();
2055
2056 // Run through the function arguments and initialize their values...
2057 assert((ArgVals.size() == F->arg_size() ||
2058 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
2059 "Invalid number of values passed to function invocation!");
2060
2061 // Handle non-varargs arguments...
2062 unsigned i = 0;
2063 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
2064 AI != E; ++AI, ++i)
2065 SetValue(V: &*AI, Val: ArgVals[i], SF&: StackFrame);
2066
2067 // Handle varargs arguments...
2068 StackFrame.VarArgs.assign(first: ArgVals.begin()+i, last: ArgVals.end());
2069}
2070
2071
2072void Interpreter::run() {
2073 while (!ECStack.empty()) {
2074 // Interpret a single instruction & increment the "PC".
2075 ExecutionContext &SF = ECStack.back(); // Current stack frame
2076 Instruction &I = *SF.CurInst++; // Increment before execute
2077
2078 // Track the number of dynamic instructions executed.
2079 ++NumDynamicInsts;
2080
2081 LLVM_DEBUG(dbgs() << "About to interpret: " << I << "\n");
2082 visit(I); // Dispatch to one of the visit* methods...
2083 }
2084}
2085