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 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 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
#include "stdafx.h"
#include "Layout.h"
#include "Asm.h"
#include "../Listing.h"
#include "../Binary.h"
#include "../Layout.h"
#include "../Exception.h"
#include "../UsedRegs.h"
#include "../FnState.h"
namespace code {
namespace arm64 {
// Number used for inactive variables.
static const Nat INACTIVE = 0xFFFFFFFF;
#define TRANSFORM(x) { op::x, &Layout::x ## Tfm }
const OpEntry<Layout::TransformFn> Layout::transformMap[] = {
TRANSFORM(prolog),
TRANSFORM(epilog),
TRANSFORM(beginBlock),
TRANSFORM(endBlock),
TRANSFORM(jmpBlock),
TRANSFORM(activate),
TRANSFORM(mov),
TRANSFORM(lea),
TRANSFORM(icast),
TRANSFORM(ucast),
TRANSFORM(call),
TRANSFORM(fnRet),
TRANSFORM(fnRetRef),
};
Layout::Layout() {}
void Layout::before(Listing *dest, Listing *src) {
// Initialize state.
currentBlock = Block();
usingEH = src->exceptionAware();
// Find registers that need to be preserved.
preserved = allUsedRegs(src);
for (size_t i = 0; i < fnDirtyCount; i++)
preserved->remove(fnDirtyRegs[i]);
// Find parameters.
Array<Var> *p = src->allParams();
params = new (this) Params();
params->result(src->result);
for (Nat i = 0; i < p->count(); i++) {
params->add(i, src->paramDesc(p->at(i)));
}
// If the result is stored in registers, we need to "spill" it to non-clobbered
// registers during cleanup. Make sure we have them.
// Note: We don't always have to preserve register 19. We only need it if we
// need to run destructors in the epilog.
Result result = params->result();
if (usingEH) {
for (Nat i = 0; i < result.registerCount(); i++)
preserved->put(ptrr(19 + i));
}
Nat preserveCount = preserved->count();
// If result is passed in memory, we need to spill x8 to the stack as well. We treat it
// as a regular clobbered register.
if (result.memoryRegister() != noReg) {
preserveCount++;
}
layout = code::arm64::layout(src, params, preserveCount);
// Keep track of which parameters are stored indirectly.
varIndirect = new (this) Array<Bool>(layout->count(), false);
for (Nat i = 0; i < params->totalCount(); i++) {
Param par = params->totalParam(i);
if (par.any()) {
Var v = p->at(par.id());
varIndirect->at(v.key()) = par.inMemory();
// If passed in memory, modify the EH data for the parameter. These parameters
// are freed by the caller, and should be freed by reference if we ever free
// them.
if (par.inMemory()) {
FreeOpt flags = dest->freeOpt(v);
flags &= ~freeOnException;
flags &= ~freeOnBlockExit;
flags |= freeIndirection;
dest->freeOpt(v, flags);
}
}
}
// Initialize our bookkeeping of active variables.
Array<Var> *vars = src->allVars();
activated = new (this) Array<Nat>(vars->count(), 0);
activationId = 0;
for (Nat i = 0; i < vars->count(); i++) {
Var var = vars->at(i);
if (src->freeOpt(var) & freeInactive)
activated->at(var.key()) = INACTIVE;
}
// Keep track of active blocks.
activeBlocks = new (this) Array<ActiveBlock>();
}
void Layout::during(Listing *dest, Listing *src, Nat id) {
static OpTable<TransformFn> t(transformMap, ARRAY_COUNT(transformMap));
// Note: mov and lea are the only operands that may have references to memory.
Instr *i = src->at(id);
if (TransformFn f = t[i->op()]) {
(this->*f)(dest, i);
} else {
#ifdef DEBUG
// This should not happen, but we keep it for debugging.
if (i->src().type() == opVariable || i->dest().type() == opVariable) {
WARNING(L"Unexpected variable reference in: " << i);
}
#endif
*dest << i;
}
}
void Layout::after(Listing *dest, Listing *src) {
*dest << alignAs(Size::sPtr);
*dest << dest->meta();
// Offset between sp and fp. On ARM64, it is always zero.
// As such, store the size of the stack, but mark it by setting the LSB.
Nat size = Nat(layout->last().current());
*dest << dat(ptrConst(size | 0x1));
// Output metadata table.
Array<Var> *vars = src->allVars();
for (Nat i = 0; i < vars->count(); i++) {
Var &v = vars->at(i);
Operand fn = src->freeFn(v);
if (fn.empty())
*dest << dat(ptrConst(Offset(0)));
else
*dest << dat(src->freeFn(v));
*dest << dat(intConst(layout->at(v.key())));
*dest << dat(natConst(activated->at(v.key())));
}
// Output active blocks. Used by the exception handling.
*dest << alignAs(Size::sPtr);
for (Nat i = 0; i < activeBlocks->count(); i++) {
const ActiveBlock &a = activeBlocks->at(i);
*dest << lblOffset(a.pos);
*dest << dat(natConst(code::encodeFnState(a.block.key(), a.activated)));
}
// Table size.
*dest << dat(ptrConst(activeBlocks->count()));
}
Operand Layout::resolve(Listing *dest, const Operand &op, Reg tmpReg) {
return resolve(dest, op, op.size(), tmpReg);
}
Operand Layout::resolve(Listing *dest, const Operand &op, const Size &size, Reg tmpReg) {
if (op.type() != opVariable)
return op;
Var v = op.var();
if (!dest->accessible(v, currentBlock))
throw new (this) VariableUseError(v, currentBlock);
if (varIndirect->at(v.key())) {
assert(tmpReg != noReg);
tmpReg = asSize(tmpReg, Size::sPtr);
*dest << mov(tmpReg, ptrRel(ptrFrame, layout->at(v.key())));
return xRel(size, tmpReg, op.offsetRef());
} else {
return xRel(size, ptrFrame, op.offsetRef() + layout->at(v.key()));
}
}
static void zeroVar(Listing *dest, Offset offset, Size size) {
// Note: Everything is aligned to 8 bytes, so we can just fill memory with 8-byte store
// instructions that can be merged by the code generation.
Nat s = size.size64();
for (Nat i = 0; i < s; i += 8) {
*dest << mov(longRel(ptrFrame, offset), xzr);
offset += Size::sLong;
}
}
void Layout::initBlock(Listing *dest, Block init) {
if (currentBlock != dest->parent(init)) {
Str *msg = TO_S(engine(), S("Can not begin ") << init << S(" unless the current is ")
<< dest->parent(init) << S(". Current is ") << currentBlock);
throw new (this) BlockBeginError(msg);
}
currentBlock = init;
Array<Var> *vars = dest->allVars(init);
for (Nat i = 0; i < vars->count(); i++) {
Var v = vars->at(i);
// Don't initialize parameters or variables that are marked to not need initialization.
if (!dest->isParam(v) && (dest->freeOpt(v) & freeNoInit) == 0)
zeroVar(dest, layout->at(v.key()), v.size());
}
if (true || usingEH) { // We need info for active updates.
padCallWithNop(dest);
// Remember where the block started.
Label lbl = dest->label();
*dest << lbl;
activeBlocks->push(ActiveBlock(currentBlock, activationId, lbl));
}
}
void Layout::saveResult(Listing *dest) {
Result result = params->result();
for (Nat i = 0; i < result.registerCount(); i++) {
Reg src = result.registerAt(i);
*dest << mov(asSize(ptrr(19 + i), size(src)), src);
}
}
void Layout::restoreResult(Listing *dest) {
Result result = params->result();
for (Nat i = 0; i < result.registerCount(); i++) {
Reg dst = result.registerAt(i);
*dest << mov(dst, asSize(ptrr(19 + i), size(dst)));
}
}
void Layout::destroyBlock(Listing *dest, Block destroy, Bool preserveResult, Bool table) {
if (destroy != currentBlock)
throw new (this) BlockEndError();
// Did we save the result?
Bool savedResult = false;
// Destroy in reverse order.
Array<Var> *vars = dest->allVars(destroy);
for (Nat i = vars->count(); i > 0; i--) {
Var v = vars->at(i - 1);
Operand dtor = dest->freeFn(v);
FreeOpt when = dest->freeOpt(v);
if (!dtor.empty() && (when & freeOnBlockExit) == freeOnBlockExit) {
// Should we destroy it right now?
if (activated->at(v.key()) > activationId)
continue;
if (preserveResult && !savedResult) {
saveResult(dest);
savedResult = true;
}
Reg param = ptrr(0);
if (when & freeIndirection) {
if (when & freePtr) {
*dest << mov(param, resolve(dest, v, Size::sPtr, noReg));
*dest << call(dtor, Size());
} else {
*dest << mov(param, resolve(dest, v, Size::sPtr, noReg));
*dest << mov(asSize(param, v.size()), xRel(v.size(), param, Offset()));
*dest << call(dtor, Size());
}
} else {
if (when & freePtr) {
*dest << lea(param, resolve(dest, v, noReg));
*dest << call(dtor, Size());
} else {
*dest << mov(asSize(param, v.size()), resolve(dest, v, noReg));
*dest << call(dtor, Size());
}
}
// TODO: Zero memory to avoid multiple destruction in rare cases?
}
}
if (savedResult) {
restoreResult(dest);
}
currentBlock = dest->parent(currentBlock);
if ((true || usingEH) && table) { // Need for active updates
padCallWithNop(dest);
Label lbl = dest->label();
*dest << lbl;
activeBlocks->push(ActiveBlock(currentBlock, activationId, lbl));
}
}
void Layout::prologTfm(Listing *dest, Instr *src) {
// Emit instruction for updating sp, also preserves sp and fp from old frame, and sets fp to sp.
Offset stackSize = layout->last();
*dest << instrSrc(engine(), op::prolog, ptrConst(stackSize));
// Preserve registers.
Offset offset = stackSize;
// Store x8 if we need it.
if (params->result().memoryRegister() != noReg) {
offset -= Offset::sPtr;
*dest << mov(resultLocation(), ptrr(8));
*dest << preserve(longRel(ptrFrame, Offset(offset) - stackSize), xr(8));
}
// Preserve remaining registers.
for (RegSet::Iter begin = preserved->begin(), end = preserved->end(); begin != end; ++begin) {
offset -= Offset::sPtr;
Operand memory = longRel(ptrFrame, Offset(offset));
Reg reg = asSize(begin.v(), Size::sLong);
*dest << mov(memory, reg);
// Note: offsets are relative to the CFA, which is the location of the stack pointer
// at the start of the function:
*dest << preserve(longRel(ptrFrame, Offset(offset) - stackSize), reg);
}
// Preserve parameters.
Array<Var> *paramVars = dest->allParams();
for (Nat i = 0; i < params->registerCount(); i++) {
Param p = params->registerParam(i);
if (p == Param())
continue;
Offset offset = layout->at(paramVars->at(p.id()).key()) + Offset(p.offset());
*dest << mov(xRel(p.size(), ptrFrame, offset), asSize(params->registerSrc(i), p.size()));
}
// Initialize the root block.
initBlock(dest, dest->root());
}
void Layout::epilogTfm(Listing *dest, Instr *src) {
// Destroy blocks. Note: We shall not modify 'currentBlock', nor alter the exception
// table as this may be an early return from the function.
Block oldBlock = currentBlock;
for (Block now = currentBlock; now != Block(); now = dest->parent(now)) {
destroyBlock(dest, now, true, false);
}
currentBlock = oldBlock;
// Restore spilled registers.
Offset offset = layout->last();
if (params->result().memoryRegister() != noReg)
offset -= Offset::sPtr; // Adjust for preserving x8, but we don't need to restore it.
for (RegSet::Iter begin = preserved->begin(), end = preserved->end(); begin != end; ++begin) {
offset -= Offset::sPtr;
*dest << mov(asSize(begin.v(), Size::sLong), longRel(ptrFrame, Offset(offset)));
}
// Emit the epilog, and related metadata.
*dest << instrSrc(engine(), op::epilog, ptrConst(layout->last()));
}
void Layout::beginBlockTfm(Listing *dest, Instr *src) {
initBlock(dest, src->src().block());
}
void Layout::endBlockTfm(Listing *dest, Instr *src) {
destroyBlock(dest, src->src().block(), false, true);
}
void Layout::jmpBlockTfm(Listing *dest, Instr *src) {
// Destroy blocks until we find 'to'.
Block to = src->src().block();
// We shall not modify the block level after we're done, so we must restore it.
Block oldBlock = currentBlock;
for (Block now = currentBlock; now != to; now = dest->parent(now)) {
if (now == Block()) {
Str *msg = TO_S(this, S("The block ") << to << S(" is not a parent of ") << oldBlock << S("."));
throw new (this) BlockEndError(msg);
}
destroyBlock(dest, now, false, false);
}
*dest << jmp(src->dest().label());
currentBlock = oldBlock;
}
void Layout::activateTfm(Listing *dest, Instr *src) {
Var var = src->src().var();
Nat &id = activated->at(var.key());
if (id == 0)
throw new (this) VariableActivationError(var, S("must be marked with 'freeInactive'."));
if (id != INACTIVE)
throw new (this) VariableActivationError(var, S("already activated."));
id = ++activationId;
// We only need to update the block id if this impacts exception handling.
if (dest->freeOpt(var) & freeOnException) {
padCallWithNop(dest);
Label lbl = dest->label();
*dest << lbl;
activeBlocks->push(ActiveBlock(currentBlock, activationId, lbl));
}
}
static void returnSimple(Listing *dest, const Result &result, Size size, Reg src, Operand resultLocation) {
if (result.memoryRegister() != noReg) {
// Memcpy using the mov instruction.
Reg destReg = ptrB;
if (src == ptrB)
destReg = ptrC;
*dest << mov(destReg, resultLocation);
inlineMemcpy(dest, xRel(size, destReg, Offset()), xRel(size, src, Offset()), ptrr(16), ptrr(17));
} else {
// Just populate the relevant registers!
for (Nat i = 0; i < result.registerCount(); i++) {
Reg dst = result.registerAt(i);
Offset off = result.registerOffset(i);
*dest << mov(dst, xRel(code::size(dst), src, off));
}
}
}
void Layout::fnRetTfm(Listing *dest, Instr *src) {
Operand value = resolve(dest, src->src(), ptrA);
if (value.size() != dest->result->size()) {
StrBuf *msg = new (this) StrBuf();
*msg << S("Wrong size passed to fnRet. Got: ");
*msg << value.size();
*msg << S(" but expected ");
*msg << dest->result->size() << S(".");
throw new (this) InvalidValue(msg->toS());
}
// Handle the return value.
if (as<PrimitiveDesc>(dest->result)) {
Result r = params->result();
assert(r.registerCount() <= 1);
if (r.registerCount() == 1) {
if (value.type() == opRegister && same(r.registerAt(0), value.reg())) {
// Already there, nothing to do!
} else {
*dest << mov(r.registerAt(0), value);
}
}
} else if (ComplexDesc *c = as<ComplexDesc>(dest->result)) {
// Call the copy constructor.
*dest << lea(ptrB, value);
*dest << mov(ptrA, resultLocation());
*dest << call(c->ctor, Size());
} else if (SimpleDesc *s = as<SimpleDesc>(dest->result)) {
*dest << lea(ptrA, value);
returnSimple(dest, params->result(), s->size(), ptrA, resultLocation());
} else {
assert(false);
}
epilogTfm(dest, src);
*dest << ret(Size()); // We won't do register usage analysis, so this is OK.
}
void Layout::fnRetRefTfm(Listing *dest, Instr *src) {
Operand value = resolve(dest, src->src(), ptrA);
// Handle the return value.
if (PrimitiveDesc *p = as<PrimitiveDesc>(dest->result)) {
Result r = params->result();
assert(r.registerCount() <= 1);
if (r.registerCount() == 1) {
Reg target = r.registerAt(0);
if (value.type() == opRegister) {
*dest << mov(asSize(target, p->v.size()), xRel(p->v.size(), value.reg(), Offset()));
} else {
*dest << mov(ptrA, value);
*dest << mov(asSize(target, p->v.size()), xRel(p->v.size(), ptrA, Offset()));
}
}
} else if (ComplexDesc *c = as<ComplexDesc>(dest->result)) {
// Call the copy constructor.
*dest << mov(ptrB, value);
*dest << mov(ptrA, resultLocation());
*dest << call(c->ctor, Size());
} else if (SimpleDesc *s = as<SimpleDesc>(dest->result)) {
Reg reg = ptrA;
if (value.type() == opRegister) {
reg = value.reg();
} else {
*dest << mov(reg, value);
}
returnSimple(dest, params->result(), s->size(), reg, resultLocation());
} else {
assert(false);
}
epilogTfm(dest, src);
*dest << ret(Size()); // We won't do register usage analysis, so this is OK.
}
void Layout::movTfm(Listing *out, Instr *instr) {
Operand src = instr->src();
Operand dst = instr->dest();
// Note: We can assume that only one parameter is a variable (other has to be a register).
if (src.type() == opVariable) {
Nat varId = src.var().key();
Bool indirect = varIndirect->at(varId);
Offset stackOffset = layout->at(varId);
Offset varOffset = src.offset();
if (indirect) {
// We have: mov <reg>, <var>
// Can transform into:
// mov <reg>, <var>
// mov <reg>, [<reg>+<offset>]
Reg r = asSize(dst.reg(), Size::sPtr);
*out << mov(r, ptrRel(ptrFrame, stackOffset));
*out << mov(dst, xRel(src.size(), r, varOffset));
} else {
*out << instr->alterSrc(xRel(src.size(), ptrFrame, stackOffset + varOffset));
}
} else if (dst.type() == opVariable) {
Nat varId = dst.var().key();
Bool indirect = varIndirect->at(varId);
Offset stackOffset = layout->at(varId);
Offset varOffset = dst.offset();
if (indirect) {
// We have: mov <var>, <reg>
// Can transform into:
// mov x16, <var>
// mov [x16+<offset>], <reg>
// Note: It is a bit risky to use x16 or x17 without checking if we can trash it.
// However, direct writes to the variable like this are rare, and Storm almost
// never uses x16 and x17, so we should be fine.
Reg r = ptrr(16);
if (same(r, src.reg()))
r = ptrr(17);
*out << mov(r, ptrRel(ptrFrame, stackOffset));
*out << mov(xRel(dst.size(), r, varOffset), src);
} else {
*out << instr->alterDest(xRel(dst.size(), ptrFrame, stackOffset + varOffset));
}
} else {
// No changes needed.
*out << instr;
}
}
void Layout::leaTfm(Listing *dest, Instr *instr) {
// Note: We only need to consider 'src' here!
Operand src = instr->src();
if (src.type() != opVariable) {
*dest << instr;
return;
}
Nat varId = src.var().key();
Bool indirect = varIndirect->at(varId);
Offset stackOffset = layout->at(varId);
Offset varOffset = src.offset();
// Handle indirection if needed.
if (indirect) {
*dest << mov(instr->dest(), ptrRel(ptrFrame, stackOffset));
if (varOffset != Offset())
*dest << add(instr->dest(), ptrConst(varOffset));
} else {
*dest << instr->alterSrc(xRel(src.size(), ptrFrame, stackOffset + varOffset));
}
}
void Layout::icastTfm(Listing *out, Instr *instr) {
Operand src = instr->src();
Operand dst = instr->dest();
// Note: We can assume that only one parameter is a variable (other has to be a register).
if (src.type() == opVariable) {
Nat varId = src.var().key();
Bool indirect = varIndirect->at(varId);
Offset stackOffset = layout->at(varId);
Offset varOffset = src.offset();
if (indirect) {
// We have: mov <reg>, <var>
// Can transform into:
// mov <reg>, <var>
// xcast <reg>, [<reg>+<offset>]
Reg r = asSize(dst.reg(), Size::sPtr);
*out << mov(r, ptrRel(ptrFrame, stackOffset));
*out << instr->alter(dst, xRel(src.size(), r, varOffset));
} else {
*out << instr->alterSrc(xRel(src.size(), ptrFrame, stackOffset + varOffset));
}
} else {
*out << instr;
}
}
void Layout::ucastTfm(Listing *dest, Instr *instr) {
// Works the same.
icastTfm(dest, instr);
}
void Layout::callTfm(Listing *dest, Instr *instr) {
Operand target = instr->src();
if (target.type() == opVariable) {
Nat varId = target.var().key();
Bool indirect = varIndirect->at(varId);
Offset stackOffset = layout->at(varId);
Offset varOffset = target.offset();
if (indirect) {
*dest << mov(ptrr(17), ptrRel(ptrFrame, stackOffset));
*dest << mov(ptrr(17), ptrRel(ptrr(17), varOffset));
*dest << instr->alterSrc(ptrr(17));
} else {
*dest << mov(ptrr(17), ptrRel(ptrFrame, stackOffset + varOffset));
*dest << instr->alterSrc(ptrr(17));
}
} else if (target.type() == opRelative) {
*dest << mov(ptrr(17), target);
*dest << instr->alterSrc(ptrr(17));
} else {
*dest << instr;
}
}
Array<Offset> *layout(Listing *src, Params *params, Nat spilled) {
Array<Offset> *result = code::layout(src, Size::sPtr); // Specify minimum alignment.
Array<Var> *allVars = src->allVars();
Array<Var> *paramVars = src->allParams();
// A stack frame on Arm64 is as follows. Offsets are relative sp and x29 (frame
// ptr). The prolog will make sure that we can use x29 as the frame pointer in the
// generated code. This is important as the function-call code adjusts sp to make room
// for parameters, and we still need the ability to access local variables at that
// point.
//
// ...
// param1
// param0 <- old sp
// spilled-reg0
// ...
// spilled-regN
// spilled-param0
// ...
// spilled-paramN
// local0
// ...
// localN
// (padding)
// old-x30
// old-x29 <- sp, x29 (ptrFrame)
// Note: The stack frame is "flipped" compared to how it was previously. This is to
// mirror the layout on x86, which makes active function replacement much easier, and
// somewhat consistent between platforms.
// 0: We need to keep the stack aligned to 16 bytes. It is nice to insert the padding at
// the end of the stack frame. So, compute the number of spilled parameters here and fix
// the align now already.
Nat totalSpilled = spilled + 2;
for (Nat i = 0; i < params->registerCount(); i++)
if (params->registerParam(i) != Param())
totalSpilled++;
if ((result->last() + Offset::sPtr * totalSpilled).v64() & 0xF)
result->last() += Offset::sPtr;
// 1: Flip the order of all variables and make space for the old sp and fp.
result->last() += Offset::sPtr * 2;
Offset totalSize = result->last();
for (Nat i = 0; i < allVars->count(); i++) {
Var v = allVars->at(i);
if (src->isParam(v))
continue;
Offset varSize = Offset(v.size()).alignAs(Size::sPtr);
result->at(i) = totalSize - result->at(v.key()) - varSize;
// PLN(L"Var " << i << L": " << result->at(i) << L", sz: " << varSize);
}
// 2: Fill in parameters that need to be spilled on the stack:
for (Nat i = 0; i < params->registerCount(); i++) {
Param p = params->registerParam(i);
// Some params are "padding".
if (p.empty())
continue;
// If multiple parts of the same parameter, then don't update the variable again.
if (p.offset() == 0)
result->at(paramVars->at(p.id()).key()) = result->last();
result->last() += Offset::sPtr;
}
// 3: Add space for spilled registers.
result->last() += Offset::sPtr * spilled;
// 4: Finally, fill in the offset of parameters passed on the stack.
for (Nat i = 0; i < params->stackCount(); i++) {
Offset off(params->stackOffset(i));
off += result->last(); // Increase w. size of entire stack frame.
result->at(paramVars->at(params->stackParam(i).id()).key()) = off;
}
return result;
}
}
}
|