Skip to content

Commit ee28360

Browse files
committed
cmd/compile: change sinit.go functions into methods
This will make it easier for subsequent CLs to track additional state during package initialization scheduling. Passes toolstash-check. Updates #22326. Change-Id: I528792ad34f41a4be52951531eb7525a94c9f350 Reviewed-on: https://go-review.googlesource.com/c/go/+/169898 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent c196d38 commit ee28360

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ func (o *Order) addrTemp(n *Node) *Node {
208208
dowidth(n.Type)
209209
vstat := staticname(n.Type)
210210
vstat.Name.SetReadonly(true)
211-
var out []*Node
212-
staticassign(vstat, n, &out)
213-
if out != nil {
211+
var s InitSchedule
212+
s.staticassign(vstat, n)
213+
if s.out != nil {
214214
Fatalf("staticassign of const generated code: %+v", n)
215215
}
216216
vstat = typecheck(vstat, ctxExpr)

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

+54-46
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type InitPlan struct {
2626
E []InitEntry
2727
}
2828

29+
type InitSchedule struct {
30+
out []*Node
31+
}
32+
33+
func (s *InitSchedule) append(n *Node) {
34+
s.out = append(s.out, n)
35+
}
36+
2937
var (
3038
initlist []*Node
3139
initplans map[*Node]*InitPlan
@@ -34,20 +42,20 @@ var (
3442

3543
// init1 walks the AST starting at n, and accumulates in out
3644
// the list of definitions needing init code in dependency order.
37-
func init1(n *Node, out *[]*Node) {
45+
func (s *InitSchedule) init1(n *Node) {
3846
if n == nil {
3947
return
4048
}
41-
init1(n.Left, out)
42-
init1(n.Right, out)
49+
s.init1(n.Left)
50+
s.init1(n.Right)
4351
for _, n1 := range n.List.Slice() {
44-
init1(n1, out)
52+
s.init1(n1)
4553
}
4654

4755
if n.isMethodExpression() {
4856
// Methods called as Type.Method(receiver, ...).
4957
// Definitions for method expressions are stored in type->nname.
50-
init1(asNode(n.Type.FuncType().Nname), out)
58+
s.init1(asNode(n.Type.FuncType().Nname))
5159
}
5260

5361
if n.Op != ONAME {
@@ -108,7 +116,7 @@ func init1(n *Node, out *[]*Node) {
108116
Fatalf("init1: bad defn")
109117

110118
case ODCLFUNC:
111-
init2list(defn.Nbody, out)
119+
s.init2list(defn.Nbody)
112120

113121
case OAS:
114122
if defn.Left != n {
@@ -122,15 +130,15 @@ func init1(n *Node, out *[]*Node) {
122130
break
123131
}
124132

125-
init2(defn.Right, out)
133+
s.init2(defn.Right)
126134
if Debug['j'] != 0 {
127135
fmt.Printf("%v\n", n.Sym)
128136
}
129-
if n.isBlank() || !staticinit(n, out) {
137+
if n.isBlank() || !s.staticinit(n) {
130138
if Debug['%'] != 0 {
131139
Dump("nonstatic", defn)
132140
}
133-
*out = append(*out, defn)
141+
s.append(defn)
134142
}
135143

136144
case OAS2FUNC, OAS2MAPR, OAS2DOTTYPE, OAS2RECV:
@@ -139,12 +147,12 @@ func init1(n *Node, out *[]*Node) {
139147
}
140148
defn.SetInitorder(InitPending)
141149
for _, n2 := range defn.Rlist.Slice() {
142-
init1(n2, out)
150+
s.init1(n2)
143151
}
144152
if Debug['%'] != 0 {
145153
Dump("nonstatic", defn)
146154
}
147-
*out = append(*out, defn)
155+
s.append(defn)
148156
defn.SetInitorder(InitDone)
149157
}
150158
}
@@ -196,7 +204,7 @@ func foundinitloop(node, visited *Node) {
196204
}
197205

198206
// recurse over n, doing init1 everywhere.
199-
func init2(n *Node, out *[]*Node) {
207+
func (s *InitSchedule) init2(n *Node) {
200208
if n == nil || n.Initorder() == InitDone {
201209
return
202210
}
@@ -205,70 +213,70 @@ func init2(n *Node, out *[]*Node) {
205213
Fatalf("name %v with ninit: %+v\n", n.Sym, n)
206214
}
207215

208-
init1(n, out)
209-
init2(n.Left, out)
210-
init2(n.Right, out)
211-
init2list(n.Ninit, out)
212-
init2list(n.List, out)
213-
init2list(n.Rlist, out)
214-
init2list(n.Nbody, out)
216+
s.init1(n)
217+
s.init2(n.Left)
218+
s.init2(n.Right)
219+
s.init2list(n.Ninit)
220+
s.init2list(n.List)
221+
s.init2list(n.Rlist)
222+
s.init2list(n.Nbody)
215223

216224
switch n.Op {
217225
case OCLOSURE:
218-
init2list(n.Func.Closure.Nbody, out)
226+
s.init2list(n.Func.Closure.Nbody)
219227
case ODOTMETH, OCALLPART:
220-
init2(asNode(n.Type.FuncType().Nname), out)
228+
s.init2(asNode(n.Type.FuncType().Nname))
221229
}
222230
}
223231

224-
func init2list(l Nodes, out *[]*Node) {
232+
func (s *InitSchedule) init2list(l Nodes) {
225233
for _, n := range l.Slice() {
226-
init2(n, out)
234+
s.init2(n)
227235
}
228236
}
229237

230-
func initreorder(l []*Node, out *[]*Node) {
238+
func (s *InitSchedule) initreorder(l []*Node) {
231239
for _, n := range l {
232240
switch n.Op {
233241
case ODCLFUNC, ODCLCONST, ODCLTYPE:
234242
continue
235243
}
236244

237-
initreorder(n.Ninit.Slice(), out)
245+
s.initreorder(n.Ninit.Slice())
238246
n.Ninit.Set(nil)
239-
init1(n, out)
247+
s.init1(n)
240248
}
241249
}
242250

243251
// initfix computes initialization order for a list l of top-level
244252
// declarations and outputs the corresponding list of statements
245253
// to include in the init() function body.
246254
func initfix(l []*Node) []*Node {
247-
var lout []*Node
255+
var s InitSchedule
248256
initplans = make(map[*Node]*InitPlan)
249257
lno := lineno
250-
initreorder(l, &lout)
258+
s.initreorder(l)
251259
lineno = lno
252260
initplans = nil
253-
return lout
261+
return s.out
254262
}
255263

256264
// compilation of top-level (static) assignments
257265
// into DATA statements if at all possible.
258-
func staticinit(n *Node, out *[]*Node) bool {
266+
func (s *InitSchedule) staticinit(n *Node) bool {
259267
if n.Op != ONAME || n.Class() != PEXTERN || n.Name.Defn == nil || n.Name.Defn.Op != OAS {
260268
Fatalf("staticinit")
261269
}
262270

263271
lineno = n.Pos
264272
l := n.Name.Defn.Left
265273
r := n.Name.Defn.Right
266-
return staticassign(l, r, out)
274+
return s.staticassign(l, r)
267275
}
268276

269277
// like staticassign but we are copying an already
270278
// initialized value r.
271-
func staticcopy(l *Node, r *Node, out *[]*Node) bool {
279+
func (s *InitSchedule) staticcopy(l *Node, r *Node) bool {
272280
if r.Op != ONAME {
273281
return false
274282
}
@@ -294,12 +302,12 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
294302

295303
switch r.Op {
296304
case ONAME:
297-
if staticcopy(l, r, out) {
305+
if s.staticcopy(l, r) {
298306
return true
299307
}
300308
// We may have skipped past one or more OCONVNOPs, so
301309
// use conv to ensure r is assignable to l (#13263).
302-
*out = append(*out, nod(OAS, l, conv(r, l.Type)))
310+
s.append(nod(OAS, l, conv(r, l.Type)))
303311
return true
304312

305313
case OLITERAL:
@@ -350,7 +358,7 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
350358
continue
351359
}
352360
ll := n.sepcopy()
353-
if staticcopy(ll, e.Expr, out) {
361+
if s.staticcopy(ll, e.Expr) {
354362
continue
355363
}
356364
// Requires computation, but we're
@@ -359,7 +367,7 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
359367
rr.Type = ll.Type
360368
rr.Xoffset += e.Xoffset
361369
setlineno(rr)
362-
*out = append(*out, nod(OAS, ll, rr))
370+
s.append(nod(OAS, ll, rr))
363371
}
364372

365373
return true
@@ -368,14 +376,14 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
368376
return false
369377
}
370378

371-
func staticassign(l *Node, r *Node, out *[]*Node) bool {
379+
func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
372380
for r.Op == OCONVNOP {
373381
r = r.Left
374382
}
375383

376384
switch r.Op {
377385
case ONAME:
378-
return staticcopy(l, r, out)
386+
return s.staticcopy(l, r)
379387

380388
case OLITERAL:
381389
if isZero(r) {
@@ -404,8 +412,8 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
404412
gdata(l, nod(OADDR, a, nil), int(l.Type.Width))
405413

406414
// Init underlying literal.
407-
if !staticassign(a, r.Left, out) {
408-
*out = append(*out, nod(OAS, a, r.Left))
415+
if !s.staticassign(a, r.Left) {
416+
s.append(nod(OAS, a, r.Left))
409417
}
410418
return true
411419
}
@@ -452,8 +460,8 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
452460
}
453461
setlineno(e.Expr)
454462
a := n.sepcopy()
455-
if !staticassign(a, e.Expr, out) {
456-
*out = append(*out, nod(OAS, a, e.Expr))
463+
if !s.staticassign(a, e.Expr) {
464+
s.append(nod(OAS, a, e.Expr))
457465
}
458466
}
459467

@@ -516,15 +524,15 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
516524
n.Type = val.Type
517525
setlineno(val)
518526
a := n.sepcopy()
519-
if !staticassign(a, val, out) {
520-
*out = append(*out, nod(OAS, a, val))
527+
if !s.staticassign(a, val) {
528+
s.append(nod(OAS, a, val))
521529
}
522530
} else {
523531
// Construct temp to hold val, write pointer to temp into n.
524532
a := staticname(val.Type)
525533
inittemps[val] = a
526-
if !staticassign(a, val, out) {
527-
*out = append(*out, nod(OAS, a, val))
534+
if !s.staticassign(a, val) {
535+
s.append(nod(OAS, a, val))
528536
}
529537
ptr := nod(OADDR, a, nil)
530538
n.Type = types.NewPtr(val.Type)

0 commit comments

Comments
 (0)