From f7556e7a6625146b338b9f2dbec8d72825e243a4 Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Fri, 13 Mar 2015 20:11:57 +0100 Subject: [PATCH] Remove globals from symbol table when no longer necessary. --- README.md | 2 +- lslopt/lsldeadcode.py | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 12f01e6..beac138 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ That is a requirement of the underlying `llList2List` function used in this case ### Constant folding and expression simplification -The optimizer simplifies expressions as much as it knows, which is a fair amount, though there's still room for improvement in this area. Expressions that evaluate to a constant will be replaced with that constant. Other expressions such as a+3+1 are replaced with a+4, and so on. Note, however, that for floats, `(a+b)+c` may not equal `a+(b+c)`, so that optimization is not always done for floats. Also, as of this writing this optimization is only partial, so some expressions may not be optimized, e.g. `2+a+b+3` is not optimized to `a+b+5`. Many boolean expressions are simplified too (more are in the way). For example, `(TRUE&&(expression))` is simplified to `(expression)`, and `(FALSE&&(expression))` is simplified to `(FALSE)`. The famous `if (llListFindList(...)!=-1)` to `if (~llListFindList(...))` replacement is also performed automatically. +The optimizer simplifies expressions as much as it knows, which is a fair amount, though there's still room for improvement in this area. Expressions that evaluate to a constant will be replaced with that constant. Other expressions such as a+3+1 are replaced with a+4, and so on. Note, however, that for floats, `(a+b)+c` may not equal `a+(b+c)`, so that optimization is not always done for floats. Also, as of this writing this optimization is only partial, so some expressions may not be optimized, e.g. `2+a+b+3` is not optimized to `a+b+5`. Many boolean expressions are simplified too (more are in the way). For example, `(TRUE&&(expression))` is simplified to `(expression)`, and `(FALSE&&(expression))` is simplified to `(FALSE)` provided the expression has no side effects. The famous `if (llListFindList(...)!=-1)` to `if (~llListFindList(...))` replacement is also performed automatically. The constant folder is also responsible for simplifying certain statements, e.g. `if (FALSE) { statements; } ` is completely removed, and `if (TRUE) { statements1; } else { statements2; }` is replaced with just `{ statements1; }`, removing `{ statements2; }`. Similarly, `do...while(constant)` loops and other loops are optimized too. diff --git a/lslopt/lsldeadcode.py b/lslopt/lsldeadcode.py index c099770..b7aa8b6 100644 --- a/lslopt/lsldeadcode.py +++ b/lslopt/lsldeadcode.py @@ -475,6 +475,8 @@ class deadcode(object): # Track removal of global lines, to reasign locations later. LocMap = range(len(self.tree)) + GlobalDeletions = [] + # Perform the removal idx = 0 while idx < len(self.tree): @@ -489,16 +491,24 @@ class deadcode(object): delete = self.SymbolReplacedOrDeleted(node) if delete: - # FIXME: This makes sense but it doesn't work. Analyze why and fix. - #if node['nt'] in ('DECL', 'STDEF', 'FNDEF'): - # # Delete the symbol table entry too - # del self.symtab[0][node['name']] + # Mark the symbol for later deletion from symbol table. + # We can't remove it here because there may be more references + # that we will remove in CleanNode later, that hold the + # original value. + if node['nt'] == 'DECL': + GlobalDeletions.append(node['name']) del self.tree[idx] del LocMap[idx] else: idx += 1 self.CleanNode(node) + # Remove the globals now. + for name in GlobalDeletions: + del self.symtab[0][name] + + del GlobalDeletions + # Reassign locations for name in self.symtab[0]: if name != -1: