subr.c 51.6 KB
Newer Older
1 2 3 4 5 6
// 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.

#include	"go.h"
#include	"y.tab.h"
7
#include	"opnames.h"
8 9 10 11 12 13

void
errorexit(void)
{
	if(outfile)
		remove(outfile);
14
	exit(1);
15 16
}

17 18 19 20 21 22 23 24 25
extern int yychar;
int
parserline(void)
{
	if(yychar != 0 && yychar != -2)	// parser has one symbol lookahead
		return prevlineno;
	return lineno;
}

26 27 28 29 30
void
yyerror(char *fmt, ...)
{
	va_list arg;

Russ Cox's avatar
Russ Cox committed
31
	if(strcmp(fmt, "syntax error") == 0) {
32
		print("%L: syntax error near %s\n", lexlineno, lexbuf);
Russ Cox's avatar
Russ Cox committed
33
		nsyntaxerrors++;
34
		goto out;
Russ Cox's avatar
Russ Cox committed
35
	}
36 37 38 39 40

	print("%L: ", parserline());
	va_start(arg, fmt);
	vfprint(1, fmt, arg);
	va_end(arg);
41
	print("\n");
42 43

out:
44 45 46
	if(debug['h'])
		*(int*)0 = 0;
	nerrors++;
47
	if(nerrors >= 10 && !debug['e'])
48 49 50 51 52 53 54 55
		fatal("too many errors");
}

void
warn(char *fmt, ...)
{
	va_list arg;

Ken Thompson's avatar
Ken Thompson committed
56
	print("%L: ", lineno);
57 58 59 60 61 62 63 64 65 66 67 68 69
	va_start(arg, fmt);
	vfprint(1, fmt, arg);
	va_end(arg);
	print("\n");
	if(debug['h'])
		*(int*)0 = 0;
}

void
fatal(char *fmt, ...)
{
	va_list arg;

Ken Thompson's avatar
Ken Thompson committed
70
	print("%L: fatal error: ", lineno);
71 72 73 74
	va_start(arg, fmt);
	vfprint(1, fmt, arg);
	va_end(arg);
	print("\n");
75 76 77
	if(debug['h']) {
		if(outfile)
			unlink(outfile);
78
		*(int*)0 = 0;
79 80
	}
	errorexit();
81 82
}

83
void
84
linehist(char *file, int32 off, int relative)
85 86
{
	Hist *h;
Ken Thompson's avatar
Ken Thompson committed
87
	char *cp;
88

Ken Thompson's avatar
Ken Thompson committed
89 90 91 92 93 94 95 96 97
	if(debug['i']) {
		if(file != nil) {
			if(off < 0)
				print("pragma %s at line %L\n", file, lineno);
			else
				print("import %s at line %L\n", file, lineno);
		} else
			print("end of import at line %L\n", lineno);
	}
98

99
	if(off < 0 && file[0] != '/' && !relative) {
Ken Thompson's avatar
Ken Thompson committed
100 101 102 103 104
		cp = mal(strlen(file) + strlen(pathname) + 2);
		sprint(cp, "%s/%s", pathname, file);
		file = cp;
	}

105
	h = mal(sizeof(Hist));
106
	h->name = file;
107
	h->line = lexlineno;
108 109 110 111 112 113 114 115 116 117 118
	h->offset = off;
	h->link = H;
	if(ehist == H) {
		hist = h;
		ehist = h;
		return;
	}
	ehist->link = h;
	ehist = h;
}

119
int32
Ken Thompson's avatar
Ken Thompson committed
120 121
setlineno(Node *n)
{
122
	int32 lno;
Ken Thompson's avatar
Ken Thompson committed
123 124

	lno = lineno;
125 126 127 128 129 130
	if(n != N)
	switch(n->op) {
	case ONAME:
	case OTYPE:
	case OPACK:
	case OLITERAL:
131
	case ONONAME:
132 133
		break;
	default:
Ken Thompson's avatar
Ken Thompson committed
134
		lineno = n->lineno;
Ken Thompson's avatar
Ken Thompson committed
135 136 137 138 139 140
		if(lineno == 0) {
			if(debug['K'])
				warn("setlineno: line 0");
			lineno = lno;
		}
	}
Ken Thompson's avatar
Ken Thompson committed
141 142 143
	return lno;
}

144
uint32
145 146
stringhash(char *p)
{
147
	int32 h;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	int c;

	h = 0;
	for(;;) {
		c = *p++;
		if(c == 0)
			break;
		h = h*PRIME1 + c;
	}

	if(h < 0) {
		h = -h;
		if(h < 0)
			h = 0;
	}
	return h;
}

Sym*
lookup(char *p)
{
	Sym *s;
170
	uint32 h;
171 172 173 174 175 176 177 178 179
	int c;

	h = stringhash(p) % NHASH;
	c = p[0];

	for(s = hash[h]; s != S; s = s->link) {
		if(s->name[0] != c)
			continue;
		if(strcmp(s->name, p) == 0)
Russ Cox's avatar
6g:  
Russ Cox committed
180
			if(s->package && strcmp(s->package, package) == 0)
181 182 183 184 185 186
				return s;
	}

	s = mal(sizeof(*s));
	s->name = mal(strlen(p)+1);
	s->package = package;
Russ Cox's avatar
Russ Cox committed
187
	s->lexical = LNAME;
188 189 190 191 192 193 194 195 196 197

	strcpy(s->name, p);

	s->link = hash[h];
	hash[h] = s;

	return s;
}

Sym*
198
pkglookup(char *name, char *pkg)
199 200
{
	Sym *s;
201
	uint32 h;
202 203
	int c;

204 205
	h = stringhash(name) % NHASH;
	c = name[0];
206 207 208
	for(s = hash[h]; s != S; s = s->link) {
		if(s->name[0] != c)
			continue;
209 210
		if(strcmp(s->name, name) == 0)
			if(s->package && strcmp(s->package, pkg) == 0)
211 212 213 214
				return s;
	}

	s = mal(sizeof(*s));
215 216
	s->name = mal(strlen(name)+1);
	strcpy(s->name, name);
217 218

	// botch - should probably try to reuse the pkg string
219 220
	s->package = mal(strlen(pkg)+1);
	strcpy(s->package, pkg);
221 222 223 224 225 226 227

	s->link = hash[h];
	hash[h] = s;

	return s;
}

228 229 230 231 232 233 234
Sym*
restrictlookup(char *name, char *pkg)
{
	if(!exportname(name) && strcmp(pkg, package) != 0)
		yyerror("cannot refer to %s.%s", pkg, name);
	return pkglookup(name, pkg);
}
235

236 237

// find all the exported symbols in package opkg
Russ Cox's avatar
6g:  
Russ Cox committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
// and make them available in the current package
void
importdot(Sym *opkg)
{
	Sym *s, *s1;
	uint32 h;
	int c;

	if(strcmp(opkg->name, package) == 0)
		return;

	c = opkg->name[0];
	for(h=0; h<NHASH; h++) {
		for(s = hash[h]; s != S; s = s->link) {
			if(s->package[0] != c)
				continue;
Russ Cox's avatar
Russ Cox committed
254
			if(!exportname(s->name) || utfrune(s->name, 0xb7))	// 0xb7 = center dot
255
				continue;
Russ Cox's avatar
6g:  
Russ Cox committed
256 257 258
			if(strcmp(s->package, opkg->name) != 0)
				continue;
			s1 = lookup(s->name);
Russ Cox's avatar
Russ Cox committed
259
			if(s1->def != N) {
260
				redeclare(s1, "during import");
Russ Cox's avatar
6g:  
Russ Cox committed
261 262
				continue;
			}
Russ Cox's avatar
Russ Cox committed
263
			s1->def = s->def;
Russ Cox's avatar
6g:  
Russ Cox committed
264 265 266 267
		}
	}
}

268 269 270 271
void
gethunk(void)
{
	char *h;
272
	int32 nh;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

	nh = NHUNK;
	if(thunk >= 10L*NHUNK)
		nh = 10L*NHUNK;
	h = (char*)malloc(nh);
	if(h == (char*)-1) {
		yyerror("out of memory");
		errorexit();
	}
	hunk = h;
	nhunk = nh;
	thunk += nh;
}

void*
288
mal(int32 n)
289 290 291
{
	void *p;

292
	while((uintptr)hunk & MAXALIGN) {
293 294 295 296 297 298 299 300 301 302 303 304 305 306
		hunk++;
		nhunk--;
	}
	while(nhunk < n)
		gethunk();

	p = hunk;
	nhunk -= n;
	hunk += n;
	memset(p, 0, n);
	return p;
}

void*
307
remal(void *p, int32 on, int32 n)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	void *q;

	q = (uchar*)p + on;
	if(q != hunk || nhunk < n) {
		while(nhunk < on+n)
			gethunk();
		memmove(hunk, p, on);
		p = hunk;
		hunk += on;
		nhunk -= on;
	}
	hunk += n;
	nhunk -= n;
	return p;
}

Node*
nod(int op, Node *nleft, Node *nright)
{
	Node *n;

	n = mal(sizeof(*n));
	n->op = op;
	n->left = nleft;
	n->right = nright;
334
	n->lineno = parserline();
Russ Cox's avatar
Russ Cox committed
335
	n->xoffset = BADWIDTH;
336 337 338
	return n;
}

Ken Thompson's avatar
Ken Thompson committed
339 340 341 342 343
int
algtype(Type *t)
{
	int a;

344
	if(issimple[t->etype] || isptr[t->etype] || t->etype == TCHAN || t->etype == TFUNC || t->etype == TMAP)
345
		a = AMEM;	// just bytes (int, ptr, etc)
346
	else if(t->etype == TSTRING)
Ken Thompson's avatar
Ken Thompson committed
347
		a = ASTRING;	// string
348 349
	else if(isnilinter(t))
		a = ANILINTER;	// nil interface
350
	else if(t->etype == TINTER)
Ken Thompson's avatar
Ken Thompson committed
351
		a = AINTER;	// interface
352 353
	else
		a = ANOEQ;	// just bytes, but no hash/eq
Ken Thompson's avatar
Ken Thompson committed
354 355 356
	return a;
}

357 358 359 360 361
Type*
maptype(Type *key, Type *val)
{
	Type *t;

Russ Cox's avatar
Russ Cox committed
362 363 364 365 366 367 368 369 370 371 372 373 374
	if(key != nil && key->etype != TANY && algtype(key) == ANOEQ) {
		if(key->etype == TFORW) {
			// map[key] used during definition of key.
			// postpone check until key is fully defined.
			// if there are multiple uses of map[key]
			// before key is fully defined, the error
			// will only be printed for the first one.
			// good enough.
			if(key->maplineno == 0)
				key->maplineno = lineno;
		} else
			yyerror("invalid map key type %T", key);
	}
375 376 377 378 379 380 381 382 383 384 385 386
	t = typ(TMAP);
	t->down = key;
	t->type = val;
	return t;
}

int
iskeytype(Type *t)
{
	return algtype(t) != ANOEQ;
}

387 388 389 390 391 392 393
Type*
typ(int et)
{
	Type *t;

	t = mal(sizeof(*t));
	t->etype = et;
394
	t->width = BADWIDTH;
395 396 397
	return t;
}

Russ Cox's avatar
Russ Cox committed
398 399 400

Type*
sortinter(Type *t)
401
{
Russ Cox's avatar
Russ Cox committed
402
	return t;
403 404 405
}

Node*
406
nodintconst(int64 v)
407 408 409 410 411
{
	Node *c;

	c = nod(OLITERAL, N, N);
	c->addable = 1;
Ken Thompson's avatar
Ken Thompson committed
412 413
	c->val.u.xval = mal(sizeof(*c->val.u.xval));
	mpmovecfix(c->val.u.xval, v);
414
	c->val.ctype = CTINT;
415
	c->type = types[TIDEAL];
416 417 418 419
	ullmancalc(c);
	return c;
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
void
nodconst(Node *n, Type *t, int64 v)
{
	memset(n, 0, sizeof(*n));
	n->op = OLITERAL;
	n->addable = 1;
	ullmancalc(n);
	n->val.u.xval = mal(sizeof(*n->val.u.xval));
	mpmovecfix(n->val.u.xval, v);
	n->val.ctype = CTINT;
	n->type = t;

	if(isfloat[t->etype])
		fatal("nodconst: bad type %T", t);
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
Node*
nodnil(void)
{
	Node *c;

	c = nodintconst(0);
	c->val.ctype = CTNIL;
	c->type = types[TNIL];
	return c;
}

Node*
nodbool(int b)
{
	Node *c;

	c = nodintconst(0);
	c->val.ctype = CTBOOL;
	c->val.u.bval = b;
	c->type = types[TBOOL];
	return c;
}

459 460 461
Type*
aindex(Node *b, Type *t)
{
462
	NodeList *init;
463
	Type *r;
Ken Thompson's avatar
Ken Thompson committed
464
	int bound;
465

466
	bound = -1;	// open bound
467
	init = nil;
Russ Cox's avatar
Russ Cox committed
468
	typecheck(&b, Erv);
469 470 471 472 473 474 475 476 477 478 479
	if(b != nil) {
		switch(consttype(b)) {
		default:
			yyerror("array bound must be an integer expression");
			break;
		case CTINT:
			bound = mpgetfix(b->val.u.xval);
			if(bound < 0)
				yyerror("array bound must be non negative");
			break;
		}
480
	}
Ken Thompson's avatar
Ken Thompson committed
481 482 483 484 485 486

	// fixed array
	r = typ(TARRAY);
	r->type = t;
	r->bound = bound;
	return r;
487 488 489 490 491 492 493 494 495 496 497 498
}

void
indent(int dep)
{
	int i;

	for(i=0; i<dep; i++)
		print(".   ");
}

void
499
dodumplist(NodeList *l, int dep)
500
{
501 502 503
	for(; l; l=l->next)
		dodump(l->n, dep);
}
504

505 506 507
void
dodump(Node *n, int dep)
{
508 509 510 511 512 513 514 515 516
	if(n == N)
		return;

	indent(dep);
	if(dep > 10) {
		print("...\n");
		return;
	}

517
	if(n->ninit != nil) {
Ken Thompson's avatar
Ken Thompson committed
518
		print("%O-init\n", n->op);
519
		dodumplist(n->ninit, dep+1);
Ken Thompson's avatar
Ken Thompson committed
520 521 522
		indent(dep);
	}

523 524 525
	switch(n->op) {
	default:
		print("%N\n", n);
526 527
		dodump(n->left, dep+1);
		dodump(n->right, dep+1);
528 529 530
		break;

	case OTYPE:
531 532 533 534 535 536
		print("%O %S type=%T\n", n->op, n->sym, n->type);
		if(n->type == T && n->ntype) {
			indent(dep);
			print("%O-ntype\n", n->op);
			dodump(n->ntype, dep+1);
		}
537 538 539 540 541
		break;

	case OIF:
		print("%O%J\n", n->op, n);
		dodump(n->ntest, dep+1);
542
		if(n->nbody != nil) {
543 544
			indent(dep);
			print("%O-then\n", n->op);
545
			dodumplist(n->nbody, dep+1);
546
		}
547
		if(n->nelse != nil) {
548 549
			indent(dep);
			print("%O-else\n", n->op);
550
			dodumplist(n->nelse, dep+1);
551
		}
552
		break;
553

Ken Thompson's avatar
Ken Thompson committed
554 555
	case OSELECT:
		print("%O%J\n", n->op, n);
556 557
		dodumplist(n->nbody, dep+1);
		break;
Ken Thompson's avatar
Ken Thompson committed
558

559 560 561 562 563
	case OSWITCH:
	case OFOR:
		print("%O%J\n", n->op, n);
		dodump(n->ntest, dep+1);

564
		if(n->nbody != nil) {
565 566
			indent(dep);
			print("%O-body\n", n->op);
567
			dodumplist(n->nbody, dep+1);
568 569 570 571 572 573 574
		}

		if(n->nincr != N) {
			indent(dep);
			print("%O-incr\n", n->op);
			dodump(n->nincr, dep+1);
		}
575
		break;
576 577

	case OCASE:
Ken Thompson's avatar
Ken Thompson committed
578 579 580 581 582
		// the right side points to label of the body
		if(n->right != N && n->right->op == OGOTO && n->right->left->op == ONAME)
			print("%O%J GOTO %N\n", n->op, n, n->right->left);
		else
			print("%O%J\n", n->op, n);
583
		dodump(n->left, dep+1);
584
		break;
Russ Cox's avatar
Russ Cox committed
585 586 587 588 589 590 591 592 593

	case OXCASE:
		print("%N\n", n);
		dodump(n->left, dep+1);
		dodump(n->right, dep+1);
		indent(dep);
		print("%O-nbody\n", n->op);
		dodumplist(n->nbody, dep+1);
		break;
594 595
	}

596
	if(0 && n->ntype != nil) {
597 598 599 600
		indent(dep);
		print("%O-ntype\n", n->op);
		dodump(n->ntype, dep+1);
	}
601 602 603 604 605 606 607 608 609 610
	if(n->list != nil) {
		indent(dep);
		print("%O-list\n", n->op);
		dodumplist(n->list, dep+1);
	}
	if(n->rlist != nil) {
		indent(dep);
		print("%O-rlist\n", n->op);
		dodumplist(n->rlist, dep+1);
	}
611 612 613 614 615
	if(n->nbody != nil) {
		indent(dep);
		print("%O-nbody\n", n->op);
		dodumplist(n->nbody, dep+1);
	}
616 617 618 619 620 621 622
}

void
dumplist(char *s, NodeList *l)
{
	print("%s\n", s);
	dodumplist(l, 1);
623 624 625 626 627
}

void
dump(char *s, Node *n)
{
628
	print("%s [%p]\n", s, n);
629 630 631
	dodump(n, 1);
}

Russ Cox's avatar
Russ Cox committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
static char*
goopnames[] =
{
	[OADDR]		= "&",
	[OADD]		= "+",
	[OANDAND]	= "&&",
	[OANDNOT]	= "&^",
	[OAND]		= "&",
	[OAS]		= "=",
	[OAS2]		= "=",
	[OBREAK]	= "break",
	[OCAP]		= "cap",
	[OCASE]		= "case",
	[OCLOSED]	= "closed",
	[OCLOSE]	= "close",
	[OCOM]		= "^",
	[OCONTINUE]	= "continue",
	[ODEC]		= "--",
	[ODEFER]	= "defer",
	[ODIV]		= "/",
	[OEQ]		= "==",
	[OFALL]		= "fallthrough",
	[OFOR]		= "for",
	[OFUNC]		= "func",
	[OGE]		= ">=",
	[OGOTO]		= "goto",
	[OGT]		= ">",
	[OIF]		= "if",
	[OINC]		= "++",
	[OIND]		= "*",
	[OLEN]		= "len",
	[OLE]		= "<=",
	[OLSH]		= "<<",
	[OLT]		= "<",
	[OMAKE]		= "make",
	[OMINUS]	= "-",
	[OMOD]		= "%",
	[OMUL]		= "*",
	[ONEW]		= "new",
	[ONE]		= "!=",
	[ONOT]		= "!",
	[OOROR]		= "||",
	[OOR]		= "|",
	[OPANICN]	= "panicln",
	[OPANIC]	= "panic",
	[OPLUS]		= "+",
	[OPRINTN]	= "println",
	[OPRINT]	= "print",
	[ORANGE]	= "range",
	[ORECV]		= "<-",
	[ORETURN]	= "return",
	[ORSH]		= ">>",
	[OSELECT]	= "select",
	[OSEND]		= "<-",
	[OSUB]		= "-",
	[OSWITCH]	= "switch",
	[OXOR]		= "^",
};

691 692 693 694 695 696 697
int
Oconv(Fmt *fp)
{
	char buf[500];
	int o;

	o = va_arg(fp->args, int);
Russ Cox's avatar
Russ Cox committed
698 699
	if((fp->flags & FmtSharp) && o >= 0 && o < nelem(goopnames) && goopnames[o] != nil)
		return fmtstrcpy(fp, goopnames[o]);
700 701 702 703 704 705 706
	if(o < 0 || o >= nelem(opnames) || opnames[o] == nil) {
		snprint(buf, sizeof(buf), "O-%d", o);
		return fmtstrcpy(fp, buf);
	}
	return fmtstrcpy(fp, opnames[o]);
}

707 708 709 710 711 712 713
int
Lconv(Fmt *fp)
{
	char str[STRINGSZ], s[STRINGSZ];
	struct
	{
		Hist*	incl;	/* start of this include file */
714
		int32	idel;	/* delta line number to apply to include */
715
		Hist*	line;	/* start of this #line directive */
716
		int32	ldel;	/* delta line number to apply to #line */
717
	} a[HISTSZ];
718
	int32 lno, d;
719 720 721
	int i, n;
	Hist *h;

722
	lno = va_arg(fp->args, int32);
723 724 725

	n = 0;
	for(h=hist; h!=H; h=h->link) {
Ken Thompson's avatar
Ken Thompson committed
726 727
		if(h->offset < 0)
			continue;
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
		if(lno < h->line)
			break;
		if(h->name) {
			if(n < HISTSZ) {	/* beginning of file */
				a[n].incl = h;
				a[n].idel = h->line;
				a[n].line = 0;
			}
			n++;
			continue;
		}
		n--;
		if(n > 0 && n < HISTSZ) {
			d = h->line - a[n].incl->line;
			a[n-1].ldel += d;
			a[n-1].idel += d;
		}
	}

	if(n > HISTSZ)
		n = HISTSZ;

	str[0] = 0;
	for(i=n-1; i>=0; i--) {
		if(i != n-1) {
			if(fp->flags & ~(FmtWidth|FmtPrec))
				break;
			strcat(str, " ");
		}
		if(a[i].line)
			snprint(s, STRINGSZ, "%s:%ld[%s:%ld]",
				a[i].line->name, lno-a[i].ldel+1,
				a[i].incl->name, lno-a[i].idel+1);
		else
			snprint(s, STRINGSZ, "%s:%ld",
				a[i].incl->name, lno-a[i].idel+1);
		if(strlen(s)+strlen(str) >= STRINGSZ-10)
			break;
		strcat(str, s);
		lno = a[i].incl->line - 1;	/* now print out start of this file */
	}
	if(n == 0)
Ken Thompson's avatar
Ken Thompson committed
770
		strcat(str, "<epoch>");
771 772 773 774

	return fmtstrcpy(fp, str);
}

775 776 777 778 779 780 781 782 783 784 785 786 787
/*
s%,%,\n%g
s%\n+%\n%g
s%^[ 	]*T%%g
s%,.*%%g
s%.+%	[T&]		= "&",%g
s%^	........*\]%&~%g
s%~	%%g
*/

static char*
etnames[] =
{
Ken Thompson's avatar
Ken Thompson committed
788 789
	[TINT]		= "INT",
	[TUINT]		= "UINT",
790 791 792 793 794 795 796 797
	[TINT8]		= "INT8",
	[TUINT8]	= "UINT8",
	[TINT16]	= "INT16",
	[TUINT16]	= "UINT16",
	[TINT32]	= "INT32",
	[TUINT32]	= "UINT32",
	[TINT64]	= "INT64",
	[TUINT64]	= "UINT64",
Ken Thompson's avatar
Ken Thompson committed
798 799
	[TUINTPTR]	= "UINTPTR",
	[TFLOAT]	= "FLOAT",
800 801 802 803 804 805
	[TFLOAT32]	= "FLOAT32",
	[TFLOAT64]	= "FLOAT64",
	[TFLOAT80]	= "FLOAT80",
	[TBOOL]		= "BOOL",
	[TPTR32]	= "PTR32",
	[TPTR64]	= "PTR64",
Ken Thompson's avatar
Ken Thompson committed
806
	[TDDD]		= "DDD",
807 808 809 810 811 812 813 814 815 816
	[TFUNC]		= "FUNC",
	[TARRAY]	= "ARRAY",
	[TSTRUCT]	= "STRUCT",
	[TCHAN]		= "CHAN",
	[TMAP]		= "MAP",
	[TINTER]	= "INTER",
	[TFORW]		= "FORW",
	[TFIELD]	= "FIELD",
	[TSTRING]	= "STRING",
	[TCHAN]		= "CHAN",
817
	[TANY]		= "ANY",
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
};

int
Econv(Fmt *fp)
{
	char buf[500];
	int et;

	et = va_arg(fp->args, int);
	if(et < 0 || et >= nelem(etnames) || etnames[et] == nil) {
		snprint(buf, sizeof(buf), "E-%d", et);
		return fmtstrcpy(fp, buf);
	}
	return fmtstrcpy(fp, etnames[et]);
}

int
Jconv(Fmt *fp)
{
	Node *n;

	n = va_arg(fp->args, Node*);
Russ Cox's avatar
Russ Cox committed
840 841
	if(n->ullman != 0)
		fmtprint(fp, " u(%d)", n->ullman);
842

Russ Cox's avatar
Russ Cox committed
843 844
	if(n->addable != 0)
		fmtprint(fp, " a(%d)", n->addable);
845

Russ Cox's avatar
Russ Cox committed
846 847
	if(n->vargen != 0)
		fmtprint(fp, " g(%ld)", n->vargen);
848

Russ Cox's avatar
Russ Cox committed
849 850
	if(n->lineno != 0)
		fmtprint(fp, " l(%ld)", n->lineno);
Russ Cox's avatar
Russ Cox committed
851

Russ Cox's avatar
Russ Cox committed
852 853
	if(n->xoffset != 0)
		fmtprint(fp, " x(%lld)", n->xoffset);
854

Russ Cox's avatar
Russ Cox committed
855 856
	if(n->class != 0)
		fmtprint(fp, " class(%d)", n->class);
857

Russ Cox's avatar
Russ Cox committed
858 859
	if(n->colas != 0)
		fmtprint(fp, " colas(%d)", n->colas);
Russ Cox's avatar
Russ Cox committed
860

Russ Cox's avatar
Russ Cox committed
861 862
	if(n->funcdepth != 0)
		fmtprint(fp, " f(%d)", n->funcdepth);
Russ Cox's avatar
Russ Cox committed
863

864 865 866
	if(n->typecheck != 0)
		fmtprint(fp, " tc(%d)", n->typecheck);

Russ Cox's avatar
Russ Cox committed
867
	return 0;
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
}

int
Gconv(Fmt *fp)
{
	char buf[100];
	Type *t;

	t = va_arg(fp->args, Type*);

	if(t->etype == TFUNC) {
		if(t->vargen != 0) {
			snprint(buf, sizeof(buf), "-%d%d%d g(%ld)",
				t->thistuple, t->outtuple, t->intuple, t->vargen);
			goto out;
		}
		snprint(buf, sizeof(buf), "-%d%d%d",
			t->thistuple, t->outtuple, t->intuple);
		goto out;
	}
	if(t->vargen != 0) {
		snprint(buf, sizeof(buf), " g(%ld)", t->vargen);
		goto out;
	}
	strcpy(buf, "");

out:
	return fmtstrcpy(fp, buf);
}

int
Sconv(Fmt *fp)
{
	Sym *s;
Russ Cox's avatar
6g:  
Russ Cox committed
902
	char *pkg, *nam;
903 904 905

	s = va_arg(fp->args, Sym*);
	if(s == S) {
906 907
		fmtstrcpy(fp, "<S>");
		return 0;
908 909 910 911 912 913 914 915 916 917
	}

	pkg = "<nil>";
	nam = pkg;

	if(s->package != nil)
		pkg = s->package;
	if(s->name != nil)
		nam = s->name;

Russ Cox's avatar
Russ Cox committed
918
	if(!(fp->flags & FmtShort))
Russ Cox's avatar
6g:  
Russ Cox committed
919 920
	if(strcmp(pkg, package) != 0 || (fp->flags & FmtLong)) {
		fmtprint(fp, "%s.%s", pkg, nam);
921
		return 0;
922
	}
923 924
	fmtstrcpy(fp, nam);
	return 0;
925 926
}

Ken Thompson's avatar
Ken Thompson committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
static char*
basicnames[] =
{
	[TINT]		= "int",
	[TUINT]		= "uint",
	[TINT8]		= "int8",
	[TUINT8]	= "uint8",
	[TINT16]	= "int16",
	[TUINT16]	= "uint16",
	[TINT32]	= "int32",
	[TUINT32]	= "uint32",
	[TINT64]	= "int64",
	[TUINT64]	= "uint64",
	[TUINTPTR]	= "uintptr",
	[TFLOAT]	= "float",
	[TFLOAT32]	= "float32",
	[TFLOAT64]	= "float64",
	[TFLOAT80]	= "float80",
	[TBOOL]		= "bool",
	[TANY]		= "any",
Ken Thompson's avatar
Ken Thompson committed
947
	[TDDD]		= "...",
Russ Cox's avatar
Russ Cox committed
948
	[TSTRING]		= "string",
949 950
	[TNIL]		= "nil",
	[TIDEAL]		= "ideal",
951 952 953 954 955 956
};

int
Tpretty(Fmt *fp, Type *t)
{
	Type *t1;
Russ Cox's avatar
6g:  
Russ Cox committed
957
	Sym *s;
958

Russ Cox's avatar
Russ Cox committed
959 960
	if(t->etype != TFIELD
	&& t->sym != S
Russ Cox's avatar
6g:  
Russ Cox committed
961 962
	&& !(fp->flags&FmtLong)) {
		s = t->sym;
Russ Cox's avatar
Russ Cox committed
963
		if(t == types[t->etype])
Russ Cox's avatar
6g:  
Russ Cox committed
964 965
			return fmtprint(fp, "%s", s->name);
		if(exporting) {
Russ Cox's avatar
Russ Cox committed
966 967 968 969
			if(fp->flags & FmtShort)
				fmtprint(fp, "%hS", s);
			else
				fmtprint(fp, "%lS", s);
Russ Cox's avatar
Russ Cox committed
970 971
			if(strcmp(s->package, package) != 0)
				return 0;
Russ Cox's avatar
Russ Cox committed
972
			if(s->flags & SymImported)
Russ Cox's avatar
Russ Cox committed
973
				return 0;
Russ Cox's avatar
Russ Cox committed
974
			if(t->vargen || !(s->flags & (SymExport|SymPackage))) {
Russ Cox's avatar
Russ Cox committed
975 976 977 978 979
				fmtprint(fp, "·%s", filename);
				if(t->vargen)
					fmtprint(fp, "·%d", t->vargen);
			}
			return 0;
Russ Cox's avatar
6g:  
Russ Cox committed
980 981 982
		}
		return fmtprint(fp, "%S", s);
	}
Russ Cox's avatar
Russ Cox committed
983

984 985
	if(t->etype < nelem(basicnames) && basicnames[t->etype] != nil)
		return fmtprint(fp, "%s", basicnames[t->etype]);
Russ Cox's avatar
Russ Cox committed
986

987 988 989
	switch(t->etype) {
	case TPTR32:
	case TPTR64:
Russ Cox's avatar
Russ Cox committed
990
		if(fp->flags&FmtShort)	// pass flag thru for methodsym
Russ Cox's avatar
Russ Cox committed
991 992
			return fmtprint(fp, "*%hT", t->type);
		return fmtprint(fp, "*%T", t->type);
993 994

	case TCHAN:
Russ Cox's avatar
Russ Cox committed
995 996 997 998
		switch(t->chan) {
		case Crecv:
			return fmtprint(fp, "<-chan %T", t->type);
		case Csend:
Russ Cox's avatar
Russ Cox committed
999 1000
			if(t->type != T && t->type->etype == TCHAN)
				return fmtprint(fp, "chan<- (%T)", t->type);
Russ Cox's avatar
Russ Cox committed
1001 1002 1003 1004
			return fmtprint(fp, "chan<- %T", t->type);
		}
		return fmtprint(fp, "chan %T", t->type);

1005
	case TMAP:
Russ Cox's avatar
Russ Cox committed
1006
		return fmtprint(fp, "map[%T] %T", t->down, t->type);
Russ Cox's avatar
Russ Cox committed
1007

1008
	case TFUNC:
Russ Cox's avatar
Russ Cox committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		// t->type is method struct
		// t->type->down is result struct
		// t->type->down->down is arg struct
		if(t->thistuple && !(fp->flags&FmtSharp) && !(fp->flags&FmtShort)) {
			fmtprint(fp, "method(");
			for(t1=getthisx(t)->type; t1; t1=t1->down) {
				fmtprint(fp, "%T", t1);
				if(t1->down)
					fmtprint(fp, ", ");
			}
			fmtprint(fp, ")");
		}

Russ Cox's avatar
Russ Cox committed
1022 1023
		if(!(fp->flags&FmtByte))
			fmtprint(fp, "func");
1024
		fmtprint(fp, "(");
Russ Cox's avatar
Russ Cox committed
1025
		for(t1=getinargx(t)->type; t1; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1026 1027 1028 1029
			if(noargnames && t1->etype == TFIELD)
				fmtprint(fp, "%T", t1->type);
			else
				fmtprint(fp, "%T", t1);
1030 1031 1032 1033
			if(t1->down)
				fmtprint(fp, ", ");
		}
		fmtprint(fp, ")");
Russ Cox's avatar
Russ Cox committed
1034 1035 1036 1037 1038
		switch(t->outtuple) {
		case 0:
			break;
		case 1:
			t1 = getoutargx(t)->type;
Russ Cox's avatar
Russ Cox committed
1039
			if(t1->etype != TFIELD && t1->etype != TFUNC) {
1040
				fmtprint(fp, " %T", t1);
Russ Cox's avatar
Russ Cox committed
1041
				break;
1042
			}
Russ Cox's avatar
Russ Cox committed
1043 1044 1045 1046
		default:
			t1 = getoutargx(t)->type;
			fmtprint(fp, " (");
			for(; t1; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1047 1048 1049 1050
				if(noargnames && t1->etype == TFIELD)
					fmtprint(fp, "%T", t1->type);
				else
					fmtprint(fp, "%T", t1);
Russ Cox's avatar
Russ Cox committed
1051 1052 1053 1054 1055
				if(t1->down)
					fmtprint(fp, ", ");
			}
			fmtprint(fp, ")");
			break;
1056 1057
		}
		return 0;
Russ Cox's avatar
Russ Cox committed
1058

1059 1060 1061 1062 1063 1064 1065 1066
	case TARRAY:
		if(t->bound >= 0)
			return fmtprint(fp, "[%d]%T", (int)t->bound, t->type);
		return fmtprint(fp, "[]%T", t->type);

	case TINTER:
		fmtprint(fp, "interface {");
		for(t1=t->type; t1!=T; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1067
			fmtprint(fp, " %hS %hhT", t1->sym, t1->type);
Russ Cox's avatar
Russ Cox committed
1068 1069
			if(t1->down)
				fmtprint(fp, ";");
1070
		}
Russ Cox's avatar
Russ Cox committed
1071 1072
		return fmtprint(fp, " }");

1073
	case TSTRUCT:
Russ Cox's avatar
Russ Cox committed
1074 1075 1076 1077 1078 1079 1080 1081 1082
		if(t->funarg) {
			fmtprint(fp, "(");
			for(t1=t->type; t1!=T; t1=t1->down) {
				fmtprint(fp, "%T", t1);
				if(t1->down)
					fmtprint(fp, ", ");
			}
			return fmtprint(fp, ")");
		}
1083 1084
		fmtprint(fp, "struct {");
		for(t1=t->type; t1!=T; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1085 1086 1087
			fmtprint(fp, " %T", t1);
			if(t1->down)
				fmtprint(fp, ";");
1088 1089
		}
		return fmtprint(fp, " }");
Russ Cox's avatar
Russ Cox committed
1090

1091
	case TFIELD:
1092
		if(t->sym == S || t->embedded) {
Russ Cox's avatar
Russ Cox committed
1093 1094
			if(exporting)
				fmtprint(fp, "? ");
1095 1096 1097 1098 1099 1100
			fmtprint(fp, "%T", t->type);
		} else
			fmtprint(fp, "%hS %T", t->sym, t->type);
		if(t->note)
			fmtprint(fp, " \"%Z\"", t->note);
		return 0;
Russ Cox's avatar
Russ Cox committed
1101 1102 1103 1104 1105 1106 1107

	case TFORW:
		if(exporting)
			yyerror("undefined type %S", t->sym);
		if(t->sym)
			return fmtprint(fp, "undefined %S", t->sym);
		return fmtprint(fp, "undefined");
1108 1109 1110 1111 1112 1113
	}

	// Don't know how to handle - fall back to detailed prints.
	return -1;
}

1114 1115 1116 1117
int
Tconv(Fmt *fp)
{
	Type *t, *t1;
Russ Cox's avatar
Russ Cox committed
1118 1119 1120 1121 1122
	int r, et, sharp, minus;

	sharp = (fp->flags & FmtSharp);
	minus = (fp->flags & FmtLeft);
	fp->flags &= ~(FmtSharp|FmtLeft);
Russ Cox's avatar
Russ Cox committed
1123

1124 1125 1126 1127 1128
	t = va_arg(fp->args, Type*);
	if(t == T)
		return fmtstrcpy(fp, "<T>");

	t->trecur++;
1129
	if(t->trecur > 5) {
Russ Cox's avatar
Russ Cox committed
1130
		fmtprint(fp, "...");
1131 1132 1133
		goto out;
	}

Russ Cox's avatar
6g:  
Russ Cox committed
1134
	if(!debug['t']) {
Russ Cox's avatar
Russ Cox committed
1135
		if(sharp)
Russ Cox's avatar
6g:  
Russ Cox committed
1136
			exporting++;
Russ Cox's avatar
Russ Cox committed
1137 1138 1139 1140 1141 1142 1143 1144
		if(minus)
			noargnames++;
		r = Tpretty(fp, t);
		if(sharp)
			exporting--;
		if(minus)
			noargnames--;
		if(r >= 0) {
Russ Cox's avatar
6g:  
Russ Cox committed
1145 1146 1147
			t->trecur--;
			return 0;
		}
1148 1149
	}

1150
	et = t->etype;
Russ Cox's avatar
Russ Cox committed
1151 1152 1153
	fmtprint(fp, "%E ", et);
	if(t->sym != S)
		fmtprint(fp, "<%S>", t->sym);
1154 1155 1156

	switch(et) {
	default:
Russ Cox's avatar
Russ Cox committed
1157 1158
		if(t->type != T)
			fmtprint(fp, " %T", t->type);
1159 1160 1161
		break;

	case TFIELD:
Russ Cox's avatar
Russ Cox committed
1162
		fmtprint(fp, "%T", t->type);
1163 1164 1165 1166
		break;

	case TFUNC:
		if(fp->flags & FmtLong)
Russ Cox's avatar
Russ Cox committed
1167
			fmtprint(fp, "%d%d%d(%lT,%lT)%lT",
1168 1169
				t->thistuple, t->intuple, t->outtuple,
				t->type, t->type->down->down, t->type->down);
1170
		else
Russ Cox's avatar
Russ Cox committed
1171
			fmtprint(fp, "%d%d%d(%T,%T)%T",
1172 1173
				t->thistuple, t->intuple, t->outtuple,
				t->type, t->type->down->down, t->type->down);
1174 1175 1176
		break;

	case TINTER:
Russ Cox's avatar
Russ Cox committed
1177 1178 1179 1180 1181
		fmtprint(fp, "{");
		if(fp->flags & FmtLong)
			for(t1=t->type; t1!=T; t1=t1->down)
				fmtprint(fp, "%lT;", t1);
		fmtprint(fp, "}");
1182 1183 1184
		break;

	case TSTRUCT:
Russ Cox's avatar
Russ Cox committed
1185 1186 1187 1188 1189
		fmtprint(fp, "{");
		if(fp->flags & FmtLong)
			for(t1=t->type; t1!=T; t1=t1->down)
				fmtprint(fp, "%lT;", t1);
		fmtprint(fp, "}");
1190 1191 1192
		break;

	case TMAP:
Russ Cox's avatar
Russ Cox committed
1193
		fmtprint(fp, "[%T]%T", t->down, t->type);
1194 1195 1196
		break;

	case TARRAY:
1197
		if(t->bound >= 0)
Russ Cox's avatar
Russ Cox committed
1198
			fmtprint(fp, "[%ld]%T", t->bound, t->type);
1199
		else
Russ Cox's avatar
Russ Cox committed
1200
			fmtprint(fp, "[]%T", t->type);
1201 1202 1203 1204
		break;

	case TPTR32:
	case TPTR64:
Russ Cox's avatar
Russ Cox committed
1205
		fmtprint(fp, "%T", t->type);
1206 1207 1208 1209 1210
		break;
	}

out:
	t->trecur--;
Russ Cox's avatar
Russ Cox committed
1211
	return 0;
1212 1213 1214 1215 1216
}

int
Nconv(Fmt *fp)
{
Russ Cox's avatar
Russ Cox committed
1217
	char buf1[500];
1218 1219 1220 1221
	Node *n;

	n = va_arg(fp->args, Node*);
	if(n == N) {
Russ Cox's avatar
Russ Cox committed
1222
		fmtprint(fp, "<N>");
1223 1224
		goto out;
	}
1225

Russ Cox's avatar
Russ Cox committed
1226
	if(fp->flags & FmtSign) {
Russ Cox's avatar
Russ Cox committed
1227 1228 1229
		if(n->type == T)
			fmtprint(fp, "%#N", n);
		else if(n->type->etype == TNIL)
Russ Cox's avatar
Russ Cox committed
1230 1231 1232 1233 1234 1235
			fmtprint(fp, "nil");
		else
			fmtprint(fp, "%#N (type %T)", n, n->type);
		goto out;
	}

Russ Cox's avatar
Russ Cox committed
1236 1237 1238 1239
	if(fp->flags & FmtSharp) {
		exprfmt(fp, n, 0);
		goto out;
	}
1240 1241 1242

	switch(n->op) {
	default:
Russ Cox's avatar
Russ Cox committed
1243
		fmtprint(fp, "%O%J", n->op, n);
1244 1245 1246
		break;

	case ONAME:
1247
	case ONONAME:
1248
		if(n->sym == S) {
Russ Cox's avatar
Russ Cox committed
1249
			fmtprint(fp, "%O%J", n->op, n);
1250 1251
			break;
		}
Russ Cox's avatar
Russ Cox committed
1252
		fmtprint(fp, "%O-%S G%ld%J", n->op,
Russ Cox's avatar
Russ Cox committed
1253
			n->sym, n->vargen, n);
1254 1255 1256
		goto ptyp;

	case OREGISTER:
Russ Cox's avatar
Russ Cox committed
1257
		fmtprint(fp, "%O-%R%J", n->op, n->val.u.reg, n);
1258 1259 1260 1261 1262
		break;

	case OLITERAL:
		switch(n->val.ctype) {
		default:
Ken Thompson's avatar
Ken Thompson committed
1263
			snprint(buf1, sizeof(buf1), "LITERAL-ctype=%d", n->val.ctype);
1264 1265
			break;
		case CTINT:
Ken Thompson's avatar
Ken Thompson committed
1266
			snprint(buf1, sizeof(buf1), "I%B", n->val.u.xval);
1267 1268
			break;
		case CTFLT:
Ken Thompson's avatar
Ken Thompson committed
1269
			snprint(buf1, sizeof(buf1), "F%g", mpgetflt(n->val.u.fval));
1270 1271
			break;
		case CTSTR:
Ken Thompson's avatar
Ken Thompson committed
1272
			snprint(buf1, sizeof(buf1), "S\"%Z\"", n->val.u.sval);
1273 1274
			break;
		case CTBOOL:
Ken Thompson's avatar
Ken Thompson committed
1275
			snprint(buf1, sizeof(buf1), "B%d", n->val.u.bval);
1276 1277 1278 1279 1280
			break;
		case CTNIL:
			snprint(buf1, sizeof(buf1), "N");
			break;
		}
Russ Cox's avatar
Russ Cox committed
1281
		fmtprint(fp, "%O-%s%J", n->op, buf1, n);
1282
		break;
1283

1284
	case OASOP:
Russ Cox's avatar
Russ Cox committed
1285
		fmtprint(fp, "%O-%O%J", n->op, n->etype, n);
1286 1287 1288
		break;

	case OTYPE:
Russ Cox's avatar
Russ Cox committed
1289
		fmtprint(fp, "%O %T", n->op, n->type);
1290 1291
		break;
	}
Russ Cox's avatar
Russ Cox committed
1292
	if(n->sym != S)
Russ Cox's avatar
Russ Cox committed
1293
		fmtprint(fp, " %S G%ld", n->sym, n->vargen);
1294 1295

ptyp:
Russ Cox's avatar
Russ Cox committed
1296 1297
	if(n->type != T)
		fmtprint(fp, " %T", n->type);
1298 1299

out:
Russ Cox's avatar
Russ Cox committed
1300
	return 0;
1301 1302
}

Ken Thompson's avatar
Ken Thompson committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
Node*
treecopy(Node *n)
{
	Node *m;

	if(n == N)
		return N;

	switch(n->op) {
	default:
		m = nod(OXXX, N, N);
		*m = *n;
		m->left = treecopy(n->left);
		m->right = treecopy(n->right);
1317
		m->list = listtreecopy(n->list);
1318 1319
		if(m->defn)
			abort();
Ken Thompson's avatar
Ken Thompson committed
1320 1321 1322 1323
		break;

	case OLITERAL:
		if(n->iota) {
1324
			m = nodintconst(iota);
Ken Thompson's avatar
Ken Thompson committed
1325 1326
			break;
		}
1327 1328
		// fall through
	case ONONAME:
Ken Thompson's avatar
Ken Thompson committed
1329
	case ONAME:
1330 1331
	case OTYPE:
		m = n;
Ken Thompson's avatar
Ken Thompson committed
1332 1333 1334 1335 1336
		break;
	}
	return m;
}

1337 1338 1339
int
Zconv(Fmt *fp)
{
1340
	Rune r;
1341
	Strlit *sp;
1342
	char *s, *se;
1343

1344
	sp = va_arg(fp->args, Strlit*);
1345 1346 1347
	if(sp == nil)
		return fmtstrcpy(fp, "<nil>");

1348 1349
	s = sp->s;
	se = s + sp->len;
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	while(s < se) {
		s += chartorune(&r, s);
		switch(r) {
		default:
			fmtrune(fp, r);
			break;
		case '\0':
			fmtstrcpy(fp, "\\x00");
			break;
		case '\t':
			fmtstrcpy(fp, "\\t");
			break;
		case '\n':
			fmtstrcpy(fp, "\\n");
			break;
		case '\"':
		case '\\':
			fmtrune(fp, '\\');
			fmtrune(fp, r);
			break;
		}
1371
	}
1372
	return 0;
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
}

int
isnil(Node *n)
{
	if(n == N)
		return 0;
	if(n->op != OLITERAL)
		return 0;
	if(n->val.ctype != CTNIL)
		return 0;
	return 1;
}

int
isptrto(Type *t, int et)
{
	if(t == T)
		return 0;
	if(!isptr[t->etype])
		return 0;
	t = t->type;
	if(t == T)
		return 0;
	if(t->etype != et)
		return 0;
	return 1;
}

Russ Cox's avatar
Russ Cox committed
1402 1403 1404 1405 1406 1407
int
istype(Type *t, int et)
{
	return t != T && t->etype == et;
}

1408
int
1409
isfixedarray(Type *t)
1410
{
1411
	return t != T && t->etype == TARRAY && t->bound >= 0;
1412 1413 1414
}

int
1415
isslice(Type *t)
1416
{
1417
	return t != T && t->etype == TARRAY && t->bound < 0;
Ken Thompson's avatar
Ken Thompson committed
1418 1419
}

Ken Thompson's avatar
Ken Thompson committed
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
int
isselect(Node *n)
{
	Sym *s;

	if(n == N)
		return 0;
	n = n->left;
	s = pkglookup("selectsend", "sys");
	if(s == n->sym)
		return 1;
	s = pkglookup("selectrecv", "sys");
Ken Thompson's avatar
Ken Thompson committed
1432 1433 1434
	if(s == n->sym)
		return 1;
	s = pkglookup("selectdefault", "sys");
Ken Thompson's avatar
Ken Thompson committed
1435 1436 1437 1438 1439
	if(s == n->sym)
		return 1;
	return 0;
}

1440 1441 1442
int
isinter(Type *t)
{
Ken Thompson's avatar
Ken Thompson committed
1443 1444 1445 1446 1447 1448
	if(t != T) {
		if(t->etype == TINTER)
			return 1;
		if(t->etype == TDDD)
			return 1;
	}
1449 1450 1451
	return 0;
}

Ken Thompson's avatar
Ken Thompson committed
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
int
isnilinter(Type *t)
{
	if(!isinter(t))
		return 0;
	if(t->type != T)
		return 0;
	return 1;
}

Ken Thompson's avatar
Ken Thompson committed
1462 1463 1464 1465 1466 1467 1468 1469
int
isddd(Type *t)
{
	if(t != T && t->etype == TDDD)
		return 1;
	return 0;
}

1470 1471 1472 1473
/*
 * given receiver of type t (t == r or t == *r)
 * return type to hang methods off (r).
 */
1474
Type*
1475
methtype(Type *t)
1476
{
1477
	int ptr;
1478

1479 1480 1481
	if(t == T)
		return T;

1482 1483 1484 1485 1486 1487 1488 1489 1490
	// strip away pointer if it's there
	ptr = 0;
	if(isptr[t->etype]) {
		if(t->sym != S)
			return T;
		ptr = 1;
		t = t->type;
		if(t == T)
			return T;
1491 1492
	}

1493 1494
	// need a type name
	if(t->sym == S)
1495 1496
		return T;

1497 1498 1499
	// check types
	if(!issimple[t->etype])
	switch(t->etype) {
1500
	default:
1501 1502 1503
		return T;
	case TSTRUCT:
	case TARRAY:
1504 1505 1506
	case TMAP:
	case TCHAN:
	case TSTRING:
Russ Cox's avatar
Russ Cox committed
1507
	case TFUNC:
1508 1509 1510 1511
		break;
	}

	return t;
1512 1513
}

1514 1515 1516 1517 1518 1519 1520 1521
int
iscomposite(Type *t)
{
	if(t == T)
		return 0;
	switch(t->etype) {
	case TARRAY:
	case TSTRUCT:
Russ Cox's avatar
Russ Cox committed
1522
	case TMAP:
1523 1524 1525 1526 1527
		return 1;
	}
	return 0;
}

1528
int
Russ Cox's avatar
6g:  
Russ Cox committed
1529
eqtype1(Type *t1, Type *t2, int d, int names)
1530 1531 1532 1533 1534 1535 1536 1537 1538
{
	if(d >= 10)
		return 1;
	if(t1 == t2)
		return 1;
	if(t1 == T || t2 == T)
		return 0;
	if(t1->etype != t2->etype)
		return 0;
Russ Cox's avatar
6g:  
Russ Cox committed
1539 1540
	if(names && t1->etype != TFIELD && t1->sym && t2->sym && t1 != t2)
		return 0;
1541 1542 1543 1544 1545 1546
	switch(t1->etype) {
	case TINTER:
	case TSTRUCT:
		t1 = t1->type;
		t2 = t2->type;
		for(;;) {
Russ Cox's avatar
6g:  
Russ Cox committed
1547
			if(!eqtype1(t1, t2, d+1, names))
1548 1549 1550
				return 0;
			if(t1 == T)
				return 1;
1551 1552
			if(t1->embedded != t2->embedded)
				return 0;
1553 1554 1555
			if(t1->nname != N && t1->nname->sym != S) {
				if(t2->nname == N || t2->nname->sym == S)
					return 0;
1556 1557
				if(strcmp(t1->nname->sym->name, t2->nname->sym->name) != 0)
					return 0;
1558 1559 1560 1561 1562 1563 1564
			}
			t1 = t1->down;
			t2 = t2->down;
		}
		return 1;

	case TFUNC:
1565
		// Loop over structs: receiver, in, out.
1566 1567 1568
		t1 = t1->type;
		t2 = t2->type;
		for(;;) {
1569
			Type *ta, *tb;
1570 1571 1572 1573 1574 1575 1576
			if(t1 == t2)
				break;
			if(t1 == T || t2 == T)
				return 0;
			if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
				return 0;

1577 1578 1579 1580 1581 1582 1583 1584
			// Loop over fields in structs, checking type only.
			ta = t1->type;
			tb = t2->type;
			while(ta != tb) {
				if(ta == T || tb == T)
					return 0;
				if(ta->etype != TFIELD || tb->etype != TFIELD)
					return 0;
Russ Cox's avatar
6g:  
Russ Cox committed
1585
				if(!eqtype1(ta->type, tb->type, d+1, names))
1586 1587 1588 1589
					return 0;
				ta = ta->down;
				tb = tb->down;
			}
1590 1591 1592 1593 1594

			t1 = t1->down;
			t2 = t2->down;
		}
		return 1;
Ken Thompson's avatar
Ken Thompson committed
1595 1596 1597 1598 1599

	case TARRAY:
		if(t1->bound == t2->bound)
			break;
		return 0;
Russ Cox's avatar
Russ Cox committed
1600 1601 1602 1603 1604

	case TCHAN:
		if(t1->chan == t2->chan)
			break;
		return 0;
1605
	}
Russ Cox's avatar
6g:  
Russ Cox committed
1606
	return eqtype1(t1->type, t2->type, d+1, names);
1607 1608 1609 1610 1611
}

int
eqtype(Type *t1, Type *t2)
{
Russ Cox's avatar
6g:  
Russ Cox committed
1612 1613 1614
	return eqtype1(t1, t2, 0, 1);
}

1615 1616 1617 1618
/*
 * can we convert from type src to dst with
 * a trivial conversion (no bits changing)?
 */
Russ Cox's avatar
6g:  
Russ Cox committed
1619
int
1620
cvttype(Type *dst, Type *src)
Russ Cox's avatar
6g:  
Russ Cox committed
1621
{
Russ Cox's avatar
Russ Cox committed
1622
	return eqtype1(dst, src, 0, 0);
1623 1624
}

Russ Cox's avatar
Russ Cox committed
1625 1626 1627 1628
int
eqtypenoname(Type *t1, Type *t2)
{
	if(t1 == T || t2 == T || t1->etype != TSTRUCT || t2->etype != TSTRUCT)
1629
		return eqtype(t1, t2);
Russ Cox's avatar
Russ Cox committed
1630 1631 1632 1633

	t1 = t1->type;
	t2 = t2->type;
	for(;;) {
1634
		if(!eqtype(t1, t2))
Russ Cox's avatar
Russ Cox committed
1635 1636 1637 1638 1639 1640 1641 1642
			return 0;
		if(t1 == T)
			return 1;
		t1 = t1->down;
		t2 = t2->down;
	}
}

1643
static int
Ken Thompson's avatar
Ken Thompson committed
1644
subtype(Type **stp, Type *t, int d)
1645 1646 1647 1648 1649 1650 1651
{
	Type *st;

loop:
	st = *stp;
	if(st == T)
		return 0;
Ken Thompson's avatar
Ken Thompson committed
1652 1653 1654 1655 1656

	d++;
	if(d >= 10)
		return 0;

1657 1658 1659 1660 1661 1662
	switch(st->etype) {
	default:
		return 0;

	case TPTR32:
	case TPTR64:
Ken Thompson's avatar
Ken Thompson committed
1663
	case TCHAN:
Ken Thompson's avatar
Ken Thompson committed
1664
	case TARRAY:
1665 1666 1667 1668
		stp = &st->type;
		goto loop;

	case TANY:
1669 1670
		if(!st->copyany)
			return 0;
1671 1672 1673 1674
		*stp = t;
		break;

	case TMAP:
Ken Thompson's avatar
Ken Thompson committed
1675
		if(subtype(&st->down, t, d))
1676 1677 1678 1679 1680 1681
			break;
		stp = &st->type;
		goto loop;

	case TFUNC:
		for(;;) {
Ken Thompson's avatar
Ken Thompson committed
1682
			if(subtype(&st->type, t, d))
1683
				break;
Ken Thompson's avatar
Ken Thompson committed
1684
			if(subtype(&st->type->down->down, t, d))
1685
				break;
Ken Thompson's avatar
Ken Thompson committed
1686
			if(subtype(&st->type->down, t, d))
1687 1688 1689 1690 1691 1692 1693
				break;
			return 0;
		}
		break;

	case TSTRUCT:
		for(st=st->type; st!=T; st=st->down)
Ken Thompson's avatar
Ken Thompson committed
1694
			if(subtype(&st->type, t, d))
1695 1696 1697 1698 1699 1700
				return 1;
		return 0;
	}
	return 1;
}

1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
/*
 * Is this a 64-bit type?
 */
int
is64(Type *t)
{
	if(t == T)
		return 0;
	switch(simtype[t->etype]) {
	case TINT64:
	case TUINT64:
	case TPTR64:
		return 1;
	}
	return 0;
}

/*
 * Is a conversion between t1 and t2 a no-op?
 */
int
noconv(Type *t1, Type *t2)
{
	int e1, e2;

	e1 = simtype[t1->etype];
	e2 = simtype[t2->etype];

	switch(e1) {
	case TINT8:
	case TUINT8:
		return e2 == TINT8 || e2 == TUINT8;

	case TINT16:
	case TUINT16:
		return e2 == TINT16 || e2 == TUINT16;

	case TINT32:
	case TUINT32:
	case TPTR32:
		return e2 == TINT32 || e2 == TUINT32 || e2 == TPTR32;

	case TINT64:
	case TUINT64:
	case TPTR64:
		return e2 == TINT64 || e2 == TUINT64 || e2 == TPTR64;

	case TFLOAT32:
		return e2 == TFLOAT32;

	case TFLOAT64:
		return e2 == TFLOAT64;
	}
	return 0;
}

1757 1758 1759
void
argtype(Node *on, Type *t)
{
Russ Cox's avatar
Russ Cox committed
1760
	dowidth(t);
Ken Thompson's avatar
Ken Thompson committed
1761
	if(!subtype(&on->type, t, 0))
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
		fatal("argtype: failed %N %T\n", on, t);
}

Type*
shallow(Type *t)
{
	Type *nt;

	if(t == T)
		return T;
	nt = typ(0);
	*nt = *t;
	return nt;
}

Type*
deep(Type *t)
{
	Type *nt, *xt;

	if(t == T)
		return T;

	switch(t->etype) {
	default:
		nt = t;	// share from here down
		break;

1790 1791 1792 1793 1794
	case TANY:
		nt = shallow(t);
		nt->copyany = 1;
		break;

1795 1796
	case TPTR32:
	case TPTR64:
Ken Thompson's avatar
chan  
Ken Thompson committed
1797
	case TCHAN:
Ken Thompson's avatar
Ken Thompson committed
1798
	case TARRAY:
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
		nt = shallow(t);
		nt->type = deep(t->type);
		break;

	case TMAP:
		nt = shallow(t);
		nt->down = deep(t->down);
		nt->type = deep(t->type);
		break;

	case TFUNC:
		nt = shallow(t);
		nt->type = deep(t->type);
		nt->type->down = deep(t->type->down);
		nt->type->down->down = deep(t->type->down->down);
		break;

	case TSTRUCT:
		nt = shallow(t);
		nt->type = shallow(t->type);
		xt = nt->type;

		for(t=t->type; t!=T; t=t->down) {
			xt->type = deep(t->type);
			xt->down = shallow(t->down);
			xt = xt->down;
		}
		break;
	}
	return nt;
}

Node*
syslook(char *name, int copy)
{
	Sym *s;
	Node *n;

	s = pkglookup(name, "sys");
Russ Cox's avatar
Russ Cox committed
1838
	if(s == S || s->def == N)
1839 1840 1841
		fatal("looksys: cant find sys.%s", name);

	if(!copy)
Russ Cox's avatar
Russ Cox committed
1842
		return s->def;
1843 1844

	n = nod(0, N, N);
Russ Cox's avatar
Russ Cox committed
1845 1846
	*n = *s->def;
	n->type = deep(s->def->type);
1847 1848 1849 1850

	return n;
}

1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
/*
 * are the arg names of two
 * functions the same. we know
 * that eqtype has been called
 * and has returned true.
 */
int
eqargs(Type *t1, Type *t2)
{
	if(t1 == t2)
		return 1;
	if(t1 == T || t2 == T)
		return 0;

	if(t1->etype != t2->etype)
		return 0;

	if(t1->etype != TFUNC)
		fatal("eqargs: oops %E", t1->etype);

	t1 = t1->type;
	t2 = t2->type;
	for(;;) {
		if(t1 == t2)
			break;
1876
		if(!eqtype(t1, t2))
1877 1878 1879 1880 1881 1882 1883
			return 0;
		t1 = t1->down;
		t2 = t2->down;
	}
	return 1;
}

1884
uint32
1885
typehash(Type *at, int addsym, int d)
1886
{
1887
	uint32 h;
1888 1889 1890 1891 1892 1893 1894 1895 1896
	Type *t;

	if(at == T)
		return PRIME2;
	if(d >= 5)
		return PRIME3;

	h = at->etype*PRIME4;

1897 1898 1899
	if(addsym && at->sym != S)
		h += stringhash(at->sym->name);

1900 1901
	switch(at->etype) {
	default:
1902
		h += PRIME5 * typehash(at->type, addsym, d+1);
1903 1904 1905 1906 1907
		break;

	case TINTER:
		// botch -- should be sorted?
		for(t=at->type; t!=T; t=t->down)
1908
			h += PRIME6 * typehash(t, addsym, d+1);
1909 1910 1911 1912
		break;

	case TSTRUCT:
		for(t=at->type; t!=T; t=t->down)
1913
			h += PRIME7 * typehash(t, addsym, d+1);
1914 1915 1916 1917
		break;

	case TFUNC:
		t = at->type;
Russ Cox's avatar
Russ Cox committed
1918
		// skip this (receiver) argument
1919 1920 1921
		if(t != T)
			t = t->down;
		for(; t!=T; t=t->down)
1922
			h += PRIME7 * typehash(t, addsym, d+1);
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
		break;
	}

	return h;
}

Type*
ptrto(Type *t)
{
	Type *t1;

	if(tptr == 0)
		fatal("ptrto: nil");
	t1 = typ(tptr);
	t1->type = t;
Ken Thompson's avatar
Ken Thompson committed
1938
	t1->width = types[tptr]->width;
1939 1940 1941 1942 1943 1944 1945
	return t1;
}

void
frame(int context)
{
	char *p;
Russ Cox's avatar
Russ Cox committed
1946 1947
	NodeList *l;
	Node *n;
1948 1949 1950
	int flag;

	p = "stack";
1951 1952 1953
	l = nil;
	if(curfn)
		l = curfn->dcl;
1954 1955
	if(context) {
		p = "external";
Russ Cox's avatar
Russ Cox committed
1956
		l = externdcl;
1957 1958 1959
	}

	flag = 1;
Russ Cox's avatar
Russ Cox committed
1960 1961 1962
	for(; l; l=l->next) {
		n = l->n;
		switch(n->op) {
1963 1964 1965
		case ONAME:
			if(flag)
				print("--- %s frame ---\n", p);
Russ Cox's avatar
Russ Cox committed
1966
			print("%O %S G%ld T\n", n->op, n->sym, n->vargen, n->type);
1967 1968 1969 1970 1971 1972
			flag = 0;
			break;

		case OTYPE:
			if(flag)
				print("--- %s frame ---\n", p);
Russ Cox's avatar
Russ Cox committed
1973
			print("%O %T\n", n->op, n->type);
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
			flag = 0;
			break;
		}
	}
}

/*
 * calculate sethi/ullman number
 * roughly how many registers needed to
 * compile a node. used to compile the
 * hardest side first to minimize registers.
 */
void
ullmancalc(Node *n)
{
	int ul, ur;

	if(n == N)
		return;

	switch(n->op) {
1995
	case OREGISTER:
1996 1997
	case OLITERAL:
	case ONAME:
1998
		ul = 1;
Russ Cox's avatar
Russ Cox committed
1999 2000
		if(n->class == PPARAMREF || (n->class & PHEAP))
			ul++;
2001 2002
		goto out;
	case OCALL:
Russ Cox's avatar
Russ Cox committed
2003
	case OCALLFUNC:
Ken Thompson's avatar
Ken Thompson committed
2004 2005
	case OCALLMETH:
	case OCALLINTER:
2006 2007 2008
		ul = UINF;
		goto out;
	}
2009
	ul = 1;
2010 2011
	if(n->left != N)
		ul = n->left->ullman;
2012
	ur = 1;
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
	if(n->right != N)
		ur = n->right->ullman;
	if(ul == ur)
		ul += 1;
	if(ur > ul)
		ul = ur;

out:
	n->ullman = ul;
}

void
badtype(int o, Type *tl, Type *tr)
{
2027
	yyerror("illegal types for operand: %O", o);
2028
	if(tl != T)
Russ Cox's avatar
Russ Cox committed
2029
		print("	%T\n", tl);
2030
	if(tr != T)
Russ Cox's avatar
Russ Cox committed
2031
		print("	%T\n", tr);
Russ Cox's avatar
Russ Cox committed
2032 2033 2034 2035 2036 2037 2038 2039

	// common mistake: *struct and *interface.
	if(tl && tr && isptr[tl->etype] && isptr[tr->etype]) {
		if(tl->type->etype == TSTRUCT && tr->type->etype == TINTER)
			print("	(*struct vs *interface)\n");
		else if(tl->type->etype == TINTER && tr->type->etype == TSTRUCT)
			print("	(*interface vs *struct)\n");
	}
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
}

/*
 * iterator to walk a structure declaration
 */
Type*
structfirst(Iter *s, Type **nn)
{
	Type *n, *t;

	n = *nn;
	if(n == T)
		goto bad;

	switch(n->etype) {
	default:
		goto bad;

	case TSTRUCT:
	case TINTER:
	case TFUNC:
		break;
	}

	t = n->type;
	if(t == T)
		goto rnil;

	if(t->etype != TFIELD)
		fatal("structfirst: not field %T", t);

	s->t = t;
	return t;

bad:
	fatal("structfirst: not struct %T", n);

rnil:
	return T;
}

Type*
structnext(Iter *s)
{
	Type *n, *t;

	n = s->t;
	t = n->down;
	if(t == T)
		goto rnil;

	if(t->etype != TFIELD)
		goto bad;

	s->t = t;
	return t;

bad:
	fatal("structnext: not struct %T", n);

rnil:
	return T;
}

/*
 * iterator to this and inargs in a function
 */
Type*
funcfirst(Iter *s, Type *t)
{
	Type *fp;

	if(t == T)
		goto bad;

	if(t->etype != TFUNC)
		goto bad;

	s->tfunc = t;
	s->done = 0;
	fp = structfirst(s, getthis(t));
	if(fp == T) {
		s->done = 1;
		fp = structfirst(s, getinarg(t));
	}
	return fp;

bad:
	fatal("funcfirst: not func %T", t);
	return T;
}

Type*
funcnext(Iter *s)
{
	Type *fp;

	fp = structnext(s);
	if(fp == T && !s->done) {
		s->done = 1;
		fp = structfirst(s, getinarg(s->tfunc));
	}
	return fp;
}

Type**
getthis(Type *t)
{
	if(t->etype != TFUNC)
2149
		fatal("getthis: not a func %T", t);
2150 2151 2152 2153 2154 2155 2156
	return &t->type;
}

Type**
getoutarg(Type *t)
{
	if(t->etype != TFUNC)
2157
		fatal("getoutarg: not a func %T", t);
2158 2159 2160 2161 2162 2163 2164
	return &t->type->down;
}

Type**
getinarg(Type *t)
{
	if(t->etype != TFUNC)
2165
		fatal("getinarg: not a func %T", t);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
	return &t->type->down->down;
}

Type*
getthisx(Type *t)
{
	return *getthis(t);
}

Type*
getoutargx(Type *t)
{
	return *getoutarg(t);
}

Type*
getinargx(Type *t)
{
	return *getinarg(t);
}
2186

2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
/*
 * return !(op)
 * eg == <=> !=
 */
int
brcom(int a)
{
	switch(a) {
	case OEQ:	return ONE;
	case ONE:	return OEQ;
	case OLT:	return OGE;
	case OGT:	return OLE;
	case OLE:	return OGT;
	case OGE:	return OLT;
	}
	fatal("brcom: no com for %A\n", a);
	return a;
}

/*
 * return reverse(op)
 * eg a op b <=> b r(op) a
 */
int
brrev(int a)
{
	switch(a) {
	case OEQ:	return OEQ;
	case ONE:	return ONE;
	case OLT:	return OGT;
	case OGT:	return OLT;
	case OLE:	return OGE;
	case OGE:	return OLE;
	}
	fatal("brcom: no rev for %A\n", a);
	return a;
}

2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
Node*
staticname(Type *t)
{
	Node *n;

	snprint(namebuf, sizeof(namebuf), "statictmp_%.4d·%s", statuniqgen, filename);
	statuniqgen++;
	n = newname(lookup(namebuf));
	addvar(n, t, PEXTERN);
	return n;
}

2237
/*
2238
 * return side effect-free n, appending side effects to init.
2239 2240
 */
Node*
2241
saferef(Node *n, NodeList **init)
2242 2243 2244
{
	Node *l;
	Node *r;
2245
	Node *a;
2246 2247 2248 2249 2250

	switch(n->op) {
	case ONAME:
		return n;
	case ODOT:
2251
		l = saferef(n->left, init);
2252 2253 2254 2255 2256
		if(l == n->left)
			return n;
		r = nod(OXXX, N, N);
		*r = *n;
		r->left = l;
Russ Cox's avatar
Russ Cox committed
2257
		typecheck(&r, Erv);
2258
		walkexpr(&r, init);
2259 2260 2261 2262 2263 2264 2265
		return r;

	case OINDEX:
	case ODOTPTR:
	case OIND:
		l = nod(OXXX, N, N);
		tempname(l, ptrto(n->type));
2266
		a = nod(OAS, l, nod(OADDR, n, N));
Russ Cox's avatar
Russ Cox committed
2267
		typecheck(&a, Etop);
2268
		walkexpr(&a, init);
2269
		*init = list(*init, a);
2270
		r = nod(OIND, l, N);
Russ Cox's avatar
Russ Cox committed
2271
		typecheck(&r, Erv);
2272
		walkexpr(&r, init);
2273 2274 2275 2276 2277 2278
		return r;
	}
	fatal("saferef %N", n);
	return N;
}

2279 2280 2281 2282 2283
void
setmaxarg(Type *t)
{
	int32 w;

2284
	dowidth(t);
2285
	w = t->argwid;
2286 2287
	if(t->argwid >= 100000000)
		fatal("bad argwid %T", t);
2288 2289 2290 2291
	if(w > maxarg)
		maxarg = w;
}

2292 2293 2294 2295 2296 2297 2298 2299 2300
/*
 * code to resolve elided DOTs
 * in embedded types
 */

// search depth 0 --
// return count of fields+methods
// found with a given name
int
Russ Cox's avatar
Russ Cox committed
2301
lookdot0(Sym *s, Type *t, Type **save)
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
{
	Type *f, *u;
	int c;

	u = t;
	if(isptr[u->etype])
		u = u->type;

	c = 0;
	if(u->etype == TSTRUCT || u->etype == TINTER) {
		for(f=u->type; f!=T; f=f->down)
Russ Cox's avatar
Russ Cox committed
2313 2314 2315
			if(f->sym == s) {
				if(save)
					*save = f;
2316
				c++;
Russ Cox's avatar
Russ Cox committed
2317
			}
2318 2319 2320 2321
	}
	u = methtype(t);
	if(u != T) {
		for(f=u->method; f!=T; f=f->down)
Russ Cox's avatar
Russ Cox committed
2322 2323 2324
			if(f->sym == s && f->embedded == 0) {
				if(save)
					*save = f;
2325
				c++;
Russ Cox's avatar
Russ Cox committed
2326
			}
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
	}
	return c;
}

// search depth d --
// return count of fields+methods
// found at search depth.
// answer is in dotlist array and
// count of number of ways is returned.
int
Russ Cox's avatar
Russ Cox committed
2337
adddot1(Sym *s, Type *t, int d, Type **save)
2338 2339 2340 2341 2342 2343 2344 2345 2346
{
	Type *f, *u;
	int c, a;

	if(t->trecur)
		return 0;
	t->trecur = 1;

	if(d == 0) {
Russ Cox's avatar
Russ Cox committed
2347
		c = lookdot0(s, t, save);
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
		goto out;
	}

	c = 0;
	u = t;
	if(isptr[u->etype])
		u = u->type;
	if(u->etype != TSTRUCT && u->etype != TINTER)
		goto out;

	d--;
	for(f=u->type; f!=T; f=f->down) {
		if(!f->embedded)
			continue;
		if(f->sym == S)
			continue;
Russ Cox's avatar
Russ Cox committed
2364
		a = adddot1(s, f->type, d, save);
Ken Thompson's avatar
Ken Thompson committed
2365 2366
		if(a != 0 && c == 0)
			dotlist[d].field = f;
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
		c += a;
	}

out:
	t->trecur = 0;
	return c;
}

// in T.field
// find missing fields that
// will give shortest unique addressing.
// modify the tree with missing type names.
Node*
adddot(Node *n)
{
	Type *t;
	Sym *s;
	int c, d;

Russ Cox's avatar
Russ Cox committed
2386
	typecheck(&n->left, Erv);
2387 2388
	t = n->left->type;
	if(t == T)
2389
		goto ret;
2390 2391

	if(n->right->op != ONAME)
2392
		goto ret;
2393 2394
	s = n->right->sym;
	if(s == S)
2395
		goto ret;
2396 2397

	for(d=0; d<nelem(dotlist); d++) {
Russ Cox's avatar
Russ Cox committed
2398
		c = adddot1(s, t, d, nil);
2399 2400 2401
		if(c > 0)
			goto out;
	}
2402
	goto ret;
2403 2404 2405

out:
	if(c > 1)
Russ Cox's avatar
Russ Cox committed
2406
		yyerror("ambiguous DOT reference %T.%S", t, s);
2407 2408

	// rebuild elided dots
2409 2410
	for(c=d-1; c>=0; c--)
		n->left = nod(ODOT, n->left, newname(dotlist[c].field->sym));
2411
ret:
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
	return n;
}


/*
 * code to help generate trampoline
 * functions for methods on embedded
 * subtypes.
 * these are approx the same as
 * the corresponding adddot routines
 * except that they expect to be called
 * with unique tasks and they return
 * the actual methods.
 */

typedef	struct	Symlink	Symlink;
struct	Symlink
{
	Type*		field;
	uchar		good;
2432
	uchar		followptr;
2433 2434 2435 2436
	Symlink*	link;
};
static	Symlink*	slist;

2437 2438
static void
expand0(Type *t, int followptr)
2439 2440 2441 2442 2443
{
	Type *f, *u;
	Symlink *sl;

	u = t;
2444 2445
	if(isptr[u->etype]) {
		followptr = 1;
2446
		u = u->type;
2447
	}
2448

2449 2450
	if(u->etype == TINTER) {
		for(f=u->type; f!=T; f=f->down) {
Russ Cox's avatar
6g:  
Russ Cox committed
2451
			if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0)
2452
				continue;
Russ Cox's avatar
Russ Cox committed
2453
			if(f->sym->flags & SymUniq)
2454
				continue;
Russ Cox's avatar
Russ Cox committed
2455
			f->sym->flags |= SymUniq;
2456 2457 2458 2459 2460 2461 2462 2463 2464
			sl = mal(sizeof(*sl));
			sl->field = f;
			sl->link = slist;
			sl->followptr = followptr;
			slist = sl;
		}
		return;
	}

2465 2466 2467
	u = methtype(t);
	if(u != T) {
		for(f=u->method; f!=T; f=f->down) {
Russ Cox's avatar
6g:  
Russ Cox committed
2468
			if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0)
2469
				continue;
Russ Cox's avatar
Russ Cox committed
2470
			if(f->sym->flags & SymUniq)
2471
				continue;
Russ Cox's avatar
Russ Cox committed
2472
			f->sym->flags |= SymUniq;
2473 2474 2475
			sl = mal(sizeof(*sl));
			sl->field = f;
			sl->link = slist;
2476
			sl->followptr = followptr;
2477 2478 2479 2480 2481
			slist = sl;
		}
	}
}

2482 2483
static void
expand1(Type *t, int d, int followptr)
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
{
	Type *f, *u;

	if(t->trecur)
		return;
	if(d == 0)
		return;
	t->trecur = 1;

	if(d != nelem(dotlist)-1)
2494
		expand0(t, followptr);
2495 2496

	u = t;
2497 2498
	if(isptr[u->etype]) {
		followptr = 1;
2499
		u = u->type;
2500
	}
2501 2502 2503 2504 2505 2506 2507 2508
	if(u->etype != TSTRUCT && u->etype != TINTER)
		goto out;

	for(f=u->type; f!=T; f=f->down) {
		if(!f->embedded)
			continue;
		if(f->sym == S)
			continue;
2509
		expand1(f->type, d-1, followptr);
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
	}

out:
	t->trecur = 0;
}

void
expandmeth(Sym *s, Type *t)
{
	Symlink *sl;
	Type *f;
	int c, d;

	if(s == S)
		return;
Russ Cox's avatar
Russ Cox committed
2525
	if(t == T || t->xmethod != nil)
2526 2527 2528 2529
		return;

	// generate all reachable methods
	slist = nil;
2530
	expand1(t, nelem(dotlist)-1, 0);
2531 2532 2533

	// check each method to be uniquely reachable
	for(sl=slist; sl!=nil; sl=sl->link) {
Russ Cox's avatar
Russ Cox committed
2534
		sl->field->sym->flags &= ~SymUniq;
2535
		for(d=0; d<nelem(dotlist); d++) {
Russ Cox's avatar
Russ Cox committed
2536
			c = adddot1(sl->field->sym, t, d, &f);
2537 2538
			if(c == 0)
				continue;
Russ Cox's avatar
Russ Cox committed
2539
			if(c == 1) {
2540
				sl->good = 1;
Russ Cox's avatar
Russ Cox committed
2541 2542
				sl->field = f;
			}
2543 2544 2545 2546
			break;
		}
	}

Russ Cox's avatar
Russ Cox committed
2547
	t->xmethod = t->method;
2548 2549 2550 2551 2552 2553
	for(sl=slist; sl!=nil; sl=sl->link) {
		if(sl->good) {
			// add it to the base type method list
			f = typ(TFIELD);
			*f = *sl->field;
			f->embedded = 1;	// needs a trampoline
2554 2555
			if(sl->followptr)
				f->embedded = 2;
Russ Cox's avatar
Russ Cox committed
2556 2557
			f->down = t->xmethod;
			t->xmethod = f;
2558 2559 2560 2561

		}
	}
}
Russ Cox's avatar
Russ Cox committed
2562 2563 2564 2565

/*
 * Given funarg struct list, return list of ODCLFIELD Node fn args.
 */
2566
NodeList*
Russ Cox's avatar
Russ Cox committed
2567 2568 2569
structargs(Type **tl, int mustname)
{
	Iter savet;
Russ Cox's avatar
Russ Cox committed
2570
	Node *a, *n;
2571
	NodeList *args;
Russ Cox's avatar
Russ Cox committed
2572
	Type *t;
Russ Cox's avatar
Russ Cox committed
2573 2574
	char buf[100];
	int gen;
Russ Cox's avatar
Russ Cox committed
2575

2576
	args = nil;
Russ Cox's avatar
Russ Cox committed
2577
	gen = 0;
Russ Cox's avatar
Russ Cox committed
2578
	for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) {
Russ Cox's avatar
Russ Cox committed
2579
		n = N;
Russ Cox's avatar
Russ Cox committed
2580
		if(t->sym)
Russ Cox's avatar
Russ Cox committed
2581
			n = newname(t->sym);
Russ Cox's avatar
Russ Cox committed
2582 2583
		else if(mustname) {
			// have to give it a name so we can refer to it in trampoline
Russ Cox's avatar
Russ Cox committed
2584 2585 2586
			snprint(buf, sizeof buf, ".anon%d", gen++);
			n = newname(lookup(buf));
		}
2587
		a = nod(ODCLFIELD, n, typenod(t->type));
Russ Cox's avatar
Russ Cox committed
2588 2589 2590 2591 2592 2593
		args = list(args, a);
	}
	return args;
}

/*
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
 * Generate a wrapper function to convert from
 * a receiver of type T to a receiver of type U.
 * That is,
 *
 *	func (t T) M() {
 *		...
 *	}
 *
 * already exists; this function generates
 *
 *	func (u U) M() {
 *		u.M()
 *	}
 *
 * where the types T and U are such that u.M() is valid
 * and calls the T.M method.
 * The resulting function is for use in method tables.
Russ Cox's avatar
Russ Cox committed
2611
 *
Russ Cox's avatar
Russ Cox committed
2612
 *	rcvr - U
2613 2614
 *	method - M func (t T)(), a TFIELD type struct
 *	newnam - the eventual mangled name of this function
Russ Cox's avatar
Russ Cox committed
2615 2616
 */
void
Russ Cox's avatar
Russ Cox committed
2617
genwrapper(Type *rcvr, Type *method, Sym *newnam)
Russ Cox's avatar
Russ Cox committed
2618
{
2619
	Node *this, *fn, *call, *n, *t;
2620
	NodeList *l, *args, *in, *out;
Russ Cox's avatar
Russ Cox committed
2621

Russ Cox's avatar
Russ Cox committed
2622
	if(debug['r'])
2623
		print("genwrapper rcvrtype=%T method=%T newnam=%S\n",
Russ Cox's avatar
Russ Cox committed
2624
			rcvr, method, newnam);
Russ Cox's avatar
Russ Cox committed
2625 2626 2627 2628

	dclcontext = PEXTERN;
	markdcl();

2629 2630
	this = nod(ODCLFIELD, newname(lookup(".this")), typenod(rcvr));
	this->left->ntype = this->right;
2631 2632
	in = structargs(getinarg(method->type), 1);
	out = structargs(getoutarg(method->type), 0);
Russ Cox's avatar
Russ Cox committed
2633 2634 2635

	fn = nod(ODCLFUNC, N, N);
	fn->nname = newname(newnam);
2636 2637 2638 2639
	t = nod(OTFUNC, this, N);
	t->list = in;
	t->rlist = out;
	fn->nname->ntype = t;
Russ Cox's avatar
Russ Cox committed
2640 2641 2642
	funchdr(fn);

	// arg list
2643 2644 2645
	args = nil;
	for(l=in; l; l=l->next)
		args = list(args, l->n->left);
Russ Cox's avatar
Russ Cox committed
2646

2647
	// generate call
2648
	call = nod(OCALL, adddot(nod(OXDOT, this->left, newname(method->sym))), N);
2649 2650 2651 2652 2653 2654 2655
	call->list = args;
	fn->nbody = list1(call);
	if(method->type->outtuple > 0) {
		n = nod(ORETURN, N, N);
		n->list = fn->nbody;
		fn->nbody = list1(n);
	}
Russ Cox's avatar
Russ Cox committed
2656 2657

	if(debug['r'])
2658
		dumplist("genwrapper body", fn->nbody);
Russ Cox's avatar
Russ Cox committed
2659 2660

	funcbody(fn);
2661 2662
	typecheck(&fn, Etop);
	funccompile(fn);
Russ Cox's avatar
Russ Cox committed
2663 2664
}

2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
/*
 * delayed interface type check.
 * remember that there is an interface conversion
 * on the given line.  once the file is completely read
 * and all methods are known, we can check that
 * the conversions are valid.
 */

typedef struct Icheck Icheck;
struct Icheck
{
	Icheck *next;
	Type *dst;
	Type *src;
	int lineno;
2680
	int explicit;
2681 2682 2683 2684 2685
};
Icheck *icheck;
Icheck *ichecktail;

void
2686
ifacecheck(Type *dst, Type *src, int lineno, int explicit)
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
{
	Icheck *p;

	p = mal(sizeof *p);
	if(ichecktail)
		ichecktail->next = p;
	else
		icheck = p;
	p->dst = dst;
	p->src = src;
	p->lineno = lineno;
2698
	p->explicit = explicit;
2699 2700 2701 2702
	ichecktail = p;
}

Type*
2703
ifacelookdot(Sym *s, Type *t, int *followptr)
2704
{
2705
	int i, c, d;
2706 2707
	Type *m;

2708 2709
	*followptr = 0;

2710 2711 2712
	if(t == T)
		return T;

2713 2714 2715 2716 2717 2718
	for(d=0; d<nelem(dotlist); d++) {
		c = adddot1(s, t, d, &m);
		if(c > 1) {
			yyerror("%T.%S is ambiguous", t, s);
			return T;
		}
2719 2720 2721 2722 2723 2724 2725
		if(c == 1) {
			for(i=0; i<d; i++) {
				if(isptr[dotlist[i].field->type->etype]) {
					*followptr = 1;
					break;
				}
			}
2726
			return m;
2727
		}
2728 2729 2730 2731
	}
	return T;
}

2732 2733
// check whether non-interface type t
// satisifes inteface type iface.
2734
int
2735
ifaceokT2I(Type *t0, Type *iface, Type **m)
2736
{
2737 2738
	Type *t, *im, *tm, *rcvr;
	int imhash, followptr;
2739

2740 2741
	t = methtype(t0);

2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
	// if this is too slow,
	// could sort these first
	// and then do one loop.

	// could also do full type compare
	// instead of using hash, but have to
	// avoid checking receivers, and
	// typehash already does that for us.
	// also, it's what the runtime will do,
	// so we can both be wrong together.

	for(im=iface->type; im; im=im->down) {
2754
		imhash = typehash(im, 0, 0);
2755
		tm = ifacelookdot(im->sym, t, &followptr);
2756
		if(tm == T || typehash(tm, 0, 0) != imhash) {
2757 2758 2759
			*m = im;
			return 0;
		}
2760 2761 2762
		// if pointer receiver in method,
		// the method does not exist for value types.
		rcvr = getthisx(tm->type)->type->type;
2763
		if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm)) {
2764 2765 2766 2767 2768
			if(debug['r'])
				yyerror("interface pointer mismatch");
			*m = im;
			return 0;
		}
2769 2770 2771 2772
	}
	return 1;
}

2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
// check whether interface type i1 satisifes interface type i2.
int
ifaceokI2I(Type *i1, Type *i2, Type **m)
{
	Type *m1, *m2;

	// if this is too slow,
	// could sort these first
	// and then do one loop.

	for(m2=i2->type; m2; m2=m2->down) {
		for(m1=i1->type; m1; m1=m1->down)
2785
			if(m1->sym == m2->sym && typehash(m1, 0, 0) == typehash(m2, 0, 0))
2786 2787 2788 2789 2790 2791 2792 2793
				goto found;
		*m = m2;
		return 0;
	found:;
	}
	return 1;
}

2794 2795 2796 2797
void
runifacechecks(void)
{
	Icheck *p;
2798 2799
	int lno, wrong, needexplicit;
	Type *m, *t, *iface;
2800 2801 2802 2803

	lno = lineno;
	for(p=icheck; p; p=p->next) {
		lineno = p->lineno;
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
		wrong = 0;
		needexplicit = 0;
		m = nil;
		if(isinter(p->dst) && isinter(p->src)) {
			iface = p->dst;
			t = p->src;
			needexplicit = !ifaceokI2I(t, iface, &m);
		}
		else if(isinter(p->dst)) {
			t = p->src;
			iface = p->dst;
			wrong = !ifaceokT2I(t, iface, &m);
2816
		} else {
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
			t = p->dst;
			iface = p->src;
			wrong = !ifaceokT2I(t, iface, &m);
			needexplicit = 1;
		}
		if(wrong)
			yyerror("%T is not %T\n\tmissing %S%hhT",
				t, iface, m->sym, m->type);
		else if(!p->explicit && needexplicit) {
			if(m)
2827
				yyerror("need type assertion to use %T as %T\n\tmissing %S%hhT",
2828 2829
					p->src, p->dst, m->sym, m->type);
			else
2830
				yyerror("need type assertion to use %T as %T",
2831
					p->src, p->dst);
2832 2833 2834 2835
		}
	}
	lineno = lno;
}
Russ Cox's avatar
Russ Cox committed
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846

/*
 * even simpler simtype; get rid of ptr, bool.
 * assuming that the front end has rejected
 * all the invalid conversions (like ptr -> bool)
 */
int
simsimtype(Type *t)
{
	int et;

Russ Cox's avatar
Russ Cox committed
2847 2848 2849
	if(t == 0)
		return 0;

Russ Cox's avatar
Russ Cox committed
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
	et = simtype[t->etype];
	switch(et) {
	case TPTR32:
		et = TUINT32;
		break;
	case TPTR64:
		et = TUINT64;
		break;
	case TBOOL:
		et = TUINT8;
		break;
	}
	return et;
}

2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
NodeList*
concat(NodeList *a, NodeList *b)
{
	if(a == nil)
		return b;
	if(b == nil)
		return a;

	a->end->next = b;
	a->end = b->end;
	b->end = nil;
	return a;
}

NodeList*
list1(Node *n)
{
	NodeList *l;

	if(n == nil)
		return nil;
	if(n->op == OBLOCK && n->ninit == nil)
		return n->list;
	l = mal(sizeof *l);
	l->n = n;
	l->end = l;
	return l;
}

NodeList*
list(NodeList *l, Node *n)
{
	return concat(l, list1(n));
}

NodeList*
listtreecopy(NodeList *l)
{
	NodeList *out;

	out = nil;
	for(; l; l=l->next)
		out = list(out, treecopy(l->n));
	return out;
}

Node*
liststmt(NodeList *l)
{
	Node *n;

	n = nod(OBLOCK, N, N);
	n->list = l;
	if(l)
		n->lineno = l->n->lineno;
	return n;
}

Russ Cox's avatar
Russ Cox committed
2923 2924 2925
/*
 * return nelem of list
 */
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935
int
count(NodeList *l)
{
	int n;

	n = 0;
	for(; l; l=l->next)
		n++;
	return n;
}
Russ Cox's avatar
Russ Cox committed
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990

/*
 * return nelem of list
 */
int
structcount(Type *t)
{
	int v;
	Iter s;

	v = 0;
	for(t = structfirst(&s, &t); t != T; t = structnext(&s))
		v++;
	return v;
}

/*
 * when a type's width should be known, we call checkwidth
 * to compute it.  during a declaration like
 *
 *	type T *struct { next T }
 *
 * it is necessary to defer the calculation of the struct width
 * until after T has been initialized to be a pointer to that struct.
 * similarly, during import processing structs may be used
 * before their definition.  in those situations, calling
 * defercheckwidth() stops width calculations until
 * resumecheckwidth() is called, at which point all the
 * checkwidths that were deferred are executed.
 * sometimes it is okay to
 */
typedef struct TypeList TypeList;
struct TypeList {
	Type *t;
	TypeList *next;
};

static TypeList *tlfree;
static TypeList *tlq;
static int defercalc;

void
checkwidth(Type *t)
{
	TypeList *l;

	// function arg structs should not be checked
	// outside of the enclosing function.
	if(t->funarg)
		fatal("checkwidth %T", t);

	if(!defercalc) {
		dowidth(t);
		return;
	}
2991 2992 2993
	if(t->deferwidth)
		return;
	t->deferwidth = 1;
Russ Cox's avatar
Russ Cox committed
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024

	l = tlfree;
	if(l != nil)
		tlfree = l->next;
	else
		l = mal(sizeof *l);

	l->t = t;
	l->next = tlq;
	tlq = l;
}

void
defercheckwidth(void)
{
	// we get out of sync on syntax errors, so don't be pedantic.
	// if(defercalc)
	//	fatal("defercheckwidth");
	defercalc = 1;
}

void
resumecheckwidth(void)
{
	TypeList *l;

	if(!defercalc)
		fatal("restartcheckwidth");
	defercalc = 0;

	for(l = tlq; l != nil; l = tlq) {
3025
		l->t->deferwidth = 0;
Russ Cox's avatar
Russ Cox committed
3026 3027 3028 3029 3030 3031 3032
		dowidth(l->t);
		tlq = l->next;
		l->next = tlfree;
		tlfree = l;
	}
}

3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
/*
 * return power of 2 of the constant
 * operand. -1 if it is not a power of 2.
 * 1000+ if it is a -(power of 2)
 */
int
powtwo(Node *n)
{
	uvlong v, b;
	int i;

	if(n == N || n->op != OLITERAL || n->type == T)
		goto no;
	if(!isint[n->type->etype])
		goto no;

	v = mpgetfix(n->val.u.xval);
	b = 1ULL;
	for(i=0; i<64; i++) {
		if(b == v)
			return i;
		b = b<<1;
	}

	if(!issigned[n->type->etype])
		goto no;

	v = -v;
	b = 1ULL;
	for(i=0; i<64; i++) {
		if(b == v)
			return i+1000;
		b = b<<1;
	}

no:
	return -1;
}

/*
 * return the unsigned type for
 * a signed integer type.
 * returns T if input is not a
 * signed integer type.
 */
Type*
tounsigned(Type *t)
{

	// this is types[et+1], but not sure
	// that this relation is immutable
	switch(t->etype) {
	default:
		print("tounsigned: unknown type %T\n", t);
		t = T;
		break;
	case TINT:
		t = types[TUINT];
		break;
	case TINT8:
		t = types[TUINT8];
		break;
	case TINT16:
		t = types[TUINT16];
		break;
	case TINT32:
		t = types[TUINT32];
		break;
	case TINT64:
		t = types[TUINT64];
		break;
	}
	return t;
}

/*
 * magic number for signed division
 * see hacker's delight chapter 10
 */
void
smagic(Magic *m)
{
	int p;
	uint64 ad, anc, delta, q1, r1, q2, r2, t;
	uint64 mask, two31;

	m->bad = 0;
	switch(m->w) {
	default:
		m->bad = 1;
		return;
	case 8:
		mask = 0xffLL;
		break;
	case 16:
		mask = 0xffffLL;
		break;
	case 32:
		mask = 0xffffffffLL;
		break;
	case 64:
		mask = 0xffffffffffffffffLL;
		break;
	}
	two31 = mask ^ (mask>>1);

	p = m->w-1;
	ad = m->sd;
	if(m->sd < 0)
		ad = -m->sd;

	// bad denominators
	if(ad == 0 || ad == 1 || ad == two31) {
		m->bad = 1;
		return;
	}

	t = two31;
	ad &= mask;

	anc = t - 1 - t%ad;
	anc &= mask;

	q1 = two31/anc;
	r1 = two31 - q1*anc;
	q1 &= mask;
	r1 &= mask;

	q2 = two31/ad;
	r2 = two31 - q2*ad;
	q2 &= mask;
	r2 &= mask;

	for(;;) {
		p++;
		q1 <<= 1;
		r1 <<= 1;
		q1 &= mask;
		r1 &= mask;
		if(r1 >= anc) {
			q1++;
			r1 -= anc;
			q1 &= mask;
			r1 &= mask;
		}

		q2 <<= 1;
		r2 <<= 1;
		q2 &= mask;
		r2 &= mask;
		if(r2 >= ad) {
			q2++;
			r2 -= ad;
			q2 &= mask;
			r2 &= mask;
		}

		delta = ad - r2;
		delta &= mask;
		if(q1 < delta || (q1 == delta && r1 == 0)) {
			continue;
		}
		break;
	}

	m->sm = q2+1;
	if(m->sm & two31)
		m->sm |= ~mask;
	m->s = p-m->w;
}

/*
 * magic number for unsigned division
 * see hacker's delight chapter 10
 */
void
umagic(Magic *m)
{
	int p;
	uint64 nc, delta, q1, r1, q2, r2;
	uint64 mask, two31;

	m->bad = 0;
	m->ua = 0;

	switch(m->w) {
	default:
		m->bad = 1;
		return;
	case 8:
		mask = 0xffLL;
		break;
	case 16:
		mask = 0xffffLL;
		break;
	case 32:
		mask = 0xffffffffLL;
		break;
	case 64:
		mask = 0xffffffffffffffffLL;
		break;
	}
	two31 = mask ^ (mask>>1);

	m->ud &= mask;
	if(m->ud == 0 || m->ud == two31) {
		m->bad = 1;
		return;
	}
	nc = mask - (-m->ud&mask)%m->ud;
	p = m->w-1;

	q1 = two31/nc;
	r1 = two31 - q1*nc;
	q1 &= mask;
	r1 &= mask;

	q2 = (two31-1) / m->ud;
	r2 = (two31-1) - q2*m->ud;
	q2 &= mask;
	r2 &= mask;

	for(;;) {
		p++;
		if(r1 >= nc-r1) {
			q1 <<= 1;
			q1++;
			r1 <<= 1;
			r1 -= nc;
		} else {
			q1 <<= 1;
			r1 <<= 1;
		}
		q1 &= mask;
		r1 &= mask;
		if(r2+1 >= m->ud-r2) {
			if(q2 >= two31-1) {
				m->ua = 1;
			}
			q2 <<= 1;
			q2++;
			r2 <<= 1;
			r2++;
			r2 -= m->ud;
		} else {
			if(q2 >= two31) {
				m->ua = 1;
			}
			q2 <<= 1;
			r2 <<= 1;
			r2++;
		}
		q2 &= mask;
		r2 &= mask;

		delta = m->ud - 1 - r2;
		delta &= mask;

		if(p < m->w+m->w)
		if(q1 < delta || (q1 == delta && r1 == 0)) {
			continue;
		}
		break;
	}
	m->um = q2+1;
	m->s = p-m->w;
}