cmd/compile: introduce gc.Node.copy method

When making a shallow copy of a node, various methods were used,
including calling nod(OXXX, nil, nil) and then overwriting it, or
"n1 := *n" and then using &n1.

Add a copy method instead, simplifying all of those and making them
consistent.

Passes toolstash -cmp on std cmd.

Change-Id: I3f3fc88bad708edc712bf6d87214cda4ddc43b01
Reviewed-on: https://go-review.googlesource.com/72710
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Daniel Martí 2017-10-23 19:57:07 +01:00
parent 321bd8c93b
commit 19ee2ef950
9 changed files with 57 additions and 66 deletions

View file

@ -328,35 +328,32 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
// copy slice
a := inittemps[r]
n := *l
n := l.copy()
n.Xoffset = l.Xoffset + int64(array_array)
gdata(&n, nod(OADDR, a, nil), Widthptr)
gdata(n, nod(OADDR, a, nil), Widthptr)
n.Xoffset = l.Xoffset + int64(array_nel)
gdata(&n, r.Right, Widthptr)
gdata(n, r.Right, Widthptr)
n.Xoffset = l.Xoffset + int64(array_cap)
gdata(&n, r.Right, Widthptr)
gdata(n, r.Right, Widthptr)
return true
case OARRAYLIT, OSTRUCTLIT:
p := initplans[r]
n := *l
n := l.copy()
for i := range p.E {
e := &p.E[i]
n.Xoffset = l.Xoffset + e.Xoffset
n.Type = e.Expr.Type
if e.Expr.Op == OLITERAL {
gdata(&n, e.Expr, int(n.Type.Width))
gdata(n, e.Expr, int(n.Type.Width))
} else {
ll := nod(OXXX, nil, nil)
*ll = n
ll := n.copy()
ll.Orig = ll // completely separate copy
if !staticassign(ll, e.Expr, out) {
// Requires computation, but we're
// copying someone else's computation.
rr := nod(OXXX, nil, nil)
*rr = *orig
rr := orig.copy()
rr.Orig = rr // completely separate copy
rr.Type = ll.Type
rr.Xoffset += e.Xoffset
@ -429,13 +426,13 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
ta := types.NewArray(r.Type.Elem(), bound)
a := staticname(ta)
inittemps[r] = a
n := *l
n := l.copy()
n.Xoffset = l.Xoffset + int64(array_array)
gdata(&n, nod(OADDR, a, nil), Widthptr)
gdata(n, nod(OADDR, a, nil), Widthptr)
n.Xoffset = l.Xoffset + int64(array_nel)
gdata(&n, r.Right, Widthptr)
gdata(n, r.Right, Widthptr)
n.Xoffset = l.Xoffset + int64(array_cap)
gdata(&n, r.Right, Widthptr)
gdata(n, r.Right, Widthptr)
// Fall through to init underlying array.
l = a
@ -445,17 +442,16 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
initplan(r)
p := initplans[r]
n := *l
n := l.copy()
for i := range p.E {
e := &p.E[i]
n.Xoffset = l.Xoffset + e.Xoffset
n.Type = e.Expr.Type
if e.Expr.Op == OLITERAL {
gdata(&n, e.Expr, int(n.Type.Width))
gdata(n, e.Expr, int(n.Type.Width))
} else {
setlineno(e.Expr)
a := nod(OXXX, nil, nil)
*a = n
a := n.copy()
a.Orig = a // completely separate copy
if !staticassign(a, e.Expr, out) {
*out = append(*out, nod(OAS, a, e.Expr))
@ -522,11 +518,10 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
// Copy val directly into n.
n.Type = val.Type
setlineno(val)
a := nod(OXXX, nil, nil)
*a = n
a.Orig = a
if !staticassign(a, val, out) {
*out = append(*out, nod(OAS, a, val))
a := n
a.Orig = &a
if !staticassign(&a, val, out) {
*out = append(*out, nod(OAS, &a, val))
}
} else {
// Construct temp to hold val, write pointer to temp into n.