Add "addstrings" option (disabled by default) to select whether to automatically concatenate strings during constant folding.

This commit is contained in:
Sei Lisa 2015-02-28 20:01:51 +01:00
parent 6ea01c4242
commit 8f83e2f1ab
3 changed files with 14 additions and 7 deletions

View file

@ -225,7 +225,8 @@ class foldconst(object):
op1 = lval['value'] op1 = lval['value']
op2 = rval['value'] op2 = rval['value']
if nt == '+': if nt == '+':
result = lslfuncs.add(op1, op2) if ltype != 'string' or rtype != 'string' or self.addstrings:
result = lslfuncs.add(op1, op2)
elif nt == '-': elif nt == '-':
result = lslfuncs.sub(op1, op2) result = lslfuncs.sub(op1, op2)
elif nt == '*': elif nt == '*':

View file

@ -45,6 +45,9 @@ class optimizer(foldconst, renamer, deadcode):
if 'optimize' not in options: if 'optimize' not in options:
return treesymtab return treesymtab
# Don't perform "a"+"b" -> "ab" unless explicitly requested.
self.addstrings = 'addstrings' in options
self.foldtabs = 'foldtabs' in options self.foldtabs = 'foldtabs' in options
self.shrinknames = 'shrinknames' in options self.shrinknames = 'shrinknames' in options

15
main.py
View file

@ -27,9 +27,15 @@ Options (+ means active by default, - means inactive by default):
allowkeyconcat + Allow string + key and key + string (both return string) allowkeyconcat + Allow string + key and key + string (both return string)
allowmultistrings + Allow C-like string juxtaposition, e.g. "ab" "cd" means allowmultistrings + Allow C-like string juxtaposition, e.g. "ab" "cd" means
"abcd", no concatenation involved. Very useful when used "abcd", no concatenation involved. Very useful when used
with a preprocessor (although the optimizer would with a preprocessor. Similar to addstrings, but this one
optimize concatenated strings if they are parenthesized is not an optimization, it introduces new syntax.
correctly, see note at the footer). addstrings - Concatenate strings together when possible. Note that
such an optimization can be counter-productive in some
cases, that's why it is unset by default. For example:
string a="a"+"longstring"; string b="b"+"longstring";
would keep a single copy of "longstring", while if the
strings are added, "alongstring" and "blongstring" would
both take memory.
skippreproc + Skip preprocessor directives in the source as if they skippreproc + Skip preprocessor directives in the source as if they
were comments. Not useful unless the script is itself were comments. Not useful unless the script is itself
the output of a preprocessor like cpp, which inserts the output of a preprocessor like cpp, which inserts
@ -55,9 +61,6 @@ Options (+ means active by default, - means inactive by default):
process, it turns the script into unreadable gibberish, process, it turns the script into unreadable gibberish,
hard to debug, but this gets big savings for complex hard to debug, but this gets big savings for complex
scripts. scripts.
Note that the optimizer doesn't reorder expressions to fold constants. This
means that e.g. a + 3 + 5 is not optimized to a + 8; however a + (3 + 5) is.
''' % sys.argv[0]) ''' % sys.argv[0])
return 1 return 1