walk.c 50.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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"

static	Type*	sw1(Node*, Type*);
static	Type*	sw2(Node*, Type*);
static	Type*	sw3(Node*, Type*);
static	Node*	curfn;
Ken Thompson's avatar
Ken Thompson committed
11
static	Node*	addtop;
12

Ken Thompson's avatar
Ken Thompson committed
13 14 15 16 17 18 19 20
enum
{
	Inone,
	I2T,
	I2I,
	T2I
};

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// can this code branch reach the end
// without an undcontitional RETURN
// this is hard, so it is conservative
int
walkret(Node *n)
{

loop:
	if(n != N)
	switch(n->op) {
	case OLIST:
		if(n->right == N) {
			n = n->left;
			goto loop;
		}
		n = n->right;
		goto loop;

	// at this point, we have the last
	// statement of the function

	case OGOTO:
	case OPANIC:
	case ORETURN:
		return 0;
	}

	// all other statements
	// will flow to the end
	return 1;
}

53 54 55
void
walk(Node *fn)
{
Ken Thompson's avatar
Ken Thompson committed
56 57
	char s[50];

58
	curfn = fn;
Ken Thompson's avatar
Ken Thompson committed
59 60
	if(debug['W']) {
		snprint(s, sizeof(s), "\nbefore %S", curfn->nname->sym);
61
		dump(s, curfn->nbody);
Ken Thompson's avatar
Ken Thompson committed
62
	}
63 64
	if(curfn->type->outtuple)
		if(walkret(curfn->nbody))
65
			yyerror("function ends without a return statement");
66
	walkstate(curfn->nbody);
Ken Thompson's avatar
Ken Thompson committed
67 68
	if(debug['W']) {
		snprint(s, sizeof(s), "after %S", curfn->nname->sym);
69
		dump(s, curfn->nbody);
Ken Thompson's avatar
Ken Thompson committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	}
}

void
addtotop(Node *n)
{
	Node *l;

	while(addtop != N) {
		l = addtop;
		addtop = N;
		walktype(l, Etop);
		n->ninit = list(n->ninit, l);
	}
}

void
Ken Thompson's avatar
Ken Thompson committed
87
gettype(Node *n, Node *a)
Ken Thompson's avatar
Ken Thompson committed
88 89 90 91
{
	if(debug['W'])
		dump("\nbefore gettype", n);
	walktype(n, Erv);
Ken Thompson's avatar
Ken Thompson committed
92 93 94
	if(a == N && addtop != N)
		fatal("gettype: addtop");
	addtotop(a);
Ken Thompson's avatar
Ken Thompson committed
95
	if(debug['W'])
Ken Thompson's avatar
Ken Thompson committed
96
		dump("after gettype", n);
97 98 99
}

void
Ken Thompson's avatar
Ken Thompson committed
100 101
walkstate(Node *n)
{
Ken Thompson's avatar
Ken Thompson committed
102
	Node *more;
Ken Thompson's avatar
Ken Thompson committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

loop:
	if(n == N)
		return;

	more = N;
	switch(n->op) {

	case OLIST:
		walkstate(n->left);
		more = n->right;
		break;

	default:
		yyerror("walkstate: %O not a top level statement", n->op);

	case OASOP:
	case OAS:
	case OCALLMETH:
	case OCALLINTER:
	case OCALL:
	case OSEND:
	case ORECV:
	case OPRINT:
	case OPANIC:
	case OFOR:
	case OIF:
	case OSWITCH:
	case OSELECT:
	case OEMPTY:
	case OBREAK:
	case OCONTINUE:
	case OGOTO:
	case OLABEL:
	case OFALL:
	case OXCASE:
	case OCASE:
	case OXFALL:
	case ORETURN:
	case OPROC:
		walktype(n, Etop);
		break;
	}

Ken Thompson's avatar
Ken Thompson committed
147
	addtotop(n);
Ken Thompson's avatar
Ken Thompson committed
148 149 150 151 152 153 154

	if(more != N) {
		n = more;
		goto loop;
	}
}

Ken Thompson's avatar
Ken Thompson committed
155 156 157 158 159 160 161
void
indir(Node *nl, Node *nr)
{
	if(nr != N)
		*nl = *nr;
}

Ken Thompson's avatar
Ken Thompson committed
162 163
void
walktype(Node *n, int top)
164 165 166 167
{
	Node *r, *l;
	Type *t;
	Sym *s;
Ken Thompson's avatar
maps  
Ken Thompson committed
168
	int et, cl, cr;
169
	int32 lno;
Ken Thompson's avatar
Ken Thompson committed
170 171

	lno = setlineno(n);
172 173 174 175 176 177 178 179 180 181

	/*
	 * walk the whole tree of the body of a function.
	 * the types expressions are calculated.
	 * compile-time constants are evaluated.
	 */

loop:
	if(n == N)
		goto ret;
Ken Thompson's avatar
Ken Thompson committed
182

Ken Thompson's avatar
Ken Thompson committed
183
	setlineno(n);
184

Ken Thompson's avatar
Ken Thompson committed
185 186
	if(debug['w'] > 1 && top == Etop && n->op != OLIST)
		dump("walk-before", n);
187

188 189 190 191 192 193 194 195
	t = T;
	et = Txxx;

	switch(n->op) {
	default:
		fatal("walktype: switch 1 unknown op %N", n);
		goto ret;

Ken Thompson's avatar
Ken Thompson committed
196
	case OLIST:
Ken Thompson's avatar
Ken Thompson committed
197
	case OKEY:
Ken Thompson's avatar
Ken Thompson committed
198 199 200 201
		walktype(n->left, top);
		n = n->right;
		goto loop;

202
	case OPRINT:
203 204 205
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
206
		indir(n, prcompat(n->left));
207 208 209
		goto ret;

	case OPANIC:
210 211 212
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
213
		indir(n, list(prcompat(n->left), nodpanic(n->lineno)));
214 215 216
		goto ret;

	case OLITERAL:
217 218
		if(top != Erv)
			goto nottop;
219 220 221
		n->addable = 1;
		goto ret;

222 223 224 225 226 227 228 229 230 231 232
	case ONONAME:
		s = n->sym;
		if(s->undef == 0) {
			s->undef = 1;
			yyerror("%S: undefined", s);
			goto ret;
		}
		if(top == Etop)
			goto nottop;
		goto ret;

233
	case ONAME:
234 235
		if(top == Etop)
			goto nottop;
236 237 238 239 240 241 242 243 244 245 246
		n->addable = 1;
		if(n->type == T) {
			s = n->sym;
			if(s->undef == 0) {
				yyerror("walktype: %N undeclared", n);
				s->undef = 1;
			}
		}
		goto ret;

	case OFOR:
247
		if(top != Etop)
248
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
249
		walkstate(n->ninit);
Ken Thompson's avatar
Ken Thompson committed
250
		walkbool(n->ntest);
Ken Thompson's avatar
Ken Thompson committed
251 252 253
		walkstate(n->nincr);
		walkstate(n->nbody);
		goto ret;
254 255

	case OSWITCH:
256
		if(top != Etop)
257 258
			goto nottop;

Ken Thompson's avatar
select  
Ken Thompson committed
259 260 261
		if(!casebody(n->nbody))
			yyerror("switch statement must have case labels");

262 263
		if(n->ntest == N)
			n->ntest = booltrue;
Ken Thompson's avatar
Ken Thompson committed
264
		walkstate(n->ninit);
265
		walktype(n->ntest, Erv);
Ken Thompson's avatar
Ken Thompson committed
266
		walkstate(n->nbody);
Ken Thompson's avatar
Ken Thompson committed
267

268 269
		// find common type
		if(n->ntest->type == T)
270
			n->ntest->type = walkswitch(n, sw1);
271 272 273

		// if that fails pick a type
		if(n->ntest->type == T)
274
			n->ntest->type = walkswitch(n, sw2);
275 276 277

		// set the type on all literals
		if(n->ntest->type != T)
278
			walkswitch(n, sw3);
Ken Thompson's avatar
Ken Thompson committed
279 280 281
		walktype(n->ntest, Erv);	// BOTCH is this right
		walktype(n->nincr, Erv);
		goto ret;
282

Ken Thompson's avatar
select  
Ken Thompson committed
283 284 285 286 287 288 289
	case OSELECT:
		if(top != Etop)
			goto nottop;

		walkselect(n);
		goto ret;

290
	case OEMPTY:
291
		if(top != Etop)
292 293 294 295
			goto nottop;
		goto ret;

	case OIF:
296
		if(top != Etop)
297
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
298
		walkstate(n->ninit);
Ken Thompson's avatar
Ken Thompson committed
299
		walkbool(n->ntest);
Ken Thompson's avatar
Ken Thompson committed
300
		walkstate(n->nbody);
Ken Thompson's avatar
Ken Thompson committed
301
		walkstate(n->nelse);
Ken Thompson's avatar
Ken Thompson committed
302
		goto ret;
303

Ken Thompson's avatar
Ken Thompson committed
304 305 306
	case OPROC:
		if(top != Etop)
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
307
		walkstate(n->left);
Ken Thompson's avatar
Ken Thompson committed
308 309
		goto ret;

310 311 312
	case OCALLMETH:
	case OCALLINTER:
	case OCALL:
313 314 315
		if(top == Elv)
			goto nottop;

316 317 318
		if(n->type != T)
			goto ret;

319
		walktype(n->left, Erv);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		if(n->left == N)
			goto ret;
		t = n->left->type;
		if(t == T)
			goto ret;

		dowidth(t);
		if(n->left->op == ODOTMETH)
			n->op = OCALLMETH;
		if(n->left->op == ODOTINTER)
			n->op = OCALLINTER;

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

		if(t->etype != TFUNC) {
			yyerror("call of a non-function %T", t);
			goto ret;
		}

		n->type = *getoutarg(t);
341 342 343 344 345 346 347 348 349
		switch(t->outtuple) {
		case 0:
			if(top == Erv) {
				yyerror("function requires a return type");
				n->type = types[TINT32];
			}
			break;

		case 1:
350
			n->type = n->type->type->type;
351 352
			break;
		}
353

354
		walktype(n->right, Erv);
355 356 357 358 359 360 361

		switch(n->op) {
		default:
			fatal("walk: op: %O", n->op);

		case OCALLINTER:
			l = ascompatte(n->op, getinarg(t), &n->right, 0);
Ken Thompson's avatar
Ken Thompson committed
362
			n->right = reorder1(l);
363 364 365 366
			break;

		case OCALL:
			l = ascompatte(n->op, getinarg(t), &n->right, 0);
Ken Thompson's avatar
Ken Thompson committed
367
			n->right = reorder1(l);
Ken Thompson's avatar
select  
Ken Thompson committed
368 369 370 371 372
			if(isselect(n)) {
				// clear output bool - special prob with selectsend
				r = ascompatte(n->op, getoutarg(t), &boolfalse, 0);
				n->right = list(n->right, r);
			}
373 374 375 376 377
			break;

		case OCALLMETH:
			l = ascompatte(n->op, getinarg(t), &n->right, 0);
			r = ascompatte(n->op, getthis(t), &n->left->left, 0);
Ken Thompson's avatar
select  
Ken Thompson committed
378
			l = list(r, l);
Ken Thompson's avatar
Ken Thompson committed
379 380
			n->left->left = N;
			ullmancalc(n->left);
Ken Thompson's avatar
select  
Ken Thompson committed
381
			n->right = reorder1(l);
382 383 384 385 386
			break;
		}
		goto ret;

	case OAS:
387
		if(top != Etop)
388 389
			goto nottop;

Ken Thompson's avatar
Ken Thompson committed
390 391 392
		addtop = list(addtop, n->ninit);
		n->ninit = N;

393
		l = n->left;
394 395
		r = n->right;
		walktype(l, Elv);
Ken Thompson's avatar
maps  
Ken Thompson committed
396
		if(l == N || r == N)
397 398
			goto ret;

Ken Thompson's avatar
maps  
Ken Thompson committed
399 400 401 402 403 404 405
		cl = listcount(l);
		cr = listcount(r);

		if(cl == cr) {
			walktype(r, Erv);
			l = ascompatee(n->op, &n->left, &n->right);
			if(l != N)
Ken Thompson's avatar
Ken Thompson committed
406
				indir(n, reorder3(l));
Ken Thompson's avatar
maps  
Ken Thompson committed
407 408 409 410
			goto ret;
		}

		switch(r->op) {
Ken Thompson's avatar
chan  
Ken Thompson committed
411

Ken Thompson's avatar
maps  
Ken Thompson committed
412 413 414
		case OCALLMETH:
		case OCALLINTER:
		case OCALL:
Ken Thompson's avatar
Ken Thompson committed
415 416 417 418
			if(cr == 1) {
				// a,b,... = fn()
				walktype(r, Erv);
				l = ascompatet(n->op, &n->left, &r->type, 0);
Ken Thompson's avatar
Ken Thompson committed
419 420
				if(l != N)
					indir(n, list(r, reorder2(l)));
Ken Thompson's avatar
Ken Thompson committed
421
				goto ret;
Ken Thompson's avatar
Ken Thompson committed
422
			}
Ken Thompson's avatar
maps  
Ken Thompson committed
423
			break;
Ken Thompson's avatar
Ken Thompson committed
424

Ken Thompson's avatar
maps  
Ken Thompson committed
425 426
		case OINDEX:
		case OINDEXPTR:
Ken Thompson's avatar
Ken Thompson committed
427 428
			if(cl == 2 && cr == 1) {
				// a,b = map[] - mapaccess2
Ken Thompson's avatar
bug075  
Ken Thompson committed
429
				walktype(r->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
430 431 432 433 434
				if(!isptrto(r->left->type, TMAP))
					break;
				l = mapop(n, top);
				if(l == N)
					break;
Ken Thompson's avatar
Ken Thompson committed
435
				indir(n, l);
Ken Thompson's avatar
Ken Thompson committed
436 437 438
				goto ret;
			}
			break;
Ken Thompson's avatar
chan  
Ken Thompson committed
439 440 441 442

		case ORECV:
			if(cl == 2 && cr == 1) {
				// a,b = <chan - chanrecv2
Ken Thompson's avatar
Ken Thompson committed
443
				walktype(r->left, Erv);
Ken Thompson's avatar
chan  
Ken Thompson committed
444 445 446 447 448
				if(!isptrto(r->left->type, TCHAN))
					break;
				l = chanop(n, top);
				if(l == N)
					break;
Ken Thompson's avatar
Ken Thompson committed
449
				indir(n, l);
Ken Thompson's avatar
chan  
Ken Thompson committed
450 451 452
				goto ret;
			}
			break;
Ken Thompson's avatar
Ken Thompson committed
453 454 455 456 457 458 459 460 461 462 463 464
		}

		switch(l->op) {
		case OINDEX:
		case OINDEXPTR:
			if(cl == 1 && cr == 2) {
				// map[] = a,b - mapassign2
				if(!isptrto(l->left->type, TMAP))
					break;
				l = mapop(n, top);
				if(l == N)
					break;
Ken Thompson's avatar
Ken Thompson committed
465
				indir(n, l);
Ken Thompson's avatar
Ken Thompson committed
466 467
				goto ret;
			}
Ken Thompson's avatar
maps  
Ken Thompson committed
468 469
			break;
		}
Ken Thompson's avatar
Ken Thompson committed
470 471

		yyerror("bad shape across assignment - cr=%d cl=%d\n", cr, cl);
472 473 474 475 476 477
		goto ret;

	case OBREAK:
	case OCONTINUE:
	case OGOTO:
	case OLABEL:
478 479
		if(top != Etop)
			goto nottop;
480 481 482
		goto ret;

	case OXCASE:
483 484
		if(top != Etop)
			goto nottop;
485 486 487 488
		yyerror("case statement out of place");
		n->op = OCASE;

	case OCASE:
489 490 491
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
492 493
		walkstate(n->right);
		goto ret;
494 495

	case OXFALL:
496 497
		if(top != Etop)
			goto nottop;
498 499 500 501 502 503 504 505
		yyerror("fallthrough statement out of place");
		n->op = OFALL;

	case OFALL:
	case OINDREG:
		goto ret;

	case OCONV:
Ken Thompson's avatar
Ken Thompson committed
506
		if(top == Etop)
507 508
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
509 510 511 512 513 514

		l = n->left;
		if(l == N)
			goto ret;
		t = n->type;
		if(t == T)
515 516
			goto ret;

Ken Thompson's avatar
Ken Thompson committed
517
		convlit(l, t);
518 519

		// nil conversion
Ken Thompson's avatar
Ken Thompson committed
520 521
		if(eqtype(t, l->type, 0)) {
			if(l->op != ONAME)
Ken Thompson's avatar
Ken Thompson committed
522
				indir(n, l);
523 524 525 526
			goto ret;
		}

		// simple fix-float
Ken Thompson's avatar
Ken Thompson committed
527 528 529
		if(l->type != T)
		if(isint[l->type->etype] || isfloat[l->type->etype])
		if(isint[t->etype] || isfloat[t->etype]) {
530 531 532 533 534
			evconst(n);
			goto ret;
		}

		// to string
Ken Thompson's avatar
Ken Thompson committed
535
		if(l->type != T)
Ken Thompson's avatar
Ken Thompson committed
536 537
		if(isptrto(t, TSTRING)) {
			if(isint[l->type->etype]) {
Ken Thompson's avatar
Ken Thompson committed
538
				indir(n, stringop(n, top));
539 540
				goto ret;
			}
Ken Thompson's avatar
Ken Thompson committed
541
			if(bytearraysz(l->type) != -2) {
542
				n->op = OARRAY;
Ken Thompson's avatar
Ken Thompson committed
543
				indir(n, stringop(n, top));
544 545 546 547
				goto ret;
			}
		}

Ken Thompson's avatar
arrays  
Ken Thompson committed
548
		// convert dynamic to static generated by ONEW
549
		if(isptrarray(t) && isptrdarray(l->type))
Ken Thompson's avatar
Ken Thompson committed
550
			goto ret;
Ken Thompson's avatar
arrays  
Ken Thompson committed
551

Ken Thompson's avatar
Ken Thompson committed
552
		// interface and structure
Ken Thompson's avatar
Ken Thompson committed
553 554
		et = isandss(n->type, l);
		if(et != Inone) {
Ken Thompson's avatar
Ken Thompson committed
555
if(et == I2I) dump("conv", n);
Ken Thompson's avatar
Ken Thompson committed
556
			indir(n, ifaceop(n->type, l, et));
Ken Thompson's avatar
Ken Thompson committed
557 558
			goto ret;
		}
Ken Thompson's avatar
Ken Thompson committed
559 560 561

		// structure literal
		if(t->etype == TSTRUCT) {
Ken Thompson's avatar
Ken Thompson committed
562
			indir(n, structlit(n));
Ken Thompson's avatar
Ken Thompson committed
563 564 565
			goto ret;
		}

Ken Thompson's avatar
Ken Thompson committed
566
		// array literal
Ken Thompson's avatar
Ken Thompson committed
567 568
		if(t->etype == TARRAY) {
			r = arraylit(n);
Ken Thompson's avatar
Ken Thompson committed
569
			indir(n, r);
Ken Thompson's avatar
Ken Thompson committed
570 571 572
			goto ret;
		}

Ken Thompson's avatar
Ken Thompson committed
573 574 575
		// map literal
		if(t->etype == TMAP) {
			r = maplit(n);
Ken Thompson's avatar
Ken Thompson committed
576
			indir(n, r);
Ken Thompson's avatar
Ken Thompson committed
577 578 579
			goto ret;
		}

Ken Thompson's avatar
Ken Thompson committed
580
		badtype(n->op, l->type, t);
581 582 583
		goto ret;

	case ORETURN:
584 585 586
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
587 588 589 590
		if(curfn->type->outnamed && n->left == N) {
			// print("special return\n");
			goto ret;
		}
591 592
		l = ascompatte(n->op, getoutarg(curfn->type), &n->left, 1);
		if(l != N)
Ken Thompson's avatar
Ken Thompson committed
593
			n->left = reorder4(l);
594 595 596
		goto ret;

	case ONOT:
597 598 599
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
600 601 602 603 604 605
		if(n->left == N || n->left->type == T)
			goto ret;
		et = n->left->type->etype;
		break;

	case OASOP:
606
		if(top != Etop)
607
			goto nottop;
608
		walktype(n->left, Elv);
609
		l = n->left;
Ken Thompson's avatar
Ken Thompson committed
610 611 612
		if(l->op != OINDEX) {
			if(n->etype == OLSH || n->etype == ORSH)
				goto shft;
613
			goto com;
Ken Thompson's avatar
Ken Thompson committed
614
		}
615 616
		if(!isptrto(l->left->type, TMAP))
			goto com;
Ken Thompson's avatar
Ken Thompson committed
617
		indir(n, mapop(n, top));
Ken Thompson's avatar
Ken Thompson committed
618
		goto ret;
619 620 621

	case OLSH:
	case ORSH:
Ken Thompson's avatar
Ken Thompson committed
622 623 624 625 626 627 628 629 630 631 632 633
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);

	shft:
		walktype(n->right, Erv);
		if(n->left == N || n->right == N)
			goto ret;
		evconst(n);
		if(n->op == OLITERAL)
			goto ret;
		convlit(n->right, types[TUINT32]);
Ken Thompson's avatar
Ken Thompson committed
634
		convlit(n->left, types[TINT32]);
Ken Thompson's avatar
Ken Thompson committed
635 636 637 638 639 640
		if(n->left->type == T || n->right->type == T)
			goto ret;
		if(issigned[n->right->type->etype])
			goto badt;
		break;

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	case OMOD:
	case OAND:
	case OOR:
	case OXOR:
	case OANDAND:
	case OOROR:
	case OEQ:
	case ONE:
	case OLT:
	case OLE:
	case OGE:
	case OGT:
	case OADD:
	case OSUB:
	case OMUL:
	case ODIV:
657 658 659 660 661 662
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);

	com:
		walktype(n->right, Erv);
663 664 665 666 667
		if(n->left == N || n->right == N)
			goto ret;
		evconst(n);
		if(n->op == OLITERAL)
			goto ret;
Ken Thompson's avatar
Ken Thompson committed
668 669
		convlit(n->left, n->right->type);
		convlit(n->right, n->left->type);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		if(n->left->type == T || n->right->type == T)
			goto ret;
		if(!ascompat(n->left->type, n->right->type))
			goto badt;

		switch(n->op) {
		case OEQ:
		case ONE:
		case OLT:
		case OLE:
		case OGE:
		case OGT:
		case OADD:
		case OASOP:
			if(isptrto(n->left->type, TSTRING)) {
Ken Thompson's avatar
Ken Thompson committed
685
				indir(n, stringop(n, top));
686 687 688 689 690 691 692 693
				goto ret;
			}
		}
		break;

	case OMINUS:
	case OPLUS:
	case OCOM:
694 695 696
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
697 698 699 700 701 702 703 704
		if(n->left == N)
			goto ret;
		evconst(n);
		if(n->op == OLITERAL)
			goto ret;
		break;

	case OLEN:
705 706 707
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
708 709 710 711 712 713 714 715 716 717
		evconst(n);
		t = n->left->type;
		if(t != T && isptr[t->etype])
			t = t->type;
		if(t == T)
			goto ret;
		switch(t->etype) {
		default:
			goto badt;
		case TSTRING:
Ken Thompson's avatar
Ken Thompson committed
718
		case TMAP:
Ken Thompson's avatar
Ken Thompson committed
719 720
			break;
		case TARRAY:
721 722
			if(t->bound >= 0)
				nodconst(n, types[TINT32], t->bound);
Ken Thompson's avatar
Ken Thompson committed
723
			break;
724 725 726 727
		}
		n->type = types[TINT32];
		goto ret;

Ken Thompson's avatar
arrays  
Ken Thompson committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741
	case OCAP:
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
		evconst(n);
		t = n->left->type;
		if(t != T && isptr[t->etype])
			t = t->type;
		if(t == T)
			goto ret;
		switch(t->etype) {
		default:
			goto badt;
		case TARRAY:
742 743
			if(t->bound >= 0)
				nodconst(n, types[TINT32], t->bound);
Ken Thompson's avatar
arrays  
Ken Thompson committed
744 745 746 747 748
			break;
		}
		n->type = types[TINT32];
		goto ret;

749 750
	case OINDEX:
	case OINDEXPTR:
751 752 753
		if(top == Etop)
			goto nottop;

Ken Thompson's avatar
Ken Thompson committed
754
		walktype(n->left, Erv);
755 756
		walktype(n->right, Erv);

757 758
		if(n->left == N || n->right == N)
			goto ret;
Ken Thompson's avatar
maps  
Ken Thompson committed
759 760

		defaultlit(n->left);
761 762 763 764
		t = n->left->type;
		if(t == T)
			goto ret;

Ken Thompson's avatar
Ken Thompson committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778
// BOTCH - convert each index opcode
// to look like this and get rid of OINDEXPTR
		if(isptr[t->etype])
		if(isptrto(t, TSTRING) || isptrto(t->type, TSTRING)) {
			// right side must be an int
			if(top != Erv)
				goto nottop;
			if(n->right->type == T) {
				convlit(n->right, types[TINT32]);
				if(n->right->type == T)
					goto ret;
			}
			if(!isint[n->right->type->etype])
				goto badt;
Ken Thompson's avatar
Ken Thompson committed
779
			indir(n, stringop(n, top));
Ken Thompson's avatar
Ken Thompson committed
780 781 782
			goto ret;
		}

Ken Thompson's avatar
maps  
Ken Thompson committed
783 784 785 786
		// left side is indirect
		if(isptr[t->etype]) {
			t = t->type;
			n->op = OINDEXPTR;
787 788
		}

Ken Thompson's avatar
maps  
Ken Thompson committed
789 790
		switch(t->etype) {
		default:
791 792
			goto badt;

Ken Thompson's avatar
maps  
Ken Thompson committed
793
		case TMAP:
Ken Thompson's avatar
chan  
Ken Thompson committed
794
			// right side must be map type
Ken Thompson's avatar
maps  
Ken Thompson committed
795 796 797 798 799 800 801 802 803 804 805
			if(n->right->type == T) {
				convlit(n->right, t->down);
				if(n->right->type == T)
					break;
			}
			if(!eqtype(n->right->type, t->down, 0))
				goto badt;
			if(n->op != OINDEXPTR)
				goto badt;
			n->op = OINDEX;
			n->type = t->type;
Ken Thompson's avatar
Ken Thompson committed
806
			if(top == Erv)
Ken Thompson's avatar
Ken Thompson committed
807
				indir(n, mapop(n, top));
Ken Thompson's avatar
maps  
Ken Thompson committed
808 809
			break;

Ken Thompson's avatar
arrays  
Ken Thompson committed
810
		case TARRAY:
Ken Thompson's avatar
maps  
Ken Thompson committed
811 812 813 814 815 816 817 818
			// right side must be an int
			if(n->right->type == T) {
				convlit(n->right, types[TINT32]);
				if(n->right->type == T)
					break;
			}
			if(!isint[n->right->type->etype])
				goto badt;
819

Ken Thompson's avatar
maps  
Ken Thompson committed
820 821
			n->type = t->type;
			break;
822 823 824
		}
		goto ret;

Ken Thompson's avatar
chan  
Ken Thompson committed
825
	case OSEND:
Ken Thompson's avatar
Ken Thompson committed
826
		if(top == Elv)
Ken Thompson's avatar
chan  
Ken Thompson committed
827
			goto nottop;
Ken Thompson's avatar
select  
Ken Thompson committed
828 829
		walktype(n->left, Erv);		// chan
		walktype(n->right, Erv);	// e
Ken Thompson's avatar
Ken Thompson committed
830
		indir(n, chanop(n, top));
Ken Thompson's avatar
chan  
Ken Thompson committed
831 832 833
		goto ret;

	case ORECV:
Ken Thompson's avatar
Ken Thompson committed
834
		if(top == Elv)
Ken Thompson's avatar
chan  
Ken Thompson committed
835
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
836
		if(n->right == N) {
Ken Thompson's avatar
Ken Thompson committed
837 838
			walktype(n->left, Erv);		// chan
			indir(n, chanop(n, top));	// returns e blocking
Ken Thompson's avatar
Ken Thompson committed
839 840 841 842
			goto ret;
		}
		walktype(n->left, Elv);		// e
		walktype(n->right, Erv);	// chan
Ken Thompson's avatar
Ken Thompson committed
843
		indir(n, chanop(n, top));	// returns bool non-blocking
Ken Thompson's avatar
chan  
Ken Thompson committed
844 845
		goto ret;

846
	case OSLICE:
847 848 849 850 851
		if(top == Etop)
			goto nottop;

		walktype(n->left, top);
		walktype(n->right, Erv);
852 853
		if(n->left == N || n->right == N)
			goto ret;
Ken Thompson's avatar
Ken Thompson committed
854
		convlit(n->left, types[TSTRING]);
Ken Thompson's avatar
arrays  
Ken Thompson committed
855
		t = n->left->type;
Ken Thompson's avatar
Ken Thompson committed
856 857
		if(t == T)
			goto ret;
Ken Thompson's avatar
arrays  
Ken Thompson committed
858 859 860
		if(isptr[t->etype])
			t = t->type;
		if(t->etype == TSTRING) {
Ken Thompson's avatar
Ken Thompson committed
861
			indir(n, stringop(n, top));
862 863
			goto ret;
		}
864
		if(t->etype == TARRAY) {
Ken Thompson's avatar
Ken Thompson committed
865
			indir(n, arrayop(n, top));
Ken Thompson's avatar
arrays  
Ken Thompson committed
866 867
			goto ret;
		}
868 869 870 871 872 873 874
		badtype(OSLICE, n->left->type, T);
		goto ret;

	case ODOT:
	case ODOTPTR:
	case ODOTMETH:
	case ODOTINTER:
875 876
		if(top == Etop)
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
877
		walkdot(n);
878 879 880
		goto ret;

	case OADDR:
881 882 883
		if(top != Erv)
			goto nottop;
		walktype(n->left, Elv);
884 885 886 887 888 889 890 891 892
		if(n->left == N)
			goto ret;
		t = n->left->type;
		if(t == T)
			goto ret;
		n->type = ptrto(t);
		goto ret;

	case OIND:
893 894 895
		if(top == Etop)
			goto nottop;
		walktype(n->left, top);
896 897 898 899 900 901 902 903 904 905 906
		if(n->left == N)
			goto ret;
		t = n->left->type;
		if(t == T)
			goto ret;
		if(!isptr[t->etype])
			goto badt;
		n->type = t->type;
		goto ret;

	case ONEW:
907 908
		if(top != Erv)
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
909
		indir(n, newcompat(n));
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
		goto ret;
	}

/*
 * ======== second switch ========
 */

	switch(n->op) {
	default:
		fatal("walktype: switch 2 unknown op %N", n);
		goto ret;

	case OASOP:
		break;

	case ONOT:
	case OANDAND:
	case OOROR:
928 929
		if(n->left->type == T)
			goto ret;
930 931 932 933 934 935 936 937
		et = n->left->type->etype;
		if(et != TBOOL)
			goto badt;
		t = types[TBOOL];
		break;

	case OEQ:
	case ONE:
938 939
		if(n->left->type == T)
			goto ret;
940 941 942 943 944 945 946 947 948 949
		et = n->left->type->etype;
		if(!okforeq[et])
			goto badt;
		t = types[TBOOL];
		break;

	case OLT:
	case OLE:
	case OGE:
	case OGT:
950 951
		if(n->left->type == T)
			goto ret;
952 953 954 955 956 957 958 959 960 961 962 963
		et = n->left->type->etype;
		if(!okforadd[et])
			if(!isptrto(n->left->type, TSTRING))
				goto badt;
		t = types[TBOOL];
		break;

	case OADD:
	case OSUB:
	case OMUL:
	case ODIV:
	case OPLUS:
964 965
		if(n->left->type == T)
			goto ret;
966 967 968 969 970 971
		et = n->left->type->etype;
		if(!okforadd[et])
			goto badt;
		break;

	case OMINUS:
972 973
		if(n->left->type == T)
			goto ret;
974 975 976 977 978 979 980
		et = n->left->type->etype;
		if(!okforadd[et])
			goto badt;
		if(!isfloat[et])
			break;

		l = nod(OLITERAL, N, N);
Ken Thompson's avatar
Ken Thompson committed
981
		l->val.u.fval = mal(sizeof(*l->val.u.fval));
982
		l->val.ctype = CTFLT;
Ken Thompson's avatar
Ken Thompson committed
983
		mpmovecflt(l->val.u.fval, 0.0);
984 985

		l = nod(OSUB, l, n->left);
Ken Thompson's avatar
Ken Thompson committed
986
		indir(n, l);
987
		walktype(n, Erv);
988 989 990 991 992 993 994 995 996
		goto ret;

	case OLSH:
	case ORSH:
	case OAND:
	case OOR:
	case OXOR:
	case OMOD:
	case OCOM:
997 998
		if(n->left->type == T)
			goto ret;
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		et = n->left->type->etype;
		if(!okforand[et])
			goto badt;
		break;
	}

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

nottop:
1011 1012
	dump("bad top", n);
	fatal("walktype: top=%d %O", top, n->op);
Ken Thompson's avatar
Ken Thompson committed
1013
	goto ret;
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

badt:
	if(n->right == N) {
		if(n->left == N) {
			badtype(n->op, T, T);
			goto ret;
		}
		badtype(n->op, n->left->type, T);
		goto ret;
	}
	badtype(n->op, n->left->type, n->right->type);
	goto ret;

ret:
Ken Thompson's avatar
Ken Thompson committed
1028 1029
	if(debug['w'] && top == Etop && n != N)
		dump("walk", n);
Ken Thompson's avatar
maps  
Ken Thompson committed
1030

1031
	ullmancalc(n);
Ken Thompson's avatar
Ken Thompson committed
1032
	lineno = lno;
1033 1034
}

Ken Thompson's avatar
Ken Thompson committed
1035 1036 1037 1038
void
walkbool(Node *n)
{
	walktype(n, Erv);
Ken Thompson's avatar
Ken Thompson committed
1039
	addtotop(n);
Ken Thompson's avatar
Ken Thompson committed
1040 1041 1042 1043 1044
	if(n != N && n->type != T)
		if(!eqtype(n->type, types[TBOOL], 0))
			yyerror("IF and FOR require a boolean type");
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/*
 * return the first type
 */
Type*
sw1(Node *c, Type *place)
{
	if(place == T)
		return c->type;
	return place;
}

/*
 * return a suitable type
 */
Type*
sw2(Node *c, Type *place)
{
	return types[TINT32];	// botch
}

/*
Ken Thompson's avatar
select  
Ken Thompson committed
1066
 * check that switch type
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
 * is compat with all the cases
 */
Type*
sw3(Node *c, Type *place)
{
	if(place == T)
		return c->type;
	if(c->type == T)
		c->type = place;
	convlit(c, place);
	if(!ascompat(place, c->type))
		badtype(OSWITCH, place, c->type);
	return place;
}

Type*
1083
walkswitch(Node *sw, Type*(*call)(Node*, Type*))
1084 1085 1086
{
	Node *n, *c;
	Type *place;
1087
	place = call(sw->ntest, T);
1088

Ken Thompson's avatar
Ken Thompson committed
1089 1090
	setlineno(sw);

1091
	n = sw->nbody;
1092 1093
	if(n->op == OLIST)
		n = n->left;
1094 1095
	if(n->op == OEMPTY)
		return;
1096 1097 1098 1099 1100 1101

	for(; n!=N; n=n->right) {
		if(n->op != OCASE)
			fatal("walkswitch: not case %O\n", n->op);
		for(c=n->left; c!=N; c=c->right) {
			if(c->op != OLIST) {
Ken Thompson's avatar
Ken Thompson committed
1102
				setlineno(c);
1103 1104 1105
				place = call(c, place);
				break;
			}
Ken Thompson's avatar
Ken Thompson committed
1106
			setlineno(c);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
			place = call(c->left, place);
		}
	}
	return place;
}

int
casebody(Node *n)
{
	Node *oc, *ot, *t;
	Iter save;

	/*
	 * look to see if statements at top level have
	 * case labels attached to them. convert the illegal
	 * ops XFALL and XCASE into legal ops FALL and CASE.
	 * all unconverted ops will thus be caught as illegal
	 */

	oc = N;		// last case statement
	ot = N;		// last statement (look for XFALL)
	t = listfirst(&save, &n);

loop:
	if(t == N) {
1132
		/* empty switch */
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
		if(oc == N)
			return 0;
		return 1;
	}
	if(t->op == OXCASE) {
		/* rewrite and link top level cases */
		t->op = OCASE;
		if(oc != N)
			oc->right = t;
		oc = t;

		/* rewrite top fall that preceed case */
		if(ot != N && ot->op == OXFALL)
			ot->op = OFALL;
	}

1149
	/* if first statement is not case */
1150 1151 1152 1153 1154 1155 1156 1157
	if(oc == N)
		return 0;

	ot = t;
	t = listnext(&save);
	goto loop;
}

Ken Thompson's avatar
select  
Ken Thompson committed
1158
Node*
Ken Thompson's avatar
Ken Thompson committed
1159
selcase(Node *n, Node *var)
Ken Thompson's avatar
select  
Ken Thompson committed
1160
{
Ken Thompson's avatar
Ken Thompson committed
1161
	Node *a, *r, *on, *c;
Ken Thompson's avatar
select  
Ken Thompson committed
1162
	Type *t;
Ken Thompson's avatar
select  
Ken Thompson committed
1163
	Iter iter;
Ken Thompson's avatar
select  
Ken Thompson committed
1164

Ken Thompson's avatar
Ken Thompson committed
1165 1166 1167 1168
	c = n->left;
	if(c->op == ORECV)
		goto recv;

Ken Thompson's avatar
select  
Ken Thompson committed
1169 1170
	walktype(c->left, Erv);		// chan
	walktype(c->right, Erv);	// elem
Ken Thompson's avatar
Ken Thompson committed
1171

Ken Thompson's avatar
select  
Ken Thompson committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	t = fixchan(c->left->type);
	if(t == T)
		return;

	convlit(c->right, t->type);
	if(!ascompat(t->type, c->right->type)) {
		badtype(c->op, t->type, c->right->type);
		return;
	}

	// selectsend(sel *byte, hchan *chan any, elem any) (selected bool);
	on = syslook("selectsend", 1);
	argtype(on, t->type);
	argtype(on, t->type);

Ken Thompson's avatar
Ken Thompson committed
1187
	a = c->right;			// elem
Ken Thompson's avatar
select  
Ken Thompson committed
1188
	r = a;
Ken Thompson's avatar
Ken Thompson committed
1189
	a = c->left;			// chan
Ken Thompson's avatar
select  
Ken Thompson committed
1190
	r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
1191 1192 1193
	a = var;			// sel-var
	r = list(a, r);

Ken Thompson's avatar
select  
Ken Thompson committed
1194
	goto out;
Ken Thompson's avatar
Ken Thompson committed
1195 1196

recv:
Ken Thompson's avatar
select  
Ken Thompson committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	if(c->right != N)
		goto recv2;

	walktype(c->left, Erv);		// chan

	t = fixchan(c->left->type);
	if(t == T)
		return;

	// selectrecv(sel *byte, hchan *chan any, elem *any) (selected bool);
	on = syslook("selectrecv", 1);
	argtype(on, t->type);
	argtype(on, t->type);

	a = c->left;			// nil elem
	a = nod(OLITERAL, N, N);
	a->val.ctype = CTNIL;

	r = a;
	a = c->left;			// chan
	r = list(a, r);
	a = var;			// sel-var
	r = list(a, r);
	goto out;

recv2:	
Ken Thompson's avatar
Ken Thompson committed
1223 1224 1225 1226 1227 1228
	walktype(c->right, Erv);	// chan

	t = fixchan(c->right->type);
	if(t == T)
		return;

Ken Thompson's avatar
select  
Ken Thompson committed
1229
	walktype(c->left, Elv);	// elem
Ken Thompson's avatar
Ken Thompson committed
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	convlit(c->left, t->type);
	if(!ascompat(t->type, c->left->type)) {
		badtype(c->op, t->type, c->left->type);
		return;
	}

	// selectrecv(sel *byte, hchan *chan any, elem *any) (selected bool);
	on = syslook("selectrecv", 1);
	argtype(on, t->type);
	argtype(on, t->type);

	a = c->left;			// elem
	a = nod(OADDR, a, N);
	r = a;
	a = c->right;			// chan
	r = list(a, r);
	a = var;			// sel-var
Ken Thompson's avatar
select  
Ken Thompson committed
1247 1248
	r = list(a, r);

Ken Thompson's avatar
select  
Ken Thompson committed
1249
out:
Ken Thompson's avatar
select  
Ken Thompson committed
1250 1251 1252 1253 1254 1255 1256
	a = nod(OCALL, on, r);
	r = nod(OIF, N, N);
	r->ntest = a;

	return r;
}

Ken Thompson's avatar
Ken Thompson committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
Node*
selectas(Node *name, Node *expr)
{
	Node *a;
	Type *t;

	if(expr == N || expr->op != ORECV)
		goto bad;
	t = expr->left->type;
	if(t == T)
		goto bad;
	if(isptr[t->etype])
		t = t->type;
	if(t == T)
		goto bad;
	if(t->etype != TCHAN)
		goto bad;
	a = old2new(name, t->type);
	return a;

bad:
	return name;
}

Ken Thompson's avatar
select  
Ken Thompson committed
1281 1282 1283 1284 1285 1286 1287
void
walkselect(Node *sel)
{
	Iter iter;
	Node *n, *oc, *on, *r;
	Node *var, *bod, *res;
	int count;
1288
	int32 lno;
Ken Thompson's avatar
select  
Ken Thompson committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

	lno = setlineno(sel);

	// generate sel-struct
	var = nod(OXXX, N, N);
	tempname(var, ptrto(types[TUINT8]));

	n = listfirst(&iter, &sel->left);
	if(n == N || n->op != OXCASE)
		yyerror("first select statement must be a case");

	count = 0;	// number of cases
	res = N;	// entire select body
	bod = N;	// body of each case
	oc = N;		// last case

	for(count=0; n!=N; n=listnext(&iter)) {
		setlineno(n);

		switch(n->op) {
		default:
			bod = list(bod, n);
			break;

		case OXCASE:
			switch(n->left->op) {
			default:
				yyerror("select cases must be send or recv");
				break;

Ken Thompson's avatar
Ken Thompson committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
			case OAS:
				// convert new syntax (a=recv(chan)) to (recv(a,chan))
				if(n->left->right == N || n->left->right->op != ORECV) {
					yyerror("select cases must be send or recv");
					break;
				}
				n->left->right->right = n->left->right->left;
				n->left->right->left = n->left->left;
				n->left = n->left->right;

Ken Thompson's avatar
select  
Ken Thompson committed
1329
			case OSEND:
Ken Thompson's avatar
Ken Thompson committed
1330
			case ORECV:
Ken Thompson's avatar
select  
Ken Thompson committed
1331 1332 1333 1334
				if(oc != N) {
					bod = list(bod, nod(OBREAK, N, N));
					oc->nbody = rev(bod);
				}
Ken Thompson's avatar
Ken Thompson committed
1335
				oc = selcase(n, var);
Ken Thompson's avatar
select  
Ken Thompson committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
				res = list(res, oc);
				break;
			}
			bod = N;
			count++;
			break;
		}
	}
	if(oc != N) {
		bod = list(bod, nod(OBREAK, N, N));
		oc->nbody = rev(bod);
	}
	setlineno(sel);

	// selectgo(sel *byte);
	on = syslook("selectgo", 0);
	r = nod(OCALL, on, var);		// sel-var
	res = list(res, r);

	// newselect(size uint32) (sel *byte);
	on = syslook("newselect", 0);

	r = nod(OXXX, N, N);
	nodconst(r, types[TINT32], count);	// count
	r = nod(OCALL, on, r);
	r = nod(OAS, var, r);

	sel->ninit = r;
	sel->nbody = rev(res);
	sel->left = N;

Ken Thompson's avatar
Ken Thompson committed
1367 1368
	walkstate(sel->ninit);
	walkstate(sel->nbody);
Ken Thompson's avatar
select  
Ken Thompson committed
1369

Ken Thompson's avatar
select  
Ken Thompson committed
1370
//dump("sel", sel);
Ken Thompson's avatar
Ken Thompson committed
1371

Ken Thompson's avatar
select  
Ken Thompson committed
1372 1373 1374
	lineno = lno;
}

1375 1376 1377 1378 1379
/*
 * allowable type combinations for
 * normal binary operations.
 */
Type*
Ken Thompson's avatar
Ken Thompson committed
1380
lookdot(Node *n, Type *f)
1381
{
Ken Thompson's avatar
Ken Thompson committed
1382
	Type *r, *c;
1383 1384 1385 1386 1387
	Sym *s;

	r = T;
	s = n->sym;

Ken Thompson's avatar
Ken Thompson committed
1388
	for(; f!=T; f=f->down) {
1389 1390 1391 1392 1393
		if(f->sym == S)
			continue;
		if(f->sym != s)
			continue;
		if(r != T) {
1394
			yyerror("ambiguous DOT reference %S", s);
1395 1396 1397 1398 1399 1400 1401 1402
			break;
		}
		r = f;
	}
	return r;
}

void
Ken Thompson's avatar
Ken Thompson committed
1403
walkdot(Node *n)
1404 1405 1406 1407 1408 1409
{
	Node *mn;
	Type *t, *f;

	if(n->left == N || n->right == N)
		return;
Ken Thompson's avatar
Ken Thompson committed
1410 1411
	if(n->op == ODOTINTER || n->op == ODOTMETH)
		return;	// already done
1412

1413
	walktype(n->left, Erv);
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	if(n->right->op != ONAME) {
		yyerror("rhs of . must be a name");
		return;
	}

	t = n->left->type;
	if(t == T)
		return;

	if(isptr[t->etype]) {
		t = t->type;
		if(t == T)
			return;
		n->op = ODOTPTR;
	}

Ken Thompson's avatar
Ken Thompson committed
1430 1431 1432 1433 1434 1435 1436 1437
	// as a structure field
	if(t->etype == TSTRUCT || t->etype == TINTER) {
		f = lookdot(n->right, t->type);
		if(f != T) {
			n->xoffset = f->width;
			n->right = f->nname;		// substitute real name
			n->type = f->type;
			if(t->etype == TINTER)
1438
				n->op = ODOTINTER;
Ken Thompson's avatar
Ken Thompson committed
1439
			return;
1440
		}
1441
	}
Ken Thompson's avatar
Ken Thompson committed
1442 1443 1444

	f = lookdot(n->right, t->method);
	if(f == T) {
1445
		yyerror("undefined DOT %S", n->right->sym);
Ken Thompson's avatar
Ken Thompson committed
1446 1447 1448 1449 1450 1451 1452
		return;
	}

	n->xoffset = f->width;
	n->right = methodname(n->right, t);
	n->type = f->type;
	n->op = ODOTMETH;
1453 1454 1455 1456 1457 1458 1459 1460
}

Node*
ascompatee(int op, Node **nl, Node **nr)
{
	Node *l, *r, *nn, *a;
	Iter savel, saver;

Ken Thompson's avatar
Ken Thompson committed
1461 1462 1463 1464 1465
	/*
	 * check assign expression list to
	 * a expression list. called in
	 *	expr-list = expr-list
	 */
1466 1467 1468 1469 1470 1471 1472
	l = listfirst(&savel, nl);
	r = listfirst(&saver, nr);
	nn = N;

loop:
	if(l == N || r == N) {
		if(l != r)
Ken Thompson's avatar
Ken Thompson committed
1473
			yyerror("error in shape across %O", op);
Ken Thompson's avatar
Ken Thompson committed
1474
		return rev(nn);
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	}

	convlit(r, l->type);
	if(!ascompat(l->type, r->type)) {
		badtype(op, l->type, r->type);
		return N;
	}

	a = nod(OAS, l, r);
	a = convas(a);
	if(nn == N)
		nn = a;
	else
Ken Thompson's avatar
select  
Ken Thompson committed
1488
		nn = list(a, nn);
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

	l = listnext(&savel);
	r = listnext(&saver);
	goto loop;
}

Node*
ascompatet(int op, Node **nl, Type **nr, int fp)
{
	Node *l, *nn, *a;
	Type *r;
	Iter savel, saver;

Ken Thompson's avatar
Ken Thompson committed
1502 1503 1504 1505 1506
	/*
	 * check assign type list to
	 * a expression list. called in
	 *	expr-list = func()
	 */
1507 1508 1509 1510 1511 1512 1513
	l = listfirst(&savel, nl);
	r = structfirst(&saver, nr);
	nn = N;

loop:
	if(l == N || r == T) {
		if(l != N || r != T)
Ken Thompson's avatar
Ken Thompson committed
1514
			yyerror("error in shape across %O", op);
Ken Thompson's avatar
Ken Thompson committed
1515
		return rev(nn);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
	}

	if(!ascompat(l->type, r->type)) {
		badtype(op, l->type, r->type);
		return N;
	}

	a = nod(OAS, l, nodarg(r, fp));
	a = convas(a);
	if(nn == N)
		nn = a;
	else
Ken Thompson's avatar
select  
Ken Thompson committed
1528
		nn = list(a, nn);
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542

	l = listnext(&savel);
	r = structnext(&saver);

	goto loop;
}

Node*
ascompatte(int op, Type **nl, Node **nr, int fp)
{
	Type *l;
	Node *r, *nn, *a;
	Iter savel, saver;

Ken Thompson's avatar
Ken Thompson committed
1543 1544 1545 1546 1547 1548
	/*
	 * check assign expression list to
	 * a type list. called in
	 *	return expr-list
	 *	func(expr-list)
	 */
1549 1550 1551 1552 1553 1554
	l = structfirst(&savel, nl);
	r = listfirst(&saver, nr);
	nn = N;
loop:
	if(l == T || r == N) {
		if(l != T || r != N)
Ken Thompson's avatar
Ken Thompson committed
1555
			yyerror("error in shape across %O", op);
Ken Thompson's avatar
Ken Thompson committed
1556
		return rev(nn);
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
	}
	convlit(r, l->type);
	if(!ascompat(l->type, r->type)) {
		badtype(op, l->type, r->type);
		return N;
	}

	a = nod(OAS, nodarg(l, fp), r);
	a = convas(a);
	if(nn == N)
		nn = a;
	else
Ken Thompson's avatar
select  
Ken Thompson committed
1569
		nn = list(a, nn);
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591

	l = structnext(&savel);
	r = listnext(&saver);

	goto loop;
}

/*
 * can we assign var of type t2 to var of type t1
 */
int
ascompat(Type *t1, Type *t2)
{
	if(eqtype(t1, t2, 0))
		return 1;

//	if(eqtype(t1, nilptr, 0))
//		return 1;
//	if(eqtype(t2, nilptr, 0))
//		return 1;

	if(isinter(t1))
1592
		if(ismethod(t2) || isinter(t2))
1593 1594 1595
			return 1;

	if(isinter(t2))
1596
		if(ismethod(t1))
1597 1598
			return 1;

1599 1600
	if(isptrdarray(t1))
		if(isptrarray(t2))
Ken Thompson's avatar
arrays  
Ken Thompson committed
1601
			return 1;
Ken Thompson's avatar
Ken Thompson committed
1602

1603 1604 1605 1606 1607 1608 1609
	return 0;
}

Node*
prcompat(Node *n)
{
	Node *l, *r;
Ken Thompson's avatar
Ken Thompson committed
1610
	Node *on;
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	Type *t;
	Iter save;
	int w;
	char *name;

	r = N;
	l = listfirst(&save, &n);

loop:
	if(l == N) {
1621
		walktype(r, Etop);
1622 1623 1624 1625 1626 1627
		return r;
	}

	w = whatis(l);
	switch(w) {
	default:
Ken Thompson's avatar
Ken Thompson committed
1628 1629
		if(l->type == T)
			goto out;
Ken Thompson's avatar
Ken Thompson committed
1630 1631 1632 1633
		if(isinter(l->type)) {
			on = syslook("printinter", 1);
			argtype(on, l->type);		// any-1
			break;
Ken Thompson's avatar
Ken Thompson committed
1634
		}
Ken Thompson's avatar
Ken Thompson committed
1635 1636 1637 1638 1639 1640 1641 1642
		if(isptr[l->type->etype]) {
			on = syslook("printpointer", 1);
			argtype(on, l->type->type);	// any-1
			break;
		}
		badtype(n->op, l->type, T);
		l = listnext(&save);
		goto loop;
Ken Thompson's avatar
Ken Thompson committed
1643

1644 1645
	case Wlitint:
	case Wtint:
Ken Thompson's avatar
Ken Thompson committed
1646
		on = syslook("printint", 0);
1647 1648 1649
		break;
	case Wlitfloat:
	case Wtfloat:
Ken Thompson's avatar
Ken Thompson committed
1650
		on = syslook("printfloat", 0);
1651 1652 1653
		break;
	case Wlitbool:
	case Wtbool:
Ken Thompson's avatar
Ken Thompson committed
1654
		on = syslook("printbool", 0);
1655 1656 1657
		break;
	case Wlitstr:
	case Wtstr:
Ken Thompson's avatar
Ken Thompson committed
1658
		on = syslook("printstring", 0);
1659 1660 1661
		break;
	}

1662
	t = *getinarg(on->type);
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
	if(t != nil)
		t = t->type;
	if(t != nil)
		t = t->type;

	if(!eqtype(t, l->type, 0)) {
		l = nod(OCONV, l, N);
		l->type = t;
	}

	if(r == N)
1674
		r = nod(OCALL, on, l);
1675
	else
Ken Thompson's avatar
select  
Ken Thompson committed
1676
		r = list(r, nod(OCALL, on, l));
1677

Ken Thompson's avatar
Ken Thompson committed
1678
out:
1679 1680 1681 1682 1683
	l = listnext(&save);
	goto loop;
}

Node*
1684
nodpanic(int32 lineno)
1685
{
1686
	Node *n, *on;
1687

1688
	on = syslook("panicl", 0);
1689
	n = nodintconst(lineno);
1690
	n = nod(OCALL, on, n);
1691
	walktype(n, Etop);
1692 1693 1694 1695 1696 1697
	return n;
}

Node*
newcompat(Node *n)
{
1698
	Node *r, *on;
1699
	Type *t;
1700 1701 1702 1703 1704 1705

	t = n->type;
	if(t == T || !isptr[t->etype] || t->type == T)
		fatal("newcompat: type should be pointer %lT", t);

	t = t->type;
Ken Thompson's avatar
arrays  
Ken Thompson committed
1706 1707
	switch(t->etype) {
	case TMAP:
1708 1709
		r = mapop(n, Erv);
		return r;
Ken Thompson's avatar
arrays  
Ken Thompson committed
1710 1711

	case TCHAN:
Ken Thompson's avatar
Ken Thompson committed
1712 1713
		r = chanop(n, Erv);
		return r;
Ken Thompson's avatar
arrays  
Ken Thompson committed
1714 1715 1716 1717

	case TARRAY:
		r = arrayop(n, Erv);
		return r;
Ken Thompson's avatar
Ken Thompson committed
1718
	}
1719 1720 1721 1722

	if(n->left != N)
		yyerror("dont know what new(,e) means");

1723 1724 1725
	dowidth(t);

	on = syslook("mal", 1);
1726

1727
	argtype(on, t);
1728

1729 1730 1731
	r = nodintconst(t->width);
	r = nod(OCALL, on, r);
	walktype(r, Erv);
1732 1733

//	r = nod(OCONV, r, N);
1734
	r->type = n->type;
1735 1736 1737 1738 1739

	return r;
}

Node*
1740
stringop(Node *n, int top)
1741
{
1742
	Node *r, *c, *on;
Ken Thompson's avatar
Ken Thompson committed
1743
	Type *t;
1744
	int32 l;
1745 1746 1747

	switch(n->op) {
	default:
Ken Thompson's avatar
chan  
Ken Thompson committed
1748
		fatal("stringop: unknown op %O", n->op);
1749 1750 1751 1752 1753 1754 1755 1756

	case OEQ:
	case ONE:
	case OGE:
	case OGT:
	case OLE:
	case OLT:
		// sys_cmpstring(s1, s2) :: 0
1757
		on = syslook("cmpstring", 0);
Ken Thompson's avatar
select  
Ken Thompson committed
1758
		r = list(n->left, n->right);
1759
		r = nod(OCALL, on, r);
1760 1761 1762 1763 1764 1765
		c = nodintconst(0);
		r = nod(n->op, r, c);
		break;

	case OADD:
		// sys_catstring(s1, s2)
1766
		on = syslook("catstring", 0);
Ken Thompson's avatar
select  
Ken Thompson committed
1767
		r = list(n->left, n->right);
1768
		r = nod(OCALL, on, r);
1769 1770 1771 1772 1773 1774
		break;

	case OASOP:
		// sys_catstring(s1, s2)
		switch(n->etype) {
		default:
Ken Thompson's avatar
chan  
Ken Thompson committed
1775
			fatal("stringop: unknown op %O-%O", n->op, n->etype);
1776 1777 1778

		case OADD:
			// s1 = sys_catstring(s1, s2)
1779 1780
			if(n->etype != OADD)
				fatal("stringop: not cat");
Ken Thompson's avatar
select  
Ken Thompson committed
1781
			r = list(n->left, n->right);
1782 1783
			on = syslook("catstring", 0);
			r = nod(OCALL, on, r);
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
			r = nod(OAS, n->left, r);
			break;
		}
		break;

	case OSLICE:
		// sys_slicestring(s, lb, hb)
		r = nod(OCONV, n->right->left, N);
		r->type = types[TINT32];

		c = nod(OCONV, n->right->right, N);
		c->type = types[TINT32];

Ken Thompson's avatar
select  
Ken Thompson committed
1797 1798
		r = list(r, c);
		r = list(n->left, r);
1799 1800
		on = syslook("slicestring", 0);
		r = nod(OCALL, on, r);
1801 1802
		break;

Ken Thompson's avatar
Ken Thompson committed
1803
	case OINDEX:
1804
		// sys_indexstring(s, i)
Ken Thompson's avatar
Ken Thompson committed
1805 1806 1807 1808 1809 1810
		c = n->left;
		if(isptrto(c->type->type, TSTRING)) {
			// lhs is string or *string
			c = nod(OIND, c, N);
			c->type = c->left->type->type;
		}
1811 1812
		r = nod(OCONV, n->right, N);
		r->type = types[TINT32];
Ken Thompson's avatar
select  
Ken Thompson committed
1813
		r = list(c, r);
1814 1815
		on = syslook("indexstring", 0);
		r = nod(OCALL, on, r);
1816 1817 1818 1819 1820 1821
		break;

	case OCONV:
		// sys_intstring(v)
		r = nod(OCONV, n->left, N);
		r->type = types[TINT64];
1822 1823
		on = syslook("intstring", 0);
		r = nod(OCALL, on, r);
1824 1825 1826
		break;

	case OARRAY:
Ken Thompson's avatar
Ken Thompson committed
1827 1828 1829 1830 1831
		// byteastring(*byte, int32) string;
		t = n->left->type;
		l = bytearraysz(t);

		// &a[0]
1832 1833 1834 1835
		c = nodintconst(0);
		r = nod(OINDEX, n->left, c);
		r = nod(OADDR, r, N);

Ken Thompson's avatar
Ken Thompson committed
1836 1837 1838 1839 1840 1841 1842
		if(l >= 0) {
			// static size
			c = nodintconst(l);
		} else {
			// dynamic size
			c = nod(OLEN, n->left, N);
		}
Ken Thompson's avatar
select  
Ken Thompson committed
1843
		r = list(r, c);
Ken Thompson's avatar
Ken Thompson committed
1844

1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
		on = syslook("byteastring", 0);
		r = nod(OCALL, on, r);
		break;
	}

	walktype(r, top);
	return r;
}

Type*
fixmap(Type *tm)
{
	Type *t;

	t = tm->type;
Ken Thompson's avatar
Ken Thompson committed
1860 1861 1862 1863 1864 1865
	if(t == T)
		goto bad;
	if(t->etype != TMAP)
		goto bad;
	if(t->down == T || t->type == T)
		goto bad;
1866 1867 1868 1869 1870

	dowidth(t->down);
	dowidth(t->type);

	return t;
Ken Thompson's avatar
Ken Thompson committed
1871 1872 1873 1874

bad:
	yyerror("not a map: %lT", tm);
	return T;
1875 1876
}

Ken Thompson's avatar
Ken Thompson committed
1877 1878 1879 1880 1881
Type*
fixchan(Type *tm)
{
	Type *t;

Ken Thompson's avatar
Ken Thompson committed
1882 1883
	if(tm == T) 
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
1884
	t = tm->type;
Ken Thompson's avatar
Ken Thompson committed
1885 1886 1887 1888 1889 1890
	if(t == T)
		goto bad;
	if(t->etype != TCHAN)
		goto bad;
	if(t->type == T)
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
1891 1892 1893 1894

	dowidth(t->type);

	return t;
Ken Thompson's avatar
Ken Thompson committed
1895 1896 1897 1898

bad:
	yyerror("not a channel: %lT", tm);
	return T;
Ken Thompson's avatar
Ken Thompson committed
1899 1900
}

1901 1902 1903 1904 1905
static int
algtype(Type *t)
{
	int a;

Ken Thompson's avatar
maps  
Ken Thompson committed
1906
	a = 100;
1907
	if(issimple[t->etype])
Ken Thompson's avatar
maps  
Ken Thompson committed
1908
		a = 0;		// simple mem
1909 1910
	else
	if(isptrto(t, TSTRING))
Ken Thompson's avatar
maps  
Ken Thompson committed
1911
		a = 1;		// string
1912 1913
	else
	if(isptr[t->etype])
Ken Thompson's avatar
maps  
Ken Thompson committed
1914
		a = 2;		// pointer
1915 1916
	else
	if(isinter(t))
Ken Thompson's avatar
maps  
Ken Thompson committed
1917
		a = 3;		// interface
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
	else
		fatal("algtype: cant find type %T", t);
	return a;
}

Node*
mapop(Node *n, int top)
{
	Node *r, *a;
	Type *t;
	Node *on;
Ken Thompson's avatar
Ken Thompson committed
1929
	int alg1, alg2, cl, cr;
1930

Ken Thompson's avatar
maps  
Ken Thompson committed
1931 1932
//dump("mapop", n);

1933 1934 1935
	r = n;
	switch(n->op) {
	default:
Ken Thompson's avatar
chan  
Ken Thompson committed
1936
		fatal("mapop: unknown op %O", n->op);
1937 1938

	case ONEW:
Ken Thompson's avatar
maps  
Ken Thompson committed
1939 1940 1941
		if(top != Erv)
			goto nottop;

1942 1943
		// newmap(keysize uint32, valsize uint32,
		//	keyalg uint32, valalg uint32,
Ken Thompson's avatar
maps  
Ken Thompson committed
1944
		//	hint uint32) (hmap *map[any-1]any-2);
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954

		t = fixmap(n->type);
		if(t == T)
			break;

		a = n->left;				// hint
		if(n->left == N)
			a = nodintconst(0);
		r = a;
		a = nodintconst(algtype(t->type));	// val algorithm
Ken Thompson's avatar
select  
Ken Thompson committed
1955
		r = list(a, r);
1956
		a = nodintconst(algtype(t->down));	// key algorithm
Ken Thompson's avatar
select  
Ken Thompson committed
1957
		r = list(a, r);
1958
		a = nodintconst(t->type->width);	// val width
Ken Thompson's avatar
select  
Ken Thompson committed
1959
		r = list(a, r);
1960
		a = nodintconst(t->down->width);	// key width
Ken Thompson's avatar
select  
Ken Thompson committed
1961
		r = list(a, r);
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972

		on = syslook("newmap", 1);

		argtype(on, t->down);	// any-1
		argtype(on, t->type);	// any-2

		r = nod(OCALL, on, r);
		walktype(r, top);
		r->type = n->type;
		break;

Ken Thompson's avatar
Ken Thompson committed
1973
	case OINDEX:
Ken Thompson's avatar
maps  
Ken Thompson committed
1974 1975
		if(top != Erv)
			goto nottop;
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
		// mapaccess1(hmap *map[any]any, key any) (val any);

		t = fixmap(n->left->type);
		if(t == T)
			break;

		convlit(n->right, t->down);

		if(!eqtype(n->right->type, t->down, 0)) {
			badtype(n->op, n->right->type, t->down);
			break;
		}

		a = n->right;				// key
Ken Thompson's avatar
Ken Thompson committed
1990 1991 1992 1993 1994
//		if(!isptr[t->down->etype]) {
//			a = nod(OADDR, a, N);
//			a->type = ptrto(t);
//		}

1995 1996
		r = a;
		a = n->left;				// map
Ken Thompson's avatar
select  
Ken Thompson committed
1997
		r = list(a, r);
1998

Ken Thompson's avatar
Ken Thompson committed
1999
		on = syslook("mapaccess1", 1);
2000 2001 2002 2003 2004 2005 2006 2007 2008

		argtype(on, t->down);	// any-1
		argtype(on, t->type);	// any-2
		argtype(on, t->down);	// any-3
		argtype(on, t->type);	// any-4

		r = nod(OCALL, on, r);
		walktype(r, Erv);
		r->type = t->type;
2009 2010
		break;

Ken Thompson's avatar
Ken Thompson committed
2011 2012 2013
	case OAS:
		cl = listcount(n->left);
		cr = listcount(n->right);
Ken Thompson's avatar
maps  
Ken Thompson committed
2014

Ken Thompson's avatar
Ken Thompson committed
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
		if(cl == 1 && cr == 2)
			goto assign2;
		if(cl == 2 && cr == 1)
			goto access2;
		if(cl != 1 || cr != 1)
			goto shape;

		// mapassign1(hmap *map[any-1]any-2, key any-3, val any-4);
		if(n->left->op != OINDEX)
			goto shape;

		t = fixmap(n->left->left->type);
Ken Thompson's avatar
maps  
Ken Thompson committed
2027 2028 2029
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2030
		a = n->right;				// val
Ken Thompson's avatar
maps  
Ken Thompson committed
2031
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2032
		a = n->left->right;			// key
Ken Thompson's avatar
select  
Ken Thompson committed
2033
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2034
		a = n->left->left;			// map
Ken Thompson's avatar
select  
Ken Thompson committed
2035
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2036

Ken Thompson's avatar
Ken Thompson committed
2037
		on = syslook("mapassign1", 1);
Ken Thompson's avatar
maps  
Ken Thompson committed
2038 2039 2040 2041 2042 2043

		argtype(on, t->down);	// any-1
		argtype(on, t->type);	// any-2
		argtype(on, t->down);	// any-3
		argtype(on, t->type);	// any-4

Ken Thompson's avatar
Ken Thompson committed
2044
		r = nod(OCALL, on, r);
2045
		walktype(r, Etop);
Ken Thompson's avatar
maps  
Ken Thompson committed
2046 2047
		break;

Ken Thompson's avatar
Ken Thompson committed
2048 2049 2050 2051
	assign2:
		// mapassign2(hmap *map[any]any, key any, val any, pres bool);
		if(n->left->op != OINDEX)
			goto shape;
Ken Thompson's avatar
maps  
Ken Thompson committed
2052 2053 2054 2055 2056

		t = fixmap(n->left->left->type);
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2057
		a = n->right->right;			// pres
Ken Thompson's avatar
maps  
Ken Thompson committed
2058
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2059
		a = n->right->left;			// val
Ken Thompson's avatar
select  
Ken Thompson committed
2060
		r =list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2061
		a = n->left->right;			// key
Ken Thompson's avatar
select  
Ken Thompson committed
2062
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2063
		a = n->left->left;			// map
Ken Thompson's avatar
select  
Ken Thompson committed
2064
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2065

Ken Thompson's avatar
Ken Thompson committed
2066
		on = syslook("mapassign2", 1);
Ken Thompson's avatar
maps  
Ken Thompson committed
2067 2068 2069 2070 2071 2072 2073

		argtype(on, t->down);	// any-1
		argtype(on, t->type);	// any-2
		argtype(on, t->down);	// any-3
		argtype(on, t->type);	// any-4

		r = nod(OCALL, on, r);
2074
		walktype(r, Etop);
Ken Thompson's avatar
maps  
Ken Thompson committed
2075 2076
		break;

Ken Thompson's avatar
Ken Thompson committed
2077 2078
	access2:
		// mapaccess2(hmap *map[any-1]any-2, key any-3) (val-4 any, pres bool);
Ken Thompson's avatar
maps  
Ken Thompson committed
2079

Ken Thompson's avatar
Ken Thompson committed
2080 2081 2082
//dump("access2", n);
		if(n->right->op != OINDEX)
			goto shape;
Ken Thompson's avatar
maps  
Ken Thompson committed
2083

Ken Thompson's avatar
Ken Thompson committed
2084
		t = fixmap(n->right->left->type);
Ken Thompson's avatar
maps  
Ken Thompson committed
2085 2086 2087
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2088
		a = n->right->right;			// key
Ken Thompson's avatar
maps  
Ken Thompson committed
2089
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2090
		a = n->right->left;			// map
Ken Thompson's avatar
select  
Ken Thompson committed
2091
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2092

Ken Thompson's avatar
Ken Thompson committed
2093
		on = syslook("mapaccess2", 1);
Ken Thompson's avatar
maps  
Ken Thompson committed
2094 2095 2096 2097 2098 2099

		argtype(on, t->down);	// any-1
		argtype(on, t->type);	// any-2
		argtype(on, t->down);	// any-3
		argtype(on, t->type);	// any-4

Ken Thompson's avatar
Ken Thompson committed
2100 2101 2102
		n->right = nod(OCALL, on, r);
		walktype(n, Etop);
		r = n;
Ken Thompson's avatar
maps  
Ken Thompson committed
2103 2104
		break;

2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
	case OASOP:
		// rewrite map[index] op= right
		// into tmpi := index; map[tmpi] = map[tmpi] op right

		t = n->left->left->type->type;
		a = nod(OXXX, N, N);
		tempname(a, t->down);			// tmpi
		r = nod(OAS, a, n->left->right);	// tmpi := index
		n->left->right = a;			// m[tmpi]

		a = nod(OXXX, N, N);
Ken Thompson's avatar
Ken Thompson committed
2116
		indir(a, n->left);			// copy of map[tmpi]
2117 2118 2119
		a = nod(n->etype, a, n->right);		// m[tmpi] op right
		a = nod(OAS, n->left, a);		// map[tmpi] = map[tmpi] op right
		r = nod(OLIST, r, a);
Ken Thompson's avatar
maps  
Ken Thompson committed
2120
	}
2121
	return r;
Ken Thompson's avatar
maps  
Ken Thompson committed
2122

Ken Thompson's avatar
Ken Thompson committed
2123 2124 2125 2126 2127
shape:
	dump("shape", n);
	fatal("mapop: cl=%d cr=%d, %O", top, n->op);
	return N;

Ken Thompson's avatar
maps  
Ken Thompson committed
2128 2129 2130 2131
nottop:
	dump("bad top", n);
	fatal("mapop: top=%d %O", top, n->op);
	return N;
2132 2133
}

Ken Thompson's avatar
Ken Thompson committed
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
Node*
chanop(Node *n, int top)
{
	Node *r, *a;
	Type *t;
	Node *on;
	int alg, cl, cr;

//dump("chanop", n);

	r = n;
	switch(n->op) {
	default:
Ken Thompson's avatar
chan  
Ken Thompson committed
2147
		fatal("chanop: unknown op %O", n->op);
Ken Thompson's avatar
Ken Thompson committed
2148 2149 2150 2151 2152 2153 2154 2155 2156

	case ONEW:
		// newchan(elemsize uint32, elemalg uint32,
		//	hint uint32) (hmap *chan[any-1]);

		t = fixchan(n->type);
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2157 2158 2159 2160 2161
		if(n->left != N) {
			// async buf size
			a = nod(OCONV, n->left, N);
			a->type = types[TUINT32];
		} else
Ken Thompson's avatar
Ken Thompson committed
2162
			a = nodintconst(0);
Ken Thompson's avatar
Ken Thompson committed
2163

Ken Thompson's avatar
Ken Thompson committed
2164 2165
		r = a;
		a = nodintconst(algtype(t->type));	// elem algorithm
Ken Thompson's avatar
select  
Ken Thompson committed
2166
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2167
		a = nodintconst(t->type->width);	// elem width
Ken Thompson's avatar
select  
Ken Thompson committed
2168
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2169 2170 2171 2172 2173 2174 2175 2176

		on = syslook("newchan", 1);
		argtype(on, t->type);	// any-1

		r = nod(OCALL, on, r);
		walktype(r, top);
		r->type = n->type;
		break;
Ken Thompson's avatar
chan  
Ken Thompson committed
2177 2178

	case OAS:
Ken Thompson's avatar
chan  
Ken Thompson committed
2179 2180
		cl = listcount(n->left);
		cr = listcount(n->right);
Ken Thompson's avatar
chan  
Ken Thompson committed
2181

Ken Thompson's avatar
Ken Thompson committed
2182
		if(cl != 2 || cr != 1 || n->right->op != ORECV)
Ken Thompson's avatar
chan  
Ken Thompson committed
2183 2184
			goto shape;

Ken Thompson's avatar
Ken Thompson committed
2185 2186
		// chanrecv2(hchan *chan any) (elem any, pres bool);
		t = fixchan(n->right->left->type);
Ken Thompson's avatar
chan  
Ken Thompson committed
2187 2188 2189
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2190
		a = n->right->left;			// chan
Ken Thompson's avatar
chan  
Ken Thompson committed
2191 2192
		r = a;

Ken Thompson's avatar
Ken Thompson committed
2193 2194
		on = syslook("chanrecv2", 1);

Ken Thompson's avatar
chan  
Ken Thompson committed
2195 2196 2197
		argtype(on, t->type);	// any-1
		argtype(on, t->type);	// any-2
		r = nod(OCALL, on, r);
Ken Thompson's avatar
Ken Thompson committed
2198 2199 2200
		n->right = r;
		r = n;
		walktype(r, Etop);
Ken Thompson's avatar
chan  
Ken Thompson committed
2201 2202
		break;

Ken Thompson's avatar
chan  
Ken Thompson committed
2203
	case ORECV:
Ken Thompson's avatar
Ken Thompson committed
2204 2205 2206
		if(n->right != N)
			goto recv2;

Ken Thompson's avatar
chan  
Ken Thompson committed
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
		// chanrecv1(hchan *chan any) (elem any);

		t = fixchan(n->left->type);
		if(t == T)
			break;

		a = n->left;			// chan
		r = a;

		on = syslook("chanrecv1", 1);

		argtype(on, t->type);	// any-1
		argtype(on, t->type);	// any-2
		r = nod(OCALL, on, r);
		walktype(r, Erv);
		break;

	recv2:
Ken Thompson's avatar
Ken Thompson committed
2225
		// chanrecv3(hchan *chan any, *elem any) (pres bool);
Ken Thompson's avatar
Ken Thompson committed
2226
		t = fixchan(n->right->type);
Ken Thompson's avatar
chan  
Ken Thompson committed
2227 2228 2229
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2230
		a = n->right;			// chan
Ken Thompson's avatar
chan  
Ken Thompson committed
2231
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2232 2233 2234 2235 2236 2237
		a = n->left;			// elem
		if(a == N) {
			a = nod(OLITERAL, N, N);
			a->val.ctype = CTNIL;
		} else
			a = nod(OADDR, a, N);
Ken Thompson's avatar
chan  
Ken Thompson committed
2238

Ken Thompson's avatar
Ken Thompson committed
2239
		on = syslook("chanrecv3", 1);
Ken Thompson's avatar
chan  
Ken Thompson committed
2240 2241 2242

		argtype(on, t->type);	// any-1
		argtype(on, t->type);	// any-2
Ken Thompson's avatar
Ken Thompson committed
2243

Ken Thompson's avatar
chan  
Ken Thompson committed
2244 2245 2246 2247 2248
		r = nod(OCALL, on, r);
		n->right = r;
		r = n;
		walktype(r, Etop);
		break;
Ken Thompson's avatar
Ken Thompson committed
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264

	case OSEND:
		t = fixchan(n->left->type);
		if(t == T)
			break;
		if(top != Etop)
			goto send2;

		// chansend1(hchan *chan any, elem any);
		t = fixchan(n->left->type);
		if(t == T)
			break;

		a = n->right;			// e
		r = a;
		a = n->left;			// chan
Ken Thompson's avatar
select  
Ken Thompson committed
2265
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2266 2267 2268 2269 2270

		on = syslook("chansend1", 1);
		argtype(on, t->type);	// any-1
		argtype(on, t->type);	// any-2
		r = nod(OCALL, on, r);
Ken Thompson's avatar
Ken Thompson committed
2271
		walktype(r, Etop);
Ken Thompson's avatar
Ken Thompson committed
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
		break;

	send2:
		// chansend2(hchan *chan any, val any) (pres bool);
		t = fixchan(n->left->type);
		if(t == T)
			break;

		a = n->right;			// e
		r = a;
		a = n->left;			// chan
Ken Thompson's avatar
select  
Ken Thompson committed
2283
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2284 2285 2286 2287 2288

		on = syslook("chansend2", 1);
		argtype(on, t->type);	// any-1
		argtype(on, t->type);	// any-2
		r = nod(OCALL, on, r);
Ken Thompson's avatar
Ken Thompson committed
2289
		walktype(r, Etop);
Ken Thompson's avatar
Ken Thompson committed
2290
		break;
Ken Thompson's avatar
Ken Thompson committed
2291 2292
	}
	return r;
Ken Thompson's avatar
chan  
Ken Thompson committed
2293 2294 2295 2296

shape:
	fatal("chanop: %O", n->op);
	return N;
Ken Thompson's avatar
Ken Thompson committed
2297 2298
}

Ken Thompson's avatar
arrays  
Ken Thompson committed
2299 2300 2301 2302 2303 2304
Type*
fixarray(Type *tm)
{
	Type *t;

	t = tm->type;
Ken Thompson's avatar
Ken Thompson committed
2305 2306 2307 2308 2309 2310
	if(t == T)
		goto bad;
	if(t->etype != TARRAY)
		goto bad;
	if(t->type == T)
		goto bad;
Ken Thompson's avatar
arrays  
Ken Thompson committed
2311 2312 2313 2314

	dowidth(t->type);

	return t;
Ken Thompson's avatar
Ken Thompson committed
2315 2316 2317 2318 2319

bad:
	yyerror("not an array: %lT", tm);
	return T;
	
Ken Thompson's avatar
arrays  
Ken Thompson committed
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
}

Node*
arrayop(Node *n, int top)
{
	Node *r, *a;
	Type *t;
	Node *on;
	Iter save;

	r = n;
	switch(n->op) {
	default:
		fatal("darrayop: unknown op %O", n->op);

	case ONEW:
		// newarray(nel uint32, max uint32, width uint32) (ary *[]any)
		t = fixarray(n->type);

		a = nodintconst(t->type->width);	// width
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = a;

		a = listfirst(&save, &n->left);		// max
2345
		a = listnext(&save);
Ken Thompson's avatar
arrays  
Ken Thompson committed
2346 2347 2348 2349 2350 2351
		if(a == N)
			a = nodintconst(0);
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = list(a, r);

2352 2353 2354 2355
		a = listfirst(&save, &n->left);		// nel
		if(a == N) {
			if(t->bound < 0)
				yyerror("new open array must have size");
Ken Thompson's avatar
arrays  
Ken Thompson committed
2356
			a = nodintconst(t->bound);
2357
		}
Ken Thompson's avatar
arrays  
Ken Thompson committed
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = list(a, r);

		on = syslook("newarray", 1);
		argtype(on, t->type);			// any-1
		r = nod(OCALL, on, r);

		walktype(r, top);
		if(t->etype == TARRAY) {
			// single case when we can convert a dynamic
			// array pointer to a static array pointer
			// saves making a sys function to alloc a static
			r = nod(OCONV, r, N);
			r->type = ptrto(t);
		}
		break;

	case OAS:
		// arrays2d(old *any, nel uint32) (ary *[]any)
		t = fixarray(n->right->type);

		a = nodintconst(t->bound);		// nel
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = a;

		a = n->right;				// old
		r = list(a, r);

		on = syslook("arrays2d", 1);
		argtype(on, n->right->type->type);	// any-1
		argtype(on, t->type);			// any-2
		r = nod(OCALL, on, r);

		walktype(r, top);
		n->right = r;
		return n;

	case OSLICE:
2398
		if(isptrarray(n->left->type))
Ken Thompson's avatar
arrays  
Ken Thompson committed
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
			goto slicestatic;

		// arrayslices(old *[]any, lb uint32, hb uint32, width uint32) (ary *[]any)
		t = fixarray(n->left->type);

		a = nodintconst(t->type->width);	// width
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = a;

		a = nod(OCONV, n->right->right, N);	// hb
		a->type = types[TUINT32];
		r = list(a, r);

		a = nod(OCONV, n->right->left, N);	// lb
2414
		a->type = types[TUINT32];
Ken Thompson's avatar
arrays  
Ken Thompson committed
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
		r = list(a, r);

		a = n->left;				// old
		r = list(a, r);

		on = syslook("arraysliced", 1);
		argtype(on, t->type);			// any-1
		argtype(on, t->type);			// any-2
		r = nod(OCALL, on, r);
		walktype(r, top);
		break;

	slicestatic:
		// arrayslices(old *any, nel uint32, lb uint32, hb uint32, width uint32) (ary *[]any)
		t = fixarray(n->left->type);

		a = nodintconst(t->type->width);	// width
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = a;

		a = nod(OCONV, n->right->right, N);	// hb
		a->type = types[TUINT32];
		r = list(a, r);

		a = nod(OCONV, n->right->left, N);	// lb
2441
		a->type = types[TUINT32];
Ken Thompson's avatar
arrays  
Ken Thompson committed
2442 2443 2444 2445
		r = list(a, r);

		a = nodintconst(t->bound);		// nel
		a = nod(OCONV, a, N);
2446
		a->type = types[TUINT32];
Ken Thompson's avatar
arrays  
Ken Thompson committed
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
		r = list(a, r);

		a = n->left;				// old
		r = list(a, r);

		on = syslook("arrayslices", 1);
		argtype(on, t);				// any-1
		argtype(on, t->type);			// any-2
		r = nod(OCALL, on, r);
		walktype(r, top);
		break;
	}
	return r;
}

Ken Thompson's avatar
Ken Thompson committed
2462
int
Ken Thompson's avatar
Ken Thompson committed
2463
isandss(Type *lt, Node *r)
2464
{
Ken Thompson's avatar
Ken Thompson committed
2465 2466
	Type *rt;
	Node *n;
2467
	int o;
Ken Thompson's avatar
Ken Thompson committed
2468 2469 2470

	rt = r->type;
	if(isinter(lt)) {
Ken Thompson's avatar
Ken Thompson committed
2471 2472 2473 2474
		if(ismethod(rt))
			return T2I;
		if(isinter(rt) && !eqtype(rt, lt, 0))
			return I2I;
Ken Thompson's avatar
Ken Thompson committed
2475 2476
	}

2477
	if(ismethod(lt)) {
Ken Thompson's avatar
Ken Thompson committed
2478 2479
		if(isinter(rt))
			return I2T;
Ken Thompson's avatar
Ken Thompson committed
2480 2481
	}

Ken Thompson's avatar
Ken Thompson committed
2482 2483
	return Inone;
}
Ken Thompson's avatar
Ken Thompson committed
2484

Ken Thompson's avatar
Ken Thompson committed
2485 2486 2487 2488 2489 2490
Node*
ifaceop(Type *tl, Node *n, int op)
{
	Type *tr;
	Node *r, *a, *on;
	Sym *s;
Ken Thompson's avatar
Ken Thompson committed
2491

Ken Thompson's avatar
Ken Thompson committed
2492 2493 2494 2495 2496 2497 2498
	tr = n->type;

	switch(op) {
	default:
		fatal("ifaceop: unknown op %d\n", op);

	case I2T:
Ken Thompson's avatar
Ken Thompson committed
2499
		// ifaceI2T(sigt *byte, iface any) (ret any);
Ken Thompson's avatar
Ken Thompson committed
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511

		a = n;				// interface
		r = a;

		s = signame(tl);		// sigi
		if(s == S)
			fatal("ifaceop: signame I2T");
		a = s->oname;
		a = nod(OADDR, a, N);
		r = list(a, r);

		on = syslook("ifaceI2T", 1);
Ken Thompson's avatar
Ken Thompson committed
2512
		argtype(on, tr);
Ken Thompson's avatar
Ken Thompson committed
2513 2514 2515 2516 2517
		argtype(on, tl);

		break;

	case T2I:
Ken Thompson's avatar
Ken Thompson committed
2518
		// ifaceT2I(sigi *byte, sigt *byte, elem any) (ret any);
Ken Thompson's avatar
Ken Thompson committed
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539

		a = n;				// elem
		r = a;

		s = signame(tr);		// sigt
		if(s == S)
			fatal("ifaceop: signame-1 T2I: %lT", tr);
		a = s->oname;
		a = nod(OADDR, a, N);
		r = list(a, r);

		s = signame(tl);		// sigi
		if(s == S) {
			fatal("ifaceop: signame-2 T2I: %lT", tl);
		}
		a = s->oname;
		a = nod(OADDR, a, N);
		r = list(a, r);

		on = syslook("ifaceT2I", 1);
		argtype(on, tr);
Ken Thompson's avatar
Ken Thompson committed
2540
		argtype(on, tl);
Ken Thompson's avatar
Ken Thompson committed
2541 2542 2543 2544 2545 2546 2547 2548

		break;

	case I2I:
		// ifaceI2I(sigi *byte, iface any-1) (ret any-2);

		a = n;				// interface
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2549 2550

		s = signame(tl);		// sigi
Ken Thompson's avatar
Ken Thompson committed
2551 2552 2553 2554 2555
		if(s == S)
			fatal("ifaceop: signame I2I");
		a = s->oname;
		a = nod(OADDR, a, N);
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2556

Ken Thompson's avatar
Ken Thompson committed
2557 2558
		on = syslook("ifaceI2I", 1);
		argtype(on, tr);
Ken Thompson's avatar
Ken Thompson committed
2559
		argtype(on, tl);
Ken Thompson's avatar
Ken Thompson committed
2560 2561 2562 2563 2564 2565
		break;
	}

	r = nod(OCALL, on, r);
	walktype(r, Erv);
	return r;
Ken Thompson's avatar
Ken Thompson committed
2566 2567 2568 2569 2570
}

Node*
convas(Node *n)
{
2571 2572
	Node *l, *r;
	Type *lt, *rt;
Ken Thompson's avatar
Ken Thompson committed
2573
	int et;
2574 2575

	if(n->op != OAS)
2576
		fatal("convas: not OAS %O", n->op);
2577 2578 2579 2580

	l = n->left;
	r = n->right;
	if(l == N || r == N)
2581
		goto out;
2582 2583 2584 2585

	lt = l->type;
	rt = r->type;
	if(lt == T || rt == T)
2586
		goto out;
2587

Ken Thompson's avatar
maps  
Ken Thompson committed
2588 2589
	if(n->left->op == OINDEX)
	if(isptrto(n->left->left->type, TMAP)) {
Ken Thompson's avatar
Ken Thompson committed
2590
		indir(n, mapop(n, Elv));
2591
		goto out;
Ken Thompson's avatar
maps  
Ken Thompson committed
2592 2593 2594 2595
	}

	if(n->left->op == OINDEXPTR)
	if(n->left->left->type->etype == TMAP) {
Ken Thompson's avatar
Ken Thompson committed
2596
		indir(n, mapop(n, Elv));
2597
		goto out;
Ken Thompson's avatar
maps  
Ken Thompson committed
2598 2599
	}

Ken Thompson's avatar
chan  
Ken Thompson committed
2600 2601
	if(n->left->op == OSEND)
	if(n->left->type != T) {
Ken Thompson's avatar
Ken Thompson committed
2602
		indir(n, chanop(n, Elv));
2603
		goto out;
Ken Thompson's avatar
chan  
Ken Thompson committed
2604 2605
	}

2606
	if(eqtype(lt, rt, 0))
2607
		goto out;
2608

Ken Thompson's avatar
Ken Thompson committed
2609 2610 2611
	et = isandss(lt, r);
	if(et != Inone) {
		n->right = ifaceop(lt, r, et);
2612
		goto out;
2613 2614
	}

2615
	if(isptrdarray(lt) && isptrarray(rt)) {
Ken Thompson's avatar
arrays  
Ken Thompson committed
2616 2617
		if(!eqtype(lt->type->type, rt->type->type, 0))
			goto bad;
Ken Thompson's avatar
Ken Thompson committed
2618
		indir(n, arrayop(n, Etop));
2619
		goto out;
Ken Thompson's avatar
arrays  
Ken Thompson committed
2620 2621 2622
	}

	if(ascompat(lt, rt))
2623
		goto out;
Ken Thompson's avatar
arrays  
Ken Thompson committed
2624 2625

bad:
2626
	badtype(n->op, lt, rt);
2627 2628 2629

out:
	ullmancalc(n);
2630 2631 2632
	return n;
}

2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
Node*
old2new(Node *n, Type *t)
{
	Node *l;

	if(n->op != ONAME && n->op != ONONAME) {
		yyerror("left side of := must be a name");
		return n;
	}
	l = newname(n->sym);
	dodclvar(l, t);
	return l;
}

Node*
colas(Node *nl, Node *nr)
{
	Iter savel, saver;
	Node *l, *r, *a, *n;
	Type *t;
	int cl, cr;

	/* nl is an expression list.
	 * nr is an expression list.
	 * return a newname-list from
Ken Thompson's avatar
maps  
Ken Thompson committed
2658
	 * types derived from the rhs.
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
	 */
	n = N;
	cr = listcount(nr);
	cl = listcount(nl);
	if(cl != cr) {
		if(cr == 1)
			goto multi;
		goto badt;
	}

	l = listfirst(&savel, &nl);
	r = listfirst(&saver, &nr);

Ken Thompson's avatar
maps  
Ken Thompson committed
2672 2673 2674 2675 2676 2677 2678
	while(l != N) {
		walktype(r, Erv);
		defaultlit(r);
		a = old2new(l, r->type);
		if(n == N)
			n = a;
		else
Ken Thompson's avatar
select  
Ken Thompson committed
2679
			n = list(n, a);
Ken Thompson's avatar
maps  
Ken Thompson committed
2680 2681 2682 2683

		l = listnext(&savel);
		r = listnext(&saver);
	}
Ken Thompson's avatar
Ken Thompson committed
2684
	n = rev(n);
Ken Thompson's avatar
maps  
Ken Thompson committed
2685
	return n;
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702

multi:
	/*
	 * there is a list on the left
	 * and a mono on the right.
	 * go into the right to get
	 * individual types for the left.
	 */
	switch(nr->op) {
	default:
		goto badt;

	case OCALLMETH:
	case OCALLINTER:
	case OCALL:
		walktype(nr->left, Erv);
		t = nr->left->type;
Russ Cox's avatar
Russ Cox committed
2703 2704
		if(t != T && t->etype == tptr)
			t = t->type;
2705 2706 2707 2708 2709 2710 2711 2712
		if(t == T || t->etype != TFUNC)
			goto badt;
		if(t->outtuple != cl)
			goto badt;

		l = listfirst(&savel, &nl);
		t = structfirst(&saver, getoutarg(t));
		while(l != N) {
Ken Thompson's avatar
Ken Thompson committed
2713
			a = old2new(l, t->type);
2714 2715 2716
			if(n == N)
				n = a;
			else
Ken Thompson's avatar
select  
Ken Thompson committed
2717
				n = list(n, a);
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
			l = listnext(&savel);
			t = structnext(&saver);
		}
		break;

	case OINDEX:
	case OINDEXPTR:
		// check if rhs is a map index.
		// if so, types are bool,maptype
		if(cl != 2)
			goto badt;
		walktype(nr->left, Elv);
		t = nr->left->type;
		if(t != T && isptr[t->etype])
			t = t->type;
		if(t == T || t->etype != TMAP)
			goto badt;

Ken Thompson's avatar
maps  
Ken Thompson committed
2736
		a = old2new(nl->left, t->type);
2737
		n = a;
Ken Thompson's avatar
maps  
Ken Thompson committed
2738
		a = old2new(nl->right, types[TBOOL]);
Ken Thompson's avatar
select  
Ken Thompson committed
2739
		n = list(n, a);
2740
		break;
Ken Thompson's avatar
chan  
Ken Thompson committed
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751

	case ORECV:
		if(cl != 2)
			goto badt;
		walktype(nr->left, Erv);
		t = nr->left->type;
		if(!isptrto(t, TCHAN))
			goto badt;
		a = old2new(nl->left, t->type->type);
		n = a;
		a = old2new(nl->right, types[TBOOL]);
Ken Thompson's avatar
select  
Ken Thompson committed
2752
		n = list(n, a);
2753
	}
Ken Thompson's avatar
Ken Thompson committed
2754
	n = rev(n);
2755 2756 2757 2758 2759 2760 2761
	return n;

badt:
	yyerror("shape error across :=");
	return nl;
}

2762 2763 2764 2765 2766 2767 2768 2769
/*
 * from ascompat[te]
 * evaluating actual function arguments.
 *	f(a,b)
 * if there is exactly one function expr,
 * then it is done first. otherwise must
 * make temp variables
 */
Ken Thompson's avatar
Ken Thompson committed
2770 2771 2772 2773
Node*
reorder1(Node *n)
{
	Iter save;
Ken Thompson's avatar
Ken Thompson committed
2774
	Node *l, *r, *f, *a, *g;
Ken Thompson's avatar
Ken Thompson committed
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
	int c, t;

	l = listfirst(&save, &n);
	c = 0;	// function calls
	t = 0;	// total parameters

loop1:
	if(l == N) {
		if(c == 0 || t == 1)
			return n;
		goto pass2;
	}
	if(l->op == OLIST)
		fatal("reorder1 OLIST");

	t++;
	if(l->ullman >= UINF)
		c++;
	l = listnext(&save);
	goto loop1;

pass2:
	l = listfirst(&save, &n);
Ken Thompson's avatar
Ken Thompson committed
2798 2799 2800
	g = N;	// fncalls assigned to tempnames
	f = N;	// one fncall assigned to stack
	r = N;	// non fncalls and tempnames assigned to stack
Ken Thompson's avatar
Ken Thompson committed
2801 2802 2803

loop2:
	if(l == N) {
2804
		r = rev(r);
Ken Thompson's avatar
Ken Thompson committed
2805 2806
		g = rev(g);
		if(g != N)
Ken Thompson's avatar
select  
Ken Thompson committed
2807 2808
			f = list(g, f);
		r = list(f, r);
2809
		return r;
Ken Thompson's avatar
Ken Thompson committed
2810
	}
Ken Thompson's avatar
Ken Thompson committed
2811 2812 2813 2814
	if(l->ullman < UINF) {
		if(r == N)
			r = l;
		else
Ken Thompson's avatar
select  
Ken Thompson committed
2815
			r = list(l, r);
Ken Thompson's avatar
Ken Thompson committed
2816 2817 2818
		goto more;
	}
	if(f == N) {
Ken Thompson's avatar
Ken Thompson committed
2819
		f = l;
Ken Thompson's avatar
Ken Thompson committed
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
		goto more;
	}

	// make assignment of fncall to tempname
	a = nod(OXXX, N, N);
	tempname(a, l->right->type);
	a = nod(OAS, a, l->right);

	if(g == N)
		g = a;
2830
	else
Ken Thompson's avatar
select  
Ken Thompson committed
2831
		g = list(a, g);
Ken Thompson's avatar
Ken Thompson committed
2832 2833 2834 2835

	// put normal arg assignment on list
	// with fncall replaced by tempname
	l->right = a->left;
Ken Thompson's avatar
Ken Thompson committed
2836 2837 2838
	if(r == N)
		r = l;
	else
Ken Thompson's avatar
select  
Ken Thompson committed
2839
		r = list(l, r);
Ken Thompson's avatar
Ken Thompson committed
2840

Ken Thompson's avatar
Ken Thompson committed
2841
more:
Ken Thompson's avatar
Ken Thompson committed
2842 2843 2844 2845
	l = listnext(&save);
	goto loop2;
}

2846 2847 2848 2849 2850 2851 2852
/*
 * from ascompat[et]
 *	a,b = f()
 * return of a multi.
 * there can be no function calls at all,
 * or they will over-write the return values.
 */
Ken Thompson's avatar
Ken Thompson committed
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
Node*
reorder2(Node *n)
{
	Iter save;
	Node *l;
	int c;

	l = listfirst(&save, &n);
	c = 0;

loop1:
	if(l == N) {
		if(c > 0)
			yyerror("reorder2: too many funcation calls evaluating parameters");
		return n;
	}
	if(l->op == OLIST)
		fatal("reorder2 OLIST");

	if(l->ullman >= UINF)
		c++;
	l = listnext(&save);
	goto loop1;
}

2878 2879 2880
/*
 * from ascompat[ee]
 *	a,b = c,d
Ken Thompson's avatar
Ken Thompson committed
2881 2882
 * simultaneous assignment. there cannot
 * be later use of an earlier lvalue.
2883 2884
 */
int
Ken Thompson's avatar
Ken Thompson committed
2885
vmatch2(Node *l, Node *r)
2886
{
Ken Thompson's avatar
Ken Thompson committed
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 2923 2924 2925 2926 2927 2928

loop:
	/*
	 * isolate all right sides
	 */
	if(r == N)
		return 0;
	switch(r->op) {
	case ONAME:
		// match each right given left
		if(l == r)
			return 1;
	case OLITERAL:
		return 0;
	}
	if(vmatch2(l, r->right))
		return 1;
	r = r->left;
	goto loop;
}

int
vmatch1(Node *l, Node *r)
{

loop:
	/*
	 * isolate all left sides
	 */
	if(l == N)
		return 0;
	switch(l->op) {
	case ONAME:
		// match each left with all rights
		return vmatch2(l, r);
	case OLITERAL:
		return 0;
	}
	if(vmatch1(l->right, r))
		return 1;
	l = l->left;
	goto loop;
2929 2930
}

Ken Thompson's avatar
Ken Thompson committed
2931 2932 2933
Node*
reorder3(Node *n)
{
2934
	Iter save1, save2;
Ken Thompson's avatar
Ken Thompson committed
2935
	Node *l1, *l2, *q, *r;
2936 2937
	int c1, c2;

Ken Thompson's avatar
Ken Thompson committed
2938 2939
	r = N;

2940 2941 2942 2943
	l1 = listfirst(&save1, &n);
	c1 = 0;

	while(l1 != N) {
Ken Thompson's avatar
Ken Thompson committed
2944
		l2 = listfirst(&save2, &n);
2945 2946 2947
		c2 = 0;
		while(l2 != N) {
			if(c2 > c1) {
Ken Thompson's avatar
Ken Thompson committed
2948 2949 2950 2951 2952 2953 2954 2955
				if(vmatch1(l1->left, l2->right)) {
					q = nod(OXXX, N, N);
					tempname(q, l2->right->type);
					q = nod(OAS, l1->left, q);
					l1->left = q->right;
					if(r == N)
						r = q;
					else
Ken Thompson's avatar
select  
Ken Thompson committed
2956
						r = list(r, q);
Ken Thompson's avatar
Ken Thompson committed
2957
					break;
2958 2959
				}
			}
Ken Thompson's avatar
Ken Thompson committed
2960
			l2 = listnext(&save2);
2961 2962 2963 2964 2965
			c2++;
		}
		l1 = listnext(&save1);
		c1++;
	}
Ken Thompson's avatar
Ken Thompson committed
2966 2967 2968 2969 2970 2971 2972 2973 2974
	if(r == N)
		return n;

	q = N;
	l1 = listfirst(&save1, &n);
	while(l1 != N) {
		if(q == N)
			q = l1;
		else
Ken Thompson's avatar
select  
Ken Thompson committed
2975
			q = list(q, l1);
Ken Thompson's avatar
Ken Thompson committed
2976 2977 2978 2979 2980 2981 2982 2983 2984
		l1 = listnext(&save1);
	}

	r = rev(r);
	l1 = listfirst(&save1, &r);
	while(l1 != N) {
		if(q == N)
			q = l1;
		else
Ken Thompson's avatar
select  
Ken Thompson committed
2985
			q = list(q, l1);
Ken Thompson's avatar
Ken Thompson committed
2986 2987 2988 2989 2990 2991
		l1 = listnext(&save1);
	}

	q = rev(q);
//dump("res", q);
	return q;
Ken Thompson's avatar
Ken Thompson committed
2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
}

Node*
reorder4(Node *n)
{
	/*
	 * from ascompat[te]
	 *	return c,d
	 * return expression assigned to output
	 * parameters. there may be no problems.
	 */
	return n;
}
Ken Thompson's avatar
Ken Thompson committed
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023

Node*
structlit(Node *n)
{
	Iter savel, saver;
	Type *l, *t;
	Node *var, *r, *a;

	t = n->type;
	if(t->etype != TSTRUCT)
		fatal("structlit: not struct");

	var = nod(OXXX, N, N);
	tempname(var, t);

	l = structfirst(&savel, &n->type);
	r = listfirst(&saver, &n->left);

loop:
3024 3025 3026 3027 3028 3029
	if(l != T && l->etype == TFIELD && l->type->etype == TFUNC) {
		// skip methods
		l = structnext(&savel);
		goto loop;
	}

Ken Thompson's avatar
Ken Thompson committed
3030
	if(l == T || r == N) {
3031 3032 3033 3034
		if(l != T)
			yyerror("struct literal expect expr of type %T", l);
		if(r != N)
			yyerror("struct literal too many expressions");
Ken Thompson's avatar
Ken Thompson committed
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
		return var;
	}

	// build list of var.field = expr

	a = nod(ODOT, var, newname(l->sym));
	a = nod(OAS, a, r);
	addtop = list(addtop, a);

	l = structnext(&savel);
	r = listnext(&saver);
	goto loop;
}
Ken Thompson's avatar
Ken Thompson committed
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061

Node*
arraylit(Node *n)
{
	Iter saver;
	Type *t;
	Node *var, *r, *a;
	int idx;

	t = n->type;
	if(t->etype != TARRAY)
		fatal("arraylit: not array");

	if(t->bound < 0) {
Ken Thompson's avatar
Ken Thompson committed
3062 3063 3064 3065 3066
		// make a shallow copy
		t = typ(0);
		*t = *n->type;
		n->type = t;

Ken Thompson's avatar
Ken Thompson committed
3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
		// make it a closed array
		r = listfirst(&saver, &n->left);
		for(idx=0; r!=N; idx++)
			r = listnext(&saver);
		t->bound = idx;
	}

	var = nod(OXXX, N, N);
	tempname(var, t);

	idx = 0;
	r = listfirst(&saver, &n->left);

loop:
Ken Thompson's avatar
Ken Thompson committed
3081
	if(r == N)
Ken Thompson's avatar
Ken Thompson committed
3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
		return var;

	// build list of var[c] = expr

	a = nodintconst(idx);
	a = nod(OINDEX, var, a);
	a = nod(OAS, a, r);
	addtop = list(addtop, a);
	idx++;

	r = listnext(&saver);
	goto loop;
}
Ken Thompson's avatar
Ken Thompson committed
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

Node*
maplit(Node *n)
{
	Iter saver;
	Type *t;
	Node *var, *r, *a;

	t = n->type;
	if(t->etype != TMAP)
		fatal("maplit: not array");
	t = ptrto(t);

	var = nod(OXXX, N, N);
	tempname(var, t);

	a = nod(ONEW, N, N);
	a->type = t;
	a = nod(OAS, var, a);
	addtop = list(addtop, a);

	r = listfirst(&saver, &n->left);

loop:
	if(r == N) {
		return var;
	}

	if(r->op != OKEY) {
		yyerror("map literal must have key:value pairs");
		return var;
	}

	// build list of var[c] = expr

	a = nod(OINDEX, var, r->left);
	a = nod(OAS, a, r->right);
	addtop = list(addtop, a);

	r = listnext(&saver);
	goto loop;
}