This source file includes following definitions.
- sigcmp
- lsort
- mapbucket
- hmap
- hiter
- methodfunc
- methods
- imethods
- dimportpath
- dgopkgpath
- dextratype
- haspointers
- dcommontype
- typesym
- tracksym
- typelinksym
- typesymprefix
- typenamesym
- typename
- weaktypesym
- dtypesym
- dumptypestructs
- dalgsym
- gcinline
- dgcsym1
- dgcsym
#include <u.h>
#include <libc.h>
#include "go.h"
#include "../../pkg/runtime/mgc0.h"
static NodeList* signatlist;
static Sym* dtypesym(Type*);
static Sym* weaktypesym(Type*);
static Sym* dalgsym(Type*);
static Sym* dgcsym(Type*);
static int
sigcmp(Sig *a, Sig *b)
{
int i;
i = strcmp(a->name, b->name);
if(i != 0)
return i;
if(a->pkg == b->pkg)
return 0;
if(a->pkg == nil)
return -1;
if(b->pkg == nil)
return +1;
return strcmp(a->pkg->path->s, b->pkg->path->s);
}
static Sig*
lsort(Sig *l, int(*f)(Sig*, Sig*))
{
Sig *l1, *l2, *le;
if(l == 0 || l->link == 0)
return l;
l1 = l;
l2 = l;
for(;;) {
l2 = l2->link;
if(l2 == 0)
break;
l2 = l2->link;
if(l2 == 0)
break;
l1 = l1->link;
}
l2 = l1->link;
l1->link = 0;
l1 = lsort(l, f);
l2 = lsort(l2, f);
if((*f)(l1, l2) < 0) {
l = l1;
l1 = l1->link;
} else {
l = l2;
l2 = l2->link;
}
le = l;
for(;;) {
if(l1 == 0) {
while(l2) {
le->link = l2;
le = l2;
l2 = l2->link;
}
le->link = 0;
break;
}
if(l2 == 0) {
while(l1) {
le->link = l1;
le = l1;
l1 = l1->link;
}
break;
}
if((*f)(l1, l2) < 0) {
le->link = l1;
le = l1;
l1 = l1->link;
} else {
le->link = l2;
le = l2;
l2 = l2->link;
}
}
le->link = 0;
return l;
}
enum {
BUCKETSIZE = 8,
MAXKEYSIZE = 128,
MAXVALSIZE = 128,
};
static Type*
mapbucket(Type *t)
{
Type *keytype, *valtype;
Type *bucket;
Type *overflowfield, *keysfield, *valuesfield;
int32 offset;
if(t->bucket != T)
return t->bucket;
keytype = t->down;
valtype = t->type;
dowidth(keytype);
dowidth(valtype);
if(keytype->width > MAXKEYSIZE)
keytype = ptrto(keytype);
if(valtype->width > MAXVALSIZE)
valtype = ptrto(valtype);
bucket = typ(TSTRUCT);
bucket->noalg = 1;
offset = BUCKETSIZE * 1;
overflowfield = typ(TFIELD);
overflowfield->type = ptrto(bucket);
overflowfield->width = offset;
overflowfield->sym = mal(sizeof(Sym));
overflowfield->sym->name = "overflow";
offset += widthptr;
if(widthreg > widthptr)
offset += widthreg - widthptr;
keysfield = typ(TFIELD);
keysfield->type = typ(TARRAY);
keysfield->type->type = keytype;
keysfield->type->bound = BUCKETSIZE;
keysfield->type->width = BUCKETSIZE * keytype->width;
keysfield->width = offset;
keysfield->sym = mal(sizeof(Sym));
keysfield->sym->name = "keys";
offset += BUCKETSIZE * keytype->width;
valuesfield = typ(TFIELD);
valuesfield->type = typ(TARRAY);
valuesfield->type->type = valtype;
valuesfield->type->bound = BUCKETSIZE;
valuesfield->type->width = BUCKETSIZE * valtype->width;
valuesfield->width = offset;
valuesfield->sym = mal(sizeof(Sym));
valuesfield->sym->name = "values";
offset += BUCKETSIZE * valtype->width;
bucket->type = overflowfield;
overflowfield->down = keysfield;
keysfield->down = valuesfield;
valuesfield->down = T;
bucket->width = offset;
bucket->local = t->local;
t->bucket = bucket;
bucket->map = t;
return bucket;
}
static Type*
hmap(Type *t)
{
Type *h, *bucket;
Type *bucketsfield, *oldbucketsfield;
int32 offset;
if(t->hmap != T)
return t->hmap;
bucket = mapbucket(t);
h = typ(TSTRUCT);
h->noalg = 1;
offset = widthint;
offset += 4;
offset += 4;
offset += 1;
offset += 1;
offset += 1;
offset = (offset + 1) / 2 * 2;
offset += 2;
offset = (offset + widthptr - 1) / widthptr * widthptr;
bucketsfield = typ(TFIELD);
bucketsfield->type = ptrto(bucket);
bucketsfield->width = offset;
bucketsfield->sym = mal(sizeof(Sym));
bucketsfield->sym->name = "buckets";
offset += widthptr;
oldbucketsfield = typ(TFIELD);
oldbucketsfield->type = ptrto(bucket);
oldbucketsfield->width = offset;
oldbucketsfield->sym = mal(sizeof(Sym));
oldbucketsfield->sym->name = "oldbuckets";
offset += widthptr;
offset += widthptr;
h->type = bucketsfield;
bucketsfield->down = oldbucketsfield;
oldbucketsfield->down = T;
h->width = offset;
h->local = t->local;
t->hmap = h;
h->map = t;
return h;
}
Type*
hiter(Type *t)
{
int32 n, off;
Type *field[7];
Type *i;
if(t->hiter != T)
return t->hiter;
field[0] = typ(TFIELD);
field[0]->type = ptrto(t->down);
field[0]->sym = mal(sizeof(Sym));
field[0]->sym->name = "key";
field[1] = typ(TFIELD);
field[1]->type = ptrto(t->type);
field[1]->sym = mal(sizeof(Sym));
field[1]->sym->name = "val";
field[2] = typ(TFIELD);
field[2]->type = ptrto(types[TUINT8]);
field[2]->sym = mal(sizeof(Sym));
field[2]->sym->name = "t";
field[3] = typ(TFIELD);
field[3]->type = ptrto(hmap(t));
field[3]->sym = mal(sizeof(Sym));
field[3]->sym->name = "h";
field[4] = typ(TFIELD);
field[4]->type = ptrto(mapbucket(t));
field[4]->sym = mal(sizeof(Sym));
field[4]->sym->name = "buckets";
field[5] = typ(TFIELD);
field[5]->type = ptrto(mapbucket(t));
field[5]->sym = mal(sizeof(Sym));
field[5]->sym->name = "bptr";
field[6] = typ(TFIELD);
field[6]->type = typ(TARRAY);
field[6]->type->type = types[TUINTPTR];
field[6]->type->bound = 4;
field[6]->type->width = 4 * widthptr;
field[6]->sym = mal(sizeof(Sym));
field[6]->sym->name = "other";
i = typ(TSTRUCT);
i->noalg = 1;
i->type = field[0];
off = 0;
for(n = 0; n < 6; n++) {
field[n]->down = field[n+1];
field[n]->width = off;
off += field[n]->type->width;
}
field[6]->down = T;
off += field[6]->type->width;
if(off != 10 * widthptr)
yyerror("hash_iter size not correct %d %d", off, 10 * widthptr);
t->hiter = i;
i->map = t;
return i;
}
Type*
methodfunc(Type *f, Type *receiver)
{
NodeList *in, *out;
Node *d;
Type *t;
in = nil;
if(receiver) {
d = nod(ODCLFIELD, N, N);
d->type = receiver;
in = list(in, d);
}
for(t=getinargx(f)->type; t; t=t->down) {
d = nod(ODCLFIELD, N, N);
d->type = t->type;
d->isddd = t->isddd;
in = list(in, d);
}
out = nil;
for(t=getoutargx(f)->type; t; t=t->down) {
d = nod(ODCLFIELD, N, N);
d->type = t->type;
out = list(out, d);
}
t = functype(N, in, out);
if(f->nname) {
t->nname = f->nname;
}
return t;
}
static Sig*
methods(Type *t)
{
Type *f, *mt, *it, *this;
Sig *a, *b;
Sym *method;
mt = methtype(t, 0);
if(mt == T)
return nil;
expandmeth(mt);
it = t;
if(it->width > widthptr)
it = ptrto(t);
a = nil;
for(f=mt->xmethod; f; f=f->down) {
if(f->etype != TFIELD)
fatal("methods: not field %T", f);
if (f->type->etype != TFUNC || f->type->thistuple == 0)
fatal("non-method on %T method %S %T\n", mt, f->sym, f);
if (!getthisx(f->type)->type)
fatal("receiver with no type on %T method %S %T\n", mt, f->sym, f);
if(f->nointerface)
continue;
method = f->sym;
if(method == nil)
continue;
this = getthisx(f->type)->type->type;
if(isptr[this->etype] && this->type == t)
continue;
if(isptr[this->etype] && !isptr[t->etype]
&& f->embedded != 2 && !isifacemethod(f->type))
continue;
b = mal(sizeof(*b));
b->link = a;
a = b;
a->name = method->name;
if(!exportname(method->name)) {
if(method->pkg == nil)
fatal("methods: missing package");
a->pkg = method->pkg;
}
a->isym = methodsym(method, it, 1);
a->tsym = methodsym(method, t, 0);
a->type = methodfunc(f->type, t);
a->mtype = methodfunc(f->type, nil);
if(!(a->isym->flags & SymSiggen)) {
a->isym->flags |= SymSiggen;
if(!eqtype(this, it) || this->width < types[tptr]->width) {
compiling_wrappers = 1;
genwrapper(it, f, a->isym, 1);
compiling_wrappers = 0;
}
}
if(!(a->tsym->flags & SymSiggen)) {
a->tsym->flags |= SymSiggen;
if(!eqtype(this, t)) {
compiling_wrappers = 1;
genwrapper(t, f, a->tsym, 0);
compiling_wrappers = 0;
}
}
}
return lsort(a, sigcmp);
}
static Sig*
imethods(Type *t)
{
Sig *a, *all, *last;
Type *f;
Sym *method, *isym;
all = nil;
last = nil;
for(f=t->type; f; f=f->down) {
if(f->etype != TFIELD)
fatal("imethods: not field");
if(f->type->etype != TFUNC || f->sym == nil)
continue;
method = f->sym;
a = mal(sizeof(*a));
a->name = method->name;
if(!exportname(method->name)) {
if(method->pkg == nil)
fatal("imethods: missing package");
a->pkg = method->pkg;
}
a->mtype = f->type;
a->offset = 0;
a->type = methodfunc(f->type, nil);
if(last && sigcmp(last, a) >= 0)
fatal("sigcmp vs sortinter %s %s", last->name, a->name);
if(last == nil)
all = a;
else
last->link = a;
last = a;
if(isblanksym(method))
continue;
isym = methodsym(method, t, 0);
if(!(isym->flags & SymSiggen)) {
isym->flags |= SymSiggen;
genwrapper(t, f, isym, 0);
}
}
return all;
}
static void
dimportpath(Pkg *p)
{
static Pkg *gopkg;
char *nam;
Node *n;
if(p->pathsym != S)
return;
if(gopkg == nil) {
gopkg = mkpkg(strlit("go"));
gopkg->name = "go";
}
nam = smprint("importpath.%s.", p->prefix);
n = nod(ONAME, N, N);
n->sym = pkglookup(nam, gopkg);
free(nam);
n->class = PEXTERN;
n->xoffset = 0;
p->pathsym = n->sym;
gdatastring(n, p->path);
ggloblsym(n->sym, types[TSTRING]->width, 1, 1);
}
static int
dgopkgpath(Sym *s, int ot, Pkg *pkg)
{
if(pkg == nil)
return dgostringptr(s, ot, nil);
if(pkg == localpkg) {
static Sym *ns;
if(ns == nil)
ns = pkglookup("importpath.\"\".", mkpkg(strlit("go")));
return dsymptr(s, ot, ns, 0);
}
dimportpath(pkg);
return dsymptr(s, ot, pkg->pathsym, 0);
}
static int
dextratype(Sym *sym, int off, Type *t, int ptroff)
{
int ot, n;
Sym *s;
Sig *a, *m;
m = methods(t);
if(t->sym == nil && m == nil)
return off;
dsymptr(sym, ptroff, sym, off);
n = 0;
for(a=m; a; a=a->link) {
dtypesym(a->type);
n++;
}
ot = off;
s = sym;
if(t->sym) {
ot = dgostringptr(s, ot, t->sym->name);
if(t != types[t->etype] && t != errortype)
ot = dgopkgpath(s, ot, t->sym->pkg);
else
ot = dgostringptr(s, ot, nil);
} else {
ot = dgostringptr(s, ot, nil);
ot = dgostringptr(s, ot, nil);
}
ot = dsymptr(s, ot, s, ot + widthptr + 2*widthint);
ot = duintxx(s, ot, n, widthint);
ot = duintxx(s, ot, n, widthint);
for(a=m; a; a=a->link) {
ot = dgostringptr(s, ot, a->name);
ot = dgopkgpath(s, ot, a->pkg);
ot = dsymptr(s, ot, dtypesym(a->mtype), 0);
ot = dsymptr(s, ot, dtypesym(a->type), 0);
if(a->isym)
ot = dsymptr(s, ot, a->isym, 0);
else
ot = duintptr(s, ot, 0);
if(a->tsym)
ot = dsymptr(s, ot, a->tsym, 0);
else
ot = duintptr(s, ot, 0);
}
return ot;
}
enum {
KindBool = 1,
KindInt,
KindInt8,
KindInt16,
KindInt32,
KindInt64,
KindUint,
KindUint8,
KindUint16,
KindUint32,
KindUint64,
KindUintptr,
KindFloat32,
KindFloat64,
KindComplex64,
KindComplex128,
KindArray,
KindChan,
KindFunc,
KindInterface,
KindMap,
KindPtr,
KindSlice,
KindString,
KindStruct,
KindUnsafePointer,
KindNoPointers = 1<<7,
};
static int
kinds[] =
{
[TINT] = KindInt,
[TUINT] = KindUint,
[TINT8] = KindInt8,
[TUINT8] = KindUint8,
[TINT16] = KindInt16,
[TUINT16] = KindUint16,
[TINT32] = KindInt32,
[TUINT32] = KindUint32,
[TINT64] = KindInt64,
[TUINT64] = KindUint64,
[TUINTPTR] = KindUintptr,
[TFLOAT32] = KindFloat32,
[TFLOAT64] = KindFloat64,
[TBOOL] = KindBool,
[TSTRING] = KindString,
[TPTR32] = KindPtr,
[TPTR64] = KindPtr,
[TSTRUCT] = KindStruct,
[TINTER] = KindInterface,
[TCHAN] = KindChan,
[TMAP] = KindMap,
[TARRAY] = KindArray,
[TFUNC] = KindFunc,
[TCOMPLEX64] = KindComplex64,
[TCOMPLEX128] = KindComplex128,
[TUNSAFEPTR] = KindUnsafePointer,
};
int
haspointers(Type *t)
{
Type *t1;
int ret;
if(t->haspointers != 0)
return t->haspointers - 1;
switch(t->etype) {
case TINT:
case TUINT:
case TINT8:
case TUINT8:
case TINT16:
case TUINT16:
case TINT32:
case TUINT32:
case TINT64:
case TUINT64:
case TUINTPTR:
case TFLOAT32:
case TFLOAT64:
case TCOMPLEX64:
case TCOMPLEX128:
case TBOOL:
ret = 0;
break;
case TARRAY:
if(t->bound < 0) {
ret = 1;
break;
}
if(t->bound == 0) {
ret = 0;
break;
}
ret = haspointers(t->type);
break;
case TSTRUCT:
ret = 0;
for(t1=t->type; t1!=T; t1=t1->down) {
if(haspointers(t1->type)) {
ret = 1;
break;
}
}
break;
case TSTRING:
case TPTR32:
case TPTR64:
case TUNSAFEPTR:
case TINTER:
case TCHAN:
case TMAP:
case TFUNC:
default:
ret = 1;
break;
}
t->haspointers = 1+ret;
return ret;
}
static int
dcommontype(Sym *s, int ot, Type *t)
{
int i, alg, sizeofAlg;
Sym *sptr, *algsym, *zero;
static Sym *algarray;
char *p;
if(ot != 0)
fatal("dcommontype %d", ot);
sizeofAlg = 4*widthptr;
if(algarray == nil)
algarray = pkglookup("algarray", runtimepkg);
alg = algtype(t);
algsym = S;
if(alg < 0)
algsym = dalgsym(t);
dowidth(t);
if(t->sym != nil && !isptr[t->etype])
sptr = dtypesym(ptrto(t));
else
sptr = weaktypesym(ptrto(t));
zero = pkglookup("zerovalue", runtimepkg);
ot = duintptr(s, ot, t->width);
ot = duint32(s, ot, typehash(t));
ot = duint8(s, ot, 0);
i = t->align;
if(i == 0)
i = 1;
if((i&(i-1)) != 0)
fatal("invalid alignment %d for %T", t->align, t);
ot = duint8(s, ot, t->align);
ot = duint8(s, ot, t->align);
i = kinds[t->etype];
if(t->etype == TARRAY && t->bound < 0)
i = KindSlice;
if(!haspointers(t))
i |= KindNoPointers;
ot = duint8(s, ot, i);
if(alg >= 0)
ot = dsymptr(s, ot, algarray, alg*sizeofAlg);
else
ot = dsymptr(s, ot, algsym, 0);
ot = dsymptr(s, ot, dgcsym(t), 0);
p = smprint("%-uT", t);
ot = dgostringptr(s, ot, p);
free(p);
ot += widthptr;
ot = dsymptr(s, ot, sptr, 0);
ot = dsymptr(s, ot, zero, 0);
return ot;
}
Sym*
typesym(Type *t)
{
char *p;
Sym *s;
p = smprint("%-T", t);
s = pkglookup(p, typepkg);
free(p);
return s;
}
Sym*
tracksym(Type *t)
{
char *p;
Sym *s;
p = smprint("%-T.%s", t->outer, t->sym->name);
s = pkglookup(p, trackpkg);
free(p);
return s;
}
Sym*
typelinksym(Type *t)
{
char *p;
Sym *s;
p = smprint("%-uT/%-T", t, t);
s = pkglookup(p, typelinkpkg);
free(p);
return s;
}
Sym*
typesymprefix(char *prefix, Type *t)
{
char *p;
Sym *s;
p = smprint("%s.%-T", prefix, t);
s = pkglookup(p, typepkg);
free(p);
return s;
}
Sym*
typenamesym(Type *t)
{
Sym *s;
Node *n;
if(t == T || (isptr[t->etype] && t->type == T) || isideal(t))
fatal("typename %T", t);
s = typesym(t);
if(s->def == N) {
n = nod(ONAME, N, N);
n->sym = s;
n->type = types[TUINT8];
n->addable = 1;
n->ullman = 1;
n->class = PEXTERN;
n->xoffset = 0;
n->typecheck = 1;
s->def = n;
signatlist = list(signatlist, typenod(t));
}
return s->def->sym;
}
Node*
typename(Type *t)
{
Sym *s;
Node *n;
s = typenamesym(t);
n = nod(OADDR, s->def, N);
n->type = ptrto(s->def->type);
n->addable = 1;
n->ullman = 2;
n->typecheck = 1;
return n;
}
static Sym*
weaktypesym(Type *t)
{
char *p;
Sym *s;
p = smprint("%-T", t);
s = pkglookup(p, weaktypepkg);
free(p);
return s;
}
static Sym*
dtypesym(Type *t)
{
int ot, xt, n, isddd, dupok;
Sym *s, *s1, *s2, *s3, *s4, *slink;
Sig *a, *m;
Type *t1, *tbase, *t2;
if(t == bytetype || t == runetype)
t = types[t->etype];
if(isideal(t))
fatal("dtypesym %T", t);
s = typesym(t);
if(s->flags & SymSiggen)
return s;
s->flags |= SymSiggen;
tbase = t;
if(isptr[t->etype] && t->sym == S && t->type->sym != S)
tbase = t->type;
dupok = tbase->sym == S;
if(compiling_runtime &&
(tbase == types[tbase->etype] ||
tbase == bytetype ||
tbase == runetype ||
tbase == errortype)) {
goto ok;
}
if(tbase->sym && !tbase->local)
return s;
if(isforw[tbase->etype])
return s;
ok:
ot = 0;
xt = 0;
switch(t->etype) {
default:
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
break;
case TARRAY:
if(t->bound >= 0) {
s1 = dtypesym(t->type);
t2 = typ(TARRAY);
t2->type = t->type;
t2->bound = -1;
s2 = dtypesym(t2);
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s1, 0);
ot = dsymptr(s, ot, s2, 0);
ot = duintptr(s, ot, t->bound);
} else {
s1 = dtypesym(t->type);
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s1, 0);
}
break;
case TCHAN:
s1 = dtypesym(t->type);
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s1, 0);
ot = duintptr(s, ot, t->chan);
break;
case TFUNC:
for(t1=getthisx(t)->type; t1; t1=t1->down)
dtypesym(t1->type);
isddd = 0;
for(t1=getinargx(t)->type; t1; t1=t1->down) {
isddd = t1->isddd;
dtypesym(t1->type);
}
for(t1=getoutargx(t)->type; t1; t1=t1->down)
dtypesym(t1->type);
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = duint8(s, ot, isddd);
ot = rnd(ot, widthptr);
ot = dsymptr(s, ot, s, ot+2*(widthptr+2*widthint));
n = t->thistuple + t->intuple;
ot = duintxx(s, ot, n, widthint);
ot = duintxx(s, ot, n, widthint);
ot = dsymptr(s, ot, s, ot+1*(widthptr+2*widthint)+n*widthptr);
ot = duintxx(s, ot, t->outtuple, widthint);
ot = duintxx(s, ot, t->outtuple, widthint);
for(t1=getthisx(t)->type; t1; t1=t1->down, n++)
ot = dsymptr(s, ot, dtypesym(t1->type), 0);
for(t1=getinargx(t)->type; t1; t1=t1->down, n++)
ot = dsymptr(s, ot, dtypesym(t1->type), 0);
for(t1=getoutargx(t)->type; t1; t1=t1->down, n++)
ot = dsymptr(s, ot, dtypesym(t1->type), 0);
break;
case TINTER:
m = imethods(t);
n = 0;
for(a=m; a; a=a->link) {
dtypesym(a->type);
n++;
}
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s, ot+widthptr+2*widthint);
ot = duintxx(s, ot, n, widthint);
ot = duintxx(s, ot, n, widthint);
for(a=m; a; a=a->link) {
ot = dgostringptr(s, ot, a->name);
ot = dgopkgpath(s, ot, a->pkg);
ot = dsymptr(s, ot, dtypesym(a->type), 0);
}
break;
case TMAP:
s1 = dtypesym(t->down);
s2 = dtypesym(t->type);
s3 = dtypesym(mapbucket(t));
s4 = dtypesym(hmap(t));
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s1, 0);
ot = dsymptr(s, ot, s2, 0);
ot = dsymptr(s, ot, s3, 0);
ot = dsymptr(s, ot, s4, 0);
break;
case TPTR32:
case TPTR64:
if(t->type->etype == TANY) {
ot = dcommontype(s, ot, t);
break;
}
s1 = dtypesym(t->type);
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s1, 0);
break;
case TSTRUCT:
n = 0;
for(t1=t->type; t1!=T; t1=t1->down) {
dtypesym(t1->type);
n++;
}
ot = dcommontype(s, ot, t);
xt = ot - 3*widthptr;
ot = dsymptr(s, ot, s, ot+widthptr+2*widthint);
ot = duintxx(s, ot, n, widthint);
ot = duintxx(s, ot, n, widthint);
for(t1=t->type; t1!=T; t1=t1->down) {
if(t1->sym && !t1->embedded) {
ot = dgostringptr(s, ot, t1->sym->name);
if(exportname(t1->sym->name))
ot = dgostringptr(s, ot, nil);
else
ot = dgopkgpath(s, ot, t1->sym->pkg);
} else {
ot = dgostringptr(s, ot, nil);
if(t1->type->sym != S && t1->type->sym->pkg == builtinpkg)
ot = dgopkgpath(s, ot, localpkg);
else
ot = dgostringptr(s, ot, nil);
}
ot = dsymptr(s, ot, dtypesym(t1->type), 0);
ot = dgostrlitptr(s, ot, t1->note);
ot = duintptr(s, ot, t1->width);
}
break;
}
ot = dextratype(s, ot, t, xt);
ggloblsym(s, ot, dupok, 1);
if(t->sym == S) {
switch(t->etype) {
case TARRAY:
case TCHAN:
case TMAP:
slink = typelinksym(t);
dsymptr(slink, 0, s, 0);
ggloblsym(slink, widthptr, dupok, 1);
}
}
return s;
}
void
dumptypestructs(void)
{
int i;
NodeList *l;
Node *n;
Type *t;
Pkg *p;
for(l=externdcl; l; l=l->next) {
n = l->n;
if(n->op != OTYPE)
continue;
signatlist = list(signatlist, n);
}
for(l=signatlist; l; l=l->next) {
n = l->n;
if(n->op != OTYPE)
continue;
t = n->type;
dtypesym(t);
if(t->sym)
dtypesym(ptrto(t));
}
for(i=0; i<nelem(phash); i++)
for(p=phash[i]; p; p=p->link)
if(p->direct)
dimportpath(p);
if(compiling_runtime) {
for(i=1; i<=TBOOL; i++)
dtypesym(ptrto(types[i]));
dtypesym(ptrto(types[TSTRING]));
dtypesym(ptrto(types[TUNSAFEPTR]));
dtypesym(ptrto(errortype));
dtypesym(functype(nil,
list1(nod(ODCLFIELD, N, typenod(errortype))),
list1(nod(ODCLFIELD, N, typenod(types[TSTRING])))));
dimportpath(runtimepkg);
if(flag_race)
dimportpath(racepkg);
dimportpath(mkpkg(strlit("main")));
}
}
static Sym*
dalgsym(Type *t)
{
int ot;
Sym *s, *hash, *eq;
char buf[100];
s = typesymprefix(".alg", t);
hash = typesymprefix(".hash", t);
genhash(hash, t);
eq = typesymprefix(".eq", t);
geneq(eq, t);
ot = 0;
ot = dsymptr(s, ot, hash, 0);
ot = dsymptr(s, ot, eq, 0);
ot = dsymptr(s, ot, pkglookup("memprint", runtimepkg), 0);
switch(t->width) {
default:
ot = dsymptr(s, ot, pkglookup("memcopy", runtimepkg), 0);
break;
case 1:
case 2:
case 4:
case 8:
case 16:
snprint(buf, sizeof buf, "memcopy%d", (int)t->width*8);
ot = dsymptr(s, ot, pkglookup(buf, runtimepkg), 0);
break;
}
ggloblsym(s, ot, 1, 1);
return s;
}
static int
gcinline(Type *t)
{
switch(t->etype) {
case TARRAY:
if(t->bound == 1)
return 1;
if(t->width <= 4*widthptr)
return 1;
break;
}
return 0;
}
static int
dgcsym1(Sym *s, int ot, Type *t, vlong *off, int stack_size)
{
Type *t1;
vlong o, off2, fieldoffset, i;
if(t->align > 0 && (*off % t->align) != 0)
fatal("dgcsym1: invalid initial alignment, %T", t);
if(t->width == BADWIDTH)
dowidth(t);
switch(t->etype) {
case TINT8:
case TUINT8:
case TINT16:
case TUINT16:
case TINT32:
case TUINT32:
case TINT64:
case TUINT64:
case TINT:
case TUINT:
case TUINTPTR:
case TBOOL:
case TFLOAT32:
case TFLOAT64:
case TCOMPLEX64:
case TCOMPLEX128:
*off += t->width;
break;
case TPTR32:
case TPTR64:
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
if(!haspointers(t->type)) {
ot = duintptr(s, ot, GC_APTR);
ot = duintptr(s, ot, *off);
} else {
ot = duintptr(s, ot, GC_PTR);
ot = duintptr(s, ot, *off);
ot = dsymptr(s, ot, dgcsym(t->type), 0);
}
*off += t->width;
break;
case TUNSAFEPTR:
case TFUNC:
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
ot = duintptr(s, ot, GC_APTR);
ot = duintptr(s, ot, *off);
*off += t->width;
break;
case TCHAN:
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
ot = duintptr(s, ot, GC_CHAN_PTR);
ot = duintptr(s, ot, *off);
ot = dsymptr(s, ot, dtypesym(t), 0);
*off += t->width;
break;
case TMAP:
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
ot = duintptr(s, ot, GC_PTR);
ot = duintptr(s, ot, *off);
ot = dsymptr(s, ot, dgcsym(hmap(t)), 0);
*off += t->width;
break;
case TSTRING:
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
ot = duintptr(s, ot, GC_STRING);
ot = duintptr(s, ot, *off);
*off += t->width;
break;
case TINTER:
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
if(isnilinter(t)) {
ot = duintptr(s, ot, GC_EFACE);
ot = duintptr(s, ot, *off);
} else {
ot = duintptr(s, ot, GC_IFACE);
ot = duintptr(s, ot, *off);
}
*off += t->width;
break;
case TARRAY:
if(t->bound < -1)
fatal("dgcsym1: invalid bound, %T", t);
if(t->type->width == BADWIDTH)
dowidth(t->type);
if(isslice(t)) {
if(*off % widthptr != 0)
fatal("dgcsym1: invalid alignment, %T", t);
if(t->type->width != 0) {
ot = duintptr(s, ot, GC_SLICE);
ot = duintptr(s, ot, *off);
ot = dsymptr(s, ot, dgcsym(t->type), 0);
} else {
ot = duintptr(s, ot, GC_APTR);
ot = duintptr(s, ot, *off);
}
*off += t->width;
} else {
if(t->bound < 1 || !haspointers(t->type)) {
*off += t->width;
} else if(gcinline(t)) {
for(i=0; i<t->bound; i++)
ot = dgcsym1(s, ot, t->type, off, stack_size);
} else {
if(stack_size < GC_STACK_CAPACITY) {
ot = duintptr(s, ot, GC_ARRAY_START);
ot = duintptr(s, ot, *off);
ot = duintptr(s, ot, t->bound);
ot = duintptr(s, ot, t->type->width);
off2 = 0;
ot = dgcsym1(s, ot, t->type, &off2, stack_size+1);
ot = duintptr(s, ot, GC_ARRAY_NEXT);
} else {
ot = duintptr(s, ot, GC_REGION);
ot = duintptr(s, ot, *off);
ot = duintptr(s, ot, t->width);
ot = dsymptr(s, ot, dgcsym(t), 0);
}
*off += t->width;
}
}
break;
case TSTRUCT:
o = 0;
for(t1=t->type; t1!=T; t1=t1->down) {
fieldoffset = t1->width;
*off += fieldoffset - o;
ot = dgcsym1(s, ot, t1->type, off, stack_size);
o = fieldoffset + t1->type->width;
}
*off += t->width - o;
break;
default:
fatal("dgcsym1: unexpected type %T", t);
}
return ot;
}
static Sym*
dgcsym(Type *t)
{
int ot;
vlong off;
Sym *s;
s = typesymprefix(".gc", t);
if(s->flags & SymGcgen)
return s;
s->flags |= SymGcgen;
if(t->width == BADWIDTH)
dowidth(t);
ot = 0;
off = 0;
ot = duintptr(s, ot, t->width);
ot = dgcsym1(s, ot, t, &off, 0);
ot = duintptr(s, ot, GC_END);
ggloblsym(s, ot, 1, 1);
if(t->align > 0)
off = rnd(off, t->align);
if(off != t->width)
fatal("dgcsym: off=%lld, size=%lld, type %T", off, t->width, t);
return s;
}