Commit graph

76 commits

Author SHA1 Message Date
Sei Lisa
81906475ff Update copyright year 2021-01-05 23:27:21 +01:00
Sei Lisa
4fd4bf71aa Use frozenset more consistently 2020-11-09 02:11:35 +01:00
Sei Lisa
234b9b23a3 Bump copyright year 2020-04-23 00:38:20 +02:00
Sei Lisa
fe2dd9a721 First baby steps towards dual Python2+3 compatibility 2019-01-16 00:15:06 +01:00
Sei Lisa
f243f3a3c1 New copyright year 2019-01-01 22:54:34 +01:00
Sei Lisa
075d3aba0c 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.
2018-04-01 02:14:00 +02:00
Sei Lisa
d0d6d6744b Bump copyright year
The help text in main.py was still at 2015. Oops.
2018-03-23 16:36:45 +01:00
Sei Lisa
1cdf9f7ff0 Collect used library functions as reusable names for the renamer.
Implements another TODO.

There was a TODO about a new counter per scope, but that makes no sense. The renamer only acts on global variables, global function and parameter names, state names, and event parameters. We're already restarting the counters at every function, which is the closest to what that TODO was about.
2017-11-02 23:19:33 +01:00
Sei Lisa
70f39d7f5e Remove "magic names" where possible, using new data from fndata.txt 2017-10-27 23:39:26 +02:00
Sei Lisa
a6a08fe3f3 Implement transformation of lists into additions.
If the list in brackets is SEF:
  [a, b, ...]  ->  (list)a + b + ...
  ListExpr + [a, b, ...]  ->  ListExpr + a + b + ...
2017-09-15 22:30:22 +02:00
Sei Lisa
62eebb8bef Remove line no longer used from lsloptimizer.py. 2017-08-30 19:22:12 +02:00
Sei Lisa
6738615360 Separate library function optimization into a different file.
No other functional changes. This required quite some reorganization affecting many files. As a side effect, PythonType2LSL and LSLType2Python aren't duplicate anymore.
2017-08-25 20:35:24 +02:00
Sei Lisa
fe574bb462 Bump copyright year.
'bout time.
2017-08-09 19:45:46 +02:00
Sei Lisa
f0c115f924 Add optimizer option 'listlength' to optimize llGetListLength.
llGetListLength(arg) was transformed to arg!=[] whenever constant folding was active. This option allows disabling that optimization.
2017-01-25 19:22:36 +01:00
Sei Lisa
79dff25769 Update copyright years of some files; add legalese to seftable.txt 2016-06-27 20:06:41 +02:00
Sei Lisa
b7e6e6f7b1 Fix bug with tab handling.
Commit 5804a9a introduced a bug where having the foldtabs option disabled (normal) prevented optimizations of functions. Fix it for good (hopefully). While on it, rename the nofoldtabs option to warntabs, making it default, and use it to disable a warning when there are tabs in a string.
2016-05-07 03:18:50 +02:00
Sei Lisa
dc98e477d7 Add 'nofoldtabs' option to disable warning when a function generates tabs
When a function folds to a string that contains a tab, e.g. llUnescapeURL("%09"), or to a list that contains a string that contains a tab, a warning is emitted unless the foldtabs option (which forces the optimization) is used. This option allows to quiet the warning without forcing the optimization.
2016-04-01 20:48:33 +02:00
Sei Lisa
f8a6d3d86c Reorganize so that warningPass is always set at least once when constfold is set. 2015-08-19 05:41:21 +02:00
Sei Lisa
d58bc2d350 Implement library function parameter optimization.
So far only two kinds: angle in llSensor[Repeat] and null keys in functions that take a key parameter.

Closes two TODOs.
2015-04-21 04:56:09 +02:00
Sei Lisa
6813bb0458 Make the second pass of FoldScript look a bit more logic. 2015-03-28 20:14:17 +01:00
Sei Lisa
2a617b34d0 Fix bug where a<<1 was not immediately optimized, and other minor changes.
* Add a TODO.
* Don't make two fold passes if DCR is off.
* Remove comment about parentheses that no longer applies.
2015-03-28 14:45:53 +01:00
Sei Lisa
59cdd64228 Warn about float constants that can't be represented in globals. Also don't duplicate the globals warnings. 2015-03-06 23:26:58 +01:00
Sei Lisa
c68a1f4ad6 Add copyright notices to all files, to prepare the program for release. 2015-03-05 23:18:41 +01:00
Sei Lisa
8f83e2f1ab Add "addstrings" option (disabled by default) to select whether to automatically concatenate strings during constant folding. 2015-02-28 20:01:51 +01:00
Sei Lisa
6ea01c4242 Added optimizations for most operators, and 'cornermath' option.
Only remaining operators are < <= > >= &.
2015-02-28 18:30:04 +01:00
Sei Lisa
1dea1bd12c Make parentheses no longer explicit in the AST (Beta).
The output module adds parentheses where necessary, depending on the evaluation order in the tree. Or that's the idea. Prone to bugs, let's see how it bodes.
2015-02-28 00:43:26 +01:00
Sei Lisa
8e5166bb2e Move the constant folding code to lslfoldconst.py.
lsloptimizer remains as the "conductor" and option handler.

As a result, new options have been added to enable DCR and constant folding.
2014-08-13 14:19:58 +02:00
Sei Lisa
36759cacba Move PythonType2LSL to the main lsloptimizer script.
This is preparation work for splitting the optimizer.
2014-08-13 13:44:54 +02:00
Sei Lisa
e3d8533caf Use self.shrinknames instead of checking the options again. 2014-08-10 02:05:31 +02:00
Sei Lisa
9295cb104a Cast() now adds parentheses where necessary. 2014-08-10 01:14:29 +02:00
Sei Lisa
de4d6f8857 Propagate X flag in Cast(): if the expression executes, so does the cast. 2014-08-08 04:05:01 +02:00
Sei Lisa
c080f4b596 Fix detection of SEF in assignments with vector/rot fields as LHS. 2014-08-08 04:03:37 +02:00
Sei Lisa
5d7829dc9d Fix a bunch of SEF-related bugs and forgotten cases. 2014-08-08 00:41:07 +02:00
Sei Lisa
7a6e13b786 Remove line that does nothing useful. 2014-08-07 22:10:15 +02:00
Sei Lisa
954b8d81d4 Forgot a SEF case. 2014-08-07 22:09:00 +02:00
Sei Lisa
9c4d81db08 Implement side-effect-free (SEF) analysis. Also optimize x++ to ++x.
As an additional bonus, the condition folding function is now the one that converts a condition to -1, relieving that burden from the rest of the code and simplifying tests.
2014-08-06 04:30:26 +02:00
Sei Lisa
5773449edb Convert warnings to add the WARNING: title in the function. 2014-08-05 15:55:49 +02:00
Sei Lisa
58e22f77ce Prepare for the incoming Dead Code Removal module. 2014-08-05 15:47:14 +02:00
Sei Lisa
bcc7fe3524 Fix bug in FoldStmt where EXPR nodes were not optimized out. 2014-08-05 15:38:19 +02:00
Sei Lisa
24a32c30e3 Fix bug where folding a FOR loop with const false cond produced bad code.
More precisely, the initializator list was not wrapped into EXPR nodes. Now it is.

While on it, fix a minor bug (the substitutive of a FOR statement should produce no output type), and change order of execution to the actual one (iterators come last).
2014-08-04 02:19:36 +02:00
Sei Lisa
17e94a0d0f Add FoldCond as a stub. Change cond constant to -1. Add support for EXPR.
(EXPR as a statement, not as a general 'expression')
2014-08-03 20:06:50 +02:00
Sei Lisa
14e5b78d94 Remove empty statements. Optimize out 'else ;' and 'else {}'.
Without empty statement removal, {;} would trick the optimizer into believing the statement was not empty. E.g. if(x) ; else {;} would not remove the else.
2014-08-02 10:40:59 +02:00
Sei Lisa
dce6419b4f Deal with the state change in globals error. 2014-08-01 23:51:24 +02:00
Sei Lisa
400809671e Fix GetNextShortest. Add test and regression cases. Rename AssignNewNames. 2014-08-01 18:02:55 +02:00
Sei Lisa
6c248c46e3 Implement the shrinknames option.
Fixes some bugs with the treatment of the shrink attribute, some others with the output of renamed stuff.
2014-08-01 05:07:50 +02:00
Sei Lisa
fb65144e1b One less to-do. 2014-08-01 02:02:01 +02:00
Sei Lisa
cf880b8dbf Fix priorities for x << const optimization. 2014-07-31 19:45:30 +02:00
Sei Lisa
a303ef2066 Unfold all assignment+operation operators to optimize them better.
With test case.
2014-07-31 18:44:50 +02:00
Sei Lisa
5bedc17b73 Fix bug where optimization disabled didn't return a value. 2014-07-31 05:39:20 +02:00
Sei Lisa
e5714eba25 Fix the obnoxious issue with globals propagation in full expressions.
Fixed by backtracking in the parser, and keeping a copy of the original expression if it's a simple_expr, which is used for output in place of the folded one.

There's still the potential issue that if a global is optimized away, then it will "come back" during output and cause an error because the definition is missing.
2014-07-31 04:47:19 +02:00