From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 9E7E83858D32 for ; Sun, 8 Oct 2023 07:32:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9E7E83858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxNugdWyJl3_svAA--.38369S3; Sun, 08 Oct 2023 15:32:46 +0800 (CST) Received: from localhost.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxeuQcWyJlprUaAA--.58716S2; Sun, 08 Oct 2023 15:32:44 +0800 (CST) From: Hui Li To: gdb-patches@sourceware.org Subject: [PATCH v2] gdb: LoongArch: Handle special struct in dummy call Date: Sun, 8 Oct 2023 15:32:44 +0800 Message-Id: <20231008073244.17594-1-lihui@loongson.cn> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8CxeuQcWyJlprUaAA--.58716S2 X-CM-SenderInfo: 5olk3xo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj93XoW3tw15Kw48ZryrZr18tFWfCrX_yoWkAr17pF 9xCF1xKF48Jr97CwnrC3Z8Zw1Sk34ruF1DCasxAa4I9r1DJ3s8ua1rC34YgFZ0kr1jq34a yFs8KFy3CFy7XrXCm3ZEXasCq-sJn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkFb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1j6r15MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07UE-erUUUUU= X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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 --- 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