# SPDX-License-Identifier: GPL-2.0-only OR License-Ref-kk-custom
#
# Copyright (C) 2023 Kernkonzept GmbH.
# Author(s): Georg Kotheimer <georg.kotheimer@kernkonzept.com>

ifneq ($(filter armv4 armv5 armv6,$(CPU)),)
  BUILTINS_SRC += $(arm_min_SOURCES)
else
  BUILTINS_SRC += $(arm_SOURCES)
endif

define arm_or_thumb2_base_SOURCES
  arm/fp_mode.c
  arm/bswapdi2.S
  arm/bswapsi2.S
  arm/clzdi2.S
  arm/clzsi2.S
  arm/comparesf2.S
  arm/divmodsi4.S
  arm/divsi3.S
  arm/modsi3.S
  arm/udivmodsi4.S
  arm/udivsi3.S
  arm/umodsi3.S
endef

define arm_sync_SOURCES
  arm/sync_fetch_and_add_4.S
  arm/sync_fetch_and_add_8.S
  arm/sync_fetch_and_and_4.S
  arm/sync_fetch_and_and_8.S
  arm/sync_fetch_and_max_4.S
  arm/sync_fetch_and_max_8.S
  arm/sync_fetch_and_min_4.S
  arm/sync_fetch_and_min_8.S
  arm/sync_fetch_and_nand_4.S
  arm/sync_fetch_and_nand_8.S
  arm/sync_fetch_and_or_4.S
  arm/sync_fetch_and_or_8.S
  arm/sync_fetch_and_sub_4.S
  arm/sync_fetch_and_sub_8.S
  arm/sync_fetch_and_umax_4.S
  arm/sync_fetch_and_umax_8.S
  arm/sync_fetch_and_umin_4.S
  arm/sync_fetch_and_umin_8.S
  arm/sync_fetch_and_xor_4.S
  arm/sync_fetch_and_xor_8.S
endef

define arm_EABI_SOURCES
  arm/aeabi_cdcmp.S
  arm/aeabi_cdcmpeq_check_nan.c
  arm/aeabi_cfcmp.S
  arm/aeabi_cfcmpeq_check_nan.c
  arm/aeabi_dcmp.S
  arm/aeabi_div0.c
  arm/aeabi_drsub.c
  arm/aeabi_fcmp.S
  arm/aeabi_frsub.c
  arm/aeabi_idivmod.S
  arm/aeabi_ldivmod.S
  arm/aeabi_memcmp.S
  arm/aeabi_memcpy.S
  arm/aeabi_memmove.S
  arm/aeabi_memset.S
  arm/aeabi_uidivmod.S
  arm/aeabi_uldivmod.S
endef

define arm_Thumb1_JT_SOURCES
  arm/switch16.S
  arm/switch32.S
  arm/switch8.S
  arm/switchu8.S
endef

define arm_Thumb1_SjLj_EH_SOURCES
  arm/restore_vfp_d8_d15_regs.S
  arm/save_vfp_d8_d15_regs.S
endef

define arm_Thumb1_VFPv2_DP_SOURCES
  arm/adddf3vfp.S
  arm/divdf3vfp.S
  arm/eqdf2vfp.S
  arm/extendsfdf2vfp.S
  arm/fixdfsivfp.S
  arm/fixunsdfsivfp.S
  arm/floatsidfvfp.S
  arm/floatunssidfvfp.S
  arm/gedf2vfp.S
  arm/gtdf2vfp.S
  arm/ledf2vfp.S
  arm/ltdf2vfp.S
  arm/muldf3vfp.S
  arm/nedf2vfp.S
  arm/negdf2vfp.S
  arm/subdf3vfp.S
  arm/truncdfsf2vfp.S
  arm/unorddf2vfp.S
endef

define arm_Thumb1_VFPv2_SP_SOURCES
  arm/addsf3vfp.S
  arm/divsf3vfp.S
  arm/eqsf2vfp.S
  arm/fixsfsivfp.S
  arm/fixunssfsivfp.S
  arm/floatsisfvfp.S
  arm/floatunssisfvfp.S
  arm/gesf2vfp.S
  arm/gtsf2vfp.S
  arm/lesf2vfp.S
  arm/ltsf2vfp.S
  arm/mulsf3vfp.S
  arm/negsf2vfp.S
  arm/nesf2vfp.S
  arm/subsf3vfp.S
  arm/unordsf2vfp.S
endef

define arm_Thumb1_icache_SOURCES
  arm/sync_synchronize.S
endef

define arm_Thumb1_SOURCES
  $(arm_Thumb1_JT_SOURCES)
  $(arm_Thumb1_icache_SOURCES)
endef

define arm_min_SOURCES
  $(arm_or_thumb2_base_SOURCES)
  $(arm_EABI_SOURCES)
endef

define arm_SOURCES
  $(arm_or_thumb2_base_SOURCES)
  $(arm_sync_SOURCES)
  $(arm_EABI_SOURCES)
  $(arm_Thumb1_SOURCES)
endef

ifneq ($(call libclang_rt_is_predefined,__ARM_FP),)
  arm_Thumb1_SOURCES += $(arm_Thumb1_SjLj_EH_SOURCES) $(arm_Thumb1_VFPv2_SP_SOURCES)

  # Add double-precision builtins only if VFP is not single-precision-only.
  ifeq ($(call libclang_rt_try_compile,#if !(__ARM_FP & 0x8) #error No VFP DP! #endif),Ok)
    arm_Thumb1_SOURCES += $(arm_Thumb1_VFPv2_DP_SOURCES)
  endif
endif
