ggen.c 24 KB
Newer Older
1 2 3 4 5 6
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#undef	EXTERN
#define	EXTERN
Russ Cox's avatar
Russ Cox committed
7 8
#include <u.h>
#include <libc.h>
9
#include "gg.h"
10
#include "opt.h"
11

12 13
static Prog *appendpp(Prog*, int, int, vlong, int, vlong);

14
void
15
defframe(Prog *ptxt)
16
{
17
	uint32 frame;
18
	Prog *p;
19
	vlong i;
20 21
	NodeList *l;
	Node *n;
22

Ken Thompson's avatar
Ken Thompson committed
23
	// fill in argument size
Russ Cox's avatar
Russ Cox committed
24
	ptxt->to.offset = rnd(curfn->type->argwid, widthptr);
Ken Thompson's avatar
Ken Thompson committed
25

26
	// fill in final stack size
Ken Thompson's avatar
Ken Thompson committed
27
	ptxt->to.offset <<= 32;
28
	frame = rnd(stksize+maxarg, widthreg);
29
	ptxt->to.offset |= frame;
30 31 32 33
	
	// insert code to contain ambiguously live variables
	// so that garbage collector only sees initialized values
	// when it looks for pointers.
34 35 36 37
	//
	// TODO: determine best way to zero the given values.
	// among other problems, AX is initialized to 0 multiple times,
	// but that's really the tip of the iceberg.
38
	p = ptxt;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	for(l=curfn->dcl; l != nil; l = l->next) {
		n = l->n;
		if(!n->needzero)
			continue;
		if(n->class != PAUTO)
			fatal("needzero class %d", n->class);
		if(n->type->width % widthreg != 0 || n->xoffset % widthreg != 0 || n->type->width == 0)
			fatal("var %lN has size %d offset %d", n, (int)n->type->width, (int)n->xoffset);
		if(n->type->width <= 2*widthreg) {
			for(i = 0; i < n->type->width; i += widthreg)
				p = appendpp(p, AMOVQ, D_CONST, 0, D_SP+D_INDIR, frame+n->xoffset+i);
		} else if(n->type->width <= 16*widthreg) {
			p = appendpp(p, AMOVQ, D_CONST, 0, D_AX, 0);
			for(i = 0; i < n->type->width; i += widthreg)
				p = appendpp(p, AMOVQ, D_AX, 0, D_SP+D_INDIR, frame+n->xoffset+i);
		} else {
			p = appendpp(p, AMOVQ, D_CONST, 0, D_AX, 0);
			p = appendpp(p, AMOVQ, D_CONST, n->type->width/widthreg, D_CX, 0);
			p = appendpp(p, leaptr, D_SP+D_INDIR, frame+n->xoffset, D_DI, 0);
			p = appendpp(p, AREP, D_NONE, 0, D_NONE, 0);
			p = appendpp(p, ASTOSQ, D_NONE, 0, D_NONE, 0);
60
		}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	}
}

static Prog*	
appendpp(Prog *p, int as, int ftype, vlong foffset, int ttype, vlong toffset)	
{
	Prog *q;
	q = mal(sizeof(*q));	
	clearp(q);	
	q->as = as;	
	q->lineno = p->lineno;	
	q->from.type = ftype;	
	q->from.offset = foffset;	
	q->to.type = ttype;	
	q->to.offset = toffset;	
	q->link = p->link;	
	p->link = q;	
	return q;	
79 80
}

81 82 83 84 85
// Sweep the prog list to mark any used nodes.
void
markautoused(Prog* p)
{
	for (; p; p = p->link) {
86
		if (p->as == ATYPE || p->as == AVARDEF || p->as == AVARKILL)
87 88
			continue;

89
		if (p->from.node)
90
			p->from.node->used = 1;
91

92
		if (p->to.node)
93
			p->to.node->used = 1;
94 95 96
	}
}

97
// Fixup instructions after allocauto (formerly compactframe) has moved all autos around.
98
void
99
fixautoused(Prog *p)
100
{
101 102 103 104 105 106 107
	Prog **lp;

	for (lp=&p; (p=*lp) != P; ) {
		if (p->as == ATYPE && p->from.node && p->from.type == D_AUTO && !p->from.node->used) {
			*lp = p->link;
			continue;
		}
108
		if ((p->as == AVARDEF || p->as == AVARKILL) && p->to.node && !p->to.node->used) {
109 110 111 112 113 114 115 116 117
			// Cannot remove VARDEF instruction, because - unlike TYPE handled above -
			// VARDEFs are interspersed with other code, and a jump might be using the
			// VARDEF as a target. Replace with a no-op instead. A later pass will remove
			// the no-ops.
			p->to.type = D_NONE;
			p->to.node = N;
			p->as = ANOP;
			continue;
		}
118 119 120 121 122
		if (p->from.type == D_AUTO && p->from.node)
			p->from.offset += p->from.node->stkdelta;

		if (p->to.type == D_AUTO && p->to.node)
			p->to.offset += p->to.node->stkdelta;
123 124

		lp = &p->link;
125 126 127
	}
}

128

Russ Cox's avatar
Russ Cox committed
129 130 131
/*
 * generate:
 *	call f
132
 *	proc=-1	normal call but no return
Ken Thompson's avatar
defer  
Ken Thompson committed
133 134 135
 *	proc=0	normal call
 *	proc=1	goroutine run in new proc
 *	proc=2	defer call save away stack
136
  *	proc=3	normal call to C pointer (not Go func value)
Russ Cox's avatar
Russ Cox committed
137
 */
138
void
Ken Thompson's avatar
Ken Thompson committed
139 140
ginscall(Node *f, int proc)
{
141
	int32 arg;
Russ Cox's avatar
Russ Cox committed
142
	Prog *p;
Ken Thompson's avatar
Ken Thompson committed
143
	Node reg, con;
144
	Node r1;
Ken Thompson's avatar
Ken Thompson committed
145

146 147 148 149
	if(f->type != T)
		setmaxarg(f->type);

	arg = -1;
150 151 152 153 154 155
	// Most functions have a fixed-size argument block, so traceback uses that during unwind.
	// Not all, though: there are some variadic functions in package runtime,
	// and for those we emit call-specific metadata recorded by caller.
	// Reflect generates functions with variable argsize (see reflect.methodValueCall/makeFuncStub),
	// so we do this for all indirect calls as well.
	if(f->type != T && (f->sym == S || (f->sym != S && f->sym->pkg == runtimepkg) || proc == 1 || proc == 2)) {
156 157 158 159 160 161 162 163
		arg = f->type->argwid;
		if(proc == 1 || proc == 2)
			arg += 2*widthptr;
	}

	if(arg != -1)
		gargsize(arg);

Ken Thompson's avatar
defer  
Ken Thompson committed
164 165 166 167 168 169
	switch(proc) {
	default:
		fatal("ginscall: bad proc %d", proc);
		break;

	case 0:	// normal call
Russ Cox's avatar
Russ Cox committed
170
	case -1:	// normal call but no return
171
		if(f->op == ONAME && f->class == PFUNC) {
172 173 174 175 176 177 178 179 180 181 182 183 184
			if(f == deferreturn) {
				// Deferred calls will appear to be returning to
				// the CALL deferreturn(SB) that we are about to emit.
				// However, the stack trace code will show the line
				// of the instruction byte before the return PC. 
				// To avoid that being an unrelated instruction,
				// insert an x86 NOP that we will have the right line number.
				// x86 NOP 0x90 is really XCHG AX, AX; use that description
				// because the NOP pseudo-instruction would be removed by
				// the linker.
				nodreg(&reg, types[TINT], D_AX);
				gins(AXCHGL, &reg, &reg);
			}
185 186 187 188 189 190
			p = gins(ACALL, N, f);
			afunclit(&p->to, f);
			if(proc == -1 || noreturn(p))
				gins(AUNDEF, N, N);
			break;
		}
191
		nodreg(&reg, types[tptr], D_DX);
192 193 194 195
		nodreg(&r1, types[tptr], D_BX);
		gmove(f, &reg);
		reg.op = OINDREG;
		gmove(&reg, &r1);
196 197
		reg.op = OREGISTER;
		gins(ACALL, &reg, &r1);
198 199 200 201
		break;
	
	case 3:	// normal call of c function pointer
		gins(ACALL, N, f);
Ken Thompson's avatar
defer  
Ken Thompson committed
202 203 204
		break;

	case 1:	// call in new proc (go)
Russ Cox's avatar
Russ Cox committed
205
	case 2:	// deferred call (defer)
206 207 208
		nodconst(&con, types[TINT64], argsize(f->type));
		if(widthptr == 4) {
			nodreg(&r1, types[TINT32], D_CX);
209
			gmove(f, &r1);
210 211 212 213 214
			nodreg(&reg, types[TINT64], D_CX);
			nodconst(&r1, types[TINT64], 32);
			gins(ASHLQ, &r1, &reg);
			gins(AORQ, &con, &reg);
			gins(APUSHQ, &reg, N);
215
		} else {
216 217 218 219
			nodreg(&reg, types[TINT64], D_CX);
			gmove(f, &reg);
			gins(APUSHQ, &reg, N);
			gins(APUSHQ, &con, N);
220
		}
Ken Thompson's avatar
defer  
Ken Thompson committed
221
		if(proc == 1)
Russ Cox's avatar
Russ Cox committed
222
			ginscall(newproc, 0);
223 224 225
		else {
			if(!hasdefer)
				fatal("hasdefer=0 but has defer");
Russ Cox's avatar
Russ Cox committed
226
			ginscall(deferproc, 0);
227
		}
228
		nodreg(&reg, types[TINT64], D_CX);
229
		gins(APOPQ, N, &reg);
230 231
		if(widthptr == 8)
			gins(APOPQ, N, &reg);
Russ Cox's avatar
Russ Cox committed
232 233 234
		if(proc == 2) {
			nodreg(&reg, types[TINT64], D_AX);
			gins(ATESTQ, &reg, &reg);
Russ Cox's avatar
Russ Cox committed
235
			patch(gbranch(AJNE, T, -1), retpc);
Russ Cox's avatar
Russ Cox committed
236
		}
Ken Thompson's avatar
defer  
Ken Thompson committed
237
		break;
Ken Thompson's avatar
Ken Thompson committed
238
	}
239 240 241

	if(arg != -1)
		gargsize(-1);
Ken Thompson's avatar
Ken Thompson committed
242 243
}

Russ Cox's avatar
Russ Cox committed
244 245 246 247
/*
 * n is call to interface method.
 * generate res = n.
 */
Ken Thompson's avatar
Ken Thompson committed
248 249
void
cgen_callinter(Node *n, Node *res, int proc)
250 251
{
	Node *i, *f;
252
	Node tmpi, nodi, nodo, nodr, nodsp;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

	i = n->left;
	if(i->op != ODOTINTER)
		fatal("cgen_callinter: not ODOTINTER %O", i->op);

	f = i->right;		// field
	if(f->op != ONAME)
		fatal("cgen_callinter: not ONAME %O", f->op);

	i = i->left;		// interface

	if(!i->addable) {
		tempname(&tmpi, i->type);
		cgen(i, &tmpi);
		i = &tmpi;
	}

270
	genlist(n->list);		// assign the args
271

272 273 274
	// i is now addable, prepare an indirected
	// register to hold its address.
	igen(i, &nodi, res);		// REG = &inter
275 276

	nodindreg(&nodsp, types[tptr], D_SP);
277 278 279 280 281 282 283 284 285 286 287 288 289
	nodi.type = types[tptr];
	nodi.xoffset += widthptr;
	cgen(&nodi, &nodsp);	// 0(SP) = 8(REG) -- i.data

	regalloc(&nodo, types[tptr], res);
	nodi.type = types[tptr];
	nodi.xoffset -= widthptr;
	cgen(&nodi, &nodo);	// REG = 0(REG) -- i.tab
	regfree(&nodi);

	regalloc(&nodr, types[tptr], &nodo);
	if(n->left->xoffset == BADWIDTH)
		fatal("cgen_callinter: badwidth");
Russ Cox's avatar
Russ Cox committed
290
	cgen_checknil(&nodo); // in case offset is huge
291
	nodo.op = OINDREG;
292
	nodo.xoffset = n->left->xoffset + 3*widthptr + 8;
293 294 295 296 297 298 299 300
	if(proc == 0) {
		// plain call: use direct c function pointer - more efficient
		cgen(&nodo, &nodr);	// REG = 32+offset(REG) -- i.tab->fun[f]
		proc = 3;
	} else {
		// go/defer. generate go func value.
		gins(ALEAQ, &nodo, &nodr);	// REG = &(32+offset(REG)) -- i.tab->fun[f]
	}
301

302
	nodr.type = n->left->type;
Ken Thompson's avatar
Ken Thompson committed
303 304
	ginscall(&nodr, proc);

305
	regfree(&nodr);
Ken Thompson's avatar
Ken Thompson committed
306
	regfree(&nodo);
307 308
}

Russ Cox's avatar
Russ Cox committed
309 310
/*
 * generate function call;
Ken Thompson's avatar
defer  
Ken Thompson committed
311 312 313
 *	proc=0	normal call
 *	proc=1	goroutine run in new proc
 *	proc=2	defer call save away stack
Russ Cox's avatar
Russ Cox committed
314
 */
315
void
Ken Thompson's avatar
Ken Thompson committed
316
cgen_call(Node *n, int proc)
317 318
{
	Type *t;
Russ Cox's avatar
Russ Cox committed
319
	Node nod, afun;
320 321 322 323 324 325 326 327

	if(n == N)
		return;

	if(n->left->ullman >= UINF) {
		// if name involves a fn call
		// precompute the address of the fn
		tempname(&afun, types[tptr]);
Russ Cox's avatar
Russ Cox committed
328
		cgen(n->left, &afun);
329 330
	}

331
	genlist(n->list);		// assign the args
332 333 334 335 336
	t = n->left->type;

	// call tempname pointer
	if(n->left->ullman >= UINF) {
		regalloc(&nod, types[tptr], N);
Russ Cox's avatar
Russ Cox committed
337
		cgen_as(&nod, &afun);
Ken Thompson's avatar
Ken Thompson committed
338
		nod.type = t;
Ken Thompson's avatar
Ken Thompson committed
339
		ginscall(&nod, proc);
340
		regfree(&nod);
341
		return;
342 343 344
	}

	// call pointer
Russ Cox's avatar
Russ Cox committed
345
	if(n->left->op != ONAME || n->left->class != PFUNC) {
346
		regalloc(&nod, types[tptr], N);
Russ Cox's avatar
Russ Cox committed
347
		cgen_as(&nod, n->left);
Ken Thompson's avatar
Ken Thompson committed
348
		nod.type = t;
Ken Thompson's avatar
Ken Thompson committed
349
		ginscall(&nod, proc);
350
		regfree(&nod);
351
		return;
352 353 354
	}

	// call direct
Ken Thompson's avatar
Ken Thompson committed
355
	n->left->method = 1;
Ken Thompson's avatar
Ken Thompson committed
356
	ginscall(n->left, proc);
357 358
}

Russ Cox's avatar
Russ Cox committed
359 360 361 362 363
/*
 * call to n has already been generated.
 * generate:
 *	res = return value from call.
 */
364 365 366 367 368 369 370 371 372 373 374 375 376
void
cgen_callret(Node *n, Node *res)
{
	Node nod;
	Type *fp, *t;
	Iter flist;

	t = n->left->type;
	if(t->etype == TPTR32 || t->etype == TPTR64)
		t = t->type;

	fp = structfirst(&flist, getoutarg(t));
	if(fp == T)
Ken Thompson's avatar
Ken Thompson committed
377
		fatal("cgen_callret: nil");
378 379 380

	memset(&nod, 0, sizeof(nod));
	nod.op = OINDREG;
Ken Thompson's avatar
Ken Thompson committed
381
	nod.val.u.reg = D_SP;
382 383 384 385
	nod.addable = 1;

	nod.xoffset = fp->width;
	nod.type = fp->type;
Russ Cox's avatar
Russ Cox committed
386
	cgen_as(res, &nod);
387 388
}

Russ Cox's avatar
Russ Cox committed
389 390 391 392 393
/*
 * call to n has already been generated.
 * generate:
 *	res = &return value from call.
 */
394 395 396
void
cgen_aret(Node *n, Node *res)
{
Ken Thompson's avatar
Ken Thompson committed
397
	Node nod1, nod2;
398 399 400 401
	Type *fp, *t;
	Iter flist;

	t = n->left->type;
Ken Thompson's avatar
Ken Thompson committed
402
	if(isptr[t->etype])
403 404 405 406 407
		t = t->type;

	fp = structfirst(&flist, getoutarg(t));
	if(fp == T)
		fatal("cgen_aret: nil");
Ken Thompson's avatar
Ken Thompson committed
408

409 410
	memset(&nod1, 0, sizeof(nod1));
	nod1.op = OINDREG;
Ken Thompson's avatar
Ken Thompson committed
411
	nod1.val.u.reg = D_SP;
412 413 414 415 416
	nod1.addable = 1;

	nod1.xoffset = fp->width;
	nod1.type = fp->type;

Ken Thompson's avatar
Ken Thompson committed
417 418
	if(res->op != OREGISTER) {
		regalloc(&nod2, types[tptr], res);
419 420
		gins(leaptr, &nod1, &nod2);
		gins(movptr, &nod2, res);
Ken Thompson's avatar
Ken Thompson committed
421
		regfree(&nod2);
Ken Thompson's avatar
Ken Thompson committed
422
	} else
423
		gins(leaptr, &nod1, res);
424 425
}

Russ Cox's avatar
Russ Cox committed
426 427 428 429
/*
 * generate return.
 * n->left is assignments to return values.
 */
430 431 432
void
cgen_ret(Node *n)
{
433 434
	Prog *p;

435
	genlist(n->list);		// copy out args
436
	if(hasdefer || curfn->exit) {
437
		gjmp(retpc);
438 439 440 441 442
		return;
	}
	p = gins(ARET, N, N);
	if(n->op == ORETJMP) {
		p->to.type = D_EXTERN;
443
		p->to.sym = linksym(n->left->sym);
444
	}
445 446
}

Russ Cox's avatar
Russ Cox committed
447 448 449
/*
 * generate += *= etc.
 */
450
void
Ken Thompson's avatar
Ken Thompson committed
451
cgen_asop(Node *n)
452
{
Ken Thompson's avatar
Ken Thompson committed
453 454
	Node n1, n2, n3, n4;
	Node *nl, *nr;
Ken Thompson's avatar
Ken Thompson committed
455 456
	Prog *p1;
	Addr addr;
Ken Thompson's avatar
Ken Thompson committed
457
	int a;
Ken Thompson's avatar
Ken Thompson committed
458

Ken Thompson's avatar
Ken Thompson committed
459 460
	nl = n->left;
	nr = n->right;
461

Ken Thompson's avatar
Ken Thompson committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475
	if(nr->ullman >= UINF && nl->ullman >= UINF) {
		tempname(&n1, nr->type);
		cgen(nr, &n1);
		n2 = *n;
		n2.right = &n1;
		cgen_asop(&n2);
		goto ret;
	}

	if(!isint[nl->type->etype])
		goto hard;
	if(!isint[nr->type->etype])
		goto hard;

Ken Thompson's avatar
Ken Thompson committed
476 477
	switch(n->etype) {
	case OADD:
Ken Thompson's avatar
Ken Thompson committed
478 479
		if(smallintconst(nr))
		if(mpgetfix(nr->val.u.xval) == 1) {
Ken Thompson's avatar
Ken Thompson committed
480
			a = optoas(OINC, nl->type);
Ken Thompson's avatar
Ken Thompson committed
481
			if(nl->addable) {
Ken Thompson's avatar
Ken Thompson committed
482
				gins(a, N, nl);
Ken Thompson's avatar
Ken Thompson committed
483 484
				goto ret;
			}
Ken Thompson's avatar
Ken Thompson committed
485 486
			if(sudoaddable(a, nl, &addr)) {
				p1 = gins(a, N, N);
Ken Thompson's avatar
Ken Thompson committed
487 488 489 490 491 492 493
				p1->to = addr;
				sudoclean();
				goto ret;
			}
		}
		break;

Ken Thompson's avatar
Ken Thompson committed
494
	case OSUB:
Ken Thompson's avatar
Ken Thompson committed
495 496
		if(smallintconst(nr))
		if(mpgetfix(nr->val.u.xval) == 1) {
Ken Thompson's avatar
Ken Thompson committed
497
			a = optoas(ODEC, nl->type);
Ken Thompson's avatar
Ken Thompson committed
498
			if(nl->addable) {
Ken Thompson's avatar
Ken Thompson committed
499
				gins(a, N, nl);
Ken Thompson's avatar
Ken Thompson committed
500 501
				goto ret;
			}
Ken Thompson's avatar
Ken Thompson committed
502 503
			if(sudoaddable(a, nl, &addr)) {
				p1 = gins(a, N, N);
Ken Thompson's avatar
Ken Thompson committed
504 505 506 507 508 509
				p1->to = addr;
				sudoclean();
				goto ret;
			}
		}
		break;
510
	}
Ken Thompson's avatar
Ken Thompson committed
511

512
	switch(n->etype) {
Ken Thompson's avatar
Ken Thompson committed
513 514
	case OADD:
	case OSUB:
Ken Thompson's avatar
Ken Thompson committed
515 516 517
	case OXOR:
	case OAND:
	case OOR:
Ken Thompson's avatar
Ken Thompson committed
518
		a = optoas(n->etype, nl->type);
Ken Thompson's avatar
Ken Thompson committed
519 520
		if(nl->addable) {
			if(smallintconst(nr)) {
Ken Thompson's avatar
Ken Thompson committed
521
				gins(a, nr, nl);
Ken Thompson's avatar
Ken Thompson committed
522 523 524 525
				goto ret;
			}
			regalloc(&n2, nr->type, N);
			cgen(nr, &n2);
Ken Thompson's avatar
Ken Thompson committed
526
			gins(a, &n2, nl);
Ken Thompson's avatar
Ken Thompson committed
527 528 529 530
			regfree(&n2);
			goto ret;
		}
		if(nr->ullman < UINF)
Ken Thompson's avatar
Ken Thompson committed
531
		if(sudoaddable(a, nl, &addr)) {
Ken Thompson's avatar
Ken Thompson committed
532
			if(smallintconst(nr)) {
Ken Thompson's avatar
Ken Thompson committed
533
				p1 = gins(a, nr, N);
Ken Thompson's avatar
Ken Thompson committed
534 535 536 537 538 539
				p1->to = addr;
				sudoclean();
				goto ret;
			}
			regalloc(&n2, nr->type, N);
			cgen(nr, &n2);
Ken Thompson's avatar
Ken Thompson committed
540
			p1 = gins(a, &n2, N);
Ken Thompson's avatar
Ken Thompson committed
541 542 543 544 545
			p1->to = addr;
			regfree(&n2);
			sudoclean();
			goto ret;
		}
Ken Thompson's avatar
Ken Thompson committed
546
	}
547

Ken Thompson's avatar
Ken Thompson committed
548
hard:
549 550
	n2.op = 0;
	n1.op = 0;
551 552 553
	if(nr->op == OLITERAL) {
		// don't allocate a register for literals.
	} else if(nr->ullman >= nl->ullman || nl->addable) {
Ken Thompson's avatar
Ken Thompson committed
554
		regalloc(&n2, nr->type, N);
Ken Thompson's avatar
Ken Thompson committed
555
		cgen(nr, &n2);
556
		nr = &n2;
Ken Thompson's avatar
Ken Thompson committed
557
	} else {
558
		tempname(&n2, nr->type);
Ken Thompson's avatar
Ken Thompson committed
559
		cgen(nr, &n2);
560 561 562 563 564
		nr = &n2;
	}
	if(!nl->addable) {
		igen(nl, &n1, N);
		nl = &n1;
565 566
	}

Ken Thompson's avatar
Ken Thompson committed
567
	n3 = *n;
568 569
	n3.left = nl;
	n3.right = nr;
Ken Thompson's avatar
Ken Thompson committed
570 571
	n3.op = n->etype;

Ken Thompson's avatar
Ken Thompson committed
572
	regalloc(&n4, nl->type, N);
Ken Thompson's avatar
Ken Thompson committed
573
	cgen(&n3, &n4);
574
	gmove(&n4, nl);
Ken Thompson's avatar
Ken Thompson committed
575

576 577 578 579
	if(n1.op)
		regfree(&n1);
	if(n2.op == OREGISTER)
		regfree(&n2);
Ken Thompson's avatar
Ken Thompson committed
580
	regfree(&n4);
Ken Thompson's avatar
Ken Thompson committed
581 582 583

ret:
	;
584 585
}

Ken Thompson's avatar
Ken Thompson committed
586 587 588
int
samereg(Node *a, Node *b)
{
Russ Cox's avatar
Russ Cox committed
589 590
	if(a == N || b == N)
		return 0;
Ken Thompson's avatar
Ken Thompson committed
591 592 593 594
	if(a->op != OREGISTER)
		return 0;
	if(b->op != OREGISTER)
		return 0;
Ken Thompson's avatar
Ken Thompson committed
595
	if(a->val.u.reg != b->val.u.reg)
Ken Thompson's avatar
Ken Thompson committed
596 597 598 599
		return 0;
	return 1;
}

Russ Cox's avatar
Russ Cox committed
600 601 602 603 604 605 606
/*
 * generate division.
 * generates one of:
 *	res = nl / nr
 *	res = nl % nr
 * according to op.
 */
Ken Thompson's avatar
Ken Thompson committed
607
void
Russ Cox's avatar
Russ Cox committed
608
dodiv(int op, Node *nl, Node *nr, Node *res)
Ken Thompson's avatar
Ken Thompson committed
609
{
Russ Cox's avatar
Russ Cox committed
610
	int a, check;
611
	Node n3, n4;
Russ Cox's avatar
Russ Cox committed
612 613
	Type *t, *t0;
	Node ax, dx, ax1, n31, oldax, olddx;
614
	Prog *p1, *p2;
Russ Cox's avatar
Russ Cox committed
615 616 617 618 619 620 621 622 623

	// Have to be careful about handling
	// most negative int divided by -1 correctly.
	// The hardware will trap.
	// Also the byte divide instruction needs AH,
	// which we otherwise don't have to deal with.
	// Easiest way to avoid for int8, int16: use int32.
	// For int32 and int64, use explicit test.
	// Could use int64 hw for int32.
624
	t = nl->type;
Russ Cox's avatar
Russ Cox committed
625
	t0 = t;
Russ Cox's avatar
Russ Cox committed
626 627 628
	check = 0;
	if(issigned[t->etype]) {
		check = 1;
629
		if(isconst(nl, CTINT) && mpgetfix(nl->val.u.xval) != -(1ULL<<(t->width*8-1)))
Russ Cox's avatar
Russ Cox committed
630 631 632 633 634
			check = 0;
		else if(isconst(nr, CTINT) && mpgetfix(nr->val.u.xval) != -1)
			check = 0;
	}
	if(t->width < 4) {
635 636 637 638
		if(issigned[t->etype])
			t = types[TINT32];
		else
			t = types[TUINT32];
Russ Cox's avatar
Russ Cox committed
639
		check = 0;
640 641
	}
	a = optoas(op, t);
Ken Thompson's avatar
Ken Thompson committed
642

Russ Cox's avatar
Russ Cox committed
643
	regalloc(&n3, t0, N);
Ken Thompson's avatar
Ken Thompson committed
644
	if(nl->ullman >= nr->ullman) {
Russ Cox's avatar
Russ Cox committed
645
		savex(D_AX, &ax, &oldax, res, t0);
Russ Cox's avatar
Russ Cox committed
646
		cgen(nl, &ax);
Russ Cox's avatar
Russ Cox committed
647
		regalloc(&ax, t0, &ax);	// mark ax live during cgen
Ken Thompson's avatar
Ken Thompson committed
648
		cgen(nr, &n3);
Russ Cox's avatar
Russ Cox committed
649
		regfree(&ax);
Ken Thompson's avatar
Ken Thompson committed
650 651
	} else {
		cgen(nr, &n3);
Russ Cox's avatar
Russ Cox committed
652
		savex(D_AX, &ax, &oldax, res, t0);
Russ Cox's avatar
Russ Cox committed
653
		cgen(nl, &ax);
Ken Thompson's avatar
Ken Thompson committed
654
	}
Russ Cox's avatar
Russ Cox committed
655 656 657 658 659 660 661 662 663 664
	if(t != t0) {
		// Convert
		ax1 = ax;
		n31 = n3;
		ax.type = t;
		n3.type = t;
		gmove(&ax1, &ax);
		gmove(&n31, &n3);
	}

665
	p2 = P;
666 667 668 669 670 671 672 673 674 675 676 677
	if(nacl) {
		// Native Client does not relay the divide-by-zero trap
		// to the executing program, so we must insert a check
		// for ourselves.
		nodconst(&n4, t, 0);
		gins(optoas(OCMP, t), &n3, &n4);
		p1 = gbranch(optoas(ONE, t), T, +1);
		if(panicdiv == N)
			panicdiv = sysfunc("panicdivide");
		ginscall(panicdiv, -1);
		patch(p1, pc);
	}
Russ Cox's avatar
Russ Cox committed
678 679 680
	if(check) {
		nodconst(&n4, t, -1);
		gins(optoas(OCMP, t), &n3, &n4);
Russ Cox's avatar
Russ Cox committed
681
		p1 = gbranch(optoas(ONE, t), T, +1);
682 683 684 685 686 687
		if(op == ODIV) {
			// a / (-1) is -a.
			gins(optoas(OMINUS, t), N, &ax);
			gmove(&ax, res);
		} else {
			// a % (-1) is 0.
Russ Cox's avatar
Russ Cox committed
688 689 690
			nodconst(&n4, t, 0);
			gmove(&n4, res);
		}
691
		p2 = gbranch(AJMP, T, 0);
Russ Cox's avatar
Russ Cox committed
692 693
		patch(p1, pc);
	}
Russ Cox's avatar
Russ Cox committed
694 695 696 697 698 699
	savex(D_DX, &dx, &olddx, res, t);
	if(!issigned[t->etype]) {
		nodconst(&n4, t, 0);
		gmove(&n4, &dx);
	} else
		gins(optoas(OEXTEND, t), N, N);
Ken Thompson's avatar
Ken Thompson committed
700
	gins(a, &n3, N);
Ken Thompson's avatar
Ken Thompson committed
701
	regfree(&n3);
Ken Thompson's avatar
Ken Thompson committed
702
	if(op == ODIV)
Russ Cox's avatar
Russ Cox committed
703
		gmove(&ax, res);
Ken Thompson's avatar
Ken Thompson committed
704
	else
Russ Cox's avatar
Russ Cox committed
705 706
		gmove(&dx, res);
	restx(&dx, &olddx);
Russ Cox's avatar
Russ Cox committed
707
	if(check)
708
		patch(p2, pc);
Russ Cox's avatar
Russ Cox committed
709
	restx(&ax, &oldax);
Ken Thompson's avatar
Ken Thompson committed
710
}
Ken Thompson's avatar
Ken Thompson committed
711

Russ Cox's avatar
Russ Cox committed
712 713 714 715 716 717 718 719 720 721 722
/*
 * register dr is one of the special ones (AX, CX, DI, SI, etc.).
 * we need to use it.  if it is already allocated as a temporary
 * (r > 1; can only happen if a routine like sgen passed a
 * special as cgen's res and then cgen used regalloc to reuse
 * it as its own temporary), then move it for now to another
 * register.  caller must call restx to move it back.
 * the move is not necessary if dr == res, because res is
 * known to be dead.
 */
void
723 724 725 726 727 728 729 730 731
savex(int dr, Node *x, Node *oldx, Node *res, Type *t)
{
	int r;

	r = reg[dr];

	// save current ax and dx if they are live
	// and not the destination
	memset(oldx, 0, sizeof *oldx);
Russ Cox's avatar
Russ Cox committed
732 733
	nodreg(x, t, dr);
	if(r > 1 && !samereg(x, res)) {
Russ Cox's avatar
Russ Cox committed
734
		regalloc(oldx, types[TINT64], N);
Russ Cox's avatar
Russ Cox committed
735
		x->type = types[TINT64];
736
		gmove(x, oldx);
Russ Cox's avatar
Russ Cox committed
737 738 739
		x->type = t;
		oldx->ostk = r;	// squirrel away old r value
		reg[dr] = 1;
740 741 742
	}
}

Russ Cox's avatar
Russ Cox committed
743
void
744 745 746
restx(Node *x, Node *oldx)
{
	if(oldx->op != 0) {
Russ Cox's avatar
Russ Cox committed
747
		x->type = types[TINT64];
Russ Cox's avatar
Russ Cox committed
748
		reg[x->val.u.reg] = oldx->ostk;
749 750 751 752 753
		gmove(oldx, x);
		regfree(oldx);
	}
}

Russ Cox's avatar
Russ Cox committed
754 755 756 757 758
/*
 * generate division according to op, one of:
 *	res = nl / nr
 *	res = nl % nr
 */
Ken Thompson's avatar
Ken Thompson committed
759 760 761
void
cgen_div(int op, Node *nl, Node *nr, Node *res)
{
762 763
	Node n1, n2, n3;
	int w, a;
764
	Magic m;
Ken Thompson's avatar
Ken Thompson committed
765

766 767 768 769
	if(nr->op != OLITERAL)
		goto longdiv;
	w = nl->type->width*8;

770
	// Front end handled 32-bit division. We only need to handle 64-bit.
Ken Thompson's avatar
Ken Thompson committed
771 772
	// try to do division by multiply by (2^w)/d
	// see hacker's delight chapter 10
773 774 775 776 777
	switch(simtype[nl->type->etype]) {
	default:
		goto longdiv;

	case TUINT64:
778 779 780 781 782
		m.w = w;
		m.ud = mpgetfix(nr->val.u.xval);
		umagic(&m);
		if(m.bad)
			break;
Ken Thompson's avatar
Ken Thompson committed
783 784
		if(op == OMOD)
			goto longmod;
785

786
		cgenr(nl, &n1, N);
787
		nodconst(&n2, nl->type, m.um);
788 789
		regalloc(&n3, nl->type, res);
		cgen_hmul(&n1, &n2, &n3);
790

791 792
		if(m.ua) {
			// need to add numerator accounting for overflow
793
			gins(optoas(OADD, nl->type), &n1, &n3);
794
			nodconst(&n2, nl->type, 1);
795
			gins(optoas(ORROTC, nl->type), &n2, &n3);
796
			nodconst(&n2, nl->type, m.s-1);
797
			gins(optoas(ORSH, nl->type), &n2, &n3);
798 799
		} else {
			nodconst(&n2, nl->type, m.s);
800
			gins(optoas(ORSH, nl->type), &n2, &n3);	// shift dx
801 802
		}

803
		gmove(&n3, res);
804
		regfree(&n1);
805
		regfree(&n3);
806
		return;
807

808 809 810 811 812 813
	case TINT64:
		m.w = w;
		m.sd = mpgetfix(nr->val.u.xval);
		smagic(&m);
		if(m.bad)
			break;
Ken Thompson's avatar
Ken Thompson committed
814 815
		if(op == OMOD)
			goto longmod;
816

817
		cgenr(nl, &n1, res);
818
		nodconst(&n2, nl->type, m.sm);
819 820
		regalloc(&n3, nl->type, N);
		cgen_hmul(&n1, &n2, &n3);
Russ Cox's avatar
Russ Cox committed
821

822 823
		if(m.sm < 0) {
			// need to add numerator
824
			gins(optoas(OADD, nl->type), &n1, &n3);
825 826
		}

827
		nodconst(&n2, nl->type, m.s);
828
		gins(optoas(ORSH, nl->type), &n2, &n3);	// shift n3
829 830 831

		nodconst(&n2, nl->type, w-1);
		gins(optoas(ORSH, nl->type), &n2, &n1);	// -1 iff num is neg
832
		gins(optoas(OSUB, nl->type), &n1, &n3);	// added
833

834 835 836
		if(m.sd < 0) {
			// this could probably be removed
			// by factoring it into the multiplier
837
			gins(optoas(OMINUS, nl->type), N, &n3);
838
		}
839

840
		gmove(&n3, res);
841
		regfree(&n1);
842
		regfree(&n3);
843
		return;
Russ Cox's avatar
Russ Cox committed
844
	}
845
	goto longdiv;
Russ Cox's avatar
Russ Cox committed
846

847
longdiv:
Ken Thompson's avatar
Ken Thompson committed
848
	// division and mod using (slow) hardware instruction
Russ Cox's avatar
Russ Cox committed
849
	dodiv(op, nl, nr, res);
Ken Thompson's avatar
Ken Thompson committed
850 851 852 853 854 855 856 857 858
	return;

longmod:
	// mod using formula A%B = A-(A/B*B) but
	// we know that there is a fast algorithm for A/B
	regalloc(&n1, nl->type, res);
	cgen(nl, &n1);
	regalloc(&n2, nl->type, N);
	cgen_div(ODIV, &n1, nr, &n2);
Russ Cox's avatar
Russ Cox committed
859 860 861 862 863 864
	a = optoas(OMUL, nl->type);
	if(w == 8) {
		// use 2-operand 16-bit multiply
		// because there is no 2-operand 8-bit multiply
		a = AIMULW;
	}
Ken Thompson's avatar
Ken Thompson committed
865 866 867
	if(!smallintconst(nr)) {
		regalloc(&n3, nl->type, N);
		cgen(nr, &n3);
Russ Cox's avatar
Russ Cox committed
868
		gins(a, &n3, &n2);
Ken Thompson's avatar
Ken Thompson committed
869 870
		regfree(&n3);
	} else
Russ Cox's avatar
Russ Cox committed
871
		gins(a, nr, &n2);
Ken Thompson's avatar
Ken Thompson committed
872 873 874 875
	gins(optoas(OSUB, nl->type), &n2, &n1);
	gmove(&n1, res);
	regfree(&n1);
	regfree(&n2);
Ken Thompson's avatar
Ken Thompson committed
876
}
877

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
/*
 * generate high multiply:
 *   res = (nl*nr) >> width
 */
void
cgen_hmul(Node *nl, Node *nr, Node *res)
{
	Type *t;
	int a;
	Node n1, n2, ax, dx, *tmp;

	t = nl->type;
	a = optoas(OHMUL, t);
	if(nl->ullman < nr->ullman) {
		tmp = nl;
		nl = nr;
		nr = tmp;
	}
	cgenr(nl, &n1, res);
	cgenr(nr, &n2, N);
	nodreg(&ax, t, D_AX);
	gmove(&n1, &ax);
	gins(a, &n2, N);
	regfree(&n2);
	regfree(&n1);

	if(t->width == 1) {
		// byte multiply behaves differently.
		nodreg(&ax, t, D_AH);
		nodreg(&dx, t, D_DL);
		gmove(&ax, &dx);
	}
	nodreg(&dx, t, D_DX);
	gmove(&dx, res);
}

Russ Cox's avatar
Russ Cox committed
914 915 916 917 918
/*
 * generate shift according to op, one of:
 *	res = nl << nr
 *	res = nl >> nr
 */
919
void
920
cgen_shift(int op, int bounded, Node *nl, Node *nr, Node *res)
921
{
922 923
	Node n1, n2, n3, n4, n5, cx, oldcx;
	int a, rcx;
Ken Thompson's avatar
Ken Thompson committed
924
	Prog *p1;
Ken Thompson's avatar
Ken Thompson committed
925
	uvlong sc;
926
	Type *tcount;
927 928 929 930

	a = optoas(op, nl->type);

	if(nr->op == OLITERAL) {
Ken Thompson's avatar
Ken Thompson committed
931
		regalloc(&n1, nl->type, res);
932
		cgen(nl, &n1);
Ken Thompson's avatar
Ken Thompson committed
933 934
		sc = mpgetfix(nr->val.u.xval);
		if(sc >= nl->type->width*8) {
935
			// large shift gets 2 shifts by width-1
Ken Thompson's avatar
Ken Thompson committed
936 937 938 939 940
			nodconst(&n3, types[TUINT32], nl->type->width*8-1);
			gins(a, &n3, &n1);
			gins(a, &n3, &n1);
		} else
			gins(a, nr, &n1);
941 942
		gmove(&n1, res);
		regfree(&n1);
Ken Thompson's avatar
Ken Thompson committed
943
		goto ret;
944 945
	}

946 947 948 949 950 951 952 953 954 955 956 957
	if(nl->ullman >= UINF) {
		tempname(&n4, nl->type);
		cgen(nl, &n4);
		nl = &n4;
	}
	if(nr->ullman >= UINF) {
		tempname(&n5, nr->type);
		cgen(nr, &n5);
		nr = &n5;
	}

	rcx = reg[D_CX];
958
	nodreg(&n1, types[TUINT32], D_CX);
959 960 961 962 963 964 965 966
	
	// Allow either uint32 or uint64 as shift type,
	// to avoid unnecessary conversion from uint32 to uint64
	// just to do the comparison.
	tcount = types[simtype[nr->type->etype]];
	if(tcount->etype < TUINT32)
		tcount = types[TUINT32];

967
	regalloc(&n1, nr->type, &n1);		// to hold the shift type in CX
968
	regalloc(&n3, tcount, &n1);	// to clear high bits of CX
969

970 971 972 973 974 975
	nodreg(&cx, types[TUINT64], D_CX);
	memset(&oldcx, 0, sizeof oldcx);
	if(rcx > 0 && !samereg(&cx, res)) {
		regalloc(&oldcx, types[TUINT64], N);
		gmove(&cx, &oldcx);
	}
976
	cx.type = tcount;
977 978 979 980 981

	if(samereg(&cx, res))
		regalloc(&n2, nl->type, N);
	else
		regalloc(&n2, nl->type, res);
982 983 984
	if(nl->ullman >= nr->ullman) {
		cgen(nl, &n2);
		cgen(nr, &n1);
985
		gmove(&n1, &n3);
986 987
	} else {
		cgen(nr, &n1);
988
		gmove(&n1, &n3);
989 990
		cgen(nl, &n2);
	}
991 992
	regfree(&n3);

Ken Thompson's avatar
Ken Thompson committed
993
	// test and fix up large shifts
994 995 996
	if(!bounded) {
		nodconst(&n3, tcount, nl->type->width*8);
		gins(optoas(OCMP, tcount), &n1, &n3);
Russ Cox's avatar
Russ Cox committed
997
		p1 = gbranch(optoas(OLT, tcount), T, +1);
998 999 1000 1001 1002 1003 1004 1005
		if(op == ORSH && issigned[nl->type->etype]) {
			nodconst(&n3, types[TUINT32], nl->type->width*8-1);
			gins(a, &n3, &n2);
		} else {
			nodconst(&n3, nl->type, 0);
			gmove(&n3, &n2);
		}
		patch(p1, pc);
Ken Thompson's avatar
Ken Thompson committed
1006
	}
1007

1008
	gins(a, &n1, &n2);
Ken Thompson's avatar
Ken Thompson committed
1009

1010
	if(oldcx.op != 0) {
1011
		cx.type = types[TUINT64];
1012 1013 1014 1015
		gmove(&oldcx, &cx);
		regfree(&oldcx);
	}

1016 1017 1018 1019
	gmove(&n2, res);

	regfree(&n1);
	regfree(&n2);
Ken Thompson's avatar
Ken Thompson committed
1020 1021

ret:
Ken Thompson's avatar
Ken Thompson committed
1022
	;
1023
}
1024

Russ Cox's avatar
Russ Cox committed
1025 1026 1027
/*
 * generate byte multiply:
 *	res = nl * nr
1028 1029
 * there is no 2-operand byte multiply instruction so
 * we do a full-width multiplication and truncate afterwards.
Russ Cox's avatar
Russ Cox committed
1030
 */
Ken Thompson's avatar
Ken Thompson committed
1031 1032 1033
void
cgen_bmul(int op, Node *nl, Node *nr, Node *res)
{
1034
	Node n1, n2, n1b, n2b, *tmp;
Ken Thompson's avatar
Ken Thompson committed
1035 1036 1037
	Type *t;
	int a;

1038 1039 1040 1041 1042 1043
	// largest ullman on left.
	if(nl->ullman < nr->ullman) {
		tmp = nl;
		nl = nr;
		nr = tmp;
	}
Ken Thompson's avatar
Ken Thompson committed
1044

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	// generate operands in "8-bit" registers.
	regalloc(&n1b, nl->type, res);
	cgen(nl, &n1b);
	regalloc(&n2b, nr->type, N);
	cgen(nr, &n2b);

	// perform full-width multiplication.
	t = types[TUINT64];
	if(issigned[nl->type->etype])
		t = types[TINT64];
	nodreg(&n1, t, n1b.val.u.reg);
	nodreg(&n2, t, n2b.val.u.reg);
Ken Thompson's avatar
Ken Thompson committed
1057
	a = optoas(op, t);
1058
	gins(a, &n2, &n1);
1059 1060

	// truncate.
1061
	gmove(&n1, res);
1062 1063
	regfree(&n1b);
	regfree(&n2b);
Ken Thompson's avatar
Ken Thompson committed
1064 1065
}

1066
void
1067
clearfat(Node *nl)
1068
{
1069
	int64 w, c, q;
1070 1071
	Node n1, oldn1, ax, oldax, di, z;
	Prog *p;
1072

1073 1074 1075
	/* clear a fat object */
	if(debug['g'])
		dump("\nclearfat", nl);
1076

1077
	w = nl->type->width;
1078 1079 1080
	// Avoid taking the address for simple enough types.
	if(componentgen(N, nl))
		return;
1081

1082 1083 1084
	c = w % 8;	// bytes
	q = w / 8;	// quads

Russ Cox's avatar
Russ Cox committed
1085
	savex(D_DI, &n1, &oldn1, N, types[tptr]);
1086 1087
	agen(nl, &n1);

Russ Cox's avatar
Russ Cox committed
1088
	savex(D_AX, &ax, &oldax, N, types[tptr]);
1089
	gconreg(AMOVL, 0, D_AX);
Russ Cox's avatar
Russ Cox committed
1090

1091
	if(q >= 4) {
1092
		gconreg(movptr, q, D_CX);
1093 1094 1095 1096 1097 1098 1099 1100
		gins(AREP, N, N);	// repeat
		gins(ASTOSQ, N, N);	// STOQ AL,*(DI)+
	} else
	while(q > 0) {
		gins(ASTOSQ, N, N);	// STOQ AL,*(DI)+
		q--;
	}

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	z = ax;
	di = n1;
	if(w >= 8 && c >= 4) {
		di.op = OINDREG;
		di.type = z.type = types[TINT64];
		p = gins(AMOVQ, &z, &di);
		p->to.scale = 1;
		p->to.offset = c-8;
	} else if(c >= 4) {
		di.op = OINDREG;
		di.type = z.type = types[TINT32];
		p = gins(AMOVL, &z, &di);
		if(c > 4) {
			p = gins(AMOVL, &z, &di);
			p->to.scale = 1;
			p->to.offset = c-4;
		}
1118 1119 1120 1121
	} else
	while(c > 0) {
		gins(ASTOSB, N, N);	// STOB AL,*(DI)+
		c--;
1122
	}
Russ Cox's avatar
Russ Cox committed
1123 1124 1125

	restx(&n1, &oldn1);
	restx(&ax, &oldax);
1126
}
Russ Cox's avatar
Russ Cox committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138

// Called after regopt and peep have run.
// Expand CHECKNIL pseudo-op into actual nil pointer check.
void
expandchecks(Prog *firstp)
{
	Prog *p, *p1, *p2;

	for(p = firstp; p != P; p = p->link) {
		if(p->as != ACHECKNIL)
			continue;
		if(debug_checknil && p->lineno > 1) // p->lineno==1 in generated wrappers
1139
			warnl(p->lineno, "generated nil check");
Russ Cox's avatar
Russ Cox committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
		// check is
		//	CMP arg, $0
		//	JNE 2(PC) (likely)
		//	MOV AX, 0
		p1 = mal(sizeof *p1);
		p2 = mal(sizeof *p2);
		clearp(p1);
		clearp(p2);
		p1->link = p2;
		p2->link = p->link;
		p->link = p1;
		p1->lineno = p->lineno;
		p2->lineno = p->lineno;
1153 1154
		p1->pc = 9999;
		p2->pc = 9999;
1155
		p->as = cmpptr;
Russ Cox's avatar
Russ Cox committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		p->to.type = D_CONST;
		p->to.offset = 0;
		p1->as = AJNE;
		p1->from.type = D_CONST;
		p1->from.offset = 1; // likely
		p1->to.type = D_BRANCH;
		p1->to.u.branch = p2->link;
		// crash by write to memory address 0.
		// if possible, since we know arg is 0, use 0(arg),
		// which will be shorter to encode than plain 0.
		p2->as = AMOVL;
		p2->from.type = D_AX;
		if(regtyp(&p->from))
			p2->to.type = p->from.type + D_INDIR;
		else
			p2->to.type = D_INDIR+D_NONE;
		p2->to.offset = 0;
	}
}