]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: resolve static addresses of the form &x.f at link time
authorKeith Randall <khr@golang.org>
Tue, 16 Dec 2014 22:15:49 +0000 (14:15 -0800)
committerKeith Randall <khr@golang.org>
Thu, 18 Dec 2014 21:53:03 +0000 (21:53 +0000)
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 <minux@golang.org>
src/cmd/gc/sinit.c
test/fixedbugs/issue9355.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue9355.go [new file with mode: 0644]

index 8ad7ae7abb3a6264e7bb66818305f0abc3036833..8c24c122d6882fc04e5153555fca440ec8d49948 100644 (file)
@@ -374,7 +374,7 @@ staticcopy(Node *l, Node *r, NodeList **out)
 static int
 staticassign(Node *l, Node *r, NodeList **out)
 {
-       Node *a, n1;
+       Node *a, n1, nam;
        Type *ta;
        InitPlan *p;
        InitEntry *e;
@@ -398,13 +398,10 @@ staticassign(Node *l, Node *r, NodeList **out)
                return 1;
 
        case OADDR:
-               switch(r->left->op) {
-               default:
-                       //dump("not static addr", r);
-                       break;
-
-               case ONAME:
-                       gdata(l, r, l->type->width);
+               if(stataddr(&nam, r->left)) {
+                       n1 = *r;
+                       n1.left = &nam;
+                       gdata(l, &n1, l->type->width);
                        return 1;
                }
        
diff --git a/test/fixedbugs/issue9355.dir/a.go b/test/fixedbugs/issue9355.dir/a.go
new file mode 100644 (file)
index 0000000..84500c8
--- /dev/null
@@ -0,0 +1,16 @@
+package main
+
+var x struct {
+       a, b, c int64
+       d       struct{ p, q, r int32 }
+       e       [8]byte
+       f       [4]struct{ p, q, r int32 }
+}
+
+var y = &x.b
+var z = &x.d.q
+
+var b [10]byte
+var c = &b[5]
+
+var w = &x.f[3].r
diff --git a/test/fixedbugs/issue9355.go b/test/fixedbugs/issue9355.go
new file mode 100644 (file)
index 0000000..7903ff2
--- /dev/null
@@ -0,0 +1,51 @@
+// run
+
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+       "fmt"
+       "go/build"
+       "os"
+       "os/exec"
+       "path/filepath"
+       "regexp"
+       "runtime"
+)
+
+func main() {
+       if runtime.Compiler != "gc" {
+               return
+       }
+       a, err := build.ArchChar(runtime.GOARCH)
+       if err != nil {
+               fmt.Println("BUG:", err)
+               os.Exit(1)
+       }
+       out := run("go", "tool", a+"g", "-S", filepath.Join("fixedbugs", "issue9355.dir", "a.go"))
+       patterns := []string{
+               `rel 0\+\d t=1 \"\"\.x\+8\n`,  // y = &x.b
+               `rel 0\+\d t=1 \"\"\.x\+28\n`, // z = &x.d.q
+               `rel 0\+\d t=1 \"\"\.b\+5\n`,  // c = &b[5]
+               `rel 0\+\d t=1 \"\"\.x\+88\n`, // w = &x.f[3].r
+       }
+       for _, p := range patterns {
+               if ok, err := regexp.Match(p, out); !ok || err != nil {
+                       println(string(out))
+                       panic("can't find pattern " + p)
+               }
+       }
+}
+
+func run(cmd string, args ...string) []byte {
+       out, err := exec.Command(cmd, args...).CombinedOutput()
+       if err != nil {
+               fmt.Println(string(out))
+               fmt.Println(err)
+               os.Exit(1)
+       }
+       return out
+}