2024-04-14 02:40:21 -07:00
|
|
|
# (C) Copyright 2015-2024 Sei Lisa. All rights reserved.
|
2015-03-05 15:18:41 -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/>.
|
|
|
|
|
|
|
|
# Optimizer class that wraps and calls the other parts.
|
2014-07-26 12:29:35 -07:00
|
|
|
|
2019-01-15 12:27:02 -07:00
|
|
|
from lslopt import lslfuncs
|
2014-07-26 12:29:35 -07:00
|
|
|
|
2019-01-15 12:27:02 -07:00
|
|
|
from lslopt.lslcommon import nr
|
|
|
|
from lslopt.lslfoldconst import foldconst
|
|
|
|
from lslopt.lslrenamer import renamer
|
|
|
|
from lslopt.lsldeadcode import deadcode
|
|
|
|
from lslopt.lsllastpass import lastpass
|
2014-07-31 20:07:50 -07:00
|
|
|
|
2017-09-15 13:30:22 -07:00
|
|
|
class optimizer(foldconst, renamer, deadcode, lastpass):
|
2014-07-26 12:29:35 -07:00
|
|
|
|
2014-07-29 19:54:16 -07:00
|
|
|
# Default values per type when declaring variables
|
|
|
|
DefaultValues = {'integer': 0, 'float': 0.0, 'string': u'',
|
|
|
|
'key': lslfuncs.Key(u''), 'vector': lslfuncs.ZERO_VECTOR,
|
|
|
|
'rotation': lslfuncs.ZERO_ROTATION, 'list': []
|
|
|
|
}
|
|
|
|
|
2014-07-26 12:29:35 -07:00
|
|
|
# explicitly exclude assignments
|
2020-11-08 18:06:37 -07:00
|
|
|
binary_ops = frozenset({'+','-','*','/','%','<<','>>','<','<=','>','>=',
|
|
|
|
'==','!=','|','^','&','||','&&'})
|
|
|
|
assign_ops = frozenset({'=','+=','-=','*=','/=','%=','&=','|=','^=','<<=','>>='})
|
2014-08-13 04:44:54 -07:00
|
|
|
|
2014-07-30 17:45:28 -07:00
|
|
|
def Cast(self, value, newtype):
|
2015-03-05 15:18:41 -07:00
|
|
|
"""Return a CAST node if the types are not equal, otherwise the
|
|
|
|
value unchanged.
|
|
|
|
"""
|
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 value.t == newtype:
|
2014-07-30 17:45:28 -07:00
|
|
|
return value
|
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
|
|
|
ret = nr(nt='CAST', t=newtype, ch=[value], SEF=value.SEF)
|
|
|
|
if value.SEF:
|
|
|
|
ret.SEF = True
|
|
|
|
if hasattr(value, 'X'):
|
|
|
|
ret.X = value.X
|
2014-08-07 15:41:07 -07:00
|
|
|
return ret
|
2014-07-30 17:45:28 -07:00
|
|
|
|
2016-05-06 18:18:50 -07:00
|
|
|
def optimize(self, treesymtab, options = ('optimize','constfold','dcr',
|
|
|
|
'warntabs')):
|
2014-07-27 11:48:37 -07:00
|
|
|
"""Optimize the symbolic table symtab in place. Requires a table of
|
2014-07-26 12:29:35 -07:00
|
|
|
predefined functions for folding constants.
|
|
|
|
"""
|
2014-07-27 17:13:08 -07:00
|
|
|
if 'optimize' not in options:
|
2014-07-30 20:39:20 -07:00
|
|
|
return treesymtab
|
2014-07-27 17:13:08 -07:00
|
|
|
|
2015-02-28 12:01:51 -07:00
|
|
|
# Don't perform "a"+"b" -> "ab" unless explicitly requested.
|
|
|
|
self.addstrings = 'addstrings' in options
|
|
|
|
|
2014-07-28 08:39:35 -07:00
|
|
|
self.foldtabs = 'foldtabs' in options
|
2016-05-06 18:18:50 -07:00
|
|
|
self.warntabs = 'warntabs' in options
|
2014-07-28 08:39:35 -07:00
|
|
|
|
2014-08-05 06:47:14 -07:00
|
|
|
self.shrinknames = 'shrinknames' in options
|
|
|
|
|
2014-08-13 05:19:58 -07:00
|
|
|
self.constfold = 'constfold' in options
|
2022-10-31 12:08:26 -07:00
|
|
|
self.ifelseswap = 'ifelseswap' in options
|
2017-01-25 11:22:36 -07:00
|
|
|
self.optlistlength = 'listlength' in options
|
2017-09-15 13:30:22 -07:00
|
|
|
self.optlistadd = 'listadd' in options
|
2014-08-13 05:19:58 -07:00
|
|
|
self.dcr = 'dcr' in options
|
|
|
|
|
2015-02-28 10:30:04 -07:00
|
|
|
# Math that works fine except in rare corner-cases can be optimized.
|
|
|
|
self.cornermath = 'cornermath' in options
|
|
|
|
|
2014-07-29 19:54:16 -07:00
|
|
|
tree, symtab = self.tree, self.symtab = treesymtab
|
2014-07-26 12:29:35 -07:00
|
|
|
|
2014-07-27 14:33:20 -07:00
|
|
|
self.globalmode = False
|
|
|
|
|
2014-08-13 05:19:58 -07:00
|
|
|
if self.dcr:
|
2015-08-18 20:41:21 -07:00
|
|
|
if self.constfold:
|
|
|
|
self.FoldScript(warningpass=False)
|
|
|
|
|
2014-08-13 05:19:58 -07:00
|
|
|
self.RemoveDeadCode()
|
|
|
|
|
2015-08-18 20:41:21 -07:00
|
|
|
# Make another fold pass, since RemoveDeadCode can embed expressions
|
|
|
|
# into other expressions and generate unoptimized code.
|
|
|
|
# Or make the first pass here if DCR is disabled.
|
|
|
|
if self.constfold:
|
|
|
|
self.FoldScript(warningpass=True)
|
2014-07-29 19:54:16 -07:00
|
|
|
|
2017-11-02 15:19:33 -07:00
|
|
|
names = self.LastPass()
|
2017-09-15 13:30:22 -07:00
|
|
|
|
2014-08-09 17:05:31 -07:00
|
|
|
if self.shrinknames:
|
2017-11-02 15:19:33 -07:00
|
|
|
self.ShrinkNames(UsableAsParams = names['libfuncs'])
|
2014-07-31 20:07:50 -07:00
|
|
|
|
2014-07-29 19:54:16 -07:00
|
|
|
treesymtab = (self.tree, self.symtab)
|
|
|
|
del self.tree
|
|
|
|
del self.symtab
|
|
|
|
return treesymtab
|
2017-10-27 12:13:07 -07:00
|
|
|
|
|
|
|
def __init__(self, lib):
|
|
|
|
self.events = lib[0]
|