diff --git a/tools/cc.py b/tools/cc.py index 9b15f20..7879679 100644 --- a/tools/cc.py +++ b/tools/cc.py @@ -186,6 +186,15 @@ class RegBank: if var1 is not None: self.loaded(var1, reg0) + def snap(self): + return dict(self.vars), dict(self.cleanup) + + def restore(self, snap): + self.reset() + vardict, cleanup = snap + for var, reg in vardict.items(): + self.loaded(var, reg, cleanup=cleanup.get(reg, None)) + @contextlib.contextmanager def borrow(self, howmany): regs = [self.take() for i in range(howmany)] @@ -775,8 +784,10 @@ class CcInterp(lark.visitors.Interpreter): has_else = len(tree.children) > 2 op = IfOp(self.cur_fun, [tree.children[0].op.out, mark, has_else]) self._synth(op) + begin_vars = self.cur_fun.regs.snap() self.visit(tree.children[1]) if has_else: + self.cur_fun.regs.restore(begin_vars) self.cur_fun.ops.append(op.synth_else) self.visit(tree.children[2]) self.cur_fun.ops.append(op.synth_endif)