gen.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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.

/*
 * portable half of code generator.
 * mainly statements and control flow.
 */

#include "go.h"

Russ Cox's avatar
Russ Cox committed
12 13 14
static void	cgen_dcl(Node *n);
static void	cgen_proc(Node *n, int proc);

15 16 17 18 19
Node*
sysfunc(char *name)
{
	Node *n;

20
	n = newname(pkglookup(name, runtimepkg));
21 22 23 24 25 26 27
	n->class = PFUNC;
	return n;
}

void
allocparams(void)
{
Russ Cox's avatar
Russ Cox committed
28
	NodeList *l;
29 30
	Node *n;
	uint32 w;
31
	Sym *s;
32
	int lno;
33 34 35

	if(stksize < 0)
		fatal("allocparams not during code generation");
36 37 38 39 40 41

	/*
	 * allocate (set xoffset) the stack
	 * slots for all automatics.
	 * allocated starting at -w down.
	 */
42
	lno = lineno;
43
	for(l=curfn->dcl; l; l=l->next) {
Russ Cox's avatar
Russ Cox committed
44
		n = l->n;
45 46 47 48 49 50 51
		if(n->op == ONAME && n->class == PHEAP-1) {
			// heap address variable; finish the job
			// started in addrescapes.
			s = n->sym;
			tempname(n, n->type);
			n->sym = s;
		}
Russ Cox's avatar
Russ Cox committed
52
		if(n->op != ONAME || n->class != PAUTO)
53
			continue;
Luuk van Dijk's avatar
Luuk van Dijk committed
54 55
		if (n->xoffset != BADWIDTH)
			continue;
Russ Cox's avatar
Russ Cox committed
56 57
		if(n->type == T)
			continue;
58 59
		dowidth(n->type);
		w = n->type->width;
60
		if(w >= MAXWIDTH)
61
			fatal("bad width");
62
		stksize += w;
Russ Cox's avatar
Russ Cox committed
63
		stksize = rnd(stksize, n->type->align);
Russ Cox's avatar
Russ Cox committed
64 65
		if(thechar == '5')
			stksize = rnd(stksize, widthptr);
66 67
		n->xoffset = -stksize;
	}
68
	lineno = lno;
69 70
}

Russ Cox's avatar
Russ Cox committed
71 72 73 74 75 76 77 78 79 80 81 82
void
clearlabels(void)
{
	Label *l;

	for(l=labellist; l!=L; l=l->link)
		l->sym->label = L;
	
	labellist = L;
	lastlabel = L;
}

Russ Cox's avatar
Russ Cox committed
83
static void
Russ Cox's avatar
Russ Cox committed
84
newlab(int op, Node *nlab, Node *stmt)
85 86
{
	Label *lab;
Russ Cox's avatar
Russ Cox committed
87 88 89 90 91
	Sym *s;
	int32 lno;
	
	s = nlab->left->sym;
	lno = nlab->left->lineno;
92 93

	lab = mal(sizeof(*lab));
Russ Cox's avatar
Russ Cox committed
94 95 96 97 98
	if(lastlabel == nil)
		labellist = lab;
	else
		lastlabel->link = lab;
	lastlabel = lab;
99

Russ Cox's avatar
Russ Cox committed
100
	lab->lineno = lno;
101 102 103
	lab->sym = s;
	lab->op = op;
	lab->label = pc;
Russ Cox's avatar
Russ Cox committed
104
	lab->stmt = stmt;
Russ Cox's avatar
Russ Cox committed
105 106 107 108 109 110 111
	if(op == OLABEL) {
		if(s->label != L) {
			lineno = lno;
			yyerror("label %S already defined at %L", s, s->label->lineno);
		} else
			s->label = lab;
	}	
112 113 114 115 116
}

void
checklabels(void)
{
Russ Cox's avatar
Russ Cox committed
117
	Label *l;
118
	Sym *s;
Russ Cox's avatar
Russ Cox committed
119
	int lno;
120

Russ Cox's avatar
Russ Cox committed
121 122 123
	lno = lineno;
	
	// resolve goto using syms
124
	for(l=labellist; l!=L; l=l->link) {
Russ Cox's avatar
Russ Cox committed
125 126
		switch(l->op) {
		case OGOTO:
127
			s = l->sym;
Russ Cox's avatar
Russ Cox committed
128 129 130 131
			if(s->label == L) {
				lineno = l->lineno;
				yyerror("label %S not defined", s);
				break;
132
			}
Russ Cox's avatar
Russ Cox committed
133 134 135
			s->label->used = 1;
			patch(l->label, s->label->label);
			break;
136 137
		}
	}
Russ Cox's avatar
Russ Cox committed
138 139 140 141 142 143 144 145 146 147
	
	// diagnose unused labels
	for(l=labellist; l!=L; l=l->link) {
		if(l->op == OLABEL && !l->used) {
			lineno = l->lineno;
			yyerror("label %S defined and not used", l->sym);
		}
	}
	
	lineno = lno;
148 149 150 151 152
}

/*
 * compile statements
 */
153 154 155 156 157 158 159
void
genlist(NodeList *l)
{
	for(; l; l=l->next)
		gen(l->n);
}

160 161 162 163 164 165 166
void
gen(Node *n)
{
	int32 lno;
	Prog *scontin, *sbreak;
	Prog *p1, *p2, *p3;
	Label *lab;
Russ Cox's avatar
Russ Cox committed
167
	int32 wasregalloc;
168 169

	lno = setlineno(n);
Russ Cox's avatar
Russ Cox committed
170
	wasregalloc = anyregalloc();
171 172 173 174 175 176

	if(n == N)
		goto ret;

	p3 = pc;	// save pc for loop labels
	if(n->ninit)
177
		genlist(n->ninit);
178 179 180 181 182 183 184 185 186 187 188 189

	setlineno(n);

	switch(n->op) {
	default:
		fatal("gen: unknown op %N", n);
		break;

	case OCASE:
	case OFALL:
	case OXCASE:
	case OXFALL:
190 191 192
	case ODCLCONST:
	case ODCLFUNC:
	case ODCLTYPE:
Russ Cox's avatar
Russ Cox committed
193 194
		break;

195
	case OEMPTY:
Russ Cox's avatar
Russ Cox committed
196 197 198
		// insert no-op so that
		//	L:; for { }
		// does not treat L as a label for the loop.
Russ Cox's avatar
Russ Cox committed
199
		if(lastlabel != L && lastlabel->label == p3)
Russ Cox's avatar
Russ Cox committed
200
			gused(N);
201 202
		break;

203 204 205 206
	case OBLOCK:
		genlist(n->list);
		break;

207
	case OLABEL:
Russ Cox's avatar
Russ Cox committed
208
		newlab(OLABEL, n, n->right);
209 210 211
		break;

	case OGOTO:
Russ Cox's avatar
Russ Cox committed
212
		hasgoto = 1;
Russ Cox's avatar
Russ Cox committed
213
		newlab(OGOTO, n, N);
214 215 216 217 218
		gjmp(P);
		break;

	case OBREAK:
		if(n->left != N) {
Russ Cox's avatar
Russ Cox committed
219 220
			lab = n->left->sym->label;
			if(lab == L) {
221
				yyerror("break label not defined: %S", n->left->sym);
Russ Cox's avatar
Russ Cox committed
222 223 224 225 226 227 228 229
				break;
			}
			lab->used = 1;
			if(lab->breakpc == P) {
				yyerror("invalid break label %S", n->left->sym);
				break;
			}
			gjmp(lab->breakpc);
230 231 232 233 234 235 236 237 238 239 240
			break;
		}
		if(breakpc == P) {
			yyerror("break is not in a loop");
			break;
		}
		gjmp(breakpc);
		break;

	case OCONTINUE:
		if(n->left != N) {
Russ Cox's avatar
Russ Cox committed
241 242
			lab = n->left->sym->label;
			if(lab == L) {
Russ Cox's avatar
Russ Cox committed
243
				yyerror("continue label not defined: %S", n->left->sym);
Russ Cox's avatar
Russ Cox committed
244 245 246 247 248 249 250 251
				break;
			}
			lab->used = 1;
			if(lab->continpc == P) {
				yyerror("invalid continue label %S", n->left->sym);
				break;
			}
			gjmp(lab->continpc);
252 253 254
			break;
		}
		if(continpc == P) {
Russ Cox's avatar
Russ Cox committed
255
			yyerror("continue is not in a loop");
256 257 258 259 260 261 262
			break;
		}
		gjmp(continpc);
		break;

	case OFOR:
		sbreak = breakpc;
Russ Cox's avatar
Russ Cox committed
263
		p1 = gjmp(P);			//		goto test
264 265 266 267
		breakpc = gjmp(P);		// break:	goto done
		scontin = continpc;
		continpc = pc;

Russ Cox's avatar
Russ Cox committed
268
		// define break and continue labels
Russ Cox's avatar
Russ Cox committed
269
		if((lab = lastlabel) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n) {
Russ Cox's avatar
Russ Cox committed
270 271
			lab->breakpc = breakpc;
			lab->continpc = continpc;
Russ Cox's avatar
Russ Cox committed
272 273
		} else
			lab = L;
274 275 276 277

		gen(n->nincr);				// contin:	incr
		patch(p1, pc);				// test:
		bgen(n->ntest, 0, breakpc);		//		if(!test) goto break
278
		genlist(n->nbody);				//		body
279 280 281 282
		gjmp(continpc);
		patch(breakpc, pc);			// done:
		continpc = scontin;
		breakpc = sbreak;
Russ Cox's avatar
Russ Cox committed
283 284 285 286
		if(lab) {
			lab->breakpc = P;
			lab->continpc = P;
		}
287 288 289 290 291 292
		break;

	case OIF:
		p1 = gjmp(P);			//		goto test
		p2 = gjmp(P);			// p2:		goto else
		patch(p1, pc);				// test:
Russ Cox's avatar
Russ Cox committed
293
		bgen(n->ntest, 0, p2);			//		if(!test) goto p2
294
		genlist(n->nbody);				//		then
295 296
		p3 = gjmp(P);			//		goto done
		patch(p2, pc);				// else:
297
		genlist(n->nelse);				//		else
298 299 300 301 302
		patch(p3, pc);				// done:
		break;

	case OSWITCH:
		sbreak = breakpc;
Russ Cox's avatar
Russ Cox committed
303
		p1 = gjmp(P);			//		goto test
304 305 306
		breakpc = gjmp(P);		// break:	goto done

		// define break label
Russ Cox's avatar
Russ Cox committed
307
		if((lab = lastlabel) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n)
Russ Cox's avatar
Russ Cox committed
308
			lab->breakpc = breakpc;
Russ Cox's avatar
Russ Cox committed
309 310
		else
			lab = L;
311 312

		patch(p1, pc);				// test:
313
		genlist(n->nbody);				//		switch(test) body
314 315
		patch(breakpc, pc);			// done:
		breakpc = sbreak;
Russ Cox's avatar
Russ Cox committed
316 317
		if(lab != L)
			lab->breakpc = P;
318 319 320 321
		break;

	case OSELECT:
		sbreak = breakpc;
Russ Cox's avatar
Russ Cox committed
322
		p1 = gjmp(P);			//		goto test
323 324 325
		breakpc = gjmp(P);		// break:	goto done

		// define break label
Russ Cox's avatar
Russ Cox committed
326
		if((lab = lastlabel) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n)
Russ Cox's avatar
Russ Cox committed
327
			lab->breakpc = breakpc;
Russ Cox's avatar
Russ Cox committed
328 329
		else
			lab = L;
330 331

		patch(p1, pc);				// test:
332
		genlist(n->nbody);				//		select() body
333 334
		patch(breakpc, pc);			// done:
		breakpc = sbreak;
Russ Cox's avatar
Russ Cox committed
335 336
		if(lab != L)
			lab->breakpc = P;
337 338 339 340 341 342 343 344 345 346 347
		break;

	case OASOP:
		cgen_asop(n);
		break;

	case ODCL:
		cgen_dcl(n->left);
		break;

	case OAS:
Ken Thompson's avatar
Ken Thompson committed
348 349
		if(gen_as_init(n))
			break;
350 351 352 353 354 355 356 357 358 359 360
		cgen_as(n->left, n->right);
		break;

	case OCALLMETH:
		cgen_callmeth(n, 0);
		break;

	case OCALLINTER:
		cgen_callinter(n, N, 0);
		break;

Russ Cox's avatar
Russ Cox committed
361
	case OCALLFUNC:
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		cgen_call(n, 0);
		break;

	case OPROC:
		cgen_proc(n, 1);
		break;

	case ODEFER:
		cgen_proc(n, 2);
		break;

	case ORETURN:
		cgen_ret(n);
		break;
	}

ret:
Russ Cox's avatar
Russ Cox committed
379 380 381 382 383
	if(anyregalloc() != wasregalloc) {
		dump("node", n);
		fatal("registers left allocated");
	}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
	lineno = lno;
}

/*
 * generate call to non-interface method
 *	proc=0	normal call
 *	proc=1	goroutine run in new proc
 *	proc=2	defer call save away stack
 */
void
cgen_callmeth(Node *n, int proc)
{
	Node *l;

	// generate a rewrite for method call
	// (p.f)(...) goes to (f)(p,...)

	l = n->left;
	if(l->op != ODOTMETH)
		fatal("cgen_callmeth: not dotmethod: %N");

Russ Cox's avatar
Russ Cox committed
405
	n->op = OCALLFUNC;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	n->left = n->left->right;
	n->left->type = l->type;

	if(n->left->op == ONAME)
		n->left->class = PFUNC;
	cgen_call(n, proc);
}

/*
 * generate code to start new proc running call n.
 */
void
cgen_proc(Node *n, int proc)
{
	switch(n->left->op) {
	default:
		fatal("cgen_proc: unknown call %O", n->left->op);

	case OCALLMETH:
		cgen_callmeth(n->left, proc);
		break;

	case OCALLINTER:
		cgen_callinter(n->left, N, proc);
		break;

Russ Cox's avatar
Russ Cox committed
432
	case OCALLFUNC:
433 434 435 436 437 438 439 440 441 442 443 444
		cgen_call(n->left, proc);
		break;
	}

}

/*
 * generate declaration.
 * nothing to do for on-stack automatics,
 * but might have to allocate heap copy
 * for escaped variables.
 */
Russ Cox's avatar
Russ Cox committed
445
static void
446 447 448 449 450 451 452 453 454 455
cgen_dcl(Node *n)
{
	if(debug['g'])
		dump("\ncgen-dcl", n);
	if(n->op != ONAME) {
		dump("cgen_dcl", n);
		fatal("cgen_dcl");
	}
	if(!(n->class & PHEAP))
		return;
Russ Cox's avatar
Russ Cox committed
456 457
	if(n->alloc == nil)
		n->alloc = callnew(n->type);
458 459 460
	cgen_as(n->heapaddr, n->alloc);
}

Russ Cox's avatar
Russ Cox committed
461 462 463
/*
 * generate discard of value
 */
Russ Cox's avatar
Russ Cox committed
464
static void
Russ Cox's avatar
Russ Cox committed
465 466 467 468 469 470 471 472 473
cgen_discard(Node *nr)
{
	Node tmp;

	if(nr == N)
		return;

	switch(nr->op) {
	case ONAME:
Russ Cox's avatar
Russ Cox committed
474
		if(!(nr->class & PHEAP) && nr->class != PEXTERN && nr->class != PFUNC && nr->class != PPARAMREF)
Russ Cox's avatar
Russ Cox committed
475
			gused(nr);
Russ Cox's avatar
Russ Cox committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		break;

	// unary
	case OADD:
	case OAND:
	case ODIV:
	case OEQ:
	case OGE:
	case OGT:
	case OLE:
	case OLSH:
	case OLT:
	case OMOD:
	case OMUL:
	case ONE:
	case OOR:
	case ORSH:
	case OSUB:
	case OXOR:
		cgen_discard(nr->left);
		cgen_discard(nr->right);
		break;

	// binary
	case OCAP:
	case OCOM:
	case OLEN:
	case OMINUS:
	case ONOT:
	case OPLUS:
		cgen_discard(nr->left);
		break;

	// special enough to just evaluate
	default:
		tempname(&tmp, nr->type);
		cgen_as(&tmp, nr);
		gused(&tmp);
	}
}

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
/*
 * generate assignment:
 *	nl = nr
 * nr == N means zero nl.
 */
void
cgen_as(Node *nl, Node *nr)
{
	Node nc;
	Type *tl;
	int iszer;

	if(nl == N)
		return;

	if(debug['g']) {
		dump("cgen_as", nl);
		dump("cgen_as = ", nr);
	}

Russ Cox's avatar
Russ Cox committed
537 538 539 540 541
	if(isblank(nl)) {
		cgen_discard(nr);
		return;
	}

542 543
	iszer = 0;
	if(nr == N || isnil(nr)) {
Ken Thompson's avatar
Ken Thompson committed
544
		// externals and heaps should already be clear
545 546 547 548 549 550 551
		if(nr == N) {
			if(nl->class == PEXTERN)
				return;
			if(nl->class & PHEAP)
				return;
		}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
		tl = nl->type;
		if(tl == T)
			return;
		if(isfat(tl)) {
			clearfat(nl);
			goto ret;
		}

		/* invent a "zero" for the rhs */
		iszer = 1;
		nr = &nc;
		memset(nr, 0, sizeof(*nr));
		switch(simtype[tl->etype]) {
		default:
			fatal("cgen_as: tl %T", tl);
			break;

		case TINT8:
		case TUINT8:
		case TINT16:
		case TUINT16:
		case TINT32:
		case TUINT32:
		case TINT64:
		case TUINT64:
			nr->val.u.xval = mal(sizeof(*nr->val.u.xval));
			mpmovecfix(nr->val.u.xval, 0);
			nr->val.ctype = CTINT;
			break;

		case TFLOAT32:
		case TFLOAT64:
			nr->val.u.fval = mal(sizeof(*nr->val.u.fval));
			mpmovecflt(nr->val.u.fval, 0.0);
			nr->val.ctype = CTFLT;
			break;

		case TBOOL:
			nr->val.u.bval = 0;
			nr->val.ctype = CTBOOL;
			break;

		case TPTR32:
		case TPTR64:
			nr->val.ctype = CTNIL;
			break;

Ken Thompson's avatar
Ken Thompson committed
599 600 601 602 603 604
		case TCOMPLEX64:
		case TCOMPLEX128:
			nr->val.u.cval = mal(sizeof(*nr->val.u.cval));
			mpmovecflt(&nr->val.u.cval->real, 0.0);
			mpmovecflt(&nr->val.u.cval->imag, 0.0);
			break;
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
		}
		nr->op = OLITERAL;
		nr->type = tl;
		nr->addable = 1;
		ullmancalc(nr);
	}

	tl = nl->type;
	if(tl == T)
		return;

	cgen(nr, nl);
	if(iszer && nl->addable)
		gused(nl);

ret:
	;
}
623 624 625 626 627 628 629 630 631 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

/*
 * gather series of offsets
 * >=0 is direct addressed field
 * <0 is pointer to next field (+1)
 */
int
dotoffset(Node *n, int *oary, Node **nn)
{
	int i;

	switch(n->op) {
	case ODOT:
		if(n->xoffset == BADWIDTH) {
			dump("bad width in dotoffset", n);
			fatal("bad width in dotoffset");
		}
		i = dotoffset(n->left, oary, nn);
		if(i > 0) {
			if(oary[i-1] >= 0)
				oary[i-1] += n->xoffset;
			else
				oary[i-1] -= n->xoffset;
			break;
		}
		if(i < 10)
			oary[i++] = n->xoffset;
		break;

	case ODOTPTR:
		if(n->xoffset == BADWIDTH) {
			dump("bad width in dotoffset", n);
			fatal("bad width in dotoffset");
		}
		i = dotoffset(n->left, oary, nn);
		if(i < 10)
			oary[i++] = -(n->xoffset+1);
		break;

	default:
		*nn = n;
		return 0;
	}
	if(i >= 10)
		*nn = N;
	return i;
}

/*
 * make a new off the books
 */
void
Luuk van Dijk's avatar
Luuk van Dijk committed
675
tempname(Node *nn, Type *t)
676
{
Luuk van Dijk's avatar
Luuk van Dijk committed
677
	Node *n;
678 679 680 681 682 683
	Sym *s;
	uint32 w;

	if(stksize < 0)
		fatal("tempname not during code generation");

Luuk van Dijk's avatar
Luuk van Dijk committed
684 685 686
	if (curfn == N)
		fatal("no curfn for tempname");

687 688 689 690 691 692 693 694 695 696
	if(t == T) {
		yyerror("tempname called with nil type");
		t = types[TINT32];
	}

	// give each tmp a different name so that there
	// a chance to registerizer them
	snprint(namebuf, sizeof(namebuf), "autotmp_%.4d", statuniqgen);
	statuniqgen++;
	s = lookup(namebuf);
Luuk van Dijk's avatar
Luuk van Dijk committed
697
	n = nod(ONAME, N, N);
698 699 700 701 702 703
	n->sym = s;
	n->type = t;
	n->class = PAUTO;
	n->addable = 1;
	n->ullman = 1;
	n->noescape = 1;
Luuk van Dijk's avatar
Luuk van Dijk committed
704 705
	n->curfn = curfn;
	curfn->dcl = list(curfn->dcl, n);
706 707 708 709

	dowidth(t);
	w = t->width;
	stksize += w;
Russ Cox's avatar
Russ Cox committed
710
	stksize = rnd(stksize, t->align);
Russ Cox's avatar
Russ Cox committed
711 712
	if(thechar == '5')
		stksize = rnd(stksize, widthptr);
713
	n->xoffset = -stksize;
Luuk van Dijk's avatar
Luuk van Dijk committed
714 715 716 717

	//	print("\ttmpname (%d): %N\n", stksize, n);

	*nn = *n;
718
}