@@ -27,19 +27,16 @@ type InitPlan struct {
27
27
}
28
28
29
29
type InitSchedule struct {
30
- out []* Node
30
+ out []* Node
31
+ initlist []* Node
32
+ initplans map [* Node ]* InitPlan
33
+ inittemps map [* Node ]* Node
31
34
}
32
35
33
36
func (s * InitSchedule ) append (n * Node ) {
34
37
s .out = append (s .out , n )
35
38
}
36
39
37
- var (
38
- initlist []* Node
39
- initplans map [* Node ]* InitPlan
40
- inittemps = make (map [* Node ]* Node )
41
- )
42
-
43
40
// init1 walks the AST starting at n, and accumulates in out
44
41
// the list of definitions needing init code in dependency order.
45
42
func (s * InitSchedule ) init1 (n * Node ) {
@@ -86,16 +83,16 @@ func (s *InitSchedule) init1(n *Node) {
86
83
// a variable in the program, the tree walk will reach a cycle
87
84
// involving that variable.
88
85
if n .Class () != PFUNC {
89
- foundinitloop (n , n )
86
+ s . foundinitloop (n , n )
90
87
}
91
88
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 ]
94
91
if x == n {
95
92
break
96
93
}
97
94
if x .Class () != PFUNC {
98
- foundinitloop (n , x )
95
+ s . foundinitloop (n , x )
99
96
}
100
97
}
101
98
@@ -105,7 +102,7 @@ func (s *InitSchedule) init1(n *Node) {
105
102
106
103
// reached a new unvisited node.
107
104
n .SetInitorder (InitPending )
108
- initlist = append (initlist , n )
105
+ s . initlist = append (s . initlist , n )
109
106
110
107
// make sure that everything n depends on is initialized.
111
108
// n->defn is an assignment to n
@@ -157,18 +154,18 @@ func (s *InitSchedule) init1(n *Node) {
157
154
}
158
155
}
159
156
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 )
163
160
}
164
- initlist [last ] = nil // allow GC
165
- initlist = initlist [:last ]
161
+ s . initlist [last ] = nil // allow GC
162
+ s . initlist = s . initlist [:last ]
166
163
167
164
n .SetInitorder (InitDone )
168
165
}
169
166
170
167
// foundinitloop prints an init loop error and exits.
171
- func foundinitloop (node , visited * Node ) {
168
+ func ( s * InitSchedule ) foundinitloop (node , visited * Node ) {
172
169
// If there have already been errors printed,
173
170
// those errors probably confused us and
174
171
// there might not be a loop. Let the user
@@ -180,22 +177,22 @@ func foundinitloop(node, visited *Node) {
180
177
181
178
// Find the index of node and visited in the initlist.
182
179
var nodeindex , visitedindex int
183
- for ; initlist [nodeindex ] != node ; nodeindex ++ {
180
+ for ; s . initlist [nodeindex ] != node ; nodeindex ++ {
184
181
}
185
- for ; initlist [visitedindex ] != visited ; visitedindex ++ {
182
+ for ; s . initlist [visitedindex ] != visited ; visitedindex ++ {
186
183
}
187
184
188
185
// There is a loop involving visited. We know about node and
189
186
// initlist = n1 <- ... <- visited <- ... <- node <- ...
190
187
fmt .Printf ("%v: initialization loop:\n " , visited .Line ())
191
188
192
189
// Print visited -> ... -> n1 -> node.
193
- for _ , n := range initlist [visitedindex :] {
190
+ for _ , n := range s . initlist [visitedindex :] {
194
191
fmt .Printf ("\t %v %v refers to\n " , n .Line (), n .Sym )
195
192
}
196
193
197
194
// Print node -> ... -> visited.
198
- for _ , n := range initlist [nodeindex :visitedindex ] {
195
+ for _ , n := range s . initlist [nodeindex :visitedindex ] {
199
196
fmt .Printf ("\t %v %v refers to\n " , n .Line (), n .Sym )
200
197
}
201
198
@@ -252,12 +249,13 @@ func (s *InitSchedule) initreorder(l []*Node) {
252
249
// declarations and outputs the corresponding list of statements
253
250
// to include in the init() function body.
254
251
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
+ }
257
256
lno := lineno
258
257
s .initreorder (l )
259
258
lineno = lno
260
- initplans = nil
261
259
return s .out
262
260
}
263
261
@@ -328,13 +326,13 @@ func (s *InitSchedule) staticcopy(l *Node, r *Node) bool {
328
326
switch r .Left .Op {
329
327
case OARRAYLIT , OSLICELIT , OSTRUCTLIT , OMAPLIT :
330
328
// 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 ))
332
330
return true
333
331
}
334
332
335
333
case OSLICELIT :
336
334
// copy slice
337
- a := inittemps [r ]
335
+ a := s . inittemps [r ]
338
336
339
337
n := l .copy ()
340
338
n .Xoffset = l .Xoffset + int64 (array_array )
@@ -346,7 +344,7 @@ func (s *InitSchedule) staticcopy(l *Node, r *Node) bool {
346
344
return true
347
345
348
346
case OARRAYLIT , OSTRUCTLIT :
349
- p := initplans [r ]
347
+ p := s . initplans [r ]
350
348
351
349
n := l .copy ()
352
350
for i := range p .E {
@@ -408,7 +406,7 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
408
406
// Init pointer.
409
407
a := staticname (r .Left .Type )
410
408
411
- inittemps [r ] = a
409
+ s . inittemps [r ] = a
412
410
gdata (l , nod (OADDR , a , nil ), int (l .Type .Width ))
413
411
414
412
// Init underlying literal.
@@ -427,12 +425,12 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
427
425
}
428
426
429
427
case OSLICELIT :
430
- initplan (r )
428
+ s . initplan (r )
431
429
// Init slice.
432
430
bound := r .Right .Int64 ()
433
431
ta := types .NewArray (r .Type .Elem (), bound )
434
432
a := staticname (ta )
435
- inittemps [r ] = a
433
+ s . inittemps [r ] = a
436
434
n := l .copy ()
437
435
n .Xoffset = l .Xoffset + int64 (array_array )
438
436
gdata (n , nod (OADDR , a , nil ), Widthptr )
@@ -446,9 +444,9 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
446
444
fallthrough
447
445
448
446
case OARRAYLIT , OSTRUCTLIT :
449
- initplan (r )
447
+ s . initplan (r )
450
448
451
- p := initplans [r ]
449
+ p := s . initplans [r ]
452
450
n := l .copy ()
453
451
for i := range p .E {
454
452
e := & p .E [i ]
@@ -530,7 +528,7 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
530
528
} else {
531
529
// Construct temp to hold val, write pointer to temp into n.
532
530
a := staticname (val .Type )
533
- inittemps [val ] = a
531
+ s . inittemps [val ] = a
534
532
if ! s .staticassign (a , val ) {
535
533
s .append (nod (OAS , a , val ))
536
534
}
@@ -1251,12 +1249,12 @@ func stataddr(nam *Node, n *Node) bool {
1251
1249
return false
1252
1250
}
1253
1251
1254
- func initplan (n * Node ) {
1255
- if initplans [n ] != nil {
1252
+ func ( s * InitSchedule ) initplan (n * Node ) {
1253
+ if s . initplans [n ] != nil {
1256
1254
return
1257
1255
}
1258
1256
p := new (InitPlan )
1259
- initplans [n ] = p
1257
+ s . initplans [n ] = p
1260
1258
switch n .Op {
1261
1259
default :
1262
1260
Fatalf ("initplan" )
@@ -1271,7 +1269,7 @@ func initplan(n *Node) {
1271
1269
}
1272
1270
a = a .Right
1273
1271
}
1274
- addvalue (p , k * n .Type .Elem ().Width , a )
1272
+ s . addvalue (p , k * n .Type .Elem ().Width , a )
1275
1273
k ++
1276
1274
}
1277
1275
@@ -1280,29 +1278,29 @@ func initplan(n *Node) {
1280
1278
if a .Op != OSTRUCTKEY {
1281
1279
Fatalf ("initplan structlit" )
1282
1280
}
1283
- addvalue (p , a .Xoffset , a .Left )
1281
+ s . addvalue (p , a .Xoffset , a .Left )
1284
1282
}
1285
1283
1286
1284
case OMAPLIT :
1287
1285
for _ , a := range n .List .Slice () {
1288
1286
if a .Op != OKEY {
1289
1287
Fatalf ("initplan maplit" )
1290
1288
}
1291
- addvalue (p , - 1 , a .Right )
1289
+ s . addvalue (p , - 1 , a .Right )
1292
1290
}
1293
1291
}
1294
1292
}
1295
1293
1296
- func addvalue (p * InitPlan , xoffset int64 , n * Node ) {
1294
+ func ( s * InitSchedule ) addvalue (p * InitPlan , xoffset int64 , n * Node ) {
1297
1295
// special case: zero can be dropped entirely
1298
1296
if isZero (n ) {
1299
1297
return
1300
1298
}
1301
1299
1302
1300
// special case: inline struct and array (not slice) literals
1303
1301
if isvaluelit (n ) {
1304
- initplan (n )
1305
- q := initplans [n ]
1302
+ s . initplan (n )
1303
+ q := s . initplans [n ]
1306
1304
for _ , qe := range q .E {
1307
1305
// qe is a copy; we are not modifying entries in q.E
1308
1306
qe .Xoffset += xoffset
0 commit comments