From f3928f52a446e2d7f18af31aa84a2f8bd021cec9 Mon Sep 17 00:00:00 2001 From: Paul Mathieu Date: Mon, 22 Feb 2021 20:33:35 -0800 Subject: [PATCH] Fix if-else regs --- tools/cc.py | 11 +++++++++++ 1 file changed, 11 insertions(+) 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)