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 // Get the number of elements being allocated by the array...
983 unsigned NumElements =
984 getOperandValue(V: I.getOperand(i_nocapture: 0), SF).IntVal.getZExtValue();
985
986 unsigned TypeSize = (size_t)I.getAllocationBaseSize(DL: getDataLayout());
987
988 // Avoid malloc-ing zero bytes, use max()...
989 unsigned MemToAlloc = std::max(a: 1U, b: NumElements * TypeSize);
990
991 // Allocate enough memory to hold the type...
992 void *Memory = safe_malloc(Sz: MemToAlloc);
993
994 LLVM_DEBUG(dbgs() << "Allocation: (" << TypeSize << " bytes) x "
995 << NumElements << " (Total: " << MemToAlloc << ") at "
996 << uintptr_t(Memory) << '\n');
997
998 GenericValue Result = PTOGV(P: Memory);
999 assert(Result.PointerVal && "Null pointer returned by malloc!");
1000 SetValue(V: &I, Val: Result, SF);
1001
1002 if (I.getOpcode() == Instruction::Alloca)
1003 ECStack.back().Allocas.add(Mem: Memory);
1004}
1005
1006// getElementOffset - The workhorse for getelementptr.
1007//
1008GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
1009 gep_type_iterator E,
1010 ExecutionContext &SF) {
1011 assert(Ptr->getType()->isPointerTy() &&
1012 "Cannot getElementOffset of a nonpointer type!");
1013
1014 uint64_t Total = 0;
1015
1016 for (; I != E; ++I) {
1017 if (StructType *STy = I.getStructTypeOrNull()) {
1018 const StructLayout *SLO = getDataLayout().getStructLayout(Ty: STy);
1019
1020 const ConstantInt *CPU = cast<ConstantInt>(Val: I.getOperand());
1021 unsigned Index = unsigned(CPU->getZExtValue());
1022
1023 Total += SLO->getElementOffset(Idx: Index);
1024 } else {
1025 // Get the index number for the array... which must be long type...
1026 GenericValue IdxGV = getOperandValue(V: I.getOperand(), SF);
1027
1028 int64_t Idx;
1029 unsigned BitWidth =
1030 cast<IntegerType>(Val: I.getOperand()->getType())->getBitWidth();
1031 if (BitWidth == 32)
1032 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
1033 else {
1034 assert(BitWidth == 64 && "Invalid index type for getelementptr");
1035 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
1036 }
1037 Total += I.getSequentialElementStride(DL: getDataLayout()) * Idx;
1038 }
1039 }
1040
1041 GenericValue Result;
1042 Result.PointerVal = ((char*)getOperandValue(V: Ptr, SF).PointerVal) + Total;
1043 LLVM_DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
1044 return Result;
1045}
1046
1047void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1048 ExecutionContext &SF = ECStack.back();
1049 SetValue(V: &I, Val: executeGEPOperation(Ptr: I.getPointerOperand(),
1050 I: gep_type_begin(GEP: I), E: gep_type_end(GEP: I), SF), SF);
1051}
1052
1053void Interpreter::visitLoadInst(LoadInst &I) {
1054 ExecutionContext &SF = ECStack.back();
1055 GenericValue SRC = getOperandValue(V: I.getPointerOperand(), SF);
1056 GenericValue *Ptr = (GenericValue*)GVTOP(GV: SRC);
1057 GenericValue Result;
1058 LoadValueFromMemory(Result, Ptr, Ty: I.getType());
1059 SetValue(V: &I, Val: Result, SF);
1060 if (I.isVolatile() && PrintVolatile)
1061 dbgs() << "Volatile load " << I;
1062}
1063
1064void Interpreter::visitStoreInst(StoreInst &I) {
1065 ExecutionContext &SF = ECStack.back();
1066 GenericValue Val = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1067 GenericValue SRC = getOperandValue(V: I.getPointerOperand(), SF);
1068 StoreValueToMemory(Val, Ptr: (GenericValue *)GVTOP(GV: SRC),
1069 Ty: I.getOperand(i_nocapture: 0)->getType());
1070 if (I.isVolatile() && PrintVolatile)
1071 dbgs() << "Volatile store: " << I;
1072}
1073
1074//===----------------------------------------------------------------------===//
1075// Miscellaneous Instruction Implementations
1076//===----------------------------------------------------------------------===//
1077
1078void Interpreter::visitVAStartInst(VAStartInst &I) {
1079 ExecutionContext &SF = ECStack.back();
1080 GenericValue ArgIndex;
1081 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1082 ArgIndex.UIntPairVal.second = 0;
1083 SetValue(V: I.getArgList(), Val: ArgIndex, SF);
1084}
1085
1086void Interpreter::visitVAEndInst(VAEndInst &I) {
1087 // va_end is a noop for the interpreter
1088}
1089
1090void Interpreter::visitVACopyInst(VACopyInst &I) {
1091 ExecutionContext &SF = ECStack.back();
1092 SetValue(V: &I, Val: getOperandValue(V: *I.arg_begin(), SF), SF);
1093}
1094
1095void Interpreter::visitIntrinsicInst(IntrinsicInst &I) {
1096 ExecutionContext &SF = ECStack.back();
1097
1098 // If it is an unknown intrinsic function, use the intrinsic lowering
1099 // class to transform it into hopefully tasty LLVM code.
1100 //
1101 BasicBlock::iterator Me(&I);
1102 BasicBlock *Parent = I.getParent();
1103 bool atBegin(Parent->begin() == Me);
1104 if (!atBegin)
1105 --Me;
1106 IL->LowerIntrinsicCall(CI: &I);
1107
1108 // Restore the CurInst pointer to the first instruction newly inserted, if
1109 // any.
1110 if (atBegin) {
1111 SF.CurInst = Parent->begin();
1112 } else {
1113 SF.CurInst = Me;
1114 ++SF.CurInst;
1115 }
1116}
1117
1118void Interpreter::visitCallBase(CallBase &I) {
1119 ExecutionContext &SF = ECStack.back();
1120
1121 SF.Caller = &I;
1122 std::vector<GenericValue> ArgVals;
1123 const unsigned NumArgs = SF.Caller->arg_size();
1124 ArgVals.reserve(n: NumArgs);
1125 for (Value *V : SF.Caller->args())
1126 ArgVals.push_back(x: getOperandValue(V, SF));
1127
1128 // To handle indirect calls, we must get the pointer value from the argument
1129 // and treat it as a function pointer.
1130 GenericValue SRC = getOperandValue(V: SF.Caller->getCalledOperand(), SF);
1131 callFunction(F: (Function*)GVTOP(GV: SRC), ArgVals);
1132}
1133
1134// auxiliary function for shift operations
1135static unsigned getShiftAmount(uint64_t orgShiftAmount,
1136 llvm::APInt valueToShift) {
1137 unsigned valueWidth = valueToShift.getBitWidth();
1138 if (orgShiftAmount < (uint64_t)valueWidth)
1139 return orgShiftAmount;
1140 // according to the llvm documentation, if orgShiftAmount > valueWidth,
1141 // the result is undfeined. but we do shift by this rule:
1142 return (NextPowerOf2(A: valueWidth-1) - 1) & orgShiftAmount;
1143}
1144
1145
1146void Interpreter::visitShl(BinaryOperator &I) {
1147 ExecutionContext &SF = ECStack.back();
1148 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1149 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1150 GenericValue Dest;
1151 Type *Ty = I.getType();
1152
1153 if (Ty->isVectorTy()) {
1154 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1155 assert(src1Size == Src2.AggregateVal.size());
1156 for (unsigned i = 0; i < src1Size; i++) {
1157 GenericValue Result;
1158 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1159 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1160 Result.IntVal = valueToShift.shl(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1161 Dest.AggregateVal.push_back(x: Result);
1162 }
1163 } else {
1164 // scalar
1165 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1166 llvm::APInt valueToShift = Src1.IntVal;
1167 Dest.IntVal = valueToShift.shl(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1168 }
1169
1170 SetValue(V: &I, Val: Dest, SF);
1171}
1172
1173void Interpreter::visitLShr(BinaryOperator &I) {
1174 ExecutionContext &SF = ECStack.back();
1175 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1176 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1177 GenericValue Dest;
1178 Type *Ty = I.getType();
1179
1180 if (Ty->isVectorTy()) {
1181 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1182 assert(src1Size == Src2.AggregateVal.size());
1183 for (unsigned i = 0; i < src1Size; i++) {
1184 GenericValue Result;
1185 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1186 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1187 Result.IntVal = valueToShift.lshr(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1188 Dest.AggregateVal.push_back(x: Result);
1189 }
1190 } else {
1191 // scalar
1192 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1193 llvm::APInt valueToShift = Src1.IntVal;
1194 Dest.IntVal = valueToShift.lshr(shiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1195 }
1196
1197 SetValue(V: &I, Val: Dest, SF);
1198}
1199
1200void Interpreter::visitAShr(BinaryOperator &I) {
1201 ExecutionContext &SF = ECStack.back();
1202 GenericValue Src1 = getOperandValue(V: I.getOperand(i_nocapture: 0), SF);
1203 GenericValue Src2 = getOperandValue(V: I.getOperand(i_nocapture: 1), SF);
1204 GenericValue Dest;
1205 Type *Ty = I.getType();
1206
1207 if (Ty->isVectorTy()) {
1208 size_t src1Size = Src1.AggregateVal.size();
1209 assert(src1Size == Src2.AggregateVal.size());
1210 for (unsigned i = 0; i < src1Size; i++) {
1211 GenericValue Result;
1212 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1213 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1214 Result.IntVal = valueToShift.ashr(ShiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1215 Dest.AggregateVal.push_back(x: Result);
1216 }
1217 } else {
1218 // scalar
1219 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1220 llvm::APInt valueToShift = Src1.IntVal;
1221 Dest.IntVal = valueToShift.ashr(ShiftAmt: getShiftAmount(orgShiftAmount: shiftAmount, valueToShift));
1222 }
1223
1224 SetValue(V: &I, Val: Dest, SF);
1225}
1226
1227GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
1228 ExecutionContext &SF) {
1229 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1230 Type *SrcTy = SrcVal->getType();
1231 if (SrcTy->isVectorTy()) {
1232 Type *DstVecTy = DstTy->getScalarType();
1233 unsigned DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1234 unsigned NumElts = Src.AggregateVal.size();
1235 // the sizes of src and dst vectors must be equal
1236 Dest.AggregateVal.resize(new_size: NumElts);
1237 for (unsigned i = 0; i < NumElts; i++)
1238 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(width: DBitWidth);
1239 } else {
1240 IntegerType *DITy = cast<IntegerType>(Val: DstTy);
1241 unsigned DBitWidth = DITy->getBitWidth();
1242 Dest.IntVal = Src.IntVal.trunc(width: DBitWidth);
1243 }
1244 return Dest;
1245}
1246
1247GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
1248 ExecutionContext &SF) {
1249 Type *SrcTy = SrcVal->getType();
1250 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1251 if (SrcTy->isVectorTy()) {
1252 Type *DstVecTy = DstTy->getScalarType();
1253 unsigned DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1254 unsigned size = Src.AggregateVal.size();
1255 // the sizes of src and dst vectors must be equal.
1256 Dest.AggregateVal.resize(new_size: size);
1257 for (unsigned i = 0; i < size; i++)
1258 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(width: DBitWidth);
1259 } else {
1260 auto *DITy = cast<IntegerType>(Val: DstTy);
1261 unsigned DBitWidth = DITy->getBitWidth();
1262 Dest.IntVal = Src.IntVal.sext(width: DBitWidth);
1263 }
1264 return Dest;
1265}
1266
1267GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
1268 ExecutionContext &SF) {
1269 Type *SrcTy = SrcVal->getType();
1270 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1271 if (SrcTy->isVectorTy()) {
1272 Type *DstVecTy = DstTy->getScalarType();
1273 unsigned DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1274
1275 unsigned size = Src.AggregateVal.size();
1276 // the sizes of src and dst vectors must be equal.
1277 Dest.AggregateVal.resize(new_size: size);
1278 for (unsigned i = 0; i < size; i++)
1279 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(width: DBitWidth);
1280 } else {
1281 auto *DITy = cast<IntegerType>(Val: DstTy);
1282 unsigned DBitWidth = DITy->getBitWidth();
1283 Dest.IntVal = Src.IntVal.zext(width: DBitWidth);
1284 }
1285 return Dest;
1286}
1287
1288GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
1289 ExecutionContext &SF) {
1290 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1291
1292 if (isa<VectorType>(Val: SrcVal->getType())) {
1293 assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
1294 DstTy->getScalarType()->isFloatTy() &&
1295 "Invalid FPTrunc instruction");
1296
1297 unsigned size = Src.AggregateVal.size();
1298 // the sizes of src and dst vectors must be equal.
1299 Dest.AggregateVal.resize(new_size: size);
1300 for (unsigned i = 0; i < size; i++)
1301 Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
1302 } else {
1303 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1304 "Invalid FPTrunc instruction");
1305 Dest.FloatVal = (float)Src.DoubleVal;
1306 }
1307
1308 return Dest;
1309}
1310
1311GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
1312 ExecutionContext &SF) {
1313 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1314
1315 if (isa<VectorType>(Val: SrcVal->getType())) {
1316 assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
1317 DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
1318
1319 unsigned size = Src.AggregateVal.size();
1320 // the sizes of src and dst vectors must be equal.
1321 Dest.AggregateVal.resize(new_size: size);
1322 for (unsigned i = 0; i < size; i++)
1323 Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
1324 } else {
1325 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1326 "Invalid FPExt instruction");
1327 Dest.DoubleVal = (double)Src.FloatVal;
1328 }
1329
1330 return Dest;
1331}
1332
1333GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
1334 ExecutionContext &SF) {
1335 Type *SrcTy = SrcVal->getType();
1336 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1337
1338 if (isa<VectorType>(Val: SrcTy)) {
1339 Type *DstVecTy = DstTy->getScalarType();
1340 Type *SrcVecTy = SrcTy->getScalarType();
1341 uint32_t DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1342 unsigned size = Src.AggregateVal.size();
1343 // the sizes of src and dst vectors must be equal.
1344 Dest.AggregateVal.resize(new_size: size);
1345
1346 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1347 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1348 for (unsigned i = 0; i < size; i++)
1349 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1350 Float: Src.AggregateVal[i].FloatVal, width: DBitWidth);
1351 } else {
1352 for (unsigned i = 0; i < size; i++)
1353 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1354 Double: Src.AggregateVal[i].DoubleVal, width: DBitWidth);
1355 }
1356 } else {
1357 // scalar
1358 uint32_t DBitWidth = cast<IntegerType>(Val: DstTy)->getBitWidth();
1359 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1360
1361 if (SrcTy->getTypeID() == Type::FloatTyID)
1362 Dest.IntVal = APIntOps::RoundFloatToAPInt(Float: Src.FloatVal, width: DBitWidth);
1363 else {
1364 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Double: Src.DoubleVal, width: DBitWidth);
1365 }
1366 }
1367
1368 return Dest;
1369}
1370
1371GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1372 ExecutionContext &SF) {
1373 Type *SrcTy = SrcVal->getType();
1374 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1375
1376 if (isa<VectorType>(Val: SrcTy)) {
1377 Type *DstVecTy = DstTy->getScalarType();
1378 Type *SrcVecTy = SrcTy->getScalarType();
1379 uint32_t DBitWidth = cast<IntegerType>(Val: DstVecTy)->getBitWidth();
1380 unsigned size = Src.AggregateVal.size();
1381 // the sizes of src and dst vectors must be equal
1382 Dest.AggregateVal.resize(new_size: size);
1383
1384 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1385 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1386 for (unsigned i = 0; i < size; i++)
1387 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1388 Float: Src.AggregateVal[i].FloatVal, width: DBitWidth);
1389 } else {
1390 for (unsigned i = 0; i < size; i++)
1391 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1392 Double: Src.AggregateVal[i].DoubleVal, width: DBitWidth);
1393 }
1394 } else {
1395 // scalar
1396 unsigned DBitWidth = cast<IntegerType>(Val: DstTy)->getBitWidth();
1397 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1398
1399 if (SrcTy->getTypeID() == Type::FloatTyID)
1400 Dest.IntVal = APIntOps::RoundFloatToAPInt(Float: Src.FloatVal, width: DBitWidth);
1401 else {
1402 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Double: Src.DoubleVal, width: DBitWidth);
1403 }
1404 }
1405 return Dest;
1406}
1407
1408GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1409 ExecutionContext &SF) {
1410 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1411
1412 if (isa<VectorType>(Val: SrcVal->getType())) {
1413 Type *DstVecTy = DstTy->getScalarType();
1414 unsigned size = Src.AggregateVal.size();
1415 // the sizes of src and dst vectors must be equal
1416 Dest.AggregateVal.resize(new_size: size);
1417
1418 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1419 assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1420 for (unsigned i = 0; i < size; i++)
1421 Dest.AggregateVal[i].FloatVal =
1422 APIntOps::RoundAPIntToFloat(APIVal: Src.AggregateVal[i].IntVal);
1423 } else {
1424 for (unsigned i = 0; i < size; i++)
1425 Dest.AggregateVal[i].DoubleVal =
1426 APIntOps::RoundAPIntToDouble(APIVal: Src.AggregateVal[i].IntVal);
1427 }
1428 } else {
1429 // scalar
1430 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1431 if (DstTy->getTypeID() == Type::FloatTyID)
1432 Dest.FloatVal = APIntOps::RoundAPIntToFloat(APIVal: Src.IntVal);
1433 else {
1434 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(APIVal: Src.IntVal);
1435 }
1436 }
1437 return Dest;
1438}
1439
1440GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1441 ExecutionContext &SF) {
1442 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1443
1444 if (isa<VectorType>(Val: SrcVal->getType())) {
1445 Type *DstVecTy = DstTy->getScalarType();
1446 unsigned size = Src.AggregateVal.size();
1447 // the sizes of src and dst vectors must be equal
1448 Dest.AggregateVal.resize(new_size: size);
1449
1450 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1451 assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1452 for (unsigned i = 0; i < size; i++)
1453 Dest.AggregateVal[i].FloatVal =
1454 APIntOps::RoundSignedAPIntToFloat(APIVal: Src.AggregateVal[i].IntVal);
1455 } else {
1456 for (unsigned i = 0; i < size; i++)
1457 Dest.AggregateVal[i].DoubleVal =
1458 APIntOps::RoundSignedAPIntToDouble(APIVal: Src.AggregateVal[i].IntVal);
1459 }
1460 } else {
1461 // scalar
1462 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1463
1464 if (DstTy->getTypeID() == Type::FloatTyID)
1465 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(APIVal: Src.IntVal);
1466 else {
1467 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(APIVal: Src.IntVal);
1468 }
1469 }
1470
1471 return Dest;
1472}
1473
1474GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1475 ExecutionContext &SF) {
1476 uint32_t DBitWidth = cast<IntegerType>(Val: DstTy)->getBitWidth();
1477 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1478 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1479
1480 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1481 return Dest;
1482}
1483
1484GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1485 ExecutionContext &SF) {
1486 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1487 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1488
1489 uint32_t PtrSize = getDataLayout().getPointerSizeInBits();
1490 if (PtrSize != Src.IntVal.getBitWidth())
1491 Src.IntVal = Src.IntVal.zextOrTrunc(width: PtrSize);
1492
1493 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
1494 return Dest;
1495}
1496
1497GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1498 ExecutionContext &SF) {
1499
1500 // This instruction supports bitwise conversion of vectors to integers and
1501 // to vectors of other types (as long as they have the same size)
1502 Type *SrcTy = SrcVal->getType();
1503 GenericValue Dest, Src = getOperandValue(V: SrcVal, SF);
1504
1505 if (isa<VectorType>(Val: SrcTy) || isa<VectorType>(Val: DstTy)) {
1506 // vector src bitcast to vector dst or vector src bitcast to scalar dst or
1507 // scalar src bitcast to vector dst
1508 bool isLittleEndian = getDataLayout().isLittleEndian();
1509 GenericValue TempDst, TempSrc, SrcVec;
1510 Type *SrcElemTy;
1511 Type *DstElemTy;
1512 unsigned SrcBitSize;
1513 unsigned DstBitSize;
1514 unsigned SrcNum;
1515 unsigned DstNum;
1516
1517 if (isa<VectorType>(Val: SrcTy)) {
1518 SrcElemTy = SrcTy->getScalarType();
1519 SrcBitSize = SrcTy->getScalarSizeInBits();
1520 SrcNum = Src.AggregateVal.size();
1521 SrcVec = Src;
1522 } else {
1523 // if src is scalar value, make it vector <1 x type>
1524 SrcElemTy = SrcTy;
1525 SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1526 SrcNum = 1;
1527 SrcVec.AggregateVal.push_back(x: Src);
1528 }
1529
1530 if (isa<VectorType>(Val: DstTy)) {
1531 DstElemTy = DstTy->getScalarType();
1532 DstBitSize = DstTy->getScalarSizeInBits();
1533 DstNum = (SrcNum * SrcBitSize) / DstBitSize;
1534 } else {
1535 DstElemTy = DstTy;
1536 DstBitSize = DstTy->getPrimitiveSizeInBits();
1537 DstNum = 1;
1538 }
1539
1540 if (SrcNum * SrcBitSize != DstNum * DstBitSize)
1541 llvm_unreachable("Invalid BitCast");
1542
1543 // If src is floating point, cast to integer first.
1544 TempSrc.AggregateVal.resize(new_size: SrcNum);
1545 if (SrcElemTy->isFloatTy()) {
1546 for (unsigned i = 0; i < SrcNum; i++)
1547 TempSrc.AggregateVal[i].IntVal =
1548 APInt::floatToBits(V: SrcVec.AggregateVal[i].FloatVal);
1549
1550 } else if (SrcElemTy->isDoubleTy()) {
1551 for (unsigned i = 0; i < SrcNum; i++)
1552 TempSrc.AggregateVal[i].IntVal =
1553 APInt::doubleToBits(V: SrcVec.AggregateVal[i].DoubleVal);
1554 } else if (SrcElemTy->isIntegerTy()) {
1555 for (unsigned i = 0; i < SrcNum; i++)
1556 TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
1557 } else {
1558 // Pointers are not allowed as the element type of vector.
1559 llvm_unreachable("Invalid Bitcast");
1560 }
1561
1562 // now TempSrc is integer type vector
1563 if (DstNum < SrcNum) {
1564 // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
1565 unsigned Ratio = SrcNum / DstNum;
1566 unsigned SrcElt = 0;
1567 for (unsigned i = 0; i < DstNum; i++) {
1568 GenericValue Elt;
1569 Elt.IntVal = 0;
1570 Elt.IntVal = Elt.IntVal.zext(width: DstBitSize);
1571 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
1572 for (unsigned j = 0; j < Ratio; j++) {
1573 APInt Tmp;
1574 Tmp = Tmp.zext(width: SrcBitSize);
1575 Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
1576 Tmp = Tmp.zext(width: DstBitSize);
1577 Tmp <<= ShiftAmt;
1578 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
1579 Elt.IntVal |= Tmp;
1580 }
1581 TempDst.AggregateVal.push_back(x: Elt);
1582 }
1583 } else {
1584 // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
1585 unsigned Ratio = DstNum / SrcNum;
1586 for (unsigned i = 0; i < SrcNum; i++) {
1587 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
1588 for (unsigned j = 0; j < Ratio; j++) {
1589 GenericValue Elt;
1590 Elt.IntVal = Elt.IntVal.zext(width: SrcBitSize);
1591 Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
1592 Elt.IntVal.lshrInPlace(ShiftAmt);
1593 // it could be DstBitSize == SrcBitSize, so check it
1594 if (DstBitSize < SrcBitSize)
1595 Elt.IntVal = Elt.IntVal.trunc(width: DstBitSize);
1596 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
1597 TempDst.AggregateVal.push_back(x: Elt);
1598 }
1599 }
1600 }
1601
1602 // convert result from integer to specified type
1603 if (isa<VectorType>(Val: DstTy)) {
1604 if (DstElemTy->isDoubleTy()) {
1605 Dest.AggregateVal.resize(new_size: DstNum);
1606 for (unsigned i = 0; i < DstNum; i++)
1607 Dest.AggregateVal[i].DoubleVal =
1608 TempDst.AggregateVal[i].IntVal.bitsToDouble();
1609 } else if (DstElemTy->isFloatTy()) {
1610 Dest.AggregateVal.resize(new_size: DstNum);
1611 for (unsigned i = 0; i < DstNum; i++)
1612 Dest.AggregateVal[i].FloatVal =
1613 TempDst.AggregateVal[i].IntVal.bitsToFloat();
1614 } else {
1615 Dest = TempDst;
1616 }
1617 } else {
1618 if (DstElemTy->isDoubleTy())
1619 Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
1620 else if (DstElemTy->isFloatTy()) {
1621 Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
1622 } else {
1623 Dest.IntVal = TempDst.AggregateVal[0].IntVal;
1624 }
1625 }
1626 } else { // if (isa<VectorType>(SrcTy)) || isa<VectorType>(DstTy))
1627
1628 // scalar src bitcast to scalar dst
1629 if (DstTy->isPointerTy()) {
1630 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1631 Dest.PointerVal = Src.PointerVal;
1632 } else if (DstTy->isIntegerTy()) {
1633 if (SrcTy->isFloatTy())
1634 Dest.IntVal = APInt::floatToBits(V: Src.FloatVal);
1635 else if (SrcTy->isDoubleTy()) {
1636 Dest.IntVal = APInt::doubleToBits(V: Src.DoubleVal);
1637 } else if (SrcTy->isIntegerTy()) {
1638 Dest.IntVal = Src.IntVal;
1639 } else {
1640 llvm_unreachable("Invalid BitCast");
1641 }
1642 } else if (DstTy->isFloatTy()) {
1643 if (SrcTy->isIntegerTy())
1644 Dest.FloatVal = Src.IntVal.bitsToFloat();
1645 else {
1646 Dest.FloatVal = Src.FloatVal;
1647 }
1648 } else if (DstTy->isDoubleTy()) {
1649 if (SrcTy->isIntegerTy())
1650 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1651 else {
1652 Dest.DoubleVal = Src.DoubleVal;
1653 }
1654 } else {
1655 llvm_unreachable("Invalid Bitcast");
1656 }
1657 }
1658
1659 return Dest;
1660}
1661
1662void Interpreter::visitTruncInst(TruncInst &I) {
1663 ExecutionContext &SF = ECStack.back();
1664 SetValue(V: &I, Val: executeTruncInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1665}
1666
1667void Interpreter::visitSExtInst(SExtInst &I) {
1668 ExecutionContext &SF = ECStack.back();
1669 SetValue(V: &I, Val: executeSExtInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1670}
1671
1672void Interpreter::visitZExtInst(ZExtInst &I) {
1673 ExecutionContext &SF = ECStack.back();
1674 SetValue(V: &I, Val: executeZExtInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1675}
1676
1677void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1678 ExecutionContext &SF = ECStack.back();
1679 SetValue(V: &I, Val: executeFPTruncInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1680}
1681
1682void Interpreter::visitFPExtInst(FPExtInst &I) {
1683 ExecutionContext &SF = ECStack.back();
1684 SetValue(V: &I, Val: executeFPExtInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1685}
1686
1687void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1688 ExecutionContext &SF = ECStack.back();
1689 SetValue(V: &I, Val: executeUIToFPInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1690}
1691
1692void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1693 ExecutionContext &SF = ECStack.back();
1694 SetValue(V: &I, Val: executeSIToFPInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1695}
1696
1697void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1698 ExecutionContext &SF = ECStack.back();
1699 SetValue(V: &I, Val: executeFPToUIInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1700}
1701
1702void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1703 ExecutionContext &SF = ECStack.back();
1704 SetValue(V: &I, Val: executeFPToSIInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1705}
1706
1707void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1708 ExecutionContext &SF = ECStack.back();
1709 SetValue(V: &I, Val: executePtrToIntInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1710}
1711
1712void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1713 ExecutionContext &SF = ECStack.back();
1714 SetValue(V: &I, Val: executeIntToPtrInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1715}
1716
1717void Interpreter::visitBitCastInst(BitCastInst &I) {
1718 ExecutionContext &SF = ECStack.back();
1719 SetValue(V: &I, Val: executeBitCastInst(SrcVal: I.getOperand(i_nocapture: 0), DstTy: I.getType(), SF), SF);
1720}
1721
1722#define IMPLEMENT_VAARG(TY) \
1723 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1724
1725void Interpreter::visitVAArgInst(VAArgInst &I) {
1726 ExecutionContext &SF = ECStack.back();
1727
1728 // Get the incoming valist parameter. LLI treats the valist as a
1729 // (ec-stack-depth var-arg-index) pair.
1730 Value *V = I.getOperand(i_nocapture: 0);
1731 GenericValue VAList = getOperandValue(V, SF);
1732 GenericValue Dest;
1733 GenericValue Src = ECStack[VAList.UIntPairVal.first]
1734 .VarArgs[VAList.UIntPairVal.second];
1735 Type *Ty = I.getType();
1736 switch (Ty->getTypeID()) {
1737 case Type::IntegerTyID:
1738 Dest.IntVal = Src.IntVal;
1739 break;
1740 IMPLEMENT_VAARG(Pointer);
1741 IMPLEMENT_VAARG(Float);
1742 IMPLEMENT_VAARG(Double);
1743 default:
1744 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1745 llvm_unreachable(nullptr);
1746 }
1747
1748 // Set the Value of this Instruction.
1749 SetValue(V: &I, Val: Dest, SF);
1750
1751 // Move the pointer to the next vararg and set new value back.
1752 ++VAList.UIntPairVal.second;
1753 SetValue(V, Val: VAList, SF);
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