Skip to content

Commit

Permalink
compile/internal/walk: add walkGrowslice
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
  • Loading branch information
egonelbre authored and randall77 committed Sep 5, 2023
1 parent 0e1a9e1 commit a2647f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
13 changes: 3 additions & 10 deletions src/cmd/compile/internal/walk/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions src/cmd/compile/internal/walk/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit a2647f0

Please sign in to comment.