mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Inliner: Add and use newSymtab() and newId()
This commit is contained in:
parent
a800604632
commit
852fec2f26
3 changed files with 27 additions and 27 deletions
|
@ -34,6 +34,15 @@ class EExpansionLoop(Exception):
|
||||||
u" inline functions")
|
u" inline functions")
|
||||||
|
|
||||||
class inliner(object):
|
class inliner(object):
|
||||||
|
def newId(self, namespace):
|
||||||
|
"""Create a new identifier based on a namespace"""
|
||||||
|
self.symCtr[namespace] = self.symCtr.get(namespace, 0) + 1
|
||||||
|
return '___%s__%05d' % (namespace, self.symCtr[namespace])
|
||||||
|
|
||||||
|
def newSymtab(self):
|
||||||
|
self.symtab.append({})
|
||||||
|
return len(self.symtab) - 1
|
||||||
|
|
||||||
def FixJumps(self, node):
|
def FixJumps(self, node):
|
||||||
"""Change name and scope of JUMPs to point to the correct symtab entry
|
"""Change name and scope of JUMPs to point to the correct symtab entry
|
||||||
"""
|
"""
|
||||||
|
@ -89,9 +98,8 @@ class inliner(object):
|
||||||
|
|
||||||
if nt == '{}':
|
if nt == '{}':
|
||||||
copy = node.copy()
|
copy = node.copy()
|
||||||
copy.scope = len(self.symtab)
|
copy.scope = self.newSymtab()
|
||||||
copy.ch = []
|
copy.ch = []
|
||||||
self.symtab.append({})
|
|
||||||
for i in node.ch:
|
for i in node.ch:
|
||||||
copy.ch.append(self.GetFuncCopy(i, node.scope))
|
copy.ch.append(self.GetFuncCopy(i, node.scope))
|
||||||
if i.nt == 'DECL':
|
if i.nt == 'DECL':
|
||||||
|
@ -106,8 +114,7 @@ class inliner(object):
|
||||||
copy = node.copy()
|
copy = node.copy()
|
||||||
oldscope = node.scope
|
oldscope = node.scope
|
||||||
oldname = node.name
|
oldname = node.name
|
||||||
self.lblCount += 1
|
copy.name = self.newId('lbl')
|
||||||
copy.name = '___lbl__%05d' % self.lblCount
|
|
||||||
copy.scope = scope
|
copy.scope = scope
|
||||||
if copy.name in self.symtab[scope]:
|
if copy.name in self.symtab[scope]:
|
||||||
raise ENameAlreadyExists(
|
raise ENameAlreadyExists(
|
||||||
|
@ -124,7 +131,7 @@ class inliner(object):
|
||||||
if node.ch:
|
if node.ch:
|
||||||
# Returns a value. Wrap in {} and add an assignment.
|
# Returns a value. Wrap in {} and add an assignment.
|
||||||
# BUG: We don't honour ExplicitCast here.
|
# BUG: We don't honour ExplicitCast here.
|
||||||
newnode = nr(nt='{}', t=None, scope=len(self.symtab), ch=[
|
newnode = nr(nt='{}', t=None, scope=self.newSymtab(), ch=[
|
||||||
nr(nt='EXPR', t=self.rettype, ch=[
|
nr(nt='EXPR', t=self.rettype, ch=[
|
||||||
nr(nt='=', t=self.rettype, ch=[
|
nr(nt='=', t=self.rettype, ch=[
|
||||||
nr(nt='IDENT', t=node.ch[0].t,
|
nr(nt='IDENT', t=node.ch[0].t,
|
||||||
|
@ -133,7 +140,6 @@ class inliner(object):
|
||||||
])
|
])
|
||||||
]), newnode
|
]), newnode
|
||||||
])
|
])
|
||||||
self.symtab.append({})
|
|
||||||
return newnode
|
return newnode
|
||||||
|
|
||||||
if nt == 'IDENT':
|
if nt == 'IDENT':
|
||||||
|
@ -164,8 +170,7 @@ class inliner(object):
|
||||||
retvar = None
|
retvar = None
|
||||||
if rettype is not None:
|
if rettype is not None:
|
||||||
# Returns a value. Create a local variable at the starting level.
|
# Returns a value. Create a local variable at the starting level.
|
||||||
self.retCount += 1
|
retvar = self.newId('ret')
|
||||||
retvar = '___ret__%05d' % self.retCount
|
|
||||||
if retvar in self.symtab[scope]:
|
if retvar in self.symtab[scope]:
|
||||||
raise ENameAlreadyExists(u"Symbol %s already exists"
|
raise ENameAlreadyExists(u"Symbol %s already exists"
|
||||||
% retvar.decode('utf8'))
|
% retvar.decode('utf8'))
|
||||||
|
@ -182,8 +187,7 @@ class inliner(object):
|
||||||
outer = None
|
outer = None
|
||||||
if fnsym['ParamNames']:
|
if fnsym['ParamNames']:
|
||||||
# Add a new block + symbols + assignments for parameter values
|
# Add a new block + symbols + assignments for parameter values
|
||||||
pscope = len(self.symtab)
|
pscope = self.newSymtab()
|
||||||
self.symtab.append({})
|
|
||||||
outer = nr(nt='{}', t=None, scope=pscope, ch=[])
|
outer = nr(nt='{}', t=None, scope=pscope, ch=[])
|
||||||
origpscope = self.tree[fnsym['Loc']].pscope
|
origpscope = self.tree[fnsym['Loc']].pscope
|
||||||
for i in range(len(fnsym['ParamNames'])):
|
for i in range(len(fnsym['ParamNames'])):
|
||||||
|
@ -202,8 +206,7 @@ class inliner(object):
|
||||||
self.retvar = retvar
|
self.retvar = retvar
|
||||||
self.retscope = scope
|
self.retscope = scope
|
||||||
self.retlscope = scope
|
self.retlscope = scope
|
||||||
self.lblCount += 1
|
retlabel = self.newId('rtl')
|
||||||
retlabel = '___rtl__%05d' % self.lblCount
|
|
||||||
self.retlabel = retlabel
|
self.retlabel = retlabel
|
||||||
self.symtab[scope][retlabel] = {'Type':'l', 'Scope':scope, 'ref':0}
|
self.symtab[scope][retlabel] = {'Type':'l', 'Scope':scope, 'ref':0}
|
||||||
|
|
||||||
|
@ -247,8 +250,7 @@ class inliner(object):
|
||||||
|
|
||||||
def RecurseSingleStatement(self, parent, index, scope):
|
def RecurseSingleStatement(self, parent, index, scope):
|
||||||
# Synthesize a block node whose child is the statement.
|
# Synthesize a block node whose child is the statement.
|
||||||
newscope = len(self.symtab)
|
newscope = self.newSymtab()
|
||||||
self.symtab.append({})
|
|
||||||
node = nr(nt='{}', t=None, scope=newscope, ch=[parent[index]],
|
node = nr(nt='{}', t=None, scope=newscope, ch=[parent[index]],
|
||||||
SEF=parent[index].SEF)
|
SEF=parent[index].SEF)
|
||||||
|
|
||||||
|
@ -352,7 +354,7 @@ class inliner(object):
|
||||||
# llOwnerSay("doing stuff");
|
# llOwnerSay("doing stuff");
|
||||||
# while (f());
|
# while (f());
|
||||||
#
|
#
|
||||||
# should be converted to:
|
# is converted to:
|
||||||
#
|
#
|
||||||
# integer ___ret__00001;
|
# integer ___ret__00001;
|
||||||
# do
|
# do
|
||||||
|
@ -379,8 +381,7 @@ class inliner(object):
|
||||||
if child[0].nt != '{}':
|
if child[0].nt != '{}':
|
||||||
# Needs wrapping now
|
# Needs wrapping now
|
||||||
child[0] = nr(nt='{}', t=None, ch=[child[0]],
|
child[0] = nr(nt='{}', t=None, ch=[child[0]],
|
||||||
scope=len(self.symtab))
|
scope=self.newSymtab())
|
||||||
self.symtab.append({})
|
|
||||||
child[0].ch.append(fns.pop(i))
|
child[0].ch.append(fns.pop(i))
|
||||||
else:
|
else:
|
||||||
i += 1
|
i += 1
|
||||||
|
@ -409,8 +410,7 @@ class inliner(object):
|
||||||
def inline(self, tree, symtab):
|
def inline(self, tree, symtab):
|
||||||
self.tree = tree
|
self.tree = tree
|
||||||
self.symtab = symtab
|
self.symtab = symtab
|
||||||
self.retCount = 0
|
self.symCtr = {}
|
||||||
self.lblCount = 0
|
|
||||||
self.expanding = []
|
self.expanding = []
|
||||||
for i in range(len(tree)):
|
for i in range(len(tree)):
|
||||||
if tree[i].nt == 'STDEF':
|
if tree[i].nt == 'STDEF':
|
||||||
|
|
|
@ -28,10 +28,10 @@ default
|
||||||
}
|
}
|
||||||
integer f3p1;
|
integer f3p1;
|
||||||
{
|
{
|
||||||
jump ___lbl__00005;
|
jump ___lbl__00001;
|
||||||
llOwnerSay("f3:" + (string)f3p1 + f3p2);
|
llOwnerSay("f3:" + (string)f3p1 + f3p2);
|
||||||
}
|
}
|
||||||
@___lbl__00005;
|
@___lbl__00001;
|
||||||
if (f3p2 != "")
|
if (f3p2 != "")
|
||||||
{
|
{
|
||||||
___ret__00001 = <1, 1, 1>;
|
___ret__00001 = <1, 1, 1>;
|
||||||
|
@ -44,10 +44,10 @@ default
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
___ret__00002 = llGetLinkNumber();
|
___ret__00002 = llGetLinkNumber();
|
||||||
jump ___rtl__00007;
|
jump ___rtl__00006;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@___rtl__00007;
|
@___rtl__00006;
|
||||||
}
|
}
|
||||||
while (___ret__00002);
|
while (___ret__00002);
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,9 +22,9 @@ default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
jump ___lbl__00005;
|
jump ___lbl__00001;
|
||||||
}
|
}
|
||||||
@___lbl__00005;
|
@___lbl__00001;
|
||||||
{
|
{
|
||||||
___ret__00001 = <((float)1), ((float)1), ((float)1)>;
|
___ret__00001 = <((float)1), ((float)1), ((float)1)>;
|
||||||
jump ___rtl__00004;
|
jump ___rtl__00004;
|
||||||
|
@ -35,10 +35,10 @@ default
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
___ret__00002 = llGetLinkNumber();
|
___ret__00002 = llGetLinkNumber();
|
||||||
jump ___rtl__00007;
|
jump ___rtl__00006;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@___rtl__00007;
|
@___rtl__00006;
|
||||||
}
|
}
|
||||||
while (___ret__00002);
|
while (___ret__00002);
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue