几个装得下、要溢出几个
(每道题开头都有同一段:regs_used(prog) 程序里不同临时变量的个数;spills(ntemps, k) = max(0, ntemps-k)(k 个寄存器装不下就溢出);fits(ntemps, k) 够不够装。)
5 个临时值、3 个寄存器:问够不够装、要溢出几个:
def regs_used(prog):
"""程序里出现了多少个不同的临时变量(每条 IR 的 dst)。"""
return len({t[1] for t in prog})
def spills(ntemps, k):
"""ntemps 个临时值、只有 k 个寄存器:要溢出到内存的个数 = max(0, ntemps - k)。"""
return max(0, ntemps - k)
def fits(ntemps, k):
"""k 个寄存器够不够装下 ntemps 个临时值。"""
return ntemps <= k
print(str(fits(5, 3)) + "/" + str(spills(5, 3)))
全部评论