gsubr.c 33.4 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// Derived from Inferno utils/6c/txt.c
// http://code.google.com/p/inferno-os/source/browse/utils/6c/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.

#include "gg.h"

void
clearp(Prog *p)
{
	p->as = AEND;
	p->from.type = D_NONE;
	p->from.index = D_NONE;
	p->to.type = D_NONE;
	p->to.index = D_NONE;
	p->loc = pcloc;
	pcloc++;
}

Russ Cox's avatar
Russ Cox committed
45 46 47 48
/*
 * generate and return proc with p->as = as,
 * linked into program.  pc is next instruction.
 */
49 50 51 52 53 54 55 56 57 58
Prog*
prog(int as)
{
	Prog *p;

	p = pc;
	pc = mal(sizeof(*pc));

	clearp(pc);

Ken Thompson's avatar
Ken Thompson committed
59 60 61 62 63
	if(lineno == 0) {
		if(debug['K'])
			warn("prog: line 0");
	}

64
	p->as = as;
Ken Thompson's avatar
Ken Thompson committed
65
	p->lineno = lineno;
66 67 68 69
	p->link = pc;
	return p;
}

Russ Cox's avatar
Russ Cox committed
70 71 72 73
/*
 * generate a branch.
 * t is ignored.
 */
74 75 76 77 78 79 80 81 82 83 84
Prog*
gbranch(int as, Type *t)
{
	Prog *p;

	p = prog(as);
	p->to.type = D_BRANCH;
	p->to.branch = P;
	return p;
}

Russ Cox's avatar
Russ Cox committed
85 86 87
/*
 * patch previous branch to jump to to.
 */
88 89 90 91 92 93 94 95 96
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
97 98 99
/*
 * start a new Prog list.
 */
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
void
gused(Node *n)
{
	gins(ANOP, n, N);	// used
}

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

	p = gbranch(AJMP, 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;
}

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

	p = gins(AGLOBL, N, N);
	p->from.type = D_EXTERN;
	p->from.index = D_NONE;
	p->from.sym = s;
	p->to.type = D_CONST;
	p->to.index = D_NONE;
	p->to.offset = width;
	if(dupok)
		p->from.scale = DUPOK;
}

int
isfat(Type *t)
{
	if(t != T)
	switch(t->etype) {
	case TSTRUCT:
	case TARRAY:
171
	case TSTRING:
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	case TINTER:	// maybe remove later
	case TDDD:	// 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.
 */
void
afunclit(Addr *a)
{
	if(a->type == D_ADDR && a->index == D_EXTERN) {
		a->type = D_EXTERN;
		a->index = D_NONE;
	}
}

Ken Thompson's avatar
Ken Thompson committed
193 194 195 196 197 198 199 200 201 202 203 204 205
static	int	resvd[] =
{
//	D_DI,	// for movstring
//	D_SI,	// for movstring

	D_AX,	// for divide
	D_CX,	// for shift
	D_DX,	// for divide
	D_SP,	// for stack
	D_R14,	// reserved for m
	D_R15,	// reserved for u
};

206 207 208 209 210 211 212 213 214 215 216
void
ginit(void)
{
	int i;

	for(i=0; i<nelem(reg); i++)
		reg[i] = 1;
	for(i=D_AX; i<=D_R15; i++)
		reg[i] = 0;
	for(i=D_X0; i<=D_X7; i++)
		reg[i] = 0;
Ken Thompson's avatar
Ken Thompson committed
217

Ken Thompson's avatar
Ken Thompson committed
218 219
	for(i=0; i<nelem(resvd); i++)
		reg[resvd[i]]++;
220 221 222 223 224 225 226
}

void
gclean(void)
{
	int i;

Ken Thompson's avatar
Ken Thompson committed
227 228
	for(i=0; i<nelem(resvd); i++)
		reg[resvd[i]]--;
Ken Thompson's avatar
Ken Thompson committed
229

230 231 232 233 234 235 236 237
	for(i=D_AX; i<=D_R15; i++)
		if(reg[i])
			yyerror("reg %R left allocated\n", i);
	for(i=D_X0; i<=D_X7; i++)
		if(reg[i])
			yyerror("reg %R left allocated\n", i);
}

Russ Cox's avatar
Russ Cox committed
238 239 240 241 242
/*
 * allocate register of type t, leave in n.
 * if o != N, o is desired fixed register.
 * caller must regfree(n).
 */
243 244 245
void
regalloc(Node *n, Type *t, Node *o)
{
Ken Thompson's avatar
Ken Thompson committed
246
	int i, et;
247 248 249

	if(t == T)
		fatal("regalloc: t nil");
Ken Thompson's avatar
Ken Thompson committed
250
	et = simtype[t->etype];
Russ Cox's avatar
Russ Cox committed
251

Ken Thompson's avatar
Ken Thompson committed
252
	switch(et) {
253 254 255 256 257 258 259 260 261 262 263 264
	case TINT8:
	case TUINT8:
	case TINT16:
	case TUINT16:
	case TINT32:
	case TUINT32:
	case TINT64:
	case TUINT64:
	case TPTR32:
	case TPTR64:
	case TBOOL:
		if(o != N && o->op == OREGISTER) {
Ken Thompson's avatar
Ken Thompson committed
265
			i = o->val.u.reg;
266 267 268 269 270 271 272 273 274 275 276 277 278 279
			if(i >= D_AX && i <= D_R15)
				goto out;
		}
		for(i=D_AX; i<=D_R15; i++)
			if(reg[i] == 0)
				goto out;

		yyerror("out of fixed registers");
		goto err;

	case TFLOAT32:
	case TFLOAT64:
	case TFLOAT80:
		if(o != N && o->op == OREGISTER) {
Ken Thompson's avatar
Ken Thompson committed
280
			i = o->val.u.reg;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
			if(i >= D_X0 && i <= D_X7)
				goto out;
		}
		for(i=D_X0; i<=D_X7; i++)
			if(reg[i] == 0)
				goto out;
		yyerror("out of floating registers");
		goto err;
	}
	yyerror("regalloc: unknown type %T", t);

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

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

void
regfree(Node *n)
{
	int i;

	if(n->op != OREGISTER && n->op != OINDREG)
		fatal("regfree: not a register");
Ken Thompson's avatar
Ken Thompson committed
308
	i = n->val.u.reg;
309 310 311 312 313 314 315
	if(i < 0 || i >= sizeof(reg))
		fatal("regfree: reg out of range");
	if(reg[i] <= 0)
		fatal("regfree: reg not allocated");
	reg[i]--;
}

Russ Cox's avatar
Russ Cox committed
316 317 318
/*
 * initialize n to be register r of type t.
 */
319 320 321 322 323 324 325 326 327 328
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);
Ken Thompson's avatar
Ken Thompson committed
329
	n->val.u.reg = r;
330 331 332
	n->type = t;
}

Russ Cox's avatar
Russ Cox committed
333 334 335
/*
 * initialize n to be indirect of register r; n is type t.
 */
336 337 338 339 340 341 342 343 344 345 346
void
nodindreg(Node *n, Type *t, int r)
{
	nodreg(n, t, r);
	n->op = OINDREG;
}

Node*
nodarg(Type *t, int fp)
{
	Node *n;
Russ Cox's avatar
Russ Cox committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	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);
		n->xoffset = first->width;
		n->addable = 1;
		goto fp;
	}
364 365 366 367 368 369 370

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

	n = nod(ONAME, N, N);
	n->type = t->type;
	n->sym = t->sym;
Russ Cox's avatar
Russ Cox committed
371 372
	if(t->width == BADWIDTH)
		fatal("nodarg: offset not computed for %T", t);
373 374 375
	n->xoffset = t->width;
	n->addable = 1;

Russ Cox's avatar
Russ Cox committed
376
fp:
377 378 379
	switch(fp) {
	case 0:		// output arg
		n->op = OINDREG;
Ken Thompson's avatar
Ken Thompson committed
380
		n->val.u.reg = D_SP;
381 382 383 384 385 386 387
		break;

	case 1:		// input arg
		n->class = PPARAM;
		break;

	case 2:		// offset output arg
Russ Cox's avatar
Russ Cox committed
388
fatal("shouldnt be used");
389
		n->op = OINDREG;
Ken Thompson's avatar
Ken Thompson committed
390
		n->val.u.reg = D_SP;
391 392 393 394 395 396
		n->xoffset += types[tptr]->width;
		break;
	}
	return n;
}

Russ Cox's avatar
Russ Cox committed
397 398 399 400
/*
 * generate
 *	as $c, reg
 */
401 402 403 404 405 406 407 408 409 410 411 412
void
gconreg(int as, vlong c, int reg)
{
	Node n1, n2;

	nodconst(&n1, types[TINT64], c);
	nodreg(&n2, types[TINT64], reg);
	gins(as, &n1, &n2);
}

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

Russ Cox's avatar
Russ Cox committed
413
/*
Russ Cox's avatar
Russ Cox committed
414
 * Is this node a memory operand?
Russ Cox's avatar
Russ Cox committed
415
 */
Russ Cox's avatar
Russ Cox committed
416 417
int
ismem(Node *n)
418
{
Russ Cox's avatar
Russ Cox committed
419
	switch(n->op) {
420 421
	case OLEN:
	case OCAP:
Russ Cox's avatar
Russ Cox committed
422 423 424 425
	case OINDREG:
	case ONAME:
	case OPARAM:
		return 1;
426
	}
Russ Cox's avatar
Russ Cox committed
427 428 429
	return 0;
}

430
/*
Russ Cox's avatar
Russ Cox committed
431
 * set up nodes representing 2^63
432
 */
Russ Cox's avatar
Russ Cox committed
433 434
Node bigi;
Node bigf;
435

Russ Cox's avatar
Russ Cox committed
436 437 438 439
void
bignodes(void)
{
	static int did;
440

Russ Cox's avatar
Russ Cox committed
441
	if(did)
442
		return;
Russ Cox's avatar
Russ Cox committed
443 444 445 446 447 448 449 450 451 452 453
	did = 1;

	nodconst(&bigi, types[TUINT64], 1);
	mpshiftfix(bigi.val.u.xval, 63);

	bigf = bigi;
	bigf.type = types[TFLOAT64];
	bigf.val.ctype = CTFLT;
	bigf.val.u.fval = mal(sizeof *bigf.val.u.fval);
	mpmovefixflt(bigf.val.u.fval, bigi.val.u.xval);
}
454 455

/*
Russ Cox's avatar
Russ Cox committed
456 457 458
 * generate move:
 *	t = f
 * hard part is conversions.
459
 */
460
// TODO: lost special constants for floating point.  XORPD for 0.0?
Russ Cox's avatar
Russ Cox committed
461 462 463 464 465 466 467
void
gmove(Node *f, Node *t)
{
	int a, ft, tt;
	Type *cvt;
	Node r1, r2, r3, r4, zero, one, con;
	Prog *p1, *p2;
Russ Cox's avatar
Russ Cox committed
468

Russ Cox's avatar
Russ Cox committed
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
	if(debug['M'])
		print("gmove %N -> %N\n", f, t);

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

	// cannot have two memory operands
	if(ismem(f) && ismem(t))
		goto hard;

	// convert constant to desired type
	if(f->op == OLITERAL) {
		convconst(&con, t->type, &f->val);
		f = &con;
		ft = tt;	// so big switch will choose a simple mov

		// some constants can't move directly to memory.
		if(ismem(t)) {
			// float constants come from memory.
			if(isfloat[tt])
				goto hard;
			// 64-bit immediates are really 32-bit sign-extended
			// unless moving into a register.
			if(isint[tt]) {
				if(mpcmpfixfix(con.val.u.xval, minintval[TINT32]) < 0)
					goto hard;
				if(mpcmpfixfix(con.val.u.xval, maxintval[TINT32]) > 0)
					goto hard;
Russ Cox's avatar
Russ Cox committed
498 499
			}
		}
500 501
	}

Russ Cox's avatar
Russ Cox committed
502 503 504 505 506 507 508
	// 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.

509 510
	switch(CASE(ft, tt)) {
	default:
Russ Cox's avatar
Russ Cox committed
511
		fatal("gmove %lT -> %lT", f->type, t->type);
Russ Cox's avatar
Russ Cox committed
512 513 514 515 516 517 518 519 520 521 522 523

	/*
	 * integer copy and truncate
	 */
	case CASE(TINT8, TINT8):	// same size
	case CASE(TINT8, TUINT8):
	case CASE(TUINT8, TINT8):
	case CASE(TUINT8, TUINT8):
	case CASE(TINT16, TINT8):	// truncate
	case CASE(TUINT16, TINT8):
	case CASE(TINT32, TINT8):
	case CASE(TUINT32, TINT8):
524 525
	case CASE(TINT64, TINT8):
	case CASE(TUINT64, TINT8):
Russ Cox's avatar
Russ Cox committed
526 527 528 529 530 531 532
	case CASE(TINT16, TUINT8):
	case CASE(TUINT16, TUINT8):
	case CASE(TINT32, TUINT8):
	case CASE(TUINT32, TUINT8):
	case CASE(TINT64, TUINT8):
	case CASE(TUINT64, TUINT8):
		a = AMOVB;
Ken Thompson's avatar
Ken Thompson committed
533 534
		break;

Russ Cox's avatar
Russ Cox committed
535 536 537 538 539 540 541 542 543 544 545 546 547
	case CASE(TINT16, TINT16):	// same size
	case CASE(TINT16, TUINT16):
	case CASE(TUINT16, TINT16):
	case CASE(TUINT16, TUINT16):
	case CASE(TINT32, TINT16):	// truncate
	case CASE(TUINT32, TINT16):
	case CASE(TINT64, TINT16):
	case CASE(TUINT64, TINT16):
	case CASE(TINT32, TUINT16):
	case CASE(TUINT32, TUINT16):
	case CASE(TINT64, TUINT16):
	case CASE(TUINT64, TUINT16):
		a = AMOVW;
548 549
		break;

Russ Cox's avatar
Russ Cox committed
550 551 552 553 554 555 556 557 558
	case CASE(TINT32, TINT32):	// same size
	case CASE(TINT32, TUINT32):
	case CASE(TUINT32, TINT32):
	case CASE(TUINT32, TUINT32):
	case CASE(TINT64, TINT32):	// truncate
	case CASE(TUINT64, TINT32):
	case CASE(TINT64, TUINT32):
	case CASE(TUINT64, TUINT32):
		a = AMOVL;
559 560
		break;

Russ Cox's avatar
Russ Cox committed
561
	case CASE(TINT64, TINT64):	// same size
562
	case CASE(TINT64, TUINT64):
Russ Cox's avatar
Russ Cox committed
563
	case CASE(TUINT64, TINT64):
564 565 566 567
	case CASE(TUINT64, TUINT64):
		a = AMOVQ;
		break;

Russ Cox's avatar
Russ Cox committed
568 569 570 571
	/*
	 * integer up-conversions
	 */
	case CASE(TINT8, TINT16):	// sign extend int8
572
	case CASE(TINT8, TUINT16):
Russ Cox's avatar
Russ Cox committed
573 574
		a = AMOVBWSX;
		goto rdst;
575 576 577
	case CASE(TINT8, TINT32):
	case CASE(TINT8, TUINT32):
		a = AMOVBLSX;
Russ Cox's avatar
Russ Cox committed
578
		goto rdst;
579 580 581
	case CASE(TINT8, TINT64):
	case CASE(TINT8, TUINT64):
		a = AMOVBQSX;
Russ Cox's avatar
Russ Cox committed
582
		goto rdst;
583

Russ Cox's avatar
Russ Cox committed
584
	case CASE(TUINT8, TINT16):	// zero extend uint8
585
	case CASE(TUINT8, TUINT16):
Russ Cox's avatar
Russ Cox committed
586 587
		a = AMOVBWZX;
		goto rdst;
588 589 590
	case CASE(TUINT8, TINT32):
	case CASE(TUINT8, TUINT32):
		a = AMOVBLZX;
Russ Cox's avatar
Russ Cox committed
591
		goto rdst;
592 593 594
	case CASE(TUINT8, TINT64):
	case CASE(TUINT8, TUINT64):
		a = AMOVBQZX;
Russ Cox's avatar
Russ Cox committed
595
		goto rdst;
596

Russ Cox's avatar
Russ Cox committed
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
	case CASE(TINT16, TINT32):	// sign extend int16
	case CASE(TINT16, TUINT32):
		a = AMOVWLSX;
		goto rdst;
	case CASE(TINT16, TINT64):
	case CASE(TINT16, TUINT64):
		a = AMOVWQSX;
		goto rdst;

	case CASE(TUINT16, TINT32):	// zero extend uint16
	case CASE(TUINT16, TUINT32):
		a = AMOVWLZX;
		goto rdst;
	case CASE(TUINT16, TINT64):
	case CASE(TUINT16, TUINT64):
		a = AMOVWQZX;
		goto rdst;

	case CASE(TINT32, TINT64):	// sign extend int32
	case CASE(TINT32, TUINT64):
		a = AMOVLQSX;
		goto rdst;

	case CASE(TUINT32, TINT64):	// zero extend uint32
	case CASE(TUINT32, TUINT64):
		// AMOVL into a register zeros the top of the register,
		// so this is not always necessary, but if we rely on AMOVL
		// the optimizer is almost certain to screw with us.
		a = AMOVLQZX;
		goto rdst;

	/*
	* float to integer
	*/
631
	case CASE(TFLOAT32, TINT32):
Russ Cox's avatar
Russ Cox committed
632 633 634 635 636 637
		a = ACVTTSS2SL;
		goto rdst;

	case CASE(TFLOAT64, TINT32):
		a = ACVTTSD2SL;
		goto rdst;
638 639

	case CASE(TFLOAT32, TINT64):
Russ Cox's avatar
Russ Cox committed
640 641
		a = ACVTTSS2SQ;
		goto rdst;
642

Russ Cox's avatar
Russ Cox committed
643 644 645
	case CASE(TFLOAT64, TINT64):
		a = ACVTTSD2SQ;
		goto rdst;
646

Russ Cox's avatar
Russ Cox committed
647 648 649 650 651 652
	case CASE(TFLOAT32, TINT16):
	case CASE(TFLOAT32, TINT8):
	case CASE(TFLOAT32, TUINT16):
	case CASE(TFLOAT32, TUINT8):
	case CASE(TFLOAT64, TINT16):
	case CASE(TFLOAT64, TINT8):
653
	case CASE(TFLOAT64, TUINT16):
Russ Cox's avatar
Russ Cox committed
654 655 656 657 658 659
	case CASE(TFLOAT64, TUINT8):
		// convert via int32.
		cvt = types[TINT32];
		goto hard;

	case CASE(TFLOAT32, TUINT32):
660
	case CASE(TFLOAT64, TUINT32):
Russ Cox's avatar
Russ Cox committed
661 662 663
		// convert via int64.
		cvt = types[TINT64];
		goto hard;
664

Russ Cox's avatar
Russ Cox committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
	case CASE(TFLOAT32, TUINT64):
	case CASE(TFLOAT64, TUINT64):
		// algorithm is:
		//	if small enough, use native float64 -> int64 conversion.
		//	otherwise, subtract 2^63, convert, and add it back.
		a = ACVTSS2SQ;
		if(ft == TFLOAT64)
			a = ACVTSD2SQ;
		bignodes();
		regalloc(&r1, types[ft], N);
		regalloc(&r2, types[tt], t);
		regalloc(&r3, types[ft], N);
		regalloc(&r4, types[tt], N);
		gins(optoas(OAS, f->type), f, &r1);
		gins(optoas(OCMP, f->type), &bigf, &r1);
		p1 = gbranch(optoas(OLE, f->type), T);
		gins(a, &r1, &r2);
Russ Cox's avatar
Russ Cox committed
682
		p2 = gbranch(AJMP, T);
683
		patch(p1, pc);
Russ Cox's avatar
Russ Cox committed
684 685 686 687 688
		gins(optoas(OAS, f->type), &bigf, &r3);
		gins(optoas(OSUB, f->type), &r3, &r1);
		gins(a, &r1, &r2);
		gins(AMOVQ, &bigi, &r4);
		gins(AXORQ, &r4, &r2);
689
		patch(p2, pc);
Russ Cox's avatar
Russ Cox committed
690 691 692 693 694
		gmove(&r2, t);
		regfree(&r4);
		regfree(&r3);
		regfree(&r2);
		regfree(&r1);
695 696
		return;

Russ Cox's avatar
Russ Cox committed
697 698 699 700 701 702 703 704 705 706 707
	/*
	 * integer to float
	 */
	case CASE(TINT32, TFLOAT32):
		a = ACVTSL2SS;
		goto rdst;


	case CASE(TINT32, TFLOAT64):
		a = ACVTSL2SD;
		goto rdst;
708 709

	case CASE(TINT64, TFLOAT32):
Russ Cox's avatar
Russ Cox committed
710 711
		a = ACVTSQ2SS;
		goto rdst;
712 713

	case CASE(TINT64, TFLOAT64):
Russ Cox's avatar
Russ Cox committed
714 715
		a = ACVTSQ2SD;
		goto rdst;
716 717 718

	case CASE(TINT16, TFLOAT32):
	case CASE(TINT16, TFLOAT64):
Russ Cox's avatar
Russ Cox committed
719 720 721
	case CASE(TINT8, TFLOAT32):
	case CASE(TINT8, TFLOAT64):
	case CASE(TUINT16, TFLOAT32):
722
	case CASE(TUINT16, TFLOAT64):
Russ Cox's avatar
Russ Cox committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	case CASE(TUINT8, TFLOAT32):
	case CASE(TUINT8, TFLOAT64):
		// convert via int32
		cvt = types[TINT32];
		goto hard;

	case CASE(TUINT32, TFLOAT32):
	case CASE(TUINT32, TFLOAT64):
		// convert via int64.
		cvt = types[TINT64];
		goto hard;

	case CASE(TUINT64, TFLOAT32):
	case CASE(TUINT64, TFLOAT64):
		// algorithm is:
		//	if small enough, use native int64 -> uint64 conversion.
		//	otherwise, halve (rounding to odd?), convert, and double.
		a = ACVTSQ2SS;
		if(tt == TFLOAT64)
			a = ACVTSQ2SD;
		nodconst(&zero, types[TUINT64], 0);
		nodconst(&one, types[TUINT64], 1);
		regalloc(&r1, f->type, f);
		regalloc(&r2, t->type, t);
		regalloc(&r3, f->type, N);
		regalloc(&r4, f->type, N);
		gmove(f, &r1);
		gins(ACMPQ, &r1, &zero);
		p1 = gbranch(AJLT, T);
		gins(a, &r1, &r2);
		p2 = gbranch(AJMP, T);
		patch(p1, pc);
		gmove(&r1, &r3);
		gins(ASHRQ, &one, &r3);
		gmove(&r1, &r4);
		gins(AANDL, &one, &r4);
		gins(AORQ, &r4, &r3);
		gins(a, &r3, &r2);
		gins(optoas(OADD, t->type), &r2, &r2);
		patch(p2, pc);
		gmove(&r2, t);
		regfree(&r4);
		regfree(&r3);
		regfree(&r2);
		regfree(&r1);
768 769
		return;

Russ Cox's avatar
Russ Cox committed
770 771 772
	/*
	 * float to float
	 */
773 774 775
	case CASE(TFLOAT32, TFLOAT32):
		a = AMOVSS;
		break;
Russ Cox's avatar
Russ Cox committed
776

777 778 779
	case CASE(TFLOAT64, TFLOAT64):
		a = AMOVSD;
		break;
Russ Cox's avatar
Russ Cox committed
780 781 782 783 784 785 786 787

	case CASE(TFLOAT32, TFLOAT64):
		a = ACVTSS2SD;
		goto rdst;

	case CASE(TFLOAT64, TFLOAT32):
		a = ACVTSD2SS;
		goto rdst;
788
	}
Russ Cox's avatar
Russ Cox committed
789

790
	gins(a, f, t);
Russ Cox's avatar
Russ Cox committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	return;

rdst:
	// requires register destination
	regalloc(&r1, t->type, t);
	gins(a, f, &r1);
	gmove(&r1, t);
	regfree(&r1);
	return;

hard:
	// requires register intermediate
	regalloc(&r1, cvt, t);
	gmove(f, &r1);
	gmove(&r1, t);
	regfree(&r1);
	return;
808 809 810 811 812 813 814 815 816 817 818
}

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

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

	switch(f->op) {
	case OREGISTER:
Ken Thompson's avatar
Ken Thompson committed
819
		if(f->val.u.reg != t->val.u.reg)
820 821 822 823 824 825
			break;
		return 1;
	}
	return 0;
}

Russ Cox's avatar
Russ Cox committed
826 827 828 829
/*
 * generate one instruction:
 *	as f, t
 */
830 831 832 833
Prog*
gins(int as, Node *f, Node *t)
{
//	Node nod;
834
//	int32 v;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	Prog *p;

//	if(f != N && f->op == OINDEX) {
//		regalloc(&nod, &regnode, Z);
//		v = constnode.vconst;
//		cgen(f->right, &nod);
//		constnode.vconst = v;
//		idx.reg = nod.reg;
//		regfree(&nod);
//	}
//	if(t != N && t->op == OINDEX) {
//		regalloc(&nod, &regnode, Z);
//		v = constnode.vconst;
//		cgen(t->right, &nod);
//		constnode.vconst = v;
//		idx.reg = nod.reg;
//		regfree(&nod);
//	}

Russ Cox's avatar
Russ Cox committed
854 855 856 857 858 859 860 861 862 863 864
	switch(as) {
	case AMOVB:
	case AMOVW:
	case AMOVL:
	case AMOVQ:
	case AMOVSS:
	case AMOVSD:
		if(f != N && t != N && samaddr(f, t))
			return nil;
	}

865 866 867 868 869 870 871 872 873 874
	p = prog(as);
	if(f != N)
		naddr(f, &p->from);
	if(t != N)
		naddr(t, &p->to);
	if(debug['g'])
		print("%P\n", p);
	return p;
}

Russ Cox's avatar
Russ Cox committed
875 876 877 878
/*
 * generate code to compute n;
 * make a refer to result.
 */
879 880 881
void
naddr(Node *n, Addr *a)
{
Russ Cox's avatar
Russ Cox committed
882 883
	a->scale = 0;
	a->index = D_NONE;
884 885 886 887 888 889 890 891 892 893
	a->type = D_NONE;
	if(n == N)
		return;

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

	case OREGISTER:
Ken Thompson's avatar
Ken Thompson committed
894
		a->type = n->val.u.reg;
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
		a->sym = S;
		break;

//	case OINDEX:
//	case OIND:
//		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;

	case OINDREG:
Ken Thompson's avatar
Ken Thompson committed
919
		a->type = n->val.u.reg+D_INDIR;
920 921 922 923
		a->sym = n->sym;
		a->offset = n->xoffset;
		break;

924 925 926
	case OPARAM:
		// n->left is PHEAP ONAME for stack parameter.
		// compute address of actual parameter on stack.
927 928
		a->etype = simtype[n->left->type->etype];
		a->width = n->left->type->width;
929 930 931 932 933
		a->offset = n->xoffset;
		a->sym = n->left->sym;
		a->type = D_PARAM;
		break;

934
	case ONAME:
Ken Thompson's avatar
Ken Thompson committed
935
		a->etype = 0;
936 937
		a->width = 0;
		if(n->type != T) {
Russ Cox's avatar
Russ Cox committed
938
			a->etype = simtype[n->type->etype];
939 940
			a->width = n->type->width;
		}
941 942
		a->offset = n->xoffset;
		a->sym = n->sym;
943
		if(a->sym == S)
944
			a->sym = lookup(".noname");
Ken Thompson's avatar
Ken Thompson committed
945 946 947
		if(n->method) {
			if(n->type != T)
			if(n->type->sym != S)
Russ Cox's avatar
6g:  
Russ Cox committed
948 949
			if(n->type->sym->package != nil)
				a->sym = pkglookup(a->sym->name, n->type->sym->package);
Ken Thompson's avatar
Ken Thompson committed
950
		}
951 952 953 954 955 956 957 958 959 960 961

		switch(n->class) {
		default:
			fatal("naddr: ONAME class %S %d\n", n->sym, n->class);
		case PEXTERN:
			a->type = D_EXTERN;
			break;
		case PAUTO:
			a->type = D_AUTO;
			break;
		case PPARAM:
962
		case PPARAMOUT:
963 964
			a->type = D_PARAM;
			break;
Russ Cox's avatar
Russ Cox committed
965 966 967 968
		case PFUNC:
			a->index = D_EXTERN;
			a->type = D_ADDR;
			break;
969 970 971 972
		}
		break;

	case OLITERAL:
Ken Thompson's avatar
Ken Thompson committed
973 974 975 976 977
		switch(n->val.ctype) {
		default:
			fatal("naddr: const %lT", n->type);
			break;
		case CTFLT:
978
			a->type = D_FCONST;
Ken Thompson's avatar
Ken Thompson committed
979
			a->dval = mpgetflt(n->val.u.fval);
980
			break;
Ken Thompson's avatar
Ken Thompson committed
981 982 983 984 985 986
		case CTINT:
			a->sym = S;
			a->type = D_CONST;
			a->offset = mpgetfix(n->val.u.xval);
			break;
		case CTSTR:
987
			datagostring(n->val.u.sval, a);
988
			break;
Ken Thompson's avatar
Ken Thompson committed
989 990 991 992 993 994
		case CTBOOL:
			a->sym = S;
			a->type = D_CONST;
			a->offset = n->val.u.bval;
			break;
		case CTNIL:
995 996
			a->sym = S;
			a->type = D_CONST;
Ken Thompson's avatar
Ken Thompson committed
997
			a->offset = 0;
998 999 1000 1001
			break;
		}
		break;

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	case OADDR:
		naddr(n->left, a);
		if(a->type >= D_INDIR) {
			a->type -= D_INDIR;
			break;
		}
		if(a->type == D_EXTERN || a->type == D_STATIC ||
		   a->type == D_AUTO || a->type == D_PARAM)
			if(a->index == D_NONE) {
				a->index = a->type;
				a->type = D_ADDR;
				break;
			}
		fatal("naddr: OADDR\n");
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	case OLEN:
		// len of string or slice
		naddr(n->left, a);
		a->offset += Array_nel;
		break;

	case OCAP:
		// cap of string or slice
		naddr(n->left, a);
		a->offset += Array_cap;
		break;

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
//	case OADD:
//		if(n->right->op == OLITERAL) {
//			v = n->right->vconst;
//			naddr(n->left, a);
//		} else
//		if(n->left->op == OLITERAL) {
//			v = n->left->vconst;
//			naddr(n->right, a);
//		} else
//			goto bad;
//		a->offset += v;
//		break;

	}
}

Russ Cox's avatar
Russ Cox committed
1045 1046 1047
/*
 * return Axxx for Oxxx on type t.
 */
1048 1049 1050 1051 1052 1053 1054 1055 1056
int
optoas(int op, Type *t)
{
	int a;

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

	a = AGOK;
Ken Thompson's avatar
Ken Thompson committed
1057
	switch(CASE(op, simtype[t->etype])) {
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	default:
		fatal("optoas: no entry %O-%T", op, t);
		break;

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

	case CASE(OADDR, TPTR64):
		a = ALEAQ;
		break;

	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):
Ken Thompson's avatar
Ken Thompson committed
1081 1082
	case CASE(OEQ, TFLOAT32):
	case CASE(OEQ, TFLOAT64):
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
		a = AJEQ;
		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):
Ken Thompson's avatar
Ken Thompson committed
1097 1098
	case CASE(ONE, TFLOAT32):
	case CASE(ONE, TFLOAT64):
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
		a = AJNE;
		break;

	case CASE(OLT, TINT8):
	case CASE(OLT, TINT16):
	case CASE(OLT, TINT32):
	case CASE(OLT, TINT64):
		a = AJLT;
		break;

	case CASE(OLT, TUINT8):
	case CASE(OLT, TUINT16):
	case CASE(OLT, TUINT32):
	case CASE(OLT, TUINT64):
Ken Thompson's avatar
Ken Thompson committed
1113 1114
	case CASE(OGT, TFLOAT32):
	case CASE(OGT, TFLOAT64):
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		a = AJCS;
		break;

	case CASE(OLE, TINT8):
	case CASE(OLE, TINT16):
	case CASE(OLE, TINT32):
	case CASE(OLE, TINT64):
		a = AJLE;
		break;

	case CASE(OLE, TUINT8):
	case CASE(OLE, TUINT16):
	case CASE(OLE, TUINT32):
	case CASE(OLE, TUINT64):
Ken Thompson's avatar
Ken Thompson committed
1129 1130
	case CASE(OGE, TFLOAT32):
	case CASE(OGE, TFLOAT64):
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
		a = AJLS;
		break;

	case CASE(OGT, TINT8):
	case CASE(OGT, TINT16):
	case CASE(OGT, TINT32):
	case CASE(OGT, TINT64):
		a = AJGT;
		break;

	case CASE(OGT, TUINT8):
	case CASE(OGT, TUINT16):
	case CASE(OGT, TUINT32):
	case CASE(OGT, TUINT64):
Ken Thompson's avatar
Ken Thompson committed
1145 1146
	case CASE(OLT, TFLOAT32):
	case CASE(OLT, TFLOAT64):
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
		a = AJHI;
		break;

	case CASE(OGE, TINT8):
	case CASE(OGE, TINT16):
	case CASE(OGE, TINT32):
	case CASE(OGE, TINT64):
		a = AJGE;
		break;

	case CASE(OGE, TUINT8):
	case CASE(OGE, TUINT16):
	case CASE(OGE, TUINT32):
	case CASE(OGE, TUINT64):
Ken Thompson's avatar
Ken Thompson committed
1161 1162
	case CASE(OLE, TFLOAT32):
	case CASE(OLE, TFLOAT64):
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
		a = AJCC;
		break;

	case CASE(OCMP, TBOOL):
	case CASE(OCMP, TINT8):
	case CASE(OCMP, TUINT8):
		a = ACMPB;
		break;

	case CASE(OCMP, TINT16):
	case CASE(OCMP, TUINT16):
		a = ACMPW;
		break;

	case CASE(OCMP, TINT32):
	case CASE(OCMP, TUINT32):
	case CASE(OCMP, TPTR32):
		a = ACMPL;
		break;

	case CASE(OCMP, TINT64):
	case CASE(OCMP, TUINT64):
	case CASE(OCMP, TPTR64):
		a = ACMPQ;
		break;

	case CASE(OCMP, TFLOAT32):
		a = AUCOMISS;
		break;

	case CASE(OCMP, TFLOAT64):
		a = AUCOMISD;
		break;

Ken Thompson's avatar
Ken Thompson committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
	case CASE(OAS, TBOOL):
	case CASE(OAS, TINT8):
	case CASE(OAS, TUINT8):
		a = AMOVB;
		break;

	case CASE(OAS, TINT16):
	case CASE(OAS, TUINT16):
		a = AMOVW;
		break;

	case CASE(OAS, TINT32):
	case CASE(OAS, TUINT32):
	case CASE(OAS, TPTR32):
		a = AMOVL;
		break;

	case CASE(OAS, TINT64):
	case CASE(OAS, TUINT64):
	case CASE(OAS, TPTR64):
		a = AMOVQ;
		break;

	case CASE(OAS, TFLOAT32):
		a = AMOVSS;
		break;

	case CASE(OAS, TFLOAT64):
		a = AMOVSD;
		break;

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	case CASE(OADD, TINT8):
	case CASE(OADD, TUINT8):
		a = AADDB;
		break;

	case CASE(OADD, TINT16):
	case CASE(OADD, TUINT16):
		a = AADDW;
		break;

	case CASE(OADD, TINT32):
	case CASE(OADD, TUINT32):
	case CASE(OADD, TPTR32):
		a = AADDL;
		break;

	case CASE(OADD, TINT64):
	case CASE(OADD, TUINT64):
	case CASE(OADD, TPTR64):
		a = AADDQ;
		break;

	case CASE(OADD, TFLOAT32):
		a = AADDSS;
		break;

	case CASE(OADD, TFLOAT64):
		a = AADDSD;
		break;

	case CASE(OSUB, TINT8):
	case CASE(OSUB, TUINT8):
		a = ASUBB;
		break;

	case CASE(OSUB, TINT16):
	case CASE(OSUB, TUINT16):
		a = ASUBW;
		break;

	case CASE(OSUB, TINT32):
	case CASE(OSUB, TUINT32):
	case CASE(OSUB, TPTR32):
		a = ASUBL;
		break;

	case CASE(OSUB, TINT64):
	case CASE(OSUB, TUINT64):
	case CASE(OSUB, TPTR64):
		a = ASUBQ;
		break;

	case CASE(OSUB, TFLOAT32):
		a = ASUBSS;
		break;

	case CASE(OSUB, TFLOAT64):
		a = ASUBSD;
		break;

Ken Thompson's avatar
Ken Thompson committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	case CASE(OINC, TINT8):
	case CASE(OINC, TUINT8):
		a = AINCB;
		break;

	case CASE(OINC, TINT16):
	case CASE(OINC, TUINT16):
		a = AINCW;
		break;

	case CASE(OINC, TINT32):
	case CASE(OINC, TUINT32):
	case CASE(OINC, TPTR32):
		a = AINCL;
		break;

	case CASE(OINC, TINT64):
	case CASE(OINC, TUINT64):
	case CASE(OINC, TPTR64):
		a = AINCQ;
		break;

	case CASE(ODEC, TINT8):
	case CASE(ODEC, TUINT8):
		a = ADECB;
		break;

	case CASE(ODEC, TINT16):
	case CASE(ODEC, TUINT16):
		a = ADECW;
		break;

	case CASE(ODEC, TINT32):
	case CASE(ODEC, TUINT32):
	case CASE(ODEC, TPTR32):
		a = ADECL;
		break;

	case CASE(ODEC, TINT64):
	case CASE(ODEC, TUINT64):
	case CASE(ODEC, TPTR64):
		a = ADECQ;
		break;

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	case CASE(OMINUS, TINT8):
	case CASE(OMINUS, TUINT8):
		a = ANEGB;
		break;

	case CASE(OMINUS, TINT16):
	case CASE(OMINUS, TUINT16):
		a = ANEGW;
		break;

	case CASE(OMINUS, TINT32):
	case CASE(OMINUS, TUINT32):
	case CASE(OMINUS, TPTR32):
		a = ANEGL;
		break;

	case CASE(OMINUS, TINT64):
	case CASE(OMINUS, TUINT64):
	case CASE(OMINUS, TPTR64):
		a = ANEGQ;
		break;

Ken Thompson's avatar
Ken Thompson committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	case CASE(OAND, TINT8):
	case CASE(OAND, TUINT8):
		a = AANDB;
		break;

	case CASE(OAND, TINT16):
	case CASE(OAND, TUINT16):
		a = AANDW;
		break;

	case CASE(OAND, TINT32):
	case CASE(OAND, TUINT32):
	case CASE(OAND, TPTR32):
		a = AANDL;
		break;

	case CASE(OAND, TINT64):
	case CASE(OAND, TUINT64):
	case CASE(OAND, TPTR64):
		a = AANDQ;
		break;

	case CASE(OOR, TINT8):
	case CASE(OOR, TUINT8):
		a = AORB;
		break;

	case CASE(OOR, TINT16):
	case CASE(OOR, TUINT16):
		a = AORW;
		break;

	case CASE(OOR, TINT32):
	case CASE(OOR, TUINT32):
	case CASE(OOR, TPTR32):
		a = AORL;
		break;

	case CASE(OOR, TINT64):
	case CASE(OOR, TUINT64):
	case CASE(OOR, TPTR64):
		a = AORQ;
		break;

	case CASE(OXOR, TINT8):
	case CASE(OXOR, TUINT8):
		a = AXORB;
		break;

	case CASE(OXOR, TINT16):
	case CASE(OXOR, TUINT16):
		a = AXORW;
		break;

	case CASE(OXOR, TINT32):
	case CASE(OXOR, TUINT32):
	case CASE(OXOR, TPTR32):
		a = AXORL;
		break;

	case CASE(OXOR, TINT64):
	case CASE(OXOR, TUINT64):
	case CASE(OXOR, TPTR64):
		a = AXORQ;
		break;

1420 1421 1422
	case CASE(OLSH, TINT8):
	case CASE(OLSH, TUINT8):
		a = ASHLB;
1423 1424
		break;

1425 1426 1427
	case CASE(OLSH, TINT16):
	case CASE(OLSH, TUINT16):
		a = ASHLW;
1428 1429
		break;

1430 1431 1432 1433
	case CASE(OLSH, TINT32):
	case CASE(OLSH, TUINT32):
	case CASE(OLSH, TPTR32):
		a = ASHLL;
1434 1435
		break;

1436 1437 1438 1439
	case CASE(OLSH, TINT64):
	case CASE(OLSH, TUINT64):
	case CASE(OLSH, TPTR64):
		a = ASHLQ;
1440 1441
		break;

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
	case CASE(ORSH, TUINT8):
		a = ASHRB;
		break;

	case CASE(ORSH, TUINT16):
		a = ASHRW;
		break;

	case CASE(ORSH, TUINT32):
	case CASE(ORSH, TPTR32):
		a = ASHRL;
		break;

	case CASE(ORSH, TUINT64):
	case CASE(ORSH, TPTR64):
		a = ASHRQ;
		break;

	case CASE(ORSH, TINT8):
		a = ASARB;
		break;

	case CASE(ORSH, TINT16):
		a = ASARW;
		break;

	case CASE(ORSH, TINT32):
		a = ASARL;
		break;

	case CASE(ORSH, TINT64):
		a = ASARQ;
1474 1475
		break;

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
	case CASE(ORRC, TINT8):
	case CASE(ORRC, TUINT8):
		a = ARCRB;
		break;

	case CASE(ORRC, TINT16):
	case CASE(ORRC, TUINT16):
		a = ARCRW;
		break;

	case CASE(ORRC, TINT32):
	case CASE(ORRC, TUINT32):
		a = ARCRL;
		break;

	case CASE(ORRC, TINT64):
	case CASE(ORRC, TUINT64):
		a = ARCRQ;
		break;

Ken Thompson's avatar
Ken Thompson committed
1496
	case CASE(OHMUL, TINT8):
1497 1498 1499 1500 1501
	case CASE(OMUL, TINT8):
	case CASE(OMUL, TUINT8):
		a = AIMULB;
		break;

Ken Thompson's avatar
Ken Thompson committed
1502
	case CASE(OHMUL, TINT16):
1503 1504 1505 1506 1507
	case CASE(OMUL, TINT16):
	case CASE(OMUL, TUINT16):
		a = AIMULW;
		break;

Ken Thompson's avatar
Ken Thompson committed
1508
	case CASE(OHMUL, TINT32):
1509
	case CASE(OMUL, TINT32):
1510 1511
	case CASE(OMUL, TUINT32):
	case CASE(OMUL, TPTR32):
1512
		a = AIMULL;
1513 1514
		break;

Ken Thompson's avatar
Ken Thompson committed
1515
	case CASE(OHMUL, TINT64):
1516 1517 1518
	case CASE(OMUL, TINT64):
	case CASE(OMUL, TUINT64):
	case CASE(OMUL, TPTR64):
1519
		a = AIMULQ;
1520 1521
		break;

Ken Thompson's avatar
Ken Thompson committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	case CASE(OHMUL, TUINT8):
		a = AMULB;
		break;

	case CASE(OHMUL, TUINT16):
		a = AMULW;
		break;

	case CASE(OHMUL, TUINT32):
	case CASE(OHMUL, TPTR32):
		a = AMULL;
		break;

	case CASE(OHMUL, TUINT64):
	case CASE(OHMUL, TPTR64):
		a = AMULQ;
		break;

1540 1541 1542 1543 1544 1545 1546 1547 1548
	case CASE(OMUL, TFLOAT32):
		a = AMULSS;
		break;

	case CASE(OMUL, TFLOAT64):
		a = AMULSD;
		break;

	case CASE(ODIV, TINT8):
Ken Thompson's avatar
Ken Thompson committed
1549
	case CASE(OMOD, TINT8):
1550 1551 1552 1553
		a = AIDIVB;
		break;

	case CASE(ODIV, TUINT8):
Ken Thompson's avatar
Ken Thompson committed
1554
	case CASE(OMOD, TUINT8):
1555 1556 1557 1558
		a = ADIVB;
		break;

	case CASE(ODIV, TINT16):
Ken Thompson's avatar
Ken Thompson committed
1559
	case CASE(OMOD, TINT16):
1560 1561 1562 1563
		a = AIDIVW;
		break;

	case CASE(ODIV, TUINT16):
Ken Thompson's avatar
Ken Thompson committed
1564
	case CASE(OMOD, TUINT16):
1565 1566 1567 1568
		a = ADIVW;
		break;

	case CASE(ODIV, TINT32):
Ken Thompson's avatar
Ken Thompson committed
1569
	case CASE(OMOD, TINT32):
1570 1571 1572 1573 1574
		a = AIDIVL;
		break;

	case CASE(ODIV, TUINT32):
	case CASE(ODIV, TPTR32):
Ken Thompson's avatar
Ken Thompson committed
1575 1576
	case CASE(OMOD, TUINT32):
	case CASE(OMOD, TPTR32):
1577 1578 1579 1580
		a = ADIVL;
		break;

	case CASE(ODIV, TINT64):
Ken Thompson's avatar
Ken Thompson committed
1581
	case CASE(OMOD, TINT64):
1582 1583 1584 1585 1586
		a = AIDIVQ;
		break;

	case CASE(ODIV, TUINT64):
	case CASE(ODIV, TPTR64):
Ken Thompson's avatar
Ken Thompson committed
1587 1588
	case CASE(OMOD, TUINT64):
	case CASE(OMOD, TPTR64):
1589 1590 1591
		a = ADIVQ;
		break;

Russ Cox's avatar
Russ Cox committed
1592
	case CASE(OEXTEND, TINT16):
1593 1594 1595
		a = ACWD;
		break;

Russ Cox's avatar
Russ Cox committed
1596
	case CASE(OEXTEND, TINT32):
1597 1598 1599
		a = ACDQ;
		break;

Russ Cox's avatar
Russ Cox committed
1600
	case CASE(OEXTEND, TINT64):
1601 1602 1603
		a = ACQO;
		break;

1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
	case CASE(ODIV, TFLOAT32):
		a = ADIVSS;
		break;

	case CASE(ODIV, TFLOAT64):
		a = ADIVSD;
		break;

	}
	return a;
}

Ken Thompson's avatar
Ken Thompson committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
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;
}

Russ Cox's avatar
Russ Cox committed
1635 1636 1637
/*
 * generate code to compute address of n,
 * a reference to a (perhaps nested) field inside
Russ Cox's avatar
Russ Cox committed
1638
 * an array or struct.
Russ Cox's avatar
Russ Cox committed
1639 1640 1641 1642 1643 1644 1645
 * 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.
 */
Ken Thompson's avatar
Ken Thompson committed
1646
int
Ken Thompson's avatar
Ken Thompson committed
1647
sudoaddable(int as, Node *n, Addr *a)
Ken Thompson's avatar
Ken Thompson committed
1648
{
Ken Thompson's avatar
Ken Thompson committed
1649
	int o, i, w;
Ken Thompson's avatar
Ken Thompson committed
1650
	int oary[10];
Ken Thompson's avatar
Ken Thompson committed
1651
	int64 v;
1652
	Node n1, n2, n3, *nn, *l, *r;
Ken Thompson's avatar
Ken Thompson committed
1653 1654
	Node *reg, *reg1;
	Prog *p1;
Russ Cox's avatar
Russ Cox committed
1655
	Type *t;
Ken Thompson's avatar
Ken Thompson committed
1656

Russ Cox's avatar
Russ Cox committed
1657
	if(n->type == T)
Ken Thompson's avatar
Ken Thompson committed
1658
		return 0;
Ken Thompson's avatar
Ken Thompson committed
1659 1660

	switch(n->op) {
Ken Thompson's avatar
Ken Thompson committed
1661 1662 1663 1664 1665 1666 1667
	case OLITERAL:
		if(n->val.ctype != CTINT)
			break;
		v = mpgetfix(n->val.u.xval);
		if(v >= 32000 || v <= -32000)
			break;
		goto lit;
Ken Thompson's avatar
Ken Thompson committed
1668 1669 1670

	case ODOT:
	case ODOTPTR:
Ken Thompson's avatar
Ken Thompson committed
1671 1672 1673 1674 1675
		cleani += 2;
		reg = &clean[cleani-1];
		reg1 = &clean[cleani-2];
		reg->op = OEMPTY;
		reg1->op = OEMPTY;
Ken Thompson's avatar
Ken Thompson committed
1676 1677 1678
		goto odot;

	case OINDEX:
Ken Thompson's avatar
Ken Thompson committed
1679 1680 1681 1682 1683
		cleani += 2;
		reg = &clean[cleani-1];
		reg1 = &clean[cleani-2];
		reg->op = OEMPTY;
		reg1->op = OEMPTY;
Ken Thompson's avatar
Ken Thompson committed
1684 1685
		goto oindex;
	}
Ken Thompson's avatar
Ken Thompson committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
	return 0;

lit:
	switch(as) {
	default:
		return 0;
	case AADDB: case AADDW: case AADDL: case AADDQ:
	case ASUBB: case ASUBW: case ASUBL: case ASUBQ:
	case AANDB: case AANDW: case AANDL: case AANDQ:
	case AORB:  case AORW:  case AORL:  case AORQ:
	case AXORB: case AXORW: case AXORL: case AXORQ:
	case AINCB: case AINCW: case AINCL: case AINCQ:
	case ADECB: case ADECW: case ADECL: case ADECQ:
	case AMOVB: case AMOVW: case AMOVL: case AMOVQ:
		break;
	}

	cleani += 2;
	reg = &clean[cleani-1];
	reg1 = &clean[cleani-2];
	reg->op = OEMPTY;
	reg1->op = OEMPTY;
	naddr(n, a);
	goto yes;
Ken Thompson's avatar
Ken Thompson committed
1710 1711 1712 1713 1714

odot:
	o = dotoffset(n, oary, &nn);
	if(nn == N)
		goto no;
Russ Cox's avatar
Russ Cox committed
1715

1716 1717 1718 1719 1720 1721 1722 1723 1724
	if(nn->addable && o == 1 && oary[0] >= 0) {
		// directly addressable set of DOTs
		n1 = *nn;
		n1.type = n->type;
		n1.xoffset += oary[0];
		naddr(&n1, a);
		goto yes;
	}

Ken Thompson's avatar
Ken Thompson committed
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
	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);
	}
Ken Thompson's avatar
Ken Thompson committed
1735

Ken Thompson's avatar
Ken Thompson committed
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
	for(i=1; i<o; i++) {
		if(oary[i] >= 0)
			fatal("cant happen");
		gins(AMOVQ, &n1, reg);
		n1.xoffset = -(oary[i]+1);
	}

	a->type = D_NONE;
	a->index = D_NONE;
	naddr(&n1, a);
	goto yes;

oindex:
	l = n->left;
	r = n->right;
Ken Thompson's avatar
Ken Thompson committed
1751
	if(l->ullman >= UINF && r->ullman >= UINF)
Ken Thompson's avatar
Ken Thompson committed
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
		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;
	}

	w = n->type->width;
1770
	if(isconst(r, CTINT))
Ken Thompson's avatar
Ken Thompson committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
		goto oindex_const;

	switch(w) {
	default:
		goto no;
	case 1:
	case 2:
	case 4:
	case 8:
		break;
	}

	// load the array (reg)
	if(l->ullman > r->ullman) {
Ken Thompson's avatar
Ken Thompson committed
1785
		regalloc(reg, types[tptr], N);
Ken Thompson's avatar
Ken Thompson committed
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
		if(o & OPtrto)
			cgen(l, reg);
		else
			agen(l, reg);
	}

	// load the index (reg1)
	t = types[TUINT64];
	if(issigned[r->type->etype])
		t = types[TINT64];
	regalloc(reg1, t, N);
1797 1798 1799 1800
	regalloc(&n3, r->type, reg1);
	cgen(r, &n3);
	gmove(&n3, reg1);
	regfree(&n3);
Ken Thompson's avatar
Ken Thompson committed
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816

	// 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];
Ken Thompson's avatar
Ken Thompson committed
1817
			n2.xoffset = Array_nel;
Ken Thompson's avatar
Ken Thompson committed
1818
		} else {
Ken Thompson's avatar
Ken Thompson committed
1819 1820 1821
			nodconst(&n2, types[TUINT64], l->type->bound);
			if(o & OPtrto)
				nodconst(&n2, types[TUINT64], l->type->type->bound);
Ken Thompson's avatar
Ken Thompson committed
1822
		}
Ken Thompson's avatar
Ken Thompson committed
1823 1824
		gins(optoas(OCMP, types[TUINT32]), reg1, &n2);
		p1 = gbranch(optoas(OLT, types[TUINT32]), T);
Russ Cox's avatar
Russ Cox committed
1825
		ginscall(throwindex, 0);
Ken Thompson's avatar
Ken Thompson committed
1826 1827 1828 1829 1830 1831 1832
		patch(p1, pc);
	}

	if(o & ODynam) {
		n2 = *reg;
		n2.op = OINDREG;
		n2.type = types[tptr];
Ken Thompson's avatar
Ken Thompson committed
1833
		n2.xoffset = Array_array;
Ken Thompson's avatar
Ken Thompson committed
1834 1835
		gmove(&n2, reg);
	}
Ken Thompson's avatar
Ken Thompson committed
1836

Ken Thompson's avatar
Ken Thompson committed
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
	naddr(reg1, a);
	a->offset = 0;
	a->scale = w;
	a->index = a->type;
	a->type = reg->val.u.reg + D_INDIR;

	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) {

		if(!debug['B']) {
			n1 = *reg;
			n1.op = OINDREG;
			n1.type = types[tptr];
Ken Thompson's avatar
Ken Thompson committed
1863
			n1.xoffset = Array_nel;
Ken Thompson's avatar
Ken Thompson committed
1864 1865 1866
			nodconst(&n2, types[TUINT64], v);
			gins(optoas(OCMP, types[TUINT32]), &n1, &n2);
			p1 = gbranch(optoas(OGT, types[TUINT32]), T);
Russ Cox's avatar
Russ Cox committed
1867
			ginscall(throwindex, 0);
Ken Thompson's avatar
Ken Thompson committed
1868
			patch(p1, pc);
Ken Thompson's avatar
Ken Thompson committed
1869 1870
		}

Ken Thompson's avatar
Ken Thompson committed
1871 1872 1873
		n1 = *reg;
		n1.op = OINDREG;
		n1.type = types[tptr];
Ken Thompson's avatar
Ken Thompson committed
1874
		n1.xoffset = Array_array;
Ken Thompson's avatar
Ken Thompson committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
		gmove(&n1, reg);

	} else
	if(!debug['B']) {
		if(v < 0) {
			yyerror("out of bounds on array");
		} else
		if(o & OPtrto) {
			if(v >= l->type->type->bound)
				yyerror("out of bounds on array");
		} else
		if(v >= l->type->bound) {
			yyerror("out of bounds on array");
		}
Ken Thompson's avatar
Ken Thompson committed
1889
	}
Ken Thompson's avatar
Ken Thompson committed
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899

	n2 = *reg;
	n2.op = OINDREG;
	n2.xoffset = v*w;
	a->type = D_NONE;
	a->index = D_NONE;
	naddr(&n2, a);
	goto yes;

yes:
Ken Thompson's avatar
Ken Thompson committed
1900
	return 1;
Ken Thompson's avatar
Ken Thompson committed
1901 1902 1903 1904

no:
	sudoclean();
	return 0;
Ken Thompson's avatar
Ken Thompson committed
1905
}