mirror of
https://github.com/golang/go
synced 2024-11-24 12:40:12 -07:00
cmd/compile: refactor walkIndexMap
So all runtime map functions will use the same code path, make it easier for future CL to handle stack object for map key. Passes toolstash -cmp. For #43753 Change-Id: I374fa4e351c1eba079e2ccb637b1ef5adad1488f Reviewed-on: https://go-review.googlesource.com/c/go/+/321713 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
60be4ec096
commit
209521c3e3
@ -157,7 +157,7 @@ func walkAssignMapRead(init *ir.Nodes, n *ir.AssignListStmt) ir.Node {
|
|||||||
t := r.X.Type()
|
t := r.X.Type()
|
||||||
|
|
||||||
fast := mapfast(t)
|
fast := mapfast(t)
|
||||||
key := mapKeyArg(fast, r, r.Index)
|
key := mapKeyArg(fast, r, r.Index, false)
|
||||||
|
|
||||||
// from:
|
// from:
|
||||||
// a,b = m[i]
|
// a,b = m[i]
|
||||||
|
@ -214,7 +214,7 @@ func walkDelete(init *ir.Nodes, n *ir.CallExpr) ir.Node {
|
|||||||
|
|
||||||
t := map_.Type()
|
t := map_.Type()
|
||||||
fast := mapfast(t)
|
fast := mapfast(t)
|
||||||
key = mapKeyArg(fast, n, key)
|
key = mapKeyArg(fast, n, key, false)
|
||||||
return mkcall1(mapfndel(mapdelete[fast], t), nil, init, reflectdata.TypePtr(t), map_, key)
|
return mkcall1(mapfndel(mapdelete[fast], t), nil, init, reflectdata.TypePtr(t), map_, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -723,21 +723,23 @@ func walkIndex(n *ir.IndexExpr, init *ir.Nodes) ir.Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mapKeyArg returns an expression for key that is suitable to be passed
|
// mapKeyArg returns an expression for key that is suitable to be passed
|
||||||
// as the key argument for mapaccess and mapdelete functions.
|
// as the key argument for runtime map* functions.
|
||||||
// n is is the map indexing or delete Node (to provide Pos).
|
// n is is the map indexing or delete Node (to provide Pos).
|
||||||
// Note: this is not used for mapassign, which does distinguish pointer vs.
|
func mapKeyArg(fast int, n, key ir.Node, assigned bool) ir.Node {
|
||||||
// integer key.
|
if fast == mapslow {
|
||||||
func mapKeyArg(fast int, n, key ir.Node) ir.Node {
|
|
||||||
switch fast {
|
|
||||||
case mapslow:
|
|
||||||
// standard version takes key by reference.
|
// standard version takes key by reference.
|
||||||
// order.expr made sure key is addressable.
|
// orderState.expr made sure key is addressable.
|
||||||
return typecheck.NodAddr(key)
|
return typecheck.NodAddr(key)
|
||||||
case mapfast32ptr:
|
}
|
||||||
|
if assigned {
|
||||||
|
// mapassign does distinguish pointer vs. integer key.
|
||||||
|
return key
|
||||||
|
}
|
||||||
// mapaccess and mapdelete don't distinguish pointer vs. integer key.
|
// mapaccess and mapdelete don't distinguish pointer vs. integer key.
|
||||||
|
switch fast {
|
||||||
|
case mapfast32ptr:
|
||||||
return ir.NewConvExpr(n.Pos(), ir.OCONVNOP, types.Types[types.TUINT32], key)
|
return ir.NewConvExpr(n.Pos(), ir.OCONVNOP, types.Types[types.TUINT32], key)
|
||||||
case mapfast64ptr:
|
case mapfast64ptr:
|
||||||
// mapaccess and mapdelete don't distinguish pointer vs. integer key.
|
|
||||||
return ir.NewConvExpr(n.Pos(), ir.OCONVNOP, types.Types[types.TUINT64], key)
|
return ir.NewConvExpr(n.Pos(), ir.OCONVNOP, types.Types[types.TUINT64], key)
|
||||||
default:
|
default:
|
||||||
// fast version takes key by value.
|
// fast version takes key by value.
|
||||||
@ -746,34 +748,27 @@ func mapKeyArg(fast int, n, key ir.Node) ir.Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// walkIndexMap walks an OINDEXMAP node.
|
// walkIndexMap walks an OINDEXMAP node.
|
||||||
|
// It replaces m[k] with *map{access1,assign}(maptype, m, &k)
|
||||||
func walkIndexMap(n *ir.IndexExpr, init *ir.Nodes) ir.Node {
|
func walkIndexMap(n *ir.IndexExpr, init *ir.Nodes) ir.Node {
|
||||||
// Replace m[k] with *map{access1,assign}(maptype, m, &k)
|
|
||||||
n.X = walkExpr(n.X, init)
|
n.X = walkExpr(n.X, init)
|
||||||
n.Index = walkExpr(n.Index, init)
|
n.Index = walkExpr(n.Index, init)
|
||||||
map_ := n.X
|
map_ := n.X
|
||||||
key := n.Index
|
|
||||||
t := map_.Type()
|
t := map_.Type()
|
||||||
var call *ir.CallExpr
|
|
||||||
if n.Assigned {
|
|
||||||
// This m[k] expression is on the left-hand side of an assignment.
|
|
||||||
fast := mapfast(t)
|
fast := mapfast(t)
|
||||||
if fast == mapslow {
|
key := mapKeyArg(fast, n, n.Index, n.Assigned)
|
||||||
// standard version takes key by reference.
|
args := []ir.Node{reflectdata.TypePtr(t), map_, key}
|
||||||
// order.expr made sure key is addressable.
|
|
||||||
key = typecheck.NodAddr(key)
|
var mapFn ir.Node
|
||||||
}
|
switch {
|
||||||
call = mkcall1(mapfn(mapassign[fast], t, false), nil, init, reflectdata.TypePtr(t), map_, key)
|
case n.Assigned:
|
||||||
} else {
|
mapFn = mapfn(mapassign[fast], t, false)
|
||||||
// m[k] is not the target of an assignment.
|
case t.Elem().Size() > zeroValSize:
|
||||||
fast := mapfast(t)
|
args = append(args, reflectdata.ZeroAddr(t.Elem().Size()))
|
||||||
key = mapKeyArg(fast, n, key)
|
mapFn = mapfn("mapaccess1_fat", t, true)
|
||||||
if w := t.Elem().Size(); w <= zeroValSize {
|
default:
|
||||||
call = mkcall1(mapfn(mapaccess1[fast], t, false), types.NewPtr(t.Elem()), init, reflectdata.TypePtr(t), map_, key)
|
mapFn = mapfn(mapaccess1[fast], t, false)
|
||||||
} else {
|
|
||||||
z := reflectdata.ZeroAddr(w)
|
|
||||||
call = mkcall1(mapfn("mapaccess1_fat", t, true), types.NewPtr(t.Elem()), init, reflectdata.TypePtr(t), map_, key, z)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
call := mkcall1(mapFn, nil, init, args...)
|
||||||
call.SetType(types.NewPtr(t.Elem()))
|
call.SetType(types.NewPtr(t.Elem()))
|
||||||
call.MarkNonNil() // mapaccess1* and mapassign always return non-nil pointers.
|
call.MarkNonNil() // mapaccess1* and mapassign always return non-nil pointers.
|
||||||
star := ir.NewStarExpr(base.Pos, call)
|
star := ir.NewStarExpr(base.Pos, call)
|
||||||
|
Loading…
Reference in New Issue
Block a user