Skip to content

Commit 85387aa

Browse files
committed
cmd/compile: remove dynamic entry handling from sinit/maplit
The order pass now handles all the dynamic entries. Update #26552 Followup to CL 174417 Change-Id: Ie924cadb0e0ba36c423868f654f13040100b44c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/174498 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 70b890c commit 85387aa

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

src/cmd/compile/internal/gc/sinit.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ func getdyn(n *Node, top bool) initGenType {
650650
// isStaticCompositeLiteral reports whether n is a compile-time constant.
651651
func isStaticCompositeLiteral(n *Node) bool {
652652
switch n.Op {
653+
case ONAME:
654+
return n.Class() == PEXTERN && n.Name != nil && n.Name.Readonly()
653655
case OSLICELIT:
654656
return false
655657
case OARRAYLIT:
@@ -954,26 +956,22 @@ func maplit(n *Node, m *Node, init *Nodes) {
954956
a.List.Set2(typenod(n.Type), nodintconst(int64(n.List.Len())))
955957
litas(m, a, init)
956958

957-
// Split the initializers into static and dynamic.
958-
var stat, dyn []*Node
959-
for _, r := range n.List.Slice() {
960-
if r.Op != OKEY {
961-
Fatalf("maplit: rhs not OKEY: %v", r)
962-
}
963-
if isStaticCompositeLiteral(r.Left) && isStaticCompositeLiteral(r.Right) {
964-
stat = append(stat, r)
965-
} else {
966-
dyn = append(dyn, r)
959+
entries := n.List.Slice()
960+
961+
// The order pass already removed any dynamic (runtime-computed) entries.
962+
// All remaining entries are static. Double-check that.
963+
for _, r := range entries {
964+
if !isStaticCompositeLiteral(r.Left) || !isStaticCompositeLiteral(r.Right) {
965+
Fatalf("maplit: entry is not a literal: %v", r)
967966
}
968967
}
969968

970-
// Add static entries.
971-
if len(stat) > 25 {
972-
// For a large number of static entries, put them in an array and loop.
969+
if len(entries) > 25 {
970+
// For a large number of entries, put them in an array and loop.
973971

974972
// build types [count]Tindex and [count]Tvalue
975-
tk := types.NewArray(n.Type.Key(), int64(len(stat)))
976-
te := types.NewArray(n.Type.Elem(), int64(len(stat)))
973+
tk := types.NewArray(n.Type.Key(), int64(len(entries)))
974+
te := types.NewArray(n.Type.Elem(), int64(len(entries)))
977975

978976
// TODO(josharian): suppress alg generation for these types?
979977
dowidth(tk)
@@ -987,7 +985,7 @@ func maplit(n *Node, m *Node, init *Nodes) {
987985

988986
datak := nod(OARRAYLIT, nil, nil)
989987
datae := nod(OARRAYLIT, nil, nil)
990-
for _, r := range stat {
988+
for _, r := range entries {
991989
datak.List.Append(r.Left)
992990
datae.List.Append(r.Right)
993991
}
@@ -1018,29 +1016,17 @@ func maplit(n *Node, m *Node, init *Nodes) {
10181016
loop = typecheck(loop, ctxStmt)
10191017
loop = walkstmt(loop)
10201018
init.Append(loop)
1021-
} else {
1022-
// For a small number of static entries, just add them directly.
1023-
addMapEntries(m, stat, init)
1024-
}
1025-
1026-
// Add dynamic entries.
1027-
addMapEntries(m, dyn, init)
1028-
}
1029-
1030-
func addMapEntries(m *Node, dyn []*Node, init *Nodes) {
1031-
if len(dyn) == 0 {
10321019
return
10331020
}
1034-
1035-
nerr := nerrors
1021+
// For a small number of entries, just add them directly.
10361022

10371023
// Build list of var[c] = expr.
10381024
// Use temporaries so that mapassign1 can have addressable key, elem.
10391025
// TODO(josharian): avoid map key temporaries for mapfast_* assignments with literal keys.
10401026
tmpkey := temp(m.Type.Key())
10411027
tmpelem := temp(m.Type.Elem())
10421028

1043-
for _, r := range dyn {
1029+
for _, r := range entries {
10441030
index, elem := r.Left, r.Right
10451031

10461032
setlineno(index)
@@ -1060,13 +1046,9 @@ func addMapEntries(m *Node, dyn []*Node, init *Nodes) {
10601046
a = typecheck(a, ctxStmt)
10611047
a = walkstmt(a)
10621048
init.Append(a)
1063-
1064-
if nerr != nerrors {
1065-
break
1066-
}
10671049
}
10681050

1069-
a := nod(OVARKILL, tmpkey, nil)
1051+
a = nod(OVARKILL, tmpkey, nil)
10701052
a = typecheck(a, ctxStmt)
10711053
init.Append(a)
10721054
a = nod(OVARKILL, tmpelem, nil)

0 commit comments

Comments
 (0)