Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
35271 views
//===-- Execution.cpp - Implement code to simulate the program ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the actual instruction interpreter.9//10//===----------------------------------------------------------------------===//1112#include "Interpreter.h"13#include "llvm/ADT/APInt.h"14#include "llvm/ADT/Statistic.h"15#include "llvm/CodeGen/IntrinsicLowering.h"16#include "llvm/IR/Constants.h"17#include "llvm/IR/DerivedTypes.h"18#include "llvm/IR/GetElementPtrTypeIterator.h"19#include "llvm/IR/Instructions.h"20#include "llvm/Support/CommandLine.h"21#include "llvm/Support/Debug.h"22#include "llvm/Support/ErrorHandling.h"23#include "llvm/Support/MathExtras.h"24#include "llvm/Support/raw_ostream.h"25#include <algorithm>26#include <cmath>27using namespace llvm;2829#define DEBUG_TYPE "interpreter"3031STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");3233static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,34cl::desc("make the interpreter print every volatile load and store"));3536//===----------------------------------------------------------------------===//37// Various Helper Functions38//===----------------------------------------------------------------------===//3940static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {41SF.Values[V] = Val;42}4344//===----------------------------------------------------------------------===//45// Unary Instruction Implementations46//===----------------------------------------------------------------------===//4748static void executeFNegInst(GenericValue &Dest, GenericValue Src, Type *Ty) {49switch (Ty->getTypeID()) {50case Type::FloatTyID:51Dest.FloatVal = -Src.FloatVal;52break;53case Type::DoubleTyID:54Dest.DoubleVal = -Src.DoubleVal;55break;56default:57llvm_unreachable("Unhandled type for FNeg instruction");58}59}6061void Interpreter::visitUnaryOperator(UnaryOperator &I) {62ExecutionContext &SF = ECStack.back();63Type *Ty = I.getOperand(0)->getType();64GenericValue Src = getOperandValue(I.getOperand(0), SF);65GenericValue R; // Result6667// First process vector operation68if (Ty->isVectorTy()) {69R.AggregateVal.resize(Src.AggregateVal.size());7071switch(I.getOpcode()) {72default:73llvm_unreachable("Don't know how to handle this unary operator");74break;75case Instruction::FNeg:76if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {77for (unsigned i = 0; i < R.AggregateVal.size(); ++i)78R.AggregateVal[i].FloatVal = -Src.AggregateVal[i].FloatVal;79} else if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) {80for (unsigned i = 0; i < R.AggregateVal.size(); ++i)81R.AggregateVal[i].DoubleVal = -Src.AggregateVal[i].DoubleVal;82} else {83llvm_unreachable("Unhandled type for FNeg instruction");84}85break;86}87} else {88switch (I.getOpcode()) {89default:90llvm_unreachable("Don't know how to handle this unary operator");91break;92case Instruction::FNeg: executeFNegInst(R, Src, Ty); break;93}94}95SetValue(&I, R, SF);96}9798//===----------------------------------------------------------------------===//99// Binary Instruction Implementations100//===----------------------------------------------------------------------===//101102#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \103case Type::TY##TyID: \104Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \105break106107static void executeFAddInst(GenericValue &Dest, GenericValue Src1,108GenericValue Src2, Type *Ty) {109switch (Ty->getTypeID()) {110IMPLEMENT_BINARY_OPERATOR(+, Float);111IMPLEMENT_BINARY_OPERATOR(+, Double);112default:113dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";114llvm_unreachable(nullptr);115}116}117118static void executeFSubInst(GenericValue &Dest, GenericValue Src1,119GenericValue Src2, Type *Ty) {120switch (Ty->getTypeID()) {121IMPLEMENT_BINARY_OPERATOR(-, Float);122IMPLEMENT_BINARY_OPERATOR(-, Double);123default:124dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";125llvm_unreachable(nullptr);126}127}128129static void executeFMulInst(GenericValue &Dest, GenericValue Src1,130GenericValue Src2, Type *Ty) {131switch (Ty->getTypeID()) {132IMPLEMENT_BINARY_OPERATOR(*, Float);133IMPLEMENT_BINARY_OPERATOR(*, Double);134default:135dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";136llvm_unreachable(nullptr);137}138}139140static void executeFDivInst(GenericValue &Dest, GenericValue Src1,141GenericValue Src2, Type *Ty) {142switch (Ty->getTypeID()) {143IMPLEMENT_BINARY_OPERATOR(/, Float);144IMPLEMENT_BINARY_OPERATOR(/, Double);145default:146dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";147llvm_unreachable(nullptr);148}149}150151static void executeFRemInst(GenericValue &Dest, GenericValue Src1,152GenericValue Src2, Type *Ty) {153switch (Ty->getTypeID()) {154case Type::FloatTyID:155Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);156break;157case Type::DoubleTyID:158Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);159break;160default:161dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";162llvm_unreachable(nullptr);163}164}165166#define IMPLEMENT_INTEGER_ICMP(OP, TY) \167case Type::IntegerTyID: \168Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \169break;170171#define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \172case Type::FixedVectorTyID: \173case Type::ScalableVectorTyID: { \174assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \175Dest.AggregateVal.resize(Src1.AggregateVal.size()); \176for (uint32_t _i = 0; _i < Src1.AggregateVal.size(); _i++) \177Dest.AggregateVal[_i].IntVal = APInt( \1781, Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal)); \179} break;180181// Handle pointers specially because they must be compared with only as much182// width as the host has. We _do not_ want to be comparing 64 bit values when183// running on a 32-bit target, otherwise the upper 32 bits might mess up184// comparisons if they contain garbage.185#define IMPLEMENT_POINTER_ICMP(OP) \186case Type::PointerTyID: \187Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \188(void*)(intptr_t)Src2.PointerVal); \189break;190191static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,192Type *Ty) {193GenericValue Dest;194switch (Ty->getTypeID()) {195IMPLEMENT_INTEGER_ICMP(eq,Ty);196IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);197IMPLEMENT_POINTER_ICMP(==);198default:199dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";200llvm_unreachable(nullptr);201}202return Dest;203}204205static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,206Type *Ty) {207GenericValue Dest;208switch (Ty->getTypeID()) {209IMPLEMENT_INTEGER_ICMP(ne,Ty);210IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);211IMPLEMENT_POINTER_ICMP(!=);212default:213dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";214llvm_unreachable(nullptr);215}216return Dest;217}218219static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,220Type *Ty) {221GenericValue Dest;222switch (Ty->getTypeID()) {223IMPLEMENT_INTEGER_ICMP(ult,Ty);224IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);225IMPLEMENT_POINTER_ICMP(<);226default:227dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";228llvm_unreachable(nullptr);229}230return Dest;231}232233static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,234Type *Ty) {235GenericValue Dest;236switch (Ty->getTypeID()) {237IMPLEMENT_INTEGER_ICMP(slt,Ty);238IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);239IMPLEMENT_POINTER_ICMP(<);240default:241dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";242llvm_unreachable(nullptr);243}244return Dest;245}246247static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,248Type *Ty) {249GenericValue Dest;250switch (Ty->getTypeID()) {251IMPLEMENT_INTEGER_ICMP(ugt,Ty);252IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);253IMPLEMENT_POINTER_ICMP(>);254default:255dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";256llvm_unreachable(nullptr);257}258return Dest;259}260261static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,262Type *Ty) {263GenericValue Dest;264switch (Ty->getTypeID()) {265IMPLEMENT_INTEGER_ICMP(sgt,Ty);266IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);267IMPLEMENT_POINTER_ICMP(>);268default:269dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";270llvm_unreachable(nullptr);271}272return Dest;273}274275static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,276Type *Ty) {277GenericValue Dest;278switch (Ty->getTypeID()) {279IMPLEMENT_INTEGER_ICMP(ule,Ty);280IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);281IMPLEMENT_POINTER_ICMP(<=);282default:283dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";284llvm_unreachable(nullptr);285}286return Dest;287}288289static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,290Type *Ty) {291GenericValue Dest;292switch (Ty->getTypeID()) {293IMPLEMENT_INTEGER_ICMP(sle,Ty);294IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);295IMPLEMENT_POINTER_ICMP(<=);296default:297dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";298llvm_unreachable(nullptr);299}300return Dest;301}302303static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,304Type *Ty) {305GenericValue Dest;306switch (Ty->getTypeID()) {307IMPLEMENT_INTEGER_ICMP(uge,Ty);308IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);309IMPLEMENT_POINTER_ICMP(>=);310default:311dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";312llvm_unreachable(nullptr);313}314return Dest;315}316317static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,318Type *Ty) {319GenericValue Dest;320switch (Ty->getTypeID()) {321IMPLEMENT_INTEGER_ICMP(sge,Ty);322IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);323IMPLEMENT_POINTER_ICMP(>=);324default:325dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";326llvm_unreachable(nullptr);327}328return Dest;329}330331void Interpreter::visitICmpInst(ICmpInst &I) {332ExecutionContext &SF = ECStack.back();333Type *Ty = I.getOperand(0)->getType();334GenericValue Src1 = getOperandValue(I.getOperand(0), SF);335GenericValue Src2 = getOperandValue(I.getOperand(1), SF);336GenericValue R; // Result337338switch (I.getPredicate()) {339case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;340case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;341case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;342case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;343case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;344case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;345case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;346case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;347case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;348case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;349default:350dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;351llvm_unreachable(nullptr);352}353354SetValue(&I, R, SF);355}356357#define IMPLEMENT_FCMP(OP, TY) \358case Type::TY##TyID: \359Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \360break361362#define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \363assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \364Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \365for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \366Dest.AggregateVal[_i].IntVal = APInt(1, \367Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\368break;369370#define IMPLEMENT_VECTOR_FCMP(OP) \371case Type::FixedVectorTyID: \372case Type::ScalableVectorTyID: \373if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \374IMPLEMENT_VECTOR_FCMP_T(OP, Float); \375} else { \376IMPLEMENT_VECTOR_FCMP_T(OP, Double); \377}378379static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,380Type *Ty) {381GenericValue Dest;382switch (Ty->getTypeID()) {383IMPLEMENT_FCMP(==, Float);384IMPLEMENT_FCMP(==, Double);385IMPLEMENT_VECTOR_FCMP(==);386default:387dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";388llvm_unreachable(nullptr);389}390return Dest;391}392393#define IMPLEMENT_SCALAR_NANS(TY, X,Y) \394if (TY->isFloatTy()) { \395if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \396Dest.IntVal = APInt(1,false); \397return Dest; \398} \399} else { \400if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \401Dest.IntVal = APInt(1,false); \402return Dest; \403} \404}405406#define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \407assert(X.AggregateVal.size() == Y.AggregateVal.size()); \408Dest.AggregateVal.resize( X.AggregateVal.size() ); \409for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \410if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \411Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \412Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \413else { \414Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \415} \416}417418#define MASK_VECTOR_NANS(TY, X,Y, FLAG) \419if (TY->isVectorTy()) { \420if (cast<VectorType>(TY)->getElementType()->isFloatTy()) { \421MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \422} else { \423MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \424} \425} \426427428429static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,430Type *Ty)431{432GenericValue Dest;433// if input is scalar value and Src1 or Src2 is NaN return false434IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)435// if vector input detect NaNs and fill mask436MASK_VECTOR_NANS(Ty, Src1, Src2, false)437GenericValue DestMask = Dest;438switch (Ty->getTypeID()) {439IMPLEMENT_FCMP(!=, Float);440IMPLEMENT_FCMP(!=, Double);441IMPLEMENT_VECTOR_FCMP(!=);442default:443dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";444llvm_unreachable(nullptr);445}446// in vector case mask out NaN elements447if (Ty->isVectorTy())448for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)449if (DestMask.AggregateVal[_i].IntVal == false)450Dest.AggregateVal[_i].IntVal = APInt(1,false);451452return Dest;453}454455static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,456Type *Ty) {457GenericValue Dest;458switch (Ty->getTypeID()) {459IMPLEMENT_FCMP(<=, Float);460IMPLEMENT_FCMP(<=, Double);461IMPLEMENT_VECTOR_FCMP(<=);462default:463dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";464llvm_unreachable(nullptr);465}466return Dest;467}468469static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,470Type *Ty) {471GenericValue Dest;472switch (Ty->getTypeID()) {473IMPLEMENT_FCMP(>=, Float);474IMPLEMENT_FCMP(>=, Double);475IMPLEMENT_VECTOR_FCMP(>=);476default:477dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";478llvm_unreachable(nullptr);479}480return Dest;481}482483static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,484Type *Ty) {485GenericValue Dest;486switch (Ty->getTypeID()) {487IMPLEMENT_FCMP(<, Float);488IMPLEMENT_FCMP(<, Double);489IMPLEMENT_VECTOR_FCMP(<);490default:491dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";492llvm_unreachable(nullptr);493}494return Dest;495}496497static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,498Type *Ty) {499GenericValue Dest;500switch (Ty->getTypeID()) {501IMPLEMENT_FCMP(>, Float);502IMPLEMENT_FCMP(>, Double);503IMPLEMENT_VECTOR_FCMP(>);504default:505dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";506llvm_unreachable(nullptr);507}508return Dest;509}510511#define IMPLEMENT_UNORDERED(TY, X,Y) \512if (TY->isFloatTy()) { \513if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \514Dest.IntVal = APInt(1,true); \515return Dest; \516} \517} else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \518Dest.IntVal = APInt(1,true); \519return Dest; \520}521522#define IMPLEMENT_VECTOR_UNORDERED(TY, X, Y, FUNC) \523if (TY->isVectorTy()) { \524GenericValue DestMask = Dest; \525Dest = FUNC(Src1, Src2, Ty); \526for (size_t _i = 0; _i < Src1.AggregateVal.size(); _i++) \527if (DestMask.AggregateVal[_i].IntVal == true) \528Dest.AggregateVal[_i].IntVal = APInt(1, true); \529return Dest; \530}531532static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,533Type *Ty) {534GenericValue Dest;535IMPLEMENT_UNORDERED(Ty, Src1, Src2)536MASK_VECTOR_NANS(Ty, Src1, Src2, true)537IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)538return executeFCMP_OEQ(Src1, Src2, Ty);539540}541542static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,543Type *Ty) {544GenericValue Dest;545IMPLEMENT_UNORDERED(Ty, Src1, Src2)546MASK_VECTOR_NANS(Ty, Src1, Src2, true)547IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)548return executeFCMP_ONE(Src1, Src2, Ty);549}550551static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,552Type *Ty) {553GenericValue Dest;554IMPLEMENT_UNORDERED(Ty, Src1, Src2)555MASK_VECTOR_NANS(Ty, Src1, Src2, true)556IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)557return executeFCMP_OLE(Src1, Src2, Ty);558}559560static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,561Type *Ty) {562GenericValue Dest;563IMPLEMENT_UNORDERED(Ty, Src1, Src2)564MASK_VECTOR_NANS(Ty, Src1, Src2, true)565IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)566return executeFCMP_OGE(Src1, Src2, Ty);567}568569static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,570Type *Ty) {571GenericValue Dest;572IMPLEMENT_UNORDERED(Ty, Src1, Src2)573MASK_VECTOR_NANS(Ty, Src1, Src2, true)574IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)575return executeFCMP_OLT(Src1, Src2, Ty);576}577578static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,579Type *Ty) {580GenericValue Dest;581IMPLEMENT_UNORDERED(Ty, Src1, Src2)582MASK_VECTOR_NANS(Ty, Src1, Src2, true)583IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)584return executeFCMP_OGT(Src1, Src2, Ty);585}586587static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,588Type *Ty) {589GenericValue Dest;590if(Ty->isVectorTy()) {591assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());592Dest.AggregateVal.resize( Src1.AggregateVal.size() );593if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {594for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)595Dest.AggregateVal[_i].IntVal = APInt(1,596( (Src1.AggregateVal[_i].FloatVal ==597Src1.AggregateVal[_i].FloatVal) &&598(Src2.AggregateVal[_i].FloatVal ==599Src2.AggregateVal[_i].FloatVal)));600} else {601for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)602Dest.AggregateVal[_i].IntVal = APInt(1,603( (Src1.AggregateVal[_i].DoubleVal ==604Src1.AggregateVal[_i].DoubleVal) &&605(Src2.AggregateVal[_i].DoubleVal ==606Src2.AggregateVal[_i].DoubleVal)));607}608} else if (Ty->isFloatTy())609Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&610Src2.FloatVal == Src2.FloatVal));611else {612Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&613Src2.DoubleVal == Src2.DoubleVal));614}615return Dest;616}617618static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,619Type *Ty) {620GenericValue Dest;621if(Ty->isVectorTy()) {622assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());623Dest.AggregateVal.resize( Src1.AggregateVal.size() );624if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {625for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)626Dest.AggregateVal[_i].IntVal = APInt(1,627( (Src1.AggregateVal[_i].FloatVal !=628Src1.AggregateVal[_i].FloatVal) ||629(Src2.AggregateVal[_i].FloatVal !=630Src2.AggregateVal[_i].FloatVal)));631} else {632for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)633Dest.AggregateVal[_i].IntVal = APInt(1,634( (Src1.AggregateVal[_i].DoubleVal !=635Src1.AggregateVal[_i].DoubleVal) ||636(Src2.AggregateVal[_i].DoubleVal !=637Src2.AggregateVal[_i].DoubleVal)));638}639} else if (Ty->isFloatTy())640Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||641Src2.FloatVal != Src2.FloatVal));642else {643Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||644Src2.DoubleVal != Src2.DoubleVal));645}646return Dest;647}648649static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,650Type *Ty, const bool val) {651GenericValue Dest;652if(Ty->isVectorTy()) {653assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());654Dest.AggregateVal.resize( Src1.AggregateVal.size() );655for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)656Dest.AggregateVal[_i].IntVal = APInt(1,val);657} else {658Dest.IntVal = APInt(1, val);659}660661return Dest;662}663664void Interpreter::visitFCmpInst(FCmpInst &I) {665ExecutionContext &SF = ECStack.back();666Type *Ty = I.getOperand(0)->getType();667GenericValue Src1 = getOperandValue(I.getOperand(0), SF);668GenericValue Src2 = getOperandValue(I.getOperand(1), SF);669GenericValue R; // Result670671switch (I.getPredicate()) {672default:673dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;674llvm_unreachable(nullptr);675break;676case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);677break;678case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);679break;680case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;681case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;682case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;683case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;684case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;685case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;686case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;687case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;688case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;689case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;690case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;691case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;692case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;693case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;694}695696SetValue(&I, R, SF);697}698699void Interpreter::visitBinaryOperator(BinaryOperator &I) {700ExecutionContext &SF = ECStack.back();701Type *Ty = I.getOperand(0)->getType();702GenericValue Src1 = getOperandValue(I.getOperand(0), SF);703GenericValue Src2 = getOperandValue(I.getOperand(1), SF);704GenericValue R; // Result705706// First process vector operation707if (Ty->isVectorTy()) {708assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());709R.AggregateVal.resize(Src1.AggregateVal.size());710711// Macros to execute binary operation 'OP' over integer vectors712#define INTEGER_VECTOR_OPERATION(OP) \713for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \714R.AggregateVal[i].IntVal = \715Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;716717// Additional macros to execute binary operations udiv/sdiv/urem/srem since718// they have different notation.719#define INTEGER_VECTOR_FUNCTION(OP) \720for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \721R.AggregateVal[i].IntVal = \722Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);723724// Macros to execute binary operation 'OP' over floating point type TY725// (float or double) vectors726#define FLOAT_VECTOR_FUNCTION(OP, TY) \727for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \728R.AggregateVal[i].TY = \729Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;730731// Macros to choose appropriate TY: float or double and run operation732// execution733#define FLOAT_VECTOR_OP(OP) { \734if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) \735FLOAT_VECTOR_FUNCTION(OP, FloatVal) \736else { \737if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \738FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \739else { \740dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \741llvm_unreachable(0); \742} \743} \744}745746switch(I.getOpcode()){747default:748dbgs() << "Don't know how to handle this binary operator!\n-->" << I;749llvm_unreachable(nullptr);750break;751case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;752case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;753case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;754case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;755case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;756case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;757case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;758case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;759case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;760case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;761case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;762case Instruction::FSub: FLOAT_VECTOR_OP(-) break;763case Instruction::FMul: FLOAT_VECTOR_OP(*) break;764case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;765case Instruction::FRem:766if (cast<VectorType>(Ty)->getElementType()->isFloatTy())767for (unsigned i = 0; i < R.AggregateVal.size(); ++i)768R.AggregateVal[i].FloatVal =769fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);770else {771if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())772for (unsigned i = 0; i < R.AggregateVal.size(); ++i)773R.AggregateVal[i].DoubleVal =774fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);775else {776dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";777llvm_unreachable(nullptr);778}779}780break;781}782} else {783switch (I.getOpcode()) {784default:785dbgs() << "Don't know how to handle this binary operator!\n-->" << I;786llvm_unreachable(nullptr);787break;788case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;789case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;790case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;791case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;792case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;793case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;794case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;795case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;796case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;797case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;798case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;799case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;800case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;801case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;802case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;803}804}805SetValue(&I, R, SF);806}807808static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,809GenericValue Src3, Type *Ty) {810GenericValue Dest;811if(Ty->isVectorTy()) {812assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());813assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());814Dest.AggregateVal.resize( Src1.AggregateVal.size() );815for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)816Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?817Src3.AggregateVal[i] : Src2.AggregateVal[i];818} else {819Dest = (Src1.IntVal == 0) ? Src3 : Src2;820}821return Dest;822}823824void Interpreter::visitSelectInst(SelectInst &I) {825ExecutionContext &SF = ECStack.back();826Type * Ty = I.getOperand(0)->getType();827GenericValue Src1 = getOperandValue(I.getOperand(0), SF);828GenericValue Src2 = getOperandValue(I.getOperand(1), SF);829GenericValue Src3 = getOperandValue(I.getOperand(2), SF);830GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);831SetValue(&I, R, SF);832}833834//===----------------------------------------------------------------------===//835// Terminator Instruction Implementations836//===----------------------------------------------------------------------===//837838void Interpreter::exitCalled(GenericValue GV) {839// runAtExitHandlers() assumes there are no stack frames, but840// if exit() was called, then it had a stack frame. Blow away841// the stack before interpreting atexit handlers.842ECStack.clear();843runAtExitHandlers();844exit(GV.IntVal.zextOrTrunc(32).getZExtValue());845}846847/// Pop the last stack frame off of ECStack and then copy the result848/// back into the result variable if we are not returning void. The849/// result variable may be the ExitValue, or the Value of the calling850/// CallInst if there was a previous stack frame. This method may851/// invalidate any ECStack iterators you have. This method also takes852/// care of switching to the normal destination BB, if we are returning853/// from an invoke.854///855void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,856GenericValue Result) {857// Pop the current stack frame.858ECStack.pop_back();859860if (ECStack.empty()) { // Finished main. Put result into exit code...861if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?862ExitValue = Result; // Capture the exit value of the program863} else {864memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));865}866} else {867// If we have a previous stack frame, and we have a previous call,868// fill in the return value...869ExecutionContext &CallingSF = ECStack.back();870if (CallingSF.Caller) {871// Save result...872if (!CallingSF.Caller->getType()->isVoidTy())873SetValue(CallingSF.Caller, Result, CallingSF);874if (InvokeInst *II = dyn_cast<InvokeInst>(CallingSF.Caller))875SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);876CallingSF.Caller = nullptr; // We returned from the call...877}878}879}880881void Interpreter::visitReturnInst(ReturnInst &I) {882ExecutionContext &SF = ECStack.back();883Type *RetTy = Type::getVoidTy(I.getContext());884GenericValue Result;885886// Save away the return value... (if we are not 'ret void')887if (I.getNumOperands()) {888RetTy = I.getReturnValue()->getType();889Result = getOperandValue(I.getReturnValue(), SF);890}891892popStackAndReturnValueToCaller(RetTy, Result);893}894895void Interpreter::visitUnreachableInst(UnreachableInst &I) {896report_fatal_error("Program executed an 'unreachable' instruction!");897}898899void Interpreter::visitBranchInst(BranchInst &I) {900ExecutionContext &SF = ECStack.back();901BasicBlock *Dest;902903Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...904if (!I.isUnconditional()) {905Value *Cond = I.getCondition();906if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...907Dest = I.getSuccessor(1);908}909SwitchToNewBasicBlock(Dest, SF);910}911912void Interpreter::visitSwitchInst(SwitchInst &I) {913ExecutionContext &SF = ECStack.back();914Value* Cond = I.getCondition();915Type *ElTy = Cond->getType();916GenericValue CondVal = getOperandValue(Cond, SF);917918// Check to see if any of the cases match...919BasicBlock *Dest = nullptr;920for (auto Case : I.cases()) {921GenericValue CaseVal = getOperandValue(Case.getCaseValue(), SF);922if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {923Dest = cast<BasicBlock>(Case.getCaseSuccessor());924break;925}926}927if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default928SwitchToNewBasicBlock(Dest, SF);929}930931void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {932ExecutionContext &SF = ECStack.back();933void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));934SwitchToNewBasicBlock((BasicBlock*)Dest, SF);935}936937938// SwitchToNewBasicBlock - This method is used to jump to a new basic block.939// This function handles the actual updating of block and instruction iterators940// as well as execution of all of the PHI nodes in the destination block.941//942// This method does this because all of the PHI nodes must be executed943// atomically, reading their inputs before any of the results are updated. Not944// doing this can cause problems if the PHI nodes depend on other PHI nodes for945// their inputs. If the input PHI node is updated before it is read, incorrect946// results can happen. Thus we use a two phase approach.947//948void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){949BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...950SF.CurBB = Dest; // Update CurBB to branch destination951SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...952953if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do954955// Loop over all of the PHI nodes in the current block, reading their inputs.956std::vector<GenericValue> ResultValues;957958for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {959// Search for the value corresponding to this previous bb...960int i = PN->getBasicBlockIndex(PrevBB);961assert(i != -1 && "PHINode doesn't contain entry for predecessor??");962Value *IncomingValue = PN->getIncomingValue(i);963964// Save the incoming value for this PHI node...965ResultValues.push_back(getOperandValue(IncomingValue, SF));966}967968// Now loop over all of the PHI nodes setting their values...969SF.CurInst = SF.CurBB->begin();970for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {971PHINode *PN = cast<PHINode>(SF.CurInst);972SetValue(PN, ResultValues[i], SF);973}974}975976//===----------------------------------------------------------------------===//977// Memory Instruction Implementations978//===----------------------------------------------------------------------===//979980void Interpreter::visitAllocaInst(AllocaInst &I) {981ExecutionContext &SF = ECStack.back();982983Type *Ty = I.getAllocatedType(); // Type to be allocated984985// Get the number of elements being allocated by the array...986unsigned NumElements =987getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();988989unsigned TypeSize = (size_t)getDataLayout().getTypeAllocSize(Ty);990991// Avoid malloc-ing zero bytes, use max()...992unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);993994// Allocate enough memory to hold the type...995void *Memory = safe_malloc(MemToAlloc);996997LLVM_DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize998<< " bytes) x " << NumElements << " (Total: " << MemToAlloc999<< ") at " << uintptr_t(Memory) << '\n');10001001GenericValue Result = PTOGV(Memory);1002assert(Result.PointerVal && "Null pointer returned by malloc!");1003SetValue(&I, Result, SF);10041005if (I.getOpcode() == Instruction::Alloca)1006ECStack.back().Allocas.add(Memory);1007}10081009// getElementOffset - The workhorse for getelementptr.1010//1011GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,1012gep_type_iterator E,1013ExecutionContext &SF) {1014assert(Ptr->getType()->isPointerTy() &&1015"Cannot getElementOffset of a nonpointer type!");10161017uint64_t Total = 0;10181019for (; I != E; ++I) {1020if (StructType *STy = I.getStructTypeOrNull()) {1021const StructLayout *SLO = getDataLayout().getStructLayout(STy);10221023const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());1024unsigned Index = unsigned(CPU->getZExtValue());10251026Total += SLO->getElementOffset(Index);1027} else {1028// Get the index number for the array... which must be long type...1029GenericValue IdxGV = getOperandValue(I.getOperand(), SF);10301031int64_t Idx;1032unsigned BitWidth =1033cast<IntegerType>(I.getOperand()->getType())->getBitWidth();1034if (BitWidth == 32)1035Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();1036else {1037assert(BitWidth == 64 && "Invalid index type for getelementptr");1038Idx = (int64_t)IdxGV.IntVal.getZExtValue();1039}1040Total += I.getSequentialElementStride(getDataLayout()) * Idx;1041}1042}10431044GenericValue Result;1045Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;1046LLVM_DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");1047return Result;1048}10491050void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {1051ExecutionContext &SF = ECStack.back();1052SetValue(&I, executeGEPOperation(I.getPointerOperand(),1053gep_type_begin(I), gep_type_end(I), SF), SF);1054}10551056void Interpreter::visitLoadInst(LoadInst &I) {1057ExecutionContext &SF = ECStack.back();1058GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);1059GenericValue *Ptr = (GenericValue*)GVTOP(SRC);1060GenericValue Result;1061LoadValueFromMemory(Result, Ptr, I.getType());1062SetValue(&I, Result, SF);1063if (I.isVolatile() && PrintVolatile)1064dbgs() << "Volatile load " << I;1065}10661067void Interpreter::visitStoreInst(StoreInst &I) {1068ExecutionContext &SF = ECStack.back();1069GenericValue Val = getOperandValue(I.getOperand(0), SF);1070GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);1071StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),1072I.getOperand(0)->getType());1073if (I.isVolatile() && PrintVolatile)1074dbgs() << "Volatile store: " << I;1075}10761077//===----------------------------------------------------------------------===//1078// Miscellaneous Instruction Implementations1079//===----------------------------------------------------------------------===//10801081void Interpreter::visitVAStartInst(VAStartInst &I) {1082ExecutionContext &SF = ECStack.back();1083GenericValue ArgIndex;1084ArgIndex.UIntPairVal.first = ECStack.size() - 1;1085ArgIndex.UIntPairVal.second = 0;1086SetValue(&I, ArgIndex, SF);1087}10881089void Interpreter::visitVAEndInst(VAEndInst &I) {1090// va_end is a noop for the interpreter1091}10921093void Interpreter::visitVACopyInst(VACopyInst &I) {1094ExecutionContext &SF = ECStack.back();1095SetValue(&I, getOperandValue(*I.arg_begin(), SF), SF);1096}10971098void Interpreter::visitIntrinsicInst(IntrinsicInst &I) {1099ExecutionContext &SF = ECStack.back();11001101// If it is an unknown intrinsic function, use the intrinsic lowering1102// class to transform it into hopefully tasty LLVM code.1103//1104BasicBlock::iterator Me(&I);1105BasicBlock *Parent = I.getParent();1106bool atBegin(Parent->begin() == Me);1107if (!atBegin)1108--Me;1109IL->LowerIntrinsicCall(&I);11101111// Restore the CurInst pointer to the first instruction newly inserted, if1112// any.1113if (atBegin) {1114SF.CurInst = Parent->begin();1115} else {1116SF.CurInst = Me;1117++SF.CurInst;1118}1119}11201121void Interpreter::visitCallBase(CallBase &I) {1122ExecutionContext &SF = ECStack.back();11231124SF.Caller = &I;1125std::vector<GenericValue> ArgVals;1126const unsigned NumArgs = SF.Caller->arg_size();1127ArgVals.reserve(NumArgs);1128for (Value *V : SF.Caller->args())1129ArgVals.push_back(getOperandValue(V, SF));11301131// To handle indirect calls, we must get the pointer value from the argument1132// and treat it as a function pointer.1133GenericValue SRC = getOperandValue(SF.Caller->getCalledOperand(), SF);1134callFunction((Function*)GVTOP(SRC), ArgVals);1135}11361137// auxiliary function for shift operations1138static unsigned getShiftAmount(uint64_t orgShiftAmount,1139llvm::APInt valueToShift) {1140unsigned valueWidth = valueToShift.getBitWidth();1141if (orgShiftAmount < (uint64_t)valueWidth)1142return orgShiftAmount;1143// according to the llvm documentation, if orgShiftAmount > valueWidth,1144// the result is undfeined. but we do shift by this rule:1145return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount;1146}114711481149void Interpreter::visitShl(BinaryOperator &I) {1150ExecutionContext &SF = ECStack.back();1151GenericValue Src1 = getOperandValue(I.getOperand(0), SF);1152GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1153GenericValue Dest;1154Type *Ty = I.getType();11551156if (Ty->isVectorTy()) {1157uint32_t src1Size = uint32_t(Src1.AggregateVal.size());1158assert(src1Size == Src2.AggregateVal.size());1159for (unsigned i = 0; i < src1Size; i++) {1160GenericValue Result;1161uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();1162llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;1163Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));1164Dest.AggregateVal.push_back(Result);1165}1166} else {1167// scalar1168uint64_t shiftAmount = Src2.IntVal.getZExtValue();1169llvm::APInt valueToShift = Src1.IntVal;1170Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));1171}11721173SetValue(&I, Dest, SF);1174}11751176void Interpreter::visitLShr(BinaryOperator &I) {1177ExecutionContext &SF = ECStack.back();1178GenericValue Src1 = getOperandValue(I.getOperand(0), SF);1179GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1180GenericValue Dest;1181Type *Ty = I.getType();11821183if (Ty->isVectorTy()) {1184uint32_t src1Size = uint32_t(Src1.AggregateVal.size());1185assert(src1Size == Src2.AggregateVal.size());1186for (unsigned i = 0; i < src1Size; i++) {1187GenericValue Result;1188uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();1189llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;1190Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));1191Dest.AggregateVal.push_back(Result);1192}1193} else {1194// scalar1195uint64_t shiftAmount = Src2.IntVal.getZExtValue();1196llvm::APInt valueToShift = Src1.IntVal;1197Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));1198}11991200SetValue(&I, Dest, SF);1201}12021203void Interpreter::visitAShr(BinaryOperator &I) {1204ExecutionContext &SF = ECStack.back();1205GenericValue Src1 = getOperandValue(I.getOperand(0), SF);1206GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1207GenericValue Dest;1208Type *Ty = I.getType();12091210if (Ty->isVectorTy()) {1211size_t src1Size = Src1.AggregateVal.size();1212assert(src1Size == Src2.AggregateVal.size());1213for (unsigned i = 0; i < src1Size; i++) {1214GenericValue Result;1215uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();1216llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;1217Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));1218Dest.AggregateVal.push_back(Result);1219}1220} else {1221// scalar1222uint64_t shiftAmount = Src2.IntVal.getZExtValue();1223llvm::APInt valueToShift = Src1.IntVal;1224Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));1225}12261227SetValue(&I, Dest, SF);1228}12291230GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,1231ExecutionContext &SF) {1232GenericValue Dest, Src = getOperandValue(SrcVal, SF);1233Type *SrcTy = SrcVal->getType();1234if (SrcTy->isVectorTy()) {1235Type *DstVecTy = DstTy->getScalarType();1236unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();1237unsigned NumElts = Src.AggregateVal.size();1238// the sizes of src and dst vectors must be equal1239Dest.AggregateVal.resize(NumElts);1240for (unsigned i = 0; i < NumElts; i++)1241Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth);1242} else {1243IntegerType *DITy = cast<IntegerType>(DstTy);1244unsigned DBitWidth = DITy->getBitWidth();1245Dest.IntVal = Src.IntVal.trunc(DBitWidth);1246}1247return Dest;1248}12491250GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,1251ExecutionContext &SF) {1252Type *SrcTy = SrcVal->getType();1253GenericValue Dest, Src = getOperandValue(SrcVal, SF);1254if (SrcTy->isVectorTy()) {1255Type *DstVecTy = DstTy->getScalarType();1256unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();1257unsigned size = Src.AggregateVal.size();1258// the sizes of src and dst vectors must be equal.1259Dest.AggregateVal.resize(size);1260for (unsigned i = 0; i < size; i++)1261Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth);1262} else {1263auto *DITy = cast<IntegerType>(DstTy);1264unsigned DBitWidth = DITy->getBitWidth();1265Dest.IntVal = Src.IntVal.sext(DBitWidth);1266}1267return Dest;1268}12691270GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,1271ExecutionContext &SF) {1272Type *SrcTy = SrcVal->getType();1273GenericValue Dest, Src = getOperandValue(SrcVal, SF);1274if (SrcTy->isVectorTy()) {1275Type *DstVecTy = DstTy->getScalarType();1276unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();12771278unsigned size = Src.AggregateVal.size();1279// the sizes of src and dst vectors must be equal.1280Dest.AggregateVal.resize(size);1281for (unsigned i = 0; i < size; i++)1282Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth);1283} else {1284auto *DITy = cast<IntegerType>(DstTy);1285unsigned DBitWidth = DITy->getBitWidth();1286Dest.IntVal = Src.IntVal.zext(DBitWidth);1287}1288return Dest;1289}12901291GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,1292ExecutionContext &SF) {1293GenericValue Dest, Src = getOperandValue(SrcVal, SF);12941295if (isa<VectorType>(SrcVal->getType())) {1296assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&1297DstTy->getScalarType()->isFloatTy() &&1298"Invalid FPTrunc instruction");12991300unsigned size = Src.AggregateVal.size();1301// the sizes of src and dst vectors must be equal.1302Dest.AggregateVal.resize(size);1303for (unsigned i = 0; i < size; i++)1304Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;1305} else {1306assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&1307"Invalid FPTrunc instruction");1308Dest.FloatVal = (float)Src.DoubleVal;1309}13101311return Dest;1312}13131314GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,1315ExecutionContext &SF) {1316GenericValue Dest, Src = getOperandValue(SrcVal, SF);13171318if (isa<VectorType>(SrcVal->getType())) {1319assert(SrcVal->getType()->getScalarType()->isFloatTy() &&1320DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");13211322unsigned size = Src.AggregateVal.size();1323// the sizes of src and dst vectors must be equal.1324Dest.AggregateVal.resize(size);1325for (unsigned i = 0; i < size; i++)1326Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;1327} else {1328assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&1329"Invalid FPExt instruction");1330Dest.DoubleVal = (double)Src.FloatVal;1331}13321333return Dest;1334}13351336GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,1337ExecutionContext &SF) {1338Type *SrcTy = SrcVal->getType();1339GenericValue Dest, Src = getOperandValue(SrcVal, SF);13401341if (isa<VectorType>(SrcTy)) {1342Type *DstVecTy = DstTy->getScalarType();1343Type *SrcVecTy = SrcTy->getScalarType();1344uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();1345unsigned size = Src.AggregateVal.size();1346// the sizes of src and dst vectors must be equal.1347Dest.AggregateVal.resize(size);13481349if (SrcVecTy->getTypeID() == Type::FloatTyID) {1350assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");1351for (unsigned i = 0; i < size; i++)1352Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(1353Src.AggregateVal[i].FloatVal, DBitWidth);1354} else {1355for (unsigned i = 0; i < size; i++)1356Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(1357Src.AggregateVal[i].DoubleVal, DBitWidth);1358}1359} else {1360// scalar1361uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();1362assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");13631364if (SrcTy->getTypeID() == Type::FloatTyID)1365Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);1366else {1367Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);1368}1369}13701371return Dest;1372}13731374GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,1375ExecutionContext &SF) {1376Type *SrcTy = SrcVal->getType();1377GenericValue Dest, Src = getOperandValue(SrcVal, SF);13781379if (isa<VectorType>(SrcTy)) {1380Type *DstVecTy = DstTy->getScalarType();1381Type *SrcVecTy = SrcTy->getScalarType();1382uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();1383unsigned size = Src.AggregateVal.size();1384// the sizes of src and dst vectors must be equal1385Dest.AggregateVal.resize(size);13861387if (SrcVecTy->getTypeID() == Type::FloatTyID) {1388assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");1389for (unsigned i = 0; i < size; i++)1390Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(1391Src.AggregateVal[i].FloatVal, DBitWidth);1392} else {1393for (unsigned i = 0; i < size; i++)1394Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(1395Src.AggregateVal[i].DoubleVal, DBitWidth);1396}1397} else {1398// scalar1399unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();1400assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");14011402if (SrcTy->getTypeID() == Type::FloatTyID)1403Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);1404else {1405Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);1406}1407}1408return Dest;1409}14101411GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,1412ExecutionContext &SF) {1413GenericValue Dest, Src = getOperandValue(SrcVal, SF);14141415if (isa<VectorType>(SrcVal->getType())) {1416Type *DstVecTy = DstTy->getScalarType();1417unsigned size = Src.AggregateVal.size();1418// the sizes of src and dst vectors must be equal1419Dest.AggregateVal.resize(size);14201421if (DstVecTy->getTypeID() == Type::FloatTyID) {1422assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");1423for (unsigned i = 0; i < size; i++)1424Dest.AggregateVal[i].FloatVal =1425APIntOps::RoundAPIntToFloat(Src.AggregateVal[i].IntVal);1426} else {1427for (unsigned i = 0; i < size; i++)1428Dest.AggregateVal[i].DoubleVal =1429APIntOps::RoundAPIntToDouble(Src.AggregateVal[i].IntVal);1430}1431} else {1432// scalar1433assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");1434if (DstTy->getTypeID() == Type::FloatTyID)1435Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);1436else {1437Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);1438}1439}1440return Dest;1441}14421443GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,1444ExecutionContext &SF) {1445GenericValue Dest, Src = getOperandValue(SrcVal, SF);14461447if (isa<VectorType>(SrcVal->getType())) {1448Type *DstVecTy = DstTy->getScalarType();1449unsigned size = Src.AggregateVal.size();1450// the sizes of src and dst vectors must be equal1451Dest.AggregateVal.resize(size);14521453if (DstVecTy->getTypeID() == Type::FloatTyID) {1454assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");1455for (unsigned i = 0; i < size; i++)1456Dest.AggregateVal[i].FloatVal =1457APIntOps::RoundSignedAPIntToFloat(Src.AggregateVal[i].IntVal);1458} else {1459for (unsigned i = 0; i < size; i++)1460Dest.AggregateVal[i].DoubleVal =1461APIntOps::RoundSignedAPIntToDouble(Src.AggregateVal[i].IntVal);1462}1463} else {1464// scalar1465assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");14661467if (DstTy->getTypeID() == Type::FloatTyID)1468Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);1469else {1470Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);1471}1472}14731474return Dest;1475}14761477GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,1478ExecutionContext &SF) {1479uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();1480GenericValue Dest, Src = getOperandValue(SrcVal, SF);1481assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");14821483Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);1484return Dest;1485}14861487GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,1488ExecutionContext &SF) {1489GenericValue Dest, Src = getOperandValue(SrcVal, SF);1490assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");14911492uint32_t PtrSize = getDataLayout().getPointerSizeInBits();1493if (PtrSize != Src.IntVal.getBitWidth())1494Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);14951496Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));1497return Dest;1498}14991500GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,1501ExecutionContext &SF) {15021503// This instruction supports bitwise conversion of vectors to integers and1504// to vectors of other types (as long as they have the same size)1505Type *SrcTy = SrcVal->getType();1506GenericValue Dest, Src = getOperandValue(SrcVal, SF);15071508if (isa<VectorType>(SrcTy) || isa<VectorType>(DstTy)) {1509// vector src bitcast to vector dst or vector src bitcast to scalar dst or1510// scalar src bitcast to vector dst1511bool isLittleEndian = getDataLayout().isLittleEndian();1512GenericValue TempDst, TempSrc, SrcVec;1513Type *SrcElemTy;1514Type *DstElemTy;1515unsigned SrcBitSize;1516unsigned DstBitSize;1517unsigned SrcNum;1518unsigned DstNum;15191520if (isa<VectorType>(SrcTy)) {1521SrcElemTy = SrcTy->getScalarType();1522SrcBitSize = SrcTy->getScalarSizeInBits();1523SrcNum = Src.AggregateVal.size();1524SrcVec = Src;1525} else {1526// if src is scalar value, make it vector <1 x type>1527SrcElemTy = SrcTy;1528SrcBitSize = SrcTy->getPrimitiveSizeInBits();1529SrcNum = 1;1530SrcVec.AggregateVal.push_back(Src);1531}15321533if (isa<VectorType>(DstTy)) {1534DstElemTy = DstTy->getScalarType();1535DstBitSize = DstTy->getScalarSizeInBits();1536DstNum = (SrcNum * SrcBitSize) / DstBitSize;1537} else {1538DstElemTy = DstTy;1539DstBitSize = DstTy->getPrimitiveSizeInBits();1540DstNum = 1;1541}15421543if (SrcNum * SrcBitSize != DstNum * DstBitSize)1544llvm_unreachable("Invalid BitCast");15451546// If src is floating point, cast to integer first.1547TempSrc.AggregateVal.resize(SrcNum);1548if (SrcElemTy->isFloatTy()) {1549for (unsigned i = 0; i < SrcNum; i++)1550TempSrc.AggregateVal[i].IntVal =1551APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal);15521553} else if (SrcElemTy->isDoubleTy()) {1554for (unsigned i = 0; i < SrcNum; i++)1555TempSrc.AggregateVal[i].IntVal =1556APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal);1557} else if (SrcElemTy->isIntegerTy()) {1558for (unsigned i = 0; i < SrcNum; i++)1559TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;1560} else {1561// Pointers are not allowed as the element type of vector.1562llvm_unreachable("Invalid Bitcast");1563}15641565// now TempSrc is integer type vector1566if (DstNum < SrcNum) {1567// Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>1568unsigned Ratio = SrcNum / DstNum;1569unsigned SrcElt = 0;1570for (unsigned i = 0; i < DstNum; i++) {1571GenericValue Elt;1572Elt.IntVal = 0;1573Elt.IntVal = Elt.IntVal.zext(DstBitSize);1574unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);1575for (unsigned j = 0; j < Ratio; j++) {1576APInt Tmp;1577Tmp = Tmp.zext(SrcBitSize);1578Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;1579Tmp = Tmp.zext(DstBitSize);1580Tmp <<= ShiftAmt;1581ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;1582Elt.IntVal |= Tmp;1583}1584TempDst.AggregateVal.push_back(Elt);1585}1586} else {1587// Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>1588unsigned Ratio = DstNum / SrcNum;1589for (unsigned i = 0; i < SrcNum; i++) {1590unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);1591for (unsigned j = 0; j < Ratio; j++) {1592GenericValue Elt;1593Elt.IntVal = Elt.IntVal.zext(SrcBitSize);1594Elt.IntVal = TempSrc.AggregateVal[i].IntVal;1595Elt.IntVal.lshrInPlace(ShiftAmt);1596// it could be DstBitSize == SrcBitSize, so check it1597if (DstBitSize < SrcBitSize)1598Elt.IntVal = Elt.IntVal.trunc(DstBitSize);1599ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;1600TempDst.AggregateVal.push_back(Elt);1601}1602}1603}16041605// convert result from integer to specified type1606if (isa<VectorType>(DstTy)) {1607if (DstElemTy->isDoubleTy()) {1608Dest.AggregateVal.resize(DstNum);1609for (unsigned i = 0; i < DstNum; i++)1610Dest.AggregateVal[i].DoubleVal =1611TempDst.AggregateVal[i].IntVal.bitsToDouble();1612} else if (DstElemTy->isFloatTy()) {1613Dest.AggregateVal.resize(DstNum);1614for (unsigned i = 0; i < DstNum; i++)1615Dest.AggregateVal[i].FloatVal =1616TempDst.AggregateVal[i].IntVal.bitsToFloat();1617} else {1618Dest = TempDst;1619}1620} else {1621if (DstElemTy->isDoubleTy())1622Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();1623else if (DstElemTy->isFloatTy()) {1624Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();1625} else {1626Dest.IntVal = TempDst.AggregateVal[0].IntVal;1627}1628}1629} else { // if (isa<VectorType>(SrcTy)) || isa<VectorType>(DstTy))16301631// scalar src bitcast to scalar dst1632if (DstTy->isPointerTy()) {1633assert(SrcTy->isPointerTy() && "Invalid BitCast");1634Dest.PointerVal = Src.PointerVal;1635} else if (DstTy->isIntegerTy()) {1636if (SrcTy->isFloatTy())1637Dest.IntVal = APInt::floatToBits(Src.FloatVal);1638else if (SrcTy->isDoubleTy()) {1639Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);1640} else if (SrcTy->isIntegerTy()) {1641Dest.IntVal = Src.IntVal;1642} else {1643llvm_unreachable("Invalid BitCast");1644}1645} else if (DstTy->isFloatTy()) {1646if (SrcTy->isIntegerTy())1647Dest.FloatVal = Src.IntVal.bitsToFloat();1648else {1649Dest.FloatVal = Src.FloatVal;1650}1651} else if (DstTy->isDoubleTy()) {1652if (SrcTy->isIntegerTy())1653Dest.DoubleVal = Src.IntVal.bitsToDouble();1654else {1655Dest.DoubleVal = Src.DoubleVal;1656}1657} else {1658llvm_unreachable("Invalid Bitcast");1659}1660}16611662return Dest;1663}16641665void Interpreter::visitTruncInst(TruncInst &I) {1666ExecutionContext &SF = ECStack.back();1667SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);1668}16691670void Interpreter::visitSExtInst(SExtInst &I) {1671ExecutionContext &SF = ECStack.back();1672SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);1673}16741675void Interpreter::visitZExtInst(ZExtInst &I) {1676ExecutionContext &SF = ECStack.back();1677SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);1678}16791680void Interpreter::visitFPTruncInst(FPTruncInst &I) {1681ExecutionContext &SF = ECStack.back();1682SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);1683}16841685void Interpreter::visitFPExtInst(FPExtInst &I) {1686ExecutionContext &SF = ECStack.back();1687SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);1688}16891690void Interpreter::visitUIToFPInst(UIToFPInst &I) {1691ExecutionContext &SF = ECStack.back();1692SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);1693}16941695void Interpreter::visitSIToFPInst(SIToFPInst &I) {1696ExecutionContext &SF = ECStack.back();1697SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);1698}16991700void Interpreter::visitFPToUIInst(FPToUIInst &I) {1701ExecutionContext &SF = ECStack.back();1702SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);1703}17041705void Interpreter::visitFPToSIInst(FPToSIInst &I) {1706ExecutionContext &SF = ECStack.back();1707SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);1708}17091710void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {1711ExecutionContext &SF = ECStack.back();1712SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);1713}17141715void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {1716ExecutionContext &SF = ECStack.back();1717SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);1718}17191720void Interpreter::visitBitCastInst(BitCastInst &I) {1721ExecutionContext &SF = ECStack.back();1722SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);1723}17241725#define IMPLEMENT_VAARG(TY) \1726case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break17271728void Interpreter::visitVAArgInst(VAArgInst &I) {1729ExecutionContext &SF = ECStack.back();17301731// Get the incoming valist parameter. LLI treats the valist as a1732// (ec-stack-depth var-arg-index) pair.1733GenericValue VAList = getOperandValue(I.getOperand(0), SF);1734GenericValue Dest;1735GenericValue Src = ECStack[VAList.UIntPairVal.first]1736.VarArgs[VAList.UIntPairVal.second];1737Type *Ty = I.getType();1738switch (Ty->getTypeID()) {1739case Type::IntegerTyID:1740Dest.IntVal = Src.IntVal;1741break;1742IMPLEMENT_VAARG(Pointer);1743IMPLEMENT_VAARG(Float);1744IMPLEMENT_VAARG(Double);1745default:1746dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";1747llvm_unreachable(nullptr);1748}17491750// Set the Value of this Instruction.1751SetValue(&I, Dest, SF);17521753// Move the pointer to the next vararg.1754++VAList.UIntPairVal.second;1755}17561757void Interpreter::visitExtractElementInst(ExtractElementInst &I) {1758ExecutionContext &SF = ECStack.back();1759GenericValue Src1 = getOperandValue(I.getOperand(0), SF);1760GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1761GenericValue Dest;17621763Type *Ty = I.getType();1764const unsigned indx = unsigned(Src2.IntVal.getZExtValue());17651766if(Src1.AggregateVal.size() > indx) {1767switch (Ty->getTypeID()) {1768default:1769dbgs() << "Unhandled destination type for extractelement instruction: "1770<< *Ty << "\n";1771llvm_unreachable(nullptr);1772break;1773case Type::IntegerTyID:1774Dest.IntVal = Src1.AggregateVal[indx].IntVal;1775break;1776case Type::FloatTyID:1777Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;1778break;1779case Type::DoubleTyID:1780Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;1781break;1782}1783} else {1784dbgs() << "Invalid index in extractelement instruction\n";1785}17861787SetValue(&I, Dest, SF);1788}17891790void Interpreter::visitInsertElementInst(InsertElementInst &I) {1791ExecutionContext &SF = ECStack.back();1792VectorType *Ty = cast<VectorType>(I.getType());17931794GenericValue Src1 = getOperandValue(I.getOperand(0), SF);1795GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1796GenericValue Src3 = getOperandValue(I.getOperand(2), SF);1797GenericValue Dest;17981799Type *TyContained = Ty->getElementType();18001801const unsigned indx = unsigned(Src3.IntVal.getZExtValue());1802Dest.AggregateVal = Src1.AggregateVal;18031804if(Src1.AggregateVal.size() <= indx)1805llvm_unreachable("Invalid index in insertelement instruction");1806switch (TyContained->getTypeID()) {1807default:1808llvm_unreachable("Unhandled dest type for insertelement instruction");1809case Type::IntegerTyID:1810Dest.AggregateVal[indx].IntVal = Src2.IntVal;1811break;1812case Type::FloatTyID:1813Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;1814break;1815case Type::DoubleTyID:1816Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;1817break;1818}1819SetValue(&I, Dest, SF);1820}18211822void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){1823ExecutionContext &SF = ECStack.back();18241825VectorType *Ty = cast<VectorType>(I.getType());18261827GenericValue Src1 = getOperandValue(I.getOperand(0), SF);1828GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1829GenericValue Dest;18301831// There is no need to check types of src1 and src2, because the compiled1832// bytecode can't contain different types for src1 and src2 for a1833// shufflevector instruction.18341835Type *TyContained = Ty->getElementType();1836unsigned src1Size = (unsigned)Src1.AggregateVal.size();1837unsigned src2Size = (unsigned)Src2.AggregateVal.size();1838unsigned src3Size = I.getShuffleMask().size();18391840Dest.AggregateVal.resize(src3Size);18411842switch (TyContained->getTypeID()) {1843default:1844llvm_unreachable("Unhandled dest type for insertelement instruction");1845break;1846case Type::IntegerTyID:1847for( unsigned i=0; i<src3Size; i++) {1848unsigned j = std::max(0, I.getMaskValue(i));1849if(j < src1Size)1850Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;1851else if(j < src1Size + src2Size)1852Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;1853else1854// The selector may not be greater than sum of lengths of first and1855// second operands and llasm should not allow situation like1856// %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,1857// <2 x i32> < i32 0, i32 5 >,1858// where i32 5 is invalid, but let it be additional check here:1859llvm_unreachable("Invalid mask in shufflevector instruction");1860}1861break;1862case Type::FloatTyID:1863for( unsigned i=0; i<src3Size; i++) {1864unsigned j = std::max(0, I.getMaskValue(i));1865if(j < src1Size)1866Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;1867else if(j < src1Size + src2Size)1868Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;1869else1870llvm_unreachable("Invalid mask in shufflevector instruction");1871}1872break;1873case Type::DoubleTyID:1874for( unsigned i=0; i<src3Size; i++) {1875unsigned j = std::max(0, I.getMaskValue(i));1876if(j < src1Size)1877Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;1878else if(j < src1Size + src2Size)1879Dest.AggregateVal[i].DoubleVal =1880Src2.AggregateVal[j-src1Size].DoubleVal;1881else1882llvm_unreachable("Invalid mask in shufflevector instruction");1883}1884break;1885}1886SetValue(&I, Dest, SF);1887}18881889void Interpreter::visitExtractValueInst(ExtractValueInst &I) {1890ExecutionContext &SF = ECStack.back();1891Value *Agg = I.getAggregateOperand();1892GenericValue Dest;1893GenericValue Src = getOperandValue(Agg, SF);18941895ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();1896unsigned Num = I.getNumIndices();1897GenericValue *pSrc = &Src;18981899for (unsigned i = 0 ; i < Num; ++i) {1900pSrc = &pSrc->AggregateVal[*IdxBegin];1901++IdxBegin;1902}19031904Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());1905switch (IndexedType->getTypeID()) {1906default:1907llvm_unreachable("Unhandled dest type for extractelement instruction");1908break;1909case Type::IntegerTyID:1910Dest.IntVal = pSrc->IntVal;1911break;1912case Type::FloatTyID:1913Dest.FloatVal = pSrc->FloatVal;1914break;1915case Type::DoubleTyID:1916Dest.DoubleVal = pSrc->DoubleVal;1917break;1918case Type::ArrayTyID:1919case Type::StructTyID:1920case Type::FixedVectorTyID:1921case Type::ScalableVectorTyID:1922Dest.AggregateVal = pSrc->AggregateVal;1923break;1924case Type::PointerTyID:1925Dest.PointerVal = pSrc->PointerVal;1926break;1927}19281929SetValue(&I, Dest, SF);1930}19311932void Interpreter::visitInsertValueInst(InsertValueInst &I) {19331934ExecutionContext &SF = ECStack.back();1935Value *Agg = I.getAggregateOperand();19361937GenericValue Src1 = getOperandValue(Agg, SF);1938GenericValue Src2 = getOperandValue(I.getOperand(1), SF);1939GenericValue Dest = Src1; // Dest is a slightly changed Src119401941ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();1942unsigned Num = I.getNumIndices();19431944GenericValue *pDest = &Dest;1945for (unsigned i = 0 ; i < Num; ++i) {1946pDest = &pDest->AggregateVal[*IdxBegin];1947++IdxBegin;1948}1949// pDest points to the target value in the Dest now19501951Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());19521953switch (IndexedType->getTypeID()) {1954default:1955llvm_unreachable("Unhandled dest type for insertelement instruction");1956break;1957case Type::IntegerTyID:1958pDest->IntVal = Src2.IntVal;1959break;1960case Type::FloatTyID:1961pDest->FloatVal = Src2.FloatVal;1962break;1963case Type::DoubleTyID:1964pDest->DoubleVal = Src2.DoubleVal;1965break;1966case Type::ArrayTyID:1967case Type::StructTyID:1968case Type::FixedVectorTyID:1969case Type::ScalableVectorTyID:1970pDest->AggregateVal = Src2.AggregateVal;1971break;1972case Type::PointerTyID:1973pDest->PointerVal = Src2.PointerVal;1974break;1975}19761977SetValue(&I, Dest, SF);1978}19791980GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,1981ExecutionContext &SF) {1982switch (CE->getOpcode()) {1983case Instruction::Trunc:1984return executeTruncInst(CE->getOperand(0), CE->getType(), SF);1985case Instruction::PtrToInt:1986return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);1987case Instruction::IntToPtr:1988return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);1989case Instruction::BitCast:1990return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);1991case Instruction::GetElementPtr:1992return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),1993gep_type_end(CE), SF);1994break;1995}19961997// The cases below here require a GenericValue parameter for the result1998// so we initialize one, compute it and then return it.1999GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);2000GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);2001GenericValue Dest;2002switch (CE->getOpcode()) {2003case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;2004case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;2005case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;2006case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;2007case Instruction::Shl:2008Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());2009break;2010default:2011dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";2012llvm_unreachable("Unhandled ConstantExpr");2013}2014return Dest;2015}20162017GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {2018if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {2019return getConstantExprValue(CE, SF);2020} else if (Constant *CPV = dyn_cast<Constant>(V)) {2021return getConstantValue(CPV);2022} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {2023return PTOGV(getPointerToGlobal(GV));2024} else {2025return SF.Values[V];2026}2027}20282029//===----------------------------------------------------------------------===//2030// Dispatch and Execution Code2031//===----------------------------------------------------------------------===//20322033//===----------------------------------------------------------------------===//2034// callFunction - Execute the specified function...2035//2036void Interpreter::callFunction(Function *F, ArrayRef<GenericValue> ArgVals) {2037assert((ECStack.empty() || !ECStack.back().Caller ||2038ECStack.back().Caller->arg_size() == ArgVals.size()) &&2039"Incorrect number of arguments passed into function call!");2040// Make a new stack frame... and fill it in.2041ECStack.emplace_back();2042ExecutionContext &StackFrame = ECStack.back();2043StackFrame.CurFunction = F;20442045// Special handling for external functions.2046if (F->isDeclaration()) {2047GenericValue Result = callExternalFunction (F, ArgVals);2048// Simulate a 'ret' instruction of the appropriate type.2049popStackAndReturnValueToCaller (F->getReturnType (), Result);2050return;2051}20522053// Get pointers to first LLVM BB & Instruction in function.2054StackFrame.CurBB = &F->front();2055StackFrame.CurInst = StackFrame.CurBB->begin();20562057// Run through the function arguments and initialize their values...2058assert((ArgVals.size() == F->arg_size() ||2059(ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&2060"Invalid number of values passed to function invocation!");20612062// Handle non-varargs arguments...2063unsigned i = 0;2064for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();2065AI != E; ++AI, ++i)2066SetValue(&*AI, ArgVals[i], StackFrame);20672068// Handle varargs arguments...2069StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());2070}207120722073void Interpreter::run() {2074while (!ECStack.empty()) {2075// Interpret a single instruction & increment the "PC".2076ExecutionContext &SF = ECStack.back(); // Current stack frame2077Instruction &I = *SF.CurInst++; // Increment before execute20782079// Track the number of dynamic instructions executed.2080++NumDynamicInsts;20812082LLVM_DEBUG(dbgs() << "About to interpret: " << I << "\n");2083visit(I); // Dispatch to one of the visit* methods...2084}2085}208620872088