walk.c 49.3 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

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// 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;
}

45 46 47
void
walk(Node *fn)
{
Ken Thompson's avatar
Ken Thompson committed
48 49
	char s[50];

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

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
79
gettype(Node *n, Node *a)
Ken Thompson's avatar
Ken Thompson committed
80 81 82 83
{
	if(debug['W'])
		dump("\nbefore gettype", n);
	walktype(n, Erv);
Ken Thompson's avatar
Ken Thompson committed
84 85 86
	if(a == N && addtop != N)
		fatal("gettype: addtop");
	addtotop(a);
Ken Thompson's avatar
Ken Thompson committed
87
	if(debug['W'])
Ken Thompson's avatar
Ken Thompson committed
88
		dump("after gettype", n);
89 90 91
}

void
Ken Thompson's avatar
Ken Thompson committed
92 93
walkstate(Node *n)
{
Ken Thompson's avatar
Ken Thompson committed
94
	Node *more;
Ken Thompson's avatar
Ken Thompson committed
95 96 97 98 99 100 101 102 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

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
139
	addtotop(n);
Ken Thompson's avatar
Ken Thompson committed
140 141 142 143 144 145 146

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

Ken Thompson's avatar
Ken Thompson committed
147 148 149 150 151 152 153
void
indir(Node *nl, Node *nr)
{
	if(nr != N)
		*nl = *nr;
}

Ken Thompson's avatar
Ken Thompson committed
154 155
void
walktype(Node *n, int top)
156 157 158 159
{
	Node *r, *l;
	Type *t;
	Sym *s;
Ken Thompson's avatar
maps  
Ken Thompson committed
160
	int et, cl, cr;
161
	int32 lno;
Ken Thompson's avatar
Ken Thompson committed
162 163

	lno = setlineno(n);
164 165 166 167 168 169 170 171 172 173

	/*
	 * 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
174

Ken Thompson's avatar
Ken Thompson committed
175
	setlineno(n);
176

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

180 181 182 183 184 185 186 187
	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
188
	case OLIST:
Ken Thompson's avatar
Ken Thompson committed
189
	case OKEY:
Ken Thompson's avatar
Ken Thompson committed
190 191 192 193
		walktype(n->left, top);
		n = n->right;
		goto loop;

194
	case OPRINT:
195 196 197
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
198
		indir(n, prcompat(n->left));
199 200 201
		goto ret;

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

	case OLITERAL:
209 210
		if(top != Erv)
			goto nottop;
211 212 213
		n->addable = 1;
		goto ret;

214 215 216 217 218 219 220 221 222 223 224
	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;

225
	case ONAME:
226 227
		if(top == Etop)
			goto nottop;
228 229 230 231 232 233 234 235 236 237 238
		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:
239
		if(top != Etop)
240
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
241
		walkstate(n->ninit);
Ken Thompson's avatar
Ken Thompson committed
242
		walkbool(n->ntest);
Ken Thompson's avatar
Ken Thompson committed
243 244 245
		walkstate(n->nincr);
		walkstate(n->nbody);
		goto ret;
246 247

	case OSWITCH:
248
		if(top != Etop)
249 250
			goto nottop;

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

254 255
		if(n->ntest == N)
			n->ntest = booltrue;
Ken Thompson's avatar
Ken Thompson committed
256
		walkstate(n->ninit);
257
		walktype(n->ntest, Erv);
Ken Thompson's avatar
Ken Thompson committed
258
		walkstate(n->nbody);
Ken Thompson's avatar
Ken Thompson committed
259

260 261
		// find common type
		if(n->ntest->type == T)
262
			n->ntest->type = walkswitch(n, sw1);
263 264 265

		// if that fails pick a type
		if(n->ntest->type == T)
266
			n->ntest->type = walkswitch(n, sw2);
267 268 269

		// set the type on all literals
		if(n->ntest->type != T)
270
			walkswitch(n, sw3);
Ken Thompson's avatar
Ken Thompson committed
271 272 273
		walktype(n->ntest, Erv);	// BOTCH is this right
		walktype(n->nincr, Erv);
		goto ret;
274

Ken Thompson's avatar
select  
Ken Thompson committed
275 276 277 278 279 280 281
	case OSELECT:
		if(top != Etop)
			goto nottop;

		walkselect(n);
		goto ret;

282
	case OEMPTY:
283
		if(top != Etop)
284 285 286 287
			goto nottop;
		goto ret;

	case OIF:
288
		if(top != Etop)
289
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
290
		walkstate(n->ninit);
Ken Thompson's avatar
Ken Thompson committed
291
		walkbool(n->ntest);
Ken Thompson's avatar
Ken Thompson committed
292
		walkstate(n->nbody);
Ken Thompson's avatar
Ken Thompson committed
293
		walkstate(n->nelse);
Ken Thompson's avatar
Ken Thompson committed
294
		goto ret;
295

Ken Thompson's avatar
Ken Thompson committed
296 297 298
	case OPROC:
		if(top != Etop)
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
299
		walkstate(n->left);
Ken Thompson's avatar
Ken Thompson committed
300 301
		goto ret;

302 303 304
	case OCALLMETH:
	case OCALLINTER:
	case OCALL:
305 306 307
		if(top == Elv)
			goto nottop;

308 309 310
		if(n->type != T)
			goto ret;

311
		walktype(n->left, Erv);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		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);
		if(t->outtuple == 1)
			n->type = n->type->type->type;

336
		walktype(n->right, Erv);
337 338 339 340 341 342 343

		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
344
			n->right = reorder1(l);
345 346 347 348
			break;

		case OCALL:
			l = ascompatte(n->op, getinarg(t), &n->right, 0);
Ken Thompson's avatar
Ken Thompson committed
349
			n->right = reorder1(l);
Ken Thompson's avatar
select  
Ken Thompson committed
350 351 352 353 354
			if(isselect(n)) {
				// clear output bool - special prob with selectsend
				r = ascompatte(n->op, getoutarg(t), &boolfalse, 0);
				n->right = list(n->right, r);
			}
355 356 357 358 359
			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
360
			l = list(r, l);
Ken Thompson's avatar
Ken Thompson committed
361 362
			n->left->left = N;
			ullmancalc(n->left);
Ken Thompson's avatar
select  
Ken Thompson committed
363
			n->right = reorder1(l);
364 365 366 367 368
			break;
		}
		goto ret;

	case OAS:
369
		if(top != Etop)
370 371
			goto nottop;

Ken Thompson's avatar
Ken Thompson committed
372 373 374
		addtop = list(addtop, n->ninit);
		n->ninit = N;

375
		l = n->left;
376 377
		r = n->right;
		walktype(l, Elv);
Ken Thompson's avatar
maps  
Ken Thompson committed
378
		if(l == N || r == N)
379 380
			goto ret;

Ken Thompson's avatar
maps  
Ken Thompson committed
381 382 383 384 385 386 387
		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
388
				indir(n, reorder3(l));
Ken Thompson's avatar
maps  
Ken Thompson committed
389 390 391 392
			goto ret;
		}

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

Ken Thompson's avatar
maps  
Ken Thompson committed
394 395 396
		case OCALLMETH:
		case OCALLINTER:
		case OCALL:
Ken Thompson's avatar
Ken Thompson committed
397 398 399 400
			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
401 402
				if(l != N)
					indir(n, list(r, reorder2(l)));
Ken Thompson's avatar
Ken Thompson committed
403
				goto ret;
Ken Thompson's avatar
Ken Thompson committed
404
			}
Ken Thompson's avatar
maps  
Ken Thompson committed
405
			break;
Ken Thompson's avatar
Ken Thompson committed
406

Ken Thompson's avatar
maps  
Ken Thompson committed
407 408
		case OINDEX:
		case OINDEXPTR:
Ken Thompson's avatar
Ken Thompson committed
409 410
			if(cl == 2 && cr == 1) {
				// a,b = map[] - mapaccess2
Ken Thompson's avatar
bug075  
Ken Thompson committed
411
				walktype(r->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
412 413 414 415 416
				if(!isptrto(r->left->type, TMAP))
					break;
				l = mapop(n, top);
				if(l == N)
					break;
Ken Thompson's avatar
Ken Thompson committed
417
				indir(n, l);
Ken Thompson's avatar
Ken Thompson committed
418 419 420
				goto ret;
			}
			break;
Ken Thompson's avatar
chan  
Ken Thompson committed
421 422 423 424

		case ORECV:
			if(cl == 2 && cr == 1) {
				// a,b = <chan - chanrecv2
Ken Thompson's avatar
Ken Thompson committed
425
				walktype(r->left, Erv);
Ken Thompson's avatar
chan  
Ken Thompson committed
426 427 428 429 430
				if(!isptrto(r->left->type, TCHAN))
					break;
				l = chanop(n, top);
				if(l == N)
					break;
Ken Thompson's avatar
Ken Thompson committed
431
				indir(n, l);
Ken Thompson's avatar
chan  
Ken Thompson committed
432 433 434
				goto ret;
			}
			break;
Ken Thompson's avatar
Ken Thompson committed
435 436 437 438 439 440 441 442 443 444 445 446
		}

		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
447
				indir(n, l);
Ken Thompson's avatar
Ken Thompson committed
448 449
				goto ret;
			}
Ken Thompson's avatar
maps  
Ken Thompson committed
450 451
			break;
		}
Ken Thompson's avatar
Ken Thompson committed
452 453

		yyerror("bad shape across assignment - cr=%d cl=%d\n", cr, cl);
454 455 456 457 458 459
		goto ret;

	case OBREAK:
	case OCONTINUE:
	case OGOTO:
	case OLABEL:
460 461
		if(top != Etop)
			goto nottop;
462 463 464
		goto ret;

	case OXCASE:
465 466
		if(top != Etop)
			goto nottop;
467 468 469 470
		yyerror("case statement out of place");
		n->op = OCASE;

	case OCASE:
471 472 473
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
474 475
		walkstate(n->right);
		goto ret;
476 477

	case OXFALL:
478 479
		if(top != Etop)
			goto nottop;
480 481 482 483 484 485 486 487 488 489
		yyerror("fallthrough statement out of place");
		n->op = OFALL;

	case OFALL:
	case OINDREG:
		goto ret;

	case OS2I:
	case OI2S:
	case OI2I:
490 491
		if(top != Erv)
			goto nottop;
492
		n->addable = 0;
493
		walktype(n->left, Erv);
494 495 496
		goto ret;

	case OCONV:
Ken Thompson's avatar
Ken Thompson committed
497
		if(top == Etop)
498 499
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
500 501 502 503 504 505

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

Ken Thompson's avatar
Ken Thompson committed
508
		convlit(l, t);
509 510

		// nil conversion
Ken Thompson's avatar
Ken Thompson committed
511 512
		if(eqtype(t, l->type, 0)) {
			if(l->op != ONAME)
Ken Thompson's avatar
Ken Thompson committed
513
				indir(n, l);
514 515 516 517
			goto ret;
		}

		// simple fix-float
Ken Thompson's avatar
Ken Thompson committed
518 519 520
		if(l->type != T)
		if(isint[l->type->etype] || isfloat[l->type->etype])
		if(isint[t->etype] || isfloat[t->etype]) {
521 522 523 524 525
			evconst(n);
			goto ret;
		}

		// to string
Ken Thompson's avatar
Ken Thompson committed
526
		if(l->type != T)
Ken Thompson's avatar
Ken Thompson committed
527 528
		if(isptrto(t, TSTRING)) {
			if(isint[l->type->etype]) {
Ken Thompson's avatar
Ken Thompson committed
529
				indir(n, stringop(n, top));
530 531
				goto ret;
			}
Ken Thompson's avatar
Ken Thompson committed
532
			if(bytearraysz(l->type) != -2) {
533
				n->op = OARRAY;
Ken Thompson's avatar
Ken Thompson committed
534
				indir(n, stringop(n, top));
535 536 537 538
				goto ret;
			}
		}

Ken Thompson's avatar
arrays  
Ken Thompson committed
539
		// convert dynamic to static generated by ONEW
540
		if(isptrarray(t) && isptrdarray(l->type))
Ken Thompson's avatar
Ken Thompson committed
541
			goto ret;
Ken Thompson's avatar
arrays  
Ken Thompson committed
542

Ken Thompson's avatar
Ken Thompson committed
543
		// interface and structure
Ken Thompson's avatar
Ken Thompson committed
544 545
		r = isandss(n->type, l);
		if(r != N) {
Ken Thompson's avatar
Ken Thompson committed
546
			indir(n, r);
Ken Thompson's avatar
Ken Thompson committed
547 548
			goto ret;
		}
Ken Thompson's avatar
Ken Thompson committed
549 550 551

		// structure literal
		if(t->etype == TSTRUCT) {
Ken Thompson's avatar
Ken Thompson committed
552
			indir(n, structlit(n));
Ken Thompson's avatar
Ken Thompson committed
553 554 555
			goto ret;
		}

Ken Thompson's avatar
Ken Thompson committed
556 557 558
		// structure literal
		if(t->etype == TARRAY) {
			r = arraylit(n);
Ken Thompson's avatar
Ken Thompson committed
559
			indir(n, r);
Ken Thompson's avatar
Ken Thompson committed
560 561 562
			goto ret;
		}

Ken Thompson's avatar
Ken Thompson committed
563 564 565
		// map literal
		if(t->etype == TMAP) {
			r = maplit(n);
Ken Thompson's avatar
Ken Thompson committed
566
			indir(n, r);
Ken Thompson's avatar
Ken Thompson committed
567 568 569
			goto ret;
		}

Ken Thompson's avatar
Ken Thompson committed
570
		badtype(n->op, l->type, t);
571 572 573
		goto ret;

	case ORETURN:
574 575 576
		if(top != Etop)
			goto nottop;
		walktype(n->left, Erv);
Ken Thompson's avatar
Ken Thompson committed
577 578 579 580
		if(curfn->type->outnamed && n->left == N) {
			// print("special return\n");
			goto ret;
		}
581 582
		l = ascompatte(n->op, getoutarg(curfn->type), &n->left, 1);
		if(l != N)
Ken Thompson's avatar
Ken Thompson committed
583
			n->left = reorder4(l);
584 585 586
		goto ret;

	case ONOT:
587 588 589
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
590 591 592 593 594 595
		if(n->left == N || n->left->type == T)
			goto ret;
		et = n->left->type->etype;
		break;

	case OASOP:
596
		if(top != Etop)
597
			goto nottop;
598
		walktype(n->left, Elv);
599
		l = n->left;
Ken Thompson's avatar
Ken Thompson committed
600 601 602
		if(l->op != OINDEX) {
			if(n->etype == OLSH || n->etype == ORSH)
				goto shft;
603
			goto com;
Ken Thompson's avatar
Ken Thompson committed
604
		}
605 606
		if(!isptrto(l->left->type, TMAP))
			goto com;
Ken Thompson's avatar
Ken Thompson committed
607
		indir(n, mapop(n, top));
Ken Thompson's avatar
Ken Thompson committed
608
		goto ret;
609 610 611

	case OLSH:
	case ORSH:
Ken Thompson's avatar
Ken Thompson committed
612 613 614 615 616 617 618 619 620 621 622 623
		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
624
		convlit(n->left, types[TINT32]);
Ken Thompson's avatar
Ken Thompson committed
625 626 627 628 629 630
		if(n->left->type == T || n->right->type == T)
			goto ret;
		if(issigned[n->right->type->etype])
			goto badt;
		break;

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	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:
647 648 649 650 651 652
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);

	com:
		walktype(n->right, Erv);
653 654 655 656 657
		if(n->left == N || n->right == N)
			goto ret;
		evconst(n);
		if(n->op == OLITERAL)
			goto ret;
Ken Thompson's avatar
Ken Thompson committed
658 659
		convlit(n->left, n->right->type);
		convlit(n->right, n->left->type);
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
		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
675
				indir(n, stringop(n, top));
676 677 678 679 680 681 682 683
				goto ret;
			}
		}
		break;

	case OMINUS:
	case OPLUS:
	case OCOM:
684 685 686
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
687 688 689 690 691 692 693 694
		if(n->left == N)
			goto ret;
		evconst(n);
		if(n->op == OLITERAL)
			goto ret;
		break;

	case OLEN:
695 696 697
		if(top != Erv)
			goto nottop;
		walktype(n->left, Erv);
698 699 700 701 702 703 704 705 706 707
		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
708
		case TMAP:
Ken Thompson's avatar
Ken Thompson committed
709 710
			break;
		case TARRAY:
711 712
			if(t->bound >= 0)
				nodconst(n, types[TINT32], t->bound);
Ken Thompson's avatar
Ken Thompson committed
713
			break;
714 715 716 717
		}
		n->type = types[TINT32];
		goto ret;

Ken Thompson's avatar
arrays  
Ken Thompson committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731
	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:
732 733
			if(t->bound >= 0)
				nodconst(n, types[TINT32], t->bound);
Ken Thompson's avatar
arrays  
Ken Thompson committed
734 735 736 737 738
			break;
		}
		n->type = types[TINT32];
		goto ret;

739 740
	case OINDEX:
	case OINDEXPTR:
741 742 743
		if(top == Etop)
			goto nottop;

Ken Thompson's avatar
Ken Thompson committed
744
		walktype(n->left, Erv);
745 746
		walktype(n->right, Erv);

747 748
		if(n->left == N || n->right == N)
			goto ret;
Ken Thompson's avatar
maps  
Ken Thompson committed
749 750

		defaultlit(n->left);
751 752 753 754
		t = n->left->type;
		if(t == T)
			goto ret;

Ken Thompson's avatar
Ken Thompson committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768
// 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
769
			indir(n, stringop(n, top));
Ken Thompson's avatar
Ken Thompson committed
770 771 772
			goto ret;
		}

Ken Thompson's avatar
maps  
Ken Thompson committed
773 774 775 776
		// left side is indirect
		if(isptr[t->etype]) {
			t = t->type;
			n->op = OINDEXPTR;
777 778
		}

Ken Thompson's avatar
maps  
Ken Thompson committed
779 780
		switch(t->etype) {
		default:
781 782
			goto badt;

Ken Thompson's avatar
maps  
Ken Thompson committed
783
		case TMAP:
Ken Thompson's avatar
chan  
Ken Thompson committed
784
			// right side must be map type
Ken Thompson's avatar
maps  
Ken Thompson committed
785 786 787 788 789 790 791 792 793 794 795
			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
796
			if(top == Erv)
Ken Thompson's avatar
Ken Thompson committed
797
				indir(n, mapop(n, top));
Ken Thompson's avatar
maps  
Ken Thompson committed
798 799
			break;

Ken Thompson's avatar
arrays  
Ken Thompson committed
800
		case TARRAY:
Ken Thompson's avatar
maps  
Ken Thompson committed
801 802 803 804 805 806 807 808
			// 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;
809

Ken Thompson's avatar
maps  
Ken Thompson committed
810 811
			n->type = t->type;
			break;
812 813 814
		}
		goto ret;

Ken Thompson's avatar
chan  
Ken Thompson committed
815
	case OSEND:
Ken Thompson's avatar
Ken Thompson committed
816
		if(top == Elv)
Ken Thompson's avatar
chan  
Ken Thompson committed
817
			goto nottop;
Ken Thompson's avatar
select  
Ken Thompson committed
818 819
		walktype(n->left, Erv);		// chan
		walktype(n->right, Erv);	// e
Ken Thompson's avatar
Ken Thompson committed
820
		indir(n, chanop(n, top));
Ken Thompson's avatar
chan  
Ken Thompson committed
821 822 823
		goto ret;

	case ORECV:
Ken Thompson's avatar
Ken Thompson committed
824
		if(top == Elv)
Ken Thompson's avatar
chan  
Ken Thompson committed
825
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
826
		if(n->right == N) {
Ken Thompson's avatar
Ken Thompson committed
827 828
			walktype(n->left, Erv);		// chan
			indir(n, chanop(n, top));	// returns e blocking
Ken Thompson's avatar
Ken Thompson committed
829 830 831 832
			goto ret;
		}
		walktype(n->left, Elv);		// e
		walktype(n->right, Erv);	// chan
Ken Thompson's avatar
Ken Thompson committed
833
		indir(n, chanop(n, top));	// returns bool non-blocking
Ken Thompson's avatar
chan  
Ken Thompson committed
834 835
		goto ret;

836
	case OSLICE:
837 838 839 840 841
		if(top == Etop)
			goto nottop;

		walktype(n->left, top);
		walktype(n->right, Erv);
842 843
		if(n->left == N || n->right == N)
			goto ret;
Ken Thompson's avatar
Ken Thompson committed
844
		convlit(n->left, types[TSTRING]);
Ken Thompson's avatar
arrays  
Ken Thompson committed
845
		t = n->left->type;
Ken Thompson's avatar
Ken Thompson committed
846 847
		if(t == T)
			goto ret;
Ken Thompson's avatar
arrays  
Ken Thompson committed
848 849 850
		if(isptr[t->etype])
			t = t->type;
		if(t->etype == TSTRING) {
Ken Thompson's avatar
Ken Thompson committed
851
			indir(n, stringop(n, top));
852 853
			goto ret;
		}
854
		if(t->etype == TARRAY) {
Ken Thompson's avatar
Ken Thompson committed
855
			indir(n, arrayop(n, top));
Ken Thompson's avatar
arrays  
Ken Thompson committed
856 857
			goto ret;
		}
858 859 860 861 862 863 864
		badtype(OSLICE, n->left->type, T);
		goto ret;

	case ODOT:
	case ODOTPTR:
	case ODOTMETH:
	case ODOTINTER:
865 866
		if(top == Etop)
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
867
		walkdot(n);
868 869 870
		goto ret;

	case OADDR:
871 872 873
		if(top != Erv)
			goto nottop;
		walktype(n->left, Elv);
874 875 876 877 878 879 880 881 882
		if(n->left == N)
			goto ret;
		t = n->left->type;
		if(t == T)
			goto ret;
		n->type = ptrto(t);
		goto ret;

	case OIND:
883 884 885
		if(top == Etop)
			goto nottop;
		walktype(n->left, top);
886 887 888 889 890 891 892 893 894 895 896
		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:
897 898
		if(top != Erv)
			goto nottop;
Ken Thompson's avatar
Ken Thompson committed
899
		indir(n, newcompat(n));
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
		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:
918 919
		if(n->left->type == T)
			goto ret;
920 921 922 923 924 925 926 927
		et = n->left->type->etype;
		if(et != TBOOL)
			goto badt;
		t = types[TBOOL];
		break;

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

	case OLT:
	case OLE:
	case OGE:
	case OGT:
940 941
		if(n->left->type == T)
			goto ret;
942 943 944 945 946 947 948 949 950 951 952 953
		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:
954 955
		if(n->left->type == T)
			goto ret;
956 957 958 959 960 961
		et = n->left->type->etype;
		if(!okforadd[et])
			goto badt;
		break;

	case OMINUS:
962 963
		if(n->left->type == T)
			goto ret;
964 965 966 967 968 969 970
		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
971
		l->val.u.fval = mal(sizeof(*l->val.u.fval));
972
		l->val.ctype = CTFLT;
Ken Thompson's avatar
Ken Thompson committed
973
		mpmovecflt(l->val.u.fval, 0.0);
974 975

		l = nod(OSUB, l, n->left);
Ken Thompson's avatar
Ken Thompson committed
976
		indir(n, l);
977
		walktype(n, Erv);
978 979 980 981 982 983 984 985 986
		goto ret;

	case OLSH:
	case ORSH:
	case OAND:
	case OOR:
	case OXOR:
	case OMOD:
	case OCOM:
987 988
		if(n->left->type == T)
			goto ret;
989 990 991 992 993 994 995 996 997 998 999 1000
		et = n->left->type->etype;
		if(!okforand[et])
			goto badt;
		break;
	}

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

nottop:
1001 1002
	dump("bad top", n);
	fatal("walktype: top=%d %O", top, n->op);
Ken Thompson's avatar
Ken Thompson committed
1003
	goto ret;
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

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
1018 1019
	if(debug['w'] && top == Etop && n != N)
		dump("walk", n);
Ken Thompson's avatar
maps  
Ken Thompson committed
1020

1021
	ullmancalc(n);
Ken Thompson's avatar
Ken Thompson committed
1022
	lineno = lno;
1023 1024
}

Ken Thompson's avatar
Ken Thompson committed
1025 1026 1027 1028
void
walkbool(Node *n)
{
	walktype(n, Erv);
Ken Thompson's avatar
Ken Thompson committed
1029
	addtotop(n);
Ken Thompson's avatar
Ken Thompson committed
1030 1031 1032 1033 1034
	if(n != N && n->type != T)
		if(!eqtype(n->type, types[TBOOL], 0))
			yyerror("IF and FOR require a boolean type");
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
/*
 * 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
1056
 * check that switch type
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
 * 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*
1073
walkswitch(Node *sw, Type*(*call)(Node*, Type*))
1074 1075 1076
{
	Node *n, *c;
	Type *place;
1077
	place = call(sw->ntest, T);
1078

Ken Thompson's avatar
Ken Thompson committed
1079 1080
	setlineno(sw);

1081
	n = sw->nbody;
1082 1083
	if(n->op == OLIST)
		n = n->left;
1084 1085
	if(n->op == OEMPTY)
		return;
1086 1087 1088 1089 1090 1091

	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
1092
				setlineno(c);
1093 1094 1095
				place = call(c, place);
				break;
			}
Ken Thompson's avatar
Ken Thompson committed
1096
			setlineno(c);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
			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) {
1122
		/* empty switch */
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
		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;
	}

1139
	/* if first statement is not case */
1140 1141 1142 1143 1144 1145 1146 1147
	if(oc == N)
		return 0;

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

Ken Thompson's avatar
select  
Ken Thompson committed
1148
Node*
Ken Thompson's avatar
Ken Thompson committed
1149
selcase(Node *n, Node *var)
Ken Thompson's avatar
select  
Ken Thompson committed
1150
{
Ken Thompson's avatar
Ken Thompson committed
1151
	Node *a, *r, *on, *c;
Ken Thompson's avatar
select  
Ken Thompson committed
1152
	Type *t;
Ken Thompson's avatar
select  
Ken Thompson committed
1153
	Iter iter;
Ken Thompson's avatar
select  
Ken Thompson committed
1154

Ken Thompson's avatar
Ken Thompson committed
1155 1156 1157 1158
	c = n->left;
	if(c->op == ORECV)
		goto recv;

Ken Thompson's avatar
select  
Ken Thompson committed
1159 1160
	walktype(c->left, Erv);		// chan
	walktype(c->right, Erv);	// elem
Ken Thompson's avatar
Ken Thompson committed
1161

Ken Thompson's avatar
select  
Ken Thompson committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	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
1177
	a = c->right;			// elem
Ken Thompson's avatar
select  
Ken Thompson committed
1178
	r = a;
Ken Thompson's avatar
Ken Thompson committed
1179
	a = c->left;			// chan
Ken Thompson's avatar
select  
Ken Thompson committed
1180
	r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
1181 1182 1183
	a = var;			// sel-var
	r = list(a, r);

Ken Thompson's avatar
select  
Ken Thompson committed
1184
	goto out;
Ken Thompson's avatar
Ken Thompson committed
1185 1186

recv:
Ken Thompson's avatar
select  
Ken Thompson committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
	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
1213 1214 1215 1216 1217 1218
	walktype(c->right, Erv);	// chan

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

Ken Thompson's avatar
select  
Ken Thompson committed
1219
	walktype(c->left, Elv);	// elem
Ken Thompson's avatar
Ken Thompson committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	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
1237 1238
	r = list(a, r);

Ken Thompson's avatar
select  
Ken Thompson committed
1239
out:
Ken Thompson's avatar
select  
Ken Thompson committed
1240 1241 1242 1243 1244 1245 1246
	a = nod(OCALL, on, r);
	r = nod(OIF, N, N);
	r->ntest = a;

	return r;
}

Ken Thompson's avatar
Ken Thompson committed
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
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
1271 1272 1273 1274 1275 1276 1277
void
walkselect(Node *sel)
{
	Iter iter;
	Node *n, *oc, *on, *r;
	Node *var, *bod, *res;
	int count;
1278
	int32 lno;
Ken Thompson's avatar
select  
Ken Thompson committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

	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
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
			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
1319
			case OSEND:
Ken Thompson's avatar
Ken Thompson committed
1320
			case ORECV:
Ken Thompson's avatar
select  
Ken Thompson committed
1321 1322 1323 1324
				if(oc != N) {
					bod = list(bod, nod(OBREAK, N, N));
					oc->nbody = rev(bod);
				}
Ken Thompson's avatar
Ken Thompson committed
1325
				oc = selcase(n, var);
Ken Thompson's avatar
select  
Ken Thompson committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
				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
1357 1358
	walkstate(sel->ninit);
	walkstate(sel->nbody);
Ken Thompson's avatar
select  
Ken Thompson committed
1359

Ken Thompson's avatar
select  
Ken Thompson committed
1360
//dump("sel", sel);
Ken Thompson's avatar
Ken Thompson committed
1361

Ken Thompson's avatar
select  
Ken Thompson committed
1362 1363 1364
	lineno = lno;
}

1365 1366 1367 1368 1369
/*
 * allowable type combinations for
 * normal binary operations.
 */
Type*
Ken Thompson's avatar
Ken Thompson committed
1370
lookdot(Node *n, Type *f)
1371
{
Ken Thompson's avatar
Ken Thompson committed
1372
	Type *r, *c;
1373 1374 1375 1376 1377
	Sym *s;

	r = T;
	s = n->sym;

Ken Thompson's avatar
Ken Thompson committed
1378
	for(; f!=T; f=f->down) {
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
		if(f->sym == S)
			continue;
		if(f->sym != s)
			continue;
		if(r != T) {
			yyerror("ambiguous DOT reference %s", s->name);
			break;
		}
		r = f;
	}
	return r;
}

void
Ken Thompson's avatar
Ken Thompson committed
1393
walkdot(Node *n)
1394 1395 1396 1397 1398 1399
{
	Node *mn;
	Type *t, *f;

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

1403
	walktype(n->left, Erv);
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	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
1420 1421 1422 1423 1424 1425 1426 1427
	// 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)
1428
				n->op = ODOTINTER;
Ken Thompson's avatar
Ken Thompson committed
1429
			return;
1430
		}
1431
	}
Ken Thompson's avatar
Ken Thompson committed
1432 1433 1434

	f = lookdot(n->right, t->method);
	if(f == T) {
1435
		yyerror("undefined DOT %s", n->right->sym->name);
Ken Thompson's avatar
Ken Thompson committed
1436 1437 1438 1439 1440 1441 1442
		return;
	}

	n->xoffset = f->width;
	n->right = methodname(n->right, t);
	n->type = f->type;
	n->op = ODOTMETH;
1443 1444 1445 1446 1447 1448 1449 1450
}

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

Ken Thompson's avatar
Ken Thompson committed
1451 1452 1453 1454 1455
	/*
	 * check assign expression list to
	 * a expression list. called in
	 *	expr-list = expr-list
	 */
1456 1457 1458 1459 1460 1461 1462
	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
1463
			yyerror("error in shape across %O", op);
Ken Thompson's avatar
Ken Thompson committed
1464
		return rev(nn);
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
	}

	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
1478
		nn = list(a, nn);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

	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
1492 1493 1494 1495 1496
	/*
	 * check assign type list to
	 * a expression list. called in
	 *	expr-list = func()
	 */
1497 1498 1499 1500 1501 1502 1503
	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
1504
			yyerror("error in shape across %O", op);
Ken Thompson's avatar
Ken Thompson committed
1505
		return rev(nn);
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	}

	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
1518
		nn = list(a, nn);
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532

	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
1533 1534 1535 1536 1537 1538
	/*
	 * check assign expression list to
	 * a type list. called in
	 *	return expr-list
	 *	func(expr-list)
	 */
1539 1540 1541 1542 1543 1544
	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
1545
			yyerror("error in shape across %O", op);
Ken Thompson's avatar
Ken Thompson committed
1546
		return rev(nn);
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	}
	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
1559
		nn = list(a, nn);
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588

	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))
		if(isptrto(t2, TSTRUCT) || isinter(t2))
			return 1;

	if(isinter(t2))
		if(isptrto(t1, TSTRUCT))
			return 1;

1589 1590
	if(isptrdarray(t1))
		if(isptrarray(t2))
Ken Thompson's avatar
arrays  
Ken Thompson committed
1591
			return 1;
Ken Thompson's avatar
Ken Thompson committed
1592

1593 1594 1595 1596 1597 1598 1599
	return 0;
}

Node*
prcompat(Node *n)
{
	Node *l, *r;
Ken Thompson's avatar
Ken Thompson committed
1600
	Node *on;
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	Type *t;
	Iter save;
	int w;
	char *name;

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

loop:
	if(l == N) {
Ken Thompson's avatar
Ken Thompson committed
1611
		walktype(r, Erv);
1612 1613 1614 1615 1616 1617
		return r;
	}

	w = whatis(l);
	switch(w) {
	default:
Ken Thompson's avatar
Ken Thompson committed
1618 1619
		if(l->type == T)
			goto out;
Ken Thompson's avatar
Ken Thompson committed
1620 1621 1622 1623 1624 1625 1626 1627 1628
		if(!isptr[l->type->etype]) {
			badtype(n->op, l->type, T);
			l = listnext(&save);
			goto loop;
		}
		on = syslook("printpointer", 1);
		argtype(on, l->type->type);	// any-1
		break;

1629 1630
	case Wlitint:
	case Wtint:
Ken Thompson's avatar
Ken Thompson committed
1631
		on = syslook("printint", 0);
1632 1633 1634
		break;
	case Wlitfloat:
	case Wtfloat:
Ken Thompson's avatar
Ken Thompson committed
1635
		on = syslook("printfloat", 0);
1636 1637 1638
		break;
	case Wlitbool:
	case Wtbool:
Ken Thompson's avatar
Ken Thompson committed
1639
		on = syslook("printbool", 0);
1640 1641 1642
		break;
	case Wlitstr:
	case Wtstr:
Ken Thompson's avatar
Ken Thompson committed
1643
		on = syslook("printstring", 0);
1644 1645 1646
		break;
	}

1647
	t = *getinarg(on->type);
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
	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)
1659
		r = nod(OCALL, on, l);
1660
	else
Ken Thompson's avatar
select  
Ken Thompson committed
1661
		r = list(r, nod(OCALL, on, l));
1662

Ken Thompson's avatar
Ken Thompson committed
1663
out:
1664 1665 1666 1667 1668
	l = listnext(&save);
	goto loop;
}

Node*
1669
nodpanic(int32 lineno)
1670
{
1671
	Node *n, *on;
1672

1673
	on = syslook("panicl", 0);
1674
	n = nodintconst(lineno);
1675
	n = nod(OCALL, on, n);
Ken Thompson's avatar
Ken Thompson committed
1676
	walktype(n, Erv);
1677 1678 1679 1680 1681 1682
	return n;
}

Node*
newcompat(Node *n)
{
1683
	Node *r, *on;
1684
	Type *t;
1685 1686 1687 1688 1689 1690

	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
1691 1692
	switch(t->etype) {
	case TMAP:
1693 1694
		r = mapop(n, Erv);
		return r;
Ken Thompson's avatar
arrays  
Ken Thompson committed
1695 1696

	case TCHAN:
Ken Thompson's avatar
Ken Thompson committed
1697 1698
		r = chanop(n, Erv);
		return r;
Ken Thompson's avatar
arrays  
Ken Thompson committed
1699 1700 1701 1702

	case TARRAY:
		r = arrayop(n, Erv);
		return r;
Ken Thompson's avatar
Ken Thompson committed
1703
	}
1704 1705 1706 1707

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

1708 1709 1710
	dowidth(t);

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

1712
	argtype(on, t);
1713

1714 1715 1716
	r = nodintconst(t->width);
	r = nod(OCALL, on, r);
	walktype(r, Erv);
1717 1718

//	r = nod(OCONV, r, N);
1719
	r->type = n->type;
1720 1721 1722 1723 1724

	return r;
}

Node*
1725
stringop(Node *n, int top)
1726
{
1727
	Node *r, *c, *on;
Ken Thompson's avatar
Ken Thompson committed
1728
	Type *t;
1729
	int32 l;
1730 1731 1732

	switch(n->op) {
	default:
Ken Thompson's avatar
chan  
Ken Thompson committed
1733
		fatal("stringop: unknown op %O", n->op);
1734 1735 1736 1737 1738 1739 1740 1741

	case OEQ:
	case ONE:
	case OGE:
	case OGT:
	case OLE:
	case OLT:
		// sys_cmpstring(s1, s2) :: 0
1742
		on = syslook("cmpstring", 0);
Ken Thompson's avatar
select  
Ken Thompson committed
1743
		r = list(n->left, n->right);
1744
		r = nod(OCALL, on, r);
1745 1746 1747 1748 1749 1750
		c = nodintconst(0);
		r = nod(n->op, r, c);
		break;

	case OADD:
		// sys_catstring(s1, s2)
1751
		on = syslook("catstring", 0);
Ken Thompson's avatar
select  
Ken Thompson committed
1752
		r = list(n->left, n->right);
1753
		r = nod(OCALL, on, r);
1754 1755 1756 1757 1758 1759
		break;

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

		case OADD:
			// s1 = sys_catstring(s1, s2)
1764 1765
			if(n->etype != OADD)
				fatal("stringop: not cat");
Ken Thompson's avatar
select  
Ken Thompson committed
1766
			r = list(n->left, n->right);
1767 1768
			on = syslook("catstring", 0);
			r = nod(OCALL, on, r);
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
			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
1782 1783
		r = list(r, c);
		r = list(n->left, r);
1784 1785
		on = syslook("slicestring", 0);
		r = nod(OCALL, on, r);
1786 1787
		break;

Ken Thompson's avatar
Ken Thompson committed
1788
	case OINDEX:
1789
		// sys_indexstring(s, i)
Ken Thompson's avatar
Ken Thompson committed
1790 1791 1792 1793 1794 1795
		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;
		}
1796 1797
		r = nod(OCONV, n->right, N);
		r->type = types[TINT32];
Ken Thompson's avatar
select  
Ken Thompson committed
1798
		r = list(c, r);
1799 1800
		on = syslook("indexstring", 0);
		r = nod(OCALL, on, r);
1801 1802 1803 1804 1805 1806
		break;

	case OCONV:
		// sys_intstring(v)
		r = nod(OCONV, n->left, N);
		r->type = types[TINT64];
1807 1808
		on = syslook("intstring", 0);
		r = nod(OCALL, on, r);
1809 1810 1811
		break;

	case OARRAY:
Ken Thompson's avatar
Ken Thompson committed
1812 1813 1814 1815 1816
		// byteastring(*byte, int32) string;
		t = n->left->type;
		l = bytearraysz(t);

		// &a[0]
1817 1818 1819 1820
		c = nodintconst(0);
		r = nod(OINDEX, n->left, c);
		r = nod(OADDR, r, N);

Ken Thompson's avatar
Ken Thompson committed
1821 1822 1823 1824 1825 1826 1827
		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
1828
		r = list(r, c);
Ken Thompson's avatar
Ken Thompson committed
1829

1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
		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
1845 1846 1847 1848 1849 1850
	if(t == T)
		goto bad;
	if(t->etype != TMAP)
		goto bad;
	if(t->down == T || t->type == T)
		goto bad;
1851 1852 1853 1854 1855

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

	return t;
Ken Thompson's avatar
Ken Thompson committed
1856 1857 1858 1859

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

Ken Thompson's avatar
Ken Thompson committed
1862 1863 1864 1865 1866
Type*
fixchan(Type *tm)
{
	Type *t;

Ken Thompson's avatar
Ken Thompson committed
1867 1868
	if(tm == T) 
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
1869
	t = tm->type;
Ken Thompson's avatar
Ken Thompson committed
1870 1871 1872 1873 1874 1875
	if(t == T)
		goto bad;
	if(t->etype != TCHAN)
		goto bad;
	if(t->type == T)
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
1876 1877 1878 1879

	dowidth(t->type);

	return t;
Ken Thompson's avatar
Ken Thompson committed
1880 1881 1882 1883

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

1886 1887 1888 1889 1890
static int
algtype(Type *t)
{
	int a;

Ken Thompson's avatar
maps  
Ken Thompson committed
1891
	a = 100;
1892
	if(issimple[t->etype])
Ken Thompson's avatar
maps  
Ken Thompson committed
1893
		a = 0;		// simple mem
1894 1895
	else
	if(isptrto(t, TSTRING))
Ken Thompson's avatar
maps  
Ken Thompson committed
1896
		a = 1;		// string
1897 1898
	else
	if(isptr[t->etype])
Ken Thompson's avatar
maps  
Ken Thompson committed
1899
		a = 2;		// pointer
1900 1901
	else
	if(isinter(t))
Ken Thompson's avatar
maps  
Ken Thompson committed
1902
		a = 3;		// interface
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
	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
1914
	int alg1, alg2, cl, cr;
1915

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

1918 1919 1920
	r = n;
	switch(n->op) {
	default:
Ken Thompson's avatar
chan  
Ken Thompson committed
1921
		fatal("mapop: unknown op %O", n->op);
1922 1923

	case ONEW:
Ken Thompson's avatar
maps  
Ken Thompson committed
1924 1925 1926
		if(top != Erv)
			goto nottop;

1927 1928
		// newmap(keysize uint32, valsize uint32,
		//	keyalg uint32, valalg uint32,
Ken Thompson's avatar
maps  
Ken Thompson committed
1929
		//	hint uint32) (hmap *map[any-1]any-2);
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939

		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
1940
		r = list(a, r);
1941
		a = nodintconst(algtype(t->down));	// key algorithm
Ken Thompson's avatar
select  
Ken Thompson committed
1942
		r = list(a, r);
1943
		a = nodintconst(t->type->width);	// val width
Ken Thompson's avatar
select  
Ken Thompson committed
1944
		r = list(a, r);
1945
		a = nodintconst(t->down->width);	// key width
Ken Thompson's avatar
select  
Ken Thompson committed
1946
		r = list(a, r);
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957

		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
1958
	case OINDEX:
Ken Thompson's avatar
maps  
Ken Thompson committed
1959 1960
		if(top != Erv)
			goto nottop;
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
		// 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
1975 1976 1977 1978 1979
//		if(!isptr[t->down->etype]) {
//			a = nod(OADDR, a, N);
//			a->type = ptrto(t);
//		}

1980 1981
		r = a;
		a = n->left;				// map
Ken Thompson's avatar
select  
Ken Thompson committed
1982
		r = list(a, r);
1983

Ken Thompson's avatar
Ken Thompson committed
1984
		on = syslook("mapaccess1", 1);
1985 1986 1987 1988 1989 1990 1991 1992 1993

		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;
1994 1995
		break;

Ken Thompson's avatar
Ken Thompson committed
1996 1997 1998
	case OAS:
		cl = listcount(n->left);
		cr = listcount(n->right);
Ken Thompson's avatar
maps  
Ken Thompson committed
1999

Ken Thompson's avatar
Ken Thompson committed
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
		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
2012 2013 2014
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2015
		a = n->right;				// val
Ken Thompson's avatar
maps  
Ken Thompson committed
2016
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2017
		a = n->left->right;			// key
Ken Thompson's avatar
select  
Ken Thompson committed
2018
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2019
		a = n->left->left;			// map
Ken Thompson's avatar
select  
Ken Thompson committed
2020
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2021

Ken Thompson's avatar
Ken Thompson committed
2022
		on = syslook("mapassign1", 1);
Ken Thompson's avatar
maps  
Ken Thompson committed
2023 2024 2025 2026 2027 2028

		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
2029 2030
		r = nod(OCALL, on, r);
		walktype(r, Erv);
Ken Thompson's avatar
maps  
Ken Thompson committed
2031 2032
		break;

Ken Thompson's avatar
Ken Thompson committed
2033 2034 2035 2036
	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
2037 2038 2039 2040 2041

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

Ken Thompson's avatar
Ken Thompson committed
2042
		a = n->right->right;			// pres
Ken Thompson's avatar
maps  
Ken Thompson committed
2043
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2044
		a = n->right->left;			// val
Ken Thompson's avatar
select  
Ken Thompson committed
2045
		r =list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2046
		a = n->left->right;			// key
Ken Thompson's avatar
select  
Ken Thompson committed
2047
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2048
		a = n->left->left;			// map
Ken Thompson's avatar
select  
Ken Thompson committed
2049
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2050

Ken Thompson's avatar
Ken Thompson committed
2051
		on = syslook("mapassign2", 1);
Ken Thompson's avatar
maps  
Ken Thompson committed
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

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

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

Ken Thompson's avatar
Ken Thompson committed
2065 2066 2067
//dump("access2", n);
		if(n->right->op != OINDEX)
			goto shape;
Ken Thompson's avatar
maps  
Ken Thompson committed
2068

Ken Thompson's avatar
Ken Thompson committed
2069
		t = fixmap(n->right->left->type);
Ken Thompson's avatar
maps  
Ken Thompson committed
2070 2071 2072
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2073
		a = n->right->right;			// key
Ken Thompson's avatar
maps  
Ken Thompson committed
2074
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2075
		a = n->right->left;			// map
Ken Thompson's avatar
select  
Ken Thompson committed
2076
		r = list(a, r);
Ken Thompson's avatar
maps  
Ken Thompson committed
2077

Ken Thompson's avatar
Ken Thompson committed
2078
		on = syslook("mapaccess2", 1);
Ken Thompson's avatar
maps  
Ken Thompson committed
2079 2080 2081 2082 2083 2084

		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
2085 2086 2087
		n->right = nod(OCALL, on, r);
		walktype(n, Etop);
		r = n;
Ken Thompson's avatar
maps  
Ken Thompson committed
2088 2089
		break;

2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
	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
2101
		indir(a, n->left);			// copy of map[tmpi]
2102 2103 2104
		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
2105
	}
2106
	return r;
Ken Thompson's avatar
maps  
Ken Thompson committed
2107

Ken Thompson's avatar
Ken Thompson committed
2108 2109 2110 2111 2112
shape:
	dump("shape", n);
	fatal("mapop: cl=%d cr=%d, %O", top, n->op);
	return N;

Ken Thompson's avatar
maps  
Ken Thompson committed
2113 2114 2115 2116
nottop:
	dump("bad top", n);
	fatal("mapop: top=%d %O", top, n->op);
	return N;
2117 2118
}

Ken Thompson's avatar
Ken Thompson committed
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
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
2132
		fatal("chanop: unknown op %O", n->op);
Ken Thompson's avatar
Ken Thompson committed
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146

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

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

		a = n->left;				// hint
		if(n->left == N)
			a = nodintconst(0);
		r = a;
		a = nodintconst(algtype(t->type));	// elem algorithm
Ken Thompson's avatar
select  
Ken Thompson committed
2147
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2148
		a = nodintconst(t->type->width);	// elem width
Ken Thompson's avatar
select  
Ken Thompson committed
2149
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2150 2151 2152 2153 2154 2155 2156 2157

		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
2158 2159

	case OAS:
Ken Thompson's avatar
chan  
Ken Thompson committed
2160 2161
		cl = listcount(n->left);
		cr = listcount(n->right);
Ken Thompson's avatar
chan  
Ken Thompson committed
2162

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

Ken Thompson's avatar
Ken Thompson committed
2166 2167
		// chanrecv2(hchan *chan any) (elem any, pres bool);
		t = fixchan(n->right->left->type);
Ken Thompson's avatar
chan  
Ken Thompson committed
2168 2169 2170
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2171
		a = n->right->left;			// chan
Ken Thompson's avatar
chan  
Ken Thompson committed
2172 2173
		r = a;

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

Ken Thompson's avatar
chan  
Ken Thompson committed
2176 2177 2178
		argtype(on, t->type);	// any-1
		argtype(on, t->type);	// any-2
		r = nod(OCALL, on, r);
Ken Thompson's avatar
Ken Thompson committed
2179 2180 2181
		n->right = r;
		r = n;
		walktype(r, Etop);
Ken Thompson's avatar
chan  
Ken Thompson committed
2182 2183
		break;

Ken Thompson's avatar
chan  
Ken Thompson committed
2184
	case ORECV:
Ken Thompson's avatar
Ken Thompson committed
2185 2186 2187
		if(n->right != N)
			goto recv2;

Ken Thompson's avatar
chan  
Ken Thompson committed
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
		// 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
2206
		// chanrecv3(hchan *chan any, *elem any) (pres bool);
Ken Thompson's avatar
Ken Thompson committed
2207
		t = fixchan(n->right->type);
Ken Thompson's avatar
chan  
Ken Thompson committed
2208 2209 2210
		if(t == T)
			break;

Ken Thompson's avatar
Ken Thompson committed
2211
		a = n->right;			// chan
Ken Thompson's avatar
chan  
Ken Thompson committed
2212
		r = a;
Ken Thompson's avatar
Ken Thompson committed
2213 2214 2215 2216 2217 2218
		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
2219

Ken Thompson's avatar
Ken Thompson committed
2220
		on = syslook("chanrecv3", 1);
Ken Thompson's avatar
chan  
Ken Thompson committed
2221 2222 2223

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

Ken Thompson's avatar
chan  
Ken Thompson committed
2225 2226 2227 2228 2229
		r = nod(OCALL, on, r);
		n->right = r;
		r = n;
		walktype(r, Etop);
		break;
Ken Thompson's avatar
Ken Thompson committed
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245

	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
2246
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2247 2248 2249 2250 2251

		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
2252
		walktype(r, Etop);
Ken Thompson's avatar
Ken Thompson committed
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
		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
2264
		r = list(a, r);
Ken Thompson's avatar
Ken Thompson committed
2265 2266 2267 2268 2269

		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
2270
		walktype(r, Etop);
Ken Thompson's avatar
Ken Thompson committed
2271
		break;
Ken Thompson's avatar
Ken Thompson committed
2272 2273
	}
	return r;
Ken Thompson's avatar
chan  
Ken Thompson committed
2274 2275 2276 2277

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

Ken Thompson's avatar
arrays  
Ken Thompson committed
2280 2281 2282 2283 2284 2285
Type*
fixarray(Type *tm)
{
	Type *t;

	t = tm->type;
Ken Thompson's avatar
Ken Thompson committed
2286 2287 2288 2289 2290 2291
	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
2292 2293 2294 2295

	dowidth(t->type);

	return t;
Ken Thompson's avatar
Ken Thompson committed
2296 2297 2298 2299 2300

bad:
	yyerror("not an array: %lT", tm);
	return T;
	
Ken Thompson's avatar
arrays  
Ken Thompson committed
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
}

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
2326
		a = listnext(&save);
Ken Thompson's avatar
arrays  
Ken Thompson committed
2327 2328 2329 2330 2331 2332
		if(a == N)
			a = nodintconst(0);
		a = nod(OCONV, a, N);
		a->type = types[TUINT32];
		r = list(a, r);

2333 2334 2335 2336
		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
2337
			a = nodintconst(t->bound);
2338
		}
Ken Thompson's avatar
arrays  
Ken Thompson committed
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
		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:
2379
		if(isptrarray(n->left->type))
Ken Thompson's avatar
arrays  
Ken Thompson committed
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
			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
2395
		a->type = types[TUINT32];
Ken Thompson's avatar
arrays  
Ken Thompson committed
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
		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
2422
		a->type = types[TUINT32];
Ken Thompson's avatar
arrays  
Ken Thompson committed
2423 2424 2425 2426
		r = list(a, r);

		a = nodintconst(t->bound);		// nel
		a = nod(OCONV, a, N);
2427
		a->type = types[TUINT32];
Ken Thompson's avatar
arrays  
Ken Thompson committed
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
		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;
}

2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
void
diagnamed(Type *t)
{
	if(isinter(t))
		if(t->sym == S)
			yyerror("interface type must be named");
	if(isptrto(t, TSTRUCT))
		if(t->type == T || t->type->sym == S)
			yyerror("structure type must be named");
}

Node*
Ken Thompson's avatar
Ken Thompson committed
2455
isandss(Type *lt, Node *r)
2456
{
Ken Thompson's avatar
Ken Thompson committed
2457 2458
	Type *rt;
	Node *n;
2459
	int o;
Ken Thompson's avatar
Ken Thompson committed
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493

	rt = r->type;
	if(isinter(lt)) {
		if(isptrto(rt, TSTRUCT)) {
			o = OS2I;
			goto ret;
		}
		if(isinter(rt)) {
			o = OI2I;
			goto ret;
		}
	}

	if(isptrto(lt, TSTRUCT)) {
		if(isinter(rt)) {
			o = OI2S;
			goto ret;
		}
	}

	return N;

ret:
	diagnamed(lt);
	diagnamed(rt);

	n = nod(o, r, N);
	n->type = lt;
	return n;
}

Node*
convas(Node *n)
{
2494 2495 2496 2497
	Node *l, *r;
	Type *lt, *rt;

	if(n->op != OAS)
2498
		fatal("convas: not OAS %O", n->op);
2499 2500 2501 2502

	l = n->left;
	r = n->right;
	if(l == N || r == N)
2503
		goto out;
2504 2505 2506 2507

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

Ken Thompson's avatar
maps  
Ken Thompson committed
2510 2511
	if(n->left->op == OINDEX)
	if(isptrto(n->left->left->type, TMAP)) {
Ken Thompson's avatar
Ken Thompson committed
2512
		indir(n, mapop(n, Elv));
2513
		goto out;
Ken Thompson's avatar
maps  
Ken Thompson committed
2514 2515 2516 2517
	}

	if(n->left->op == OINDEXPTR)
	if(n->left->left->type->etype == TMAP) {
Ken Thompson's avatar
Ken Thompson committed
2518
		indir(n, mapop(n, Elv));
2519
		goto out;
Ken Thompson's avatar
maps  
Ken Thompson committed
2520 2521
	}

Ken Thompson's avatar
chan  
Ken Thompson committed
2522 2523
	if(n->left->op == OSEND)
	if(n->left->type != T) {
Ken Thompson's avatar
Ken Thompson committed
2524
		indir(n, chanop(n, Elv));
2525
		goto out;
Ken Thompson's avatar
chan  
Ken Thompson committed
2526 2527
	}

2528
	if(eqtype(lt, rt, 0))
2529
		goto out;
2530

Ken Thompson's avatar
Ken Thompson committed
2531 2532 2533 2534
	r = isandss(lt, r);
	if(r != N) {
		n->right = r;
		walktype(n, Etop);
2535
		goto out;
2536 2537
	}

2538
	if(isptrdarray(lt) && isptrarray(rt)) {
Ken Thompson's avatar
arrays  
Ken Thompson committed
2539 2540
		if(!eqtype(lt->type->type, rt->type->type, 0))
			goto bad;
Ken Thompson's avatar
Ken Thompson committed
2541
		indir(n, arrayop(n, Etop));
2542
		goto out;
Ken Thompson's avatar
arrays  
Ken Thompson committed
2543 2544 2545
	}

	if(ascompat(lt, rt))
2546
		goto out;
Ken Thompson's avatar
arrays  
Ken Thompson committed
2547 2548

bad:
2549
	badtype(n->op, lt, rt);
2550 2551 2552

out:
	ullmancalc(n);
2553 2554 2555
	return n;
}

2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
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
2581
	 * types derived from the rhs.
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
	 */
	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
2595 2596 2597 2598 2599 2600 2601
	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
2602
			n = list(n, a);
Ken Thompson's avatar
maps  
Ken Thompson committed
2603 2604 2605 2606

		l = listnext(&savel);
		r = listnext(&saver);
	}
Ken Thompson's avatar
Ken Thompson committed
2607
	n = rev(n);
Ken Thompson's avatar
maps  
Ken Thompson committed
2608
	return n;
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633

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;
		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
2634
			a = old2new(l, t->type);
2635 2636 2637
			if(n == N)
				n = a;
			else
Ken Thompson's avatar
select  
Ken Thompson committed
2638
				n = list(n, a);
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
			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
2657
		a = old2new(nl->left, t->type);
2658
		n = a;
Ken Thompson's avatar
maps  
Ken Thompson committed
2659
		a = old2new(nl->right, types[TBOOL]);
Ken Thompson's avatar
select  
Ken Thompson committed
2660
		n = list(n, a);
2661
		break;
Ken Thompson's avatar
chan  
Ken Thompson committed
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672

	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
2673
		n = list(n, a);
2674
	}
Ken Thompson's avatar
Ken Thompson committed
2675
	n = rev(n);
2676 2677 2678 2679 2680 2681 2682
	return n;

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

2683 2684 2685 2686 2687 2688 2689 2690
/*
 * 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
2691 2692 2693 2694
Node*
reorder1(Node *n)
{
	Iter save;
Ken Thompson's avatar
Ken Thompson committed
2695
	Node *l, *r, *f, *a, *g;
Ken Thompson's avatar
Ken Thompson committed
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
	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
2719 2720 2721
	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
2722 2723 2724

loop2:
	if(l == N) {
2725
		r = rev(r);
Ken Thompson's avatar
Ken Thompson committed
2726 2727
		g = rev(g);
		if(g != N)
Ken Thompson's avatar
select  
Ken Thompson committed
2728 2729
			f = list(g, f);
		r = list(f, r);
2730
		return r;
Ken Thompson's avatar
Ken Thompson committed
2731
	}
Ken Thompson's avatar
Ken Thompson committed
2732 2733 2734 2735
	if(l->ullman < UINF) {
		if(r == N)
			r = l;
		else
Ken Thompson's avatar
select  
Ken Thompson committed
2736
			r = list(l, r);
Ken Thompson's avatar
Ken Thompson committed
2737 2738 2739
		goto more;
	}
	if(f == N) {
Ken Thompson's avatar
Ken Thompson committed
2740
		f = l;
Ken Thompson's avatar
Ken Thompson committed
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
		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;
2751
	else
Ken Thompson's avatar
select  
Ken Thompson committed
2752
		g = list(a, g);
Ken Thompson's avatar
Ken Thompson committed
2753 2754 2755 2756

	// put normal arg assignment on list
	// with fncall replaced by tempname
	l->right = a->left;
Ken Thompson's avatar
Ken Thompson committed
2757 2758 2759
	if(r == N)
		r = l;
	else
Ken Thompson's avatar
select  
Ken Thompson committed
2760
		r = list(l, r);
Ken Thompson's avatar
Ken Thompson committed
2761

Ken Thompson's avatar
Ken Thompson committed
2762
more:
Ken Thompson's avatar
Ken Thompson committed
2763 2764 2765 2766
	l = listnext(&save);
	goto loop2;
}

2767 2768 2769 2770 2771 2772 2773
/*
 * 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
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
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;
}

2799 2800 2801
/*
 * from ascompat[ee]
 *	a,b = c,d
Ken Thompson's avatar
Ken Thompson committed
2802 2803
 * simultaneous assignment. there cannot
 * be later use of an earlier lvalue.
2804 2805
 */
int
Ken Thompson's avatar
Ken Thompson committed
2806
vmatch2(Node *l, Node *r)
2807
{
Ken Thompson's avatar
Ken Thompson committed
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849

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

Ken Thompson's avatar
Ken Thompson committed
2852 2853 2854
Node*
reorder3(Node *n)
{
2855
	Iter save1, save2;
Ken Thompson's avatar
Ken Thompson committed
2856
	Node *l1, *l2, *q, *r;
2857 2858
	int c1, c2;

Ken Thompson's avatar
Ken Thompson committed
2859 2860
	r = N;

2861 2862 2863 2864
	l1 = listfirst(&save1, &n);
	c1 = 0;

	while(l1 != N) {
Ken Thompson's avatar
Ken Thompson committed
2865
		l2 = listfirst(&save2, &n);
2866 2867 2868
		c2 = 0;
		while(l2 != N) {
			if(c2 > c1) {
Ken Thompson's avatar
Ken Thompson committed
2869 2870 2871 2872 2873 2874 2875 2876
				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
2877
						r = list(r, q);
Ken Thompson's avatar
Ken Thompson committed
2878
					break;
2879 2880
				}
			}
Ken Thompson's avatar
Ken Thompson committed
2881
			l2 = listnext(&save2);
2882 2883 2884 2885 2886
			c2++;
		}
		l1 = listnext(&save1);
		c1++;
	}
Ken Thompson's avatar
Ken Thompson committed
2887 2888 2889 2890 2891 2892 2893 2894 2895
	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
2896
			q = list(q, l1);
Ken Thompson's avatar
Ken Thompson committed
2897 2898 2899 2900 2901 2902 2903 2904 2905
		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
2906
			q = list(q, l1);
Ken Thompson's avatar
Ken Thompson committed
2907 2908 2909 2910 2911 2912
		l1 = listnext(&save1);
	}

	q = rev(q);
//dump("res", q);
	return q;
Ken Thompson's avatar
Ken Thompson committed
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925
}

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
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944

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:
2945 2946 2947 2948 2949 2950
	if(l != T && l->etype == TFIELD && l->type->etype == TFUNC) {
		// skip methods
		l = structnext(&savel);
		goto loop;
	}

Ken Thompson's avatar
Ken Thompson committed
2951
	if(l == T || r == N) {
2952 2953 2954 2955
		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
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
		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
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997

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) {
		// make it a closed array
		// should there be a type copy here?
		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
2998
	if(r == N)
Ken Thompson's avatar
Ken Thompson committed
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
		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
3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053

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