Skip to content

Commit 2fc29a8

Browse files
committed
cmd/gc: resolve static addresses of the form &x.f at link time
When we do y = &x for global variables x and y, y gets initialized at link time. Do the same for y = &x.f if x is a struct and y=&x[5] if x is an array. fixes #9217 fixes #9355 Change-Id: Iea3c0ce2ce1b309e2b760e345608fd95460b5713 Reviewed-on: https://go-review.googlesource.com/1691 Reviewed-by: Minux Ma <[email protected]>
1 parent 340ef00 commit 2fc29a8

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

src/cmd/gc/sinit.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ staticcopy(Node *l, Node *r, NodeList **out)
374374
static int
375375
staticassign(Node *l, Node *r, NodeList **out)
376376
{
377-
Node *a, n1;
377+
Node *a, n1, nam;
378378
Type *ta;
379379
InitPlan *p;
380380
InitEntry *e;
@@ -398,13 +398,10 @@ staticassign(Node *l, Node *r, NodeList **out)
398398
return 1;
399399

400400
case OADDR:
401-
switch(r->left->op) {
402-
default:
403-
//dump("not static addr", r);
404-
break;
405-
406-
case ONAME:
407-
gdata(l, r, l->type->width);
401+
if(stataddr(&nam, r->left)) {
402+
n1 = *r;
403+
n1.left = &nam;
404+
gdata(l, &n1, l->type->width);
408405
return 1;
409406
}
410407

test/fixedbugs/issue9355.dir/a.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
var x struct {
4+
a, b, c int64
5+
d struct{ p, q, r int32 }
6+
e [8]byte
7+
f [4]struct{ p, q, r int32 }
8+
}
9+
10+
var y = &x.b
11+
var z = &x.d.q
12+
13+
var b [10]byte
14+
var c = &b[5]
15+
16+
var w = &x.f[3].r

test/fixedbugs/issue9355.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// run
2+
3+
// Copyright 2014 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"go/build"
12+
"os"
13+
"os/exec"
14+
"path/filepath"
15+
"regexp"
16+
"runtime"
17+
)
18+
19+
func main() {
20+
if runtime.Compiler != "gc" {
21+
return
22+
}
23+
a, err := build.ArchChar(runtime.GOARCH)
24+
if err != nil {
25+
fmt.Println("BUG:", err)
26+
os.Exit(1)
27+
}
28+
out := run("go", "tool", a+"g", "-S", filepath.Join("fixedbugs", "issue9355.dir", "a.go"))
29+
patterns := []string{
30+
`rel 0\+\d t=1 \"\"\.x\+8\n`, // y = &x.b
31+
`rel 0\+\d t=1 \"\"\.x\+28\n`, // z = &x.d.q
32+
`rel 0\+\d t=1 \"\"\.b\+5\n`, // c = &b[5]
33+
`rel 0\+\d t=1 \"\"\.x\+88\n`, // w = &x.f[3].r
34+
}
35+
for _, p := range patterns {
36+
if ok, err := regexp.Match(p, out); !ok || err != nil {
37+
println(string(out))
38+
panic("can't find pattern " + p)
39+
}
40+
}
41+
}
42+
43+
func run(cmd string, args ...string) []byte {
44+
out, err := exec.Command(cmd, args...).CombinedOutput()
45+
if err != nil {
46+
fmt.Println(string(out))
47+
fmt.Println(err)
48+
os.Exit(1)
49+
}
50+
return out
51+
}

0 commit comments

Comments
 (0)