Skip to content

Commit d23bf3d

Browse files
committed
cmd/compile: move sinit.go globals into InitSchedule
Eliminates global state from sinit.go. Passes toolstash-check. Updates #22326. Change-Id: Ie3cb14bff625baa20134d1488962ab02d24f0c15 Reviewed-on: https://go-review.googlesource.com/c/go/+/169899 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 57bd577 commit d23bf3d

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

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

+41-43
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,16 @@ type InitPlan struct {
2727
}
2828

2929
type InitSchedule struct {
30-
out []*Node
30+
out []*Node
31+
initlist []*Node
32+
initplans map[*Node]*InitPlan
33+
inittemps map[*Node]*Node
3134
}
3235

3336
func (s *InitSchedule) append(n *Node) {
3437
s.out = append(s.out, n)
3538
}
3639

37-
var (
38-
initlist []*Node
39-
initplans map[*Node]*InitPlan
40-
inittemps = make(map[*Node]*Node)
41-
)
42-
4340
// init1 walks the AST starting at n, and accumulates in out
4441
// the list of definitions needing init code in dependency order.
4542
func (s *InitSchedule) init1(n *Node) {
@@ -86,16 +83,16 @@ func (s *InitSchedule) init1(n *Node) {
8683
// a variable in the program, the tree walk will reach a cycle
8784
// involving that variable.
8885
if n.Class() != PFUNC {
89-
foundinitloop(n, n)
86+
s.foundinitloop(n, n)
9087
}
9188

92-
for i := len(initlist) - 1; i >= 0; i-- {
93-
x := initlist[i]
89+
for i := len(s.initlist) - 1; i >= 0; i-- {
90+
x := s.initlist[i]
9491
if x == n {
9592
break
9693
}
9794
if x.Class() != PFUNC {
98-
foundinitloop(n, x)
95+
s.foundinitloop(n, x)
9996
}
10097
}
10198

@@ -105,7 +102,7 @@ func (s *InitSchedule) init1(n *Node) {
105102

106103
// reached a new unvisited node.
107104
n.SetInitorder(InitPending)
108-
initlist = append(initlist, n)
105+
s.initlist = append(s.initlist, n)
109106

110107
// make sure that everything n depends on is initialized.
111108
// n->defn is an assignment to n
@@ -157,18 +154,18 @@ func (s *InitSchedule) init1(n *Node) {
157154
}
158155
}
159156

160-
last := len(initlist) - 1
161-
if initlist[last] != n {
162-
Fatalf("bad initlist %v", initlist)
157+
last := len(s.initlist) - 1
158+
if s.initlist[last] != n {
159+
Fatalf("bad initlist %v", s.initlist)
163160
}
164-
initlist[last] = nil // allow GC
165-
initlist = initlist[:last]
161+
s.initlist[last] = nil // allow GC
162+
s.initlist = s.initlist[:last]
166163

167164
n.SetInitorder(InitDone)
168165
}
169166

170167
// foundinitloop prints an init loop error and exits.
171-
func foundinitloop(node, visited *Node) {
168+
func (s *InitSchedule) foundinitloop(node, visited *Node) {
172169
// If there have already been errors printed,
173170
// those errors probably confused us and
174171
// there might not be a loop. Let the user
@@ -180,22 +177,22 @@ func foundinitloop(node, visited *Node) {
180177

181178
// Find the index of node and visited in the initlist.
182179
var nodeindex, visitedindex int
183-
for ; initlist[nodeindex] != node; nodeindex++ {
180+
for ; s.initlist[nodeindex] != node; nodeindex++ {
184181
}
185-
for ; initlist[visitedindex] != visited; visitedindex++ {
182+
for ; s.initlist[visitedindex] != visited; visitedindex++ {
186183
}
187184

188185
// There is a loop involving visited. We know about node and
189186
// initlist = n1 <- ... <- visited <- ... <- node <- ...
190187
fmt.Printf("%v: initialization loop:\n", visited.Line())
191188

192189
// Print visited -> ... -> n1 -> node.
193-
for _, n := range initlist[visitedindex:] {
190+
for _, n := range s.initlist[visitedindex:] {
194191
fmt.Printf("\t%v %v refers to\n", n.Line(), n.Sym)
195192
}
196193

197194
// Print node -> ... -> visited.
198-
for _, n := range initlist[nodeindex:visitedindex] {
195+
for _, n := range s.initlist[nodeindex:visitedindex] {
199196
fmt.Printf("\t%v %v refers to\n", n.Line(), n.Sym)
200197
}
201198

@@ -252,12 +249,13 @@ func (s *InitSchedule) initreorder(l []*Node) {
252249
// declarations and outputs the corresponding list of statements
253250
// to include in the init() function body.
254251
func initfix(l []*Node) []*Node {
255-
var s InitSchedule
256-
initplans = make(map[*Node]*InitPlan)
252+
s := InitSchedule{
253+
initplans: make(map[*Node]*InitPlan),
254+
inittemps: make(map[*Node]*Node),
255+
}
257256
lno := lineno
258257
s.initreorder(l)
259258
lineno = lno
260-
initplans = nil
261259
return s.out
262260
}
263261

@@ -328,13 +326,13 @@ func (s *InitSchedule) staticcopy(l *Node, r *Node) bool {
328326
switch r.Left.Op {
329327
case OARRAYLIT, OSLICELIT, OSTRUCTLIT, OMAPLIT:
330328
// copy pointer
331-
gdata(l, nod(OADDR, inittemps[r], nil), int(l.Type.Width))
329+
gdata(l, nod(OADDR, s.inittemps[r], nil), int(l.Type.Width))
332330
return true
333331
}
334332

335333
case OSLICELIT:
336334
// copy slice
337-
a := inittemps[r]
335+
a := s.inittemps[r]
338336

339337
n := l.copy()
340338
n.Xoffset = l.Xoffset + int64(array_array)
@@ -346,7 +344,7 @@ func (s *InitSchedule) staticcopy(l *Node, r *Node) bool {
346344
return true
347345

348346
case OARRAYLIT, OSTRUCTLIT:
349-
p := initplans[r]
347+
p := s.initplans[r]
350348

351349
n := l.copy()
352350
for i := range p.E {
@@ -408,7 +406,7 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
408406
// Init pointer.
409407
a := staticname(r.Left.Type)
410408

411-
inittemps[r] = a
409+
s.inittemps[r] = a
412410
gdata(l, nod(OADDR, a, nil), int(l.Type.Width))
413411

414412
// Init underlying literal.
@@ -427,12 +425,12 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
427425
}
428426

429427
case OSLICELIT:
430-
initplan(r)
428+
s.initplan(r)
431429
// Init slice.
432430
bound := r.Right.Int64()
433431
ta := types.NewArray(r.Type.Elem(), bound)
434432
a := staticname(ta)
435-
inittemps[r] = a
433+
s.inittemps[r] = a
436434
n := l.copy()
437435
n.Xoffset = l.Xoffset + int64(array_array)
438436
gdata(n, nod(OADDR, a, nil), Widthptr)
@@ -446,9 +444,9 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
446444
fallthrough
447445

448446
case OARRAYLIT, OSTRUCTLIT:
449-
initplan(r)
447+
s.initplan(r)
450448

451-
p := initplans[r]
449+
p := s.initplans[r]
452450
n := l.copy()
453451
for i := range p.E {
454452
e := &p.E[i]
@@ -530,7 +528,7 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
530528
} else {
531529
// Construct temp to hold val, write pointer to temp into n.
532530
a := staticname(val.Type)
533-
inittemps[val] = a
531+
s.inittemps[val] = a
534532
if !s.staticassign(a, val) {
535533
s.append(nod(OAS, a, val))
536534
}
@@ -1251,12 +1249,12 @@ func stataddr(nam *Node, n *Node) bool {
12511249
return false
12521250
}
12531251

1254-
func initplan(n *Node) {
1255-
if initplans[n] != nil {
1252+
func (s *InitSchedule) initplan(n *Node) {
1253+
if s.initplans[n] != nil {
12561254
return
12571255
}
12581256
p := new(InitPlan)
1259-
initplans[n] = p
1257+
s.initplans[n] = p
12601258
switch n.Op {
12611259
default:
12621260
Fatalf("initplan")
@@ -1271,7 +1269,7 @@ func initplan(n *Node) {
12711269
}
12721270
a = a.Right
12731271
}
1274-
addvalue(p, k*n.Type.Elem().Width, a)
1272+
s.addvalue(p, k*n.Type.Elem().Width, a)
12751273
k++
12761274
}
12771275

@@ -1280,29 +1278,29 @@ func initplan(n *Node) {
12801278
if a.Op != OSTRUCTKEY {
12811279
Fatalf("initplan structlit")
12821280
}
1283-
addvalue(p, a.Xoffset, a.Left)
1281+
s.addvalue(p, a.Xoffset, a.Left)
12841282
}
12851283

12861284
case OMAPLIT:
12871285
for _, a := range n.List.Slice() {
12881286
if a.Op != OKEY {
12891287
Fatalf("initplan maplit")
12901288
}
1291-
addvalue(p, -1, a.Right)
1289+
s.addvalue(p, -1, a.Right)
12921290
}
12931291
}
12941292
}
12951293

1296-
func addvalue(p *InitPlan, xoffset int64, n *Node) {
1294+
func (s *InitSchedule) addvalue(p *InitPlan, xoffset int64, n *Node) {
12971295
// special case: zero can be dropped entirely
12981296
if isZero(n) {
12991297
return
13001298
}
13011299

13021300
// special case: inline struct and array (not slice) literals
13031301
if isvaluelit(n) {
1304-
initplan(n)
1305-
q := initplans[n]
1302+
s.initplan(n)
1303+
q := s.initplans[n]
13061304
for _, qe := range q.E {
13071305
// qe is a copy; we are not modifying entries in q.E
13081306
qe.Xoffset += xoffset

0 commit comments

Comments
 (0)