Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: type alias error when compiled with assembly file including go_asm.h #22877

Closed
zhaozhiqianghw opened this issue Nov 25, 2017 · 4 comments

Comments

Projects
None yet
5 participants
@zhaozhiqianghw
Copy link

commented Nov 25, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

Go 1.9.2

Does this issue reproduce with the latest release?

I don't know

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

I have two files, main.go and asm_amd64.s

// main.go
package main

type S struct {
     i int
}

type SS = S

func Sub()

func main() {
     Sub()
}

and

#include "go_asm.h"
#include "textflag.h"

TEXT  ·Sub(SB), NOSPLIT, $0-16
       RET

What did you expect to see?

I use go build -o main, expect a success compiling.

What did you see instead?

# _/home/zhaozq/work/test/go/typealias
go_asm.h:6: redefinition of macro: S__size
make: *** [all] Error 2

The log tells me that the go_asm.h is wrong, so I use go tool compile -o main.o -asmhdr go_asm.h main.go, get go_asm.h as follows:

// generated by compile -asmhdr from package main

#define S__size 8
#define S_i 0
#define S__size 8
#define SS_i 0

Actually, this is a wrong file. So when I remove the statement #include go_asm.h in asm_amd64.s, the compiling is successful. Because this error file doesn't been used.

And I find this file is generated in cmd/compile/internal/gc/export.go:370

func dumpasmhdr() {
      b, err := bio.Create(asmhdr)
      if err != nil {
            Fatalf("%v", err)
      }
      fmt.Fprintf(b, "// generated by compile -asmhdr from package %s\n\n", localpkg.Name)
      for _, n := range asmlist {
            if n.Sym.IsBlank() {
                  continue
            }
            switch n.Op {
            case OLITERAL:
                  fmt.Fprintf(b, "#define const_%s %#v\n", n.Sym.Name, n.Val())

            case OTYPE:
                  t := n.Type
                  if !t.IsStruct() || t.StructType().Map != nil || t.IsFuncArgStruct() {
                        break
                  }
                  fmt.Fprintf(b, "#define %s__size %d\n", t.Sym.Name, int(t.Width))
                  for _, t := range t.Fields().Slice() {
                        if !t.Sym.IsBlank() {
                              fmt.Fprintf(b, "#define %s_%s %d\n", n.Sym.Name, t.Sym.Name, int(t.Offset))
                        }
                  }
            }
      }

      b.Close()
}

The code at cmd/compile/internal/gc/export.go:389 is the error.

 fmt.Fprintf(b, "#define %s__size %d\n", t.Sym.Name, int(t.Width))

Because the Sym t has been change at cmd/compile/internal/gc/typecheck.go:3821

n.Type = p.Ntype.Type

So the code at cmd/compile/internal/gc/export.go:389 should be changed to

 fmt.Fprintf(b, "#define %s__size %d\n", n.Sym.Name, int(t.Width))

When I changed this, the go_asm.h becomes

// generated by compile -asmhdr from package main

#define S__size 8
#define S_i 0
#define SS__size 8
#define SS_i 0

Then the world becomes good.

@bradfitz bradfitz changed the title compile: type alias error when compiled with assembly file including go_asm.h cmd/compile: type alias error when compiled with assembly file including go_asm.h Nov 25, 2017

@bradfitz bradfitz added the NeedsFix label Nov 25, 2017

@bradfitz bradfitz added this to the Go1.10 milestone Nov 25, 2017

@bradfitz

This comment has been minimized.

Copy link
Member

commented Nov 25, 2017

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 26, 2017

@mdempsky mdempsky self-assigned this Nov 29, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Nov 29, 2017

Change https://golang.org/cl/80759 mentions this issue: cmd/compile: make -asmhdr work with type aliases

@gopherbot gopherbot closed this in 2f588ff Nov 29, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Jun 13, 2018

Change https://golang.org/cl/118475 mentions this issue: [release-branch.go1.9] cmd/compile: make -asmhdr work with type aliases

gopherbot pushed a commit that referenced this issue Jun 13, 2018

[release-branch.go1.9] cmd/compile: make -asmhdr work with type aliases
For "type T = U" we were accidentally emitting a #define for "U__size"
instead of "T__size".

Updates #22877.
Fixes #25561.

Change-Id: I5ed6757d697753ed6d944077c16150759f6e1285
Reviewed-on: https://go-review.googlesource.com/80759
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
(cherry picked from commit 2f588ff)
Reviewed-on: https://go-review.googlesource.com/118475
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.