From f5d5b99c9334e7b9bd73b7a1ca0c1214ed4a0663 Mon Sep 17 00:00:00 2001 From: MarkFedoseev Date: Sat, 28 Aug 2021 00:00:45 +0300 Subject: [PATCH 1/2] Builtins support + SelectLibCall improvement --- .../include/cg/aarch64/aarch64_cgfunc.h | 3 +- src/mapleall/maple_be/include/cg/cgfunc.h | 1 + src/mapleall/maple_be/src/be/lower.cpp | 10 ++ .../src/cg/aarch64/aarch64_cgfunc.cpp | 98 ++++++++++++++----- src/mapleall/maple_be/src/cg/cgfunc.cpp | 20 ++++ src/mapleall/maple_ir/include/intrinsic_c.def | 2 + .../ast_input/include/ast_builtin_func.def | 9 -- src/mplfe/ast_input/include/ast_expr.h | 11 +++ .../ast_input/include/builtin_func_emit.def | 21 ++++ .../src/ast_parser_builting_func.cpp | 39 ++++++++ 10 files changed, 181 insertions(+), 33 deletions(-) diff --git a/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h b/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h index fe657e0f57..5b558b9b3d 100644 --- a/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h +++ b/src/mapleall/maple_be/include/cg/aarch64/aarch64_cgfunc.h @@ -115,6 +115,7 @@ class AArch64CGFunc : public CGFunc { void SelectIcall(IcallNode &icallNode, Operand &fptrOpnd) override; void SelectIntrinCall(IntrinsiccallNode &intrinsicCallNode) override; Operand *SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrinopNode, std::string name) override; + Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinopNode, PrimType retType, std::string name) override; Operand *SelectCclz(IntrinsicopNode &intrinopNode) override; Operand *SelectCctz(IntrinsicopNode &intrinopNode) override; Operand *SelectCpopcount(IntrinsicopNode &intrinopNode) override; @@ -237,7 +238,7 @@ class AArch64CGFunc : public CGFunc { RegOperand &SelectCopy(Operand &src, PrimType stype, PrimType dtype) override; void SelectCopy(Operand &dest, PrimType dtype, Operand &src, PrimType stype); void SelectCopyImm(Operand &dest, ImmOperand &src, PrimType dtype); - void SelectLibCall(const std::string&, std::vector&, PrimType, PrimType, bool is2ndRet = false); + void SelectLibCall(const std::string&, std::vector&, std::vector, bool is2ndRet = false); Operand &GetTargetRetOperand(PrimType primType, int32 sReg) override; Operand &GetOrCreateRflag() override; const Operand *GetRflag() const override; diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index a4d023136a..dd113bea62 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -178,6 +178,7 @@ class CGFunc { virtual void SelectIcall(IcallNode &icallNode, Operand &fptrOpnd) = 0; virtual void SelectIntrinCall(IntrinsiccallNode &intrinsiccallNode) = 0; virtual Operand *SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrinopNode, std::string name) = 0; + virtual Operand *SelectIntrinsicOpWithNParams(IntrinsicopNode &intrinopNode, PrimType retType, std::string name) = 0; virtual Operand *SelectCclz(IntrinsicopNode &intrinopNode) = 0; virtual Operand *SelectCctz(IntrinsicopNode &intrinopNode) = 0; virtual Operand *SelectCpopcount(IntrinsicopNode &intrinopNode) = 0; diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 12dadad412..6345513aff 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -3497,6 +3497,16 @@ bool CGLowerer::IsIntrinsicOpHandledAtLowerLevel(MIRIntrinsicID intrinsic) { case INTRN_C___sync_lock_release_8: case INTRN_C___sync_lock_release_4: case INTRN_C__builtin_return_address: + case INTRN_C_strcpy: + case INTRN_C_strncpy: + case INTRN_C_strchr: + case INTRN_C_memcmp: + case INTRN_C_memcpy: + case INTRN_C_memset: + case INTRN_C_strlen: + case INTRN_C_strcmp: + case INTRN_C_strncmp: + case INTRN_C_memmove: return true; #endif default: diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp index 8d92320022..d4eeaeeca2 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -1538,7 +1538,9 @@ void AArch64CGFunc::SelectAggDassign(DassignNode &stmt) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - SelectLibCall("memcpy", opndVec, PTY_a64, PTY_a64); + std::vector opndTypes(4, PTY_a64); + + SelectLibCall("memcpy", opndVec, opndTypes); return; } @@ -1659,7 +1661,9 @@ void AArch64CGFunc::SelectAggDassign(DassignNode &stmt) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - SelectLibCall("memcpy", opndVec, PTY_a64, PTY_a64); + std::vector opndTypes(4, PTY_a64); + + SelectLibCall("memcpy", opndVec, opndTypes); return; } @@ -2035,7 +2039,9 @@ void AArch64CGFunc::SelectAggIassign(IassignNode &stmt, Operand &AddrOpnd) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - SelectLibCall("memcpy", opndVec, PTY_a64, PTY_a64); + std::vector opndTypes(4, PTY_a64); + + SelectLibCall("memcpy", opndVec, opndTypes); return; } @@ -2171,7 +2177,9 @@ void AArch64CGFunc::SelectAggIassign(IassignNode &stmt, Operand &AddrOpnd) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - SelectLibCall("memcpy", opndVec, PTY_a64, PTY_a64); + std::vector opndTypes(4, PTY_a64); + + SelectLibCall("memcpy", opndVec, opndTypes); return; } @@ -4637,11 +4645,44 @@ Operand *AArch64CGFunc::SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrnNode RegOperand *dst = &CreateRegisterOperandOfType(ptype); opndVec.push_back(dst); /* result */ opndVec.push_back(opnd); /* param 0 */ - SelectLibCall(name, opndVec, ptype, ptype); + + std::vector opndTypes(2, ptype); + + SelectLibCall(name, opndVec, opndTypes); return dst; } +Operand *AArch64CGFunc::SelectIntrinsicOpWithNParams(IntrinsicopNode &intrnNode, PrimType retType, std::string name) { + MapleVector argNodes = intrnNode.GetNopnd(); + //FATAL(argNodes.size() > 0, "Faild to process intrinsic with zero params by NParams func"); + + std::vector opndVec; + std::vector opndTypes; + RegOperand *retOpnd = &CreateRegisterOperandOfType(retType); + opndVec.push_back(retOpnd); + opndTypes.push_back(retType); + + for (BaseNode* argexpr : argNodes) { + PrimType ptype = argexpr->GetPrimType(); + Operand *opnd = HandleExpr(intrnNode, *argexpr); + + if (opnd->IsMemoryAccessOperand()) { + RegOperand &ldDest = CreateRegisterOperandOfType(ptype); + Insn &insn = GetCG()->BuildInstruction(PickLdInsn(GetPrimTypeBitSize(ptype), ptype), ldDest, *opnd); + GetCurBB()->AppendInsn(insn); + opnd = &ldDest; + } + + opndVec.push_back(opnd); + opndTypes.push_back(ptype); + } + + SelectLibCall(name, opndVec, opndTypes); + + return retOpnd; +} + /* According to gcc.target/aarch64/ffs.c */ Operand *AArch64CGFunc::SelectAArch64ffs(Operand &argOpnd, PrimType argType) { RegOperand &destOpnd = LoadIntoRegister(argOpnd, argType); @@ -4688,7 +4729,10 @@ Operand *AArch64CGFunc::SelectRoundLibCall(RoundType roundType, const TypeCvtNod } else { libName.assign(is64Bits ? "round" : "roundf"); } - SelectLibCall(libName, opndVec, ftype, rtype); + + std::vector opndTypes{rtype, ftype}; + + SelectLibCall(libName, opndVec, opndTypes); return resOpnd; } @@ -5234,7 +5278,9 @@ Operand *AArch64CGFunc::SelectMalloc(UnaryNode &node, Operand &opnd0) { } Operand &opnd1 = CreateImmOperand(1, srcPty, false); opndVec.emplace_back(&opnd1); - SelectLibCall(funcName, opndVec, srcPty, retType); + std::vector opndTypes{retType, srcPty, srcPty}; + + SelectLibCall(funcName, opndVec, opndTypes); return &resOpnd; } @@ -5256,7 +5302,9 @@ Operand *AArch64CGFunc::SelectGCMalloc(GCMallocNode &node) { std::vector opndVec{ &resOpnd, &opndSize, &opndAlign }; const std::string &funcName = "MCC_NewObj"; - SelectLibCall(funcName, opndVec, PTY_u64, retType); + std::vector opndTypes{retType, PTY_u64, PTY_u64}; + + SelectLibCall(funcName, opndVec, opndTypes); return &resOpnd; } @@ -5292,9 +5340,10 @@ Operand *AArch64CGFunc::SelectJarrayMalloc(JarrayMallocNode &node, Operand &opnd RegOperand &resOpnd = CreateRegisterOperandOfType(retType); std::vector opndVec{ &resOpnd, &opndFixedSize, &opndElemSize, opndNElems64, &opndAlign }; + std::vector opndTypes{ retType, PTY_u64, PTY_u64, PTY_u64, PTY_u64 }; const std::string &funcName = "MCC_NewObj_flexible"; - SelectLibCall(funcName, opndVec, PTY_u64, retType); + SelectLibCall(funcName, opndVec, opndTypes); /* Generate the store of the object length field */ MemOperand &opndArrayLengthField = CreateMemOpnd(resOpnd, AArch64RTSupport::kArrayLengthOffset, k4BitSize); @@ -6468,7 +6517,9 @@ void AArch64CGFunc::CreateCallStructParamMemcpy(const MIRSymbol *sym, RegOperand GetCurBB()->AppendInsn(GetCG()->BuildInstruction(MOP_xmovri32, vreg3, sizeOpnd)); opndVec.push_back(&vreg3); /* param 2 */ - SelectLibCall("memcpy", opndVec, PTY_a64, PTY_a64); + std::vector opndTypes(4, PTY_a64); + + SelectLibCall("memcpy", opndVec, opndTypes); } AArch64RegOperand *AArch64CGFunc::CreateCallStructParamCopyToStack(uint32 numMemOp, MIRSymbol *sym, RegOperand *addrOpd, @@ -7809,8 +7860,8 @@ Operand &AArch64CGFunc::GetOrCreatevaryreg() { } /* the first operand in opndvec is return opnd */ -void AArch64CGFunc::SelectLibCall(const std::string &funcName, std::vector &opndVec, PrimType primType, - PrimType retPrimType, bool is2ndRet) { +void AArch64CGFunc::SelectLibCall(const std::string &funcName, std::vector &opndVec, + std::vector opndTypes, bool is2ndRet) { MIRSymbol *st = GlobalTables::GetGsymTable().CreateSymbol(kScopeGlobal); st->SetNameStrIdx(funcName); st->SetStorageClass(kScExtern); @@ -7818,12 +7869,13 @@ void AArch64CGFunc::SelectLibCall(const std::string &funcName, std::vector vec; std::vector vecAt; + //FATAL(opndVec.size() == opndTypes.size(), ""); for (size_t i = 1; i < opndVec.size(); ++i) { - vec.emplace_back(GlobalTables::GetTypeTable().GetTypeTable()[static_cast(primType)]->GetTypeIndex()); + vec.emplace_back(GlobalTables::GetTypeTable().GetTypeTable()[static_cast(opndTypes[i])]->GetTypeIndex()); vecAt.emplace_back(TypeAttrs()); } - MIRType *retType = GlobalTables::GetTypeTable().GetTypeTable().at(static_cast(primType)); + MIRType *retType = GlobalTables::GetTypeTable().GetTypeTable().at(static_cast(opndTypes[0])); st->SetTyIdx(GetBecommon().BeGetOrCreateFunctionType(retType->GetTypeIndex(), vec, vecAt)->GetTypeIndex()); if (GetCG()->GenerateVerboseCG()) { @@ -7833,22 +7885,22 @@ void AArch64CGFunc::SelectLibCall(const std::string &funcName, std::vectorNew(*GetFuncScopeAllocator()); for (size_t i = 1; i < opndVec.size(); ++i) { MIRType *ty; - ty = GlobalTables::GetTypeTable().GetTypeTable()[static_cast(primType)]; + ty = GlobalTables::GetTypeTable().GetTypeTable()[static_cast(opndTypes[i])]; Operand *stOpnd = opndVec[i]; if (stOpnd->GetKind() != Operand::kOpdRegister) { - stOpnd = &SelectCopy(*stOpnd, primType, primType); + stOpnd = &SelectCopy(*stOpnd, opndTypes[i], opndTypes[i]); } RegOperand *expRegOpnd = static_cast(stOpnd); parmLocator.LocateNextParm(*ty, ploc); if (ploc.reg0 != 0) { /* load to the register */ AArch64RegOperand &parmRegOpnd = - GetOrCreatePhysicalRegisterOperand(ploc.reg0, expRegOpnd->GetSize(), GetRegTyFromPrimTy(primType)); - SelectCopy(parmRegOpnd, primType, *expRegOpnd, primType); + GetOrCreatePhysicalRegisterOperand(ploc.reg0, expRegOpnd->GetSize(), GetRegTyFromPrimTy(opndTypes[i])); + SelectCopy(parmRegOpnd, opndTypes[i], *expRegOpnd, opndTypes[i]); srcOpnds->PushOpnd(parmRegOpnd); } ASSERT(ploc.reg1 == 0, "SelectCall NYI"); @@ -7856,7 +7908,7 @@ void AArch64CGFunc::SelectLibCall(const std::string &funcName, std::vectorGetStIdx(), false); Insn &callInsn = AppendCall(*sym, *srcOpnds); - MIRType *callRetType = GlobalTables::GetTypeTable().GetTypeTable().at(static_cast(retPrimType)); + MIRType *callRetType = GlobalTables::GetTypeTable().GetTypeTable().at(static_cast(opndTypes[0])); if (callRetType != nullptr) { callInsn.SetRetSize(callRetType->GetSize()); callInsn.SetIsCallReturnUnsigned(IsUnsignedInteger(callRetType->GetPrimType())); @@ -7864,7 +7916,7 @@ void AArch64CGFunc::SelectLibCall(const std::string &funcName, std::vectorGetRegisterNumber() != regNum) { AArch64RegOperand &retOpnd = GetOrCreatePhysicalRegisterOperand(regNum, regOpnd->GetSize(), - GetRegTyFromPrimTy(retPrimType)); - SelectCopy(*opnd0, retPrimType, retOpnd, retPrimType); + GetRegTyFromPrimTy(opndTypes[0])); + SelectCopy(*opnd0, opndTypes[0], retOpnd, opndTypes[0]); } } diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index 7e51397b40..194583521f 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -635,6 +635,26 @@ Operand *HandleIntrinOp(const BaseNode &parent, BaseNode &expr, CGFunc &cgFunc) return cgFunc.SelectCSyncLockRelease(intrinsicopNode, PTY_i64); case INTRN_C__builtin_return_address: return cgFunc.SelectCReturnAddress(intrinsicopNode); + case INTRN_C_strcpy: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strcpy"); + case INTRN_C_strncpy: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strncpy"); + case INTRN_C_strchr: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strchr"); + case INTRN_C_memcmp: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memcmp"); + case INTRN_C_memcpy: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memcpy"); + case INTRN_C_memset: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memset"); + case INTRN_C_strlen: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i64, "strlen"); + case INTRN_C_strcmp: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "strcmp"); + case INTRN_C_strncmp: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "strncmp"); + case INTRN_C_memmove: + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memmove"); case INTRN_vector_sum_v8u8: case INTRN_vector_sum_v8i8: case INTRN_vector_sum_v4u16: case INTRN_vector_sum_v4i16: case INTRN_vector_sum_v2u32: case INTRN_vector_sum_v2i32: diff --git a/src/mapleall/maple_ir/include/intrinsic_c.def b/src/mapleall/maple_ir/include/intrinsic_c.def index 343f18af9b..a67884515a 100644 --- a/src/mapleall/maple_ir/include/intrinsic_c.def +++ b/src/mapleall/maple_ir/include/intrinsic_c.def @@ -23,6 +23,8 @@ DEF_MIR_INTRINSIC(C_strcpy,\ "strcpy", 0, kArgTyVoid, kArgTyPtr, kArgTyPtr) DEF_MIR_INTRINSIC(C_strncpy,\ "strncpy", 0, kArgTyVoid, kArgTyPtr, kArgTyPtr, kArgTyU64) +DEF_MIR_INTRINSIC(C_strchr,\ + "strchr", 0, kArgTyVoid, kArgTyPtr, kArgTyI32) DEF_MIR_INTRINSIC(C_strlen,\ "strlen", INTRNNOSIDEEFFECT | INTRNISPURE, kArgTyU64, kArgTyPtr) DEF_MIR_INTRINSIC(C_memcmp,\ diff --git a/src/mplfe/ast_input/include/ast_builtin_func.def b/src/mplfe/ast_input/include/ast_builtin_func.def index 08747d4fbf..5cfac5db4e 100644 --- a/src/mplfe/ast_input/include/ast_builtin_func.def +++ b/src/mplfe/ast_input/include/ast_builtin_func.def @@ -1,15 +1,6 @@ // BUILTIN_FUNC(funcName) BUILTIN_FUNC(printf) BUILTIN_FUNC(snprintf) -BUILTIN_FUNC(strncpy) -BUILTIN_FUNC(strcpy) -BUILTIN_FUNC(strcmp) -BUILTIN_FUNC(strlen) -BUILTIN_FUNC(strchr) -BUILTIN_FUNC(memcmp) -BUILTIN_FUNC(memcpy) -BUILTIN_FUNC(memset) -BUILTIN_FUNC(memmove) BUILTIN_FUNC(__memcpy_chk) BUILTIN_FUNC(__memset_chk) BUILTIN_FUNC(abs) diff --git a/src/mplfe/ast_input/include/ast_expr.h b/src/mplfe/ast_input/include/ast_expr.h index 4c913266f3..2ec9d17058 100644 --- a/src/mplfe/ast_input/include/ast_expr.h +++ b/src/mplfe/ast_input/include/ast_expr.h @@ -1213,6 +1213,17 @@ class ASTCallExpr : public ASTExpr { UniqueFEIRExpr EMIT_BUILTIIN_FUNC(SyncLockRelease4); UniqueFEIRExpr EMIT_BUILTIIN_FUNC(ReturnAddress); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Strcpy); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Strncpy); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Strchr); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Memcmp); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Memcpy); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Memset); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Strlen); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Strcmp); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Strncmp); + UniqueFEIRExpr EMIT_BUILTIIN_FUNC(Memmove); + // vector builtinfunc #define DEF_MIR_INTRINSIC(STR, NAME, INTRN_CLASS, RETURN_TYPE, ...) \ UniqueFEIRExpr EmitBuiltin##STR(std::list &stmts) const; diff --git a/src/mplfe/ast_input/include/builtin_func_emit.def b/src/mplfe/ast_input/include/builtin_func_emit.def index c51a39b661..bd90b8219a 100644 --- a/src/mplfe/ast_input/include/builtin_func_emit.def +++ b/src/mplfe/ast_input/include/builtin_func_emit.def @@ -128,3 +128,24 @@ BUILTIN_FUNC_EMIT("__sync_lock_test_and_set_4", &ASTCallExpr::EmitBuiltinSyncLoc BUILTIN_FUNC_EMIT("__sync_lock_release_8", &ASTCallExpr::EmitBuiltinSyncLockRelease8) BUILTIN_FUNC_EMIT("__sync_lock_release_4", &ASTCallExpr::EmitBuiltinSyncLockRelease4) BUILTIN_FUNC_EMIT("__builtin_return_address", &ASTCallExpr::EmitBuiltinReturnAddress) + +BUILTIN_FUNC_EMIT("__builtin_strcpy", &ASTCallExpr::EmitBuiltinStrcpy) +BUILTIN_FUNC_EMIT("strcpy", &ASTCallExpr::EmitBuiltinStrcpy) +BUILTIN_FUNC_EMIT("__builtin_strncpy", &ASTCallExpr::EmitBuiltinStrncpy) +BUILTIN_FUNC_EMIT("strncpy", &ASTCallExpr::EmitBuiltinStrncpy) +BUILTIN_FUNC_EMIT("__builtin_strchr", &ASTCallExpr::EmitBuiltinStrchr) +BUILTIN_FUNC_EMIT("strchr", &ASTCallExpr::EmitBuiltinStrchr) +BUILTIN_FUNC_EMIT("__builtin_memcmp", &ASTCallExpr::EmitBuiltinMemcmp) +BUILTIN_FUNC_EMIT("memcmp", &ASTCallExpr::EmitBuiltinMemcmp) +BUILTIN_FUNC_EMIT("__builtin_memcpy", &ASTCallExpr::EmitBuiltinMemcpy) +BUILTIN_FUNC_EMIT("memcpy", &ASTCallExpr::EmitBuiltinMemcpy) +BUILTIN_FUNC_EMIT("__builtin_memset", &ASTCallExpr::EmitBuiltinMemset) +BUILTIN_FUNC_EMIT("memset", &ASTCallExpr::EmitBuiltinMemset) +BUILTIN_FUNC_EMIT("__builtin_strlen", &ASTCallExpr::EmitBuiltinStrlen) +BUILTIN_FUNC_EMIT("strlen", &ASTCallExpr::EmitBuiltinStrlen) +BUILTIN_FUNC_EMIT("__builtin_strcmp", &ASTCallExpr::EmitBuiltinStrcmp) +BUILTIN_FUNC_EMIT("strcmp", &ASTCallExpr::EmitBuiltinStrcmp) +BUILTIN_FUNC_EMIT("__builtin_strncmp", &ASTCallExpr::EmitBuiltinStrncmp) +BUILTIN_FUNC_EMIT("strncmp", &ASTCallExpr::EmitBuiltinStrncmp) +BUILTIN_FUNC_EMIT("__builtin_memmove", &ASTCallExpr::EmitBuiltinMemmove) +BUILTIN_FUNC_EMIT("memmove", &ASTCallExpr::EmitBuiltinMemmove) \ No newline at end of file diff --git a/src/mplfe/ast_input/src/ast_parser_builting_func.cpp b/src/mplfe/ast_input/src/ast_parser_builting_func.cpp index 7131011167..5f31e2aa73 100644 --- a/src/mplfe/ast_input/src/ast_parser_builting_func.cpp +++ b/src/mplfe/ast_input/src/ast_parser_builting_func.cpp @@ -613,6 +613,45 @@ UniqueFEIRExpr ASTCallExpr::EmitBuiltinRotate(std::list &stmts, maskExpr->Clone()))); } +UniqueFEIRExpr ASTCallExpr::EmitBuiltinStrcpy(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_strcpy); +} +UniqueFEIRExpr ASTCallExpr::EmitBuiltinStrncpy(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_strncpy); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinMemcpy(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_memcpy); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinMemset(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_memset); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinMemcmp(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_memcmp); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinStrchr(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_strchr); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinStrlen(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_strlen); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinStrcmp(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_strcmp); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinStrncmp(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_strncmp); +} + +UniqueFEIRExpr ASTCallExpr::EmitBuiltinMemmove(std::list &stmts) const { + return CreateIntrinsicopForC(stmts, INTRN_C_memmove); +} + std::map ASTParser::InitBuiltinFuncPtrMap() { std::map ans; #define BUILTIN_FUNC_PARSE(funcName, FuncPtrBuiltinFunc) \ -- Gitee From 38cace850556e0cbd0ba1acffd4ce4e896bd84e9 Mon Sep 17 00:00:00 2001 From: MarkFedoseev Date: Sat, 11 Sep 2021 17:39:53 +0300 Subject: [PATCH 2/2] small fix --- .../src/cg/aarch64/aarch64_cgfunc.cpp | 19 ++++++++++++------- src/mapleall/maple_be/src/cg/cgfunc.cpp | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp index d4eeaeeca2..4b16954651 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -1530,6 +1530,7 @@ void AArch64CGFunc::SelectAggDassign(DassignNode &stmt) { if (lhsSize > kParmMemcpySize) { std::vector opndVec; RegOperand *regResult = &CreateVirtualRegisterOperand(NewVReg(kRegTyInt, k8ByteSize)); + #define NUM_OF_OPERANDS 4 opndVec.push_back(regResult); /* result */ opndVec.push_back(PrepareMemcpyParamOpnd(lhsIsLo12, *lhsSymbol, lhsOffsetVal, *lhsBaseReg)); /* param 0 */ @@ -1538,7 +1539,7 @@ void AArch64CGFunc::SelectAggDassign(DassignNode &stmt) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - std::vector opndTypes(4, PTY_a64); + std::vector opndTypes(NUM_OF_OPERANDS, PTY_a64); SelectLibCall("memcpy", opndVec, opndTypes); @@ -1653,6 +1654,7 @@ void AArch64CGFunc::SelectAggDassign(DassignNode &stmt) { if (lhsSize > kParmMemcpySize) { std::vector opndVec; RegOperand *regResult = &CreateVirtualRegisterOperand(NewVReg(kRegTyInt, k8ByteSize)); + #define NUM_OF_OPERANDS 4 opndVec.push_back(regResult); /* result */ opndVec.push_back(PrepareMemcpyParamOpnd(lhsIsLo12, *lhsSymbol, lhsOffsetVal, *lhsBaseReg)); /* param 0 */ @@ -1661,7 +1663,7 @@ void AArch64CGFunc::SelectAggDassign(DassignNode &stmt) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - std::vector opndTypes(4, PTY_a64); + std::vector opndTypes(NUM_OF_OPERANDS, PTY_a64); SelectLibCall("memcpy", opndVec, opndTypes); @@ -2031,6 +2033,7 @@ void AArch64CGFunc::SelectAggIassign(IassignNode &stmt, Operand &AddrOpnd) { if (lhsSize > kParmMemcpySize) { std::vector opndVec; RegOperand *regResult = &CreateVirtualRegisterOperand(NewVReg(kRegTyInt, k8ByteSize)); + #define NUM_OF_OPERANDS 4 opndVec.push_back(regResult); /* result */ opndVec.push_back(PrepareMemcpyParamOpnd(static_cast(lhsOffset), lhsAddrOpnd)); /* param 0 */ @@ -2039,7 +2042,7 @@ void AArch64CGFunc::SelectAggIassign(IassignNode &stmt, Operand &AddrOpnd) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - std::vector opndTypes(4, PTY_a64); + std::vector opndTypes(NUM_OF_OPERANDS, PTY_a64); SelectLibCall("memcpy", opndVec, opndTypes); @@ -2169,6 +2172,7 @@ void AArch64CGFunc::SelectAggIassign(IassignNode &stmt, Operand &AddrOpnd) { if (lhsSize > kParmMemcpySize) { std::vector opndVec; RegOperand *regResult = &CreateVirtualRegisterOperand(NewVReg(kRegTyInt, k8ByteSize)); + #define NUM_OF_OPERANDS 4 opndVec.push_back(regResult); /* result */ opndVec.push_back(PrepareMemcpyParamOpnd(static_cast(lhsOffset), lhsAddrOpnd)); /* param 0 */ @@ -2177,7 +2181,7 @@ void AArch64CGFunc::SelectAggIassign(IassignNode &stmt, Operand &AddrOpnd) { opndVec.push_back(PrepareMemcpyParamOpnd(lhsSize)); /* param 2 */ - std::vector opndTypes(4, PTY_a64); + std::vector opndTypes(NUM_OF_OPERANDS, PTY_a64); SelectLibCall("memcpy", opndVec, opndTypes); @@ -4643,10 +4647,11 @@ Operand *AArch64CGFunc::SelectIntrinsicOpWithOneParam(IntrinsicopNode &intrnNode } std::vector opndVec; RegOperand *dst = &CreateRegisterOperandOfType(ptype); + #define NUM_OF_OPERANDS 2 opndVec.push_back(dst); /* result */ opndVec.push_back(opnd); /* param 0 */ - std::vector opndTypes(2, ptype); + std::vector opndTypes(NUM_OF_OPERANDS, ptype); SelectLibCall(name, opndVec, opndTypes); @@ -6516,8 +6521,8 @@ void AArch64CGFunc::CreateCallStructParamMemcpy(const MIRSymbol *sym, RegOperand AArch64ImmOperand &sizeOpnd = CreateImmOperand(structSize, k64BitSize, false); GetCurBB()->AppendInsn(GetCG()->BuildInstruction(MOP_xmovri32, vreg3, sizeOpnd)); opndVec.push_back(&vreg3); /* param 2 */ - - std::vector opndTypes(4, PTY_a64); + #define NUM_OF_OPERANDS 4 + std::vector opndTypes(NUM_OF_OPERANDS, PTY_a64); SelectLibCall("memcpy", opndVec, opndTypes); } diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index 194583521f..94f3fd93df 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -642,13 +642,13 @@ Operand *HandleIntrinOp(const BaseNode &parent, BaseNode &expr, CGFunc &cgFunc) case INTRN_C_strchr: return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strchr"); case INTRN_C_memcmp: - return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memcmp"); + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "memcmp"); case INTRN_C_memcpy: return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memcpy"); case INTRN_C_memset: return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "memset"); case INTRN_C_strlen: - return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i64, "strlen"); + return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_a64, "strlen"); case INTRN_C_strcmp: return cgFunc.SelectIntrinsicOpWithNParams(intrinsicopNode, PTY_i32, "strcmp"); case INTRN_C_strncmp: -- Gitee