From caf50c4e1440098f389e85dcc3969a9af75dd2e2 Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Thu, 17 May 2018 06:19:35 +0200 Subject: [PATCH] Change while (cond) /*empty*/; into do /*empty*/; while (cond); --- lslopt/lslfoldconst.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lslopt/lslfoldconst.py b/lslopt/lslfoldconst.py index 3772c2d..d8cbd00 100644 --- a/lslopt/lslfoldconst.py +++ b/lslopt/lslfoldconst.py @@ -116,7 +116,7 @@ class foldconst(object): labels, and applies to a block's statement list, not to a node. """ maybe_label = ';' if labels else '@' - if maybe_label != node.nt != ';': + if maybe_label != node.nt != ';' and not node.SEF: if node.nt == '{}': for subnode in node.ch: # Labels embedded in {} are not reachable. They do nothing. @@ -1675,18 +1675,26 @@ class foldconst(object): # out anyway. Otherwise we just don't know if it may be infinite, # even if every component is SEF. - self.ExpandCondition(child, 0) - self.FoldTree(child, 0) - self.FoldCond(child, 0) - if child[0].nt == 'CONST': - # See if the whole WHILE can be eliminated. - if not lslfuncs.cond(child[0].value): - # Whole statement can be removed. - parent[index] = nr(nt=';', t=None, SEF=True) - return - self.FoldTree(child, 1) - self.FoldStmt(child, 1) - return + if self.DoesSomething(child[1]): + + self.ExpandCondition(child, 0) + self.FoldTree(child, 0) + self.FoldCond(child, 0) + if child[0].nt == 'CONST': + # See if the whole WHILE can be eliminated. + if not lslfuncs.cond(child[0].value): + # Whole statement can be removed. + parent[index] = nr(nt=';', t=None, SEF=True) + return + self.FoldTree(child, 1) + self.FoldStmt(child, 1) + return + + # It does nothing - Turn it into a do..while + nt = node.nt = 'DO' + child[0], child[1] = child[1], child[0] + + # Fall through to optimize as DO..WHILE if nt == 'DO': self.FoldTree(child, 0) # This one is always executed.