inl.c 17.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2011 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.
//
// The inlining facility makes 2 passes: first caninl determines which
// functions are suitable for inlining, and for those that are it
// saves a copy of the body. Then inlcalls walks each function body to
// expand calls to inlinable functions.
//
Luuk van Dijk's avatar
Luuk van Dijk committed
10 11 12 13 14
// TODO:
//   - inline functions with ... args
//   - handle T.meth(f()) with func f() (t T, arg, arg, )
//   - (limited) recursive inlining
//   - it would be nice if func max(x, y int) { if x > y { return x }; return y } would be inlineable
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <u.h>
#include <libc.h>
#include "go.h"

// Used by caninl.
static Node*	inlcopy(Node *n);
static NodeList* inlcopylist(NodeList *ll);
static int	ishairy(Node *n);
static int	ishairylist(NodeList *ll); 

// Used by inlcalls
static void	inlnodelist(NodeList *l);
static void	inlnode(Node **np);
static void	mkinlcall(Node **np, Node *fn);
static Node*	inlvar(Node *n);
static Node*	retvar(Type *n, int i);
static Node*	newlabel(void);
static Node*	inlsubst(Node *n);
static NodeList* inlsubstlist(NodeList *ll);

36 37
static void	setlno(Node*, int);

38 39 40 41 42 43
// Used during inlsubst[list]
static Node *inlfn;		// function currently being inlined
static Node *inlretlabel;	// target of the goto substituted in place of a return
static NodeList *inlretvars;	// temp out variables


Luuk van Dijk's avatar
Luuk van Dijk committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
void
typecheckinl(Node *fn)
{
	Node *savefn;

	if (debug['m']>2)
		print("typecheck import [%S] %lN { %#H }\n", fn->sym, fn, fn->inl);

	savefn = curfn;
	curfn = fn;
	importpkg = fn->sym->pkg;
	typechecklist(fn->inl, Etop);
	importpkg = nil;
	curfn = savefn;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
// Caninl determines whether fn is inlineable. Currently that means:
// fn is exactly 1 statement, either a return or an assignment, and
// some temporary constraints marked TODO.  If fn is inlineable, saves
// fn->nbody in fn->inl and substitutes it with a copy.
void
caninl(Node *fn)
{
	Node *savefn;
	Type *t;

	if(fn->op != ODCLFUNC)
		fatal("caninl %N", fn);
	if(!fn->nname)
		fatal("caninl no nname %+N", fn);

	// exactly 1 statement
	if(fn->nbody == nil || fn->nbody->next != nil)
		return;

Luuk van Dijk's avatar
Luuk van Dijk committed
79
	// the single statement should be a return, an assignment or empty.
80 81 82 83 84 85
	switch(fn->nbody->n->op) {
	default:
		return;
	case ORETURN:
	case OAS:
	case OAS2:
86
	case OEMPTY:
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		break;
	}

	// can't handle ... args yet
	for(t=fn->type->type->down->down->type; t; t=t->down)
		if(t->isddd)
			return;

	// TODO Anything non-trivial
	if(ishairy(fn))
		return;

	savefn = curfn;
	curfn = fn;

	fn->nname->inl = fn->nbody;
	fn->nbody = inlcopylist(fn->nname->inl);

	// hack, TODO, check for better way to link method nodes back to the thing with the ->inl
	// this is so export can find the body of a method
	fn->type->nname = fn->nname;

Luuk van Dijk's avatar
Luuk van Dijk committed
109
	if(debug['m'] > 1)
110
		print("%L: can inline %#N as: %#T { %#H }\n", fn->lineno, fn->nname, fn->type, fn->nname->inl);
Luuk van Dijk's avatar
Luuk van Dijk committed
111 112
	else if(debug['m'])
		print("%L: can inline %N\n", fn->lineno, fn->nname);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

	curfn = savefn;
}

// Look for anything we want to punt on.
static int
ishairylist(NodeList *ll)
{
	for(;ll;ll=ll->next)
		if(ishairy(ll->n))
			return 1;
	return 0;
}

static int
ishairy(Node *n)
{
	if(!n)
		return 0;

Luuk van Dijk's avatar
Luuk van Dijk committed
133 134
	// Some of these are implied by the single-assign-or-return condition in caninl,
	// but they may stay even if that one is relaxed.
135 136 137 138 139
	switch(n->op) {
	case OCALL:
	case OCALLFUNC:
	case OCALLINTER:
	case OCALLMETH:
Luuk van Dijk's avatar
Luuk van Dijk committed
140 141 142 143 144 145 146 147
	case OCLOSURE:	// TODO too hard to inlvar the PARAMREFs
	case OIF:
	case ORANGE:
	case OFOR:
	case OSELECT:
	case OSWITCH:
	case OPROC:
	case ODEFER:
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 211 212 213 214 215 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
		return 1;
	}

	return  ishairy(n->left) ||
		ishairy(n->right) ||
		ishairylist(n->list) ||
		ishairylist(n->rlist) ||
		ishairylist(n->ninit) ||
		ishairy(n->ntest) ||
		ishairy(n->nincr) ||
		ishairylist(n->nbody) ||
		ishairylist(n->nelse);
}

// Inlcopy and inlcopylist recursively copy the body of a function.
// Any name-like node of non-local class is marked for re-export by adding it to
// the exportlist.
static NodeList*
inlcopylist(NodeList *ll)
{
	NodeList *l;

	l = nil;
	for(; ll; ll=ll->next)
		l = list(l, inlcopy(ll->n));
	return l;
}

static Node*
inlcopy(Node *n)
{
	Node *m;

	if(n == N)
		return N;

	switch(n->op) {
	case ONAME:
	case OTYPE:
	case OLITERAL:
		return n;
	}

	m = nod(OXXX, N, N);
	*m = *n;
	m->inl = nil;
	m->left	  = inlcopy(n->left);
	m->right  = inlcopy(n->right);
	m->list   = inlcopylist(n->list);
	m->rlist  = inlcopylist(n->rlist);
	m->ninit  = inlcopylist(n->ninit);
	m->ntest  = inlcopy(n->ntest);
	m->nincr  = inlcopy(n->nincr);
	m->nbody  = inlcopylist(n->nbody);
	m->nelse  = inlcopylist(n->nelse);

	return m;
}


// Inlcalls/nodelist/node walks fn's statements and expressions and substitutes any
// calls made to inlineable functions.  This is the external entry point.
void
inlcalls(Node *fn)
{
	Node *savefn;

	savefn = curfn;
	curfn = fn;
	inlnode(&fn);
	if(fn != curfn)
		fatal("inlnode replaced curfn");
	curfn = savefn;
}

// Turn an OINLCALL into a statement.
static void
inlconv2stmt(Node *n)
{
	n->op = OBLOCK;
	n->list = n->nbody;
	n->nbody = nil;
	n->rlist = nil;
}

// Turn an OINLCALL into a single valued expression.
static void
inlconv2expr(Node *n)
{
	n->op = OCONVNOP;
	n->left = n->rlist->n;
	n->rlist = nil;
	n->ninit = concat(n->ninit, n->nbody);
	n->nbody = nil;
}

// Turn the OINLCALL in n->list into an expression list on n.
// Used in return and call statements.
static void
inlgluelist(Node *n)
{
	Node *c;

	c = n->list->n;
	n->ninit = concat(n->ninit, c->ninit);
	n->ninit = concat(n->ninit, c->nbody);
	n->list  = c->rlist;
} 

// Turn the OINLCALL in n->rlist->n into an expression list on n.
// Used in OAS2FUNC.
static void
inlgluerlist(Node *n)
{
	Node *c;

	c = n->rlist->n;
	n->ninit = concat(n->ninit, c->ninit);
	n->ninit = concat(n->ninit, c->nbody);
	n->rlist = c->rlist;
} 

static void
inlnodelist(NodeList *l)
{
	for(; l; l=l->next)
		inlnode(&l->n);
}

// inlnode recurses over the tree to find inlineable calls, which will
// be turned into OINLCALLs by mkinlcall.  When the recursion comes
// back up will examine left, right, list, rlist, ninit, ntest, nincr,
// nbody and nelse and use one of the 4 inlconv/glue functions above
// to turn the OINLCALL into an expression, a statement, or patch it
// in to this nodes list or rlist as appropriate.
Luuk van Dijk's avatar
Luuk van Dijk committed
283 284 285 286 287
// NOTE it makes no sense to pass the glue functions down the
// recursion to the level where the OINLCALL gets created because they
// have to edit /this/ n, so you'd have to push that one down as well,
// but then you may as well do it here.  so this is cleaner and
// shorter and less complicated.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
static void
inlnode(Node **np)
{
	Node *n;
	NodeList *l;

	if(*np == nil)
		return;

	n = *np;

	switch(n->op) {
	case ODEFER:
	case OPROC:
		// inhibit inlining of their argument
		switch(n->left->op) {
		case OCALLFUNC:
		case OCALLMETH:
			n->left->etype = n->op;
		}

	case OCLOSURE:
Luuk van Dijk's avatar
Luuk van Dijk committed
310 311
		// TODO do them here instead of in lex.c phase 6b, so escape analysis
		// can avoid more heapmoves.
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 410
		return;
	}

	inlnodelist(n->ninit);
	for(l=n->ninit; l; l=l->next)
		if(l->n->op == OINLCALL)
			inlconv2stmt(l->n);

	inlnode(&n->left);
	if(n->left && n->left->op == OINLCALL)
		inlconv2expr(n->left);

	inlnode(&n->right);
	if(n->right && n->right->op == OINLCALL)
		inlconv2expr(n->right);

	inlnodelist(n->list);
	switch(n->op) {
	case OBLOCK:
		for(l=n->list; l; l=l->next)
			if(l->n->op == OINLCALL)
				inlconv2stmt(l->n);
		break;

	case ORETURN:
		if(count(n->list) == 1 && curfn->type->outtuple > 1 && n->list->n->op == OINLCALL) {
			inlgluelist(n);
			break;
		}
		
		goto list_dflt;

	case OCALLMETH:
	case OCALLINTER:
	case OCALLFUNC:
		// if we just replaced arg in f(arg()) with an inlined call
		// and arg returns multiple values, glue as list
		if(count(n->list) == 1 && n->list->n->op == OINLCALL && count(n->list->n->rlist) > 1) {
			inlgluelist(n);
			break;
		}

		// fallthrough
	default:
	list_dflt:
		for(l=n->list; l; l=l->next)
			if(l->n->op == OINLCALL)
				inlconv2expr(l->n);
	}

	inlnodelist(n->rlist);
	switch(n->op) {
	case OAS2FUNC:
		if(n->rlist->n->op == OINLCALL) {
			inlgluerlist(n);
			n->op = OAS2;
			n->typecheck = 0;
			typecheck(np, Etop);
			break;
		}

		// fallthrough
	default:
		for(l=n->rlist; l; l=l->next)
			if(l->n->op == OINLCALL)
				inlconv2expr(l->n);

	}

	inlnode(&n->ntest);
	if(n->ntest && n->ntest->op == OINLCALL)
		inlconv2expr(n->ntest);

	inlnode(&n->nincr);
	if(n->nincr && n->nincr->op == OINLCALL)
		inlconv2stmt(n->nincr);

	inlnodelist(n->nbody);
	for(l=n->nbody; l; l=l->next)
		if(l->n->op == OINLCALL)
			inlconv2stmt(l->n);

	inlnodelist(n->nelse);
	for(l=n->nelse; l; l=l->next)
		if(l->n->op == OINLCALL)
			inlconv2stmt(l->n);

	// with all the branches out of the way, it is now time to
	// transmogrify this node itself unless inhibited by the
	// switch at the top of this function.
	switch(n->op) {
	case OCALLFUNC:
	case OCALLMETH:
		if (n->etype == OPROC || n->etype == ODEFER)
			return;
	}

	switch(n->op) {
	case OCALLFUNC:
Luuk van Dijk's avatar
Luuk van Dijk committed
411 412 413 414 415 416 417
		if(debug['m']>3)
			print("%L:call to func %+N\n", n->lineno, n->left);
		if(n->left->inl)	// normal case
			mkinlcall(np, n->left);
		else if(n->left->op == ONAME && n->left->left && n->left->left->op == OTYPE && n->left->right &&  n->left->right->op == ONAME)  // methods called as functions
			if(n->left->sym->def)
				mkinlcall(np, n->left->sym->def);
418 419 420
		break;

	case OCALLMETH:
Luuk van Dijk's avatar
Luuk van Dijk committed
421
		if(debug['m']>3)
422
			print("%L:call to meth %lN\n", n->lineno, n->left->right);
Luuk van Dijk's avatar
Luuk van Dijk committed
423 424 425 426 427
		// typecheck should have resolved ODOTMETH->type, whose nname points to the actual function.
		if(n->left->type == T) 
			fatal("no function type for [%p] %+N\n", n->left, n->left);

		if(n->left->type->nname == N) 
428
			fatal("no function definition for [%p] %+T\n", n->left->type, n->left->type);
Luuk van Dijk's avatar
Luuk van Dijk committed
429 430 431

		mkinlcall(np, n->left->type->nname);

432 433 434 435 436 437 438 439 440 441 442 443
		break;
	}
}

// if *np is a call, and fn is a function with an inlinable body, substitute *np with an OINLCALL.
// On return ninit has the parameter assignments, the nbody is the
// inlined function body and list, rlist contain the input, output
// parameters.
static void
mkinlcall(Node **np, Node *fn)
{
	int i;
Luuk van Dijk's avatar
Luuk van Dijk committed
444
	Node *n, *call, *saveinlfn, *as, *m;
445 446 447 448 449 450
	NodeList *dcl, *ll, *ninit, *body;
	Type *t;

	if (fn->inl == nil)
		return;

Luuk van Dijk's avatar
Luuk van Dijk committed
451 452 453
	if(debug['l']<2)
		typecheckinl(fn);

454 455 456
	n = *np;

	// Bingo, we have a function node, and it has an inlineable body
Luuk van Dijk's avatar
Luuk van Dijk committed
457
	if(debug['m']>1)
458
		print("%L: inlining call to %S %#T { %#H }\n", n->lineno, fn->sym, fn->type, fn->inl);
Luuk van Dijk's avatar
Luuk van Dijk committed
459 460
	else if(debug['m'])
		print("%L: inlining call to %N\n", n->lineno, fn);
461

Luuk van Dijk's avatar
Luuk van Dijk committed
462
	if(debug['m']>2)
463 464 465 466 467 468 469 470 471 472 473 474
		print("%L: Before inlining: %+N\n", n->lineno, n);

	saveinlfn = inlfn;
	inlfn = fn;

	ninit = n->ninit;

	if (fn->defn) // local function
		dcl = fn->defn->dcl;
	else // imported function
		dcl = fn->dcl;

Luuk van Dijk's avatar
Luuk van Dijk committed
475 476 477
	inlretvars = nil;
	i = 0;
	// Make temp names to use instead of the originals
478
	for(ll = dcl; ll; ll=ll->next)
Luuk van Dijk's avatar
Luuk van Dijk committed
479
		if(ll->n->op == ONAME) {
480 481
			ll->n->inlvar = inlvar(ll->n);
			ninit = list(ninit, nod(ODCL, ll->n->inlvar, N));  // otherwise gen won't emit the allocations for heapallocs
Luuk van Dijk's avatar
Luuk van Dijk committed
482 483 484 485 486 487 488 489 490 491
			if (ll->n->class == PPARAMOUT)  // we rely on the order being correct here
				inlretvars = list(inlretvars, ll->n->inlvar);
		}

	// anonymous return values, synthesize names for use in assignment that replaces return
	if(inlretvars == nil && fn->type->outtuple > 0)
		for(t = getoutargx(fn->type)->type; t; t = t->down) {
			m = retvar(t, i++);
			ninit = list(ninit, nod(ODCL, m, N));
			inlretvars = list(inlretvars, m);
492 493 494
		}

	// assign arguments to the parameters' temp names
Luuk van Dijk's avatar
Luuk van Dijk committed
495
	as = N;
496 497
	if(fn->type->thistuple) {
		t = getthisx(fn->type)->type;
Luuk van Dijk's avatar
Luuk van Dijk committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

		if(t != T && t->nname != N && !t->nname->inlvar)
			fatal("missing inlvar for %N\n", t->nname);

		if(n->left->op == ODOTMETH) {
			if (!n->left->left)
				fatal("method call without receiver: %+N", n);
			if(t != T && t->nname)
				as = nod(OAS, t->nname->inlvar, n->left->left);
			// else if !ONAME add to init anyway?
		} else {  // non-method call to method
			if (!n->list)
				fatal("non-method call to method without first arg: %+N", n);
			if(t != T && t->nname)
				as = nod(OAS, t->nname->inlvar, n->list->n);
		}

		if(as != N) {
516 517
			typecheck(&as, Etop);
			ninit = list(ninit, as);
Luuk van Dijk's avatar
Luuk van Dijk committed
518
		}
519 520 521 522 523
	}

	as = nod(OAS2, N, N);
	if(fn->type->intuple > 1 && n->list && !n->list->next) {
		// TODO check that n->list->n is a call?
Luuk van Dijk's avatar
Luuk van Dijk committed
524
		// TODO: non-method call to T.meth(f()) where f returns t, args...
525 526
		as->rlist = n->list;
		for(t = getinargx(fn->type)->type; t; t=t->down) {
527
			if(t->nname && !isblank(t->nname)) {
528 529 530 531 532 533 534 535 536
				if(!t->nname->inlvar)
					fatal("missing inlvar for %N\n", t->nname);
				as->list = list(as->list, t->nname->inlvar);
			} else {
				as->list = list(as->list, temp(t->type));
			}
		}		
	} else {
		ll = n->list;
Luuk van Dijk's avatar
Luuk van Dijk committed
537 538 539
		if(fn->type->thistuple && n->left->op != ODOTMETH) // non method call to method
			ll=ll->next;  // was handled above in if(thistuple)

540
		for(t = getinargx(fn->type)->type; t && ll; t=t->down) {
541
			if(t->nname && !isblank(t->nname)) {
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
				if(!t->nname->inlvar)
					fatal("missing inlvar for %N\n", t->nname);
				as->list = list(as->list, t->nname->inlvar);
				as->rlist = list(as->rlist, ll->n);
			}
			ll=ll->next;
		}
		if(ll || t)
			fatal("arg count mismatch: %#T  vs %,H\n",  getinargx(fn->type), n->list);
	}

	if (as->rlist) {
		typecheck(&as, Etop);
		ninit = list(ninit, as);
	}

Luuk van Dijk's avatar
Luuk van Dijk committed
558 559 560 561 562 563 564
	// zero the outparams
	for(ll = inlretvars; ll; ll=ll->next) {
		as = nod(OAS, ll->n, N);
		typecheck(&as, Etop);
		ninit = list(ninit, as);
	}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	inlretlabel = newlabel();
	body = inlsubstlist(fn->inl);

	body = list(body, nod(OGOTO, inlretlabel, N));	// avoid 'not used' when function doesnt have return
	body = list(body, nod(OLABEL, inlretlabel, N));

	typechecklist(body, Etop);

	call = nod(OINLCALL, N, N);
	call->ninit = ninit;
	call->nbody = body;
	call->rlist = inlretvars;
	call->type = n->type;
	call->typecheck = 1;

580 581
	setlno(call, n->lineno);

582 583 584
	*np = call;

	inlfn =	saveinlfn;
Luuk van Dijk's avatar
Luuk van Dijk committed
585
	if(debug['m']>2)
586 587 588 589 590 591 592 593 594 595 596 597
		print("%L: After inlining %+N\n\n", n->lineno, *np);

}

// Every time we expand a function we generate a new set of tmpnames,
// PAUTO's in the calling functions, and link them off of the
// PPARAM's, PAUTOS and PPARAMOUTs of the called function. 
static Node*
inlvar(Node *var)
{
	Node *n;

Luuk van Dijk's avatar
Luuk van Dijk committed
598
	if(debug['m']>3)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		print("inlvar %+N\n", var);

	n = newname(var->sym);
	n->type = var->type;
	n->class = PAUTO;
	n->used = 1;
	n->curfn = curfn;   // the calling function, not the called one
	curfn->dcl = list(curfn->dcl, n);
	return n;
}

// Synthesize a variable to store the inlined function's results in.
static Node*
retvar(Type *t, int i)
{
	Node *n;

	snprint(namebuf, sizeof(namebuf), ".r%d", i);
	n = newname(lookup(namebuf));
	n->type = t->type;
	n->class = PAUTO;
	n->used = 1;
	n->curfn = curfn;   // the calling function, not the called one
	curfn->dcl = list(curfn->dcl, n);
	return n;
}

static Node*
newlabel(void)
{
	Node *n;
	static int label;
	
	label++;
	snprint(namebuf, sizeof(namebuf), ".inlret%.6d", label);
	n = newname(lookup(namebuf));
	n->etype = 1;  // flag 'safe' for escape analysis (no backjumps)
	return n;
}

// inlsubst and inlsubstlist recursively copy the body of the saved
// pristine ->inl body of the function while substituting references
// to input/output parameters with ones to the tmpnames, and
// substituting returns with assignments to the output.
static NodeList*
inlsubstlist(NodeList *ll)
{
	NodeList *l;

	l = nil;
	for(; ll; ll=ll->next)
		l = list(l, inlsubst(ll->n));
	return l;
}

static Node*
inlsubst(Node *n)
{
	Node *m, *as;
	NodeList *ll;

	if(n == N)
		return N;

	switch(n->op) {
	case ONAME:
		if(n->inlvar) { // These will be set during inlnode
Luuk van Dijk's avatar
Luuk van Dijk committed
666 667
			if (debug['m']>2)
				print ("substituting name %+N  ->  %+N\n", n, n->inlvar);
668 669
			return n->inlvar;
		}
Luuk van Dijk's avatar
Luuk van Dijk committed
670 671
		if (debug['m']>2)
			print ("not substituting name %+N\n", n);
672 673 674 675 676 677 678
		return n;

	case OLITERAL:
	case OTYPE:
		return n;

	case ORETURN:
Luuk van Dijk's avatar
Luuk van Dijk committed
679 680
		// Since we don't handle bodies with closures, this return is guaranteed to belong to the current inlined function.

681 682 683 684 685 686
//		dump("Return before substitution", n);
		m = nod(OGOTO, inlretlabel, N);
		m->ninit  = inlsubstlist(n->ninit);

		if(inlretvars && n->list) {
			as = nod(OAS2, N, N);
Luuk van Dijk's avatar
Luuk van Dijk committed
687 688 689
			// shallow copy or OINLCALL->rlist will be the same list, and later walk and typecheck may clobber that.
			for(ll=inlretvars; ll; ll=ll->next)
				as->list = list(as->list, ll->n);
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
			as->rlist = inlsubstlist(n->list);
			typecheck(&as, Etop);
			m->ninit = list(m->ninit, as);
		}

		typechecklist(m->ninit, Etop);
		typecheck(&m, Etop);
//		dump("Return after substitution", m);
		return m;
	}


	m = nod(OXXX, N, N);
	*m = *n;
	m->ninit = nil;
	
Luuk van Dijk's avatar
Luuk van Dijk committed
706 707
	if(n->op == OCLOSURE)
		fatal("cannot inline function containing closure: %+N", n);
708 709 710 711 712 713 714 715 716 717 718 719 720

	m->left	  = inlsubst(n->left);
	m->right  = inlsubst(n->right);
	m->list	  = inlsubstlist(n->list);
	m->rlist  = inlsubstlist(n->rlist);
	m->ninit  = concat(m->ninit, inlsubstlist(n->ninit));
	m->ntest  = inlsubst(n->ntest);
	m->nincr  = inlsubst(n->nincr);
	m->nbody  = inlsubstlist(n->nbody);
	m->nelse  = inlsubstlist(n->nelse);

	return m;
}
721 722 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

// Plaster over linenumbers
static void
setlnolist(NodeList *ll, int lno)
{
	for(;ll;ll=ll->next)
		setlno(ll->n, lno);
}

static void
setlno(Node *n, int lno)
{
	if(!n)
		return;

	// don't clobber names, unless they're freshly synthesized
	if(n->op != ONAME || n->lineno == 0)
		n->lineno = lno;
	
	setlno(n->left, lno);
	setlno(n->right, lno);
	setlnolist(n->list, lno);
	setlnolist(n->rlist, lno);
	setlnolist(n->ninit, lno);
	setlno(n->ntest, lno);
	setlno(n->nincr, lno);
	setlnolist(n->nbody, lno);
	setlnolist(n->nelse, lno);
}