]> Cypherpunks repositories - gostls13.git/commitdiff
compile/internal/walk: add walkGrowslice
authorEgon Elbre <egonelbre@gmail.com>
Wed, 17 May 2023 15:10:58 +0000 (18:10 +0300)
committerKeith Randall <khr@golang.org>
Tue, 5 Sep 2023 15:06:41 +0000 (15:06 +0000)
Move growslice generation to a separate func so that specialization
logic can be shared.

Updates #49480

Change-Id: I9ea5bb898753622d2d767546a46b4db6410dc725
Reviewed-on: https://go-review.googlesource.com/c/go/+/495877
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/compile/internal/walk/assign.go
src/cmd/compile/internal/walk/builtin.go

index bf7592967ec90cf602c4688e7d2a1e5fd2fea46f..b8dcba5968a6f194564225885fbe4d2b891cb071 100644 (file)
@@ -513,11 +513,8 @@ func appendSlice(n *ir.CallExpr, init *ir.Nodes) ir.Node {
        slice.SetBounded(true)
        nif.Body = []ir.Node{ir.NewAssignStmt(base.Pos, s, slice)}
 
-       // func growslice(oldPtr unsafe.Pointer, newLen, oldCap, num int, et *_type) []T
-       fn := typecheck.LookupRuntime("growslice", elemtype, elemtype)
-
        // else { s = growslice(oldPtr, newLen, oldCap, num, T) }
-       call := mkcall1(fn, s.Type(), nif.PtrInit(), oldPtr, newLen, oldCap, num, reflectdata.TypePtrAt(base.Pos, elemtype))
+       call := walkGrowslice(s, nif.PtrInit(), oldPtr, newLen, oldCap, num)
        nif.Else = []ir.Node{ir.NewAssignStmt(base.Pos, s, call)}
 
        nodes.Append(nif)
@@ -691,17 +688,13 @@ func extendSlice(n *ir.CallExpr, init *ir.Nodes) ir.Node {
        nt.SetBounded(true)
        nif.Body = []ir.Node{ir.NewAssignStmt(base.Pos, s, nt)}
 
-       // instantiate growslice(oldPtr *any, newLen, oldCap, num int, typ *type) []any
-       fn := typecheck.LookupRuntime("growslice", elemtype, elemtype)
-
        // else { s = growslice(s.ptr, n, s.cap, l2, T) }
        nif.Else = []ir.Node{
-               ir.NewAssignStmt(base.Pos, s, mkcall1(fn, s.Type(), nif.PtrInit(),
+               ir.NewAssignStmt(base.Pos, s, walkGrowslice(s, nif.PtrInit(),
                        ir.NewUnaryExpr(base.Pos, ir.OSPTR, s),
                        nn,
                        ir.NewUnaryExpr(base.Pos, ir.OCAP, s),
-                       l2,
-                       reflectdata.TypePtrAt(base.Pos, elemtype))),
+                       l2)),
        }
 
        nodes = append(nodes, nif)
index c3a641eac9d7e81fab16ef5d6e0bec88a54b93ec..56dad14f21acf68b81ba465b2aa781a4a8c1acda 100644 (file)
@@ -101,17 +101,13 @@ func walkAppend(n *ir.CallExpr, init *ir.Nodes, dst ir.Node) ir.Node {
                ir.NewAssignStmt(base.Pos, s, slice),
        }
 
-       //   growslice(ptr *T, newLen, oldCap, num int, <type>) (ret []T)
-       fn := typecheck.LookupRuntime("growslice", s.Type().Elem(), s.Type().Elem())
-
        // else { s = growslice(s.ptr, n, s.cap, a, T) }
        nif.Else = []ir.Node{
-               ir.NewAssignStmt(base.Pos, s, mkcall1(fn, s.Type(), nif.PtrInit(),
+               ir.NewAssignStmt(base.Pos, s, walkGrowslice(s, nif.PtrInit(),
                        ir.NewUnaryExpr(base.Pos, ir.OSPTR, s),
                        newLen,
                        ir.NewUnaryExpr(base.Pos, ir.OCAP, s),
-                       num,
-                       reflectdata.TypePtrAt(base.Pos, s.Type().Elem()))),
+                       num)),
        }
 
        l = append(l, nif)
@@ -130,6 +126,14 @@ func walkAppend(n *ir.CallExpr, init *ir.Nodes, dst ir.Node) ir.Node {
        return s
 }
 
+// growslice(ptr *T, newLen, oldCap, num int, <type>) (ret []T)
+func walkGrowslice(slice *ir.Name, init *ir.Nodes, oldPtr, newLen, oldCap, num ir.Node) *ir.CallExpr {
+       elemtype := slice.Type().Elem()
+       fn := typecheck.LookupRuntime("growslice", elemtype, elemtype)
+       elemtypeptr := reflectdata.TypePtrAt(base.Pos, elemtype)
+       return mkcall1(fn, slice.Type(), init, oldPtr, newLen, oldCap, num, elemtypeptr)
+}
+
 // walkClear walks an OCLEAR node.
 func walkClear(n *ir.UnaryExpr) ir.Node {
        typ := n.X.Type()