Remove globals from symbol table when no longer necessary.

This commit is contained in:
Sei Lisa 2015-03-13 20:11:57 +01:00
parent 5ca8e3db3f
commit f7556e7a66
2 changed files with 15 additions and 5 deletions

View file

@ -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.

View file

@ -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: