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.
This commit is contained in:
Sei Lisa 2018-03-28 00:19:08 +02:00
parent d890f0b5fa
commit 075d3aba0c
9 changed files with 1294 additions and 1201 deletions

View file

@ -18,6 +18,7 @@
# Optimizations that have a negative effect on other stages.
import lslcommon
from lslcommon import nr
#from lslcommon import Vector, Quaternion
#import lslfuncs
#from lslfuncs import ZERO_VECTOR, ZERO_ROTATION
@ -28,14 +29,14 @@ import lslcommon
class lastpass(object):
def LastPassPreOrder(self, parent, index):
node = parent[index]
nt = node['nt']
child = node['ch'] if 'ch' in node else None
nt = node.nt
child = node.ch
if (self.optlistadd and not self.globalmode
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')
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')
)
):
# Perform these transformations if the list is SEF:
@ -53,17 +54,16 @@ class lastpass(object):
# 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
if 'SEF' in listnode:
for elem in (listnode['value'] if listnode['nt'] == 'CONST'
else listnode['ch']):
elemnode = {'nt':'CONST',
't':lslcommon.PythonType2LSL[type(elem)],
'SEF':True,
'value':elem
} if listnode['nt'] == 'CONST' else elem
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
left = (self.Cast(elemnode, 'list') if left is None
else {'nt':'+', 't':'list', 'SEF':True,
'ch':[left, elemnode]})
else nr(nt='+', t='list', SEF=True,
ch=[left, elemnode]))
del elemnode
if left is not None: # it's none for empty lists
parent[index] = left
@ -78,7 +78,7 @@ class lastpass(object):
# where state changes are considered bad.
# BadStCh will be True if at least one state change statement
# is found while monitoring state changes.
self.subinfo['StChAreBad'] = 'scope' in node
self.subinfo['StChAreBad'] = hasattr(node, 'scope')
self.BadStCh = False
return
@ -109,38 +109,38 @@ class lastpass(object):
if nt == 'FNCALL':
# lslrenamer can benefit from a list of used library functions,
# so provide it.
if 'Loc' not in self.symtab[0][node['name']]:
if 'Loc' not in self.symtab[0][node.name]:
# system library function
self.usedlibfuncs.add(node['name'])
self.usedlibfuncs.add(node.name)
def LastPassPostOrder(self, parent, index):
node = parent[index]
nt = node['nt']
child = node['ch'] if 'ch' in node else None
nt = node.nt
child = node.ch
if nt == 'FNDEF':
if 'scope' in node and self.BadStCh:
if hasattr(node, 'scope') and self.BadStCh:
# 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).
child[0] = {'nt':'{}', 't':None, 'ch':[
{'nt':'IF', 't':None, 'ch':[
{'nt':'CONST', 't':'integer', 'value':1},
child[0] = nr(nt='{}', t=None, ch=[
nr(nt='IF', t=None, ch=[
nr(nt='CONST', t='integer', value=1, SEF=True),
child[0]
]}
]}
child = node['ch']
if node['t'] is not None:
])
])
child = node.ch
if node.t is not None:
# 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.
child[0]['ch'].append({'nt':'RETURN', 't':None, 'ch':[
{'nt':'CONST', 't':node['t'],
'value':lslcommon.LSLTypeDefaults[node['t']]
}]
})
child[0].ch.append(nr(nt='RETURN', t=None, ch=[
nr(nt='CONST', t=node.t, SEF=True,
value=lslcommon.LSLTypeDefaults[node.t]
)]
))
del self.BadStCh
return
@ -150,8 +150,8 @@ class lastpass(object):
self.subinfo = subinfo.copy()
self.LastPassPreOrder(parent, index)
if 'ch' in parent[index]:
child = parent[index]['ch']
if parent[index].ch is not None:
child = parent[index].ch
idx = 0
while idx < len(child):
self.RecursiveLastPass(child, idx)
@ -172,7 +172,7 @@ class lastpass(object):
# self.subinfo is subtree-local info.
self.subinfo = {}
for idx in xrange(len(tree)):
if tree[idx]['nt'] == 'DECL':
if tree[idx].nt == 'DECL':
self.globalmode = True
self.RecursiveLastPass(tree, idx)
self.globalmode = False