https://gcc.gnu.org/g:8e3b5a74145c1db9c7af8c5037705b313c3efe11

commit r14-10972-g8e3b5a74145c1db9c7af8c5037705b313c3efe11
Author: Gaius Mulley <gaiusm...@gmail.com>
Date:   Sat Nov 23 14:27:06 2024 +0000

    [PATCH] PR modula2/115823 Wrong expansion of isnormal optab
    
    The bug fix changes gcc/m2/gm2-gcc/m2builtins.c:m2builtins_BuiltinExists
    to recognise both __builtin_<functionname> and functionname as a builtin.
    
    gcc/m2/ChangeLog:
    
            PR modula2/115823
            * gm2-gcc/m2builtins.cc (struct builtin_macro_definition): New
            field builtinname.
            (builtin_function_match): New function.
            (builtin_macro_match): Ditto.
            (m2builtins_BuiltinExists): Use builtin_function_match and
            builtin_macro_match.
            (lookup_builtin_macro): Use builtin_macro_match.
            (lookup_builtin_function): Use builtin_function_match.
            (define_builtin): Assign builtinname field.
    
    gcc/testsuite/ChangeLog:
    
            PR modula2/115823
            * gm2/builtins/run/pass/testalloa.mod: New test.
    
    (cherry picked from commit 2d1f68e7965795dc66db83bc7840ba7a23eeb01b)
    
    Signed-off-by: Gaius Mulley <gaiusm...@gmail.com>

Diff:
---
 gcc/m2/gm2-gcc/m2builtins.cc                      | 31 ++++++++++++---
 gcc/testsuite/gm2/builtins/run/pass/testalloa.mod | 47 +++++++++++++++++++++++
 2 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/gcc/m2/gm2-gcc/m2builtins.cc b/gcc/m2/gm2-gcc/m2builtins.cc
index 31c344c4a59e..e3e55a699917 100644
--- a/gcc/m2/gm2-gcc/m2builtins.cc
+++ b/gcc/m2/gm2-gcc/m2builtins.cc
@@ -396,6 +396,7 @@ struct builtin_type_info
 struct GTY(()) builtin_macro_definition
 {
   const char *name;
+  const char *builtinname;
   tree function_node;
   tree return_node;
 };
@@ -911,6 +912,26 @@ target_support_exists (struct builtin_function_entry *fe)
     }
 }
 
+/* Return true if name matches the builtin name.  */
+
+static
+bool builtin_function_match (struct builtin_function_entry *fe,
+                            const char *name)
+{
+  return (strcmp (name, fe->name) == 0)
+    || (strcmp (name, fe->library_name) == 0);
+}
+
+/* Return true if name matches the builtin macro name.  */
+
+static
+bool builtin_macro_match (builtin_macro_definition bmd,
+                         const char *name)
+{
+  return (strcmp (bmd.name, name) == 0)
+    || (strcmp (bmd.builtinname, name) == 0);
+}
+
 
 /* BuiltinExists - returns TRUE if the builtin function, name, exists
    for this target architecture.  */
@@ -921,12 +942,11 @@ m2builtins_BuiltinExists (char *name)
   struct builtin_function_entry *fe;
 
   for (fe = &list_of_builtins[0]; fe->name != NULL; fe++)
-    if (strcmp (name, fe->name) == 0)
+    if (builtin_function_match (fe, name))
       return true;
-      // return target_support_exists (fe);
   int length = vec_safe_length (builtin_macros);
   for (int idx = 0; idx < length; idx++)
-    if (strcmp ((*builtin_macros)[idx].name, name) == 0)
+    if (builtin_macro_match ((*builtin_macros)[idx], name))
       return true;
   return false;
 }
@@ -939,7 +959,7 @@ lookup_builtin_macro (location_t location, char *name)
 {
   int length = vec_safe_length (builtin_macros);
   for (int idx = 0; idx < length; idx++)
-    if (strcmp ((*builtin_macros)[idx].name, name) == 0)
+    if (builtin_macro_match ((*builtin_macros)[idx], name))
       {
         tree functype = TREE_TYPE ((*builtin_macros)[idx].function_node);
         tree funcptr = build1 (ADDR_EXPR, build_pointer_type (functype),
@@ -965,7 +985,7 @@ lookup_builtin_function (location_t location, char *name)
   struct builtin_function_entry *fe;
 
   for (fe = &list_of_builtins[0]; fe->name != NULL; fe++)
-    if ((strcmp (name, fe->name) == 0) && target_support_exists (fe))
+    if (builtin_function_match (fe, name) && target_support_exists (fe))
       {
         tree functype = TREE_TYPE (fe->function_node);
         tree funcptr = build1 (ADDR_EXPR, build_pointer_type (functype),
@@ -1422,6 +1442,7 @@ define_builtin (enum built_in_function val, const char 
*name, tree prototype,
   set_call_expr_flags (decl, flags);
   set_builtin_decl (val, decl, true);
   bmd.name = name;
+  bmd.builtinname = libname;
   bmd.function_node = decl;
   bmd.return_node = TREE_TYPE (prototype);
   vec_safe_push (builtin_macros, bmd);
diff --git a/gcc/testsuite/gm2/builtins/run/pass/testalloa.mod 
b/gcc/testsuite/gm2/builtins/run/pass/testalloa.mod
new file mode 100644
index 000000000000..9d88dbae3043
--- /dev/null
+++ b/gcc/testsuite/gm2/builtins/run/pass/testalloa.mod
@@ -0,0 +1,47 @@
+MODULE testalloa ;
+
+FROM libc IMPORT printf, exit ;
+FROM Builtins IMPORT alloca ;
+FROM SYSTEM IMPORT ADR, ADDRESS ;
+
+
+(*
+   assert -
+*)
+
+PROCEDURE assert (value: BOOLEAN; message: ARRAY OF CHAR) ;
+BEGIN
+   IF NOT value
+   THEN
+      printf ("test failed: %s\n", ADR (message)) ;
+      code := 1
+   END
+END assert ;
+
+
+(*
+   test -
+*)
+
+PROCEDURE test ;
+VAR
+   ptr: ADDRESS ;
+BEGIN
+   ptr := alloca (10) ;
+   assert (ptr # NIL, "alloca (10) # NIL")
+END test ;
+
+
+VAR
+   code: INTEGER ;
+BEGIN
+   code := 0 ;
+   test ;
+   IF code = 0
+   THEN
+      printf ("all tests pass\n")
+   ELSE
+      printf ("some tests failed\n")
+   END ;
+   exit (code)
+END testalloa.

Reply via email to