You need to sign in or sign up before continuing.
gsubr.c 33.7 KB
Newer Older
Russ Cox's avatar
Russ Cox committed
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 171 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
// Derived from Inferno utils/8c/txt.c
// http://code.google.com/p/inferno-os/source/browse/utils/8c/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"

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

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

/*
 * generate and return proc with p->as = as,
 * linked into program.  pc is next instruction.
 */
Prog*
prog(int as)
{
	Prog *p;

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

	clearp(pc);

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

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

/*
 * generate a branch.
 * t is ignored.
 */
Prog*
gbranch(int as, Type *t)
{
	Prog *p;

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

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

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:
	case TSTRING:
	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;
	}
}

/*
 * return Axxx for Oxxx on type t.
 */
int
optoas(int op, Type *t)
{
	int a;

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

	a = AGOK;
	switch(CASE(op, simtype[t->etype])) {
	default:
		fatal("optoas: no entry %O-%T", op, t);
		break;
211

Russ Cox's avatar
Russ Cox committed
212 213 214
	case CASE(OADDR, TPTR32):
		a = ALEAL;
		break;
215

Russ Cox's avatar
Russ Cox committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 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 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	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 = 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):
	case CASE(ONE, TFLOAT32):
	case CASE(ONE, TFLOAT64):
		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):
	case CASE(OGT, TFLOAT32):
	case CASE(OGT, TFLOAT64):
		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):
	case CASE(OGE, TFLOAT32):
	case CASE(OGE, TFLOAT64):
		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):
	case CASE(OLT, TFLOAT32):
	case CASE(OLT, TFLOAT64):
		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):
	case CASE(OLE, TFLOAT32):
	case CASE(OLE, TFLOAT64):
		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(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(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(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(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(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;

Russ Cox's avatar
8g:  
Russ Cox committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	case CASE(OCOM, TINT8):
	case CASE(OCOM, TUINT8):
		a = ANOTB;
		break;

	case CASE(OCOM, TINT16):
	case CASE(OCOM, TUINT16):
		a = ANOTW;
		break;

	case CASE(OCOM, TINT32):
	case CASE(OCOM, TUINT32):
	case CASE(OCOM, TPTR32):
		a = ANOTL;
		break;

Russ Cox's avatar
Russ Cox committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	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(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(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(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(OLSH, TINT8):
	case CASE(OLSH, TUINT8):
		a = ASHLB;
		break;

	case CASE(OLSH, TINT16):
	case CASE(OLSH, TUINT16):
		a = ASHLW;
		break;

	case CASE(OLSH, TINT32):
	case CASE(OLSH, TUINT32):
	case CASE(OLSH, TPTR32):
		a = ASHLL;
		break;

	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, TINT8):
		a = ASARB;
		break;

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

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

	case CASE(OMUL, TINT8):
	case CASE(OMUL, TUINT8):
		a = AIMULB;
		break;

	case CASE(OMUL, TINT16):
	case CASE(OMUL, TUINT16):
		a = AIMULW;
		break;

	case CASE(OMUL, TINT32):
	case CASE(OMUL, TUINT32):
	case CASE(OMUL, TPTR32):
		a = AIMULL;
		break;

	case CASE(ODIV, TINT8):
	case CASE(OMOD, TINT8):
		a = AIDIVB;
		break;

	case CASE(ODIV, TUINT8):
	case CASE(OMOD, TUINT8):
		a = ADIVB;
		break;

	case CASE(ODIV, TINT16):
	case CASE(OMOD, TINT16):
		a = AIDIVW;
		break;

	case CASE(ODIV, TUINT16):
	case CASE(OMOD, TUINT16):
		a = ADIVW;
		break;

	case CASE(ODIV, TINT32):
	case CASE(OMOD, TINT32):
		a = AIDIVL;
		break;

	case CASE(ODIV, TUINT32):
	case CASE(ODIV, TPTR32):
	case CASE(OMOD, TUINT32):
	case CASE(OMOD, TPTR32):
		a = ADIVL;
		break;

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

583 584 585
	case CASE(OEXTEND, TINT32):
		a = ACDQ;
		break;
Russ Cox's avatar
Russ Cox committed
586 587 588 589
	}
	return a;
}

Russ Cox's avatar
Russ Cox committed
590 591 592 593 594 595
#define FCASE(a, b, c)  (((a)<<16)|((b)<<8)|(c))
int
foptoas(int op, Type *t, int flg)
{
	int et;

Russ Cox's avatar
8g:  
Russ Cox committed
596 597 598 599 600 601 602
	et = simtype[t->etype];

	// If we need Fpop, it means we're working on
	// two different floating-point registers, not memory.
	// There the instruction only has a float64 form.
	if(flg & Fpop)
		et = TFLOAT64;
Russ Cox's avatar
Russ Cox committed
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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

	// clear Frev if unneeded
	switch(op) {
	case OADD:
	case OMUL:
		flg &= ~Frev;
		break;
	}

	switch(FCASE(op, et, flg)) {
	case FCASE(OADD, TFLOAT32, 0):
		return AFADDF;
	case FCASE(OADD, TFLOAT64, 0):
		return AFADDD;
	case FCASE(OADD, TFLOAT64, Fpop):
		return AFADDDP;

	case FCASE(OSUB, TFLOAT32, 0):
		return AFSUBF;
	case FCASE(OSUB, TFLOAT32, Frev):
		return AFSUBRF;

	case FCASE(OSUB, TFLOAT64, 0):
		return AFSUBD;
	case FCASE(OSUB, TFLOAT64, Frev):
		return AFSUBRD;
	case FCASE(OSUB, TFLOAT64, Fpop):
		return AFSUBDP;
	case FCASE(OSUB, TFLOAT64, Fpop|Frev):
		return AFSUBRDP;

	case FCASE(OMUL, TFLOAT32, 0):
		return AFMULF;
	case FCASE(OMUL, TFLOAT64, 0):
		return AFMULD;
	case FCASE(OMUL, TFLOAT64, Fpop):
		return AFMULDP;

	case FCASE(ODIV, TFLOAT32, 0):
		return AFDIVF;
	case FCASE(ODIV, TFLOAT32, Frev):
		return AFDIVRF;

	case FCASE(ODIV, TFLOAT64, 0):
		return AFDIVD;
	case FCASE(ODIV, TFLOAT64, Frev):
		return AFDIVRD;
	case FCASE(ODIV, TFLOAT64, Fpop):
		return AFDIVDP;
	case FCASE(ODIV, TFLOAT64, Fpop|Frev):
		return AFDIVRDP;

	case FCASE(OCMP, TFLOAT32, 0):
		return AFCOMF;
	case FCASE(OCMP, TFLOAT32, Fpop):
		return AFCOMFP;
	case FCASE(OCMP, TFLOAT64, 0):
		return AFCOMD;
	case FCASE(OCMP, TFLOAT64, Fpop):
		return AFCOMDP;
	case FCASE(OCMP, TFLOAT64, Fpop2):
		return AFCOMDPP;
	}

	fatal("foptoas %O %T %#x", op, t, flg);
	return 0;
}

Russ Cox's avatar
Russ Cox committed
671 672 673 674 675 676 677 678 679
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
Russ Cox's avatar
8g:  
Russ Cox committed
680 681 682

	D_BL,	// because D_BX can be allocated
	D_BH,
Russ Cox's avatar
Russ Cox committed
683 684 685 686 687 688 689 690 691
};

void
ginit(void)
{
	int i;

	for(i=0; i<nelem(reg); i++)
		reg[i] = 1;
Russ Cox's avatar
8g:  
Russ Cox committed
692
	for(i=D_AL; i<=D_DI; i++)
Russ Cox's avatar
Russ Cox committed
693
		reg[i] = 0;
694

Russ Cox's avatar
Russ Cox committed
695 696 697 698
	for(i=0; i<nelem(resvd); i++)
		reg[resvd[i]]++;
}

Russ Cox's avatar
Russ Cox committed
699 700
ulong regpc[D_NONE];

Russ Cox's avatar
Russ Cox committed
701 702 703 704 705 706 707 708
void
gclean(void)
{
	int i;

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

Russ Cox's avatar
8g:  
Russ Cox committed
709
	for(i=D_AL; i<=D_DI; i++)
Russ Cox's avatar
Russ Cox committed
710
		if(reg[i])
Russ Cox's avatar
Russ Cox committed
711
			yyerror("reg %R left allocated at %lux", i, regpc[i]);
Russ Cox's avatar
Russ Cox committed
712 713 714 715 716 717 718 719 720 721
}

/*
 * allocate register of type t, leave in n.
 * if o != N, o is desired fixed register.
 * caller must regfree(n).
 */
void
regalloc(Node *n, Type *t, Node *o)
{
Russ Cox's avatar
Russ Cox committed
722
	int i, et;
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

	if(t == T)
		fatal("regalloc: t nil");
	et = simtype[t->etype];

	switch(et) {
	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) {
			i = o->val.u.reg;
Russ Cox's avatar
Russ Cox committed
742
			if(i >= D_AX && i <= D_DI)
Russ Cox's avatar
Russ Cox committed
743 744
				goto out;
		}
Russ Cox's avatar
Russ Cox committed
745
		for(i=D_AX; i<=D_DI; i++)
Russ Cox's avatar
Russ Cox committed
746 747 748 749
			if(reg[i] == 0)
				goto out;

		fprint(2, "registers allocated at\n");
Russ Cox's avatar
Russ Cox committed
750
		for(i=D_AX; i<=D_DI; i++)
Russ Cox's avatar
Russ Cox committed
751 752 753 754 755 756
			fprint(2, "\t%R\t%#lux\n", i, regpc[i]);
		yyerror("out of fixed registers");
		goto err;

	case TFLOAT32:
	case TFLOAT64:
Russ Cox's avatar
Russ Cox committed
757 758
		i = D_F0;
		goto out;
Russ Cox's avatar
Russ Cox committed
759 760 761 762 763 764 765 766 767
	}
	yyerror("regalloc: unknown type %T", t);
	i = 0;

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

out:
768
	if(reg[i] == 0) {
Russ Cox's avatar
Russ Cox committed
769
		regpc[i] = (ulong)__builtin_return_address(0);
770 771 772 773 774
		if(i == D_AX || i == D_CX || i == D_DX || i == D_SP) {
			dump("regalloc-o", o);
			fatal("regalloc %R", i);
		}
	}
Russ Cox's avatar
Russ Cox committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	reg[i]++;
	nodreg(n, t, i);
}

void
regfree(Node *n)
{
	int i;

	if(n->op != OREGISTER && n->op != OINDREG)
		fatal("regfree: not a register");
	i = n->val.u.reg;
	if(i < 0 || i >= sizeof(reg))
		fatal("regfree: reg out of range");
	if(reg[i] <= 0)
		fatal("regfree: reg not allocated");
	reg[i]--;
792 793
	if(reg[i] == 0 && (i == D_AX || i == D_CX || i == D_DX || i == D_SP))
		fatal("regfree %R", i);
Russ Cox's avatar
Russ Cox committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
}

void
tempalloc(Node *n, Type *t)
{
	int w;

	dowidth(t);

	memset(n, 0, sizeof(*n));
	n->op = ONAME;
	n->sym = S;
	n->type = t;
	n->etype = t->etype;
	n->class = PAUTO;
	n->addable = 1;
	n->ullman = 1;
	n->noescape = 1;
	n->ostk = stksize;

	w = t->width;
	stksize += w;
	stksize = rnd(stksize, w);
	n->xoffset = -stksize;
Russ Cox's avatar
8g:  
Russ Cox committed
818
//print("tempalloc %d -> %d from %p\n", n->ostk, n->xoffset, __builtin_return_address(0));
Russ Cox's avatar
Russ Cox committed
819 820 821 822 823 824 825
	if(stksize > maxstksize)
		maxstksize = stksize;
}

void
tempfree(Node *n)
{
Russ Cox's avatar
8g:  
Russ Cox committed
826
//print("tempfree %d\n", n->xoffset);
Russ Cox's avatar
Russ Cox committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
	if(n->xoffset != -stksize)
		fatal("tempfree %lld %d", -n->xoffset, stksize);
	stksize = n->ostk;
}

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

849 850 851 852 853 854 855 856 857 858
/*
 * 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;
}

Russ Cox's avatar
Russ Cox committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
Node*
nodarg(Type *t, int fp)
{
	Node *n;
	Type *first;
	Iter savet;

	// entire argument struct, not just one arg
	switch(t->etype) {
	default:
		fatal("nodarg %T", t);

	case TSTRUCT:
		if(!t->funarg)
			fatal("nodarg: TSTRUCT but not 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;
		break;
885

Russ Cox's avatar
Russ Cox committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	case TFIELD:
		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);
		n->xoffset = t->width;
		n->addable = 1;
		break;
	}

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

	case 0:		// output arg
		n->op = OINDREG;
		n->val.u.reg = D_SP;
		break;

	case 1:		// input arg
		n->class = PPARAM;
		break;
	}
Russ Cox's avatar
Russ Cox committed
910 911

	n->typecheck = 1;
Russ Cox's avatar
Russ Cox committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	return n;
}

/*
 * generate
 *	as $c, reg
 */
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);
}

Russ Cox's avatar
8g:  
Russ Cox committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
/*
 * swap node contents
 */
void
nswap(Node *a, Node *b)
{
	Node t;

	t = *a;
	*a = *b;
	*b = t;
}

/*
 * return constant i node.
 * overwritten by next call, but useful in calls to gins.
 */
Node*
ncon(uint32 i)
{
	static Node n;

	if(n.type == T)
		nodconst(&n, types[TUINT32], 0);
	mpmovecfix(n.val.u.xval, i);
	return &n;
}
Russ Cox's avatar
Russ Cox committed
956

Russ Cox's avatar
Russ Cox committed
957
/*
Russ Cox's avatar
Russ Cox committed
958 959 960 961 962 963
 * Is this node a memory operand?
 */
int
ismem(Node *n)
{
	switch(n->op) {
964 965
	case OLEN:
	case OCAP:
Russ Cox's avatar
Russ Cox committed
966 967 968 969 970 971 972 973 974 975 976 977 978
	case OINDREG:
	case ONAME:
	case OPARAM:
		return 1;
	}
	return 0;
}

Node sclean[10];
int nsclean;

/*
 * n is a 64-bit value.  fill in lo and hi to refer to its 32-bit halves.
Russ Cox's avatar
Russ Cox committed
979 980
 */
void
Russ Cox's avatar
Russ Cox committed
981
split64(Node *n, Node *lo, Node *hi)
Russ Cox's avatar
Russ Cox committed
982
{
Russ Cox's avatar
Russ Cox committed
983 984
	Node n1;
	int64 i;
Russ Cox's avatar
Russ Cox committed
985

Russ Cox's avatar
Russ Cox committed
986 987
	if(!is64(n->type))
		fatal("split64 %T", n->type);
Russ Cox's avatar
Russ Cox committed
988

Russ Cox's avatar
Russ Cox committed
989 990 991 992 993
	sclean[nsclean].op = OEMPTY;
	if(nsclean >= nelem(sclean))
		fatal("split64 clean");
	nsclean++;
	switch(n->op) {
Russ Cox's avatar
Russ Cox committed
994
	default:
Russ Cox's avatar
Russ Cox committed
995 996 997 998 999
		if(!dotaddable(n, &n1)) {
			igen(n, &n1, N);
			sclean[nsclean-1] = n1;
		}
		n = &n1;
Russ Cox's avatar
8g:  
Russ Cox committed
1000
		goto common;
Russ Cox's avatar
Russ Cox committed
1001
	case ONAME:
Russ Cox's avatar
8g:  
Russ Cox committed
1002 1003 1004 1005 1006 1007 1008
		if(n->class == PPARAMREF) {
			cgen(n->heapaddr, &n1);
			sclean[nsclean-1] = n1;
			// fall through.
			n = &n1;
		}
		goto common;
Russ Cox's avatar
Russ Cox committed
1009
	case OINDREG:
Russ Cox's avatar
8g:  
Russ Cox committed
1010
	common:
Russ Cox's avatar
Russ Cox committed
1011 1012 1013 1014 1015 1016 1017 1018
		*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;
Russ Cox's avatar
Russ Cox committed
1019
		break;
Russ Cox's avatar
Russ Cox committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

	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);
Russ Cox's avatar
Russ Cox committed
1030 1031
		break;
	}
Russ Cox's avatar
Russ Cox committed
1032
}
Russ Cox's avatar
Russ Cox committed
1033

Russ Cox's avatar
Russ Cox committed
1034 1035 1036 1037 1038 1039 1040 1041
void
splitclean(void)
{
	if(nsclean <= 0)
		fatal("splitclean");
	nsclean--;
	if(sclean[nsclean].op != OEMPTY)
		regfree(&sclean[nsclean]);
Russ Cox's avatar
Russ Cox committed
1042 1043
}

Russ Cox's avatar
8g:  
Russ Cox committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
/*
 * set up nodes representing fp constants
 */
Node zerof;
Node two64f;
Node two63f;

void
bignodes(void)
{
	static int did;

	if(did)
		return;
	did = 1;

	two64f = *ncon(0);
	two64f.type = types[TFLOAT64];
	two64f.val.ctype = CTFLT;
	two64f.val.u.fval = mal(sizeof *two64f.val.u.fval);
	mpmovecflt(two64f.val.u.fval, 18446744073709551616.);

	two63f = two64f;
	two63f.val.u.fval = mal(sizeof *two63f.val.u.fval);
	mpmovecflt(two63f.val.u.fval, 9223372036854775808.);

	zerof = two64f;
	zerof.val.u.fval = mal(sizeof *zerof.val.u.fval);
	mpmovecflt(zerof.val.u.fval, 0);
}

Russ Cox's avatar
Russ Cox committed
1075
void
Russ Cox's avatar
Russ Cox committed
1076
gmove(Node *f, Node *t)
Russ Cox's avatar
Russ Cox committed
1077 1078
{
	int a, ft, tt;
Russ Cox's avatar
Russ Cox committed
1079
	Type *cvt;
Russ Cox's avatar
8g:  
Russ Cox committed
1080 1081
	Node r1, r2, t1, t2, flo, fhi, tlo, thi, con, f0, f1, ax, dx, cx;
	Prog *p1, *p2, *p3;
Russ Cox's avatar
Russ Cox committed
1082 1083 1084 1085 1086 1087 1088 1089

	if(debug['M'])
		print("gmove %N -> %N\n", f, t);

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

Russ Cox's avatar
8g:  
Russ Cox committed
1090
	// cannot have two integer memory operands;
Russ Cox's avatar
Russ Cox committed
1091
	// except 64-bit, which always copies via registers anyway.
Russ Cox's avatar
8g:  
Russ Cox committed
1092
	if(isint[ft] && isint[tt] && !is64(f->type) && !is64(t->type) && ismem(f) && ismem(t))
Russ Cox's avatar
Russ Cox committed
1093 1094 1095 1096
		goto hard;

	// convert constant to desired type
	if(f->op == OLITERAL) {
Russ Cox's avatar
8g:  
Russ Cox committed
1097 1098 1099 1100
		if(tt == TFLOAT32)
			convconst(&con, types[TFLOAT64], &f->val);
		else
			convconst(&con, t->type, &f->val);
Russ Cox's avatar
Russ Cox committed
1101
		f = &con;
Russ Cox's avatar
8g:  
Russ Cox committed
1102
		ft = simsimtype(con.type);
Russ Cox's avatar
Russ Cox committed
1103 1104 1105 1106 1107 1108 1109 1110

		// some constants can't move directly to memory.
		if(ismem(t)) {
			// float constants come from memory.
			if(isfloat[tt])
				goto hard;
		}
	}
1111

Russ Cox's avatar
Russ Cox committed
1112 1113 1114 1115 1116 1117
	// 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.
Russ Cox's avatar
Russ Cox committed
1118

Russ Cox's avatar
Russ Cox committed
1119
	switch(CASE(ft, tt)) {
Russ Cox's avatar
Russ Cox committed
1120
	default:
Russ Cox's avatar
8g:  
Russ Cox committed
1121
		goto fatal;
Russ Cox's avatar
Russ Cox committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

	/*
	 * 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):
	case CASE(TINT16, TUINT8):
	case CASE(TUINT16, TUINT8):
	case CASE(TINT32, TUINT8):
	case CASE(TUINT32, TUINT8):
Russ Cox's avatar
Russ Cox committed
1138 1139
		a = AMOVB;
		break;
Russ Cox's avatar
Russ Cox committed
1140

Russ Cox's avatar
Russ Cox committed
1141 1142 1143 1144 1145
	case CASE(TINT64, TINT8):	// truncate low word
	case CASE(TUINT64, TINT8):
	case CASE(TINT64, TUINT8):
	case CASE(TUINT64, TUINT8):
		split64(f, &flo, &fhi);
Russ Cox's avatar
8g:  
Russ Cox committed
1146
		nodreg(&r1, t->type, D_AX);
Russ Cox's avatar
Russ Cox committed
1147 1148 1149 1150 1151
		gins(AMOVB, &flo, &r1);
		gins(AMOVB, &r1, t);
		splitclean();
		return;

Russ Cox's avatar
Russ Cox committed
1152 1153 1154 1155 1156 1157 1158 1159
	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(TINT32, TUINT16):
	case CASE(TUINT32, TUINT16):
Russ Cox's avatar
Russ Cox committed
1160 1161
		a = AMOVW;
		break;
Russ Cox's avatar
Russ Cox committed
1162

Russ Cox's avatar
Russ Cox committed
1163 1164 1165 1166 1167
	case CASE(TINT64, TINT16):	// truncate low word
	case CASE(TUINT64, TINT16):
	case CASE(TINT64, TUINT16):
	case CASE(TUINT64, TUINT16):
		split64(f, &flo, &fhi);
Russ Cox's avatar
8g:  
Russ Cox committed
1168
		nodreg(&r1, t->type, D_AX);
Russ Cox's avatar
Russ Cox committed
1169 1170 1171 1172 1173
		gins(AMOVW, &flo, &r1);
		gins(AMOVW, &r1, t);
		splitclean();
		return;

Russ Cox's avatar
Russ Cox committed
1174 1175 1176 1177
	case CASE(TINT32, TINT32):	// same size
	case CASE(TINT32, TUINT32):
	case CASE(TUINT32, TINT32):
	case CASE(TUINT32, TUINT32):
Russ Cox's avatar
Russ Cox committed
1178 1179
		a = AMOVL;
		break;
Russ Cox's avatar
Russ Cox committed
1180

Russ Cox's avatar
Russ Cox committed
1181 1182 1183 1184 1185
	case CASE(TINT64, TINT32):	// truncate
	case CASE(TUINT64, TINT32):
	case CASE(TINT64, TUINT32):
	case CASE(TUINT64, TUINT32):
		split64(f, &flo, &fhi);
Russ Cox's avatar
8g:  
Russ Cox committed
1186
		nodreg(&r1, t->type, D_AX);
Russ Cox's avatar
Russ Cox committed
1187 1188 1189 1190 1191
		gins(AMOVL, &flo, &r1);
		gins(AMOVL, &r1, t);
		splitclean();
		return;

Russ Cox's avatar
Russ Cox committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	case CASE(TINT64, TINT64):	// same size
	case CASE(TINT64, TUINT64):
	case CASE(TUINT64, TINT64):
	case CASE(TUINT64, TUINT64):
		split64(f, &flo, &fhi);
		split64(t, &tlo, &thi);
		if(f->op == OLITERAL) {
			gins(AMOVL, &flo, &tlo);
			gins(AMOVL, &fhi, &thi);
		} else {
Russ Cox's avatar
8g:  
Russ Cox committed
1202 1203
			nodreg(&r1, t->type, D_AX);
			nodreg(&r2, t->type, D_DX);
Russ Cox's avatar
Russ Cox committed
1204 1205 1206 1207
			gins(AMOVL, &flo, &r1);
			gins(AMOVL, &fhi, &r2);
			gins(AMOVL, &r1, &tlo);
			gins(AMOVL, &r2, &thi);
Russ Cox's avatar
Russ Cox committed
1208
		}
Russ Cox's avatar
Russ Cox committed
1209 1210 1211
		splitclean();
		splitclean();
		return;
Russ Cox's avatar
Russ Cox committed
1212

Russ Cox's avatar
Russ Cox committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
	/*
	 * integer up-conversions
	 */
	case CASE(TINT8, TINT16):	// sign extend int8
	case CASE(TINT8, TUINT16):
		a = AMOVBWSX;
		goto rdst;
	case CASE(TINT8, TINT32):
	case CASE(TINT8, TUINT32):
		a = AMOVBLSX;
		goto rdst;
Russ Cox's avatar
Russ Cox committed
1224 1225 1226 1227
	case CASE(TINT8, TINT64):	// convert via int32
	case CASE(TINT8, TUINT64):
		cvt = types[TINT32];
		goto hard;
Russ Cox's avatar
Russ Cox committed
1228 1229 1230 1231 1232 1233 1234 1235 1236

	case CASE(TUINT8, TINT16):	// zero extend uint8
	case CASE(TUINT8, TUINT16):
		a = AMOVBWZX;
		goto rdst;
	case CASE(TUINT8, TINT32):
	case CASE(TUINT8, TUINT32):
		a = AMOVBLZX;
		goto rdst;
Russ Cox's avatar
Russ Cox committed
1237 1238 1239 1240
	case CASE(TUINT8, TINT64):	// convert via uint32
	case CASE(TUINT8, TUINT64):
		cvt = types[TUINT32];
		goto hard;
Russ Cox's avatar
Russ Cox committed
1241 1242 1243 1244 1245

	case CASE(TINT16, TINT32):	// sign extend int16
	case CASE(TINT16, TUINT32):
		a = AMOVWLSX;
		goto rdst;
Russ Cox's avatar
Russ Cox committed
1246 1247 1248 1249
	case CASE(TINT16, TINT64):	// convert via int32
	case CASE(TINT16, TUINT64):
		cvt = types[TINT32];
		goto hard;
Russ Cox's avatar
Russ Cox committed
1250 1251 1252 1253 1254

	case CASE(TUINT16, TINT32):	// zero extend uint16
	case CASE(TUINT16, TUINT32):
		a = AMOVWLZX;
		goto rdst;
Russ Cox's avatar
Russ Cox committed
1255 1256 1257 1258
	case CASE(TUINT16, TINT64):	// convert via uint32
	case CASE(TUINT16, TUINT64):
		cvt = types[TUINT32];
		goto hard;
Russ Cox's avatar
Russ Cox committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

	case CASE(TINT32, TINT64):	// sign extend int32
	case CASE(TINT32, TUINT64):
		split64(t, &tlo, &thi);
		nodreg(&flo, tlo.type, D_AX);
		nodreg(&fhi, thi.type, D_DX);
		gmove(f, &flo);
		gins(ACDQ, N, N);
		gins(AMOVL, &flo, &tlo);
		gins(AMOVL, &fhi, &thi);
Russ Cox's avatar
Russ Cox committed
1269
		splitclean();
Russ Cox's avatar
Russ Cox committed
1270
		return;
Russ Cox's avatar
Russ Cox committed
1271

Russ Cox's avatar
Russ Cox committed
1272 1273 1274 1275 1276 1277 1278
	case CASE(TUINT32, TINT64):	// zero extend uint32
	case CASE(TUINT32, TUINT64):
		split64(t, &tlo, &thi);
		gmove(f, &tlo);
		gins(AMOVL, ncon(0), &thi);
		splitclean();
		return;
Russ Cox's avatar
Russ Cox committed
1279

Russ Cox's avatar
Russ Cox committed
1280 1281
	/*
	* float to integer
Russ Cox's avatar
8g:  
Russ Cox committed
1282
	*/
Russ Cox's avatar
Russ Cox committed
1283 1284 1285 1286 1287 1288
	case CASE(TFLOAT32, TINT16):
	case CASE(TFLOAT32, TINT32):
	case CASE(TFLOAT32, TINT64):
	case CASE(TFLOAT64, TINT16):
	case CASE(TFLOAT64, TINT32):
	case CASE(TFLOAT64, TINT64):
Russ Cox's avatar
8g:  
Russ Cox committed
1289 1290 1291
		if(t->op == OREGISTER)
			goto hardmem;
		nodreg(&r1, types[ft], D_F0);
Russ Cox's avatar
Russ Cox committed
1292
		if(ft == TFLOAT32)
Russ Cox's avatar
8g:  
Russ Cox committed
1293
			gins(AFMOVF, f, &r1);
Russ Cox's avatar
Russ Cox committed
1294
		else
Russ Cox's avatar
8g:  
Russ Cox committed
1295 1296 1297 1298 1299 1300 1301 1302
			gins(AFMOVD, f, &r1);

		// set round to zero mode during conversion
		tempalloc(&t1, types[TUINT16]);
		tempalloc(&t2, types[TUINT16]);
		gins(AFSTCW, N, &t1);
		gins(AMOVW, ncon(0xf7f), &t2);
		gins(AFLDCW, &t2, N);
Russ Cox's avatar
Russ Cox committed
1303
		if(tt == TINT16)
Russ Cox's avatar
8g:  
Russ Cox committed
1304
			gins(AFMOVWP, &r1, t);
Russ Cox's avatar
Russ Cox committed
1305
		else if(tt == TINT32)
Russ Cox's avatar
8g:  
Russ Cox committed
1306
			gins(AFMOVLP, &r1, t);
Russ Cox's avatar
Russ Cox committed
1307
		else
Russ Cox's avatar
8g:  
Russ Cox committed
1308 1309 1310 1311
			gins(AFMOVVP, &r1, t);
		gins(AFLDCW, &t1, N);
		tempfree(&t2);
		tempfree(&t1);
Russ Cox's avatar
Russ Cox committed
1312
		return;
Russ Cox's avatar
Russ Cox committed
1313

Russ Cox's avatar
Russ Cox committed
1314 1315 1316 1317 1318 1319 1320
	case CASE(TFLOAT32, TINT8):
	case CASE(TFLOAT32, TUINT16):
	case CASE(TFLOAT32, TUINT8):
	case CASE(TFLOAT64, TINT8):
	case CASE(TFLOAT64, TUINT16):
	case CASE(TFLOAT64, TUINT8):
		// convert via int32.
Russ Cox's avatar
8g:  
Russ Cox committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
		tempalloc(&t1, types[TINT32]);
		gmove(f, &t1);
		switch(tt) {
		default:
			fatal("gmove %T", t);
		case TINT8:
			gins(ACMPL, &t1, ncon(-0x80));
			p1 = gbranch(optoas(OLT, types[TINT32]), T);
			gins(ACMPL, &t1, ncon(0x7f));
			p2 = gbranch(optoas(OGT, types[TINT32]), T);
			p3 = gbranch(AJMP, T);
			patch(p1, pc);
			patch(p2, pc);
			gmove(ncon(-0x80), &t1);
			patch(p3, pc);
			gmove(&t1, t);
			break;
		case TUINT8:
			gins(ATESTL, ncon(0xffffff00), &t1);
			p1 = gbranch(AJEQ, T);
			gins(AMOVB, ncon(0), &t1);
			patch(p1, pc);
			gmove(&t1, t);
			break;
		case TUINT16:
			gins(ATESTL, ncon(0xffff0000), &t1);
			p1 = gbranch(AJEQ, T);
			gins(AMOVW, ncon(0), &t1);
			patch(p1, pc);
			gmove(&t1, t);
			break;
		}
		tempfree(&t1);
		return;
Russ Cox's avatar
Russ Cox committed
1355 1356 1357

	case CASE(TFLOAT32, TUINT32):
	case CASE(TFLOAT64, TUINT32):
Russ Cox's avatar
8g:  
Russ Cox committed
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
		// convert via int64.
		tempalloc(&t1, types[TINT64]);
		gmove(f, &t1);
		split64(&t1, &tlo, &thi);
		gins(ACMPL, &thi, ncon(0));
		p1 = gbranch(AJEQ, T);
		gins(AMOVL, ncon(0), &tlo);
		patch(p1, pc);
		gmove(&tlo, t);
		splitclean();
		tempfree(&t1);
		return;
Russ Cox's avatar
Russ Cox committed
1370 1371 1372

	case CASE(TFLOAT32, TUINT64):
	case CASE(TFLOAT64, TUINT64):
Russ Cox's avatar
8g:  
Russ Cox committed
1373 1374 1375 1376 1377
		bignodes();
		nodreg(&f0, types[ft], D_F0);
		nodreg(&f1, types[ft], D_F0 + 1);
		nodreg(&ax, types[TUINT16], D_AX);

Russ Cox's avatar
Russ Cox committed
1378
		gmove(f, &f0);
Russ Cox's avatar
8g:  
Russ Cox committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

		// if 0 > v { answer = 0 }
		gmove(&zerof, &f0);
		gins(AFUCOMP, &f0, &f1);
		gins(AFSTSW, N, &ax);
		gins(ASAHF, N, N);
		p1 = gbranch(optoas(OGT, types[tt]), T);
		// if 1<<64 <= v { answer = 0 too }
		gmove(&two64f, &f0);
		gins(AFUCOMP, &f0, &f1);
		gins(AFSTSW, N, &ax);
		gins(ASAHF, N, N);
		p2 = gbranch(optoas(OGT, types[tt]), T);
		patch(p1, pc);
		gins(AFMOVVP, &f0, t);	// don't care about t, but will pop the stack
		split64(t, &tlo, &thi);
		gins(AMOVL, ncon(0), &tlo);
		gins(AMOVL, ncon(0), &thi);
		splitclean();
		p1 = gbranch(AJMP, T);
		patch(p2, pc);

		// in range; algorithm is:
Russ Cox's avatar
Russ Cox committed
1402 1403
		//	if small enough, use native float64 -> int64 conversion.
		//	otherwise, subtract 2^63, convert, and add it back.
Russ Cox's avatar
8g:  
Russ Cox committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420

		// set round to zero mode during conversion
		tempalloc(&t1, types[TUINT16]);
		tempalloc(&t2, types[TUINT16]);
		gins(AFSTCW, N, &t1);
		gins(AMOVW, ncon(0xf7f), &t2);
		gins(AFLDCW, &t2, N);
		tempfree(&t2);

		// actual work
		gmove(&two63f, &f0);
		gins(AFUCOMP, &f0, &f1);
		gins(AFSTSW, N, &ax);
		gins(ASAHF, N, N);
		p2 = gbranch(optoas(OLE, types[tt]), T);
		gins(AFMOVVP, &f0, t);
		p3 = gbranch(AJMP, T);
Russ Cox's avatar
Russ Cox committed
1421
		patch(p2, pc);
Russ Cox's avatar
8g:  
Russ Cox committed
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
		gmove(&two63f, &f0);
		gins(AFSUBDP, &f0, &f1);
		gins(AFMOVVP, &f0, t);
		split64(t, &tlo, &thi);
		gins(AXORL, ncon(0x80000000), &thi);	// + 2^63
		patch(p3, pc);
		patch(p1, pc);
		splitclean();

		// restore rounding mode
		gins(AFLDCW, &t1, N);
		tempfree(&t1);
Russ Cox's avatar
Russ Cox committed
1434
		return;
Russ Cox's avatar
8g:  
Russ Cox committed
1435

Russ Cox's avatar
Russ Cox committed
1436 1437
	/*
	 * integer to float
Russ Cox's avatar
8g:  
Russ Cox committed
1438 1439 1440
	 */
	case CASE(TINT16, TFLOAT32):
	case CASE(TINT16, TFLOAT64):
Russ Cox's avatar
Russ Cox committed
1441 1442 1443 1444
	case CASE(TINT32, TFLOAT32):
	case CASE(TINT32, TFLOAT64):
	case CASE(TINT64, TFLOAT32):
	case CASE(TINT64, TFLOAT64):
Russ Cox's avatar
8g:  
Russ Cox committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
		if(t->op != OREGISTER)
			goto hard;
		if(f->op == OREGISTER) {
			cvt = f->type;
			goto hardmem;
		}
		switch(ft) {
		case TINT16:
			a = AFMOVW;
			break;
		case TINT32:
			a = AFMOVL;
			break;
		default:
			a = AFMOVV;
			break;
		}
		break;
Russ Cox's avatar
Russ Cox committed
1463 1464 1465 1466 1467 1468 1469

	case CASE(TINT8, TFLOAT32):
	case CASE(TINT8, TFLOAT64):
	case CASE(TUINT16, TFLOAT32):
	case CASE(TUINT16, TFLOAT64):
	case CASE(TUINT8, TFLOAT32):
	case CASE(TUINT8, TFLOAT64):
Russ Cox's avatar
8g:  
Russ Cox committed
1470
		// convert via int32 memory
Russ Cox's avatar
Russ Cox committed
1471
		cvt = types[TINT32];
Russ Cox's avatar
8g:  
Russ Cox committed
1472
		goto hardmem;
Russ Cox's avatar
Russ Cox committed
1473 1474 1475

	case CASE(TUINT32, TFLOAT32):
	case CASE(TUINT32, TFLOAT64):
Russ Cox's avatar
8g:  
Russ Cox committed
1476
		// convert via int64 memory
Russ Cox's avatar
Russ Cox committed
1477
		cvt = types[TINT64];
Russ Cox's avatar
8g:  
Russ Cox committed
1478
		goto hardmem;
Russ Cox's avatar
Russ Cox committed
1479 1480 1481 1482 1483 1484

	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.
Russ Cox's avatar
8g:  
Russ Cox committed
1485 1486 1487 1488 1489 1490 1491
		nodreg(&ax, types[TUINT32], D_AX);
		nodreg(&dx, types[TUINT32], D_DX);
		nodreg(&cx, types[TUINT32], D_CX);
		tempalloc(&t1, f->type);
		split64(&t1, &tlo, &thi);
		gmove(f, &t1);
		gins(ACMPL, &thi, ncon(0));
Russ Cox's avatar
Russ Cox committed
1492
		p1 = gbranch(AJLT, T);
Russ Cox's avatar
8g:  
Russ Cox committed
1493 1494 1495
		// native
		t1.type = types[TINT64];
		gmove(&t1, t);
Russ Cox's avatar
Russ Cox committed
1496
		p2 = gbranch(AJMP, T);
Russ Cox's avatar
8g:  
Russ Cox committed
1497
		// simulated
Russ Cox's avatar
Russ Cox committed
1498
		patch(p1, pc);
Russ Cox's avatar
8g:  
Russ Cox committed
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
		gmove(&tlo, &ax);
		gmove(&thi, &dx);
		p1 = gins(ASHRL, ncon(1), &ax);
		p1->from.index = D_DX;	// double-width shift DX -> AX
		p1->from.scale = 0;
		gins(ASETCC, N, &cx);
		gins(AORB, &cx, &ax);
		gins(ASHRL, ncon(1), &dx);
		gmove(&dx, &thi);
		gmove(&ax, &tlo);
		nodreg(&r1, types[tt], D_F0);
		nodreg(&r2, types[tt], D_F0 + 1);
		gmove(&t1, &r1);	// t1.type is TINT64 now, set above
		gins(AFMOVD, &r1, &r1);
		gins(AFADDDP, &r1, &r2);
		gmove(&r1, t);
Russ Cox's avatar
Russ Cox committed
1515
		patch(p2, pc);
Russ Cox's avatar
8g:  
Russ Cox committed
1516 1517
		splitclean();
		tempfree(&t1);
Russ Cox's avatar
Russ Cox committed
1518
		return;
Russ Cox's avatar
8g:  
Russ Cox committed
1519

Russ Cox's avatar
Russ Cox committed
1520 1521
	/*
	 * float to float
Russ Cox's avatar
Russ Cox committed
1522
	 */
Russ Cox's avatar
Russ Cox committed
1523 1524
	case CASE(TFLOAT32, TFLOAT32):
	case CASE(TFLOAT64, TFLOAT64):
Russ Cox's avatar
8g:  
Russ Cox committed
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
		// The way the code generator uses floating-point
		// registers, a move from F0 to F0 is intended as a no-op.
		// On the x86, it's not: it pushes a second copy of F0
		// on the floating point stack.  So toss it away here.
		// Also, F0 is the *only* register we ever evaluate
		// into, so we should only see register/register as F0/F0.
		if(f->op == OREGISTER && t->op == OREGISTER) {
			if(f->val.u.reg != D_F0 || t->val.u.reg != D_F0)
				goto fatal;
			return;
		}
		if(ismem(f) && ismem(t))
			goto hard;
		a = AFMOVF;
		if(ft == TFLOAT64)
			a = AFMOVD;
		if(ismem(t)) {
Russ Cox's avatar
Russ Cox committed
1542 1543
			if(f->op != OREGISTER || f->val.u.reg != D_F0)
				fatal("gmove %N", f);
Russ Cox's avatar
8g:  
Russ Cox committed
1544 1545 1546 1547
			a = AFMOVFP;
			if(ft == TFLOAT64)
				a = AFMOVDP;
		}
Russ Cox's avatar
Russ Cox committed
1548
		break;
Russ Cox's avatar
Russ Cox committed
1549

Russ Cox's avatar
Russ Cox committed
1550
	case CASE(TFLOAT32, TFLOAT64):
Russ Cox's avatar
Russ Cox committed
1551 1552 1553 1554 1555
		if(f->op == OREGISTER && t->op == OREGISTER) {
			if(f->val.u.reg != D_F0 || t->val.u.reg != D_F0)
				goto fatal;
			return;
		}
Russ Cox's avatar
8g:  
Russ Cox committed
1556
		if(f->op == OREGISTER)
Russ Cox's avatar
Russ Cox committed
1557
			gins(AFMOVDP, f, t);
Russ Cox's avatar
8g:  
Russ Cox committed
1558 1559 1560
		else
			gins(AFMOVF, f, t);
		return;
Russ Cox's avatar
Russ Cox committed
1561 1562

	case CASE(TFLOAT64, TFLOAT32):
Russ Cox's avatar
Russ Cox committed
1563 1564 1565 1566 1567 1568 1569
		if(f->op == OREGISTER && t->op == OREGISTER) {
			tempalloc(&r1, types[TFLOAT32]);
			gins(AFMOVFP, f, &r1);
			gins(AFMOVF, &r1, t);
			tempfree(&r1);
			return;
		}
Russ Cox's avatar
8g:  
Russ Cox committed
1570
		if(f->op == OREGISTER)
Russ Cox's avatar
Russ Cox committed
1571
			gins(AFMOVFP, f, t);
Russ Cox's avatar
8g:  
Russ Cox committed
1572 1573 1574
		else
			gins(AFMOVD, f, t);
		return;
Russ Cox's avatar
Russ Cox committed
1575 1576
	}

Russ Cox's avatar
Russ Cox committed
1577 1578
	gins(a, f, t);
	return;
Russ Cox's avatar
Russ Cox committed
1579

Russ Cox's avatar
Russ Cox committed
1580 1581 1582 1583 1584 1585 1586
rdst:
	// requires register destination
	regalloc(&r1, t->type, t);
	gins(a, f, &r1);
	gmove(&r1, t);
	regfree(&r1);
	return;
1587

Russ Cox's avatar
Russ Cox committed
1588 1589 1590 1591 1592 1593 1594
hard:
	// requires register intermediate
	regalloc(&r1, cvt, t);
	gmove(f, &r1);
	gmove(&r1, t);
	regfree(&r1);
	return;
Russ Cox's avatar
8g:  
Russ Cox committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

hardmem:
	// requires memory intermediate
	tempalloc(&r1, cvt);
	gmove(f, &r1);
	gmove(&r1, t);
	tempfree(&r1);
	return;

fatal:
	// should not happen
	fatal("gmove %N -> %N", f, t);
Russ Cox's avatar
Russ Cox committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
}

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)
{
	Prog *p;

Russ Cox's avatar
Russ Cox committed
1633 1634 1635 1636 1637 1638 1639
	switch(as) {
	case AMOVB:
	case AMOVW:
	case AMOVL:
		if(f != N && t != N && samaddr(f, t))
			return nil;
	}
Russ Cox's avatar
Russ Cox committed
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

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

/*
 * generate code to compute n;
 * make a refer to result.
 */
void
naddr(Node *n, Addr *a)
{
	a->scale = 0;
	a->index = D_NONE;
	a->type = D_NONE;
Russ Cox's avatar
Russ Cox committed
1661
	a->gotype = S;
Russ Cox's avatar
Russ Cox committed
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	if(n == N)
		return;

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

	case OREGISTER:
		a->type = n->val.u.reg;
		a->sym = S;
		break;

	case OINDREG:
		a->type = n->val.u.reg+D_INDIR;
		a->sym = n->sym;
		a->offset = n->xoffset;
		break;

	case OPARAM:
		// n->left is PHEAP ONAME for stack parameter.
		// compute address of actual parameter on stack.
		a->etype = n->left->type->etype;
		a->offset = n->xoffset;
		a->sym = n->left->sym;
		a->type = D_PARAM;
		break;

	case ONAME:
		a->etype = 0;
Russ Cox's avatar
Russ Cox committed
1692
		if(n->type != T) {
Russ Cox's avatar
Russ Cox committed
1693
			a->etype = simtype[n->type->etype];
Russ Cox's avatar
Russ Cox committed
1694 1695 1696
			if(n->sym != S && strncmp(n->sym->name, "autotmp_", 8) != 0)
				a->gotype = typename(n->type)->left->sym;
		}
Russ Cox's avatar
Russ Cox committed
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
		a->offset = n->xoffset;
		a->sym = n->sym;
		if(a->sym == S)
			a->sym = lookup(".noname");
		if(n->method) {
			if(n->type != T)
			if(n->type->sym != S)
			if(n->type->sym->package != nil)
				a->sym = pkglookup(a->sym->name, n->type->sym->package);
		}

		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:
		case PPARAMOUT:
			a->type = D_PARAM;
			break;
		case PFUNC:
			a->index = D_EXTERN;
			a->type = D_ADDR;
			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:
			a->sym = S;
			a->type = D_CONST;
			a->offset = mpgetfix(n->val.u.xval);
			break;
		case CTSTR:
			datagostring(n->val.u.sval, a);
			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;

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

1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
	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;

Russ Cox's avatar
Russ Cox committed
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
//	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
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
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;
}

Russ Cox's avatar
Russ Cox committed
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
void
sudoclean(void)
{
}

int
sudoaddable(int as, Node *n, Addr *a)
{
	return 0;
}