2024-04-14 02:40:21 -07:00
|
|
|
# (C) Copyright 2015-2024 Sei Lisa. All rights reserved.
|
2017-09-15 13:30:22 -07:00
|
|
|
#
|
|
|
|
# This file is part of LSL PyOptimizer.
|
|
|
|
#
|
|
|
|
# LSL PyOptimizer is free software: you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# LSL PyOptimizer is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with LSL PyOptimizer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Optimizations that have a negative effect on other stages.
|
|
|
|
|
2019-01-15 12:27:02 -07:00
|
|
|
from lslopt import lslcommon
|
|
|
|
from lslopt.lslcommon import nr
|
2017-09-15 13:30:22 -07:00
|
|
|
#from lslcommon import Vector, Quaternion
|
|
|
|
#import lslfuncs
|
|
|
|
#from lslfuncs import ZERO_VECTOR, ZERO_ROTATION
|
|
|
|
#import math
|
|
|
|
#from lslparse import warning
|
|
|
|
#from lslfuncopt import OptimizeFunc, OptimizeArgs, FuncOptSetup
|
2020-11-08 18:28:57 -07:00
|
|
|
from strutil import xrange
|
2017-09-15 13:30:22 -07:00
|
|
|
|
2019-01-06 14:32:19 -07:00
|
|
|
class rec:
|
|
|
|
def __init__(self, **init):
|
|
|
|
for i in init:
|
|
|
|
setattr(self, i, init[i])
|
|
|
|
|
2017-09-15 13:30:22 -07:00
|
|
|
class lastpass(object):
|
2019-01-06 14:32:19 -07:00
|
|
|
def visible(self, scope):
|
|
|
|
return scope in self.scopeStack
|
2017-09-15 13:30:22 -07:00
|
|
|
def LastPassPreOrder(self, parent, index):
|
|
|
|
node = parent[index]
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
nt = node.nt
|
|
|
|
child = node.ch
|
2017-09-15 13:30:22 -07:00
|
|
|
|
2019-01-06 14:32:19 -07:00
|
|
|
if (nt != '{}' and nt != ';' and nt != 'LAMBDA' and nt != '@'
|
|
|
|
and not self.inExpr
|
|
|
|
):
|
|
|
|
# Node that generates code.
|
|
|
|
# EXPR counts as a single statement for JUMP purposes.
|
|
|
|
self.prevStmt = self.lastStmt
|
|
|
|
self.lastStmt = rec(node=node, parent=parent, index=index)
|
|
|
|
# These are all the labels that label the current statement
|
|
|
|
self.lastLabels = self.curLabels
|
|
|
|
self.curLabels = set()
|
|
|
|
|
|
|
|
if nt == 'EXPR':
|
|
|
|
self.inExpr = True
|
|
|
|
return
|
|
|
|
|
2017-09-15 13:30:22 -07:00
|
|
|
if (self.optlistadd and not self.globalmode
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
and (nt == 'CONST' and node.t == 'list' or nt == 'LIST'
|
|
|
|
or nt == '+' and child[0].t == 'list' and
|
|
|
|
(child[1].nt == 'CONST' and child[1].t == 'list'
|
|
|
|
or child[1].nt == 'LIST')
|
2017-09-15 13:30:22 -07:00
|
|
|
)
|
|
|
|
):
|
|
|
|
# Perform these transformations if the list is SEF:
|
|
|
|
# [a, b, ...] -> (list)a + b...
|
|
|
|
# ListExpr + [a, b, ..] -> ListExpr + a + b...
|
|
|
|
# (ListExpr doesn't need to be SEF for this to work)
|
|
|
|
|
|
|
|
# This transformation makes it difficult to handle lists during
|
|
|
|
# optimization, that's why it's done in a separate pass.
|
2017-09-22 05:17:56 -07:00
|
|
|
|
|
|
|
# Make listnode be the LIST or CONST element.
|
|
|
|
listnode = child[1] if nt == '+' else node
|
|
|
|
|
|
|
|
# left is the leftmost element in the addition.
|
|
|
|
# left = None means the first element of the list must be
|
|
|
|
# turned into a cast within the loop.
|
|
|
|
left = child[0] if nt == '+' else None
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
if listnode.SEF:
|
|
|
|
for elem in (listnode.value if listnode.nt == 'CONST'
|
|
|
|
else listnode.ch):
|
|
|
|
elemnode = nr(nt='CONST',
|
|
|
|
t=lslcommon.PythonType2LSL[type(elem)],
|
|
|
|
value=elem, SEF=True
|
|
|
|
) if listnode.nt == 'CONST' else elem
|
2017-11-02 08:39:12 -07:00
|
|
|
left = (self.Cast(elemnode, 'list') if left is None
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
else nr(nt='+', t='list', SEF=True,
|
|
|
|
ch=[left, elemnode]))
|
2017-09-22 05:17:56 -07:00
|
|
|
del elemnode
|
|
|
|
if left is not None: # it's none for empty lists
|
|
|
|
parent[index] = left
|
2017-09-15 13:30:22 -07:00
|
|
|
# Do another pass on the result
|
|
|
|
self.RecursiveLastPass(parent, index)
|
|
|
|
return
|
|
|
|
|
2017-09-22 05:17:56 -07:00
|
|
|
del listnode, left
|
|
|
|
|
2017-11-02 13:50:55 -07:00
|
|
|
if nt == 'FNDEF':
|
|
|
|
# StChAreBad will be True if this is a user-defined function,
|
|
|
|
# where state changes are considered bad.
|
|
|
|
# BadStCh will be True if at least one state change statement
|
|
|
|
# is found while monitoring state changes.
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
self.subinfo['StChAreBad'] = hasattr(node, 'scope')
|
2017-11-02 13:50:55 -07:00
|
|
|
self.BadStCh = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if nt == 'IF':
|
|
|
|
if len(child) == 2:
|
|
|
|
# Don't monitor the children.
|
|
|
|
self.subinfo['StChAreBad'] = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if nt == 'DO':
|
|
|
|
self.subinfo['StChAreBad'] = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if nt == 'FOR':
|
|
|
|
self.subinfo['StChAreBad'] = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if nt == 'WHILE':
|
|
|
|
self.subinfo['StChAreBad'] = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if nt == 'STSW':
|
|
|
|
if self.subinfo['StChAreBad']:
|
|
|
|
# Found one.
|
|
|
|
self.BadStCh = True
|
|
|
|
return
|
|
|
|
|
2017-11-02 15:19:33 -07:00
|
|
|
if nt == 'FNCALL':
|
|
|
|
# lslrenamer can benefit from a list of used library functions,
|
|
|
|
# so provide it.
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
if 'Loc' not in self.symtab[0][node.name]:
|
2017-11-02 15:19:33 -07:00
|
|
|
# system library function
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
self.usedlibfuncs.add(node.name)
|
2017-11-02 15:19:33 -07:00
|
|
|
|
2019-01-06 14:32:19 -07:00
|
|
|
if nt == 'JUMP':
|
|
|
|
if self.lastLabels:
|
|
|
|
# This jump is labeled. The jumps that go to any of its labels
|
|
|
|
# might be changeable to the destination of this jump, if the
|
|
|
|
# scope of the destination is visible from those jumps, and the
|
|
|
|
# jump eliminated.
|
|
|
|
# TODO: We need the positions of these jumps for this to work.
|
|
|
|
# We could perhaps change 'ref' in the symbol to a list instead
|
|
|
|
# of a counter, pointing to the jumps.
|
|
|
|
pass
|
|
|
|
|
|
|
|
if nt == '@':
|
|
|
|
self.curLabels.add(node)
|
|
|
|
if self.lastStmt.node.nt == 'JUMP':
|
|
|
|
jump = self.lastStmt.node
|
|
|
|
if jump.scope == node.scope and jump.name == node.name:
|
|
|
|
# Remove the jump
|
|
|
|
self.lastStmt.parent[self.lastStmt.index] = nr(nt=';')
|
|
|
|
assert self.symtab[node.scope][node.name]['ref'] > 0
|
|
|
|
self.symtab[node.scope][node.name]['ref'] -= 1
|
|
|
|
|
|
|
|
if nt == '{}':
|
|
|
|
self.scopeStack.append(node.scope)
|
|
|
|
|
2017-09-15 13:30:22 -07:00
|
|
|
def LastPassPostOrder(self, parent, index):
|
2017-11-02 08:39:12 -07:00
|
|
|
node = parent[index]
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
nt = node.nt
|
|
|
|
child = node.ch
|
2017-11-02 08:39:12 -07:00
|
|
|
|
|
|
|
if nt == 'FNDEF':
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
if hasattr(node, 'scope') and self.BadStCh:
|
2017-11-02 08:39:12 -07:00
|
|
|
# There is at least one bad state change statement in the
|
|
|
|
# function (must be the result of optimization).
|
|
|
|
# Insert dummy IF(1){...} statement covering the whole function
|
|
|
|
# (if it returs a value, insert a return statement too).
|
2019-01-06 14:32:19 -07:00
|
|
|
child[0] = nr(nt='{}', t=None, scope=len(self.symtab), ch=[
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
nr(nt='IF', t=None, ch=[
|
|
|
|
nr(nt='CONST', t='integer', value=1, SEF=True),
|
2017-11-02 08:39:12 -07:00
|
|
|
child[0]
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
])
|
|
|
|
])
|
2019-01-06 14:32:19 -07:00
|
|
|
self.symtab.append({})
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
child = node.ch
|
|
|
|
if node.t is not None:
|
2017-11-02 08:39:12 -07:00
|
|
|
# Inserting a state switch in a function that returns a
|
|
|
|
# value must count as one of the dumbest things to do.
|
|
|
|
# We do as best as we can: add a return statement with the
|
|
|
|
# default value for the type.
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
child[0].ch.append(nr(nt='RETURN', t=None, ch=[
|
|
|
|
nr(nt='CONST', t=node.t, SEF=True,
|
|
|
|
value=lslcommon.LSLTypeDefaults[node.t]
|
|
|
|
)]
|
|
|
|
))
|
2017-11-02 08:39:12 -07:00
|
|
|
del self.BadStCh
|
|
|
|
return
|
|
|
|
|
2019-01-06 14:32:19 -07:00
|
|
|
if nt == 'EXPR':
|
|
|
|
self.inExpr = False
|
|
|
|
|
|
|
|
if nt == '{}':
|
|
|
|
self.scopeStack.pop()
|
|
|
|
|
|
|
|
if nt == 'WHILE' or nt == 'DO' or nt == 'FOR':
|
|
|
|
self.prevStmt = self.lastStmt
|
|
|
|
self.lastStmt = rec(node=node, parent=parent, index=index)
|
2017-09-15 13:30:22 -07:00
|
|
|
|
|
|
|
def RecursiveLastPass(self, parent, index):
|
2017-11-02 08:39:12 -07:00
|
|
|
subinfo = self.subinfo
|
|
|
|
self.subinfo = subinfo.copy()
|
2017-09-15 13:30:22 -07:00
|
|
|
self.LastPassPreOrder(parent, index)
|
|
|
|
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
if parent[index].ch is not None:
|
|
|
|
child = parent[index].ch
|
2017-09-15 13:30:22 -07:00
|
|
|
idx = 0
|
|
|
|
while idx < len(child):
|
|
|
|
self.RecursiveLastPass(child, idx)
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
self.LastPassPostOrder(parent, index)
|
2017-11-02 08:39:12 -07:00
|
|
|
self.subinfo = subinfo
|
2017-09-15 13:30:22 -07:00
|
|
|
|
|
|
|
def LastPass(self):
|
2017-11-02 15:19:33 -07:00
|
|
|
"""Last optimizations pass"""
|
|
|
|
|
2017-09-15 13:30:22 -07:00
|
|
|
self.globalmode = False
|
|
|
|
|
|
|
|
tree = self.tree
|
|
|
|
|
2017-11-02 15:19:33 -07:00
|
|
|
self.usedlibfuncs = set()
|
|
|
|
|
2019-01-06 14:32:19 -07:00
|
|
|
self.scopeStack = [0]
|
|
|
|
self.curLabels = set()
|
|
|
|
self.lastLabels = set()
|
|
|
|
self.prevStmt = None
|
|
|
|
self.lastStmt = None
|
|
|
|
self.inExpr = False
|
|
|
|
|
2017-11-02 08:39:12 -07:00
|
|
|
# self.subinfo is subtree-local info.
|
|
|
|
self.subinfo = {}
|
2017-09-15 13:30:22 -07:00
|
|
|
for idx in xrange(len(tree)):
|
Change the AST node type from dict to object
That was long overdue. Obviously, this is a large commit.
The new nr (node record) class has built-in dump capabilities, rather than using print_node().
SEF always exists now, and is a boolean, rather than using the existence of SEF as the flag. This was changed for sanity. However, other flags like 'X' are still possibly absent, and in some cases the absence itself has meaning (in the case of 'X', its absence means that the node has not yet been analyzed).
Similarly, an event is distinguished from a UDF by checking for the existence of the 'scope' attribute. This trick works because events are not in the symbol table therefore they have no scope. But this should probably be changed in future to something more rational and faster.
A few minor bugfixes were applied while going through the code.
- Some tabs used as Unicode were written as byte strings. Add the u'\t' prefix.
- After simplifying a%1 -> a&0, fold again the node and return. It's not clear why it didn't return, and whether it depended on subsequent passes (e.g. after DCR) for possibly optimizing out the result. Now we're sure.
- A few places lacked a SEF declaration.
- Formatting changes to split lines that spilled the margin.
- Some comment changes.
- Expanded lazy_list_set definition while adapting it to object format. The plan was to re-compress it after done, but decided to leave it in expanded form.
- Added a few TODOs & FIXMEs, resisting the temptation to fix them in the same commit:
- TODO: ~-~-~-expr -> expr + -3.
- FIXME: Now that we have CompareTrees, we can easily check if expr + -expr cancels out and remove a TODO. Low-hanging fruit.
- TODO: Check what we can do when comparing non-SEF and non-CONST values in '>' (current code relies on converting '>' to '<' for applying more optimizations, but that may miss some opportunities).
- FIXME: Could remove one comparison in nt == '&&' or nt == '||'. Low-hanging fruit.
2018-03-27 15:19:08 -07:00
|
|
|
if tree[idx].nt == 'DECL':
|
2017-09-15 13:30:22 -07:00
|
|
|
self.globalmode = True
|
|
|
|
self.RecursiveLastPass(tree, idx)
|
|
|
|
self.globalmode = False
|
|
|
|
else:
|
|
|
|
self.RecursiveLastPass(tree, idx)
|
2019-01-06 14:32:19 -07:00
|
|
|
|
|
|
|
assert len(self.scopeStack) == 1 and self.scopeStack[0] == 0
|
2017-11-02 15:19:33 -07:00
|
|
|
return {'libfuncs':self.usedlibfuncs}
|