ExpressionPtr buildPtrSubscript(const ExpressionPtr& ptrExpr, const ExpressionPtr& subscriptExpr) { assert_pred1(isPointer, ptrExpr) << "Trying to build a ptr subscript from non-ptr."; assert_pred1(ptrExpr->getNodeManager().getLangBasic().isInt, subscriptExpr->getType()) << "Trying to build a ptr subscript with non-integral subscript."; IRBuilder builder(ptrExpr->getNodeManager()); auto& pExt = ptrExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrSubscript(), ptrExpr, subscriptExpr); }
ExpressionPtr buildPtrOfFunction(const ExpressionPtr& funExpr) { assert_true(funExpr->getType().isa<FunctionTypePtr>()) << "Trying to build a ptr of a non-function type:\n" << dumpColor(funExpr) << "\nType: " << dumpColor(funExpr->getType()); IRBuilder builder(funExpr->getNodeManager()); auto& pExt = funExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrOfFunction(), funExpr); }
ExpressionPtr buildPtrToIntegral(const ExpressionPtr& ptrExpr, const TypePtr& intType) { assert_pred1(isPointer, ptrExpr) << "Trying to build a ref from non-ptr."; assert_pred1(ptrExpr->getNodeManager().getLangBasic().isInt, intType) << "Trying to build non-integral from ptr."; IRBuilder builder(ptrExpr->getNodeManager()); auto& pExt = ptrExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrToIntegral(), ptrExpr, builder.getTypeLiteral(intType)); }
ExpressionPtr buildPtrFromArray(const ExpressionPtr& arrExpr) { assert_pred1(isReference, arrExpr) << "Trying to buildPtrFromArray from non-ref."; assert_pred1(isArray, core::analysis::getReferencedType(arrExpr->getType())) << "Trying to buildPtrFromArray from non-array."; IRBuilder builder(arrExpr->getNodeManager()); auto& pExt = arrExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrFromArray(), arrExpr); }
ExpressionPtr buildPtrReinterpret(const ExpressionPtr& ptrExpr, const TypePtr& newElementType) { assert_pred1(core::lang::isPointer, ptrExpr) << "Trying to build a ptr reinterpret from non-ptr."; PointerType srcTy(ptrExpr->getType()); // early exit if there is nothing to do if(srcTy.getElementType() == newElementType) return ptrExpr; // otherwise, build reinterpret IRBuilder builder(ptrExpr->getNodeManager()); auto& pExt = ptrExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrReinterpret(), ptrExpr, builder.getTypeLiteral(newElementType)); }
ExpressionPtr buildPtrCast(const ExpressionPtr& ptrExpr, bool newConst, bool newVolatile) { assert_pred1(core::lang::isPointer, ptrExpr) << "Trying to build a ptr cast from non-ptr."; PointerType srcTy(ptrExpr->getType()); // early exit if there is nothing to do if(srcTy.isConst() == newConst && srcTy.isVolatile() == newVolatile) return ptrExpr; // otherwise, build cast IRBuilder builder(ptrExpr->getNodeManager()); auto& pExt = ptrExpr->getNodeManager().getLangExtension<PointerExtension>(); auto& bmExt = ptrExpr->getNodeManager().getLangExtension<BooleanMarkerExtension>(); return builder.callExpr(pExt.getPtrCast(), ptrExpr, bmExt.getMarkerTypeLiteral(newConst), bmExt.getMarkerTypeLiteral(newVolatile)); }
ExpressionPtr buildPtrOperation(BasicGenerator::Operator op, const ExpressionPtr& ptrExpr) { assert_true(isReference(ptrExpr) && isPointer(core::analysis::getReferencedType(ptrExpr->getType()))) << "Trying to build a unary pointer operation with non-ref<ptr>."; IRBuilder builder(ptrExpr->getNodeManager()); auto& pExt = ptrExpr->getNodeManager().getLangExtension<PointerExtension>(); switch(op) { case BasicGenerator::Operator::PostInc: return builder.callExpr(pExt.getPtrPostInc(), ptrExpr); case BasicGenerator::Operator::PostDec: return builder.callExpr(pExt.getPtrPostDec(), ptrExpr); case BasicGenerator::Operator::PreInc: return builder.callExpr(pExt.getPtrPreInc(), ptrExpr); case BasicGenerator::Operator::PreDec: return builder.callExpr(pExt.getPtrPreDec(), ptrExpr); default: break; } assert_fail() << "Unsupported unary pointer operation " << op; return ExpressionPtr(); }
/* * Changes the type of the field of a struct */ void updateStruct(const ExpressionPtr& structure, TypePtr& type, const ExpressionPtr& identifier) { NodeManager& mgr = structure->getNodeManager(); IRBuilder builder(mgr); TypePtr baseType = structure->getType(); RefTypePtr refTy = baseType.isa<RefTypePtr>(); StructTypePtr kst = refTy ? refTy->getElementType().as<StructTypePtr>() : structure->getType().as<StructTypePtr>(); std::string name = identifier->toString(); NamedTypePtr oldType = kst->getNamedTypeEntryOf(name); NamedTypePtr newType = builder.namedType(name, refTy ? builder.refType(type) : type); TypePtr newStructType = transform::replaceAll(mgr, baseType, oldType, newType).as<TypePtr>(); type = newStructType; }
ExpressionPtr getCreateBuffer(const TypePtr& type, const ExpressionPtr& size, const bool copyPtr, const ExpressionPtr& hostPtr, const ExpressionPtr& errcode_ret) { NodeManager& mgr = size->getNodeManager(); IRBuilder builder(mgr); Ocl2Inspire o2i; ExpressionPtr fun = o2i.getClCreateBuffer(copyPtr, core::types::isNullPtrExpression(errcode_ret), builder); vector<ExpressionPtr> args; args.push_back(builder.getTypeLiteral(type)); args.push_back(size); if(copyPtr) args.push_back(hostPtr); args.push_back(errcode_ret); return builder.callExpr(builder.refType(builder.arrayType(type)), fun, args); }
ExpressionPtr buildPtrOperation(BasicGenerator::Operator op, const ExpressionPtr& lhs, const ExpressionPtr& rhs) { auto& basic = lhs->getNodeManager().getLangBasic(); auto& pExt = lhs->getNodeManager().getLangExtension<PointerExtension>(); IRBuilder builder(lhs->getNodeManager()); auto assertPtr = [&](const ExpressionPtr& exp) { assert_pred1(isPointer, exp) << "Trying to build a ptr operation from non-ptr:" << "\n lhs: " << *lhs << "\n - of type: " << *lhs->getType() << "\n rhs: " << *rhs << "\n - of type: " << *rhs->getType() << "\n op: " << op; }; auto assertInt = [&](const ExpressionPtr& exp) { assert_pred1(basic.isInt, exp->getType()) << "Trying to build a ptr add/sub with non-int" << "\n lhs: " << *lhs << "\n - of type: " << *lhs->getType() << "\n rhs: " << *rhs << "\n - of type: " << *rhs->getType() << "\n op: " << op; }; auto buildInt8Cast = [&](const ExpressionPtr& exp) { if(!core::types::isSubTypeOf(exp->getType(), basic.getInt8())) { return builder.numericCast(exp, basic.getInt8()); } return exp; }; // arithmetic operations switch(op) { case BasicGenerator::Operator::Add: { if(!isPointer(lhs)) { assertPtr(rhs); assertInt(lhs); return builder.callExpr(pExt.getPtrAdd(), rhs, buildInt8Cast(lhs)); } assertPtr(lhs); assertInt(rhs); return builder.callExpr(pExt.getPtrAdd(), lhs, buildInt8Cast(rhs)); } case BasicGenerator::Operator::Sub: { // minus is only supported with ptr on the lhs assertPtr(lhs); if(!isPointer(rhs)) { assertInt(rhs); return builder.callExpr(pExt.getPtrSub(), lhs, buildInt8Cast(rhs)); } else { return builder.callExpr(pExt.getPtrDiff(), lhs, rhs); } } default: break; } // comparison operations assertPtr(lhs); assertPtr(rhs); switch(op) { case BasicGenerator::Operator::Eq: return builder.callExpr(pExt.getPtrEqual(), lhs, rhs); case BasicGenerator::Operator::Ne: return builder.callExpr(pExt.getPtrNotEqual(), lhs, rhs); case BasicGenerator::Operator::Le: return builder.callExpr(pExt.getPtrLessEqual(), lhs, rhs); case BasicGenerator::Operator::Lt: return builder.callExpr(pExt.getPtrLessThan(), lhs, rhs); case BasicGenerator::Operator::Ge: return builder.callExpr(pExt.getPtrGreaterEqual(), lhs, rhs); case BasicGenerator::Operator::Gt: return builder.callExpr(pExt.getPtrGreaterThan(), lhs, rhs); default: break; } assert_fail() << "Unsupported binary pointer operation " << op; return ExpressionPtr(); }
ExpressionPtr buildPtrDeref(const ExpressionPtr& ptrExpr) { assert_pred1(isPointer, ptrExpr) << "Trying to build ptr_deref from non-ptr type."; IRBuilder builder(ptrExpr->getNodeManager()); auto& pExt = ptrExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrDeref(), ptrExpr); }
ExpressionPtr buildPtrFromRef(const ExpressionPtr& refExpr) { assert_pred1(isReference, refExpr) << "Trying to build ptr from non-ref."; IRBuilder builder(refExpr->getNodeManager()); auto& pExt = refExpr->getNodeManager().getLangExtension<PointerExtension>(); return builder.callExpr(pExt.getPtrFromRef(), refExpr); }