2015-03-05 15:18:41 -07:00
|
|
|
#!/usr/bin/env python2
|
2018-03-23 08:36:45 -07:00
|
|
|
#
|
2024-04-14 02:40:21 -07:00
|
|
|
# (C) Copyright 2015-2024 Sei Lisa. All rights reserved.
|
2015-03-05 15:18:41 -07: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/>.
|
2019-01-05 15:30:19 -07:00
|
|
|
#
|
|
|
|
# If you choose to use a later version of the GPL, please modify the text
|
|
|
|
# in the Usage() function appropriately to indicate the correct version.
|
2019-01-10 13:52:54 -07:00
|
|
|
#
|
|
|
|
# Sei Lisa is the author's username in the Second Life(R) virtual world.
|
|
|
|
# Second Life is a registered trademark of Linden Research, Inc.
|
2015-03-05 15:18:41 -07:00
|
|
|
|
|
|
|
# This is the main executable program that imports the libraries.
|
2014-07-25 17:43:44 -07:00
|
|
|
|
2017-10-01 15:40:59 -07:00
|
|
|
from lslopt.lslparse import parser,EParse
|
2014-07-25 17:43:44 -07:00
|
|
|
from lslopt.lsloutput import outscript
|
2014-07-26 12:29:35 -07:00
|
|
|
from lslopt.lsloptimizer import optimizer
|
2015-03-14 22:18:55 -07:00
|
|
|
import sys, os, getopt, re
|
2015-03-06 12:29:54 -07:00
|
|
|
import lslopt.lslcommon
|
2017-10-20 07:26:05 -07:00
|
|
|
import lslopt.lslloadlib
|
2019-01-15 12:27:02 -07:00
|
|
|
from strutil import *
|
2014-07-25 17:43:44 -07:00
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
|
2019-01-01 14:29:12 -07:00
|
|
|
VERSION = '0.3.0beta'
|
2015-03-14 22:18:55 -07:00
|
|
|
|
|
|
|
|
2016-01-01 19:39:47 -07:00
|
|
|
def ReportError(script, e):
|
2020-11-09 16:15:48 -07:00
|
|
|
linestart = script.rfind('\n', 0, e.errorpos) + 1
|
|
|
|
lineend = script.find('\n', e.errorpos)
|
2017-10-01 15:40:59 -07:00
|
|
|
if lineend == -1: lineend = len(script) # may hit EOF
|
|
|
|
|
|
|
|
# When the encoding of stderr is unknown (e.g. when redirected to a file),
|
|
|
|
# output will be encoded in UTF-8; otherwise the terminal's encoding will
|
|
|
|
# be used.
|
2019-04-30 16:22:54 -07:00
|
|
|
enc = getattr(sys.stderr, 'encoding', 'utf8') or 'utf8'
|
2017-10-01 15:40:59 -07:00
|
|
|
|
|
|
|
# Synchronize the UTF-8 encoded line with the output line in the
|
|
|
|
# terminal's encoding. We need to compensate for the fact that the
|
|
|
|
# reported column applies to the UTF-8 version of the script.
|
|
|
|
# 1. Trim the UTF-8 line.
|
|
|
|
err_frag = script[linestart:e.errorpos]
|
|
|
|
# 2. Convert to Unicode; encode in the target encoding with replacing.
|
2020-11-09 16:15:48 -07:00
|
|
|
err_frag = str2u(err_frag, 'utf8').encode(enc, 'backslashreplace')
|
2017-10-01 15:40:59 -07:00
|
|
|
# 3. Collect our prize: the length of that in characters.
|
|
|
|
cno = len(err_frag.decode(enc))
|
|
|
|
|
|
|
|
# Write the whole line in the target encoding.
|
2020-11-09 16:15:48 -07:00
|
|
|
err_line = script[linestart:lineend] + '\n'
|
|
|
|
werr(err_line)
|
2020-11-09 18:14:15 -07:00
|
|
|
werr(u" " * cno + u"^\n")
|
2019-01-15 12:27:02 -07:00
|
|
|
werr(e.args[0] + u"\n")
|
2016-01-01 19:39:47 -07:00
|
|
|
|
2015-03-15 12:01:41 -07:00
|
|
|
class UniConvScript(object):
|
2016-12-20 11:22:48 -07:00
|
|
|
"""Converts the script to Unicode, setting the properties required by
|
2015-03-15 12:01:41 -07:00
|
|
|
EParse to report a meaningful error position.
|
2016-12-20 11:22:48 -07:00
|
|
|
"""
|
2019-02-04 11:03:46 -07:00
|
|
|
def __init__(self, script, options=(), filename=b'<stdin>', emap=False):
|
2017-10-10 20:04:13 -07:00
|
|
|
self.linedir = []
|
|
|
|
self.filename = filename
|
2019-02-04 11:03:46 -07:00
|
|
|
self.emap = emap
|
2017-10-10 20:04:13 -07:00
|
|
|
# We don't interpret #line here. In case of an encode error,
|
|
|
|
# we're in the dark about which file it comes from. User needs
|
|
|
|
# --preshow to view the #line directives and find the correspondence
|
|
|
|
# themselves.
|
|
|
|
#self.processpre = 'processpre' in options
|
|
|
|
self.processpre = False
|
2015-03-15 12:01:41 -07:00
|
|
|
self.script = script
|
|
|
|
|
|
|
|
def to_unicode(self):
|
|
|
|
if type(self.script) is not unicode:
|
|
|
|
try:
|
|
|
|
self.script = self.script.decode('utf8')
|
|
|
|
except UnicodeDecodeError as e:
|
2020-11-09 18:14:15 -07:00
|
|
|
# EParse requires str
|
|
|
|
self.script = b2str(self.script, 'utf8')
|
2015-03-15 12:01:41 -07:00
|
|
|
self.errorpos = e.start
|
2016-12-20 11:22:48 -07:00
|
|
|
raise EParse(self, u"Invalid UTF-8 in script")
|
2015-03-15 12:01:41 -07:00
|
|
|
return self.script
|
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
def PreparePreproc(script):
|
2019-01-12 13:16:35 -07:00
|
|
|
"""LSL accepts multiline strings, but the preprocessor doesn't.
|
|
|
|
Fix that by converting newlines to "\n". But in order to report accurate
|
|
|
|
line and column numbers for text past that point, insert blank lines to
|
|
|
|
fill the space previously occupied by the string, and spaces in the last
|
|
|
|
line up to the point where the string was closed. That will place the next
|
|
|
|
token in the same line and column it previously was.
|
|
|
|
"""
|
2020-11-08 17:51:24 -07:00
|
|
|
s = u''
|
2015-03-14 22:18:55 -07:00
|
|
|
nlines = 0
|
|
|
|
col = 0
|
|
|
|
|
|
|
|
# Trigraphs make our life really difficult.
|
2015-03-20 09:45:38 -07:00
|
|
|
# We join lines that have \<return> or ??/<return> inside strings,
|
|
|
|
# and we also replace regular <return> inside strings with \n, counting how
|
|
|
|
# many lines we join, to add them back at the end of the string in order to
|
|
|
|
# keep the line count exact prior to preprocessing. We also preserve the
|
2015-03-24 13:56:35 -07:00
|
|
|
# original column of the text after the string, by adding as many spaces as
|
|
|
|
# necessary.
|
2015-03-20 09:45:38 -07:00
|
|
|
# We could let the preprocessor do the line joining on backslash-newline,
|
|
|
|
# but by eliminating all newlines, we have control over the output column
|
|
|
|
# of the text that follows the string and can report an accurate column
|
2015-03-24 13:56:35 -07:00
|
|
|
# and line position in case of error.
|
2015-03-20 09:45:38 -07:00
|
|
|
# The REs skip as much as possible in one go every time, only stopping to
|
|
|
|
# analyze critical tokens.
|
2015-03-24 13:56:35 -07:00
|
|
|
# We don't follow the C convention that backslash-return is analyzed first.
|
|
|
|
# In c, the string "a\\<return>nb" is the same as "a\nb" which prints as
|
|
|
|
# a<return>b. But in LSL, forgetting about the preprocessor, the string
|
|
|
|
# "a\\<return>nb" is valid and stands for a\<return>nb. The principle of
|
|
|
|
# least surprise seems to suggest to accept valid LSL strings as LSL
|
2017-01-03 21:07:50 -07:00
|
|
|
# instead of reproducing that C quirk. This also matches what FS is doing
|
|
|
|
# currently, so it's good for compatibility.
|
2020-11-08 17:51:24 -07:00
|
|
|
tok = re.compile(str2u( # Python 3.5 does not recognize ur'...' literals
|
2019-01-15 12:27:02 -07:00
|
|
|
r'(?:'
|
|
|
|
r'/(?:\?\?/\n|\\\n)*\*.*?\*(?:\?\?/\n|\\\n)*/'
|
|
|
|
r'|/(?:\?\?/\n|\\\n)*/(?:\?\?/\n|\\\n|[^\n])*\n'
|
|
|
|
r'|[^"]'
|
|
|
|
r')+'
|
|
|
|
r'|"'
|
2020-11-08 17:51:24 -07:00
|
|
|
, 'utf8'), re.S)
|
2015-03-20 09:45:38 -07:00
|
|
|
# RE used inside strings.
|
2019-01-15 12:27:02 -07:00
|
|
|
tok2 = re.compile(str2u(
|
|
|
|
r'(?:'
|
|
|
|
r"\?\?[='()!<>-]" # valid trigraph except ??/ (backslash)
|
|
|
|
r"|(?:\?\?/|\\)(?:\?\?[/='()!<>-]|[^\n])"
|
2020-11-08 17:51:24 -07:00
|
|
|
# backslash trigraph or actual backslash,
|
|
|
|
# followed by any trigraph or non-newline
|
2019-01-15 12:27:02 -07:00
|
|
|
r'|(?!\?\?/\n|\\\n|"|\n).'
|
2020-11-08 17:51:24 -07:00
|
|
|
# any character that doesn't start a trigraph/
|
|
|
|
# backslash escape followed by a newline
|
|
|
|
# or is a newline or double quote, as we're
|
|
|
|
# interested in all those individually.
|
|
|
|
r')' # as many of those as possible
|
|
|
|
r'|\?\?/\n|\\\n|\n|"' # or any of those individually
|
|
|
|
, 'utf8'))
|
2015-03-14 22:18:55 -07:00
|
|
|
|
|
|
|
pos = 0
|
|
|
|
match = tok.search(script, pos)
|
|
|
|
while match:
|
|
|
|
matched = match.group(0)
|
|
|
|
pos += len(matched)
|
2017-11-26 06:10:33 -07:00
|
|
|
if matched == u'"':
|
2015-03-14 22:18:55 -07:00
|
|
|
s += matched
|
|
|
|
nlines = col = 0
|
|
|
|
match2 = tok2.search(script, pos)
|
|
|
|
while match2:
|
|
|
|
matched2 = match2.group(0)
|
|
|
|
pos += len(matched2)
|
|
|
|
|
2020-11-08 17:51:24 -07:00
|
|
|
if matched2 == u'\\\n' or matched2 == u'??/\n':
|
2015-03-14 22:18:55 -07:00
|
|
|
nlines += 1
|
|
|
|
col = 0
|
|
|
|
match2 = tok2.search(script, pos)
|
|
|
|
continue
|
2020-11-08 17:51:24 -07:00
|
|
|
if matched2 == u'"':
|
2015-03-14 22:18:55 -07:00
|
|
|
if nlines:
|
2020-11-08 17:51:24 -07:00
|
|
|
if script[pos:pos+1] == u'\n':
|
2015-03-14 22:18:55 -07:00
|
|
|
col = -1 # don't add spaces if not necessary
|
|
|
|
# col misses the quote added here, so add 1
|
2020-11-08 17:51:24 -07:00
|
|
|
s += u'"' + u'\n'*nlines + u' '*(col+1)
|
2015-03-14 22:18:55 -07:00
|
|
|
else:
|
2020-11-08 17:51:24 -07:00
|
|
|
s += u'"'
|
2015-03-14 22:18:55 -07:00
|
|
|
break
|
2020-11-08 17:51:24 -07:00
|
|
|
if matched2 == u'\n':
|
2015-03-14 22:18:55 -07:00
|
|
|
nlines += 1
|
|
|
|
col = 0
|
2020-11-08 17:51:24 -07:00
|
|
|
s += u'\\n'
|
2015-03-14 22:18:55 -07:00
|
|
|
else:
|
|
|
|
col += len(matched2)
|
|
|
|
s += matched2
|
|
|
|
match2 = tok2.search(script, pos)
|
|
|
|
|
|
|
|
else:
|
|
|
|
s += matched
|
|
|
|
match = tok.search(script, pos)
|
2015-03-14 15:17:15 -07:00
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
return s
|
|
|
|
|
|
|
|
def ScriptHeader(script, avname):
|
|
|
|
if avname:
|
2020-11-08 18:28:57 -07:00
|
|
|
avname = ' - ' + avname
|
|
|
|
return ('//start_unprocessed_text\n/*'
|
2017-08-09 08:48:23 -07:00
|
|
|
# + re.sub(r'([*/])(?=[*|/])', r'\1|', script) # FS's algorithm
|
|
|
|
# HACK: This won't break strings containing ** or /* or // like URLs,
|
|
|
|
# while still being compatible with FS.
|
2020-11-08 18:28:57 -07:00
|
|
|
+ re.sub(r'([*/]\||\*(?=/))', r'\1|', script)
|
|
|
|
+ '*/\n//end_unprocessed_text\n//nfo_preprocessor_version 0\n'
|
|
|
|
'//program_version LSL PyOptimizer v' + VERSION
|
|
|
|
+ avname + '\n//mono\n\n')
|
2015-03-14 15:17:15 -07:00
|
|
|
|
2016-12-24 17:12:17 -07:00
|
|
|
def Usage(progname, about = None):
|
2015-03-14 15:17:15 -07:00
|
|
|
if about is None:
|
2019-01-15 12:27:02 -07:00
|
|
|
werr(
|
|
|
|
u"""LSL optimizer v{version}
|
2015-03-05 15:18:41 -07:00
|
|
|
|
2024-04-14 02:40:21 -07:00
|
|
|
(C) Copyright 2015-2024 Sei Lisa. All rights reserved.
|
2015-03-05 15:18:41 -07:00
|
|
|
|
|
|
|
This program comes with ABSOLUTELY NO WARRANTY.
|
|
|
|
This is free software, and you are welcome to redistribute it
|
|
|
|
under certain conditions; see the file COPYING for details.
|
|
|
|
This program is licensed under the GNU General Public License
|
|
|
|
version 3.
|
2014-07-27 17:13:08 -07:00
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
Usage: {progname}
|
2024-05-28 04:24:42 -07:00
|
|
|
[-O|--optimizer-options=[+|-]<option>[,[+|-]<option>[,...]]]
|
2015-03-14 22:18:55 -07:00
|
|
|
optimizer options (use '-O help' for help)
|
|
|
|
[-h|--help] print this help
|
|
|
|
[--version] print this program's version
|
|
|
|
[-o|--output=<filename>] output to file rather than stdout
|
2017-04-28 14:43:15 -07:00
|
|
|
[-b|--builtins=<filename>] use a builtins file other than builtins.txt
|
2017-10-21 01:00:31 -07:00
|
|
|
[-L|--libdata=<filename>] use a function data file other than fndata.txt
|
2015-03-18 20:11:29 -07:00
|
|
|
[-H|--header] add the script as a comment in Firestorm format
|
2015-12-25 11:04:24 -07:00
|
|
|
[-T|--timestamp] add a timestamp as a comment at the beginning
|
2017-01-11 17:55:52 -07:00
|
|
|
[-y|--python-exceptions] when an exception is raised, show a stack trace
|
2015-03-18 20:11:29 -07:00
|
|
|
[-p|--preproc=mode] run external preprocessor (see below for modes)
|
|
|
|
(resets the preprocessor command line so far)
|
2015-03-14 22:18:55 -07:00
|
|
|
[-P|--prearg=<arg>] add parameter to preprocessor's command line
|
2019-01-16 11:19:27 -07:00
|
|
|
before predefined ones
|
|
|
|
[-A|--postarg=<arg>] add parameter to preprocessor's command line
|
|
|
|
after predefined ones
|
2015-03-18 20:11:29 -07:00
|
|
|
[--precmd=<cmd>] preprocessor command ('cpp' by default)
|
|
|
|
[--prenodef] no LSL specific defines (__AGENTKEY__ etc.)
|
2015-05-05 17:45:37 -07:00
|
|
|
[--preshow] show preprocessor output, and stop
|
2022-06-17 06:09:24 -07:00
|
|
|
[--preproc-show-cmdline] show preprocessor invocation line, and stop
|
2017-01-07 21:23:50 -07:00
|
|
|
[--avid=<UUID>] * specify UUID of avatar saving the script
|
|
|
|
[--avname=<name>] * specify name of avatar saving the script
|
|
|
|
[--assetid=<UUID>] * specify the asset UUID of the script
|
|
|
|
[--shortname=<name>] * specify the script's short file name
|
2024-05-28 04:24:42 -07:00
|
|
|
[-B|--blacklist=<consts>] Comma-separated list of LSL constants that
|
|
|
|
should appear verbatim in the source, without
|
|
|
|
being expanded. Defaults to: NAK,JSON_*
|
|
|
|
[--prettify] Prettify source file. Disables all -O options
|
|
|
|
and blacklists all constants.
|
2018-05-11 13:37:09 -07:00
|
|
|
[--bom] Prefix script with a UTF-8 byte-order mark
|
2019-02-04 09:20:12 -07:00
|
|
|
[--emap] Output error messages in a format suitable for
|
|
|
|
automated processing
|
2015-03-14 22:18:55 -07:00
|
|
|
filename input file
|
2014-07-28 08:28:12 -07:00
|
|
|
|
2017-01-07 21:23:50 -07:00
|
|
|
Options marked with * are used to define the preprocessor macros __AGENTID__,
|
|
|
|
__AGENTKEY__, __AGENTIDRAW__, __AGENTNAME__, __ASSETID__ and __SHORTFILE__,
|
|
|
|
and have no effect if --prenodef is specified.
|
|
|
|
|
2014-07-28 08:28:12 -07:00
|
|
|
If filename is a dash (-) then standard input is used.
|
2017-01-07 21:23:50 -07:00
|
|
|
Use: {progname} -O help for help on the optimizer control options.
|
2014-07-27 17:13:08 -07:00
|
|
|
|
2018-12-22 08:02:08 -07:00
|
|
|
Comments are always removed in the output, even when using --prettify.
|
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
Preprocessor modes:
|
2018-10-15 12:11:46 -07:00
|
|
|
ext Invoke a preprocessor with no default parameters
|
2015-03-18 20:11:29 -07:00
|
|
|
mcpp Invoke mcpp as preprocessor, setting default parameters pertinent
|
|
|
|
to it. Implies --precmd=mcpp
|
|
|
|
gcpp Invoke GNU cpp as preprocessor, setting default parameters
|
|
|
|
pertinent to it. Implies --precmd=cpp
|
2015-03-24 13:56:35 -07:00
|
|
|
none No preprocessing (default)
|
|
|
|
|
2016-06-27 18:20:21 -07:00
|
|
|
Normally, running the preprocessor needs the option 'processpre' active, to
|
|
|
|
make the output readable by the optimizer. This option is active by default.
|
2019-01-15 12:27:02 -07:00
|
|
|
""".format(progname=str2u(progname), version=str2u(VERSION)))
|
2015-03-14 15:17:15 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
if about == 'optimizer-options':
|
2019-01-15 12:27:02 -07:00
|
|
|
werr(
|
|
|
|
u"""
|
2016-06-27 18:45:39 -07:00
|
|
|
Optimizer control options.
|
|
|
|
+ means active by default, - means inactive by default.
|
|
|
|
Case insensitive.
|
2015-03-04 17:37:32 -07:00
|
|
|
|
|
|
|
Syntax extensions options:
|
|
|
|
|
2016-06-27 18:45:39 -07:00
|
|
|
ExtendedGlobalExpr + Enables arbitrary expressions in globals (as opposed to
|
2014-07-27 17:13:08 -07:00
|
|
|
dull simple expressions allowed by regular LSL). Needs
|
2015-04-15 13:52:49 -07:00
|
|
|
constant folding active for the result to be compilable.
|
2016-06-27 18:45:39 -07:00
|
|
|
BreakCont - Allow break/continue statements for loops. Note that
|
2015-03-05 18:56:58 -07:00
|
|
|
when active, 'break' and 'continue' become reserved
|
|
|
|
words, but when inactive they can be used as variables.
|
2016-06-27 18:45:39 -07:00
|
|
|
ExtendedTypeCast + Allows extended typecast syntax e.g. (string)(integer)a
|
2014-07-27 17:13:08 -07:00
|
|
|
is valid with this option.
|
2016-06-27 18:45:39 -07:00
|
|
|
ExtendedAssignment + Enables &=, |=, ^=, <<=, >>= assignment operators.
|
|
|
|
AllowKeyConcat + Allow string + key and key + string (both return string)
|
|
|
|
AllowMultiStrings + Allow C-like string juxtaposition, e.g. "ab" "cd" means
|
2014-07-27 17:13:08 -07:00
|
|
|
"abcd", no concatenation involved. Very useful when used
|
2015-02-28 12:01:51 -07:00
|
|
|
with a preprocessor. Similar to addstrings, but this one
|
|
|
|
is not an optimization, it introduces new syntax.
|
2016-06-27 18:45:39 -07:00
|
|
|
DupLabels - Normally, a duplicate label within a function is allowed
|
2017-01-07 21:23:50 -07:00
|
|
|
by the syntax by using {{}} blocks; however, the server
|
2015-03-05 18:56:58 -07:00
|
|
|
will just refuse to save the script (under Mono) or do
|
2017-05-06 07:19:42 -07:00
|
|
|
something completely unexpected (under LSO: only the
|
|
|
|
last jump will execute, and it will go to the last label
|
|
|
|
with that name). This flag works around that limitation
|
|
|
|
by replacing the names of the labels in the output with
|
|
|
|
unique ones.
|
2019-01-05 16:41:50 -07:00
|
|
|
Inline - Enable 'inline' keyword to force functions to be inlined
|
2019-01-01 14:29:12 -07:00
|
|
|
(EXPERIMENTAL)
|
2015-03-05 18:56:58 -07:00
|
|
|
|
2015-03-07 14:55:25 -07:00
|
|
|
Deprecated / compatibility syntax extensions options:
|
2015-03-05 18:56:58 -07:00
|
|
|
|
2016-06-27 18:45:39 -07:00
|
|
|
LazyLists - Support syntax like mylist[index] = 5; rather than using
|
2015-03-04 17:37:32 -07:00
|
|
|
llListReplaceList. Only assignment supported. The list
|
|
|
|
is extended when the argument is greater than the list
|
|
|
|
length, by inserting integer zeros. This is implemented
|
|
|
|
for compatibility with Firestorm, but its use is not
|
|
|
|
recommended, as it adds a new function, wasting memory
|
|
|
|
against the very spirit of this program.
|
2016-06-27 18:45:39 -07:00
|
|
|
EnableSwitch - Support C-like switch() syntax, with some limitations.
|
2015-03-04 17:37:32 -07:00
|
|
|
Like lazylists, it's implemented for compatibility with
|
|
|
|
Firestorm, but not recommended. Note that the operand to
|
|
|
|
switch() may be evaluated more than once.
|
2016-06-27 18:45:39 -07:00
|
|
|
ErrMissingDefault + Throw an error in case the 'default:' label of a switch
|
2017-05-05 11:37:38 -07:00
|
|
|
statement is missing. Only meaningful with EnableSwitch.
|
2016-06-27 18:45:39 -07:00
|
|
|
FuncOverride - Allow duplicate function definitions to override the
|
2015-03-26 16:26:56 -07:00
|
|
|
previous definition. For compatibility with Firestorm's
|
|
|
|
optimizer.
|
2015-03-04 17:37:32 -07:00
|
|
|
|
|
|
|
Optimization options
|
|
|
|
|
2016-06-27 18:45:39 -07:00
|
|
|
Optimize + Runs the optimizer.
|
|
|
|
OptSigns + Optimize signs in float and integer constants.
|
|
|
|
OptFloats + Optimize floats that represent an integral value.
|
|
|
|
ConstFold + Fold constant expressions to their values, and simplify
|
2015-03-05 12:45:38 -07:00
|
|
|
some expressions and statements.
|
2022-10-31 12:08:26 -07:00
|
|
|
IfElseSwap + In 'if' statements, when negating the condition produces
|
|
|
|
shorter code, do it and swap the if and else branches.
|
|
|
|
Requires ConstFold to be enabled.
|
2016-06-27 18:45:39 -07:00
|
|
|
DCR + Dead code removal. This option removes several instances
|
2015-03-04 17:37:32 -07:00
|
|
|
of code that will never execute, and performs other
|
|
|
|
optimizations like removal of unused variables,
|
|
|
|
functions or expressions.
|
2016-06-27 18:45:39 -07:00
|
|
|
ShrinkNames - Reduces script memory by shrinking identifiers. In the
|
2014-07-31 20:07:50 -07:00
|
|
|
process, it turns the script into unreadable gibberish,
|
|
|
|
hard to debug, but this gets big savings for complex
|
|
|
|
scripts.
|
2016-06-27 18:45:39 -07:00
|
|
|
AddStrings - Concatenate strings together when possible. Note that
|
2015-03-04 17:37:32 -07:00
|
|
|
such an optimization can be counter-productive in some
|
2015-03-05 12:45:38 -07:00
|
|
|
cases, that's why it's disabled by default. For example:
|
2015-03-04 17:37:32 -07:00
|
|
|
string a="a"+"longstring"; string b="b"+"longstring";
|
|
|
|
would keep a single copy of "longstring", while if the
|
|
|
|
strings are added, both "alongstring" and "blongstring"
|
|
|
|
take memory.
|
2017-05-07 07:08:05 -07:00
|
|
|
ListLength + Optimize llGetListLength(arg) to arg!=[]. Needs constant
|
|
|
|
folding active to work.
|
2017-09-15 13:30:22 -07:00
|
|
|
ListAdd + Convert [a,b,c...] to (list)a + b + c... if possible.
|
2015-03-04 17:37:32 -07:00
|
|
|
|
|
|
|
Miscellaneous options
|
|
|
|
|
2016-06-27 18:45:39 -07:00
|
|
|
FoldTabs - Tabs can't be copy-pasted, so expressions that produce
|
2015-03-14 15:17:15 -07:00
|
|
|
tabs, e.g. llUnescapeURL("%09"), aren't optimized by
|
2015-03-04 17:37:32 -07:00
|
|
|
default. This option overrides that check, enabling
|
2015-03-05 12:45:38 -07:00
|
|
|
expansion of functions that produce strings with tabs.
|
|
|
|
The resulting source isn't guaranteed to be
|
|
|
|
copy-paste-able to the viewer.
|
2016-12-25 11:21:16 -07:00
|
|
|
WarnTabs + Warn when a function can't be optimized because it
|
|
|
|
generates a string or list with a tab, or when a string
|
|
|
|
contains a tab.
|
2016-06-27 18:45:39 -07:00
|
|
|
ProcessPre + Process some preprocessor directives in the source. This
|
2016-06-27 18:20:21 -07:00
|
|
|
enables usage of #pragma/#line preprocessor directives,
|
|
|
|
and is probably necessary if the script is itself the
|
|
|
|
output of a preprocessor. Note that this option does not
|
|
|
|
make the optimizer process macros.
|
2016-06-27 18:45:39 -07:00
|
|
|
ExplicitCast - Add explicit casts where they are implicit. This option
|
2015-03-05 18:56:58 -07:00
|
|
|
is useless with 'optimize' and 'optsigns', and is of
|
|
|
|
basically no use in general, other than to see where
|
|
|
|
automatic casts happen.
|
2016-12-25 12:40:15 -07:00
|
|
|
Clear - Set all options to inactive, Normally used as the first
|
|
|
|
option, to start afresh. Note that this sets to inactive
|
|
|
|
even the options that are active by default.
|
2017-01-07 21:23:50 -07:00
|
|
|
|
|
|
|
For example:
|
|
|
|
{progname} -O -DCR,+BreakCont scriptname.lsl
|
|
|
|
would turn off dead code removal (which is active by default) and turn on the
|
|
|
|
break/continue syntax extension (which is inactive by default).
|
2019-01-15 12:27:02 -07:00
|
|
|
""".format(progname=str2u(progname)))
|
2015-03-14 15:17:15 -07:00
|
|
|
return
|
2014-07-27 17:13:08 -07:00
|
|
|
|
2020-11-08 18:06:37 -07:00
|
|
|
validoptions = frozenset({'extendedglobalexpr','breakcont','extendedtypecast',
|
2016-12-25 13:34:42 -07:00
|
|
|
'extendedassignment','allowkeyconcat','allowmultistrings','duplabels',
|
|
|
|
'lazylists','enableswitch','errmissingdefault','funcoverride','optimize',
|
2022-10-31 12:08:26 -07:00
|
|
|
'optsigns','optfloats','constfold','ifelseswap','dcr','shrinknames',
|
|
|
|
'addstrings','foldtabs','warntabs','processpre','explicitcast',
|
|
|
|
'listlength','listadd','inline','help',
|
2017-05-05 11:37:38 -07:00
|
|
|
# undocumented
|
|
|
|
'lso','expr','rsrclimit',
|
2016-12-25 13:34:42 -07:00
|
|
|
# 'clear' is handled as a special case
|
2017-11-19 11:48:35 -07:00
|
|
|
# 'prettify' is internal, as it's a user flag
|
2020-11-08 18:06:37 -07:00
|
|
|
})
|
2016-12-25 13:34:42 -07:00
|
|
|
|
2016-12-24 17:12:17 -07:00
|
|
|
def main(argv):
|
2016-12-20 11:22:48 -07:00
|
|
|
"""Main executable."""
|
2015-03-18 20:11:29 -07:00
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
# If it's good to append the basename to it, it's good to append the
|
|
|
|
# auxiliary files' names to it, which should be located where this file is.
|
|
|
|
lslopt.lslcommon.DataPath = __file__[:-len(os.path.basename(__file__))]
|
|
|
|
|
|
|
|
# Default options
|
2022-10-31 12:08:26 -07:00
|
|
|
options = set(('extendedglobalexpr','extendedtypecast',
|
|
|
|
'extendedassignment','allowkeyconcat','allowmultistrings','processpre',
|
|
|
|
'warntabs','optimize','optsigns','optfloats','constfold','ifelseswap',
|
|
|
|
'dcr','errmissingdefault','listlength','listadd',
|
2014-07-28 09:19:50 -07:00
|
|
|
))
|
|
|
|
|
2016-12-25 13:34:42 -07:00
|
|
|
assert not (options - validoptions), (u"Default options not present in"
|
|
|
|
u" validoptions: '%s'"
|
|
|
|
% (b"', '".join(options - validoptions)).decode('utf8'))
|
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
try:
|
2024-05-28 04:24:42 -07:00
|
|
|
opts, args = getopt.gnu_getopt(argv[1:], 'hO:o:p:P:HTyb:L:A:B:',
|
2015-03-14 22:18:55 -07:00
|
|
|
('optimizer-options=', 'help', 'version', 'output=', 'header',
|
2019-02-04 09:20:12 -07:00
|
|
|
'timestamp', 'python-exceptions', 'prettify', 'bom', 'emap',
|
2015-05-05 17:45:37 -07:00
|
|
|
'preproc=', 'precmd=', 'prearg=', 'prenodef', 'preshow',
|
2022-12-20 13:32:07 -07:00
|
|
|
'avid=', 'avname=', 'assetid=', 'shortname=', 'builtins=',
|
2024-05-28 04:24:42 -07:00
|
|
|
'libdata=', 'postarg=', 'preproc-show-cmdline', 'blacklist='))
|
2016-12-25 12:47:54 -07:00
|
|
|
except getopt.GetoptError as e:
|
2016-12-24 17:12:17 -07:00
|
|
|
Usage(argv[0])
|
2020-11-09 16:15:48 -07:00
|
|
|
werr(u"\nError: %s\n" % str2u(str(e), 'utf8'))
|
2015-03-14 15:17:15 -07:00
|
|
|
return 1
|
2015-03-06 12:29:54 -07:00
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
outfile = '-'
|
2015-03-14 22:18:55 -07:00
|
|
|
avid = '00000000-0000-0000-0000-000000000000'
|
|
|
|
avname = ''
|
|
|
|
shortname = ''
|
|
|
|
assetid = '00000000-0000-0000-0000-000000000000'
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_command = 'cpp'
|
2019-01-16 11:19:27 -07:00
|
|
|
preproc_user_preargs = []
|
|
|
|
preproc_user_postargs = []
|
2015-03-18 20:11:29 -07:00
|
|
|
preproc = 'none'
|
|
|
|
predefines = True
|
|
|
|
script_header = ''
|
2015-12-25 11:04:24 -07:00
|
|
|
script_timestamp = ''
|
2015-05-05 17:45:37 -07:00
|
|
|
preshow = False
|
2022-06-17 06:09:24 -07:00
|
|
|
preproc_show_cmdline = False
|
2017-01-11 17:55:52 -07:00
|
|
|
raise_exception = False
|
2017-11-19 11:48:35 -07:00
|
|
|
prettify = False
|
2018-05-11 13:37:09 -07:00
|
|
|
bom = False
|
2019-02-04 09:20:12 -07:00
|
|
|
emap = False
|
2017-04-28 14:43:15 -07:00
|
|
|
builtins = None
|
2017-10-21 01:00:31 -07:00
|
|
|
libdata = None
|
2024-05-28 04:24:42 -07:00
|
|
|
blacklist = ['NAK','JSON_*']
|
2015-03-14 15:17:15 -07:00
|
|
|
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt in ('-O', '--optimizer-options'):
|
2016-12-25 12:40:15 -07:00
|
|
|
optchanges = arg.lower().split(',')
|
2015-03-14 15:17:15 -07:00
|
|
|
for chg in optchanges:
|
2016-12-25 13:34:42 -07:00
|
|
|
if not chg:
|
|
|
|
continue
|
2016-12-25 12:40:15 -07:00
|
|
|
if chg in ('clear', '+clear'):
|
|
|
|
options = set()
|
|
|
|
continue
|
|
|
|
if chg == '-clear':
|
|
|
|
# ignore
|
|
|
|
continue
|
2016-12-25 13:34:42 -07:00
|
|
|
chgfix = chg
|
|
|
|
if chgfix[0] not in ('+', '-'):
|
|
|
|
chgfix = '+' + chgfix
|
|
|
|
if chgfix[1:] not in validoptions:
|
|
|
|
Usage(argv[0], 'optimizer-options')
|
2019-01-16 12:24:31 -07:00
|
|
|
werr(u"\nError: Unrecognized"
|
2020-11-08 18:28:57 -07:00
|
|
|
u" optimizer option: %s\n" % str2u(chg, 'utf8'))
|
2016-12-25 13:34:42 -07:00
|
|
|
return 1
|
|
|
|
if chgfix[0] == '-':
|
|
|
|
options.discard(chgfix[1:])
|
2015-03-14 15:17:15 -07:00
|
|
|
else:
|
2016-12-25 13:34:42 -07:00
|
|
|
options.add(chgfix[1:])
|
|
|
|
del chgfix
|
2015-03-14 15:17:15 -07:00
|
|
|
|
|
|
|
elif opt in ('-h', '--help'):
|
2016-12-24 17:12:17 -07:00
|
|
|
Usage(argv[0])
|
2015-03-14 15:17:15 -07:00
|
|
|
return 0
|
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
elif opt == '--version':
|
2019-01-15 12:27:02 -07:00
|
|
|
wout(u'LSL PyOptimizer version %s\n' % str2u(VERSION))
|
2015-03-14 15:17:15 -07:00
|
|
|
return 0
|
|
|
|
|
|
|
|
elif opt in ('-o', '--output'):
|
|
|
|
outfile = arg
|
2015-03-14 22:18:55 -07:00
|
|
|
|
2017-04-28 14:43:15 -07:00
|
|
|
elif opt in ('-b', '--builtins'):
|
|
|
|
builtins = arg
|
|
|
|
|
2017-10-21 01:00:31 -07:00
|
|
|
elif opt in ('-L', '--libdata'):
|
|
|
|
libdata = arg
|
2017-04-28 14:43:15 -07:00
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
elif opt in ('-y', '--python-exceptions'):
|
|
|
|
raise_exception = True
|
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
elif opt in ('-p', '--preproc'):
|
|
|
|
preproc = arg.lower()
|
2016-12-25 12:47:54 -07:00
|
|
|
supported = ('ext', 'mcpp', 'gcpp', 'none')
|
|
|
|
if preproc not in supported:
|
2016-12-24 17:12:17 -07:00
|
|
|
Usage(argv[0])
|
2019-01-16 12:24:31 -07:00
|
|
|
werr(u"\nUnknown --preproc option: '%s'."
|
2016-12-25 12:47:54 -07:00
|
|
|
u" Only '%s' supported.\n"
|
2019-01-16 12:24:31 -07:00
|
|
|
% (preproc, str2u("', '".join(supported))))
|
2015-03-14 22:18:55 -07:00
|
|
|
return 1
|
|
|
|
|
2015-03-18 20:11:29 -07:00
|
|
|
if preproc == 'gcpp':
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_command = 'cpp'
|
2015-03-18 20:11:29 -07:00
|
|
|
|
|
|
|
elif preproc == 'mcpp':
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_command = 'mcpp'
|
2015-03-15 12:28:53 -07:00
|
|
|
|
2015-03-18 20:11:29 -07:00
|
|
|
elif opt == '--precmd':
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_command = arg
|
2015-03-14 22:18:55 -07:00
|
|
|
|
|
|
|
elif opt in ('-P', '--prearg'):
|
2019-01-16 11:19:27 -07:00
|
|
|
preproc_user_preargs.append(arg)
|
2015-03-14 22:18:55 -07:00
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
elif opt in ('-A', '--postarg'):
|
|
|
|
preproc_user_postargs.append(arg)
|
|
|
|
|
2015-03-18 20:11:29 -07:00
|
|
|
elif opt == '--prenodef':
|
|
|
|
predefines = False
|
|
|
|
|
2015-05-05 17:45:37 -07:00
|
|
|
elif opt == '--preshow':
|
|
|
|
preshow = True
|
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
elif opt == '--preproc-show-cmdline':
|
|
|
|
preproc_show_cmdline = True
|
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
elif opt in ('-H', '--header'):
|
|
|
|
script_header = True
|
|
|
|
|
2015-12-25 11:04:24 -07:00
|
|
|
elif opt in ('-T', '--timestamp'):
|
|
|
|
script_timestamp = True
|
|
|
|
|
2015-03-14 22:18:55 -07:00
|
|
|
elif opt == '--avid':
|
|
|
|
avid = arg
|
|
|
|
|
|
|
|
elif opt == '--avname':
|
|
|
|
avname = arg
|
|
|
|
|
|
|
|
elif opt == '--assetid':
|
|
|
|
assetid = arg
|
|
|
|
|
|
|
|
elif opt == '--shortname':
|
|
|
|
shortname = arg
|
2017-11-19 11:48:35 -07:00
|
|
|
|
|
|
|
elif opt == '--prettify':
|
|
|
|
prettify = True
|
2018-05-11 13:37:09 -07:00
|
|
|
|
2024-05-28 04:24:42 -07:00
|
|
|
elif opt in {'-B', '--blacklist'}:
|
|
|
|
blacklist = arg.split(',') if arg != '' else []
|
|
|
|
|
2018-05-11 13:37:09 -07:00
|
|
|
elif opt == '--bom':
|
|
|
|
bom = True
|
2019-02-04 09:20:12 -07:00
|
|
|
|
|
|
|
elif opt == '--emap':
|
|
|
|
emap = True
|
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
del opts
|
|
|
|
|
2017-11-19 11:48:35 -07:00
|
|
|
if prettify:
|
|
|
|
options &= set(('rsrclimit',))
|
|
|
|
options.add('prettify')
|
|
|
|
|
2019-10-11 11:15:52 -07:00
|
|
|
rsrclimit = False
|
2017-01-11 17:55:52 -07:00
|
|
|
try:
|
2017-01-03 21:08:09 -07:00
|
|
|
|
2017-05-05 11:37:38 -07:00
|
|
|
if 'rsrclimit' in options:
|
2019-10-11 11:15:52 -07:00
|
|
|
rsrclimit = True
|
2017-05-05 11:37:38 -07:00
|
|
|
import resource
|
2019-10-11 11:15:52 -07:00
|
|
|
resource.setrlimit(resource.RLIMIT_CPU, (5, 8))
|
|
|
|
resource.setrlimit(resource.RLIMIT_STACK, (0x60000, 0x80000))
|
|
|
|
resource.setrlimit(resource.RLIMIT_DATA, (9001000, 12001000))
|
|
|
|
resource.setrlimit(resource.RLIMIT_AS, (61001000, 81001000))
|
2017-05-05 11:37:38 -07:00
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
if 'lso' in options:
|
|
|
|
lslopt.lslcommon.LSO = True
|
|
|
|
options.remove('lso')
|
2017-01-03 21:08:09 -07:00
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
if 'expr' in options:
|
|
|
|
lslopt.lslcommon.IsCalc = True
|
|
|
|
options.remove('expr')
|
2016-12-25 12:43:24 -07:00
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
if 'help' in options:
|
|
|
|
Usage(argv[0], 'optimizer-options')
|
|
|
|
return 0
|
2015-03-14 15:17:15 -07:00
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
fname = args[0] if args else None
|
|
|
|
if fname is None:
|
|
|
|
Usage(argv[0])
|
2019-01-15 12:27:02 -07:00
|
|
|
werr(u"\nError: Input file not specified. Use -"
|
2017-01-11 17:55:52 -07:00
|
|
|
u" if you want to use stdin.\n")
|
2016-12-25 12:29:43 -07:00
|
|
|
return 1
|
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
del args
|
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
if not preproc_show_cmdline:
|
|
|
|
script = ''
|
|
|
|
if fname == '-':
|
|
|
|
script = sys.stdin.read()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
f = open(fname, 'r')
|
|
|
|
except IOError as e:
|
|
|
|
if e.errno == 2:
|
|
|
|
werr(u"Error: File not found: %s\n" % str2u(fname))
|
|
|
|
return 2
|
|
|
|
raise
|
|
|
|
try:
|
|
|
|
script = f.read()
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
del f
|
|
|
|
|
|
|
|
# Transform to str and check Unicode validity
|
|
|
|
if type(script) is unicode:
|
|
|
|
script = u2str(script, 'utf8')
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
# Try converting the script to Unicode, to report any encoding
|
|
|
|
# errors with accurate line information.
|
|
|
|
tmp = UniConvScript(script, options,
|
|
|
|
fname if fname != '-' else '<stdin>',
|
|
|
|
emap).to_unicode()
|
|
|
|
# For Python 2, just report any errors and ignore the result.
|
|
|
|
# For Python 3, use the Unicode.
|
|
|
|
if python3:
|
|
|
|
script = tmp
|
|
|
|
del tmp
|
|
|
|
except EParse as e:
|
|
|
|
# We don't call ReportError to prevent problems due to
|
|
|
|
# displaying invalid UTF-8
|
|
|
|
werr(e.args[0] + u"\n")
|
|
|
|
return 1
|
|
|
|
# Now script is in native str format.
|
2020-11-08 18:28:57 -07:00
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
if script_header:
|
|
|
|
script_header = ScriptHeader(script, avname)
|
2017-01-11 17:55:52 -07:00
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
if script_timestamp:
|
|
|
|
import time
|
|
|
|
tmp = time.time()
|
|
|
|
script_timestamp = time.strftime(
|
|
|
|
'// Generated on %Y-%m-%dT%H:%M:%S.{0:06d}Z\n'
|
|
|
|
.format(int(tmp % 1 * 1000000)), time.gmtime(tmp))
|
|
|
|
del tmp
|
2017-01-11 17:55:52 -07:00
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
if shortname == '':
|
|
|
|
shortname = os.path.basename(fname)
|
2017-01-11 17:55:52 -07:00
|
|
|
|
2017-11-26 05:34:37 -07:00
|
|
|
# Build preprocessor command line
|
2019-01-16 11:19:27 -07:00
|
|
|
preproc_cmdline = [preproc_command] + preproc_user_preargs
|
2018-10-15 12:11:46 -07:00
|
|
|
if preproc == 'gcpp':
|
|
|
|
preproc_cmdline += [
|
|
|
|
'-undef', '-x', 'c', '-std=c99', '-nostdinc',
|
|
|
|
'-trigraphs', '-dN', '-fno-extended-identifiers',
|
|
|
|
]
|
2017-11-26 05:34:37 -07:00
|
|
|
|
2018-10-15 12:11:46 -07:00
|
|
|
elif preproc == 'mcpp':
|
|
|
|
preproc_cmdline += [
|
2019-01-16 11:05:24 -07:00
|
|
|
'-e', 'UTF-8', '-I-', '-N', '-2', '-3', '-j',
|
2018-10-15 12:11:46 -07:00
|
|
|
'-V199901L',
|
|
|
|
]
|
2017-11-26 05:34:37 -07:00
|
|
|
|
2018-10-15 12:11:46 -07:00
|
|
|
if predefines:
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_cmdline += [
|
|
|
|
'-Dinteger(...)=((integer)(__VA_ARGS__))',
|
|
|
|
'-Dfloat(...)=((float)(__VA_ARGS__))',
|
|
|
|
'-Dstring(...)=((string)(__VA_ARGS__))',
|
|
|
|
'-Dkey(...)=((key)(__VA_ARGS__))',
|
|
|
|
'-Drotation(...)=((rotation)(__VA_ARGS__))',
|
|
|
|
'-Dquaternion(...)=((quaternion)(__VA_ARGS__))',
|
|
|
|
'-Dvector(...)=((vector)(__VA_ARGS__))',
|
|
|
|
'-Dlist(...)=((list)(__VA_ARGS__))',
|
|
|
|
]
|
|
|
|
preproc_cmdline.append('-D__AGENTKEY__="%s"' % avid)
|
|
|
|
preproc_cmdline.append('-D__AGENTID__="%s"' % avid)
|
2017-01-11 17:55:52 -07:00
|
|
|
preproc_cmdline.append('-D__AGENTIDRAW__=' + avid)
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_cmdline.append('-D__AGENTNAME__="%s"' % avname)
|
2017-01-11 17:55:52 -07:00
|
|
|
preproc_cmdline.append('-D__ASSETID__=' + assetid)
|
2017-11-26 05:34:37 -07:00
|
|
|
preproc_cmdline.append('-D__SHORTFILE__="%s"' % shortname)
|
2022-06-17 06:09:24 -07:00
|
|
|
preproc_cmdline.append('-D__OPTIMIZER__=LSL-PyOptimizer')
|
2017-01-11 17:55:52 -07:00
|
|
|
preproc_cmdline.append('-D__OPTIMIZER_VERSION__=' + VERSION)
|
|
|
|
|
2019-01-12 13:16:35 -07:00
|
|
|
# Append user arguments at the end to allow them to override defaults
|
2019-01-16 11:19:27 -07:00
|
|
|
preproc_cmdline += preproc_user_postargs
|
2019-01-12 13:16:35 -07:00
|
|
|
|
2022-06-17 06:37:22 -07:00
|
|
|
pperrors = False
|
2022-06-17 06:09:24 -07:00
|
|
|
if preproc_show_cmdline:
|
|
|
|
script = ' '.join(preproc_cmdline)
|
|
|
|
elif preproc != 'none':
|
2020-11-08 18:28:57 -07:00
|
|
|
# PreparePreproc uses and returns Unicode string encoding.
|
|
|
|
script = u2b(PreparePreproc(any2u(script, 'utf8')), 'utf8')
|
2022-06-17 06:37:22 -07:00
|
|
|
# At this point, for the preprocessor to work we need the script
|
|
|
|
# as a byte array, not as unicode, but it should be UTF-8.
|
2017-11-26 05:33:18 -07:00
|
|
|
if preproc == 'mcpp':
|
2017-01-11 17:55:52 -07:00
|
|
|
# As a special treatment for mcpp, we force it to output its
|
|
|
|
# macros so we can read if USE_xxx are defined. With GCC that
|
|
|
|
# is achieved with -dN, but mcpp has no command line option.
|
2019-01-16 12:24:31 -07:00
|
|
|
script += b'\n#pragma MCPP put_defines\n'
|
2017-01-11 17:55:52 -07:00
|
|
|
|
|
|
|
# Invoke the external preprocessor
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
p = subprocess.Popen(preproc_cmdline, stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
script = p.communicate(input=script)[0]
|
|
|
|
status = p.wait()
|
|
|
|
if status:
|
|
|
|
return status
|
|
|
|
del p, status
|
|
|
|
|
2020-11-08 18:28:57 -07:00
|
|
|
script = any2str(script, 'utf8')
|
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
# This method is very imperfect, in several senses. However, since
|
|
|
|
# it's applied to the output of the preprocessor, all of the
|
|
|
|
# concerns should be addressed:
|
|
|
|
# - \s includes \n, but \n should not be allowed.
|
|
|
|
# - Comments preceding the directive should not cause problems.
|
|
|
|
# e.g.: /* test */ #directive
|
|
|
|
# - #directive within a comment or string should be ignored.
|
2020-11-08 18:28:57 -07:00
|
|
|
for x in re.findall(r'(?:(?<=\n)|^)\s*#\s*define\s+('
|
|
|
|
r'USE_SWITCHES'
|
|
|
|
r'|USE_LAZY_LISTS'
|
|
|
|
r')(?:$|[^A-Za-z0-9_])', script, re.S):
|
|
|
|
if x == 'USE_SWITCHES':
|
2017-01-11 17:55:52 -07:00
|
|
|
options.add('enableswitch')
|
2020-11-08 18:28:57 -07:00
|
|
|
elif x == 'USE_LAZY_LISTS':
|
2017-01-11 17:55:52 -07:00
|
|
|
options.add('lazylists')
|
|
|
|
|
2022-06-17 06:09:24 -07:00
|
|
|
if not preshow and not preproc_show_cmdline:
|
2017-01-11 17:55:52 -07:00
|
|
|
|
2019-02-04 09:20:12 -07:00
|
|
|
if emap:
|
|
|
|
options.add('emap')
|
|
|
|
|
2017-10-21 01:00:31 -07:00
|
|
|
lib = lslopt.lslloadlib.LoadLibrary(builtins, libdata)
|
2024-05-28 04:24:42 -07:00
|
|
|
|
|
|
|
wildcards = re.compile(str2u(r'^\*?[A-Za-z_?][A-Za-z0-9_*?]*$'))
|
|
|
|
for i in xrange(len(blacklist)-1, -1, -1):
|
|
|
|
c = blacklist[i]
|
|
|
|
if not wildcards.search(c):
|
|
|
|
werr(u"Error: blacklist: \"%s\" contains invalid"
|
|
|
|
u" characters\n" % c)
|
|
|
|
return 1
|
|
|
|
expr = re.compile(c.replace('?', '.').replace('*', '.*') + '$')
|
|
|
|
|
|
|
|
matches = [j for j in lib[1].keys() if expr.match(j)]
|
|
|
|
|
|
|
|
# if not matches:
|
|
|
|
# werr(u"Error: blacklist: \"%s\" does not match any constant"
|
|
|
|
# u"\n" % c)
|
|
|
|
# return 1
|
|
|
|
if not matches or matches[0] != c:
|
|
|
|
blacklist = blacklist[:i] + matches + blacklist[i+1:]
|
|
|
|
blacklist = list(set(blacklist)) # remove dupes
|
|
|
|
|
2017-10-20 07:26:05 -07:00
|
|
|
p = parser(lib)
|
2024-05-28 04:24:42 -07:00
|
|
|
p.blacklist = blacklist
|
|
|
|
del blacklist
|
2020-11-08 18:28:57 -07:00
|
|
|
assert type(script) == str
|
2017-01-11 17:55:52 -07:00
|
|
|
try:
|
2017-10-10 20:04:13 -07:00
|
|
|
ts = p.parse(script, options,
|
2020-11-08 18:28:57 -07:00
|
|
|
'stdin' if fname == '-' else fname)
|
2017-01-11 17:55:52 -07:00
|
|
|
except EParse as e:
|
|
|
|
ReportError(script, e)
|
|
|
|
return 1
|
|
|
|
del p, script
|
|
|
|
|
2017-10-27 12:13:07 -07:00
|
|
|
opt = optimizer(lib)
|
2017-01-11 17:55:52 -07:00
|
|
|
ts = opt.optimize(ts, options)
|
|
|
|
del opt
|
|
|
|
|
|
|
|
outs = outscript()
|
|
|
|
script = script_header + script_timestamp + outs.output(ts, options)
|
|
|
|
del outs, ts
|
|
|
|
|
|
|
|
del script_header, script_timestamp
|
|
|
|
|
2018-05-11 13:37:09 -07:00
|
|
|
if bom:
|
2019-01-17 04:55:49 -07:00
|
|
|
if not script.startswith(b'\xEF\xBB\xBF'):
|
|
|
|
script = b'\xEF\xBB\xBF' + script
|
2018-05-11 13:37:09 -07:00
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
if outfile == '-':
|
|
|
|
sys.stdout.write(script)
|
|
|
|
else:
|
|
|
|
outf = open(outfile, 'w')
|
|
|
|
try:
|
|
|
|
outf.write(script)
|
|
|
|
finally:
|
|
|
|
outf.close()
|
2022-06-17 06:37:22 -07:00
|
|
|
|
|
|
|
if pperrors:
|
|
|
|
sys.stderr.write(u"\n* Errors found during preprocessing\n")
|
|
|
|
return 1
|
|
|
|
|
2017-01-11 17:55:52 -07:00
|
|
|
return 0
|
|
|
|
|
|
|
|
except Exception as e:
|
2019-10-11 11:15:52 -07:00
|
|
|
if rsrclimit:
|
|
|
|
# Raise the soft limits to hopefully prevent double exceptions
|
|
|
|
resource.setrlimit(resource.RLIMIT_CPU, (8, 8))
|
|
|
|
resource.setrlimit(resource.RLIMIT_STACK, (0x80000, 0x80000))
|
|
|
|
resource.setrlimit(resource.RLIMIT_DATA, (12001000, 12001000))
|
|
|
|
resource.setrlimit(resource.RLIMIT_AS, (81001000, 81001000))
|
2017-01-11 17:55:52 -07:00
|
|
|
if raise_exception:
|
|
|
|
raise
|
2020-11-09 18:14:15 -07:00
|
|
|
werr(e.__class__.__name__ + u': ' + str(e) + u'\n')
|
2017-01-11 17:55:52 -07:00
|
|
|
return 1
|
2014-07-25 17:43:44 -07:00
|
|
|
|
2015-03-14 15:17:15 -07:00
|
|
|
if __name__ == '__main__':
|
2016-12-24 17:12:17 -07:00
|
|
|
ret = main(sys.argv)
|
2015-03-14 15:17:15 -07:00
|
|
|
if ret:
|
|
|
|
sys.exit(ret)
|