bool Forest::SpillCode() { int c, m, n; Var *v; Tree *t; BasicBlock *bb; int64_t spillOffset; OCODE *cd; CSet spilled; bool ret; ret = false; cd = currentFn->spAdjust; if (cd == nullptr) return (ret); spillOffset = cd->oper3->offset->i; spilled.clear(); for (c = 0; c < treecount; c++) { t = trees[c]; // convenience // Tree couldn't be colored that means there are no available registers. // So, spill one of the registers. The register to spill should be one // that isn't live at the same time as this tree. if (t->spill) { ret = true; v = Var::Find(t->var); v = v->GetVarToSpill(&spilled); if (v == nullptr) continue; // The var is spilled at the head of the tree, and restored later t->blocks->resetPtr(); m = t->blocks->nextMember(); if (m >= 0) { // should always be true, otherwise no code bb = basicBlocks[m]; if (!spilled.isMember(v->num)) { v->spillOffset = spillOffset; spillOffset += sizeOfWord; spilled.add(v->num); } bb->InsertSpillCode(v->num, -v->spillOffset); while(1) { n = m; m = t->blocks->nextMember(); if (m < 0) break; // Detect when a different branch is present. The node // number will jump by more than 1 between branches. // *** suspect this won't work, should just pick last block in tree if (m - n > 1) { bb = basicBlocks[n]; bb->InsertFillCode(v->num, -v->spillOffset); } } bb = basicBlocks[n]; bb->InsertFillCode(v->num, -v->spillOffset); } t->spill = false; } } cd->oper3->offset->i = spillOffset; return (ret); }