cmd/compile: unify reflect, string and slice copy runtime functions

Use a common runtime slicecopy function to copy strings or slices
into slices. This deduplicates similar code previously used in
reflect.slicecopy and runtime.stringslicecopy.

Change-Id: I09572ff0647a9e12bb5c6989689ce1c43f16b7f1
Reviewed-on: https://go-review.googlesource.com/c/go/+/254658
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Martin Möhrmann <moehrmann@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Martin Möhrmann 2020-09-14 16:30:43 +02:00
parent eaa97fbf20
commit 790fa1c546
6 changed files with 248 additions and 304 deletions

View file

@ -928,16 +928,20 @@ func (o Op) IsSlice3() bool {
return false
}
// slicePtrLen extracts the pointer and length from a slice.
// backingArrayPtrLen extracts the pointer and length from a slice or string.
// This constructs two nodes referring to n, so n must be a cheapexpr.
func (n *Node) slicePtrLen() (ptr, len *Node) {
func (n *Node) backingArrayPtrLen() (ptr, len *Node) {
var init Nodes
c := cheapexpr(n, &init)
if c != n || init.Len() != 0 {
Fatalf("slicePtrLen not cheap: %v", n)
Fatalf("backingArrayPtrLen not cheap: %v", n)
}
ptr = nod(OSPTR, n, nil)
ptr.Type = n.Type.Elem().PtrTo()
if n.Type.IsString() {
ptr.Type = types.Types[TUINT8].PtrTo()
} else {
ptr.Type = n.Type.Elem().PtrTo()
}
len = nod(OLEN, n, nil)
len.Type = types.Types[TINT]
return ptr, len