Commit 335da67e authored by Russ Cox's avatar Russ Cox

gc: make static initialization more static

Does as much as possible in data layout instead
of during the init function.

Handles var x = y; var y = z as a special case too,
because it is so prevalent in package unicode
(var Greek = _Greek; var _Greek = []...).

Introduces InitPlan description of initialized data
so that it can be traversed multiple times (for example,
in the copy handler).

Cuts package unicode's init function size by 8x.
All that remains there is map initialization, which
is on the chopping block too.

Fixes sinit.go test case.

Aggregate DATA instructions at end of object file.

Checkpoint.  More to come.

R=ken2
CC=golang-dev
https://golang.org/cl/4969051
parent 5f40c5b3
......@@ -268,54 +268,6 @@ dumpfuncs(void)
}
}
/* deferred DATA output */
static Prog *strdat;
static Prog *estrdat;
static int gflag;
static Prog *savepc;
void
data(void)
{
gflag = debug['g'];
debug['g'] = 0;
if(estrdat == nil) {
strdat = mal(sizeof(*pc));
clearp(strdat);
estrdat = strdat;
}
if(savepc)
fatal("data phase error");
savepc = pc;
pc = estrdat;
}
void
text(void)
{
if(!savepc)
fatal("text phase error");
debug['g'] = gflag;
estrdat = pc;
pc = savepc;
savepc = nil;
}
void
dumpdata(void)
{
Prog *p;
if(estrdat == nil)
return;
*pc = *strdat;
if(gflag)
for(p=pc; p!=estrdat; p=p->link)
print("%P\n", p);
pc = estrdat;
}
int
dsname(Sym *sym, int off, char *t, int n)
{
......@@ -381,6 +333,17 @@ gdata(Node *nam, Node *nr, int wid)
Prog *p;
vlong v;
if(nr->op == OLITERAL) {
switch(nr->val.ctype) {
case CTCPLX:
gdatacomplex(nam, nr->val.u.cval);
return;
case CTSTR:
gdatastring(nam, nr->val.u.sval);
return;
}
}
if(wid == 8 && is64(nr->type)) {
v = mpgetfix(nr->val.u.xval);
p = gins(ADATA, nam, nodintconst(v));
......
......@@ -52,6 +52,10 @@ clearp(Prog *p)
pcloc++;
}
static int ddumped;
static Prog *dfirst;
static Prog *dpc;
/*
* generate and return proc with p->as = as,
* linked into program. pc is next instruction.
......@@ -61,10 +65,22 @@ prog(int as)
{
Prog *p;
p = pc;
pc = mal(sizeof(*pc));
clearp(pc);
if(as == ADATA || as == AGLOBL) {
if(ddumped)
fatal("already dumped data");
if(dpc == nil) {
dpc = mal(sizeof(*dpc));
dfirst = dpc;
}
p = dpc;
dpc = mal(sizeof(*dpc));
p->link = dpc;
} else {
p = pc;
pc = mal(sizeof(*pc));
clearp(pc);
p->link = pc;
}
if(lineno == 0) {
if(debug['K'])
......@@ -73,10 +89,21 @@ prog(int as)
p->as = as;
p->lineno = lineno;
p->link = pc;
return p;
}
void
dumpdata(void)
{
ddumped = 1;
if(dfirst == nil)
return;
newplist();
*pc = *dfirst;
pc = dpc;
clearp(pc);
}
/*
* generate a branch.
* t is ignored.
......
......@@ -280,54 +280,6 @@ dumpfuncs(void)
}
}
/* deferred DATA output */
static Prog *strdat;
static Prog *estrdat;
static int gflag;
static Prog *savepc;
void
data(void)
{
gflag = debug['g'];
debug['g'] = 0;
if(estrdat == nil) {
strdat = mal(sizeof(*pc));
clearp(strdat);
estrdat = strdat;
}
if(savepc)
fatal("data phase error");
savepc = pc;
pc = estrdat;
}
void
text(void)
{
if(!savepc)
fatal("text phase error");
debug['g'] = gflag;
estrdat = pc;
pc = savepc;
savepc = nil;
}
void
dumpdata(void)
{
Prog *p;
if(estrdat == nil)
return;
*pc = *strdat;
if(gflag)
for(p=pc; p!=estrdat; p=p->link)
print("%P\n", p);
pc = estrdat;
}
int
dsname(Sym *s, int off, char *t, int n)
{
......@@ -383,6 +335,16 @@ gdata(Node *nam, Node *nr, int wid)
{
Prog *p;
if(nr->op == OLITERAL) {
switch(nr->val.ctype) {
case CTCPLX:
gdatacomplex(nam, nr->val.u.cval);
return;
case CTSTR:
gdatastring(nam, nr->val.u.sval);
return;
}
}
p = gins(ADATA, nam, nr);
p->from.scale = wid;
}
......
......@@ -48,6 +48,10 @@ clearp(Prog *p)
pcloc++;
}
static int ddumped;
static Prog *dfirst;
static Prog *dpc;
/*
* generate and return proc with p->as = as,
* linked into program. pc is next instruction.
......@@ -57,10 +61,22 @@ prog(int as)
{
Prog *p;
p = pc;
pc = mal(sizeof(*pc));
clearp(pc);
if(as == ADATA || as == AGLOBL) {
if(ddumped)
fatal("already dumped data");
if(dpc == nil) {
dpc = mal(sizeof(*dpc));
dfirst = dpc;
}
p = dpc;
dpc = mal(sizeof(*dpc));
p->link = dpc;
} else {
p = pc;
pc = mal(sizeof(*pc));
clearp(pc);
p->link = pc;
}
if(lineno == 0) {
if(debug['K'])
......@@ -69,10 +85,21 @@ prog(int as)
p->as = as;
p->lineno = lineno;
p->link = pc;
return p;
}
void
dumpdata(void)
{
ddumped = 1;
if(dfirst == nil)
return;
newplist();
*pc = *dfirst;
pc = dpc;
clearp(pc);
}
/*
* generate a branch.
* t is ignored.
......
......@@ -161,12 +161,6 @@ void complexmove(Node*, Node*);
void complexgen(Node*, Node*);
void complexbool(int, Node*, Node*, int, Prog*);
/*
* gobj.c
*/
void data(void);
void text(void);
/*
* list.c
*/
......
......@@ -278,54 +278,6 @@ dumpfuncs(void)
}
}
/* deferred DATA output */
static Prog *strdat;
static Prog *estrdat;
static int gflag;
static Prog *savepc;
void
data(void)
{
gflag = debug['g'];
debug['g'] = 0;
if(estrdat == nil) {
strdat = mal(sizeof(*pc));
clearp(strdat);
estrdat = strdat;
}
if(savepc)
fatal("data phase error");
savepc = pc;
pc = estrdat;
}
void
text(void)
{
if(!savepc)
fatal("text phase error");
debug['g'] = gflag;
estrdat = pc;
pc = savepc;
savepc = nil;
}
void
dumpdata(void)
{
Prog *p;
if(estrdat == nil)
return;
*pc = *strdat;
if(gflag)
for(p=pc; p!=estrdat; p=p->link)
print("%P\n", p);
pc = estrdat;
}
int
dsname(Sym *s, int off, char *t, int n)
{
......@@ -382,6 +334,17 @@ gdata(Node *nam, Node *nr, int wid)
Prog *p;
vlong v;
if(nr->op == OLITERAL) {
switch(nr->val.ctype) {
case CTCPLX:
gdatacomplex(nam, nr->val.u.cval);
return;
case CTSTR:
gdatastring(nam, nr->val.u.sval);
return;
}
}
if(wid == 8 && is64(nr->type)) {
v = mpgetfix(nr->val.u.xval);
p = gins(ADATA, nam, nodintconst(v));
......
......@@ -50,6 +50,10 @@ clearp(Prog *p)
pcloc++;
}
static int ddumped;
static Prog *dfirst;
static Prog *dpc;
/*
* generate and return proc with p->as = as,
* linked into program. pc is next instruction.
......@@ -59,10 +63,22 @@ prog(int as)
{
Prog *p;
p = pc;
pc = mal(sizeof(*pc));
clearp(pc);
if(as == ADATA || as == AGLOBL) {
if(ddumped)
fatal("already dumped data");
if(dpc == nil) {
dpc = mal(sizeof(*dpc));
dfirst = dpc;
}
p = dpc;
dpc = mal(sizeof(*dpc));
p->link = dpc;
} else {
p = pc;
pc = mal(sizeof(*pc));
clearp(pc);
p->link = pc;
}
if(lineno == 0) {
if(debug['K'])
......@@ -71,10 +87,21 @@ prog(int as)
p->as = as;
p->lineno = lineno;
p->link = pc;
return p;
}
void
dumpdata(void)
{
ddumped = 1;
if(dfirst == nil)
return;
newplist();
*pc = *dfirst;
pc = dpc;
clearp(pc);
}
/*
* generate a branch.
* t is ignored.
......
......@@ -1101,7 +1101,7 @@ cmpslit(Node *l, Node *r)
int
smallintconst(Node *n)
{
if(n->op == OLITERAL && n->type != T)
if(n->op == OLITERAL && n->val.ctype == CTINT && n->type != T)
switch(simtype[n->type->etype]) {
case TINT8:
case TUINT8:
......@@ -1112,6 +1112,7 @@ smallintconst(Node *n)
case TBOOL:
case TPTR32:
return 1;
case TIDEAL:
case TINT64:
case TUINT64:
if(mpcmpfixfix(n->val.u.xval, minintval[TINT32]) < 0
......
......@@ -200,6 +200,27 @@ struct Type
};
#define T ((Type*)0)
typedef struct InitEntry InitEntry;
typedef struct InitPlan InitPlan;
struct InitEntry
{
vlong xoffset; // struct, array only
Node *key; // map only
Node *expr;
};
struct InitPlan
{
vlong lit; // bytes of initialized non-zero literals
vlong zero; // bytes of zeros
vlong expr; // bytes of run-time computed expressions
InitEntry *e;
int len;
int cap;
};
enum
{
EscUnknown,
......@@ -239,8 +260,8 @@ struct Node
uchar walkdef;
uchar typecheck;
uchar local;
uchar dodata;
uchar initorder;
uchar dodata; // compile literal assignment as data statement
uchar used;
uchar isddd;
uchar pun; // don't registerize variable ONAME
......@@ -281,6 +302,9 @@ struct Node
// OPACK
Pkg* pkg;
// OARRAYLIT, OMAPLIT, OSTRUCTLIT.
InitPlan* initplan;
// Escape analysis.
NodeList* escflowsrc; // flow(this, src)
......@@ -1306,8 +1330,6 @@ Prog* unpatch(Prog*);
void zfile(Biobuf *b, char *p, int n);
void zhist(Biobuf *b, int line, vlong offset);
void zname(Biobuf *b, Sym *s, int t);
void data(void);
void text(void);
#pragma varargck type "A" int
#pragma varargck type "B" Mpint*
......
......@@ -31,10 +31,6 @@ dumpobj(void)
outhist(bout);
// add nil plist w AEND to catch
// auto-generated trampolines, data
newplist();
dumpglobls();
dumptypestructs();
dumpdata();
......@@ -279,8 +275,7 @@ stringsym(char *s, int len)
if(sym->flags & SymUniq)
return sym;
sym->flags |= SymUniq;
data();
off = 0;
// string header
......@@ -297,7 +292,6 @@ stringsym(char *s, int len)
off = duint8(sym, off, 0); // terminating NUL for runtime
off = (off+widthptr-1)&~(widthptr-1); // round to pointer alignment
ggloblsym(sym, off, 1);
text();
return sym;
}
......@@ -142,7 +142,6 @@ methods(Type *t)
Type *f, *mt, *it, *this;
Sig *a, *b;
Sym *method;
Prog *oldlist;
// named method type
mt = methtype(t);
......@@ -158,7 +157,6 @@ methods(Type *t)
// make list of methods for t,
// generating code if necessary.
a = nil;
oldlist = nil;
for(f=mt->xmethod; f; f=f->down) {
if(f->type->etype != TFUNC)
continue;
......@@ -197,8 +195,6 @@ methods(Type *t)
if(!(a->isym->flags & SymSiggen)) {
a->isym->flags |= SymSiggen;
if(!eqtype(this, it) || this->width < types[tptr]->width) {
if(oldlist == nil)
oldlist = pc;
// Is okay to call genwrapper here always,
// but we can generate more efficient code
// using genembedtramp if all that is necessary
......@@ -214,8 +210,6 @@ methods(Type *t)
if(!(a->tsym->flags & SymSiggen)) {
a->tsym->flags |= SymSiggen;
if(!eqtype(this, t)) {
if(oldlist == nil)
oldlist = pc;
if(isptr[t->etype] && isptr[this->etype]
&& f->embedded && !isifacemethod(f->type))
genembedtramp(t, f, a->tsym, 0);
......@@ -225,16 +219,6 @@ methods(Type *t)
}
}
// restore data output
if(oldlist) {
// old list ended with AEND; change to ANOP
// so that the trampolines that follow can be found.
nopout(oldlist);
// start new data list
newplist();
}
return lsort(a, sigcmp);
}
......@@ -247,11 +231,9 @@ imethods(Type *t)
Sig *a, *all, *last;
Type *f;
Sym *method, *isym;
Prog *oldlist;
all = nil;
last = nil;
oldlist = nil;
for(f=t->type; f; f=f->down) {
if(f->etype != TFIELD)
fatal("imethods: not field");
......@@ -289,21 +271,9 @@ imethods(Type *t)
isym = methodsym(method, t, 0);
if(!(isym->flags & SymSiggen)) {
isym->flags |= SymSiggen;
if(oldlist == nil)
oldlist = pc;
genwrapper(t, f, isym, 0);
}
}
if(oldlist) {
// old list ended with AEND; change to ANOP
// so that the trampolines that follow can be found.
nopout(oldlist);
// start new data list
newplist();
}
return all;
}
......
This diff is collapsed.
......@@ -153,7 +153,7 @@ typecheck(Node **np, int top)
}
if(n->typecheck == 2) {
yyerror("typechecking loop");
yyerror("typechecking loop involving %#N", n);
lineno = lno;
return n;
}
......@@ -2103,6 +2103,7 @@ typecheckcomplit(Node **np)
yyerror("implicit assignment of unexported field '%s' in %T literal", s->name, t);
ll->n = assignconv(ll->n, f->type, "field value");
ll->n = nod(OKEY, newname(f->sym), ll->n);
ll->n->left->type = f;
ll->n->left->typecheck = 1;
f = f->down;
}
......@@ -2132,14 +2133,15 @@ typecheckcomplit(Node **np)
// before we do the lookup.
if(s->pkg != localpkg)
s = lookup(s->name);
l->left = newname(s);
l->left->typecheck = 1;
f = lookdot1(s, t, t->type, 0);
typecheck(&l->right, Erv);
if(f == nil) {
yyerror("unknown %T field '%s' in struct literal", t, s->name);
continue;
}
l->left = newname(s);
l->left->typecheck = 1;
l->left->type = f;
s = f->sym;
fielddup(newname(s), hash, nhash);
l->right = assignconv(l->right, f->type, "field value");
......
......@@ -42,9 +42,6 @@ hello, world
=========== ./sigchld.go
survived SIGCHLD
=========== ./sinit.go
FAIL
=========== ./turing.go
Hello World!
......
// $G -S $D/$F.go | egrep initdone >/dev/null && echo FAIL || true
// $G -S $D/$F.go | egrep initdone >/dev/null && echo BUG sinit || true
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
......@@ -9,45 +9,45 @@ package p
// Should be no init func in the assembly.
// All these initializations should be done at link time.
type S struct{ a,b,c int }
type SS struct{ aa,bb,cc S }
type SA struct{ a,b,c [3]int }
type SC struct{ a,b,c []int }
type S struct{ a, b, c int }
type SS struct{ aa, bb, cc S }
type SA struct{ a, b, c [3]int }
type SC struct{ a, b, c []int }
var (
zero = 2
one = 1
pi = 3.14
slice = []byte{1,2,3}
sliceInt = []int{1,2,3}
hello = "hello, world"
bytes = []byte("hello, world")
four, five = 4, 5
x, y = 0.1, "hello"
nilslice []byte = nil
nilmap map[string]int = nil
nilfunc func() = nil
nilchan chan int = nil
nilptr *byte = nil
zero = 2
one = 1
pi = 3.14
slice = []byte{1, 2, 3}
sliceInt = []int{1, 2, 3}
hello = "hello, world"
bytes = []byte("hello, world")
four, five = 4, 5
x, y = 0.1, "hello"
nilslice []byte = nil
nilmap map[string]int = nil
nilfunc func() = nil
nilchan chan int = nil
nilptr *byte = nil
)
var a = [3]int{1001, 1002, 1003}
var s = S{1101, 1102, 1103}
var c = []int{1201, 1202, 1203}
var a = [3]int{1001, 1002, 1003}
var s = S{1101, 1102, 1103}
var c = []int{1201, 1202, 1203}
var aa = [3][3]int{[3]int{2001,2002,2003}, [3]int{2004,2005,2006}, [3]int{2007,2008,2009}}
var as = [3]S{S{2101,2102,2103},S{2104,2105,2106},S{2107,2108,2109}}
var ac = [3][]int{[]int{2201,2202,2203}, []int{2204,2205,2206}, []int{2207,2208,2209}}
var aa = [3][3]int{[3]int{2001, 2002, 2003}, [3]int{2004, 2005, 2006}, [3]int{2007, 2008, 2009}}
var as = [3]S{S{2101, 2102, 2103}, S{2104, 2105, 2106}, S{2107, 2108, 2109}}
var ac = [3][]int{[]int{2201, 2202, 2203}, []int{2204, 2205, 2206}, []int{2207, 2208, 2209}}
var sa = SA{[3]int{3001,3002,3003},[3]int{3004,3005,3006},[3]int{3007,3008,3009}}
var ss = SS{S{3101,3102,3103},S{3104,3105,3106},S{3107,3108,3109}}
var sc = SC{[]int{3201,3202,3203},[]int{3204,3205,3206},[]int{3207,3208,3209}}
var sa = SA{[3]int{3001, 3002, 3003}, [3]int{3004, 3005, 3006}, [3]int{3007, 3008, 3009}}
var ss = SS{S{3101, 3102, 3103}, S{3104, 3105, 3106}, S{3107, 3108, 3109}}
var sc = SC{[]int{3201, 3202, 3203}, []int{3204, 3205, 3206}, []int{3207, 3208, 3209}}
var ca = [][3]int{[3]int{4001,4002,4003}, [3]int{4004,4005,4006}, [3]int{4007,4008,4009}}
var cs = []S{S{4101,4102,4103},S{4104,4105,4106},S{4107,4108,4109}}
var cc = [][]int{[]int{4201,4202,4203}, []int{4204,4205,4206}, []int{4207,4208,4209}}
var ca = [][3]int{[3]int{4001, 4002, 4003}, [3]int{4004, 4005, 4006}, [3]int{4007, 4008, 4009}}
var cs = []S{S{4101, 4102, 4103}, S{4104, 4105, 4106}, S{4107, 4108, 4109}}
var cc = [][]int{[]int{4201, 4202, 4203}, []int{4204, 4205, 4206}, []int{4207, 4208, 4209}}
var answers = [...]int {
var answers = [...]int{
// s
1101, 1102, 1103,
......@@ -98,3 +98,158 @@ var answers = [...]int {
2008, 2208, 2308, 4008, 4208, 4308, 5008, 5208, 5308,
2009, 2209, 2309, 4009, 4209, 4309, 5009, 5209, 5309,
}
var (
copy_zero = zero
copy_one = one
copy_pi = pi
copy_slice = slice
copy_sliceInt = sliceInt
copy_hello = hello
copy_bytes = bytes
copy_four, copy_five = four, five
copy_x, copy_y = x, y
copy_nilslice = nilslice
copy_nilmap = nilmap
copy_nilfunc = nilfunc
copy_nilchan = nilchan
copy_nilptr = nilptr
)
var copy_a = a
var copy_s = s
var copy_c = c
var copy_aa = aa
var copy_as = as
var copy_ac = ac
var copy_sa = sa
var copy_ss = ss
var copy_sc = sc
var copy_ca = ca
var copy_cs = cs
var copy_cc = cc
var copy_answers = answers
var bx bool
var b0 = false
var b1 = true
var fx float32
var f0 = float32(0)
var f1 = float32(1)
var gx float64
var g0 = float64(0)
var g1 = float64(1)
var ix int
var i0 = 0
var i1 = 1
var jx uint
var j0 = uint(0)
var j1 = uint(1)
var cx complex64
var c0 = complex64(0)
var c1 = complex64(1)
var dx complex128
var d0 = complex128(0)
var d1 = complex128(1)
var sx []int
var s0 = []int{0, 0, 0}
var s1 = []int{1, 2, 3}
func fi() int
var ax [10]int
var a0 = [10]int{0, 0, 0}
var a1 = [10]int{1, 2, 3, 4}
type T struct{ X, Y int }
var tx T
var t0 = T{}
var t0a = T{0, 0}
var t0b = T{X: 0}
var t1 = T{X: 1, Y: 2}
var t1a = T{3, 4}
var psx *[]int
var ps0 = &[]int{0, 0, 0}
var ps1 = &[]int{1, 2, 3}
var pax *[10]int
var pa0 = &[10]int{0, 0, 0}
var pa1 = &[10]int{1, 2, 3}
var ptx *T
var pt0 = &T{}
var pt0a = &T{0, 0}
var pt0b = &T{X: 0}
var pt1 = &T{X: 1, Y: 2}
var pt1a = &T{3, 4}
var copy_bx = bx
var copy_b0 = b0
var copy_b1 = b1
var copy_fx = fx
var copy_f0 = f0
var copy_f1 = f1
var copy_gx = gx
var copy_g0 = g0
var copy_g1 = g1
var copy_ix = ix
var copy_i0 = i0
var copy_i1 = i1
var copy_jx = jx
var copy_j0 = j0
var copy_j1 = j1
var copy_cx = cx
var copy_c0 = c0
var copy_c1 = c1
var copy_dx = dx
var copy_d0 = d0
var copy_d1 = d1
var copy_sx = sx
var copy_s0 = s0
var copy_s1 = s1
var copy_ax = ax
var copy_a0 = a0
var copy_a1 = a1
var copy_tx = tx
var copy_t0 = t0
var copy_t0a = t0a
var copy_t0b = t0b
var copy_t1 = t1
var copy_t1a = t1a
var copy_psx = psx
var copy_ps0 = ps0
var copy_ps1 = ps1
var copy_pax = pax
var copy_pa0 = pa0
var copy_pa1 = pa1
var copy_ptx = ptx
var copy_pt0 = pt0
var copy_pt0a = pt0a
var copy_pt0b = pt0b
var copy_pt1 = pt1
var copy_pt1a = pt1a
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment