gsubr.c 37.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Derived from Inferno utils/5c/txt.c
// http://code.google.com/p/inferno-os/source/browse/utils/5c/txt.c
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 Bruce Ellis
//	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

Russ Cox's avatar
Russ Cox committed
31 32
#include <u.h>
#include <libc.h>
33 34
#include "gg.h"

35 36 37 38
// TODO(kaib): Can make this bigger if we move
// the text segment up higher in 5l for all GOOS.
long unmappedzero = 4096;

39 40 41 42
void
clearp(Prog *p)
{
	p->as = AEND;
43 44
	p->reg = NREG;
	p->scond = C_SCOND_NONE;
45
	p->from.type = D_NONE;
46
	p->from.name = D_NONE;
47
	p->from.reg = NREG;
48
	p->to.type = D_NONE;
49
	p->to.name = D_NONE;
50
	p->to.reg = NREG;
51 52 53 54
	p->loc = pcloc;
	pcloc++;
}

55 56 57 58
static int ddumped;
static Prog *dfirst;
static Prog *dpc;

59 60 61 62 63 64 65 66 67
/*
 * generate and return proc with p->as = as,
 * linked into program.  pc is next instruction.
 */
Prog*
prog(int as)
{
	Prog *p;

68 69 70 71 72 73 74 75 76 77
	if(as == ADATA || as == AGLOBL) {
		if(ddumped)
			fatal("already dumped data");
		if(dpc == nil) {
			dpc = mal(sizeof(*dpc));
			dfirst = dpc;
		}
		p = dpc;
		dpc = mal(sizeof(*dpc));
		p->link = dpc;
Russ Cox's avatar
Russ Cox committed
78
		p->reg = 0;  // used for flags
79 80 81 82 83 84
	} else {
		p = pc;
		pc = mal(sizeof(*pc));
		clearp(pc);
		p->link = pc;
	}
85 86 87 88 89 90 91 92 93 94 95

	if(lineno == 0) {
		if(debug['K'])
			warn("prog: line 0");
	}

	p->as = as;
	p->lineno = lineno;
	return p;
}

96 97 98 99 100 101 102 103 104 105 106 107
void
dumpdata(void)
{
	ddumped = 1;
	if(dfirst == nil)
		return;
	newplist();
	*pc = *dfirst;
	pc = dpc;
	clearp(pc);
}

108 109 110 111 112 113 114 115 116
/*
 * generate a branch.
 * t is ignored.
 */
Prog*
gbranch(int as, Type *t)
{
	Prog *p;

Russ Cox's avatar
Russ Cox committed
117 118
	USED(t);

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	p = prog(as);
	p->to.type = D_BRANCH;
	p->to.branch = P;
	return p;
}

/*
 * patch previous branch to jump to to.
 */
void
patch(Prog *p, Prog *to)
{
	if(p->to.type != D_BRANCH)
		fatal("patch: not a branch");
	p->to.branch = to;
	p->to.offset = to->loc;
}

Russ Cox's avatar
Russ Cox committed
137 138 139 140 141 142 143 144 145 146 147 148 149
Prog*
unpatch(Prog *p)
{
	Prog *q;

	if(p->to.type != D_BRANCH)
		fatal("unpatch: not a branch");
	q = p->to.branch;
	p->to.branch = P;
	p->to.offset = 0;
	return q;
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/*
 * start a new Prog list.
 */
Plist*
newplist(void)
{
	Plist *pl;

	pl = mal(sizeof(*pl));
	if(plist == nil)
		plist = pl;
	else
		plast->link = pl;
	plast = pl;

	pc = mal(sizeof(*pc));
	clearp(pc);
	pl->firstpc = pc;

	return pl;
}

Russ Cox's avatar
Russ Cox committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
void
clearstk(void)
{
	Plist *pl;
	Prog *p, *p1, *p2, *p3;
	Node dst, end, zero, con;

	if(plast->firstpc->to.offset <= 0)
		return;

	// reestablish context for inserting code
	// at beginning of function.
	pl = plast;
	p1 = pl->firstpc;
	p2 = p1->link;
	pc = mal(sizeof(*pc));
	clearp(pc);
	p1->link = pc;
	
	// zero stack frame

	// MOVW $4(SP), R1
	nodreg(&dst, types[tptr], 1);
	p = gins(AMOVW, N, &dst);
	p->from.type = D_CONST;
	p->from.reg = REGSP;
	p->from.offset = 4;

	// MOVW $n(R1), R2
	nodreg(&end, types[tptr], 2);
	p = gins(AMOVW, N, &end);
	p->from.type = D_CONST;
	p->from.reg = 1;
	p->from.offset = p1->to.offset;
	
	// MOVW $0, R3
	nodreg(&zero, types[TUINT32], 3);
	nodconst(&con, types[TUINT32], 0);
	gmove(&con, &zero);

	// L:
	//	MOVW.P R3, 0(R1) +4
	//	CMP R1, R2
	//	BNE L
	p = gins(AMOVW, &zero, &dst);
	p->to.type = D_OREG;
	p->to.offset = 4;
	p->scond |= C_PBIT;
	p3 = p;
	p = gins(ACMP, &dst, N);
	raddr(&end, p);
	patch(gbranch(ABNE, T), p3);

	// continue with original code.
	gins(ANOP, N, N)->link = p2;
	pc = P;
}	

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
void
gused(Node *n)
{
	gins(ANOP, n, N);	// used
}

Prog*
gjmp(Prog *to)
{
	Prog *p;

	p = gbranch(AB, T);
	if(to != P)
		patch(p, to);
	return p;
}

void
ggloblnod(Node *nam, int32 width)
{
	Prog *p;

	p = gins(AGLOBL, nam, N);
	p->lineno = nam->lineno;
	p->to.sym = S;
	p->to.type = D_CONST;
	p->to.offset = width;
257
	if(nam->readonly)
258
		p->reg = RODATA;
259
	if(nam->type != T && !haspointers(nam->type))
260
		p->reg |= NOPTR;
261 262 263 264 265 266 267 268
}

void
ggloblsym(Sym *s, int32 width, int dupok)
{
	Prog *p;

	p = gins(AGLOBL, N, N);
269 270
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
271 272
	p->from.sym = s;
	p->to.type = D_CONST;
273
	p->to.name = D_NONE;
274 275
	p->to.offset = width;
	if(dupok)
276
		p->reg = DUPOK;
277
	p->reg |= RODATA;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

int
isfat(Type *t)
{
	if(t != T)
	switch(t->etype) {
	case TSTRUCT:
	case TARRAY:
	case TSTRING:
	case TINTER:	// maybe remove later
		return 1;
	}
	return 0;
}

/*
 * naddr of func generates code for address of func.
 * if using opcode that can take address implicitly,
 * call afunclit to fix up the argument.
Kai Backman's avatar
Kai Backman committed
298
 * also fix up direct register references to be D_OREG.
299 300 301 302
 */
void
afunclit(Addr *a)
{
Kai Backman's avatar
Kai Backman committed
303
	if(a->type == D_CONST && a->name == D_EXTERN || a->type == D_REG) {
304
		a->type = D_OREG;
305 306 307
	}
}

Russ Cox's avatar
Russ Cox committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
static	int	resvd[] =
{
	9,	// reserved for m
	10,	// reserved for g
};

void
ginit(void)
{
	int i;

	for(i=0; i<nelem(reg); i++)
		reg[i] = 0;
	for(i=0; i<nelem(resvd); i++)
		reg[resvd[i]]++;
}

void
gclean(void)
{
	int i;

	for(i=0; i<nelem(resvd); i++)
		reg[resvd[i]]--;

	for(i=0; i<nelem(reg); i++)
		if(reg[i])
			yyerror("reg %R left allocated\n", i);
}

Ken Thompson's avatar
Ken Thompson committed
338 339 340
int32
anyregalloc(void)
{
Russ Cox's avatar
Russ Cox committed
341 342 343 344 345 346 347 348 349 350 351
	int i, j;

	for(i=0; i<nelem(reg); i++) {
		if(reg[i] == 0)
			goto ok;
		for(j=0; j<nelem(resvd); j++)
			if(resvd[j] == i)
				goto ok;
		return 1;
	ok:;
	}
Ken Thompson's avatar
Ken Thompson committed
352 353 354
	return 0;
}

Russ Cox's avatar
Russ Cox committed
355
uintptr regpc[REGALLOC_FMAX+1];
Russ Cox's avatar
Russ Cox committed
356

357 358 359 360 361
/*
 * allocate register of type t, leave in n.
 * if o != N, o is desired fixed register.
 * caller must regfree(n).
 */
362
void
363
regalloc(Node *n, Type *t, Node *o)
364
{
365 366
	int i, et, fixfree, floatfree;

Russ Cox's avatar
Russ Cox committed
367
	if(0 && debug['r']) {
368 369 370 371 372 373 374 375 376 377
		fixfree = 0;
		for(i=REGALLOC_R0; i<=REGALLOC_RMAX; i++)
			if(reg[i] == 0)
				fixfree++;
		floatfree = 0;
		for(i=REGALLOC_F0; i<=REGALLOC_FMAX; i++)
			if(reg[i] == 0)
				floatfree++;
		print("regalloc fix %d float %d\n", fixfree, floatfree);
	}
378 379 380 381

	if(t == T)
		fatal("regalloc: t nil");
	et = simtype[t->etype];
Kai Backman's avatar
Kai Backman committed
382 383
	if(is64(t))
		fatal("regalloc: 64 bit type %T");
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

	switch(et) {
	case TINT8:
	case TUINT8:
	case TINT16:
	case TUINT16:
	case TINT32:
	case TUINT32:
	case TPTR32:
	case TBOOL:
		if(o != N && o->op == OREGISTER) {
			i = o->val.u.reg;
			if(i >= REGALLOC_R0 && i <= REGALLOC_RMAX)
				goto out;
		}
		for(i=REGALLOC_R0; i<=REGALLOC_RMAX; i++)
Russ Cox's avatar
Russ Cox committed
400 401
			if(reg[i] == 0) {
				regpc[i] = (uintptr)getcallerpc(&n);
402
				goto out;
Russ Cox's avatar
Russ Cox committed
403 404 405
			}
		for(i=REGALLOC_R0; i<=REGALLOC_RMAX; i++)
			print("%d %p\n", i, regpc[i]);
406 407
		yyerror("out of fixed registers");
		goto err;
408

409 410 411 412 413 414 415 416 417 418
	case TFLOAT32:
	case TFLOAT64:
		if(o != N && o->op == OREGISTER) {
			i = o->val.u.reg;
			if(i >= REGALLOC_F0 && i <= REGALLOC_FMAX)
				goto out;
		}
		for(i=REGALLOC_F0; i<=REGALLOC_FMAX; i++)
			if(reg[i] == 0)
				goto out;
419
		yyerror("out of floating point registers");
420
		goto err;
Russ Cox's avatar
Russ Cox committed
421 422 423 424 425

	case TCOMPLEX64:
	case TCOMPLEX128:
		tempname(n, t);
		return;
426 427 428 429 430 431 432 433 434 435
	}
	yyerror("regalloc: unknown type %T", t);

err:
	nodreg(n, t, 0);
	return;

out:
	reg[i]++;
	nodreg(n, t, i);
436 437 438
}

void
439
regfree(Node *n)
440
{
441 442
	int i, fixfree, floatfree;

Russ Cox's avatar
Russ Cox committed
443
	if(0 && debug['r']) {
444 445 446 447 448 449 450 451 452 453
		fixfree = 0;
		for(i=REGALLOC_R0; i<=REGALLOC_RMAX; i++)
			if(reg[i] == 0)
				fixfree++;
		floatfree = 0;
		for(i=REGALLOC_F0; i<=REGALLOC_FMAX; i++)
			if(reg[i] == 0)
				floatfree++;
		print("regalloc fix %d float %d\n", fixfree, floatfree);
	}
454

Russ Cox's avatar
Russ Cox committed
455 456
	if(n->op == ONAME && iscomplex[n->type->etype])
		return;
457 458 459
	if(n->op != OREGISTER && n->op != OINDREG)
		fatal("regfree: not a register");
	i = n->val.u.reg;
Russ Cox's avatar
Russ Cox committed
460
	if(i < 0 || i >= nelem(reg) || i >= nelem(regpc))
461 462 463 464
		fatal("regfree: reg out of range");
	if(reg[i] <= 0)
		fatal("regfree: reg not allocated");
	reg[i]--;
Russ Cox's avatar
Russ Cox committed
465 466
	if(reg[i] == 0)
		regpc[i] = 0;
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
}

/*
 * initialize n to be register r of type t.
 */
void
nodreg(Node *n, Type *t, int r)
{
	if(t == T)
		fatal("nodreg: t nil");

	memset(n, 0, sizeof(*n));
	n->op = OREGISTER;
	n->addable = 1;
	ullmancalc(n);
	n->val.u.reg = r;
	n->type = t;
}

/*
 * initialize n to be indirect of register r; n is type t.
 */
void
nodindreg(Node *n, Type *t, int r)
{
	nodreg(n, t, r);
	n->op = OINDREG;
}

Node*
nodarg(Type *t, int fp)
{
499 500 501 502 503 504 505 506 507 508 509 510 511 512
	Node *n;
	Type *first;
	Iter savet;

	// entire argument struct, not just one arg
	if(t->etype == TSTRUCT && t->funarg) {
		n = nod(ONAME, N, N);
		n->sym = lookup(".args");
		n->type = t;
		first = structfirst(&savet, &t);
		if(first == nil)
			fatal("nodarg: bad struct");
		if(first->width == BADWIDTH)
			fatal("nodarg: offset not computed for %T", t);
513
		n->xoffset = first->width;
514 515 516 517 518 519 520 521 522 523 524 525
		n->addable = 1;
		goto fp;
	}

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

	n = nod(ONAME, N, N);
	n->type = t->type;
	n->sym = t->sym;
	if(t->width == BADWIDTH)
		fatal("nodarg: offset not computed for %T", t);
526
	n->xoffset = t->width;
527
	n->addable = 1;
528
	n->orig = t->nname;
529 530

fp:
Russ Cox's avatar
Russ Cox committed
531 532 533 534 535 536
	// Rewrite argument named _ to __,
	// or else the assignment to _ will be
	// discarded during code generation.
	if(isblank(n))
		n->sym = lookup("__");

537
	switch(fp) {
538 539 540
	default:
		fatal("nodarg %T %d", t, fp);

541
	case 0:		// output arg for calling another function
542
		n->op = OINDREG;
543
		n->val.u.reg = REGSP;
544
		n->xoffset += 4;
545 546
		break;

547
	case 1:		// input arg to current function
548 549 550
		n->class = PPARAM;
		break;
	}
Russ Cox's avatar
Russ Cox committed
551
	n->typecheck = 1;
552
	return n;
553 554 555
}

/*
Kai Backman's avatar
Kai Backman committed
556 557
 * return constant i node.
 * overwritten by next call, but useful in calls to gins.
558
 */
Kai Backman's avatar
Kai Backman committed
559 560
Node*
ncon(uint32 i)
561
{
Kai Backman's avatar
Kai Backman committed
562
	static Node n;
563

Kai Backman's avatar
Kai Backman committed
564 565 566 567
	if(n.type == T)
		nodconst(&n, types[TUINT32], 0);
	mpmovecfix(n.val.u.xval, i);
	return &n;
568 569 570
}

/*
571
 * Is this node a memory operand?
572
 */
573 574
int
ismem(Node *n)
575
{
576 577 578 579 580 581 582 583
	switch(n->op) {
	case OINDREG:
	case ONAME:
	case OPARAM:
		return 1;
	}
	return 0;
}
584

Kai Backman's avatar
Kai Backman committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
Node sclean[10];
int nsclean;

/*
 * n is a 64-bit value.  fill in lo and hi to refer to its 32-bit halves.
 */
void
split64(Node *n, Node *lo, Node *hi)
{
	Node n1;
	int64 i;

	if(!is64(n->type))
		fatal("split64 %T", n->type);

	sclean[nsclean].op = OEMPTY;
	if(nsclean >= nelem(sclean))
		fatal("split64 clean");
	nsclean++;
	switch(n->op) {
	default:
		if(!dotaddable(n, &n1)) {
			igen(n, &n1, N);
			sclean[nsclean-1] = n1;
		}
		n = &n1;
		goto common;
	case ONAME:
		if(n->class == PPARAMREF) {
			cgen(n->heapaddr, &n1);
			sclean[nsclean-1] = n1;
			// fall through.
			n = &n1;
		}
		goto common;
	case OINDREG:
	common:
		*lo = *n;
		*hi = *n;
		lo->type = types[TUINT32];
		if(n->type->etype == TINT64)
			hi->type = types[TINT32];
		else
			hi->type = types[TUINT32];
		hi->xoffset += 4;
		break;

	case OLITERAL:
		convconst(&n1, n->type, &n->val);
		i = mpgetfix(n1.val.u.xval);
		nodconst(lo, types[TUINT32], (uint32)i);
		i >>= 32;
		if(n->type->etype == TINT64)
			nodconst(hi, types[TINT32], (int32)i);
		else
			nodconst(hi, types[TUINT32], (uint32)i);
		break;
	}
}

void
splitclean(void)
{
	if(nsclean <= 0)
		fatal("splitclean");
	nsclean--;
	if(sclean[nsclean].op != OEMPTY)
		regfree(&sclean[nsclean]);
}

655
#define	CASE(a,b)	(((a)<<16)|((b)<<0))
656

657 658 659
void
gmove(Node *f, Node *t)
{
660
	int a, ft, tt, fa, ta;
661
	Type *cvt;
Kai Backman's avatar
Kai Backman committed
662 663
	Node r1, r2, flo, fhi, tlo, thi, con;
	Prog *p1;
664 665

	if(debug['M'])
666 667 668 669 670 671
		print("gmove %N -> %N\n", f, t);

	ft = simsimtype(f->type);
	tt = simsimtype(t->type);
	cvt = t->type;

672 673 674 675 676
	if(iscomplex[ft] || iscomplex[tt]) {
		complexmove(f, t);
		return;
	}

677
	// cannot have two memory operands;
678
	// except 64-bit, which always copies via registers anyway.
679
	if(!is64(f->type) && !is64(t->type) && ismem(f) && ismem(t))
Kai Backman's avatar
Kai Backman committed
680
		goto hard;
681 682 683

	// convert constant to desired type
	if(f->op == OLITERAL) {
684 685
		switch(tt) {
		default:
686
			convconst(&con, t->type, &f->val);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
			break;

		case TINT16:
		case TINT8:
			convconst(&con, types[TINT32], &f->val);
			regalloc(&r1, con.type, t);
			gins(AMOVW, &con, &r1);
			gmove(&r1, t);
			regfree(&r1);
			return;

		case TUINT16:
		case TUINT8:
			convconst(&con, types[TUINT32], &f->val);
			regalloc(&r1, con.type, t);
			gins(AMOVW, &con, &r1);
			gmove(&r1, t);
			regfree(&r1);
			return;
		}

708 709 710
		f = &con;
		ft = simsimtype(con.type);

711
		// constants can't move directly to memory
Kai Backman's avatar
Kai Backman committed
712
		if(ismem(t) && !is64(t->type)) goto hard;
713
	}
714

715 716 717 718 719 720
	// value -> value copy, only one memory operand.
	// figure out the instruction to use.
	// break out of switch for one-instruction gins.
	// goto rdst for "destination must be register".
	// goto hard for "convert to cvt type first".
	// otherwise handle and return.
721

722 723 724 725 726 727 728 729 730 731 732 733 734
	switch(CASE(ft, tt)) {
	default:
		goto fatal;

	/*
	 * integer copy and truncate
	 */
	case CASE(TINT8, TINT8):	// same size
	case CASE(TUINT8, TINT8):
	case CASE(TINT16, TINT8):	// truncate
	case CASE(TUINT16, TINT8):
	case CASE(TINT32, TINT8):
	case CASE(TUINT32, TINT8):
735 736 737 738 739
		a = AMOVB;
		break;

	case CASE(TINT8, TUINT8):
	case CASE(TUINT8, TUINT8):
740 741 742 743
	case CASE(TINT16, TUINT8):
	case CASE(TUINT16, TUINT8):
	case CASE(TINT32, TUINT8):
	case CASE(TUINT32, TUINT8):
744
		a = AMOVBU;
745
		break;
746

747 748
	case CASE(TINT64, TINT8):	// truncate low word
	case CASE(TUINT64, TINT8):
Kai Backman's avatar
Kai Backman committed
749 750 751
		a = AMOVB;
		goto trunc64;

752 753
	case CASE(TINT64, TUINT8):
	case CASE(TUINT64, TUINT8):
Kai Backman's avatar
Kai Backman committed
754 755
		a = AMOVBU;
		goto trunc64;
756

757 758 759 760
	case CASE(TINT16, TINT16):	// same size
	case CASE(TUINT16, TINT16):
	case CASE(TINT32, TINT16):	// truncate
	case CASE(TUINT32, TINT16):
761 762 763 764 765
		a = AMOVH;
		break;

	case CASE(TINT16, TUINT16):
	case CASE(TUINT16, TUINT16):
766 767
	case CASE(TINT32, TUINT16):
	case CASE(TUINT32, TUINT16):
768
		a = AMOVHU;
769 770
		break;

771 772
	case CASE(TINT64, TINT16):	// truncate low word
	case CASE(TUINT64, TINT16):
Kai Backman's avatar
Kai Backman committed
773 774 775
		a = AMOVH;
		goto trunc64;

776 777
	case CASE(TINT64, TUINT16):
	case CASE(TUINT64, TUINT16):
Kai Backman's avatar
Kai Backman committed
778 779
		a = AMOVHU;
		goto trunc64;
780

781 782 783 784 785 786
	case CASE(TINT32, TINT32):	// same size
	case CASE(TINT32, TUINT32):
	case CASE(TUINT32, TINT32):
	case CASE(TUINT32, TUINT32):
		a = AMOVW;
		break;
787

788 789 790 791
	case CASE(TINT64, TINT32):	// truncate
	case CASE(TUINT64, TINT32):
	case CASE(TINT64, TUINT32):
	case CASE(TUINT64, TUINT32):
Kai Backman's avatar
Kai Backman committed
792 793 794 795 796 797
		split64(f, &flo, &fhi);
		regalloc(&r1, t->type, N);
		gins(AMOVW, &flo, &r1);
		gins(AMOVW, &r1, t);
		regfree(&r1);
		splitclean();
798 799
		return;

800 801 802 803
	case CASE(TINT64, TINT64):	// same size
	case CASE(TINT64, TUINT64):
	case CASE(TUINT64, TINT64):
	case CASE(TUINT64, TUINT64):
Kai Backman's avatar
Kai Backman committed
804 805
		split64(f, &flo, &fhi);
		split64(t, &tlo, &thi);
Kai Backman's avatar
Kai Backman committed
806 807 808 809 810 811 812 813
		regalloc(&r1, flo.type, N);
		regalloc(&r2, fhi.type, N);
		gins(AMOVW, &flo, &r1);
		gins(AMOVW, &fhi, &r2);
		gins(AMOVW, &r1, &tlo);
		gins(AMOVW, &r2, &thi);
		regfree(&r1);
		regfree(&r2);
Kai Backman's avatar
Kai Backman committed
814 815
		splitclean();
		splitclean();
816 817
		return;

818 819 820
	/*
	 * integer up-conversions
	 */
821 822 823 824 825 826
	case CASE(TINT8, TINT16):	// sign extend int8
	case CASE(TINT8, TUINT16):
	case CASE(TINT8, TINT32):
	case CASE(TINT8, TUINT32):
		a = AMOVB;
		goto rdst;
Kai Backman's avatar
Kai Backman committed
827 828 829 830
	case CASE(TINT8, TINT64):	// convert via int32
	case CASE(TINT8, TUINT64):
		cvt = types[TINT32];
		goto hard;
831

832 833 834 835 836 837
	case CASE(TUINT8, TINT16):	// zero extend uint8
	case CASE(TUINT8, TUINT16):
	case CASE(TUINT8, TINT32):
	case CASE(TUINT8, TUINT32):
		a = AMOVBU;
		goto rdst;
Kai Backman's avatar
Kai Backman committed
838 839 840 841
	case CASE(TUINT8, TINT64):	// convert via uint32
	case CASE(TUINT8, TUINT64):
		cvt = types[TUINT32];
		goto hard;
842

843 844 845 846
	case CASE(TINT16, TINT32):	// sign extend int16
	case CASE(TINT16, TUINT32):
		a = AMOVH;
		goto rdst;
Kai Backman's avatar
Kai Backman committed
847 848 849 850
	case CASE(TINT16, TINT64):	// convert via int32
	case CASE(TINT16, TUINT64):
		cvt = types[TINT32];
		goto hard;
851

852 853 854 855
	case CASE(TUINT16, TINT32):	// zero extend uint16
	case CASE(TUINT16, TUINT32):
		a = AMOVHU;
		goto rdst;
Kai Backman's avatar
Kai Backman committed
856 857 858 859
	case CASE(TUINT16, TINT64):	// convert via uint32
	case CASE(TUINT16, TUINT64):
		cvt = types[TUINT32];
		goto hard;
860

861 862
	case CASE(TINT32, TINT64):	// sign extend int32
	case CASE(TINT32, TUINT64):
Kai Backman's avatar
Kai Backman committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876
		split64(t, &tlo, &thi);
		regalloc(&r1, tlo.type, N);
		regalloc(&r2, thi.type, N);
		gmove(f, &r1);
		p1 = gins(AMOVW, &r1, &r2);
		p1->from.type = D_SHIFT;
		p1->from.offset = 2 << 5 | 31 << 7 | r1.val.u.reg; // r1->31
		p1->from.reg = NREG;
//print("gmove: %P\n", p1);
		gins(AMOVW, &r1, &tlo);
		gins(AMOVW, &r2, &thi);
		regfree(&r1);
		regfree(&r2);
		splitclean();
877 878 879 880
		return;

	case CASE(TUINT32, TINT64):	// zero extend uint32
	case CASE(TUINT32, TUINT64):
Kai Backman's avatar
Kai Backman committed
881 882
		split64(t, &tlo, &thi);
		gmove(f, &tlo);
883 884 885 886
		regalloc(&r1, thi.type, N);
		gins(AMOVW, ncon(0), &r1);
		gins(AMOVW, &r1, &thi);
		regfree(&r1);
Kai Backman's avatar
Kai Backman committed
887
		splitclean();
888
		return;
889

890 891 892 893 894
	/*
	* float to integer
	*/
	case CASE(TFLOAT32, TINT8):
	case CASE(TFLOAT32, TUINT8):
Kai Backman's avatar
Kai Backman committed
895
	case CASE(TFLOAT32, TINT16):
896
	case CASE(TFLOAT32, TUINT16):
Kai Backman's avatar
Kai Backman committed
897
	case CASE(TFLOAT32, TINT32):
898
	case CASE(TFLOAT32, TUINT32):
Ken Thompson's avatar
Ken Thompson committed
899
//	case CASE(TFLOAT32, TUINT64):
900

901 902
	case CASE(TFLOAT64, TINT8):
	case CASE(TFLOAT64, TUINT8):
Kai Backman's avatar
Kai Backman committed
903
	case CASE(TFLOAT64, TINT16):
904
	case CASE(TFLOAT64, TUINT16):
Kai Backman's avatar
Kai Backman committed
905
	case CASE(TFLOAT64, TINT32):
906
	case CASE(TFLOAT64, TUINT32):
Ken Thompson's avatar
Ken Thompson committed
907 908 909 910 911 912 913
//	case CASE(TFLOAT64, TUINT64):
		fa = AMOVF;
		a = AMOVFW;
		if(ft == TFLOAT64) {
			fa = AMOVD;
			a = AMOVDW;
		}
914
		ta = AMOVW;
Ken Thompson's avatar
Ken Thompson committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928
		switch(tt) {
		case TINT8:
			ta = AMOVB;
			break;
		case TUINT8:
			ta = AMOVBU;
			break;
		case TINT16:
			ta = AMOVH;
			break;
		case TUINT16:
			ta = AMOVHU;
			break;
		}
929

Ken Thompson's avatar
Ken Thompson committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943
		regalloc(&r1, types[ft], f);
		regalloc(&r2, types[tt], t);
		gins(fa, f, &r1);	// load to fpu
		p1 = gins(a, &r1, &r1);	// convert to w
		switch(tt) {
		case TUINT8:
		case TUINT16:
		case TUINT32:
			p1->scond |= C_UBIT;
		}
		gins(AMOVW, &r1, &r2);	// copy to cpu
		gins(ta, &r2, t);	// store
		regfree(&r1);
		regfree(&r2);
944 945 946 947 948 949 950
		return;

	/*
	 * integer to float
	 */
	case CASE(TINT8, TFLOAT32):
	case CASE(TUINT8, TFLOAT32):
Kai Backman's avatar
Kai Backman committed
951
	case CASE(TINT16, TFLOAT32):
952
	case CASE(TUINT16, TFLOAT32):
Kai Backman's avatar
Kai Backman committed
953
	case CASE(TINT32, TFLOAT32):
954 955 956
	case CASE(TUINT32, TFLOAT32):
	case CASE(TINT8, TFLOAT64):
	case CASE(TUINT8, TFLOAT64):
Kai Backman's avatar
Kai Backman committed
957
	case CASE(TINT16, TFLOAT64):
958
	case CASE(TUINT16, TFLOAT64):
Kai Backman's avatar
Kai Backman committed
959
	case CASE(TINT32, TFLOAT64):
960
	case CASE(TUINT32, TFLOAT64):
961
		fa = AMOVW;
Ken Thompson's avatar
Ken Thompson committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
		switch(ft) {
		case TINT8:
			fa = AMOVB;
			break;
		case TUINT8:
			fa = AMOVBU;
			break;
		case TINT16:
			fa = AMOVH;
			break;
		case TUINT16:
			fa = AMOVHU;
			break;
		}
		a = AMOVWF;
		ta = AMOVF;
		if(tt == TFLOAT64) {
			a = AMOVWD;
			ta = AMOVD;
		}
		regalloc(&r1, types[ft], f);
		regalloc(&r2, types[tt], t);
		gins(fa, f, &r1);	// load to cpu
		gins(AMOVW, &r1, &r2);	// copy to fpu
		p1 = gins(a, &r2, &r2);	// convert
		switch(ft) {
		case TUINT8:
		case TUINT16:
		case TUINT32:
			p1->scond |= C_UBIT;
		}
		gins(ta, &r2, t);	// store
		regfree(&r1);
		regfree(&r2);
		return;
997 998 999

	case CASE(TUINT64, TFLOAT32):
	case CASE(TUINT64, TFLOAT64):
1000
		fatal("gmove UINT64, TFLOAT not implemented");
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		return;


	/*
	 * float to float
	 */
	case CASE(TFLOAT32, TFLOAT32):
		a = AMOVF;
		break;

	case CASE(TFLOAT64, TFLOAT64):
		a = AMOVD;
		break;

	case CASE(TFLOAT32, TFLOAT64):
1016 1017 1018 1019 1020 1021
		regalloc(&r1, types[TFLOAT64], t);
		gins(AMOVF, f, &r1);
		gins(AMOVFD, &r1, &r1);
		gins(AMOVD, &r1, t);
		regfree(&r1);
		return;
1022 1023

	case CASE(TFLOAT64, TFLOAT32):
1024 1025 1026 1027 1028 1029
		regalloc(&r1, types[TFLOAT64], t);
		gins(AMOVD, f, &r1);
		gins(AMOVDF, &r1, &r1);
		gins(AMOVF, &r1, t);
		regfree(&r1);
		return;
1030 1031 1032
	}

	gins(a, f, t);
1033 1034
	return;

1035 1036 1037
rdst:
	// TODO(kaib): we almost always require a register dest anyway, this can probably be
	// removed.
1038 1039 1040 1041 1042 1043 1044 1045 1046
	// requires register destination
	regalloc(&r1, t->type, t);
	gins(a, f, &r1);
	gmove(&r1, t);
	regfree(&r1);
	return;

hard:
	// requires register intermediate
1047
	regalloc(&r1, cvt, t);
1048 1049 1050 1051 1052
	gmove(f, &r1);
	gmove(&r1, t);
	regfree(&r1);
	return;

Kai Backman's avatar
Kai Backman committed
1053 1054 1055 1056 1057 1058 1059 1060
trunc64:
	// truncate 64 bit integer
	split64(f, &flo, &fhi);
	regalloc(&r1, t->type, N);
	gins(a, &flo, &r1);
	gins(a, &r1, t);
	regfree(&r1);
	splitclean();
1061 1062 1063 1064 1065
	return;

fatal:
	// should not happen
	fatal("gmove %N -> %N", f, t);
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
}

int
samaddr(Node *f, Node *t)
{

	if(f->op != t->op)
		return 0;

	switch(f->op) {
	case OREGISTER:
		if(f->val.u.reg != t->val.u.reg)
			break;
		return 1;
	}
	return 0;
}

/*
 * generate one instruction:
 *	as f, t
 */
Prog*
gins(int as, Node *f, Node *t)
{
Kai Backman's avatar
Kai Backman committed
1091 1092
//	Node nod;
//	int32 v;
1093
	Prog *p;
1094
	Addr af, at;
1095

1096 1097
	if(f != N && f->op == OINDEX) {
		fatal("gins OINDEX not implemented");
1098 1099 1100 1101 1102 1103
//		regalloc(&nod, &regnode, Z);
//		v = constnode.vconst;
//		cgen(f->right, &nod);
//		constnode.vconst = v;
//		idx.reg = nod.reg;
//		regfree(&nod);
1104 1105 1106
	}
	if(t != N && t->op == OINDEX) {
		fatal("gins OINDEX not implemented");
1107 1108 1109 1110 1111 1112
//		regalloc(&nod, &regnode, Z);
//		v = constnode.vconst;
//		cgen(t->right, &nod);
//		constnode.vconst = v;
//		idx.reg = nod.reg;
//		regfree(&nod);
1113
	}
1114

1115 1116
	memset(&af, 0, sizeof af);
	memset(&at, 0, sizeof at);
1117
	if(f != N)
1118
		naddr(f, &af, 1);
1119
	if(t != N)
Russ Cox's avatar
Russ Cox committed
1120 1121
		naddr(t, &at, 1);
	p = prog(as);
1122 1123 1124 1125
	if(f != N)
		p->from = af;
	if(t != N)
		p->to = at;
1126 1127 1128 1129 1130
	if(debug['g'])
		print("%P\n", p);
	return p;
}

Kai Backman's avatar
Kai Backman committed
1131 1132 1133 1134 1135 1136 1137 1138
/*
 * insert n into reg slot of p
 */
void
raddr(Node *n, Prog *p)
{
	Addr a;

1139
	naddr(n, &a, 1);
Kai Backman's avatar
Kai Backman committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	if(a.type != D_REG && a.type != D_FREG) {
		if(n)
			fatal("bad in raddr: %O", n->op);
		else
			fatal("bad in raddr: <null>");
		p->reg = NREG;
	} else
		p->reg = a.reg;
}

1150
/* generate a comparison
Kai Backman's avatar
Kai Backman committed
1151
TODO(kaib): one of the args can actually be a small constant. relax the constraint and fix call sites.
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
 */
Prog*
gcmp(int as, Node *lhs, Node *rhs)
{
	Prog *p;

	if(lhs->op != OREGISTER || rhs->op != OREGISTER)
		fatal("bad operands to gcmp: %O %O", lhs->op, rhs->op);

	p = gins(as, rhs, N);
	raddr(lhs, p);
	return p;
}

1166
/* generate a constant shift
1167
 * arm encodes a shift by 32 as 0, thus asking for 0 shift is illegal.
1168 1169 1170 1171 1172 1173
*/
Prog*
gshift(int as, Node *lhs, int32 stype, int32 sval, Node *rhs)
{
	Prog *p;

1174
	if(sval <= 0 || sval > 32)
1175 1176
		fatal("bad shift value: %d", sval);

1177 1178
	sval = sval&0x1f;

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	p = gins(as, N, rhs);
	p->from.type = D_SHIFT;
	p->from.offset = stype | sval<<7 | lhs->val.u.reg;
	return p;
}

/* generate a register shift
*/
Prog *
gregshift(int as, Node *lhs, int32 stype, Node *reg, Node *rhs)
{
	Prog *p;
	p = gins(as, N, rhs);
	p->from.type = D_SHIFT;
	p->from.offset = stype | reg->val.u.reg << 8 | 1<<4 | lhs->val.u.reg;
	return p;
}

1197 1198 1199 1200 1201 1202 1203 1204 1205
static void
checkoffset(Addr *a, int canemitcode)
{
	Prog *p;
	Node n1;

	if(a->offset < unmappedzero)
		return;
	if(!canemitcode)
1206
		fatal("checkoffset %#x, cannot emit code", a->offset);
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

	// cannot rely on unmapped nil page at 0 to catch
	// reference with large offset.  instead, emit explicit
	// test of 0(reg).
	regalloc(&n1, types[TUINTPTR], N);
	p = gins(AMOVW, N, &n1);
	p->from = *a;
	p->from.offset = 0;
	regfree(&n1);
}
1217

1218 1219 1220 1221 1222
/*
 * generate code to compute n;
 * make a refer to result.
 */
void
1223
naddr(Node *n, Addr *a, int canemitcode)
1224 1225
{
	a->type = D_NONE;
1226 1227
	a->name = D_NONE;
	a->reg = NREG;
Luuk van Dijk's avatar
Luuk van Dijk committed
1228
	a->node = N;
Russ Cox's avatar
Russ Cox committed
1229
	a->etype = 0;
1230 1231 1232 1233 1234 1235 1236 1237
	if(n == N)
		return;

	switch(n->op) {
	default:
		fatal("naddr: bad %O %D", n->op, a);
		break;

1238
	case OREGISTER:
1239
		if(n->val.u.reg <= REGALLOC_RMAX) {
1240
			a->type = D_REG;
1241
			a->reg = n->val.u.reg;
1242 1243
		} else {
			a->type = D_FREG;
1244
			a->reg = n->val.u.reg - REGALLOC_F0;
1245
		}
1246 1247
		a->sym = S;
		break;
1248

1249 1250
	case OINDEX:
	case OIND:
1251
		fatal("naddr: OINDEX");
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
//		naddr(n->left, a);
//		if(a->type >= D_AX && a->type <= D_DI)
//			a->type += D_INDIR;
//		else
//		if(a->type == D_CONST)
//			a->type = D_NONE+D_INDIR;
//		else
//		if(a->type == D_ADDR) {
//			a->type = a->index;
//			a->index = D_NONE;
//		} else
//			goto bad;
//		if(n->op == OINDEX) {
//			a->index = idx.reg;
//			a->scale = n->scale;
//		}
//		break;

1270 1271 1272 1273 1274
	case OINDREG:
		a->type = D_OREG;
		a->reg = n->val.u.reg;
		a->sym = n->sym;
		a->offset = n->xoffset;
1275
		checkoffset(a, canemitcode);
1276
		break;
1277

1278 1279 1280 1281 1282 1283 1284 1285 1286
	case OPARAM:
		// n->left is PHEAP ONAME for stack parameter.
		// compute address of actual parameter on stack.
		a->etype = simtype[n->left->type->etype];
		a->width = n->left->type->width;
		a->offset = n->xoffset;
		a->sym = n->left->sym;
		a->type = D_OREG;
		a->name = D_PARAM;
1287
		a->node = n->left->orig;
1288
		break;
1289 1290 1291

	case ONAME:
		a->etype = 0;
1292
		a->width = 0;
1293
		a->reg = NREG;
1294
		if(n->type != T) {
1295
			a->etype = simtype[n->type->etype];
1296 1297
			a->width = n->type->width;
		}
1298 1299
		a->offset = n->xoffset;
		a->sym = n->sym;
1300 1301 1302
		a->node = n->orig;
		//if(a->node >= (Node*)&n)
		//	fatal("stack node");
1303 1304 1305 1306 1307
		if(a->sym == S)
			a->sym = lookup(".noname");
		if(n->method) {
			if(n->type != T)
			if(n->type->sym != S)
1308 1309
			if(n->type->sym->pkg != nil)
				a->sym = pkglookup(a->sym->name, n->type->sym->pkg);
1310 1311
		}

1312
		a->type = D_OREG;
1313 1314 1315 1316
		switch(n->class) {
		default:
			fatal("naddr: ONAME class %S %d\n", n->sym, n->class);
		case PEXTERN:
1317
			a->name = D_EXTERN;
1318 1319
			break;
		case PAUTO:
1320
			a->name = D_AUTO;
1321 1322 1323
			break;
		case PPARAM:
		case PPARAMOUT:
1324
			a->name = D_PARAM;
1325 1326
			break;
		case PFUNC:
1327
			a->name = D_EXTERN;
Kai Backman's avatar
Kai Backman committed
1328
			a->type = D_CONST;
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
			break;
		}
		break;

	case OLITERAL:
		switch(n->val.ctype) {
		default:
			fatal("naddr: const %lT", n->type);
			break;
		case CTFLT:
			a->type = D_FCONST;
			a->dval = mpgetflt(n->val.u.fval);
			break;
		case CTINT:
1343
		case CTRUNE:
1344 1345 1346 1347 1348
			a->sym = S;
			a->type = D_CONST;
			a->offset = mpgetfix(n->val.u.xval);
			break;
		case CTSTR:
1349
			datagostring(n->val.u.sval, a);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
			break;
		case CTBOOL:
			a->sym = S;
			a->type = D_CONST;
			a->offset = n->val.u.bval;
			break;
		case CTNIL:
			a->sym = S;
			a->type = D_CONST;
			a->offset = 0;
			break;
		}
		break;

Russ Cox's avatar
Russ Cox committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
	case OITAB:
		// itable of interface value
		naddr(n->left, a, canemitcode);
		a->etype = TINT32;
		if(a->type == D_CONST && a->offset == 0)
			break;	// len(nil)
		if(a->offset >= unmappedzero && a->offset-Array_nel < unmappedzero)
			checkoffset(a, canemitcode);
		break;

1374 1375
	case OLEN:
		// len of string or slice
1376
		naddr(n->left, a, canemitcode);
Russ Cox's avatar
Russ Cox committed
1377
		a->etype = TINT32;
1378 1379
		if(a->type == D_CONST && a->offset == 0)
			break;	// len(nil)
1380
		a->offset += Array_nel;
1381 1382
		if(a->offset >= unmappedzero && a->offset-Array_nel < unmappedzero)
			checkoffset(a, canemitcode);
1383 1384 1385 1386
		break;

	case OCAP:
		// cap of string or slice
1387
		naddr(n->left, a, canemitcode);
Russ Cox's avatar
Russ Cox committed
1388
		a->etype = TINT32;
1389 1390
		if(a->type == D_CONST && a->offset == 0)
			break;	// cap(nil)
1391
		a->offset += Array_cap;
1392 1393
		if(a->offset >= unmappedzero && a->offset-Array_cap < unmappedzero)
			checkoffset(a, canemitcode);
1394 1395
		break;

1396
	case OADDR:
1397
		naddr(n->left, a, canemitcode);
Russ Cox's avatar
Russ Cox committed
1398
		a->etype = tptr;
Kai Backman's avatar
Kai Backman committed
1399 1400
		switch(a->type) {
		case D_OREG:
1401 1402
			a->type = D_CONST;
			break;
Kai Backman's avatar
Kai Backman committed
1403 1404 1405

		case D_REG:
		case D_CONST:
1406
			break;
Kai Backman's avatar
Kai Backman committed
1407 1408 1409
		
		default:
			fatal("naddr: OADDR %d\n", a->type);
1410
		}
1411 1412 1413 1414 1415 1416 1417 1418 1419
	}
}

/*
 * return Axxx for Oxxx on type t.
 */
int
optoas(int op, Type *t)
{
1420 1421 1422 1423 1424 1425 1426 1427
	int a;

	if(t == T)
		fatal("optoas: t is nil");

	a = AGOK;
	switch(CASE(op, simtype[t->etype])) {
	default:
1428
		fatal("optoas: no entry %O-%T etype %T simtype %T", op, t, types[t->etype], types[simtype[t->etype]]);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
		break;

/*	case CASE(OADDR, TPTR32):
		a = ALEAL;
		break;

	case CASE(OADDR, TPTR64):
		a = ALEAQ;
		break;
*/
1439
	// TODO(kaib): make sure the conditional branches work on all edge cases
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
	case CASE(OEQ, TBOOL):
	case CASE(OEQ, TINT8):
	case CASE(OEQ, TUINT8):
	case CASE(OEQ, TINT16):
	case CASE(OEQ, TUINT16):
	case CASE(OEQ, TINT32):
	case CASE(OEQ, TUINT32):
	case CASE(OEQ, TINT64):
	case CASE(OEQ, TUINT64):
	case CASE(OEQ, TPTR32):
	case CASE(OEQ, TPTR64):
	case CASE(OEQ, TFLOAT32):
	case CASE(OEQ, TFLOAT64):
		a = ABEQ;
		break;

	case CASE(ONE, TBOOL):
	case CASE(ONE, TINT8):
	case CASE(ONE, TUINT8):
	case CASE(ONE, TINT16):
	case CASE(ONE, TUINT16):
	case CASE(ONE, TINT32):
	case CASE(ONE, TUINT32):
	case CASE(ONE, TINT64):
	case CASE(ONE, TUINT64):
	case CASE(ONE, TPTR32):
	case CASE(ONE, TPTR64):
	case CASE(ONE, TFLOAT32):
	case CASE(ONE, TFLOAT64):
		a = ABNE;
		break;

	case CASE(OLT, TINT8):
	case CASE(OLT, TINT16):
	case CASE(OLT, TINT32):
	case CASE(OLT, TINT64):
1476 1477 1478 1479 1480
	case CASE(OLT, TFLOAT32):
	case CASE(OLT, TFLOAT64):
		a = ABLT;
		break;

1481 1482 1483 1484
	case CASE(OLT, TUINT8):
	case CASE(OLT, TUINT16):
	case CASE(OLT, TUINT32):
	case CASE(OLT, TUINT64):
1485
		a = ABLO;
1486 1487 1488 1489 1490 1491
		break;

	case CASE(OLE, TINT8):
	case CASE(OLE, TINT16):
	case CASE(OLE, TINT32):
	case CASE(OLE, TINT64):
1492 1493 1494 1495 1496
	case CASE(OLE, TFLOAT32):
	case CASE(OLE, TFLOAT64):
		a = ABLE;
		break;

1497 1498 1499 1500
	case CASE(OLE, TUINT8):
	case CASE(OLE, TUINT16):
	case CASE(OLE, TUINT32):
	case CASE(OLE, TUINT64):
1501
		a = ABLS;
1502 1503 1504 1505 1506 1507
		break;

	case CASE(OGT, TINT8):
	case CASE(OGT, TINT16):
	case CASE(OGT, TINT32):
	case CASE(OGT, TINT64):
1508 1509 1510 1511 1512
	case CASE(OGT, TFLOAT32):
	case CASE(OGT, TFLOAT64):
		a = ABGT;
		break;

1513 1514 1515 1516
	case CASE(OGT, TUINT8):
	case CASE(OGT, TUINT16):
	case CASE(OGT, TUINT32):
	case CASE(OGT, TUINT64):
1517
		a = ABHI;
1518 1519 1520 1521 1522 1523
		break;

	case CASE(OGE, TINT8):
	case CASE(OGE, TINT16):
	case CASE(OGE, TINT32):
	case CASE(OGE, TINT64):
1524 1525 1526 1527 1528
	case CASE(OGE, TFLOAT32):
	case CASE(OGE, TFLOAT64):
		a = ABGE;
		break;

1529 1530 1531 1532
	case CASE(OGE, TUINT8):
	case CASE(OGE, TUINT16):
	case CASE(OGE, TUINT32):
	case CASE(OGE, TUINT64):
1533
		a = ABHS;
1534 1535 1536 1537 1538
		break;

	case CASE(OCMP, TBOOL):
	case CASE(OCMP, TINT8):
	case CASE(OCMP, TUINT8):
Kai Backman's avatar
Kai Backman committed
1539 1540
	case CASE(OCMP, TINT16):
	case CASE(OCMP, TUINT16):
1541 1542 1543 1544 1545
	case CASE(OCMP, TINT32):
	case CASE(OCMP, TUINT32):
	case CASE(OCMP, TPTR32):
		a = ACMP;
		break;
1546

1547 1548 1549
	case CASE(OCMP, TFLOAT32):
		a = ACMPF;
		break;
1550

1551 1552 1553
	case CASE(OCMP, TFLOAT64):
		a = ACMPD;
		break;
1554

1555 1556 1557 1558
	case CASE(OAS, TBOOL):
	case CASE(OAS, TINT8):
		a = AMOVB;
		break;
1559

1560 1561 1562 1563
	case CASE(OAS, TUINT8):
		a = AMOVBU;
		break;

1564 1565 1566
	case CASE(OAS, TINT16):
		a = AMOVH;
		break;
1567

1568 1569 1570 1571
	case CASE(OAS, TUINT16):
		a = AMOVHU;
		break;

1572 1573 1574 1575 1576
	case CASE(OAS, TINT32):
	case CASE(OAS, TUINT32):
	case CASE(OAS, TPTR32):
		a = AMOVW;
		break;
1577

1578 1579 1580
	case CASE(OAS, TFLOAT32):
		a = AMOVF;
		break;
1581

1582 1583 1584
	case CASE(OAS, TFLOAT64):
		a = AMOVD;
		break;
1585

1586 1587 1588 1589 1590 1591 1592 1593 1594
	case CASE(OADD, TINT8):
	case CASE(OADD, TUINT8):
	case CASE(OADD, TINT16):
	case CASE(OADD, TUINT16):
	case CASE(OADD, TINT32):
	case CASE(OADD, TUINT32):
	case CASE(OADD, TPTR32):
		a = AADD;
		break;
1595

1596 1597 1598
	case CASE(OADD, TFLOAT32):
		a = AADDF;
		break;
1599

1600 1601 1602
	case CASE(OADD, TFLOAT64):
		a = AADDD;
		break;
1603

1604 1605 1606 1607 1608 1609 1610 1611 1612
	case CASE(OSUB, TINT8):
	case CASE(OSUB, TUINT8):
	case CASE(OSUB, TINT16):
	case CASE(OSUB, TUINT16):
	case CASE(OSUB, TINT32):
	case CASE(OSUB, TUINT32):
	case CASE(OSUB, TPTR32):
		a = ASUB;
		break;
1613

1614 1615 1616
	case CASE(OSUB, TFLOAT32):
		a = ASUBF;
		break;
1617

1618 1619 1620
	case CASE(OSUB, TFLOAT64):
		a = ASUBD;
		break;
1621

1622 1623 1624 1625 1626 1627 1628 1629 1630
	case CASE(OAND, TINT8):
	case CASE(OAND, TUINT8):
	case CASE(OAND, TINT16):
	case CASE(OAND, TUINT16):
	case CASE(OAND, TINT32):
	case CASE(OAND, TUINT32):
	case CASE(OAND, TPTR32):
		a = AAND;
		break;
1631

1632 1633 1634 1635 1636 1637 1638 1639 1640
	case CASE(OOR, TINT8):
	case CASE(OOR, TUINT8):
	case CASE(OOR, TINT16):
	case CASE(OOR, TUINT16):
	case CASE(OOR, TINT32):
	case CASE(OOR, TUINT32):
	case CASE(OOR, TPTR32):
		a = AORR;
		break;
1641

1642 1643 1644 1645 1646 1647 1648 1649 1650
	case CASE(OXOR, TINT8):
	case CASE(OXOR, TUINT8):
	case CASE(OXOR, TINT16):
	case CASE(OXOR, TUINT16):
	case CASE(OXOR, TINT32):
	case CASE(OXOR, TUINT32):
	case CASE(OXOR, TPTR32):
		a = AEOR;
		break;
1651

1652 1653 1654 1655 1656 1657 1658 1659 1660
	case CASE(OLSH, TINT8):
	case CASE(OLSH, TUINT8):
	case CASE(OLSH, TINT16):
	case CASE(OLSH, TUINT16):
	case CASE(OLSH, TINT32):
	case CASE(OLSH, TUINT32):
	case CASE(OLSH, TPTR32):
		a = ASLL;
		break;
1661

1662 1663 1664 1665 1666 1667
	case CASE(ORSH, TUINT8):
	case CASE(ORSH, TUINT16):
	case CASE(ORSH, TUINT32):
	case CASE(ORSH, TPTR32):
		a = ASRL;
		break;
1668

1669 1670 1671 1672 1673
	case CASE(ORSH, TINT8):
	case CASE(ORSH, TINT16):
	case CASE(ORSH, TINT32):
		a = ASRA;
		break;
1674

1675 1676 1677 1678
	case CASE(OMUL, TUINT8):
	case CASE(OMUL, TUINT16):
	case CASE(OMUL, TUINT32):
	case CASE(OMUL, TPTR32):
1679 1680 1681 1682 1683 1684
		a = AMULU;
		break;

	case CASE(OMUL, TINT8):
	case CASE(OMUL, TINT16):
	case CASE(OMUL, TINT32):
1685 1686
		a = AMUL;
		break;
1687

1688 1689 1690
	case CASE(OMUL, TFLOAT32):
		a = AMULF;
		break;
1691

1692 1693 1694
	case CASE(OMUL, TFLOAT64):
		a = AMULD;
		break;
1695

1696 1697 1698 1699
	case CASE(ODIV, TUINT8):
	case CASE(ODIV, TUINT16):
	case CASE(ODIV, TUINT32):
	case CASE(ODIV, TPTR32):
1700 1701 1702 1703 1704 1705
		a = ADIVU;
		break;

	case CASE(ODIV, TINT8):
	case CASE(ODIV, TINT16):
	case CASE(ODIV, TINT32):
1706 1707
		a = ADIV;
		break;
1708

1709 1710 1711 1712
	case CASE(OMOD, TUINT8):
	case CASE(OMOD, TUINT16):
	case CASE(OMOD, TUINT32):
	case CASE(OMOD, TPTR32):
1713 1714 1715 1716 1717 1718
		a = AMODU;
		break;

	case CASE(OMOD, TINT8):
	case CASE(OMOD, TINT16):
	case CASE(OMOD, TINT32):
1719 1720
		a = AMOD;
		break;
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733

//	case CASE(OEXTEND, TINT16):
//		a = ACWD;
//		break;

//	case CASE(OEXTEND, TINT32):
//		a = ACDQ;
//		break;

//	case CASE(OEXTEND, TINT64):
//		a = ACQO;
//		break;

1734 1735 1736
	case CASE(ODIV, TFLOAT32):
		a = ADIVF;
		break;
1737

1738 1739 1740
	case CASE(ODIV, TFLOAT64):
		a = ADIVD;
		break;
1741 1742 1743

	}
	return a;
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
}

enum
{
	ODynam	= 1<<0,
	OPtrto	= 1<<1,
};

static	Node	clean[20];
static	int	cleani = 0;

void
sudoclean(void)
{
	if(clean[cleani-1].op != OEMPTY)
		regfree(&clean[cleani-1]);
	if(clean[cleani-2].op != OEMPTY)
		regfree(&clean[cleani-2]);
	cleani -= 2;
}

Kai Backman's avatar
Kai Backman committed
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
int
dotaddable(Node *n, Node *n1)
{
	int o, oary[10];
	Node *nn;

	if(n->op != ODOT)
		return 0;

	o = dotoffset(n, oary, &nn);
	if(nn != N && nn->addable && o == 1 && oary[0] >= 0) {
		*n1 = *nn;
		n1->type = n->type;
		n1->xoffset += oary[0];
		return 1;
	}
	return 0;
}

1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
/*
 * generate code to compute address of n,
 * a reference to a (perhaps nested) field inside
 * an array or struct.
 * return 0 on failure, 1 on success.
 * on success, leaves usable address in a.
 *
 * caller is responsible for calling sudoclean
 * after successful sudoaddable,
 * to release the register used for a.
 */
int
1796
sudoaddable(int as, Node *n, Addr *a, int *w)
1797
{
1798
	int o, i;
1799
	int oary[10];
1800
	int64 v;
Kai Backman's avatar
Kai Backman committed
1801
	Node n1, n2, n3, n4, *nn, *l, *r;
1802
	Node *reg, *reg1;
1803
	Prog *p1, *p2;
1804 1805 1806 1807 1808 1809
	Type *t;

	if(n->type == T)
		return 0;

	switch(n->op) {
1810
	case OLITERAL:
1811
		if(!isconst(n, CTINT))
1812 1813 1814 1815 1816
			break;
		v = mpgetfix(n->val.u.xval);
		if(v >= 32000 || v <= -32000)
			break;
		goto lit;
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827

	case ODOT:
	case ODOTPTR:
		cleani += 2;
		reg = &clean[cleani-1];
		reg1 = &clean[cleani-2];
		reg->op = OEMPTY;
		reg1->op = OEMPTY;
		goto odot;

	case OINDEX:
1828 1829
		if(n->left->type->etype == TSTRING)
			return 0;
1830 1831 1832 1833 1834 1835 1836
		cleani += 2;
		reg = &clean[cleani-1];
		reg1 = &clean[cleani-2];
		reg->op = OEMPTY;
		reg1->op = OEMPTY;
		goto oindex;
	}
1837 1838 1839
	return 0;

lit:
1840 1841 1842 1843 1844 1845 1846 1847
	switch(as) {
	default:
		return 0;
	case AADD: case ASUB: case AAND: case AORR: case AEOR:
	case AMOVB: case AMOVBU: case AMOVH: case AMOVHU:
	case AMOVW:
		break;
	}
1848

Kai Backman's avatar
Kai Backman committed
1849 1850 1851 1852 1853
	cleani += 2;
	reg = &clean[cleani-1];
	reg1 = &clean[cleani-2];
	reg->op = OEMPTY;
	reg1->op = OEMPTY;
1854
	naddr(n, a, 1);
Kai Backman's avatar
Kai Backman committed
1855
	goto yes;
1856 1857 1858 1859 1860 1861

odot:
	o = dotoffset(n, oary, &nn);
	if(nn == N)
		goto no;

1862 1863 1864 1865 1866
	if(nn->addable && o == 1 && oary[0] >= 0) {
		// directly addressable set of DOTs
		n1 = *nn;
		n1.type = n->type;
		n1.xoffset += oary[0];
1867
		naddr(&n1, a, 1);
1868 1869 1870
		goto yes;
	}

1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
	regalloc(reg, types[tptr], N);
	n1 = *reg;
	n1.op = OINDREG;
	if(oary[0] >= 0) {
		agen(nn, reg);
		n1.xoffset = oary[0];
	} else {
		cgen(nn, reg);
		n1.xoffset = -(oary[0]+1);
	}

Kai Backman's avatar
Kai Backman committed
1882 1883 1884 1885 1886 1887
	for(i=1; i<o; i++) {
		if(oary[i] >= 0)
			fatal("cant happen");
		gins(AMOVW, &n1, reg);
		n1.xoffset = -(oary[i]+1);
	}
1888 1889

	a->type = D_NONE;
1890
	a->name = D_NONE;
Russ Cox's avatar
Russ Cox committed
1891
	n1.type = n->type;
1892
	naddr(&n1, a, 1);
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
	goto yes;

oindex:
	l = n->left;
	r = n->right;
	if(l->ullman >= UINF && r->ullman >= UINF)
		goto no;

	// set o to type of array
	o = 0;
	if(isptr[l->type->etype]) {
		o += OPtrto;
		if(l->type->type->etype != TARRAY)
			fatal("not ptr ary");
		if(l->type->type->bound < 0)
			o += ODynam;
	} else {
		if(l->type->etype != TARRAY)
			fatal("not ary");
		if(l->type->bound < 0)
			o += ODynam;
	}

1916
	*w = n->type->width;
1917 1918 1919
	if(isconst(r, CTINT))
		goto oindex_const;

1920
	switch(*w) {
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
	default:
		goto no;
	case 1:
	case 2:
	case 4:
	case 8:
		break;
	}

	// load the array (reg)
	if(l->ullman > r->ullman) {
		regalloc(reg, types[tptr], N);
		if(o & OPtrto)
			cgen(l, reg);
		else
			agen(l, reg);
	}

	// load the index (reg1)
1940
	t = types[TUINT32];
1941
	if(issigned[r->type->etype])
1942
		t = types[TINT32];
1943
	regalloc(reg1, t, N);
1944 1945
	regalloc(&n3, types[TINT32], reg1);
	p2 = cgenindex(r, &n3);
1946 1947
	gmove(&n3, reg1);
	regfree(&n3);
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965

	// load the array (reg)
	if(l->ullman <= r->ullman) {
		regalloc(reg, types[tptr], N);
		if(o & OPtrto)
			cgen(l, reg);
		else
			agen(l, reg);
	}

	// check bounds
	if(!debug['B']) {
		if(o & ODynam) {
			n2 = *reg;
			n2.op = OINDREG;
			n2.type = types[tptr];
			n2.xoffset = Array_nel;
		} else {
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
			if(l->type->width >= unmappedzero && l->op == OIND) {
				// cannot rely on page protections to
				// catch array ptr == 0, so dereference.
				n2 = *reg;
				n2.op = OINDREG;
				n2.type = types[TUINTPTR];
				n2.xoffset = 0;
				regalloc(&n3, n2.type, N);
				gins(AMOVW, &n2, &n3);
				regfree(&n3);
			}
1977
			nodconst(&n2, types[TUINT32], l->type->bound);
1978
			if(o & OPtrto)
1979
				nodconst(&n2, types[TUINT32], l->type->type->bound);
1980
		}
1981 1982
		regalloc(&n3, n2.type, N);
		cgen(&n2, &n3);
1983
		gcmp(optoas(OCMP, types[TUINT32]), reg1, &n3);
1984
		regfree(&n3);
1985
		p1 = gbranch(optoas(OLT, types[TUINT32]), T);
1986 1987
		if(p2)
			patch(p2, pc);
1988
		ginscall(panicindex, 0);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
		patch(p1, pc);
	}

	if(o & ODynam) {
		n2 = *reg;
		n2.op = OINDREG;
		n2.type = types[tptr];
		n2.xoffset = Array_array;
		gmove(&n2, reg);
	}

2000 2001
	switch(*w) {
	case 1:
2002
		gins(AADD, reg1, reg);
2003 2004
		break;
	case 2:
2005
		gshift(AADD, reg1, SHIFT_LL, 1, reg);
2006 2007
		break;
	case 4:
2008
		gshift(AADD, reg1, SHIFT_LL, 2, reg);
2009 2010
		break;
	case 8:
2011
		gshift(AADD, reg1, SHIFT_LL, 3, reg);
2012 2013
		break;
	}
2014

2015
	naddr(reg1, a, 1);
2016 2017
	a->type = D_OREG;
	a->reg = reg->val.u.reg;
2018
	a->offset = 0;
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	goto yes;

oindex_const:
	// index is constant
	// can check statically and
	// can multiply by width statically

	regalloc(reg, types[tptr], N);
	if(o & OPtrto)
		cgen(l, reg);
	else
		agen(l, reg);

	v = mpgetfix(r->val.u.xval);
	if(o & ODynam) {

2035
		if(!debug['B'] && !n->etype) {
2036 2037 2038 2039
			n1 = *reg;
			n1.op = OINDREG;
			n1.type = types[tptr];
			n1.xoffset = Array_nel;
2040
			nodconst(&n2, types[TUINT32], v);
2041 2042
			regalloc(&n3, types[TUINT32], N);
			cgen(&n2, &n3);
Kai Backman's avatar
Kai Backman committed
2043 2044
			regalloc(&n4, n1.type, N);
			cgen(&n1, &n4);
2045
			gcmp(optoas(OCMP, types[TUINT32]), &n4, &n3);
Kai Backman's avatar
Kai Backman committed
2046
			regfree(&n4);
2047
			regfree(&n3);
2048
			p1 = gbranch(optoas(OGT, types[TUINT32]), T);
2049
			ginscall(panicindex, 0);
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
			patch(p1, pc);
		}

		n1 = *reg;
		n1.op = OINDREG;
		n1.type = types[tptr];
		n1.xoffset = Array_array;
		gmove(&n1, reg);
	}

	n2 = *reg;
	n2.op = OINDREG;
2062
	n2.xoffset = v * (*w);
2063
	a->type = D_NONE;
2064
	a->name = D_NONE;
2065
	naddr(&n2, a, 1);
2066 2067 2068 2069 2070 2071 2072 2073 2074
	goto yes;

yes:
	return 1;

no:
	sudoclean();
	return 0;
}