2023-02-03 13:06:18 +01:00
|
|
|
# (C) Copyright 2015-2023 Sei Lisa. All rights reserved.
|
2015-04-21 04:56:09 +02: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/>.
|
|
|
|
|
|
2017-08-25 20:11:16 +02:00
|
|
|
# Optimize calls to LSL library functions and parameters where possible
|
2015-04-21 04:56:09 +02:00
|
|
|
# This is dependent on the LSL function library.
|
|
|
|
|
|
2019-01-15 20:27:02 +01:00
|
|
|
from lslopt import lslcommon
|
|
|
|
|
from lslopt.lslcommon import Key, Vector, Quaternion, nr
|
|
|
|
|
from lslopt import lslfuncs
|
2020-11-10 00:15:48 +01:00
|
|
|
from strutil import unicode
|
2015-04-21 04:56:09 +02:00
|
|
|
|
2019-11-03 00:49:32 +01:00
|
|
|
SensorFunctions = {'llSensor', 'llSensorRepeat'}
|
2019-11-03 01:08:53 +01:00
|
|
|
# not sure about llRemoteDataReply but let's fall on the safe side
|
2019-11-03 00:49:32 +01:00
|
|
|
NoKeyOptimizationFunctions = {'llMessageLinked', 'llRemoteDataReply'}
|
|
|
|
|
|
2017-08-25 20:11:16 +02:00
|
|
|
def OptimizeArgs(node, sym):
|
|
|
|
|
"""Transform function arguments to shorter equivalents where possible."""
|
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-28 00:19:08 +02:00
|
|
|
assert node.nt == 'FNCALL'
|
|
|
|
|
params = node.ch
|
|
|
|
|
name = node.name
|
2015-04-21 04:56:09 +02:00
|
|
|
|
2019-11-03 00:49:32 +01:00
|
|
|
if 'Loc' in sym:
|
|
|
|
|
# This is a UDF. We can't do anything for these.
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if name in SensorFunctions:
|
2015-04-21 04:56:09 +02:00
|
|
|
# The cutoff value is at a bit less than 3.1275 for some reason,
|
|
|
|
|
# but we use 3.14159.
|
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-28 00:19:08 +02:00
|
|
|
if (params[4].nt == 'CONST' and params[4].t == 'float'
|
|
|
|
|
and params[4].value > 3.14159):
|
|
|
|
|
params[4].value = 4.0
|
2015-04-21 04:56:09 +02:00
|
|
|
|
|
|
|
|
types = sym['ParamTypes']
|
2019-11-03 00:49:32 +01:00
|
|
|
if name not in NoKeyOptimizationFunctions:
|
|
|
|
|
# Transform invalid/null keys to "" with the exceptions above,
|
|
|
|
|
# e.g. llGetOwnerKey(NULL_KEY) -> llGetOwnerKey("")
|
2015-04-21 04:56:09 +02:00
|
|
|
for i in range(len(types)):
|
|
|
|
|
if types[i] == 'key':
|
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-28 00:19:08 +02:00
|
|
|
if params[i].nt == 'CONST':
|
|
|
|
|
if not lslfuncs.cond(Key(params[i].value)):
|
|
|
|
|
params[i].value = u""
|
|
|
|
|
params[i].type = 'string'
|
2017-08-25 20:11:16 +02:00
|
|
|
|
|
|
|
|
|
2022-05-13 14:41:36 +02:00
|
|
|
# llGetObjectDetails return types
|
|
|
|
|
# Last: 49 (OBJECT_TEXT_ALPHA).
|
|
|
|
|
objDetailsTypes = 'issvrvkkkiiififfffkiiiiiiffkiviiksiisiiiiiifssivvf'
|
|
|
|
|
|
|
|
|
|
# llGetParcelDetails return types
|
|
|
|
|
# Last: 13 (PARCEL_DETAILS_SCRIPT_DANGER)
|
|
|
|
|
parcelDetailsTypes = 'sskkikiiivviii'
|
|
|
|
|
parcelMediaQTypes = \
|
|
|
|
|
{ 4: 'k' # PARCEL_MEDIA_COMMAND_TEXTURE
|
|
|
|
|
, 5: 's' # PARCEL_MEDIA_COMMAND_URL
|
|
|
|
|
, 10: 's' # PARCEL_MEDIA_COMMAND_TYPE
|
|
|
|
|
, 11: 'ii' # PARCEL_MEDIA_COMMAND_SIZE
|
|
|
|
|
, 12: 's' # PARCEL_MEDIA_COMMAND_DESC
|
|
|
|
|
, 13: 'f' # PARCEL_MEDIA_COMMAND_LOOP
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# llGetPrimitiveParams and llGetLinkPrimitiveParams return types
|
2017-08-25 20:11:16 +02:00
|
|
|
primParamsTypes = \
|
2022-05-13 14:41:36 +02:00
|
|
|
{ # 0 (unassigned)
|
|
|
|
|
1: 'i*' # 1=PRIM_TYPE_LEGACY
|
|
|
|
|
, 2: 'i' # 2=PRIM_MATERIAL
|
|
|
|
|
, 3: 'i' # 3=PRIM_PHYSICS
|
|
|
|
|
, 4: 'i' # 4=PRIM_TEMP_ON_REZ
|
|
|
|
|
, 5: 'i' # 5=PRIM_PHANTOM
|
|
|
|
|
, 6: 'v' # 6=PRIM_POSITION
|
|
|
|
|
, 7: 'v' # 7=PRIM_SIZE
|
|
|
|
|
, 8: 'r' # 8=PRIM_ROTATION
|
|
|
|
|
, 9: 'i*' # 9=PRIM_TYPE
|
|
|
|
|
# 10 to 16: unassigned
|
|
|
|
|
, 17: 'svvf' # 17=PRIM_TEXTURE
|
|
|
|
|
, 18: 'vf' # 18=PRIM_COLOR
|
|
|
|
|
, 19: 'ii' # 19=PRIM_BUMP_SHINY
|
|
|
|
|
, 20: 'i' # 20=PRIM_FULLBRIGHT
|
|
|
|
|
, 21: 'iiffffv' # 21=PRIM_FLEXIBLE
|
|
|
|
|
, 22: 'i' # 22=PRIM_TEXGEN
|
|
|
|
|
, 23: 'ivfff' # 23=PRIM_POINT_LIGHT
|
|
|
|
|
# 24 :unassigned
|
|
|
|
|
, 25: 'f' # 25=PRIM_GLOW
|
|
|
|
|
, 26: 'svf' # 26=PRIM_TEXT
|
|
|
|
|
, 27: 's' # 27=PRIM_NAME
|
|
|
|
|
, 28: 's' # 28=PRIM_DESC
|
|
|
|
|
, 29: 'r' # 29=PRIM_ROT_LOCAL
|
|
|
|
|
, 30: 'i' # 30=PRIM_PHYSICS_SHAPE_TYPE
|
|
|
|
|
# 31: unassigned
|
|
|
|
|
, 32: 'vff' # 32=PRIM_OMEGA
|
|
|
|
|
, 33: 'v' # 33=PRIM_POS_LOCAL
|
|
|
|
|
, 34: '' # 34=PRIM_LINK_TARGET
|
|
|
|
|
, 35: 'v' # 35=PRIM_SLICE
|
|
|
|
|
, 36: 'svvfvii' # 36=PRIM_SPECULAR
|
|
|
|
|
, 37: 'svvf' # 37=PRIM_NORMAL
|
|
|
|
|
, 38: 'ii' # 38=PRIM_ALPHA_MODE
|
|
|
|
|
, 39: 'i' # 39=PRIM_ALLOW_UNSIT
|
|
|
|
|
, 40: 'i' # 40=PRIM_SCRIPTED_SIT_ONLY
|
|
|
|
|
, 41: 'ivv' # 41=PRIM_SIT_TARGET
|
|
|
|
|
, 42: 'sfff' # 42=PRIM_PROJECTOR
|
2023-12-12 16:52:45 +01:00
|
|
|
, 43: 'i' # 43=PRIM_CLICK_ACTION
|
|
|
|
|
, 44: 'iffi' # 44=PRIM_REFLECTION_PROBE
|
|
|
|
|
# GLTF parameters admit an empty string in any of the places except the
|
|
|
|
|
# first integer (face number). We're not prepared to deal with that.
|
|
|
|
|
# , 45: 'isvvf' # 45=PRIM_GLTF_NORMAL
|
|
|
|
|
# , 46: 'isvvfv' # 46=PRIM_GLTF_EMISSIVE
|
|
|
|
|
# , 47: 'isvvfff' # 47=PRIM_GLTF_METALLIC_ROUGHNESS
|
|
|
|
|
# , 48: 'isvvfvfifi' # 48=PRIM_GLTF_BASE_COLOR
|
|
|
|
|
, 45: 'i*' # 45=PRIM_GLTF_NORMAL
|
|
|
|
|
, 46: 'i*' # 46=PRIM_GLTF_EMISSIVE
|
|
|
|
|
, 47: 'i*' # 47=PRIM_GLTF_METALLIC_ROUGHNESS
|
|
|
|
|
, 48: 'i*' # 48=PRIM_GLTF_BASE_COLOR
|
2022-05-13 14:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# llGetPrimitiveParams parameters with arguments. F=face, L=link.
|
2017-08-25 20:11:16 +02:00
|
|
|
primParamsArgs = \
|
|
|
|
|
{ 17: 'F' # 17=PRIM_TEXTURE
|
|
|
|
|
, 18: 'F' # 18=PRIM_COLOR
|
|
|
|
|
, 19: 'F' # 19=PRIM_BUMP_SHINY
|
|
|
|
|
, 20: 'F' # 20=PRIM_FULLBRIGHT
|
|
|
|
|
, 22: 'F' # PRIM_TEXGEN
|
|
|
|
|
, 25: 'F' # PRIM_GLOW
|
|
|
|
|
, 34: 'L' # PRIM_LINK_TARGET
|
|
|
|
|
, 36: 'F' # PRIM_SPECULAR
|
|
|
|
|
, 37: 'F' # PRIM_NORMAL
|
|
|
|
|
, 38: 'F' # PRIM_ALPHA_MODE
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 14:41:36 +02:00
|
|
|
# llGetPrimMediaParams and llGetLinkMedia return types
|
|
|
|
|
# Last: 14 (PRIM_MEDIA_PERMS_CONTROL)
|
|
|
|
|
primMediaTypes = 'iissiiiiiiiisii'
|
|
|
|
|
|
|
|
|
|
# llGetEnvironment return types (not to be confused with llGetEnv)
|
|
|
|
|
getEnvTypes = \
|
|
|
|
|
{ 15: 'fff'
|
|
|
|
|
, 0: 'v'
|
|
|
|
|
, 1: 'iii'
|
|
|
|
|
, 2: 'vfffvvvi'
|
|
|
|
|
, 4: 'fff'
|
|
|
|
|
, 5: 'f'
|
|
|
|
|
, 6: 'ff'
|
|
|
|
|
, 9: 'rffivvv'
|
|
|
|
|
, 13: 'f'
|
|
|
|
|
, 14: 'rfvivvv'
|
|
|
|
|
, 10: 'fff'
|
|
|
|
|
, 11: 'fff'
|
|
|
|
|
, 8: 'vvv'
|
|
|
|
|
, 100: 'f'
|
|
|
|
|
, 101: 'vff'
|
|
|
|
|
, 102: 'ff'
|
|
|
|
|
, 103: 'ii'
|
|
|
|
|
, 104: 'v'
|
|
|
|
|
, 105: 'ff'
|
|
|
|
|
, 106: 'vv'
|
|
|
|
|
, 200: 'iif'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Types for llGetVisualDetails parameters (anything not in this list is
|
|
|
|
|
# returned as an empty string)
|
|
|
|
|
visualDetailsTypes = \
|
|
|
|
|
{ 33: 'f', 'height': 'f'
|
|
|
|
|
, 38: 'f', 'torso_length': 'f'
|
|
|
|
|
, 80: 'f', 'male': 'f'
|
|
|
|
|
, 198: 'f', 'heel_height': 'f'
|
|
|
|
|
, 503: 'f', 'platform_height': 'f'
|
|
|
|
|
, 616: 'f', 'shoe_height': 'f'
|
|
|
|
|
, 692: 'f', 'leg_length': 'f'
|
|
|
|
|
, 693: 'f', 'arm_length': 'f'
|
|
|
|
|
, 756: 'f', 'neck_length': 'f'
|
|
|
|
|
, 814: 'f', 'waist_height': 'f'
|
|
|
|
|
, 842: 'f', 'hip_length': 'f'
|
|
|
|
|
, 11001: 'f', 'hover': 'f'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Compatibility: list extraction function / input type (by type's initial),
|
|
|
|
|
# e.g. 'si' means llList2String can extract an integer.
|
|
|
|
|
listCompat = {'ss', 'sk', 'si', 'sf', 'sv', 'sr', 'ks', 'kk',
|
|
|
|
|
'is', 'ii', 'if', 'fs', 'fi', 'ff', 'vv', 'rr'}
|
2017-08-25 20:11:16 +02:00
|
|
|
|
|
|
|
|
defaultListVals = {'llList2Integer':0, 'llList2Float':0.0,
|
|
|
|
|
'llList2String':u'',
|
2017-08-25 20:45:24 +02:00
|
|
|
# llList2Key is set programmatically in FuncOptSetup
|
2017-08-25 20:11:16 +02:00
|
|
|
#'llList2Key':Key(u''),
|
|
|
|
|
'llList2Vector':Vector((0.,0.,0.)),
|
|
|
|
|
'llList2Rot':Quaternion((0.,0.,0.,1.))}
|
|
|
|
|
|
2018-11-17 10:19:43 +01:00
|
|
|
# Auxiliary function for llDumpList2String optimization
|
|
|
|
|
def CastDL2S(self, node, index):
|
|
|
|
|
"""Cast a list element to string, wrapping it in a list if it's a vector or
|
|
|
|
|
rotation.
|
|
|
|
|
"""
|
|
|
|
|
elem = self.GetListNodeElement(node, index)
|
|
|
|
|
assert elem is not False
|
|
|
|
|
if type(elem) != nr:
|
|
|
|
|
elem = nr(nt='CONST', t=lslcommon.PythonType2LSL[type(elem)], SEF=True,
|
|
|
|
|
value=elem)
|
2022-05-13 14:41:36 +02:00
|
|
|
if elem.t in {'vector', 'rotation'}:
|
2018-11-17 10:19:43 +01:00
|
|
|
return self.Cast(self.Cast(elem, 'list'), 'string')
|
|
|
|
|
return self.Cast(elem, 'string')
|
|
|
|
|
|
2018-11-19 14:13:10 +01:00
|
|
|
# Quick hack to work around lack of cached per-node ContainsFunctions info
|
|
|
|
|
def FnFree(self, node):
|
|
|
|
|
if node.nt == 'FNCALL':
|
|
|
|
|
return False
|
2022-05-13 14:41:36 +02:00
|
|
|
if node.nt in {'CONST', 'IDENT', 'FLD'}:
|
2018-11-19 14:13:10 +01:00
|
|
|
return True
|
|
|
|
|
return all(FnFree(self, node.ch[i]) for i in range(len(node.ch)))
|
|
|
|
|
|
2017-08-25 20:11:16 +02:00
|
|
|
# The 'self' parameter here is the constant folding object.
|
|
|
|
|
def OptimizeFunc(self, parent, index):
|
|
|
|
|
"""Look for possible optimizations taking advantage of the specific LSL
|
|
|
|
|
library function semantics.
|
|
|
|
|
"""
|
|
|
|
|
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-28 00:19:08 +02:00
|
|
|
assert node.nt == 'FNCALL'
|
|
|
|
|
name = node.name
|
|
|
|
|
child = node.ch
|
2017-08-25 20:11:16 +02:00
|
|
|
if self.optlistlength and name == 'llGetListLength':
|
|
|
|
|
# Convert llGetListLength(expr) to (expr != [])
|
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-28 00:19:08 +02:00
|
|
|
node = nr(nt='CONST', t='list', value=[], SEF=True)
|
|
|
|
|
parent[index] = node = nr(nt='!=', t='integer',
|
|
|
|
|
ch=[child[0], node], SEF=child[0].SEF)
|
2018-11-17 10:19:43 +01:00
|
|
|
|
2017-08-25 20:11:16 +02:00
|
|
|
if name == 'llDumpList2String':
|
2018-11-17 10:19:43 +01:00
|
|
|
assert child[0].t == 'list'
|
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-28 00:19:08 +02:00
|
|
|
if (child[1].nt == 'CONST'
|
2022-05-13 14:41:36 +02:00
|
|
|
and child[1].t in {'string', 'key'}
|
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-28 00:19:08 +02:00
|
|
|
and child[1].value == u""
|
2017-08-25 20:11:16 +02:00
|
|
|
):
|
|
|
|
|
# Convert llDumpList2String(expr, "") to (string)(expr)
|
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-28 00:19:08 +02:00
|
|
|
node.nt = 'CAST'
|
2017-08-25 20:11:16 +02:00
|
|
|
del child[1]
|
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-28 00:19:08 +02:00
|
|
|
del node.name
|
2017-08-25 20:11:16 +02:00
|
|
|
return
|
|
|
|
|
|
2018-11-19 18:47:50 +01:00
|
|
|
list_len = self.GetListNodeLength(child[0])
|
|
|
|
|
if list_len is not False and list_len == 1 and child[1].SEF:
|
|
|
|
|
# A single-element list can always be transformed regardless of
|
|
|
|
|
# the presence of function calls with side effects
|
|
|
|
|
parent[index] = CastDL2S(self, child[0], 0)
|
|
|
|
|
return
|
|
|
|
|
|
2018-11-17 10:19:43 +01:00
|
|
|
if node.SEF:
|
|
|
|
|
# Attempt to convert the function call into a sum of strings when
|
|
|
|
|
# possible and productive.
|
|
|
|
|
|
|
|
|
|
if list_len is False:
|
|
|
|
|
# Can't identify the length, which means we can't optimize.
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if list_len == 0:
|
|
|
|
|
# Empty list -> empty string, no matter the separator
|
|
|
|
|
# (remember we're SEF).
|
|
|
|
|
parent[index] = nr(nt='CONST', t='string', value=u'', SEF=True)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Only optimize if the second param is a very simple expression,
|
|
|
|
|
# otherwise the sums can get large.
|
2022-05-13 14:41:36 +02:00
|
|
|
if child[1].nt in {'CONST', 'IDENT'}:
|
2018-11-17 10:19:43 +01:00
|
|
|
threshold = 10
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Apply a threshold for optimizing as a sum.
|
|
|
|
|
if list_len > threshold:
|
|
|
|
|
return
|
|
|
|
|
|
2018-11-22 23:31:50 +01:00
|
|
|
elems = [self.GetListNodeElement(child[0], i)
|
|
|
|
|
for i in range(list_len)]
|
2018-11-17 10:19:43 +01:00
|
|
|
|
2018-11-22 23:31:50 +01:00
|
|
|
# Don't optimize if an element can't be extracted or is a list
|
|
|
|
|
if any(i is False or type(i) == nr and i.t == 'list'
|
|
|
|
|
for i in elems):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# We reorder list constructors as right-to-left sums. When an
|
|
|
|
|
# element contains function calls, it will generate a savepoint,
|
|
|
|
|
# but with that strategy, the maximum extra stack size at the time
|
|
|
|
|
# of each savepoint is 1.
|
|
|
|
|
# If the first element has a function call, we may end up causing
|
|
|
|
|
# more memory usage, because in a list constructor, the first
|
|
|
|
|
# element has no stack to save; however, if any elements past the
|
|
|
|
|
# third have function calls at the same time, the memory we add
|
|
|
|
|
# will be compensated by the memory we save, because the 3rd
|
|
|
|
|
# element has 2 elements in the stack, therefore reducing it to 1
|
|
|
|
|
# is a save; similarly, any elements past the 3rd containing
|
|
|
|
|
# function calls cause bigger and bigger saves.
|
|
|
|
|
|
|
|
|
|
# Since we're also eliminating the llDumpList2String function call,
|
|
|
|
|
# that may count for the extra stack element added. Therefore, we
|
|
|
|
|
# disable this condition and optimize unconditionally.
|
|
|
|
|
|
|
|
|
|
#if (child[0].nt in ('LIST', 'CONST') and list_len >= 3
|
|
|
|
|
# and type(elems[0]) == nr and not FnFree(self, elems[0])
|
|
|
|
|
# and all(type(i) != nr or FnFree(self, i) for i in elems[2:])
|
|
|
|
|
# ):
|
|
|
|
|
# return
|
|
|
|
|
|
|
|
|
|
# Optimize to a sum of strings, right-to-left to save stack.
|
|
|
|
|
i = list_len - 1
|
|
|
|
|
newnode = CastDL2S(self, child[0], i)
|
|
|
|
|
while i > 0:
|
|
|
|
|
i -= 1
|
|
|
|
|
newnode = nr(nt='+', t='string', SEF=True,
|
|
|
|
|
ch=[CastDL2S(self, child[0], i),
|
|
|
|
|
nr(nt='+', t='string', SEF=True,
|
2023-02-26 09:41:46 +01:00
|
|
|
ch=[self.Cast(child[1], 'string').copy(), newnode]
|
2018-11-22 23:31:50 +01:00
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
parent[index] = newnode
|
|
|
|
|
# Re-fold
|
|
|
|
|
self.FoldTree(parent, index)
|
2018-11-17 10:19:43 +01:00
|
|
|
return
|
|
|
|
|
|
2022-05-13 14:41:36 +02:00
|
|
|
if (name in {'llList2String', 'llList2Key', 'llList2Integer',
|
|
|
|
|
'llList2Float', 'llList2Vector', 'llList2Rot'}
|
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-28 00:19:08 +02:00
|
|
|
and child[1].nt == 'CONST'
|
2017-08-25 20:11:16 +02:00
|
|
|
):
|
|
|
|
|
# 2nd arg to llList2XXXX must be integer
|
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-28 00:19:08 +02:00
|
|
|
assert child[1].t == 'integer'
|
2017-08-25 20:11:16 +02:00
|
|
|
|
|
|
|
|
listarg = 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-28 00:19:08 +02:00
|
|
|
idx = child[1].value
|
2017-08-25 20:11:16 +02:00
|
|
|
value = self.GetListNodeElement(listarg, idx)
|
|
|
|
|
tvalue = self.TypeFromNodeOrConst(value)
|
|
|
|
|
const = self.ConstFromNodeOrConst(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-28 00:19:08 +02:00
|
|
|
if const is not False and node.SEF:
|
2017-08-25 20:11:16 +02:00
|
|
|
# Managed to get a constant from a list, even if the
|
|
|
|
|
# list wasn't constant. Handle the type conversion.
|
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-28 00:19:08 +02:00
|
|
|
if (node.t[0] + tvalue[0]) in listCompat:
|
2017-08-25 20:11:16 +02:00
|
|
|
const = lslfuncs.InternalTypecast(const,
|
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-28 00:19:08 +02:00
|
|
|
lslcommon.LSLType2Python[node.t],
|
2017-08-25 20:11:16 +02:00
|
|
|
InList=True, f32=True)
|
|
|
|
|
else:
|
|
|
|
|
const = defaultListVals[name]
|
|
|
|
|
|
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-28 00:19:08 +02:00
|
|
|
parent[index] = nr(nt='CONST', t=node.t, value=const, SEF=True)
|
2017-08-25 20:11:16 +02:00
|
|
|
return
|
|
|
|
|
|
2022-05-13 14:41:36 +02:00
|
|
|
if listarg.nt == 'FNCALL':
|
|
|
|
|
if listarg.name == 'llGetObjectDetails':
|
|
|
|
|
|
|
|
|
|
# make it the list argument of llGetObjectDetails
|
|
|
|
|
listarg = listarg.ch[1]
|
|
|
|
|
value = self.GetListNodeElement(listarg, idx)
|
|
|
|
|
tvalue = self.TypeFromNodeOrConst(value)
|
|
|
|
|
const = self.ConstFromNodeOrConst(value)
|
|
|
|
|
if type(const) == int and self.GetListNodeLength(listarg) == 1:
|
|
|
|
|
# Some of these can be handled with a typecast to string.
|
|
|
|
|
if name == 'llList2String':
|
|
|
|
|
# turn the node into a cast of arg 0 to string
|
|
|
|
|
node.nt = 'CAST'
|
|
|
|
|
del child[1]
|
|
|
|
|
del node.name
|
|
|
|
|
return
|
|
|
|
|
# The other ones that support cast to string then to
|
|
|
|
|
# the final type in some cases (depending on the
|
|
|
|
|
# list type, which we know) are key/int/float.
|
|
|
|
|
finaltype = objDetailsTypes[const:const+1]
|
|
|
|
|
if (name == 'llList2Key' # checked via listCompat
|
|
|
|
|
or (name == 'llList2Integer'
|
|
|
|
|
and finaltype in {'s', 'i'}) # won't work for floats
|
|
|
|
|
or (name == 'llList2Float'
|
|
|
|
|
and finaltype in {'s', 'i'}) # won't work for floats
|
|
|
|
|
) and (node.t[0] + finaltype) in listCompat:
|
|
|
|
|
# -> (key)((string)llGetObjectDetails...)
|
|
|
|
|
# or (integer)((string)llGetObjectDetails...)
|
|
|
|
|
node.nt = 'CAST'
|
|
|
|
|
del child[1]
|
|
|
|
|
del node.name
|
|
|
|
|
child[0] = self.Cast(child[0], 'string')
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Check for type incompatibility or index out of range
|
|
|
|
|
# and replace node with a constant if that's the case
|
|
|
|
|
if (value is False
|
|
|
|
|
or type(const) == int
|
|
|
|
|
and (node.t[0] + objDetailsTypes[const])
|
|
|
|
|
not in listCompat
|
|
|
|
|
) and node.SEF:
|
|
|
|
|
parent[index] = nr(nt='CONST', t=node.t,
|
|
|
|
|
value=defaultListVals[name], SEF=True)
|
|
|
|
|
|
|
|
|
|
elif listarg.name in {'llGetPrimitiveParams',
|
|
|
|
|
'llGetLinkPrimitiveParams'}:
|
|
|
|
|
# We're going to work with the primitive params list.
|
|
|
|
|
listarg = listarg.ch[
|
|
|
|
|
0 if listarg.name == 'llGetPrimitiveParams'
|
|
|
|
|
else 1]
|
|
|
|
|
length = self.GetListNodeLength(listarg)
|
|
|
|
|
if length is not False:
|
|
|
|
|
# Construct a list (string) of return types.
|
|
|
|
|
# A '*' in the list means the type can't be
|
|
|
|
|
# determined past this point (used with PRIM_TYPE).
|
|
|
|
|
i = 0
|
|
|
|
|
returntypes = ''
|
|
|
|
|
while i < length:
|
|
|
|
|
param = self.GetListNodeElement(listarg, i)
|
|
|
|
|
param = self.ConstFromNodeOrConst(param)
|
|
|
|
|
if (param is False
|
|
|
|
|
or type(param) != int
|
|
|
|
|
# Parameters with arguments have
|
|
|
|
|
# side effects (errors).
|
|
|
|
|
# We could check whether there's a face
|
|
|
|
|
# argument and the face is 0, which is
|
|
|
|
|
# guaranteed to exist, but it's not worth
|
|
|
|
|
# the effort.
|
|
|
|
|
or param in primParamsArgs
|
|
|
|
|
or param not in primParamsTypes
|
|
|
|
|
):
|
|
|
|
|
# Can't process this list.
|
|
|
|
|
returntypes = '!'
|
|
|
|
|
break
|
|
|
|
|
returntypes += primParamsTypes[param]
|
|
|
|
|
i += 1
|
|
|
|
|
if returntypes != '!':
|
|
|
|
|
if (len(returntypes) == 1
|
|
|
|
|
and returntypes != '*'
|
|
|
|
|
and idx in {0, -1}
|
|
|
|
|
):
|
|
|
|
|
if name == 'llList2String':
|
|
|
|
|
node.nt = 'CAST'
|
|
|
|
|
del child[1]
|
|
|
|
|
del node.name
|
|
|
|
|
return
|
|
|
|
|
if ((name == 'llList2Key'
|
|
|
|
|
or name == 'llList2Integer'
|
|
|
|
|
and returntypes in {'s', 'i'}
|
|
|
|
|
or name == 'llList2Float'
|
|
|
|
|
and returntypes in {'s', 'i'}
|
|
|
|
|
)
|
|
|
|
|
and (node.t[0] + returntypes)
|
|
|
|
|
in listCompat
|
|
|
|
|
):
|
|
|
|
|
node.nt = 'CAST'
|
|
|
|
|
del child[1]
|
|
|
|
|
del node.name
|
|
|
|
|
child[0] = nr(nt='CAST', t='string',
|
|
|
|
|
ch=[child[0]], SEF=child[0].SEF)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# The position of parameters past the first asterisk can't
|
|
|
|
|
# be determined, so we only consider parameters before it.
|
|
|
|
|
asteriskPos = returntypes.find('*')
|
|
|
|
|
if (asteriskPos == -1
|
|
|
|
|
or 0 <= idx < asteriskPos
|
|
|
|
|
or asteriskPos - len(returntypes) < idx < 0
|
2017-08-25 20:11:16 +02:00
|
|
|
):
|
2022-05-13 14:41:36 +02:00
|
|
|
# Check for type incompatibility or index
|
|
|
|
|
# out of range.
|
|
|
|
|
if idx < 0:
|
|
|
|
|
# s[-1:0] doesn't return the last char
|
|
|
|
|
# so we make it positive to ensure correctness
|
|
|
|
|
idx += len(returntypes)
|
|
|
|
|
if ((node.t[0] + returntypes[idx:idx+1])
|
|
|
|
|
not in listCompat
|
|
|
|
|
and node.SEF):
|
|
|
|
|
parent[index] = nr(nt='CONST', t=node.t,
|
|
|
|
|
value=defaultListVals[name], SEF=True)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
del returntypes
|
|
|
|
|
|
|
|
|
|
elif listarg.name == 'llGetParcelDetails':
|
|
|
|
|
listarg = listarg.ch[1] # 0 is vector pos
|
|
|
|
|
# TODO: llList2XXX(llGetParcelDetails)
|
|
|
|
|
|
|
|
|
|
elif listarg.name == 'llParcelMediaQuery':
|
|
|
|
|
listarg = listarg.ch[0]
|
|
|
|
|
# TODO: llList2XXX(llParcelMediaQuery)
|
|
|
|
|
|
|
|
|
|
elif listarg.name in {'llGetPrimMediaParams', 'llGetLinkMedia'}:
|
|
|
|
|
listarg = listarg.ch[
|
|
|
|
|
1 if listarg.name == 'llGetPrimMediaParams'
|
|
|
|
|
else 2]
|
|
|
|
|
# TODO: llList2XXX(llGetPrimMediaXXX)
|
|
|
|
|
|
|
|
|
|
elif listarg.name == 'llGetEnvironment':
|
|
|
|
|
listarg = listarg.ch[1] # 0 is vector pos
|
|
|
|
|
# TODO: llList2XXX(llGetEnvironment)
|
|
|
|
|
|
|
|
|
|
elif listarg.name == 'llGetVisualParams':
|
|
|
|
|
listarg = listarg.ch[1] # 0 is id
|
|
|
|
|
# TODO: llList2XXX(llGetVisualParams)
|
2017-08-25 20:11:16 +02:00
|
|
|
|
|
|
|
|
del listarg, idx, value, tvalue, const
|
2017-08-25 20:22:22 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if name == 'llDialog':
|
|
|
|
|
if self.GetListNodeLength(child[2]) == 1:
|
2019-11-03 00:49:32 +01:00
|
|
|
button = self.ConstFromNodeOrConst(
|
|
|
|
|
self.GetListNodeElement(child[2], 0))
|
2017-08-25 20:22:22 +02:00
|
|
|
if type(button) == unicode and button == u'OK':
|
|
|
|
|
# remove the element, as 'OK' is the default button in SL
|
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-28 00:19:08 +02:00
|
|
|
child[2] = nr(nt='CONST', t='list', value=[], SEF=True)
|
2017-08-25 20:22:22 +02:00
|
|
|
return
|
2017-08-25 20:11:16 +02:00
|
|
|
|
2017-09-15 23:13:59 +02:00
|
|
|
if (name == 'llDeleteSubList'
|
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-28 00:19:08 +02:00
|
|
|
or name == 'llListReplaceList' and child[1].nt == 'CONST'
|
|
|
|
|
and not child[1].value
|
2017-09-15 23:13:59 +02:00
|
|
|
):
|
|
|
|
|
# llDeleteSubList(x, 0, -1) -> [] if x is SEF
|
|
|
|
|
# llListReplaceList(x, [], 0, -1) -> [] if x is SEF
|
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-28 00:19:08 +02:00
|
|
|
if (child[0].SEF
|
|
|
|
|
and child[-2].nt == 'CONST' and child[-1].nt == 'CONST'
|
|
|
|
|
and child[-2].value == 0 and child[-1].value == -1
|
2017-09-15 23:13:59 +02:00
|
|
|
):
|
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-28 00:19:08 +02:00
|
|
|
parent[index] = nr(nt='CONST', t='list', value=[], SEF=True)
|
2017-09-15 23:13:59 +02:00
|
|
|
return
|
|
|
|
|
|
2017-08-25 20:11:16 +02:00
|
|
|
def FuncOptSetup():
|
|
|
|
|
# Patch the default values list for LSO
|
|
|
|
|
if lslcommon.LSO:
|
|
|
|
|
defaultListVals['llList2Key'] = Key(lslfuncs.NULL_KEY)
|
|
|
|
|
else:
|
|
|
|
|
defaultListVals['llList2Key'] = Key(u"")
|