mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-03 00:18:20 +00:00
Rather than generating random identifiers for locals, prefix them with 'loc_'.
Should help improving readability of the optimized script.
This commit is contained in:
parent
a292846d28
commit
1e8b77bf50
1 changed files with 9 additions and 19 deletions
|
@ -25,9 +25,6 @@
|
||||||
|
|
||||||
# TODO: Rename locals to loc_<identifier> rather than random names.
|
# TODO: Rename locals to loc_<identifier> rather than random names.
|
||||||
|
|
||||||
import random
|
|
||||||
from base64 import b64encode
|
|
||||||
|
|
||||||
class renamer(object):
|
class renamer(object):
|
||||||
CharSet1 = '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
CharSet1 = '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
||||||
CharSet2 = '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
CharSet2 = '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
||||||
|
@ -67,8 +64,6 @@ class renamer(object):
|
||||||
# Names from ReusableNames that have already been used
|
# Names from ReusableNames that have already been used
|
||||||
self.UsedNames = set()
|
self.UsedNames = set()
|
||||||
|
|
||||||
UsedLocals = set()
|
|
||||||
|
|
||||||
# Make a first pass to separate the symbols into three categories.
|
# Make a first pass to separate the symbols into three categories.
|
||||||
globalvars = []
|
globalvars = []
|
||||||
states = []
|
states = []
|
||||||
|
@ -97,7 +92,7 @@ class renamer(object):
|
||||||
# the event name, e.g. edefaultstate_entry. Since a new identifier
|
# the event name, e.g. edefaultstate_entry. Since a new identifier
|
||||||
# having part of the state name is created for every event in that
|
# having part of the state name is created for every event in that
|
||||||
# state, the shortest the state name, the least bytes it will use.
|
# state, the shortest the state name, the least bytes it will use.
|
||||||
# Furthermore, a state switch instruction further adds an Unicode
|
# Furthermore, a state switch instruction further adds a Unicode
|
||||||
# string (all other identifier names use one-byte strings), which
|
# string (all other identifier names use one-byte strings), which
|
||||||
# is the more reason to shorten it as much as possible.
|
# is the more reason to shorten it as much as possible.
|
||||||
#
|
#
|
||||||
|
@ -117,7 +112,7 @@ class renamer(object):
|
||||||
del states
|
del states
|
||||||
|
|
||||||
for name in functions:
|
for name in functions:
|
||||||
# Assign a new name. Internal function names get a 'g' prepended
|
# Assign a new name. Internally, function names get a 'g' prepended
|
||||||
# to them, so these are candidates for reuse too.
|
# to them, so these are candidates for reuse too.
|
||||||
|
|
||||||
# Unfortunately, we won't find any reusable name starting with 'g'
|
# Unfortunately, we won't find any reusable name starting with 'g'
|
||||||
|
@ -150,7 +145,7 @@ class renamer(object):
|
||||||
if name == -1: continue
|
if name == -1: continue
|
||||||
if sym['Kind'] != 'v':
|
if sym['Kind'] != 'v':
|
||||||
assert sym['Kind'] == 'l'
|
assert sym['Kind'] == 'l'
|
||||||
name = name # trick the optimizer
|
name = name # trick python to not optimize out the jump
|
||||||
continue
|
continue
|
||||||
if 'Param' in sym:
|
if 'Param' in sym:
|
||||||
if not InParams:
|
if not InParams:
|
||||||
|
@ -168,17 +163,12 @@ class renamer(object):
|
||||||
short = self.GetNextShortest()
|
short = self.GetNextShortest()
|
||||||
table[name]['NewName'] = short
|
table[name]['NewName'] = short
|
||||||
else:
|
else:
|
||||||
# Generate new identifier
|
# Generate new identifier, by prepending the four character
|
||||||
while True:
|
# string 'loc_'. This generates identifiers with five chars
|
||||||
x = random.randint(0, 16777215)
|
# or more. We assume that is enough for them to stay safe
|
||||||
unique = 'L_' + b64encode(chr(x>>16) + chr((x>>8)&255)
|
# from name collisions with globals and parameter names.
|
||||||
+ chr(x&255)).replace('+', '_')
|
# Four letters allow for more than 1.4 million identifiers.
|
||||||
x = random.randint(0, 16777215)
|
unique = 'loc_' + name
|
||||||
unique += b64encode(chr(x>>16) + chr((x>>8)&255)
|
|
||||||
+ chr(x&255)).replace('+', '_')
|
|
||||||
if '/' not in unique not in UsedLocals:
|
|
||||||
break
|
|
||||||
UsedLocals.add(unique)
|
|
||||||
table[name]['NewName'] = unique
|
table[name]['NewName'] = unique
|
||||||
|
|
||||||
del globalvars
|
del globalvars
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue