public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] gdb: LoongArch: Handle special struct in dummy call
@ 2023-10-08  7:32 Hui Li
  2023-10-11  1:13 ` Tiezhu Yang
  0 siblings, 1 reply; 2+ messages in thread
From: Hui Li @ 2023-10-08  7:32 UTC (permalink / raw)
  To: gdb-patches

When execute the following command on LoongArch:

  make check-gdb TESTS="gdb.base/infcall-nested-structs-c++.exp"

there exist some failed testcases:

  === gdb Summary ===

  # of expected passes		5533
  # of unexpected failures	367

The root cause is related with a struct containing floating-point
members as function argument or return value for a dummy call.

(1) Structure consists of one floating-point member within FRLEN bits
    wide, it is passed in an FAR if available.
(2) Structure consists of two floating-point members both within FRLEN
    bits wide, it is passed in two FARs if available.
(3) Structure consists of one integer member within GRLEN bits wide and
    one floating-point member within FRLEN bits wide, it is passed in a
    GAR and an FAR if available.

Note that in the above cases, empty structure or union members are also
ignored even in C++.

Here is a simple test on LoongArch:

  loongson@bogon:~$ cat test.c

  #include<stdio.h>

  struct test {
	  long   a;
	  double b __attribute__((aligned(16)));
  };
  struct test val = { 88, 99.99 };
  int check_arg_struct (struct test arg)
    {
      printf("arg.a = %ld\n", arg.a);
      printf("arg.b = %f\n", arg.b);
      printf("sizeof(val) = %d\n", sizeof(val));
      return 1;
    }
  int main()
  {
     check_arg_struct (val);
     return 0;
  }
  loongson@bogon:~$ gcc -g test.c -o test
  loongson@bogon:~$ ./test
  arg.a = 88
  arg.b = 99.990000
  sizeof(val) = 32

Before:

loongson@bogon:~$ gdb test
...
(gdb) start
...
Temporary breakpoint 1, main () at test.c:19
19	   check_arg_struct (val);
(gdb) p check_arg_struct (val)
arg.a = 140737488286128
arg.b = -nan
sizeof(val) = 32
$1 = 1
...

After:

loongson@bogon:~$ gdb test
...
(gdb) start
...
Temporary breakpoint 1, main () at test.c:19
19	   check_arg_struct (val);
(gdb) p check_arg_struct (val)
arg.a = 88
arg.b = 99.990000
sizeof(val) = 32
$1 = 1
...

With this patch, there are no failed testcases:

  make check-gdb TESTS="gdb.base/infcall-nested-structs-c++.exp"

   === gdb Summary ===

   # of expected passes		5900

Signed-off-by: Hui Li <lihui@loongson.cn>
---
 gdb/loongarch-tdep.c | 205 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 189 insertions(+), 16 deletions(-)

diff --git a/gdb/loongarch-tdep.c b/gdb/loongarch-tdep.c
index 62c6f9b220e..c65e2414bf8 100644
--- a/gdb/loongarch-tdep.c
+++ b/gdb/loongarch-tdep.c
@@ -516,7 +516,8 @@ static void
 compute_struct_member (struct type *type,
 		       unsigned int *fixed_point_members,
 		       unsigned int *floating_point_members,
-		       bool *first_member_is_fixed_point)
+		       bool *first_member_is_fixed_point,
+		       bool *has_long_double)
 {
   for (int i = 0; i < type->num_fields (); i++)
     {
@@ -526,6 +527,12 @@ compute_struct_member (struct type *type,
 
       struct type *field_type = check_typedef (type->field (i).type ());
 
+      if ((field_type->code () == TYPE_CODE_FLT
+	   && field_type->length () == 16)
+	  || (field_type->code () == TYPE_CODE_COMPLEX
+	      && field_type->length () == 32))
+	*has_long_double = true;
+
       if (field_type->code () == TYPE_CODE_INT
 	  || field_type->code () == TYPE_CODE_BOOL
 	  || field_type->code () == TYPE_CODE_CHAR
@@ -544,12 +551,79 @@ compute_struct_member (struct type *type,
 	compute_struct_member (field_type,
 			       fixed_point_members,
 			       floating_point_members,
-			       first_member_is_fixed_point);
+			       first_member_is_fixed_point,
+			       has_long_double);
       else if (field_type->code () == TYPE_CODE_COMPLEX)
 	(*floating_point_members) += 2;
     }
 }
 
+/* Compute the lengths and offsets of struct member.  */
+
+static void
+struct_member_info (struct type *type,
+		    unsigned int *member_offsets,
+		    unsigned int *member_lens,
+		    unsigned int offset,
+		    unsigned int *fields)
+{
+  unsigned int count = type->num_fields ();
+  unsigned int i;
+
+  for (i = 0; i < count; ++i)
+    {
+      if (type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
+	continue;
+
+      struct type *field_type = check_typedef (type->field (i).type ());
+      int field_offset
+	= offset + type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
+
+      switch (field_type->code ())
+	{
+	case TYPE_CODE_STRUCT:
+	  struct_member_info (field_type, member_offsets, member_lens,
+			      field_offset, fields);
+	  break;
+
+	case TYPE_CODE_COMPLEX:
+	  if (*fields == 0)
+	    {
+	      /* _Complex float */
+	      if (field_type->length () == 8)
+		{
+		  member_offsets[0] = field_offset;
+		  member_offsets[1] = field_offset + 4;
+		  member_lens[0] = member_lens[1] = 4;
+		  *fields = 2;
+		}
+	      /* _Complex double */
+	      else if (field_type->length () == 16)
+		{
+		  member_offsets[0] = field_offset;
+		  member_offsets[1] = field_offset + 8;
+		  member_lens[0] = member_lens[1] = 8;
+		  *fields = 2;
+		}
+	    }
+	  break;
+
+	default:
+	  if (*fields < 2)
+	    {
+	      member_offsets[*fields] = field_offset;
+	      member_lens[*fields] = field_type->length ();
+	    }
+	  (*fields)++;
+	  break;
+	}
+
+      /* only has special handling for structures with 1 or 2 fields. */
+      if (*fields > 2)
+	return;
+    }
+}
+
 /* Implement the push_dummy_call gdbarch method.  */
 
 static CORE_ADDR
@@ -569,6 +643,10 @@ loongarch_push_dummy_call (struct gdbarch *gdbarch,
   unsigned int fixed_point_members;
   unsigned int floating_point_members;
   bool first_member_is_fixed_point;
+  bool has_long_double;
+  unsigned int member_offsets[2];
+  unsigned int member_lens[2];
+  unsigned int fields;
   gdb_byte buf[1024] = { 0 };
   gdb_byte *addr = buf;
 
@@ -698,12 +776,55 @@ loongarch_push_dummy_call (struct gdbarch *gdbarch,
 	    fixed_point_members = 0;
 	    floating_point_members = 0;
 	    first_member_is_fixed_point = false;
+	    has_long_double = false;
+	    member_offsets[0] = member_offsets[1] = 0;
+	    member_lens[0] = member_offsets[1] = 0;
+	    fields = 0;
 	    compute_struct_member (type,
 				   &fixed_point_members,
 				   &floating_point_members,
-				   &first_member_is_fixed_point);
-
-	    if (len > 0 && len <= regsize)
+				   &first_member_is_fixed_point,
+				   &has_long_double);
+	    struct_member_info (type, member_offsets, member_lens, 0, &fields);
+	    /* If the structure consists of one floating-point member within
+	       FRLEN bits wide, it is passed in an FAR if available. If the
+	       structure consists of two floating-point members both within
+	       FRLEN bits wide, it is passed in two FARs if available. If the
+	       structure consists of one integer member within GRLEN bits wide
+	       and one floating-point member within FRLEN bits wide, it is
+	       passed in a GAR and an FAR if available. */
+	    if (has_long_double == false
+		&& ((fixed_point_members == 0 && floating_point_members == 1
+		     && far >= 1)
+		    || (fixed_point_members == 0 && floating_point_members == 2
+			&& far >= 2)
+		    || (fixed_point_members == 1 && floating_point_members == 1
+			&& far >= 1 && gar >= 1)))
+	      {
+		if (fixed_point_members == 0 && floating_point_members == 1)
+		  {
+		    pass_in_far (regcache, far--, val + member_offsets[0]);
+		  }
+		else if (fixed_point_members == 0 && floating_point_members == 2)
+		  {
+		    pass_in_far (regcache, far--, val + member_offsets[0]);
+		    pass_in_far (regcache, far--, val + member_offsets[1]);
+		  }
+		else if (fixed_point_members == 1 && floating_point_members == 1)
+		  {
+		    if (first_member_is_fixed_point == false)
+		      {
+			pass_in_far (regcache, far--, val + member_offsets[0]);
+			pass_in_gar (regcache, gar--, val + member_offsets[1]);
+		      }
+		    else
+		      {
+			pass_in_gar (regcache, gar--, val + member_offsets[0]);
+			pass_in_far (regcache, far--, val + member_offsets[1]);
+		      }
+		  }
+	      }
+	    else if (len > 0 && len <= regsize)
 	      {
 		/* The structure has only fixed-point members.  */
 		if (fixed_point_members > 0 && floating_point_members == 0)
@@ -1160,14 +1281,15 @@ loongarch_return_value (struct gdbarch *gdbarch, struct value *function,
   unsigned int fixed_point_members;
   unsigned int floating_point_members;
   bool first_member_is_fixed_point;
+  bool has_long_double;
+  unsigned int member_offsets[2];
+  unsigned int member_lens[2];
+  unsigned int fields;
   int a0 = LOONGARCH_A0_REGNUM;
   int a1 = LOONGARCH_A0_REGNUM + 1;
   int f0 = LOONGARCH_FIRST_FP_REGNUM;
   int f1 = LOONGARCH_FIRST_FP_REGNUM + 1;
 
-  if (len > 2 * regsize)
-    return RETURN_VALUE_STRUCT_CONVENTION;
-
   switch (code)
     {
     case TYPE_CODE_INT:
@@ -1220,12 +1342,56 @@ loongarch_return_value (struct gdbarch *gdbarch, struct value *function,
 	fixed_point_members = 0;
 	floating_point_members = 0;
 	first_member_is_fixed_point = false;
+	has_long_double = false;
+	member_offsets[0] = member_offsets[1] = 0;
+	member_lens[0] = member_offsets[1] = 0;
+	fields = 0;
 	compute_struct_member (type,
 			       &fixed_point_members,
 			       &floating_point_members,
-			       &first_member_is_fixed_point);
-
-	if (len > 0 && len <= regsize)
+			       &first_member_is_fixed_point,
+			       &has_long_double);
+	struct_member_info (type, member_offsets, member_lens, 0, &fields);
+	/* struct consists of one floating-point member;
+	   struct consists of two floating-point members;
+	   struct consists of one floating-point member
+	   and one integer member. */
+	if (has_long_double == false
+	    && ((fixed_point_members == 0 && floating_point_members == 1)
+		|| (fixed_point_members == 0 && floating_point_members == 2)
+		|| (fixed_point_members == 1 && floating_point_members == 1)))
+	  {
+	    if (fixed_point_members == 0 && floating_point_members == 1)
+	      {
+		loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
+				    writebuf, member_offsets[0]);
+	      }
+	    else if (fixed_point_members == 0 && floating_point_members == 2)
+	      {
+		loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
+				    writebuf, member_offsets[0]);
+		loongarch_xfer_reg (regcache, f1, member_lens[1], readbuf,
+				    writebuf, member_offsets[1]);
+	      }
+	    else if (fixed_point_members == 1 && floating_point_members == 1)
+	      {
+		if (first_member_is_fixed_point == false)
+		  {
+		    loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
+					writebuf, member_offsets[0]);
+		    loongarch_xfer_reg (regcache, a0, member_lens[1], readbuf,
+					writebuf, member_offsets[1]);
+		  }
+		else
+		  {
+		    loongarch_xfer_reg (regcache, a0, member_lens[0], readbuf,
+					writebuf, member_offsets[0]);
+		    loongarch_xfer_reg (regcache, f0, member_lens[1], readbuf,
+					writebuf, member_offsets[1]);
+		  }
+	      }
+	  }
+	else if (len > 0 && len <= regsize)
 	  {
 	    /* The structure has only fixed-point members.  */
 	    if (fixed_point_members > 0 && floating_point_members == 0)
@@ -1338,6 +1504,8 @@ loongarch_return_value (struct gdbarch *gdbarch, struct value *function,
 		  }
 	      }
 	  }
+	else if (len > 2 * regsize)
+	  return RETURN_VALUE_STRUCT_CONVENTION;
       }
       break;
     case TYPE_CODE_UNION:
@@ -1352,13 +1520,18 @@ loongarch_return_value (struct gdbarch *gdbarch, struct value *function,
 	  loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
 	  loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
 	}
+      else if (len > 2 * regsize)
+	return RETURN_VALUE_STRUCT_CONVENTION;
       break;
     case TYPE_CODE_COMPLEX:
-      {
-	/* The return value is passed in f0 and f1.  */
-	loongarch_xfer_reg (regcache, f0, len / 2, readbuf, writebuf, 0);
-	loongarch_xfer_reg (regcache, f1, len / 2, readbuf, writebuf, len / 2);
-      }
+      if (len > 0 && len <= 2 * regsize)
+	{
+	  /* The return value is passed in f0 and f1.  */
+	  loongarch_xfer_reg (regcache, f0, len / 2, readbuf, writebuf, 0);
+	  loongarch_xfer_reg (regcache, f1, len / 2, readbuf, writebuf, len / 2);
+	}
+      else if (len > 2 * regsize)
+	return RETURN_VALUE_STRUCT_CONVENTION;
       break;
     default:
       break;
-- 
2.38.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] gdb: LoongArch: Handle special struct in dummy call
  2023-10-08  7:32 [PATCH v2] gdb: LoongArch: Handle special struct in dummy call Hui Li
@ 2023-10-11  1:13 ` Tiezhu Yang
  0 siblings, 0 replies; 2+ messages in thread
From: Tiezhu Yang @ 2023-10-11  1:13 UTC (permalink / raw)
  To: Hui Li, gdb-patches



On 10/08/2023 03:32 PM, Hui Li wrote:
> When execute the following command on LoongArch:
>
>   make check-gdb TESTS="gdb.base/infcall-nested-structs-c++.exp"
>
> there exist some failed testcases:
>
>   === gdb Summary ===
>
>   # of expected passes		5533
>   # of unexpected failures	367
>
> The root cause is related with a struct containing floating-point
> members as function argument or return value for a dummy call.

...

>
> With this patch, there are no failed testcases:
>
>   make check-gdb TESTS="gdb.base/infcall-nested-structs-c++.exp"
>
>    === gdb Summary ===
>
>    # of expected passes		5900
>
> Signed-off-by: Hui Li <lihui@loongson.cn>

Thanks for the patch, looks good to me.

Tested on LoongArch, pushed.

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-11  1:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-08  7:32 [PATCH v2] gdb: LoongArch: Handle special struct in dummy call Hui Li
2023-10-11  1:13 ` Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).