2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2015-10-22 09:51:12 +09:00
|
|
|
// static initialization
|
2015-02-13 14:40:36 -05:00
|
|
|
const (
|
|
|
|
|
InitNotStarted = 0
|
|
|
|
|
InitDone = 1
|
|
|
|
|
InitPending = 2
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-11 00:44:39 -08:00
|
|
|
type InitEntry struct {
|
|
|
|
|
Xoffset int64 // struct, array only
|
|
|
|
|
Expr *Node // bytes of run-time computed expressions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type InitPlan struct {
|
2016-03-13 17:48:17 -07:00
|
|
|
E []InitEntry
|
2016-03-11 00:44:39 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-03 14:16:01 -04:00
|
|
|
var (
|
2015-09-03 17:07:00 +10:00
|
|
|
initlist []*Node
|
2015-06-03 14:16:01 -04:00
|
|
|
initplans map[*Node]*InitPlan
|
|
|
|
|
inittemps = make(map[*Node]*Node)
|
|
|
|
|
)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// init1 walks the AST starting at n, and accumulates in out
|
|
|
|
|
// the list of definitions needing init code in dependency order.
|
2016-02-27 14:31:33 -08:00
|
|
|
func init1(n *Node, out *[]*Node) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
init1(n.Left, out)
|
|
|
|
|
init1(n.Right, out)
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n1 := range n.List.Slice() {
|
|
|
|
|
init1(n1, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Left != nil && n.Type != nil && n.Left.Op == OTYPE && n.Class == PFUNC {
|
|
|
|
|
// Methods called as Type.Method(receiver, ...).
|
|
|
|
|
// Definitions for method expressions are stored in type->nname.
|
|
|
|
|
init1(n.Type.Nname, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Op != ONAME {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch n.Class {
|
2015-04-01 09:38:44 -07:00
|
|
|
case PEXTERN, PFUNC:
|
2015-02-13 14:40:36 -05:00
|
|
|
default:
|
2015-05-27 07:31:56 -04:00
|
|
|
if isblank(n) && n.Name.Curfn == nil && n.Name.Defn != nil && n.Name.Defn.Initorder == InitNotStarted {
|
2015-02-13 14:40:36 -05:00
|
|
|
// blank names initialization is part of init() but not
|
|
|
|
|
// when they are inside a function.
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Initorder == InitDone {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if n.Initorder == InitPending {
|
|
|
|
|
// Since mutually recursive sets of functions are allowed,
|
|
|
|
|
// we don't necessarily raise an error if n depends on a node
|
|
|
|
|
// which is already waiting for its dependencies to be visited.
|
|
|
|
|
//
|
|
|
|
|
// initlist contains a cycle of identifiers referring to each other.
|
|
|
|
|
// If this cycle contains a variable, then this variable refers to itself.
|
|
|
|
|
// Conversely, if there exists an initialization cycle involving
|
|
|
|
|
// a variable in the program, the tree walk will reach a cycle
|
|
|
|
|
// involving that variable.
|
|
|
|
|
if n.Class != PFUNC {
|
2015-09-03 17:07:00 +10:00
|
|
|
foundinitloop(n, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-03 17:07:00 +10:00
|
|
|
for i := len(initlist) - 1; i >= 0; i-- {
|
|
|
|
|
x := initlist[i]
|
|
|
|
|
if x == n {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if x.Class != PFUNC {
|
|
|
|
|
foundinitloop(n, x)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The loop involves only functions, ok.
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reached a new unvisited node.
|
|
|
|
|
n.Initorder = InitPending
|
2015-09-03 17:07:00 +10:00
|
|
|
initlist = append(initlist, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// make sure that everything n depends on is initialized.
|
|
|
|
|
// n->defn is an assignment to n
|
2015-05-26 22:19:27 -04:00
|
|
|
if defn := n.Name.Defn; defn != nil {
|
|
|
|
|
switch defn.Op {
|
2015-02-13 14:40:36 -05:00
|
|
|
default:
|
2015-09-03 17:07:00 +10:00
|
|
|
Dump("defn", defn)
|
|
|
|
|
Fatalf("init1: bad defn")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case ODCLFUNC:
|
2016-03-04 16:13:17 -08:00
|
|
|
init2list(defn.Nbody, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OAS:
|
2015-05-26 22:19:27 -04:00
|
|
|
if defn.Left != n {
|
2015-09-03 17:07:00 +10:00
|
|
|
Dump("defn", defn)
|
|
|
|
|
Fatalf("init1: bad defn")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-05-26 22:19:27 -04:00
|
|
|
if isblank(defn.Left) && candiscard(defn.Right) {
|
|
|
|
|
defn.Op = OEMPTY
|
|
|
|
|
defn.Left = nil
|
|
|
|
|
defn.Right = nil
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-26 22:19:27 -04:00
|
|
|
init2(defn.Right, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
if Debug['j'] != 0 {
|
2015-04-17 12:03:22 -04:00
|
|
|
fmt.Printf("%v\n", n.Sym)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-17 22:13:49 -05:00
|
|
|
if isblank(n) || !staticinit(n, out) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if Debug['%'] != 0 {
|
2015-05-26 22:19:27 -04:00
|
|
|
Dump("nonstatic", defn)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-02-27 14:31:33 -08:00
|
|
|
*out = append(*out, defn)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-04-01 09:38:44 -07:00
|
|
|
case OAS2FUNC, OAS2MAPR, OAS2DOTTYPE, OAS2RECV:
|
2016-01-03 18:44:15 -08:00
|
|
|
if defn.Initorder == InitDone {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
2016-01-03 18:44:15 -08:00
|
|
|
defn.Initorder = InitPending
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n2 := range defn.Rlist.Slice() {
|
|
|
|
|
init1(n2, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if Debug['%'] != 0 {
|
2015-05-26 22:19:27 -04:00
|
|
|
Dump("nonstatic", defn)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-02-27 14:31:33 -08:00
|
|
|
*out = append(*out, defn)
|
2016-01-03 18:44:15 -08:00
|
|
|
defn.Initorder = InitDone
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-03 17:07:00 +10:00
|
|
|
last := len(initlist) - 1
|
|
|
|
|
if initlist[last] != n {
|
|
|
|
|
Fatalf("bad initlist %v", initlist)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-03 17:07:00 +10:00
|
|
|
initlist[last] = nil // allow GC
|
|
|
|
|
initlist = initlist[:last]
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
n.Initorder = InitDone
|
|
|
|
|
return
|
2015-09-03 17:07:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// foundinitloop prints an init loop error and exits.
|
|
|
|
|
func foundinitloop(node, visited *Node) {
|
|
|
|
|
// If there have already been errors printed,
|
|
|
|
|
// those errors probably confused us and
|
|
|
|
|
// there might not be a loop. Let the user
|
|
|
|
|
// fix those first.
|
|
|
|
|
Flusherrors()
|
|
|
|
|
if nerrors > 0 {
|
|
|
|
|
errorexit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the index of node and visited in the initlist.
|
|
|
|
|
var nodeindex, visitedindex int
|
|
|
|
|
for ; initlist[nodeindex] != node; nodeindex++ {
|
|
|
|
|
}
|
|
|
|
|
for ; initlist[visitedindex] != visited; visitedindex++ {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There is a loop involving visited. We know about node and
|
|
|
|
|
// initlist = n1 <- ... <- visited <- ... <- node <- ...
|
|
|
|
|
fmt.Printf("%v: initialization loop:\n", visited.Line())
|
|
|
|
|
|
|
|
|
|
// Print visited -> ... -> n1 -> node.
|
|
|
|
|
for _, n := range initlist[visitedindex:] {
|
|
|
|
|
fmt.Printf("\t%v %v refers to\n", n.Line(), n.Sym)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print node -> ... -> visited.
|
|
|
|
|
for _, n := range initlist[nodeindex:visitedindex] {
|
|
|
|
|
fmt.Printf("\t%v %v refers to\n", n.Line(), n.Sym)
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-09-03 17:07:00 +10:00
|
|
|
fmt.Printf("\t%v %v\n", visited.Line(), visited.Sym)
|
|
|
|
|
errorexit()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// recurse over n, doing init1 everywhere.
|
2016-02-27 14:31:33 -08:00
|
|
|
func init2(n *Node, out *[]*Node) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n == nil || n.Initorder == InitDone {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.Op == ONAME && n.Ninit.Len() != 0 {
|
2016-03-15 13:06:58 -07:00
|
|
|
Fatalf("name %v with ninit: %v\n", n.Sym, Nconv(n, FmtSign))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init1(n, out)
|
|
|
|
|
init2(n.Left, out)
|
|
|
|
|
init2(n.Right, out)
|
|
|
|
|
init2list(n.Ninit, out)
|
|
|
|
|
init2list(n.List, out)
|
|
|
|
|
init2list(n.Rlist, out)
|
2016-03-04 16:13:17 -08:00
|
|
|
init2list(n.Nbody, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
if n.Op == OCLOSURE {
|
2016-03-04 16:13:17 -08:00
|
|
|
init2list(n.Func.Closure.Nbody, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if n.Op == ODOTMETH || n.Op == OCALLPART {
|
|
|
|
|
init2(n.Type.Nname, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:26:20 -08:00
|
|
|
func init2list(l Nodes, out *[]*Node) {
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n := range l.Slice() {
|
|
|
|
|
init2(n, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:26:20 -08:00
|
|
|
func initreorder(l []*Node, out *[]*Node) {
|
2015-02-13 14:40:36 -05:00
|
|
|
var n *Node
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n = range l {
|
2015-02-13 14:40:36 -05:00
|
|
|
switch n.Op {
|
2015-04-01 09:38:44 -07:00
|
|
|
case ODCLFUNC, ODCLCONST, ODCLTYPE:
|
2015-02-13 14:40:36 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:26:20 -08:00
|
|
|
initreorder(n.Ninit.Slice(), out)
|
2016-03-08 15:10:26 -08:00
|
|
|
n.Ninit.Set(nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
init1(n, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initfix computes initialization order for a list l of top-level
|
|
|
|
|
// declarations and outputs the corresponding list of statements
|
|
|
|
|
// to include in the init() function body.
|
2016-03-08 10:26:20 -08:00
|
|
|
func initfix(l []*Node) []*Node {
|
2016-02-27 14:31:33 -08:00
|
|
|
var lout []*Node
|
2015-06-03 14:16:01 -04:00
|
|
|
initplans = make(map[*Node]*InitPlan)
|
2016-03-02 17:34:42 -08:00
|
|
|
lno := lineno
|
2015-02-13 14:40:36 -05:00
|
|
|
initreorder(l, &lout)
|
2016-03-02 17:34:42 -08:00
|
|
|
lineno = lno
|
2015-06-03 14:16:01 -04:00
|
|
|
initplans = nil
|
2015-02-13 14:40:36 -05:00
|
|
|
return lout
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-22 09:51:12 +09:00
|
|
|
// compilation of top-level (static) assignments
|
|
|
|
|
// into DATA statements if at all possible.
|
2016-02-27 14:31:33 -08:00
|
|
|
func staticinit(n *Node, out *[]*Node) bool {
|
2015-05-26 22:19:27 -04:00
|
|
|
if n.Op != ONAME || n.Class != PEXTERN || n.Name.Defn == nil || n.Name.Defn.Op != OAS {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("staticinit")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lineno = n.Lineno
|
2015-05-26 22:19:27 -04:00
|
|
|
l := n.Name.Defn.Left
|
|
|
|
|
r := n.Name.Defn.Right
|
2015-02-13 14:40:36 -05:00
|
|
|
return staticassign(l, r, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// like staticassign but we are copying an already
|
|
|
|
|
// initialized value r.
|
2016-02-27 14:31:33 -08:00
|
|
|
func staticcopy(l *Node, r *Node, out *[]*Node) bool {
|
2015-03-04 16:33:28 -08:00
|
|
|
if r.Op != ONAME {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if r.Class == PFUNC {
|
|
|
|
|
gdata(l, r, Widthptr)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if r.Class != PEXTERN || r.Sym.Pkg != localpkg {
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-05-26 22:19:27 -04:00
|
|
|
if r.Name.Defn == nil { // probably zeroed but perhaps supplied externally and of unknown value
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-05-26 22:19:27 -04:00
|
|
|
if r.Name.Defn.Op != OAS {
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
orig := r
|
2015-05-26 22:19:27 -04:00
|
|
|
r = r.Name.Defn.Right
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-06-29 12:49:25 -04:00
|
|
|
for r.Op == OCONVNOP {
|
|
|
|
|
r = r.Left
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
switch r.Op {
|
|
|
|
|
case ONAME:
|
2015-02-17 22:13:49 -05:00
|
|
|
if staticcopy(l, r, out) {
|
|
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-02-27 14:31:33 -08:00
|
|
|
*out = append(*out, Nod(OAS, l, r))
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OLITERAL:
|
2015-02-17 22:13:49 -05:00
|
|
|
if iszero(r) {
|
|
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
gdata(l, r, int(l.Type.Width))
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OADDR:
|
|
|
|
|
switch r.Left.Op {
|
|
|
|
|
case ONAME:
|
|
|
|
|
gdata(l, r, int(l.Type.Width))
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OPTRLIT:
|
|
|
|
|
switch r.Left.Op {
|
|
|
|
|
//dump("not static addr", r);
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
// copy pointer
|
2015-04-01 09:38:44 -07:00
|
|
|
case OARRAYLIT, OSTRUCTLIT, OMAPLIT:
|
2015-05-27 10:44:23 -04:00
|
|
|
gdata(l, Nod(OADDR, inittemps[r], nil), int(l.Type.Width))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
2015-02-17 22:13:49 -05:00
|
|
|
if Isslice(r.Type) {
|
2015-02-13 14:40:36 -05:00
|
|
|
// copy slice
|
2015-05-27 10:44:23 -04:00
|
|
|
a := inittemps[r]
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-09-08 22:22:44 +02:00
|
|
|
n := *l
|
|
|
|
|
n.Xoffset = l.Xoffset + int64(Array_array)
|
|
|
|
|
gdata(&n, Nod(OADDR, a, nil), Widthptr)
|
|
|
|
|
n.Xoffset = l.Xoffset + int64(Array_nel)
|
|
|
|
|
gdata(&n, r.Right, Widthint)
|
|
|
|
|
n.Xoffset = l.Xoffset + int64(Array_cap)
|
|
|
|
|
gdata(&n, r.Right, Widthint)
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
// fall through
|
|
|
|
|
case OSTRUCTLIT:
|
2015-05-22 01:16:52 -04:00
|
|
|
p := initplans[r]
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-09-08 22:22:44 +02:00
|
|
|
n := *l
|
|
|
|
|
for i := range p.E {
|
|
|
|
|
e := &p.E[i]
|
|
|
|
|
n.Xoffset = l.Xoffset + e.Xoffset
|
|
|
|
|
n.Type = e.Expr.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
if e.Expr.Op == OLITERAL {
|
2015-09-08 22:22:44 +02:00
|
|
|
gdata(&n, e.Expr, int(n.Type.Width))
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2015-09-08 22:22:44 +02:00
|
|
|
ll := Nod(OXXX, nil, nil)
|
|
|
|
|
*ll = n
|
2015-02-13 14:40:36 -05:00
|
|
|
ll.Orig = ll // completely separate copy
|
2015-02-17 22:13:49 -05:00
|
|
|
if !staticassign(ll, e.Expr, out) {
|
2015-02-13 14:40:36 -05:00
|
|
|
// Requires computation, but we're
|
|
|
|
|
// copying someone else's computation.
|
2015-09-08 22:22:44 +02:00
|
|
|
rr := Nod(OXXX, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
*rr = *orig
|
|
|
|
|
rr.Orig = rr // completely separate copy
|
|
|
|
|
rr.Type = ll.Type
|
|
|
|
|
rr.Xoffset += e.Xoffset
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(rr)
|
2016-02-27 14:31:33 -08:00
|
|
|
*out = append(*out, Nod(OAS, ll, rr))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
func staticassign(l *Node, r *Node, out *[]*Node) bool {
|
2015-06-29 12:49:25 -04:00
|
|
|
for r.Op == OCONVNOP {
|
|
|
|
|
r = r.Left
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
switch r.Op {
|
|
|
|
|
case ONAME:
|
2015-03-04 16:33:28 -08:00
|
|
|
return staticcopy(l, r, out)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OLITERAL:
|
2015-02-17 22:13:49 -05:00
|
|
|
if iszero(r) {
|
|
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
gdata(l, r, int(l.Type.Width))
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OADDR:
|
2015-02-23 16:07:24 -05:00
|
|
|
var nam Node
|
2015-02-17 22:13:49 -05:00
|
|
|
if stataddr(&nam, r.Left) {
|
2015-09-08 22:22:44 +02:00
|
|
|
n := *r
|
|
|
|
|
n.Left = &nam
|
|
|
|
|
gdata(l, &n, int(l.Type.Width))
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
case OPTRLIT:
|
|
|
|
|
switch r.Left.Op {
|
2015-04-01 09:38:44 -07:00
|
|
|
case OARRAYLIT, OMAPLIT, OSTRUCTLIT:
|
cmd/compile: captureless closures are constants
In particular, we can initialize globals with them at link time instead
of generating code for them in an init() function. Less code, less
startup cost.
But the real reason for this change is binary size. This change reduces
the binary size of hello world by ~4%.
The culprit is fmt.ssFree, a global variable which is a sync.Pool of
scratch scan states. It is initalized with a captureless closure as the
pool's New action. That action in turn references all the scanf code.
If you never call any of the fmt.Scanf* routines, ssFree is never used.
But before this change, ssFree is still referenced by fmt's init
function. That keeps ssFree and all the code it references in the
binary. With this change, ssFree is initialized at link time. As a
result, fmt.init never mentions ssFree. If you don't call fmt.Scanf*,
ssFree is unreferenced and it and the scanf code are not included.
This change is an easy fix for what is generally a much harder problem,
the unnecessary initializing of unused globals (and retention of code
that they reference). Ideally we should have separate init code for
each global and only include that code if the corresponding global is
live. (We'd need to make sure that the initializing code has no side
effects, except on the global being initialized.) That is a much harder
change.
Update #6853
Change-Id: I19d1e33992287882c83efea6ce113b7cfc504b67
Reviewed-on: https://go-review.googlesource.com/17398
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-12-03 13:20:58 -08:00
|
|
|
// Init pointer.
|
2015-02-23 16:07:24 -05:00
|
|
|
a := staticname(r.Left.Type, 1)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-27 10:44:23 -04:00
|
|
|
inittemps[r] = a
|
2015-02-13 14:40:36 -05:00
|
|
|
gdata(l, Nod(OADDR, a, nil), int(l.Type.Width))
|
|
|
|
|
|
|
|
|
|
// Init underlying literal.
|
2015-02-17 22:13:49 -05:00
|
|
|
if !staticassign(a, r.Left, out) {
|
2016-02-27 14:31:33 -08:00
|
|
|
*out = append(*out, Nod(OAS, a, r.Left))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
cmd/compile: captureless closures are constants
In particular, we can initialize globals with them at link time instead
of generating code for them in an init() function. Less code, less
startup cost.
But the real reason for this change is binary size. This change reduces
the binary size of hello world by ~4%.
The culprit is fmt.ssFree, a global variable which is a sync.Pool of
scratch scan states. It is initalized with a captureless closure as the
pool's New action. That action in turn references all the scanf code.
If you never call any of the fmt.Scanf* routines, ssFree is never used.
But before this change, ssFree is still referenced by fmt's init
function. That keeps ssFree and all the code it references in the
binary. With this change, ssFree is initialized at link time. As a
result, fmt.init never mentions ssFree. If you don't call fmt.Scanf*,
ssFree is unreferenced and it and the scanf code are not included.
This change is an easy fix for what is generally a much harder problem,
the unnecessary initializing of unused globals (and retention of code
that they reference). Ideally we should have separate init code for
each global and only include that code if the corresponding global is
live. (We'd need to make sure that the initializing code has no side
effects, except on the global being initialized.) That is a much harder
change.
Update #6853
Change-Id: I19d1e33992287882c83efea6ce113b7cfc504b67
Reviewed-on: https://go-review.googlesource.com/17398
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-12-03 13:20:58 -08:00
|
|
|
//dump("not static ptrlit", r);
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OSTRARRAYBYTE:
|
|
|
|
|
if l.Class == PEXTERN && r.Left.Op == OLITERAL {
|
2015-05-27 00:47:05 -04:00
|
|
|
sval := r.Left.Val().U.(string)
|
2015-03-02 16:03:26 -05:00
|
|
|
slicebytes(l, sval, len(sval))
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
|
|
|
|
initplan(r)
|
2015-02-17 22:13:49 -05:00
|
|
|
if Isslice(r.Type) {
|
2015-02-13 14:40:36 -05:00
|
|
|
// Init slice.
|
2015-02-23 16:07:24 -05:00
|
|
|
ta := typ(TARRAY)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
ta.Type = r.Type.Type
|
2015-05-27 00:47:05 -04:00
|
|
|
ta.Bound = Mpgetfix(r.Right.Val().U.(*Mpint))
|
2015-02-23 16:07:24 -05:00
|
|
|
a := staticname(ta, 1)
|
2015-05-27 10:44:23 -04:00
|
|
|
inittemps[r] = a
|
2015-09-08 22:22:44 +02:00
|
|
|
n := *l
|
|
|
|
|
n.Xoffset = l.Xoffset + int64(Array_array)
|
|
|
|
|
gdata(&n, Nod(OADDR, a, nil), Widthptr)
|
|
|
|
|
n.Xoffset = l.Xoffset + int64(Array_nel)
|
|
|
|
|
gdata(&n, r.Right, Widthint)
|
|
|
|
|
n.Xoffset = l.Xoffset + int64(Array_cap)
|
|
|
|
|
gdata(&n, r.Right, Widthint)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Fall through to init underlying array.
|
|
|
|
|
l = a
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
|
initplan(r)
|
|
|
|
|
|
2015-05-22 01:16:52 -04:00
|
|
|
p := initplans[r]
|
2015-09-08 22:22:44 +02:00
|
|
|
n := *l
|
|
|
|
|
for i := range p.E {
|
|
|
|
|
e := &p.E[i]
|
|
|
|
|
n.Xoffset = l.Xoffset + e.Xoffset
|
|
|
|
|
n.Type = e.Expr.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
if e.Expr.Op == OLITERAL {
|
2015-09-08 22:22:44 +02:00
|
|
|
gdata(&n, e.Expr, int(n.Type.Width))
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(e.Expr)
|
2015-09-08 22:22:44 +02:00
|
|
|
a := Nod(OXXX, nil, nil)
|
|
|
|
|
*a = n
|
2015-02-13 14:40:36 -05:00
|
|
|
a.Orig = a // completely separate copy
|
2015-02-17 22:13:49 -05:00
|
|
|
if !staticassign(a, e.Expr, out) {
|
2016-02-27 14:31:33 -08:00
|
|
|
*out = append(*out, Nod(OAS, a, e.Expr))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OMAPLIT:
|
cmd/compile: captureless closures are constants
In particular, we can initialize globals with them at link time instead
of generating code for them in an init() function. Less code, less
startup cost.
But the real reason for this change is binary size. This change reduces
the binary size of hello world by ~4%.
The culprit is fmt.ssFree, a global variable which is a sync.Pool of
scratch scan states. It is initalized with a captureless closure as the
pool's New action. That action in turn references all the scanf code.
If you never call any of the fmt.Scanf* routines, ssFree is never used.
But before this change, ssFree is still referenced by fmt's init
function. That keeps ssFree and all the code it references in the
binary. With this change, ssFree is initialized at link time. As a
result, fmt.init never mentions ssFree. If you don't call fmt.Scanf*,
ssFree is unreferenced and it and the scanf code are not included.
This change is an easy fix for what is generally a much harder problem,
the unnecessary initializing of unused globals (and retention of code
that they reference). Ideally we should have separate init code for
each global and only include that code if the corresponding global is
live. (We'd need to make sure that the initializing code has no side
effects, except on the global being initialized.) That is a much harder
change.
Update #6853
Change-Id: I19d1e33992287882c83efea6ce113b7cfc504b67
Reviewed-on: https://go-review.googlesource.com/17398
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-12-03 13:20:58 -08:00
|
|
|
// TODO: Table-driven map insert.
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
cmd/compile: captureless closures are constants
In particular, we can initialize globals with them at link time instead
of generating code for them in an init() function. Less code, less
startup cost.
But the real reason for this change is binary size. This change reduces
the binary size of hello world by ~4%.
The culprit is fmt.ssFree, a global variable which is a sync.Pool of
scratch scan states. It is initalized with a captureless closure as the
pool's New action. That action in turn references all the scanf code.
If you never call any of the fmt.Scanf* routines, ssFree is never used.
But before this change, ssFree is still referenced by fmt's init
function. That keeps ssFree and all the code it references in the
binary. With this change, ssFree is initialized at link time. As a
result, fmt.init never mentions ssFree. If you don't call fmt.Scanf*,
ssFree is unreferenced and it and the scanf code are not included.
This change is an easy fix for what is generally a much harder problem,
the unnecessary initializing of unused globals (and retention of code
that they reference). Ideally we should have separate init code for
each global and only include that code if the corresponding global is
live. (We'd need to make sure that the initializing code has no side
effects, except on the global being initialized.) That is a much harder
change.
Update #6853
Change-Id: I19d1e33992287882c83efea6ce113b7cfc504b67
Reviewed-on: https://go-review.googlesource.com/17398
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-12-03 13:20:58 -08:00
|
|
|
|
|
|
|
|
case OCLOSURE:
|
2016-02-26 17:03:58 -08:00
|
|
|
if len(r.Func.Cvars.Slice()) == 0 {
|
cmd/compile: captureless closures are constants
In particular, we can initialize globals with them at link time instead
of generating code for them in an init() function. Less code, less
startup cost.
But the real reason for this change is binary size. This change reduces
the binary size of hello world by ~4%.
The culprit is fmt.ssFree, a global variable which is a sync.Pool of
scratch scan states. It is initalized with a captureless closure as the
pool's New action. That action in turn references all the scanf code.
If you never call any of the fmt.Scanf* routines, ssFree is never used.
But before this change, ssFree is still referenced by fmt's init
function. That keeps ssFree and all the code it references in the
binary. With this change, ssFree is initialized at link time. As a
result, fmt.init never mentions ssFree. If you don't call fmt.Scanf*,
ssFree is unreferenced and it and the scanf code are not included.
This change is an easy fix for what is generally a much harder problem,
the unnecessary initializing of unused globals (and retention of code
that they reference). Ideally we should have separate init code for
each global and only include that code if the corresponding global is
live. (We'd need to make sure that the initializing code has no side
effects, except on the global being initialized.) That is a much harder
change.
Update #6853
Change-Id: I19d1e33992287882c83efea6ce113b7cfc504b67
Reviewed-on: https://go-review.googlesource.com/17398
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-12-03 13:20:58 -08:00
|
|
|
// Closures with no captured variables are globals,
|
|
|
|
|
// so the assignment can be done at link time.
|
|
|
|
|
n := *l
|
|
|
|
|
gdata(&n, r.Func.Closure.Func.Nname, Widthptr)
|
|
|
|
|
return true
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
cmd/compile: captureless closures are constants
In particular, we can initialize globals with them at link time instead
of generating code for them in an init() function. Less code, less
startup cost.
But the real reason for this change is binary size. This change reduces
the binary size of hello world by ~4%.
The culprit is fmt.ssFree, a global variable which is a sync.Pool of
scratch scan states. It is initalized with a captureless closure as the
pool's New action. That action in turn references all the scanf code.
If you never call any of the fmt.Scanf* routines, ssFree is never used.
But before this change, ssFree is still referenced by fmt's init
function. That keeps ssFree and all the code it references in the
binary. With this change, ssFree is initialized at link time. As a
result, fmt.init never mentions ssFree. If you don't call fmt.Scanf*,
ssFree is unreferenced and it and the scanf code are not included.
This change is an easy fix for what is generally a much harder problem,
the unnecessary initializing of unused globals (and retention of code
that they reference). Ideally we should have separate init code for
each global and only include that code if the corresponding global is
live. (We'd need to make sure that the initializing code has no side
effects, except on the global being initialized.) That is a much harder
change.
Update #6853
Change-Id: I19d1e33992287882c83efea6ce113b7cfc504b67
Reviewed-on: https://go-review.googlesource.com/17398
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-12-03 13:20:58 -08:00
|
|
|
//dump("not static", r);
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-10-22 09:51:12 +09:00
|
|
|
// from here down is the walk analysis
|
|
|
|
|
// of composite literals.
|
|
|
|
|
// most of the work is to generate
|
|
|
|
|
// data statements for the constant
|
|
|
|
|
// part of the composite literal.
|
2015-02-13 14:40:36 -05:00
|
|
|
func staticname(t *Type, ctxt int) *Node {
|
2015-03-06 12:02:24 -08:00
|
|
|
n := newname(Lookupf("statictmp_%.4d", statuniqgen))
|
2015-02-13 14:40:36 -05:00
|
|
|
statuniqgen++
|
2015-02-17 22:13:49 -05:00
|
|
|
if ctxt == 0 {
|
2015-05-15 10:02:19 -07:00
|
|
|
n.Name.Readonly = true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
addvar(n, t, PEXTERN)
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
func isliteral(n *Node) bool {
|
2016-03-13 21:51:07 -07:00
|
|
|
// Treat nils as zeros rather than literals.
|
2016-03-13 21:57:31 -07:00
|
|
|
return n.Op == OLITERAL && n.Val().Ctype() != CTNIL
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
func simplename(n *Node) bool {
|
2016-03-13 21:57:31 -07:00
|
|
|
return n.Op == ONAME && n.Addable && n.Class&PHEAP == 0 && n.Class != PPARAMREF
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func litas(l *Node, r *Node, init *Nodes) {
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS, l, r)
|
2015-02-13 14:40:36 -05:00
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-13 17:06:10 -07:00
|
|
|
// initGenType is a bitmap indicating the types of generation that will occur for a static value.
|
|
|
|
|
type initGenType uint8
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
const (
|
2016-03-13 17:06:10 -07:00
|
|
|
initDynamic initGenType = 1 << iota // contains some dynamic values, for which init code will be generated
|
|
|
|
|
initConst // contains some constant values, which may be written into data symbols
|
2015-02-13 14:40:36 -05:00
|
|
|
)
|
|
|
|
|
|
2016-03-13 17:06:10 -07:00
|
|
|
func getdyn(n *Node, top int) initGenType {
|
2015-02-13 14:40:36 -05:00
|
|
|
switch n.Op {
|
|
|
|
|
default:
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(n) {
|
2016-03-13 17:06:10 -07:00
|
|
|
return initConst
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-13 17:06:10 -07:00
|
|
|
return initDynamic
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
2015-02-17 22:13:49 -05:00
|
|
|
if top == 0 && n.Type.Bound < 0 {
|
2016-03-13 17:06:10 -07:00
|
|
|
return initDynamic
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
|
}
|
2016-03-13 17:06:10 -07:00
|
|
|
|
|
|
|
|
var mode initGenType
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n1 := range n.List.Slice() {
|
|
|
|
|
value := n1.Right
|
2015-02-13 14:40:36 -05:00
|
|
|
mode |= getdyn(value, 0)
|
2016-03-13 17:06:10 -07:00
|
|
|
if mode == initDynamic|initConst {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mode
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func structlit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) {
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, r := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if r.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("structlit: rhs not OKEY: %v", r)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-08 22:22:44 +02:00
|
|
|
index := r.Left
|
|
|
|
|
value := r.Right
|
|
|
|
|
|
|
|
|
|
var a *Node
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
switch value.Op {
|
|
|
|
|
case OARRAYLIT:
|
|
|
|
|
if value.Type.Bound < 0 {
|
|
|
|
|
if pass == 1 && ctxt != 0 {
|
|
|
|
|
a = Nod(ODOT, var_, newname(index.Sym))
|
|
|
|
|
slicelit(ctxt, value, a, init)
|
|
|
|
|
} else if pass == 2 && ctxt == 0 {
|
|
|
|
|
a = Nod(ODOT, var_, newname(index.Sym))
|
|
|
|
|
slicelit(ctxt, value, a, init)
|
|
|
|
|
} else if pass == 3 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a = Nod(ODOT, var_, newname(index.Sym))
|
|
|
|
|
arraylit(ctxt, pass, value, a, init)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
|
a = Nod(ODOT, var_, newname(index.Sym))
|
|
|
|
|
structlit(ctxt, pass, value, a, init)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(value) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if pass == 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} else if pass == 1 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build list of var.field = expr
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(value)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(ODOT, var_, newname(index.Sym))
|
|
|
|
|
|
|
|
|
|
a = Nod(OAS, a, value)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
if pass == 1 {
|
|
|
|
|
walkexpr(&a, init) // add any assignments in r to top
|
|
|
|
|
if a.Op != OAS {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("structlit: not as")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
a.Dodata = 2
|
|
|
|
|
} else {
|
|
|
|
|
orderstmtinplace(&a)
|
|
|
|
|
walkstmt(&a)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func arraylit(ctxt int, pass int, n *Node, var_ *Node, init *Nodes) {
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, r := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if r.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("arraylit: rhs not OKEY: %v", r)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-08 22:22:44 +02:00
|
|
|
index := r.Left
|
|
|
|
|
value := r.Right
|
|
|
|
|
|
|
|
|
|
var a *Node
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
switch value.Op {
|
|
|
|
|
case OARRAYLIT:
|
|
|
|
|
if value.Type.Bound < 0 {
|
|
|
|
|
if pass == 1 && ctxt != 0 {
|
|
|
|
|
a = Nod(OINDEX, var_, index)
|
|
|
|
|
slicelit(ctxt, value, a, init)
|
|
|
|
|
} else if pass == 2 && ctxt == 0 {
|
|
|
|
|
a = Nod(OINDEX, var_, index)
|
|
|
|
|
slicelit(ctxt, value, a, init)
|
|
|
|
|
} else if pass == 3 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a = Nod(OINDEX, var_, index)
|
|
|
|
|
arraylit(ctxt, pass, value, a, init)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
|
a = Nod(OINDEX, var_, index)
|
|
|
|
|
structlit(ctxt, pass, value, a, init)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(index) && isliteral(value) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if pass == 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} else if pass == 1 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build list of var[index] = value
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(value)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(OINDEX, var_, index)
|
|
|
|
|
|
|
|
|
|
a = Nod(OAS, a, value)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
if pass == 1 {
|
|
|
|
|
walkexpr(&a, init)
|
|
|
|
|
if a.Op != OAS {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("arraylit: not as")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
a.Dodata = 2
|
|
|
|
|
} else {
|
|
|
|
|
orderstmtinplace(&a)
|
|
|
|
|
walkstmt(&a)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func slicelit(ctxt int, n *Node, var_ *Node, init *Nodes) {
|
2015-02-13 14:40:36 -05:00
|
|
|
// make an array type
|
2016-03-09 15:32:57 -08:00
|
|
|
t := n.Type.Copy()
|
2015-05-27 00:47:05 -04:00
|
|
|
t.Bound = Mpgetfix(n.Right.Val().U.(*Mpint))
|
2015-02-13 14:40:36 -05:00
|
|
|
t.Width = 0
|
|
|
|
|
t.Sym = nil
|
|
|
|
|
t.Haspointers = 0
|
|
|
|
|
dowidth(t)
|
|
|
|
|
|
|
|
|
|
if ctxt != 0 {
|
|
|
|
|
// put everything into static array
|
2015-02-23 16:07:24 -05:00
|
|
|
vstat := staticname(t, ctxt)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
arraylit(ctxt, 1, n, vstat, init)
|
|
|
|
|
arraylit(ctxt, 2, n, vstat, init)
|
|
|
|
|
|
|
|
|
|
// copy static to slice
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OSLICE, vstat, Nod(OKEY, nil, nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
a = Nod(OAS, var_, a)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
a.Dodata = 2
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// recipe for var = []t{...}
|
|
|
|
|
// 1. make a static array
|
|
|
|
|
// var vstat [...]t
|
|
|
|
|
// 2. assign (data statements) the constant part
|
|
|
|
|
// vstat = constpart{}
|
|
|
|
|
// 3. make an auto pointer to array and allocate heap to it
|
|
|
|
|
// var vauto *[...]t = new([...]t)
|
|
|
|
|
// 4. copy the static array to the auto array
|
|
|
|
|
// *vauto = vstat
|
|
|
|
|
// 5. assign slice of allocated heap to var
|
|
|
|
|
// var = [0:]*auto
|
|
|
|
|
// 6. for each dynamic part assign to the slice
|
|
|
|
|
// var[i] = dynamic part
|
|
|
|
|
//
|
|
|
|
|
// an optimization is done if there is no constant part
|
|
|
|
|
// 3. var vauto *[...]t = new([...]t)
|
|
|
|
|
// 5. var = [0:]*auto
|
|
|
|
|
// 6. var[i] = dynamic part
|
|
|
|
|
|
|
|
|
|
// if the literal contains constants,
|
|
|
|
|
// make static initialized array (1),(2)
|
2015-03-02 14:22:05 -05:00
|
|
|
var vstat *Node
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
mode := getdyn(n, 1)
|
2016-03-13 17:06:10 -07:00
|
|
|
if mode&initConst != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
vstat = staticname(t, ctxt)
|
|
|
|
|
arraylit(ctxt, 1, n, vstat, init)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make new auto *array (3 declare)
|
2015-02-23 16:07:24 -05:00
|
|
|
vauto := temp(Ptrto(t))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// set auto to point at new temp or heap (3 assign)
|
2015-02-23 16:07:24 -05:00
|
|
|
var a *Node
|
2015-05-26 23:05:35 -04:00
|
|
|
if x := prealloc[n]; x != nil {
|
2015-10-22 09:51:12 +09:00
|
|
|
// temp allocated during order.go for dddarg
|
2015-05-26 23:05:35 -04:00
|
|
|
x.Type = t
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
if vstat == nil {
|
2015-05-26 23:05:35 -04:00
|
|
|
a = Nod(OAS, x, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
typecheck(&a, Etop)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a) // zero new temp
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-26 23:05:35 -04:00
|
|
|
a = Nod(OADDR, x, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
} else if n.Esc == EscNone {
|
|
|
|
|
a = temp(t)
|
|
|
|
|
if vstat == nil {
|
|
|
|
|
a = Nod(OAS, temp(t), nil)
|
|
|
|
|
typecheck(&a, Etop)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a) // zero new temp
|
2015-02-13 14:40:36 -05:00
|
|
|
a = a.Left
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a = Nod(OADDR, a, nil)
|
|
|
|
|
} else {
|
|
|
|
|
a = Nod(ONEW, nil, nil)
|
2016-03-10 10:13:42 -08:00
|
|
|
a.List.Set1(typenod(t))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a = Nod(OAS, vauto, a)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
if vstat != nil {
|
|
|
|
|
// copy static to heap (4)
|
|
|
|
|
a = Nod(OIND, vauto, nil)
|
|
|
|
|
|
|
|
|
|
a = Nod(OAS, a, vstat)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make slice out of heap (5)
|
|
|
|
|
a = Nod(OAS, var_, Nod(OSLICE, vauto, Nod(OKEY, nil, nil)))
|
|
|
|
|
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
orderstmtinplace(&a)
|
|
|
|
|
walkstmt(&a)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
// put dynamics into slice (6)
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, r := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if r.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("slicelit: rhs not OKEY: %v", r)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-08 22:22:44 +02:00
|
|
|
index := r.Left
|
|
|
|
|
value := r.Right
|
|
|
|
|
a := Nod(OINDEX, var_, index)
|
2015-02-17 22:13:49 -05:00
|
|
|
a.Bounded = true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// TODO need to check bounds?
|
|
|
|
|
|
|
|
|
|
switch value.Op {
|
|
|
|
|
case OARRAYLIT:
|
|
|
|
|
if value.Type.Bound < 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
arraylit(ctxt, 2, value, a, init)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
|
structlit(ctxt, 2, value, a, init)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(index) && isliteral(value) {
|
2015-02-13 14:40:36 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build list of var[c] = expr
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(value)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(OAS, a, value)
|
|
|
|
|
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
orderstmtinplace(&a)
|
|
|
|
|
walkstmt(&a)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func maplit(ctxt int, n *Node, var_ *Node, init *Nodes) {
|
2015-02-13 14:40:36 -05:00
|
|
|
ctxt = 0
|
|
|
|
|
|
|
|
|
|
// make the map var
|
2015-02-23 16:07:24 -05:00
|
|
|
nerr := nerrors
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OMAKE, nil, nil)
|
2016-03-10 10:13:42 -08:00
|
|
|
a.List.Set1(typenod(n.Type))
|
2015-02-13 14:40:36 -05:00
|
|
|
litas(var_, a, init)
|
|
|
|
|
|
|
|
|
|
// count the initializers
|
2016-02-28 14:39:38 -08:00
|
|
|
b := 0
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, r := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if r.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("maplit: rhs not OKEY: %v", r)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-08 22:22:44 +02:00
|
|
|
index := r.Left
|
|
|
|
|
value := r.Right
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(index) && isliteral(value) {
|
2015-02-13 14:40:36 -05:00
|
|
|
b++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b != 0 {
|
|
|
|
|
// build type [count]struct { a Tindex, b Tvalue }
|
2015-02-23 16:07:24 -05:00
|
|
|
t := n.Type
|
|
|
|
|
tk := t.Down
|
|
|
|
|
tv := t.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-13 23:02:38 -07:00
|
|
|
syma := Lookup("a")
|
2015-02-23 16:07:24 -05:00
|
|
|
symb := Lookup("b")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-14 01:20:49 -07:00
|
|
|
var fields [2]*Field
|
|
|
|
|
fields[0] = newField()
|
2016-03-13 23:02:38 -07:00
|
|
|
fields[0].Type = tk
|
|
|
|
|
fields[0].Sym = syma
|
2016-03-14 01:20:49 -07:00
|
|
|
fields[1] = newField()
|
2016-03-13 23:02:38 -07:00
|
|
|
fields[1].Type = tv
|
|
|
|
|
fields[1].Sym = symb
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-28 14:39:38 -08:00
|
|
|
tstruct := typ(TSTRUCT)
|
2016-03-13 23:02:38 -07:00
|
|
|
tstruct.SetFields(fields[:])
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-28 14:39:38 -08:00
|
|
|
tarr := typ(TARRAY)
|
|
|
|
|
tarr.Bound = int64(b)
|
|
|
|
|
tarr.Type = tstruct
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-28 14:39:38 -08:00
|
|
|
// TODO(josharian): suppress alg generation for these types?
|
|
|
|
|
dowidth(tarr)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// make and initialize static array
|
2016-02-28 14:39:38 -08:00
|
|
|
vstat := staticname(tarr, ctxt)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
b := int64(0)
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, r := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if r.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("maplit: rhs not OKEY: %v", r)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-08 22:22:44 +02:00
|
|
|
index := r.Left
|
|
|
|
|
value := r.Right
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(index) && isliteral(value) {
|
2015-02-13 14:40:36 -05:00
|
|
|
// build vstat[b].a = key;
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(index)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nodintconst(b)
|
|
|
|
|
|
|
|
|
|
a = Nod(OINDEX, vstat, a)
|
|
|
|
|
a = Nod(ODOT, a, newname(syma))
|
|
|
|
|
a = Nod(OAS, a, index)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
|
|
|
|
a.Dodata = 2
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// build vstat[b].b = value;
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(value)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nodintconst(b)
|
|
|
|
|
|
|
|
|
|
a = Nod(OINDEX, vstat, a)
|
|
|
|
|
a = Nod(ODOT, a, newname(symb))
|
|
|
|
|
a = Nod(OAS, a, value)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
|
|
|
|
a.Dodata = 2
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
b++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// loop adding structure elements to map
|
|
|
|
|
// for i = 0; i < len(vstat); i++ {
|
|
|
|
|
// map[vstat[i].a] = vstat[i].b
|
|
|
|
|
// }
|
2015-09-08 22:22:44 +02:00
|
|
|
index := temp(Types[TINT])
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
a = Nod(OINDEX, vstat, index)
|
2015-02-17 22:13:49 -05:00
|
|
|
a.Bounded = true
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(ODOT, a, newname(symb))
|
|
|
|
|
|
2015-09-08 22:22:44 +02:00
|
|
|
r := Nod(OINDEX, vstat, index)
|
2015-02-17 22:13:49 -05:00
|
|
|
r.Bounded = true
|
2015-02-13 14:40:36 -05:00
|
|
|
r = Nod(ODOT, r, newname(syma))
|
|
|
|
|
r = Nod(OINDEX, var_, r)
|
|
|
|
|
|
|
|
|
|
r = Nod(OAS, r, a)
|
|
|
|
|
|
|
|
|
|
a = Nod(OFOR, nil, nil)
|
2016-03-10 10:13:42 -08:00
|
|
|
a.Nbody.Set1(r)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-10 10:13:42 -08:00
|
|
|
a.Ninit.Set1(Nod(OAS, index, Nodintconst(0)))
|
2016-02-28 14:39:38 -08:00
|
|
|
a.Left = Nod(OLT, index, Nodintconst(tarr.Bound))
|
2015-05-22 01:16:52 -04:00
|
|
|
a.Right = Nod(OAS, index, Nod(OADD, index, Nodintconst(1)))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkstmt(&a)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// put in dynamic entries one-at-a-time
|
2016-02-28 14:39:38 -08:00
|
|
|
var key, val *Node
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, r := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if r.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("maplit: rhs not OKEY: %v", r)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-09-08 22:22:44 +02:00
|
|
|
index := r.Left
|
|
|
|
|
value := r.Right
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if isliteral(index) && isliteral(value) {
|
2015-02-13 14:40:36 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build list of var[c] = expr.
|
|
|
|
|
// use temporary so that mapassign1 can have addressable key, val.
|
|
|
|
|
if key == nil {
|
2016-03-10 05:22:14 -08:00
|
|
|
key = temp(var_.Type.Key())
|
2015-02-13 14:40:36 -05:00
|
|
|
val = temp(var_.Type.Type)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(r.Left)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(OAS, key, r.Left)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkstmt(&a)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(r.Right)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(OAS, val, r.Right)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkstmt(&a)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-06-29 16:30:19 -04:00
|
|
|
setlineno(val)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(OAS, Nod(OINDEX, var_, key), val)
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkstmt(&a)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
if nerr != nerrors {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if key != nil {
|
|
|
|
|
a = Nod(OVARKILL, key, nil)
|
|
|
|
|
typecheck(&a, Etop)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
a = Nod(OVARKILL, val, nil)
|
|
|
|
|
typecheck(&a, Etop)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func anylit(ctxt int, n *Node, var_ *Node, init *Nodes) {
|
2015-02-23 16:07:24 -05:00
|
|
|
t := n.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
switch n.Op {
|
|
|
|
|
default:
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("anylit: not lit")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OPTRLIT:
|
2015-03-01 07:54:01 +00:00
|
|
|
if !Isptr[t.Etype] {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("anylit: not ptr")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
var r *Node
|
2015-02-13 14:40:36 -05:00
|
|
|
if n.Right != nil {
|
|
|
|
|
r = Nod(OADDR, n.Right, nil)
|
|
|
|
|
typecheck(&r, Erv)
|
|
|
|
|
} else {
|
|
|
|
|
r = Nod(ONEW, nil, nil)
|
|
|
|
|
r.Typecheck = 1
|
|
|
|
|
r.Type = t
|
|
|
|
|
r.Esc = n.Esc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
walkexpr(&r, init)
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS, var_, r)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
typecheck(&a, Etop)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
var_ = Nod(OIND, var_, nil)
|
|
|
|
|
typecheck(&var_, Erv|Easgn)
|
|
|
|
|
anylit(ctxt, n.Left, var_, init)
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
|
|
|
|
if t.Etype != TSTRUCT {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("anylit: not struct")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
if simplename(var_) && n.List.Len() > 4 {
|
2015-02-13 14:40:36 -05:00
|
|
|
if ctxt == 0 {
|
|
|
|
|
// lay out static data
|
2015-02-23 16:07:24 -05:00
|
|
|
vstat := staticname(t, ctxt)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
structlit(ctxt, 1, n, vstat, init)
|
|
|
|
|
|
|
|
|
|
// copy static to var
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS, var_, vstat)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// add expressions to automatic
|
|
|
|
|
structlit(ctxt, 2, n, var_, init)
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
structlit(ctxt, 1, n, var_, init)
|
|
|
|
|
structlit(ctxt, 2, n, var_, init)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initialize of not completely specified
|
2016-03-08 15:10:26 -08:00
|
|
|
if simplename(var_) || n.List.Len() < structcount(t) {
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS, var_, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
structlit(ctxt, 3, n, var_, init)
|
|
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
|
|
|
|
if t.Etype != TARRAY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("anylit: not array")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if t.Bound < 0 {
|
|
|
|
|
slicelit(ctxt, n, var_, init)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
if simplename(var_) && n.List.Len() > 4 {
|
2015-02-13 14:40:36 -05:00
|
|
|
if ctxt == 0 {
|
|
|
|
|
// lay out static data
|
2015-02-23 16:07:24 -05:00
|
|
|
vstat := staticname(t, ctxt)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
arraylit(1, 1, n, vstat, init)
|
|
|
|
|
|
|
|
|
|
// copy static to automatic
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS, var_, vstat)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// add expressions to automatic
|
|
|
|
|
arraylit(ctxt, 2, n, var_, init)
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arraylit(ctxt, 1, n, var_, init)
|
|
|
|
|
arraylit(ctxt, 2, n, var_, init)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initialize of not completely specified
|
2016-03-08 15:10:26 -08:00
|
|
|
if simplename(var_) || int64(n.List.Len()) < t.Bound {
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS, var_, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
typecheck(&a, Etop)
|
|
|
|
|
walkexpr(&a, init)
|
2016-03-07 22:54:46 -08:00
|
|
|
init.Append(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arraylit(ctxt, 3, n, var_, init)
|
|
|
|
|
|
|
|
|
|
case OMAPLIT:
|
|
|
|
|
if t.Etype != TMAP {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("anylit: not map")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
maplit(ctxt, n, var_, init)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:54:46 -08:00
|
|
|
func oaslit(n *Node, init *Nodes) bool {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n.Left == nil || n.Right == nil {
|
2015-03-02 12:35:15 -05:00
|
|
|
// not a special composit literal assignment
|
|
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if n.Left.Type == nil || n.Right.Type == nil {
|
2015-03-02 12:35:15 -05:00
|
|
|
// not a special composit literal assignment
|
|
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-17 22:13:49 -05:00
|
|
|
if !simplename(n.Left) {
|
2015-03-02 12:35:15 -05:00
|
|
|
// not a special composit literal assignment
|
|
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if !Eqtype(n.Left.Type, n.Right.Type) {
|
2015-03-02 12:35:15 -05:00
|
|
|
// not a special composit literal assignment
|
|
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// context is init() function.
|
|
|
|
|
// implies generated data executed
|
|
|
|
|
// exactly once and not subject to races.
|
2015-03-02 12:35:15 -05:00
|
|
|
ctxt := 0
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// if(n->dodata == 1)
|
|
|
|
|
// ctxt = 1;
|
|
|
|
|
|
|
|
|
|
switch n.Right.Op {
|
|
|
|
|
default:
|
2015-03-02 12:35:15 -05:00
|
|
|
// not a special composit literal assignment
|
|
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-04-01 09:38:44 -07:00
|
|
|
case OSTRUCTLIT, OARRAYLIT, OMAPLIT:
|
2015-02-17 22:13:49 -05:00
|
|
|
if vmatch1(n.Left, n.Right) {
|
2015-03-02 12:35:15 -05:00
|
|
|
// not a special composit literal assignment
|
|
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
anylit(ctxt, n.Right, n.Left, init)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.Op = OEMPTY
|
2015-05-26 21:30:20 -04:00
|
|
|
n.Right = nil
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getlit(lit *Node) int {
|
2015-02-17 22:13:49 -05:00
|
|
|
if Smallintconst(lit) {
|
2015-05-27 00:47:05 -04:00
|
|
|
return int(Mpgetfix(lit.Val().U.(*Mpint)))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 10:56:39 -07:00
|
|
|
// stataddr sets nam to the static address of n and reports whether it succeeeded.
|
2015-02-17 22:13:49 -05:00
|
|
|
func stataddr(nam *Node, n *Node) bool {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n == nil {
|
2015-03-02 12:35:15 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch n.Op {
|
|
|
|
|
case ONAME:
|
|
|
|
|
*nam = *n
|
2015-04-02 19:58:37 -07:00
|
|
|
return n.Addable
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case ODOT:
|
2015-02-17 22:13:49 -05:00
|
|
|
if !stataddr(nam, n.Left) {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
nam.Xoffset += n.Xoffset
|
|
|
|
|
nam.Type = n.Type
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OINDEX:
|
|
|
|
|
if n.Left.Type.Bound < 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
2015-02-17 22:13:49 -05:00
|
|
|
if !stataddr(nam, n.Left) {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
l := getlit(n.Right)
|
2015-02-13 14:40:36 -05:00
|
|
|
if l < 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for overflow.
|
|
|
|
|
if n.Type.Width != 0 && Thearch.MAXWIDTH/n.Type.Width <= int64(l) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
nam.Xoffset += int64(l) * n.Type.Width
|
|
|
|
|
nam.Type = n.Type
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initplan(n *Node) {
|
2015-05-22 01:16:52 -04:00
|
|
|
if initplans[n] != nil {
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
p := new(InitPlan)
|
2015-05-22 01:16:52 -04:00
|
|
|
initplans[n] = p
|
2015-02-13 14:40:36 -05:00
|
|
|
switch n.Op {
|
|
|
|
|
default:
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("initplan")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, a := range n.List.Slice() {
|
2015-02-17 22:13:49 -05:00
|
|
|
if a.Op != OKEY || !Smallintconst(a.Left) {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("initplan arraylit")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-13 17:48:17 -07:00
|
|
|
addvalue(p, n.Type.Type.Width*Mpgetfix(a.Left.Val().U.(*Mpint)), a.Right)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OSTRUCTLIT:
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, a := range n.List.Slice() {
|
2016-03-14 00:24:43 -07:00
|
|
|
if a.Op != OKEY || a.Left.Type != structkey {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("initplan structlit")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-14 00:24:43 -07:00
|
|
|
addvalue(p, a.Left.Xoffset, a.Right)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OMAPLIT:
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, a := range n.List.Slice() {
|
2015-02-13 14:40:36 -05:00
|
|
|
if a.Op != OKEY {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("initplan maplit")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-13 17:48:17 -07:00
|
|
|
addvalue(p, -1, a.Right)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-13 17:48:17 -07:00
|
|
|
func addvalue(p *InitPlan, xoffset int64, n *Node) {
|
2015-02-13 14:40:36 -05:00
|
|
|
// special case: zero can be dropped entirely
|
2015-02-17 22:13:49 -05:00
|
|
|
if iszero(n) {
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// special case: inline struct and array (not slice) literals
|
2015-02-17 22:13:49 -05:00
|
|
|
if isvaluelit(n) {
|
2015-02-13 14:40:36 -05:00
|
|
|
initplan(n)
|
2015-05-22 01:16:52 -04:00
|
|
|
q := initplans[n]
|
2015-09-08 22:22:44 +02:00
|
|
|
for _, qe := range q.E {
|
2016-03-13 17:48:17 -07:00
|
|
|
// qe is a copy; we are not modifying entries in q.E
|
|
|
|
|
qe.Xoffset += xoffset
|
|
|
|
|
p.E = append(p.E, qe)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add to plan
|
2016-03-13 17:48:17 -07:00
|
|
|
p.E = append(p.E, InitEntry{Xoffset: xoffset, Expr: n})
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
func iszero(n *Node) bool {
|
2015-02-13 14:40:36 -05:00
|
|
|
switch n.Op {
|
|
|
|
|
case OLITERAL:
|
2015-05-27 00:47:05 -04:00
|
|
|
switch n.Val().Ctype() {
|
2015-02-13 14:40:36 -05:00
|
|
|
default:
|
|
|
|
|
Dump("unexpected literal", n)
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("iszero")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case CTNIL:
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case CTSTR:
|
2015-05-27 00:47:05 -04:00
|
|
|
return n.Val().U.(string) == ""
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case CTBOOL:
|
2015-05-27 00:47:05 -04:00
|
|
|
return !n.Val().U.(bool)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-04-01 09:38:44 -07:00
|
|
|
case CTINT, CTRUNE:
|
2015-05-27 00:47:05 -04:00
|
|
|
return mpcmpfixc(n.Val().U.(*Mpint), 0) == 0
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case CTFLT:
|
2015-05-27 00:47:05 -04:00
|
|
|
return mpcmpfltc(n.Val().U.(*Mpflt), 0) == 0
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case CTCPLX:
|
2015-05-27 00:47:05 -04:00
|
|
|
return mpcmpfltc(&n.Val().U.(*Mpcplx).Real, 0) == 0 && mpcmpfltc(&n.Val().U.(*Mpcplx).Imag, 0) == 0
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OARRAYLIT:
|
2015-02-17 22:13:49 -05:00
|
|
|
if Isslice(n.Type) {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
// fall through
|
|
|
|
|
case OSTRUCTLIT:
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n1 := range n.List.Slice() {
|
|
|
|
|
if !iszero(n1.Right) {
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
func isvaluelit(n *Node) bool {
|
|
|
|
|
return (n.Op == OARRAYLIT && Isfixedarray(n.Type)) || n.Op == OSTRUCTLIT
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-28 10:56:39 -07:00
|
|
|
// 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 {
|
2015-02-13 14:40:36 -05:00
|
|
|
var nr *Node
|
|
|
|
|
var nl *Node
|
|
|
|
|
var nam Node
|
|
|
|
|
|
|
|
|
|
if n.Dodata == 0 {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nr = n.Right
|
|
|
|
|
nl = n.Left
|
|
|
|
|
if nr == nil {
|
2015-02-23 16:07:24 -05:00
|
|
|
var nam Node
|
2015-02-17 22:13:49 -05:00
|
|
|
if !stataddr(&nam, nl) {
|
2015-02-13 14:40:36 -05:00
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
if nam.Class != PEXTERN {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if nr.Type == nil || !Eqtype(nl.Type, nr.Type) {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
if !stataddr(&nam, nl) {
|
2015-02-13 14:40:36 -05:00
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if nam.Class != PEXTERN {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch nr.Op {
|
|
|
|
|
default:
|
|
|
|
|
goto no
|
|
|
|
|
|
|
|
|
|
case OCONVNOP:
|
|
|
|
|
nr = nr.Left
|
|
|
|
|
if nr == nil || nr.Op != OSLICEARR {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
// fall through
|
|
|
|
|
case OSLICEARR:
|
|
|
|
|
if nr.Right.Op == OKEY && nr.Right.Left == nil && nr.Right.Right == nil {
|
|
|
|
|
nr = nr.Left
|
2015-03-02 12:35:15 -05:00
|
|
|
nl := nr
|
|
|
|
|
if nr == nil || nr.Op != OADDR {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
nr = nr.Left
|
|
|
|
|
if nr == nil || nr.Op != ONAME {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// nr is the array being converted to a slice
|
|
|
|
|
if nr.Type == nil || nr.Type.Etype != TARRAY || nr.Type.Bound < 0 {
|
|
|
|
|
goto no
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 10:56:39 -07:00
|
|
|
if !reportOnly {
|
|
|
|
|
nam.Xoffset += int64(Array_array)
|
|
|
|
|
gdata(&nam, nl, int(Types[Tptr].Width))
|
2015-03-02 12:35:15 -05:00
|
|
|
|
2015-07-28 10:56:39 -07:00
|
|
|
nam.Xoffset += int64(Array_nel) - int64(Array_array)
|
|
|
|
|
var nod1 Node
|
|
|
|
|
Nodconst(&nod1, Types[TINT], nr.Type.Bound)
|
|
|
|
|
gdata(&nam, &nod1, Widthint)
|
2015-03-02 12:35:15 -05:00
|
|
|
|
2015-07-28 10:56:39 -07:00
|
|
|
nam.Xoffset += int64(Array_cap) - int64(Array_nel)
|
|
|
|
|
gdata(&nam, &nod1, Widthint)
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
|
|
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goto no
|
|
|
|
|
|
|
|
|
|
case OLITERAL:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch nr.Type.Etype {
|
|
|
|
|
default:
|
|
|
|
|
goto no
|
|
|
|
|
|
2016-03-13 21:57:31 -07:00
|
|
|
case TBOOL, TINT8, TUINT8, TINT16, TUINT16,
|
|
|
|
|
TINT32, TUINT32, TINT64, TUINT64,
|
|
|
|
|
TINT, TUINT, TUINTPTR,
|
|
|
|
|
TPTR32, TPTR64,
|
|
|
|
|
TFLOAT32, TFLOAT64:
|
2015-07-28 10:56:39 -07:00
|
|
|
if !reportOnly {
|
|
|
|
|
gdata(&nam, nr, int(nr.Type.Width))
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-04-01 09:38:44 -07:00
|
|
|
case TCOMPLEX64, TCOMPLEX128:
|
2015-07-28 10:56:39 -07:00
|
|
|
if !reportOnly {
|
|
|
|
|
gdatacomplex(&nam, nr.Val().U.(*Mpcplx))
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case TSTRING:
|
2015-07-28 10:56:39 -07:00
|
|
|
if !reportOnly {
|
|
|
|
|
gdatastring(&nam, nr.Val().U.(string))
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
no:
|
|
|
|
|
if n.Dodata == 2 {
|
|
|
|
|
Dump("\ngen_as_init", n)
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("gen_as_init couldnt make data statement")
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return false
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|