reflect.c 24.2 KB
Newer Older
1 2 3 4
// 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.

Russ Cox's avatar
Russ Cox committed
5 6
#include <u.h>
#include <libc.h>
7
#include "go.h"
8
#include "../../pkg/runtime/mgc0.h"
9 10 11 12 13

/*
 * runtime interface and reflection data structures
 */

Russ Cox's avatar
Russ Cox committed
14 15
static	NodeList*	signatlist;
static	Sym*	dtypesym(Type*);
Russ Cox's avatar
Russ Cox committed
16
static	Sym*	weaktypesym(Type*);
17
static	Sym*	dalgsym(Type*);
18
static	Sym*	dgcsym(Type*);
19 20 21 22

static int
sigcmp(Sig *a, Sig *b)
{
23
	int i;
24

25 26 27 28 29 30 31 32 33 34
	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);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

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);

	/* set up lead element */
	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;
}

/*
 * f is method type, with receiver.
106
 * return function type, receiver as first argument (or not).
107
 */
108
Type*
109
methodfunc(Type *f, Type *receiver)
110
{
111 112
	NodeList *in, *out;
	Node *d;
113 114
	Type *t;

115
	in = nil;
116
	if(receiver) {
117
		d = nod(ODCLFIELD, N, N);
118
		d->type = receiver;
119 120
		in = list(in, d);
	}
121
	for(t=getinargx(f)->type; t; t=t->down) {
122 123
		d = nod(ODCLFIELD, N, N);
		d->type = t->type;
Russ Cox's avatar
Russ Cox committed
124
		d->isddd = t->isddd;
125 126 127
		in = list(in, d);
	}

128
	out = nil;
129
	for(t=getoutargx(f)->type; t; t=t->down) {
130 131 132 133 134
		d = nod(ODCLFIELD, N, N);
		d->type = t->type;
		out = list(out, d);
	}

135 136 137 138 139 140
	t = functype(N, in, out);
	if(f->nname) {
		// Link to name of original method function.
		t->nname = f->nname;
	}
	return t;
141 142 143
}

/*
144
 * return methods of non-interface type t, sorted by name.
145 146 147 148 149 150 151 152 153
 * generates stub functions as needed.
 */
static Sig*
methods(Type *t)
{
	Type *f, *mt, *it, *this;
	Sig *a, *b;
	Sym *method;

154 155
	// method type
	mt = methtype(t, 0);
156 157
	if(mt == T)
		return nil;
158
	expandmeth(mt);
159 160 161 162 163 164 165 166 167

	// type stored in interface word
	it = t;
	if(it->width > widthptr)
		it = ptrto(t);

	// make list of methods for t,
	// generating code if necessary.
	a = nil;
Russ Cox's avatar
Russ Cox committed
168
	for(f=mt->xmethod; f; f=f->down) {
169
		if(f->etype != TFIELD)
170 171 172 173 174 175
			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);

176 177 178 179 180 181 182 183 184
		method = f->sym;
		if(method == nil)
			continue;

		// get receiver type for this particular method.
		// if pointer receiver but non-pointer t and
		// this is not an embedded pointer inside a struct,
		// method does not apply.
		this = getthisx(f->type)->type->type;
Russ Cox's avatar
Russ Cox committed
185 186
		if(isptr[this->etype] && this->type == t)
			continue;
187
		if(isptr[this->etype] && !isptr[t->etype]
188
		&& f->embedded != 2 && !isifacemethod(f->type))
189 190 191 192 193 194 195
			continue;

		b = mal(sizeof(*b));
		b->link = a;
		a = b;

		a->name = method->name;
196 197 198 199 200
		if(!exportname(method->name)) {
			if(method->pkg == nil)
				fatal("methods: missing package");
			a->pkg = method->pkg;
		}
201 202
		a->isym = methodsym(method, it, 1);
		a->tsym = methodsym(method, t, 0);
203 204
		a->type = methodfunc(f->type, t);
		a->mtype = methodfunc(f->type, nil);
205

Russ Cox's avatar
Russ Cox committed
206 207
		if(!(a->isym->flags & SymSiggen)) {
			a->isym->flags |= SymSiggen;
208
			if(!eqtype(this, it) || this->width < types[tptr]->width) {
209 210 211 212
				// Is okay to call genwrapper here always,
				// but we can generate more efficient code
				// using genembedtramp if all that is necessary
				// is a pointer adjustment and a JMP.
213
				compiling_wrappers = 1;
214
				if(isptr[it->etype] && isptr[this->etype]
215
				&& f->embedded && !isifacemethod(f->type))
Russ Cox's avatar
Russ Cox committed
216
					genembedtramp(it, f, a->isym, 1);
217
				else
218
					genwrapper(it, f, a->isym, 1);
219
				compiling_wrappers = 0;
220 221 222
			}
		}

Russ Cox's avatar
Russ Cox committed
223 224
		if(!(a->tsym->flags & SymSiggen)) {
			a->tsym->flags |= SymSiggen;
225
			if(!eqtype(this, t)) {
226
				compiling_wrappers = 1;
Russ Cox's avatar
Russ Cox committed
227
				if(isptr[t->etype] && isptr[this->etype]
228
				&& f->embedded && !isifacemethod(f->type))
Russ Cox's avatar
Russ Cox committed
229
					genembedtramp(t, f, a->tsym, 0);
230
				else
231
					genwrapper(t, f, a->tsym, 0);
232
				compiling_wrappers = 0;
233 234 235 236 237 238 239 240
			}
		}
	}

	return lsort(a, sigcmp);
}

/*
241
 * return methods of interface type t, sorted by name.
242
 */
Russ Cox's avatar
Russ Cox committed
243
static Sig*
244 245
imethods(Type *t)
{
246
	Sig *a, *all, *last;
247
	Type *f;
248
	Sym *method, *isym;
249

250 251
	all = nil;
	last = nil;
252 253 254 255 256
	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;
257
		method = f->sym;
258
		a = mal(sizeof(*a));
259
		a->name = method->name;
260 261 262
		if(!exportname(method->name)) {
			if(method->pkg == nil)
				fatal("imethods: missing package");
263
			a->pkg = method->pkg;
264
		}
265
		a->mtype = f->type;
266
		a->offset = 0;
267 268
		a->type = methodfunc(f->type, nil);

269 270 271 272 273 274 275
		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;
276

277 278 279 280
		// Compiler can only refer to wrappers for
		// named interface types.
		if(t->sym == S)
			continue;
281

282 283 284 285 286 287 288 289 290 291
		// NOTE(rsc): Perhaps an oversight that
		// IfaceType.Method is not in the reflect data.
		// Generate the method body, so that compiled
		// code can refer to it.
		isym = methodsym(method, t, 0);
		if(!(isym->flags & SymSiggen)) {
			isym->flags |= SymSiggen;
			genwrapper(t, f, isym, 0);
		}
	}
292
	return all;
293 294
}

295 296 297 298 299 300
static void
dimportpath(Pkg *p)
{
	static Pkg *gopkg;
	char *nam;
	Node *n;
301

302 303 304
	if(p->pathsym != S)
		return;

305 306 307 308 309 310 311 312 313 314 315
	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;
316
	p->pathsym = n->sym;
317

318
	gdatastring(n, p->path);
319
	ggloblsym(n->sym, types[TSTRING]->width, 1, 1);
320
}
321

322 323 324 325 326 327 328 329 330 331 332
static int
dgopkgpath(Sym *s, int ot, Pkg *pkg)
{
	if(pkg == nil)
		return dgostringptr(s, ot, nil);

	// Emit reference to go.importpath.""., which 6l will
	// rewrite using the correct import path.  Every package
	// that imports this one directly defines the symbol.
	if(pkg == localpkg) {
		static Sym *ns;
333

334 335 336 337 338 339 340 341 342
		if(ns == nil)
			ns = pkglookup("importpath.\"\".", mkpkg(strlit("go")));
		return dsymptr(s, ot, ns, 0);
	}

	dimportpath(pkg);
	return dsymptr(s, ot, pkg->pathsym, 0);
}

343 344 345 346
/*
 * uncommonType
 * ../../pkg/runtime/type.go:/uncommonType
 */
Russ Cox's avatar
Russ Cox committed
347 348
static int
dextratype(Sym *sym, int off, Type *t, int ptroff)
349 350 351 352 353 354 355
{
	int ot, n;
	Sym *s;
	Sig *a, *m;

	m = methods(t);
	if(t->sym == nil && m == nil)
Russ Cox's avatar
Russ Cox committed
356
		return off;
357

Russ Cox's avatar
Russ Cox committed
358 359
	// fill in *extraType pointer in header
	dsymptr(sym, ptroff, sym, off);
360 361 362 363 364 365 366

	n = 0;
	for(a=m; a; a=a->link) {
		dtypesym(a->type);
		n++;
	}

Russ Cox's avatar
Russ Cox committed
367 368
	ot = off;
	s = sym;
369 370
	if(t->sym) {
		ot = dgostringptr(s, ot, t->sym->name);
371
		if(t != types[t->etype] && t != errortype)
372
			ot = dgopkgpath(s, ot, t->sym->pkg);
373 374 375 376 377 378 379 380
		else
			ot = dgostringptr(s, ot, nil);
	} else {
		ot = dgostringptr(s, ot, nil);
		ot = dgostringptr(s, ot, nil);
	}

	// slice header
Russ Cox's avatar
Russ Cox committed
381 382 383
	ot = dsymptr(s, ot, s, ot + widthptr + 2*widthint);
	ot = duintxx(s, ot, n, widthint);
	ot = duintxx(s, ot, n, widthint);
384 385 386 387 388 389

	// methods
	for(a=m; a; a=a->link) {
		// method
		// ../../pkg/runtime/type.go:/method
		ot = dgostringptr(s, ot, a->name);
390
		ot = dgopkgpath(s, ot, a->pkg);
391
		ot = dsymptr(s, ot, dtypesym(a->mtype), 0);
392 393 394 395 396 397 398 399 400 401 402
		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);
	}

Russ Cox's avatar
Russ Cox committed
403
	return ot;
404 405
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
enum {
	KindBool = 1,
	KindInt,
	KindInt8,
	KindInt16,
	KindInt32,
	KindInt64,
	KindUint,
	KindUint8,
	KindUint16,
	KindUint32,
	KindUint64,
	KindUintptr,
	KindFloat32,
	KindFloat64,
421 422
	KindComplex64,
	KindComplex128,
423 424 425 426 427 428 429 430 431 432
	KindArray,
	KindChan,
	KindFunc,
	KindInterface,
	KindMap,
	KindPtr,
	KindSlice,
	KindString,
	KindStruct,
	KindUnsafePointer,
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	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,
463 464
	[TCOMPLEX64]	= KindComplex64,
	[TCOMPLEX128]	= KindComplex128,
Russ Cox's avatar
Russ Cox committed
465
	[TUNSAFEPTR]	= KindUnsafePointer,
466 467
};

468 469 470
static Sym*
typestruct(Type *t)
{
471 472 473 474 475 476 477
	// We use a weak reference to the reflect type
	// to avoid requiring package reflect in every binary.
	// If package reflect is available, the interface{} holding
	// a runtime type will contain a *reflect.commonType.
	// Otherwise it will use a nil type word but still be usable
	// by package runtime (because we always use the memory
	// after the interface value, not the interface value itself).
478
	USED(t);
479
	return pkglookup("*reflect.commonType", weaktypepkg);
480 481
}

Luuk van Dijk's avatar
Luuk van Dijk committed
482
int
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
haspointers(Type *t)
{
	Type *t1;

	switch(t->etype) {
	case TINT:
	case TUINT:
	case TINT8:
	case TUINT8:
	case TINT16:
	case TUINT16:
	case TINT32:
	case TUINT32:
	case TINT64:
	case TUINT64:
498
	case TUINTPTR:
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
	case TFLOAT32:
	case TFLOAT64:
	case TBOOL:
		return 0;
	case TARRAY:
		if(t->bound < 0)	// slice
			return 1;
		return haspointers(t->type);
	case TSTRUCT:
		for(t1=t->type; t1!=T; t1=t1->down)
			if(haspointers(t1->type))
				return 1;
		return 0;
	case TSTRING:
	case TPTR32:
	case TPTR64:
Russ Cox's avatar
Russ Cox committed
515
	case TUNSAFEPTR:
516 517 518 519 520 521 522 523 524
	case TINTER:
	case TCHAN:
	case TMAP:
	case TFUNC:
	default:
		return 1;
	}
}

525 526 527 528 529 530 531
/*
 * commonType
 * ../../pkg/runtime/type.go:/commonType
 */
static int
dcommontype(Sym *s, int ot, Type *t)
{
532 533
	int i, alg, sizeofAlg;
	Sym *sptr, *algsym;
534
	static Sym *algarray;
535 536
	char *p;

537 538 539
	sizeofAlg = 4*widthptr;
	if(algarray == nil)
		algarray = pkglookup("algarray", runtimepkg);
540 541 542 543
	alg = algtype(t);
	algsym = S;
	if(alg < 0)
		algsym = dalgsym(t);
544

Russ Cox's avatar
Russ Cox committed
545
	dowidth(t);
Russ Cox's avatar
Russ Cox committed
546 547 548 549 550
	if(t->sym != nil && !isptr[t->etype])
		sptr = dtypesym(ptrto(t));
	else
		sptr = weaktypesym(ptrto(t));

551 552 553 554 555 556 557
	// empty interface pointing at this type.
	// all the references that we emit are *interface{};
	// they point here.
	ot = rnd(ot, widthptr);
	ot = dsymptr(s, ot, typestruct(t), 0);
	ot = dsymptr(s, ot, s, 2*widthptr);

558
	// ../../pkg/reflect/type.go:/^type.commonType
559 560
	// actual type structure
	//	type commonType struct {
561 562 563 564 565 566 567 568 569 570 571
	//		size          uintptr
	//		hash          uint32
	//		_             uint8
	//		align         uint8
	//		fieldAlign    uint8
	//		kind          uint8
	//		alg           unsafe.Pointer
	//		gc            unsafe.Pointer
	//		string        *string
	//		*extraType
	//		ptrToThis     *Type
572 573
	//	}
	ot = duintptr(s, ot, t->width);
Russ Cox's avatar
Russ Cox committed
574
	ot = duint32(s, ot, typehash(t));
575
	ot = duint8(s, ot, 0);	// unused
576 577 578 579 580 581 582

	// runtime (and common sense) expects alignment to be a power of two.
	i = t->align;
	if(i == 0)
		i = 1;
	if((i&(i-1)) != 0)
		fatal("invalid alignment %d for %T", t->align, t);
Russ Cox's avatar
Russ Cox committed
583 584
	ot = duint8(s, ot, t->align);	// align
	ot = duint8(s, ot, t->align);	// fieldAlign
585

586 587 588 589 590
	i = kinds[t->etype];
	if(t->etype == TARRAY && t->bound < 0)
		i = KindSlice;
	if(!haspointers(t))
		i |= KindNoPointers;
591
	ot = duint8(s, ot, i);  // kind
592 593 594 595
	if(alg >= 0)
		ot = dsymptr(s, ot, algarray, alg*sizeofAlg);
	else
		ot = dsymptr(s, ot, algsym, 0);
596
	ot = dsymptr(s, ot, dgcsym(t), 0);  // gc
Luuk van Dijk's avatar
Luuk van Dijk committed
597 598
	p = smprint("%-uT", t);
	//print("dcommontype: %s\n", p);
599 600
	ot = dgostringptr(s, ot, p);	// string
	free(p);
601

Russ Cox's avatar
Russ Cox committed
602 603 604 605 606 607 608
	// skip pointer to extraType,
	// which follows the rest of this type structure.
	// caller will fill in if needed.
	// otherwise linker will assume 0.
	ot += widthptr;

	ot = dsymptr(s, ot, sptr, 0);  // ptrto type
609 610 611 612 613 614 615 616 617
	return ot;
}

Sym*
typesym(Type *t)
{
	char *p;
	Sym *s;

Luuk van Dijk's avatar
Luuk van Dijk committed
618
	p = smprint("%-T", t);
619
	s = pkglookup(p, typepkg);
Luuk van Dijk's avatar
Luuk van Dijk committed
620
	//print("typesym: %s -> %+S\n", p, s);
621 622 623 624
	free(p);
	return s;
}

625 626 627 628 629 630 631 632 633 634 635 636 637
Sym*
typesymprefix(char *prefix, Type *t)
{
	char *p;
	Sym *s;

	p = smprint("%s.%-T", prefix, t);
	s = pkglookup(p, typepkg);
	//print("algsym: %s -> %+S\n", p, s);
	free(p);
	return s;
}

638 639 640 641 642 643
Node*
typename(Type *t)
{
	Sym *s;
	Node *n;

644
	if(t == T || (isptr[t->etype] && t->type == T) || isideal(t))
645
		fatal("typename %T", t);
646 647 648 649 650 651 652 653 654
	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;
655
		n->typecheck = 1;
656 657
		s->def = n;

Russ Cox's avatar
Russ Cox committed
658
		signatlist = list(signatlist, typenod(t));
659 660 661 662 663 664
	}

	n = nod(OADDR, s->def, N);
	n->type = ptrto(s->def->type);
	n->addable = 1;
	n->ullman = 2;
665
	n->typecheck = 1;
666 667 668
	return n;
}

Russ Cox's avatar
Russ Cox committed
669 670 671 672 673
static Sym*
weaktypesym(Type *t)
{
	char *p;
	Sym *s;
674

Luuk van Dijk's avatar
Luuk van Dijk committed
675
	p = smprint("%-T", t);
676
	s = pkglookup(p, weaktypepkg);
Luuk van Dijk's avatar
Luuk van Dijk committed
677
	//print("weaktypesym: %s -> %+S\n", p, s);
Russ Cox's avatar
Russ Cox committed
678 679 680 681
	free(p);
	return s;
}

682
static Sym*
683 684
dtypesym(Type *t)
{
Russ Cox's avatar
Russ Cox committed
685
	int ot, xt, n, isddd, dupok;
686 687
	Sym *s, *s1, *s2;
	Sig *a, *m;
Russ Cox's avatar
Russ Cox committed
688
	Type *t1, *tbase, *t2;
689

690 691 692 693 694 695
	// Replace byte, rune aliases with real type.
	// They've been separate internally to make error messages
	// better, but we have to merge them in the reflect tables.
	if(t == bytetype || t == runetype)
		t = types[t->etype];

Russ Cox's avatar
Russ Cox committed
696 697
	if(isideal(t))
		fatal("dtypesym %T", t);
698 699

	s = typesym(t);
Russ Cox's avatar
Russ Cox committed
700
	if(s->flags & SymSiggen)
701
		return s;
Russ Cox's avatar
Russ Cox committed
702
	s->flags |= SymSiggen;
703 704 705 706

	// special case (look for runtime below):
	// when compiling package runtime,
	// emit the type structures for int, float, etc.
707 708 709 710
	tbase = t;
	if(isptr[t->etype] && t->sym == S && t->type->sym != S)
		tbase = t->type;
	dupok = tbase->sym == S;
711

712
	if(compiling_runtime &&
Russ Cox's avatar
Russ Cox committed
713 714 715 716
			(tbase == types[tbase->etype] ||
			tbase == bytetype ||
			tbase == runetype ||
			tbase == errortype)) { // int, float, etc
Russ Cox's avatar
Russ Cox committed
717
		goto ok;
Russ Cox's avatar
Russ Cox committed
718
	}
719

720 721
	// named types from other files are defined only by those files
	if(tbase->sym && !tbase->local)
722
		return s;
723
	if(isforw[tbase->etype])
724 725 726 727
		return s;

ok:
	ot = 0;
Russ Cox's avatar
Russ Cox committed
728
	xt = 0;
729 730 731
	switch(t->etype) {
	default:
		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
732
		xt = ot - 2*widthptr;
733 734 735
		break;

	case TARRAY:
Russ Cox's avatar
Russ Cox committed
736 737 738 739 740 741 742 743 744 745 746
		if(t->bound >= 0) {
			// ../../pkg/runtime/type.go:/ArrayType
			s1 = dtypesym(t->type);
			t2 = typ(TARRAY);
			t2->type = t->type;
			t2->bound = -1;  // slice
			s2 = dtypesym(t2);
			ot = dcommontype(s, ot, t);
			xt = ot - 2*widthptr;
			ot = dsymptr(s, ot, s1, 0);
			ot = dsymptr(s, ot, s2, 0);
747
			ot = duintptr(s, ot, t->bound);
Russ Cox's avatar
Russ Cox committed
748 749 750 751 752 753 754
		} else {
			// ../../pkg/runtime/type.go:/SliceType
			s1 = dtypesym(t->type);
			ot = dcommontype(s, ot, t);
			xt = ot - 2*widthptr;
			ot = dsymptr(s, ot, s1, 0);
		}
755 756 757 758 759 760
		break;

	case TCHAN:
		// ../../pkg/runtime/type.go:/ChanType
		s1 = dtypesym(t->type);
		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
761
		xt = ot - 2*widthptr;
762 763 764 765 766 767 768
		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);
Russ Cox's avatar
Russ Cox committed
769 770 771
		isddd = 0;
		for(t1=getinargx(t)->type; t1; t1=t1->down) {
			isddd = t1->isddd;
772
			dtypesym(t1->type);
Russ Cox's avatar
Russ Cox committed
773
		}
774 775 776 777
		for(t1=getoutargx(t)->type; t1; t1=t1->down)
			dtypesym(t1->type);

		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
778
		xt = ot - 2*widthptr;
Russ Cox's avatar
Russ Cox committed
779
		ot = duint8(s, ot, isddd);
780 781

		// two slice headers: in and out.
Russ Cox's avatar
Russ Cox committed
782
		ot = rnd(ot, widthptr);
Russ Cox's avatar
Russ Cox committed
783
		ot = dsymptr(s, ot, s, ot+2*(widthptr+2*widthint));
784
		n = t->thistuple + t->intuple;
Russ Cox's avatar
Russ Cox committed
785 786 787 788 789
		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);
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

		// slice data
		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++;
		}

		// ../../pkg/runtime/type.go:/InterfaceType
		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
810
		xt = ot - 2*widthptr;
Russ Cox's avatar
Russ Cox committed
811 812 813
		ot = dsymptr(s, ot, s, ot+widthptr+2*widthint);
		ot = duintxx(s, ot, n, widthint);
		ot = duintxx(s, ot, n, widthint);
814 815 816
		for(a=m; a; a=a->link) {
			// ../../pkg/runtime/type.go:/imethod
			ot = dgostringptr(s, ot, a->name);
817
			ot = dgopkgpath(s, ot, a->pkg);
818 819 820 821 822 823 824 825 826
			ot = dsymptr(s, ot, dtypesym(a->type), 0);
		}
		break;

	case TMAP:
		// ../../pkg/runtime/type.go:/MapType
		s1 = dtypesym(t->down);
		s2 = dtypesym(t->type);
		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
827
		xt = ot - 2*widthptr;
828 829 830 831 832 833 834
		ot = dsymptr(s, ot, s1, 0);
		ot = dsymptr(s, ot, s2, 0);
		break;

	case TPTR32:
	case TPTR64:
		if(t->type->etype == TANY) {
835
			// ../../pkg/runtime/type.go:/UnsafePointerType
836 837 838 839 840 841
			ot = dcommontype(s, ot, t);
			break;
		}
		// ../../pkg/runtime/type.go:/PtrType
		s1 = dtypesym(t->type);
		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
842
		xt = ot - 2*widthptr;
843 844 845 846 847 848 849 850 851 852 853 854
		ot = dsymptr(s, ot, s1, 0);
		break;

	case TSTRUCT:
		// ../../pkg/runtime/type.go:/StructType
		// for security, only the exported fields.
		n = 0;
		for(t1=t->type; t1!=T; t1=t1->down) {
			dtypesym(t1->type);
			n++;
		}
		ot = dcommontype(s, ot, t);
Russ Cox's avatar
Russ Cox committed
855
		xt = ot - 2*widthptr;
Russ Cox's avatar
Russ Cox committed
856 857 858
		ot = dsymptr(s, ot, s, ot+widthptr+2*widthint);
		ot = duintxx(s, ot, n, widthint);
		ot = duintxx(s, ot, n, widthint);
859 860
		for(t1=t->type; t1!=T; t1=t1->down) {
			// ../../pkg/runtime/type.go:/structField
861
			if(t1->sym && !t1->embedded) {
862 863 864 865
				ot = dgostringptr(s, ot, t1->sym->name);
				if(exportname(t1->sym->name))
					ot = dgostringptr(s, ot, nil);
				else
866
					ot = dgopkgpath(s, ot, t1->sym->pkg);
867 868
			} else {
				ot = dgostringptr(s, ot, nil);
869 870 871 872
				if(t1->type->sym != S && t1->type->sym->pkg == builtinpkg)
					ot = dgopkgpath(s, ot, localpkg);
				else
					ot = dgostringptr(s, ot, nil);
873 874 875 876 877 878 879
			}
			ot = dsymptr(s, ot, dtypesym(t1->type), 0);
			ot = dgostrlitptr(s, ot, t1->note);
			ot = duintptr(s, ot, t1->width);	// field offset
		}
		break;
	}
Russ Cox's avatar
Russ Cox committed
880
	ot = dextratype(s, ot, t, xt);
881
	ggloblsym(s, ot, dupok, 1);
882 883 884 885 886 887 888
	return s;
}

void
dumptypestructs(void)
{
	int i;
Russ Cox's avatar
Russ Cox committed
889 890
	NodeList *l;
	Node *n;
891
	Type *t;
892
	Pkg *p;
893 894

	// copy types from externdcl list to signatlist
Russ Cox's avatar
Russ Cox committed
895 896 897
	for(l=externdcl; l; l=l->next) {
		n = l->n;
		if(n->op != OTYPE)
898
			continue;
Russ Cox's avatar
Russ Cox committed
899
		signatlist = list(signatlist, n);
900 901 902
	}

	// process signatlist
Russ Cox's avatar
Russ Cox committed
903 904 905
	for(l=signatlist; l; l=l->next) {
		n = l->n;
		if(n->op != OTYPE)
906
			continue;
Russ Cox's avatar
Russ Cox committed
907
		t = n->type;
908
		dtypesym(t);
909
		if(t->sym)
910 911 912
			dtypesym(ptrto(t));
	}

913 914 915 916 917 918
	// generate import strings for imported packages
	for(i=0; i<nelem(phash); i++)
		for(p=phash[i]; p; p=p->link)
			if(p->direct)
				dimportpath(p);

919
	// do basic types if compiling package runtime.
920
	// they have to be in at least one package,
921
	// and runtime is always loaded implicitly,
922 923 924
	// so this is as good as any.
	// another possible choice would be package main,
	// but using runtime means fewer copies in .6 files.
925
	if(compiling_runtime) {
926
		for(i=1; i<=TBOOL; i++)
Russ Cox's avatar
bug190.  
Russ Cox committed
927
			dtypesym(ptrto(types[i]));
928
		dtypesym(ptrto(types[TSTRING]));
Russ Cox's avatar
Russ Cox committed
929
		dtypesym(ptrto(types[TUNSAFEPTR]));
Russ Cox's avatar
Russ Cox committed
930 931 932 933

		// emit type structs for error and func(error) string.
		// The latter is the type of an auto-generated wrapper.
		dtypesym(ptrto(errortype));
934
		dtypesym(functype(nil,
Russ Cox's avatar
Russ Cox committed
935 936
			list1(nod(ODCLFIELD, N, typenod(errortype))),
			list1(nod(ODCLFIELD, N, typenod(types[TSTRING])))));
937

938 939
		// add paths for runtime and main, which 6l imports implicitly.
		dimportpath(runtimepkg);
Dmitriy Vyukov's avatar
Dmitriy Vyukov committed
940 941
		if(debug['b'])
			dimportpath(racepkg);
942
		dimportpath(mkpkg(strlit("main")));
943 944
	}
}
945

946
static Sym*
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
dalgsym(Type *t)
{
	int ot;
	Sym *s, *hash, *eq;
	char buf[100];

	// dalgsym is only called for a type that needs an algorithm table,
	// which implies that the type is comparable (or else it would use ANOEQ).

	s = typesymprefix(".alg", t);
	hash = typesymprefix(".hash", t);
	genhash(hash, t);
	eq = typesymprefix(".eq", t);
	geneq(eq, t);

	// ../../pkg/runtime/runtime.h:/Alg
	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;
	}

981
	ggloblsym(s, ot, 1, 1);
982 983 984
	return s;
}

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
static int
dgcsym1(Sym *s, int ot, Type *t, vlong *off, int stack_size)
{
	Type *t1;
	vlong o, off2, fieldoffset;

	if(t->align > 0 && (*off % t->align) != 0)
		fatal("dgcsym1: invalid initial alignment, %T", 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) || t->type->etype == TUINT8) {
			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 TCHAN:
	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;

	// struct Hmap*
	case TMAP:
		if(*off % widthptr != 0)
			fatal("dgcsym1: invalid alignment, %T", t);
		ot = duintptr(s, ot, GC_MAP_PTR);
		ot = duintptr(s, ot, *off);
		ot = dsymptr(s, ot, dtypesym(t), 0);
		*off += t->width;
		break;

	// struct { byte *str; int32 len; }
	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;

	// struct { Itab* tab;  void* data; }
	// struct { Type* type; void* data; }	// When isnilinter(t)==true
	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(isslice(t)) {
			// struct { byte* array; uint32 len; uint32 cap; }
			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(t->bound == 1) {
				ot = dgcsym1(s, ot, t->type, off, stack_size);  // recursive call of dgcsym1
			} else {
				if(stack_size < GC_STACK_CAPACITY) {
					ot = duintptr(s, ot, GC_ARRAY_START);  // a stack push during GC
					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);  // recursive call of dgcsym1
					ot = duintptr(s, ot, GC_ARRAY_NEXT);  // a stack pop during GC
				} 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);  // recursive call of dgcsym1
			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;

	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;
}