https://gcc.gnu.org/g:cde452e270e500db776e75d0bdda0b32d9c37981

commit r15-7293-gcde452e270e500db776e75d0bdda0b32d9c37981
Author: Georg-Johann Lay <a...@gjlay.de>
Date:   Thu Jan 30 12:05:19 2025 +0100

    AVR: Only provide a built-in when it is available.
    
    Some built-ins are not available for C++ since they are using
    named address-spaces or fixed-point types.
    
    gcc/
            * config/avr/builtins.def (AVR_FIRST_C_ONLY_BUILTIN_ID): New macro.
            * config/avr/avr-protos.h (avr_builtin_supported_p): New.
            * config/avr/avr.cc (avr_builtin_supported_p): New function.
            (avr_init_builtins): Only provide a built-in when it is supported.
            * config/avr/avr-c.cc (avr_cpu_cpp_builtins): Only define the
            __BUILTIN_AVR_<NAME> build-in defines when the associated built-in
            function is supported.
            * doc/extend.texi (AVR Built-in Functions): Add a note that
            following built-ins are supported for only for GNU-C.

Diff:
---
 gcc/config/avr/avr-c.cc     |  3 ++-
 gcc/config/avr/avr-protos.h |  1 +
 gcc/config/avr/avr.cc       | 25 ++++++++++++++++++++++---
 gcc/config/avr/builtins.def |  7 +++++++
 gcc/doc/extend.texi         | 18 ++++++++++--------
 5 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/gcc/config/avr/avr-c.cc b/gcc/config/avr/avr-c.cc
index 53f15f2be7b3..f4236555bf6c 100644
--- a/gcc/config/avr/avr-c.cc
+++ b/gcc/config/avr/avr-c.cc
@@ -500,7 +500,8 @@ avr_cpu_cpp_builtins (cpp_reader *pfile)
      not a specific builtin is available. */
 
 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME, ATTRS) \
-  cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
+  if (avr_builtin_supported_p (AVR_BUILTIN_ ## NAME))        \
+    cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
 #include "builtins.def"
 #undef DEF_BUILTIN
 
diff --git a/gcc/config/avr/avr-protos.h b/gcc/config/avr/avr-protos.h
index 83137c7f6f63..6f37c48143ec 100644
--- a/gcc/config/avr/avr-protos.h
+++ b/gcc/config/avr/avr-protos.h
@@ -21,6 +21,7 @@
 
 extern bool avr_function_arg_regno_p (int r);
 extern void avr_cpu_cpp_builtins (cpp_reader * pfile);
+extern bool avr_builtin_supported_p (unsigned id);
 extern enum reg_class avr_regno_reg_class (int r);
 extern void asm_globalize_label (FILE *file, const char *name);
 extern void avr_adjust_reg_alloc_order (void);
diff --git a/gcc/config/avr/avr.cc b/gcc/config/avr/avr.cc
index 656d3e7389b4..2b550e7761c8 100644
--- a/gcc/config/avr/avr.cc
+++ b/gcc/config/avr/avr.cc
@@ -15689,6 +15689,24 @@ avr_bdesc[AVR_BUILTIN_COUNT] =
   };
 
 
+/* Some of our built-in function are available for GNU-C only:
+   - Built-ins that use named address-spaces.
+   - Built-ins that use fixed-point types.  */
+
+bool
+avr_builtin_supported_p (unsigned id)
+{
+  const bool uses_as = id == AVR_BUILTIN_FLASH_SEGMENT;
+
+  // We don't support address-spaces on Reduced Tiny.
+  if (AVR_TINY && uses_as)
+    return false;
+
+  return (lang_GNU_C ()
+         || id < AVR_FIRST_C_ONLY_BUILTIN_ID);
+}
+
+
 /* Implement `TARGET_BUILTIN_DECL'.  */
 
 static tree
@@ -15891,9 +15909,10 @@ avr_init_builtins (void)
     char *name = (char *) alloca (1 + strlen (Name));                  \
                                                                        \
     gcc_assert (id < AVR_BUILTIN_COUNT);                               \
-    avr_bdesc[id].fndecl                                               \
-      = add_builtin_function (avr_tolower (name, Name), TYPE, id,      \
-                             BUILT_IN_MD, LIBNAME, ATTRS);             \
+    avr_bdesc[id].fndecl = avr_builtin_supported_p (id)                        
\
+      ? add_builtin_function (avr_tolower (name, Name), TYPE, id,      \
+                             BUILT_IN_MD, LIBNAME, ATTRS)              \
+      : NULL_TREE;                                                     \
   }
 #include "builtins.def"
 #undef DEF_BUILTIN
diff --git a/gcc/config/avr/builtins.def b/gcc/config/avr/builtins.def
index 61dbc3a6c1be..ad75fe9c267c 100644
--- a/gcc/config/avr/builtins.def
+++ b/gcc/config/avr/builtins.def
@@ -34,6 +34,8 @@
    ATTRS:   Function attributes like "attr_const" for the `const' attribute
             or "NULL_TREE" for no attribute.  */
 
+#define AVR_FIRST_C_ONLY_BUILTIN_ID AVR_BUILTIN_FLASH_SEGMENT
+
 /* Mapped to respective instruction.  */
 
 DEF_BUILTIN (NOP,  -1, void_ftype_void, nothing, NULL, NULL_TREE)
@@ -56,6 +58,11 @@ DEF_BUILTIN (DELAY_CYCLES, -1, void_ftype_uintSI, nothing, 
NULL, NULL_TREE)
 DEF_BUILTIN (NOPS,         -1, void_ftype_uintSI, nothing, NULL, NULL_TREE)
 DEF_BUILTIN (MASK1,       2, uintQI_ftype_uintQI_uintQI, gen_mask1, NULL, 
attr_const)
 DEF_BUILTIN (INSERT_BITS, 3, uintQI_ftype_uintSI_uintQI_uintQI, insert_bits, 
NULL, attr_const)
+
+/* All following built-ins are C only, see avr.cc::avr_builtin_C_only_p()
+ * since they are using named address-spaces or fixed-point types, none
+ * of which are supported for C++.  */
+
 DEF_BUILTIN (FLASH_SEGMENT, 1, intQI_ftype_const_memx_ptr, flash_segment, 
NULL, attr_const)
 
 /* ISO/IEC TR 18037 "Embedded C"
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 2764597a479b..c6e7bc37f7dc 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -17369,14 +17369,6 @@ might increase delay time. @var{ticks} must be a 
compile-time
 integer constant; delays with a variable number of cycles are not supported.
 @enddefbuiltin
 
-@defbuiltin{int8_t __builtin_avr_flash_segment (const __memx void*)}
-This built-in takes a byte address to the 24-bit
-@ref{AVR Named Address Spaces,address space} @code{__memx} and returns
-the number of the flash segment (the 64 KiB chunk) where the address
-points to.  Counting starts at @code{0}.
-If the address does not point to flash memory, return @code{-1}.
-@enddefbuiltin
-
 @defbuiltin{uint8_t __builtin_avr_insert_bits (uint32_t @var{map}, uint8_t 
@var{bits}, uint8_t @var{val})}
 Insert bits from @var{bits} into @var{val} and return the resulting
 value. The nibbles of @var{map} determine how the insertion is
@@ -17445,6 +17437,16 @@ Insert @var{count} @code{NOP} instructions.
 The number of instructions must be a compile-time integer constant.
 @enddefbuiltin
 
+@b{All of the following built-in functions are only available for GNU-C}
+
+@defbuiltin{int8_t __builtin_avr_flash_segment (const __memx void*)}
+This built-in takes a byte address to the 24-bit
+@ref{AVR Named Address Spaces,named address space} @code{__memx} and returns
+the number of the flash segment (the 64 KiB chunk) where the address
+points to.  Counting starts at @code{0}.
+If the address does not point to flash memory, return @code{-1}.
+@enddefbuiltin
+
 @noindent
 There are many more AVR-specific built-in functions that are used to
 implement the ISO/IEC TR 18037 ``Embedded C'' fixed-point functions of

Reply via email to