]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: reorg generated array hash loop
authorKeith Randall <khr@golang.org>
Sun, 6 Dec 2020 01:25:28 +0000 (17:25 -0800)
committerKeith Randall <khr@golang.org>
Mon, 14 Dec 2020 21:39:00 +0000 (21:39 +0000)
The ORANGE structure that is being replaced by this CL was causing
trouble with another CL (CL 275695).

The problem occurs if you typecheck i in the middle of generating the
body of the ORANGE loop. If you typecheck i, it ends up typechecking
its definition, which secretly typechecks the containing ORANGE.  If
you then add other items to the ORANGE body, those items will never
get typechecked, as the ORANGE is already marked as typechecked.

Instead, just steal the loop we use for the equality code. Might as
well use the same pattern in both places.

Change-Id: Idb1ac77881d2cc9da08c7437a652b50d3ee45e2e
Reviewed-on: https://go-review.googlesource.com/c/go/+/275713
Trust: Keith Randall <khr@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
src/cmd/compile/internal/gc/alg.go

index c786a27415c3876e43af0f1ee2f2d210d96d97df..ea57e7398dcdc3fa92a601bada04ed7d3e512e8e 100644 (file)
@@ -310,13 +310,13 @@ func genhash(t *types.Type) *obj.LSym {
                // pure memory.
                hashel := hashfor(t.Elem())
 
-               n := ir.Nod(ir.ORANGE, nil, ir.Nod(ir.ODEREF, np, nil))
-               ni := ir.Node(NewName(lookup("i")))
-               ni.SetType(types.Types[types.TINT])
-               n.PtrList().Set1(ni)
-               n.SetColas(true)
-               colasdefn(n.List().Slice(), n)
-               ni = n.List().First()
+               // for i := 0; i < nelem; i++
+               ni := temp(types.Types[types.TINT])
+               init := ir.Nod(ir.OAS, ni, nodintconst(0))
+               cond := ir.Nod(ir.OLT, ni, nodintconst(t.NumElem()))
+               post := ir.Nod(ir.OAS, ni, ir.Nod(ir.OADD, ni, nodintconst(1)))
+               loop := ir.Nod(ir.OFOR, cond, post)
+               loop.PtrInit().Append(init)
 
                // h = hashel(&p[i], h)
                call := ir.Nod(ir.OCALL, hashel, nil)
@@ -326,9 +326,9 @@ func genhash(t *types.Type) *obj.LSym {
                na := ir.Nod(ir.OADDR, nx, nil)
                call.PtrList().Append(na)
                call.PtrList().Append(nh)
-               n.PtrBody().Append(ir.Nod(ir.OAS, nh, call))
+               loop.PtrBody().Append(ir.Nod(ir.OAS, nh, call))
 
-               fn.PtrBody().Append(n)
+               fn.PtrBody().Append(loop)
 
        case types.TSTRUCT:
                // Walk the struct using memhash for runs of AMEM