}
type InitPlan struct {
- Lit int64
- Zero int64
- Expr int64
- E []InitEntry
+ E []InitEntry
}
var (
if a.Op != OKEY || !Smallintconst(a.Left) {
Fatalf("initplan arraylit")
}
- addvalue(p, n.Type.Type.Width*Mpgetfix(a.Left.Val().U.(*Mpint)), nil, a.Right)
+ addvalue(p, n.Type.Type.Width*Mpgetfix(a.Left.Val().U.(*Mpint)), a.Right)
}
case OSTRUCTLIT:
if a.Op != OKEY || a.Left.Type == nil {
Fatalf("initplan structlit")
}
- addvalue(p, a.Left.Type.Width, nil, a.Right)
+ addvalue(p, a.Left.Type.Width, a.Right)
}
case OMAPLIT:
if a.Op != OKEY {
Fatalf("initplan maplit")
}
- addvalue(p, -1, a.Left, a.Right)
+ addvalue(p, -1, a.Right)
}
}
}
-func addvalue(p *InitPlan, xoffset int64, key *Node, n *Node) {
+func addvalue(p *InitPlan, xoffset int64, n *Node) {
// special case: zero can be dropped entirely
if iszero(n) {
- p.Zero += n.Type.Width
return
}
initplan(n)
q := initplans[n]
for _, qe := range q.E {
- e := entry(p)
- *e = qe
- e.Xoffset += xoffset
+ // qe is a copy; we are not modifying entries in q.E
+ qe.Xoffset += xoffset
+ p.E = append(p.E, qe)
}
return
}
// add to plan
- if n.Op == OLITERAL {
- p.Lit += n.Type.Width
- } else {
- p.Expr += n.Type.Width
- }
-
- e := entry(p)
- e.Xoffset = xoffset
- e.Expr = n
+ p.E = append(p.E, InitEntry{Xoffset: xoffset, Expr: n})
}
func iszero(n *Node) bool {
return (n.Op == OARRAYLIT && Isfixedarray(n.Type)) || n.Op == OSTRUCTLIT
}
-func entry(p *InitPlan) *InitEntry {
- p.E = append(p.E, InitEntry{})
- return &p.E[len(p.E)-1]
-}
-
// gen_as_init attempts to emit static data for n and reports whether it succeeded.
// If reportOnly is true, it does not emit static data and does not modify the AST.
func gen_as_init(n *Node, reportOnly bool) bool {