public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ian Lance Taylor <iant@golang.org>
To: gcc-patches <gcc-patches@gcc.gnu.org>,
	 gofrontend-dev <gofrontend-dev@googlegroups.com>
Subject: libgo patch committed: Synchronize empty struct field handling
Date: Tue, 27 Sep 2022 09:28:59 -0700	[thread overview]
Message-ID: <CAOyqgcV8j=NpiABvshLg0FOZm+pk44B8FH1+ejFgpxX+6=ZbUA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 515 bytes --]

This libgo patch by Funan Zeng synchronizes the handling of empty
struct fields between the Go frontend and the libgo FFI code.  In the
compiler the logic for allocating one byte for the last field of a
struct is:
1. the last field has zero size
2. the struct itself does not have zero size
3. the last field is not blank
This patch adds the last two conditions to runtime.structToFFI.  This
is for https://go.dev/issue/55146.  Bootstrapped and ran Go testsuite
on x86_64-pc-linux-gnu.  Committed to mainline.

Ian

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1958 bytes --]

085bacba3502ff77c70a7660c19a68f50e9b7877
diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE
index f7a7985287d..73aa712dbdf 100644
--- a/gcc/go/gofrontend/MERGE
+++ b/gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
-42efec8c126cf3787bc7c89d9c7f224eff7c5a21
+0140cca9bc0fad1108c7ed369376ac71cc4bfecf
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
diff --git a/libgo/go/runtime/ffi.go b/libgo/go/runtime/ffi.go
index cd8479ef551..86ce5b85d04 100644
--- a/libgo/go/runtime/ffi.go
+++ b/libgo/go/runtime/ffi.go
@@ -4,6 +4,7 @@
 
 // Only build this file if libffi is supported.
 
+//go:build libffi
 // +build libffi
 
 package runtime
@@ -221,9 +222,6 @@ func stringToFFI() *__ffi_type {
 // structToFFI returns an ffi_type for a Go struct type.
 func structToFFI(typ *structtype) *__ffi_type {
 	c := len(typ.fields)
-	if c == 0 {
-		return emptyStructToFFI()
-	}
 	if typ.typ.kind&kindDirectIface != 0 {
 		return ffi_type_pointer()
 	}
@@ -231,6 +229,7 @@ func structToFFI(typ *structtype) *__ffi_type {
 	fields := make([]*__ffi_type, 0, c+1)
 	checkPad := false
 	lastzero := false
+	sawnonzero := false
 	for i, v := range typ.fields {
 		// Skip zero-sized fields; they confuse libffi,
 		// and there is no value to pass in any case.
@@ -239,10 +238,13 @@ func structToFFI(typ *structtype) *__ffi_type {
 		// next field.
 		if v.typ.size == 0 {
 			checkPad = true
-			lastzero = true
+			if v.name == nil || *v.name != "_" {
+				lastzero = true
+			}
 			continue
 		}
 		lastzero = false
+		sawnonzero = true
 
 		if checkPad {
 			off := uintptr(0)
@@ -263,6 +265,10 @@ func structToFFI(typ *structtype) *__ffi_type {
 		fields = append(fields, typeToFFI(v.typ))
 	}
 
+	if !sawnonzero {
+		return emptyStructToFFI()
+	}
+
 	if lastzero {
 		// The compiler adds one byte padding to non-empty struct ending
 		// with a zero-sized field (types.cc:get_backend_struct_fields).

                 reply	other threads:[~2022-09-27 16:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOyqgcV8j=NpiABvshLg0FOZm+pk44B8FH1+ejFgpxX+6=ZbUA@mail.gmail.com' \
    --to=iant@golang.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gofrontend-dev@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).