Commit graph

643 commits

Author SHA1 Message Date
Sei Lisa
281ff4d96a Strings representing denormals no longer give 0 when cast to float
This annoyance and discrepancy with LSO was finally fixed by Linden Lab. The change has prompted some modifications to the test suite to accommodate for the new results. A further improvement has been to make these tests more friendly to be run in SL, making it easier to verify the results.

Fixes #17.

Reported by SaladDais@users.noreply.github.com - thanks!
2022-05-17 13:48:05 +02:00
Sei Lisa
7c630f4ce4 Fix crash in conversion to string for floats < 1e-13
Fixes part of #17.

Reported by SaladDais@users.noreply.github.com, thanks!
2022-05-17 13:37:39 +02:00
Sei Lisa
c29475d072 New constants and functions; some refactoring of lslfuncopt
TODO: unit tests
2022-05-13 14:49:18 +02:00
Sei Lisa
be479771aa Implement llListRandomize for the calculator 2022-03-29 23:29:09 +02:00
Sei Lisa
02b37d6dad Add OBJECT_ACCOUNT_LEVEL, fix PRIM_PROJECTOR 2022-03-06 02:54:58 +01:00
Sei Lisa
bf72d2c3bf Allow //pragma inline or /*pragma inline*/ for inlining a function
They are transformed by the scanner to the identifier `inline`, which is how the parser identifies it. This solves the comment problem, but it results in a funny side effect. Now, in inline mode, /*pragma inline*/ will always be the identifier `inline`, therefore this is valid:

    integer /*pragma inline*/ = 5;
    llOwnerSay((string)inline); // will say 5

Not overly elegant, but better than making up a specific token or declaring comments as tokens or the like.
2021-10-17 20:29:22 +02:00
Sei Lisa
064a8c4319 Add PRIM_PROJECTOR, add PRIM_TYPE_LEGACY, minor cleanups 2021-10-17 19:08:21 +02:00
Sei Lisa
03c33d1471 Simplify comparisons for most cases in llChar 2021-05-19 22:49:14 +02:00
Sei Lisa
c4f210138f Add newly introduced functions llOrd, llChar, llHash 2021-05-08 22:43:31 +02:00
Sei Lisa
53e8bd659a The Base64 functions that used to produce garbage no longer do 2021-03-21 19:05:37 +01:00
Sei Lisa
da05d1e1c2 Add Pop to the list of idents that GetNextShortest can't generate
It's unlikely to hit this one (requires more than 10,000 identifiers to trigger) but it's a bug nevertheless.

While touching the file, try to explain the situation better in the first comment.
2021-01-05 23:27:49 +01:00
Sei Lisa
81906475ff Update copyright year 2021-01-05 23:27:21 +01:00
Sei Lisa
0b266db758 Fix InternalUTF8ToString to consider U+FFFE as invalid
This commit also adds the foldtabs option as default for the eval test suite, and substantially improves the Unicode valid/invalid character test.
2020-11-19 22:26:42 +01:00
Sei Lisa
4771c76d85 Greatly simplify InternalUTF8ToString using a regular expression 2020-11-19 20:23:44 +01:00
Sei Lisa
c77607d318 Document the behaviour of InternalUTF8ToString 2020-11-19 20:22:11 +01:00
Sei Lisa
16d8c4c9d1 More progress in migration to dual Python 2&3. Just 2 tests left. 2020-11-10 00:15:48 +01:00
Sei Lisa
f8cf78dfac Progress towards dual Python 2 & 3 2020-11-09 02:28:57 +01:00
Sei Lisa
4fd4bf71aa Use frozenset more consistently 2020-11-09 02:11:35 +01:00
Sei Lisa
d9938f1a37 Misc small non-user-visible fixes
Some are Unicode vs Str stuff, others are style adjustments, others in unused functions, and so on.
2020-11-09 02:00:40 +01:00
Sei Lisa
59c3f9fc71 Add newest llGetEnv settings 2020-11-09 01:27:13 +01:00
Sei Lisa
13e48ff956 Improve +processpre directive parsing to include #warning 2020-11-02 11:16:36 +01:00
Sei Lisa
90fddf9dcd Report EParseCodePathWithoutRet at a more meaningful position 2020-10-11 16:31:49 +02:00
Sei Lisa
9e5f6e928d Improve compatibility of llToLower/llToUpper
Based on sim experiments.
2020-09-23 01:11:21 +02:00
Sei Lisa
d6bf0c390e Fix two issues in the dead code removal module
1. When the last statement of a function is a RETURN statement which is syntactically required, it could still be deleted.
2. The child of a RETURN statement could be removed if the statement was not executed.

This commit fixes both issues.

Bug report and test case provided by @Tonaie. Fixes #14.
2020-06-01 20:49:48 +02:00
Sei Lisa
0affbf13dd Fix "" + list_expr producing list_expr
The type checks were not strong enough. While on it, strengthen other similar checks as well.

Reported by @Tonaie. Fixes #13.
2020-04-23 13:47:57 +02:00
Sei Lisa
c8a0a21823 Add llLinear2sRGB / llsRGB2Linear implementations 2020-04-23 01:10:37 +02:00
Sei Lisa
234b9b23a3 Bump copyright year 2020-04-23 00:38:20 +02:00
Sei Lisa
be850e69cc Move comment 2019-11-03 01:09:23 +01:00
Sei Lisa
a8d8c14c79 Fix NULL_KEY being optimized to "" in UDFs; do some minor changes
Bug reported by @tapple. Fixes #11.
2019-11-03 00:55:30 +01:00
Sei Lisa
6d959f6f90 Handle llGetLinkName with invalid values 2019-10-11 19:38:28 +02:00
Sei Lisa
980b588986 Add FIXME and TODO 2019-10-11 19:38:05 +02:00
Sei Lisa
c02998518a Rename FoldCond -> FoldAsBool which better expresses the idea 2019-07-25 17:44:45 +02:00
Sei Lisa
1ab9dd69b4 Fix optimization of fn > -1 and fn < 0 when fn's minimum is -1 in conditions
E.g. llSubStringIndex(...) > -1 was converted into !~llSubStringIndex(...) which is incorrect. We even had a test case for it... with a wrong expected response file.

Bug report and test case by Sinha Hynes (thanks!)
2019-07-16 01:58:19 +02:00
Sei Lisa
1bdaff32ab When optimizing nonconst * const, cast the result to the type of the operator.
Per bug report by @Welcius.

Fixes #8.
2019-07-02 15:40:20 +02:00
Sei Lisa
750465f17b New upstream version of builtins.txt, new OBJECT_* constants 2019-06-23 13:33:02 +02:00
Sei Lisa
00b103c8aa Simplify v*q, make q*q more accurate, add q*q and q/q unit tests
vector * quaternion: Simplified by precalculating the repeated products and removing math.fsum.

quaternion * quaternion: Add more F32's to get a result closer to SL's (still not there, but we're definitively closer now).

We left out quaternion/quaternion in the tests, and the q*q test was not general enough (had many zeros). Remedied that.
2019-05-23 01:57:27 +02:00
Sei Lisa
ec509b3840 Fix parenthesization of unary minus
The algorithm for adding parentheses around unary operators was not working properly. It converted a * (-b) * c into a * -b * c, which LSL handles as a * -(b * c).

Fix and add test cases for that. One of the test cases shows an example where the difference matters: 0 * (-1e20) * 1e20 should result in 0.0, but if wrongly parenthesized, it gives NaN, because 1e20*1e20 gives infinity due to float overflow, and minus infinity times 0 is indeterminate.

The addition of parentheses has been improved, but it still does not eliminate every redundant parenthesis.

Also fix the horrendous typo of using "operands" where it should be "operators".
2019-05-04 23:20:20 +02:00
Sei Lisa
9d540798b4 Allow returning void expressions where state changes are allowed
In the same places as state changes are allowed, i.e. in places where a parent of the AST node is a WHILE/DO/FOR or an IF without ELSE, it's allowed to use return statements with expressions that return void, e.g. llDie(), provided the function itself is declared as returning void.

The construction, when found, is rewritten to '{<void expression>; return;}' because the optimizer is not designed to deal with these monsters.

We've renamed the variable SuspiciousStSw to PruneBug, because it's used for both purposes now, though a better name might have been PruneBugPendingChecks, because these are only errors if the IF has an ELSE. We've also added the exception to raise as part of the data stored in the list.

Per report by Tonaie Resident.
2019-05-01 04:28:58 +02:00
Sei Lisa
73bc2c29a8 Fix a couple of slips that can cause crashes
'Type' is a string with a LSL type. 'Kind' is a single leter denoting the kind of identifier. We meant Kind. Twice.
2019-02-25 19:59:59 +01:00
Sei Lisa
8cfcdd090a Fix --emap output when the error is not in the main file 2019-02-04 17:51:15 +01:00
Sei Lisa
68c8726a64 Option to output error messages suitable for automated processing
Enables use of the optimizer as an editor plug-in.
2019-02-04 17:20:12 +01:00
Sei Lisa
f0068dd3bc Style fixes
Calm pyflakes by using identifiers, and change hexversion to version.major.
2019-02-04 00:07:12 +01:00
Sei Lisa
128005e889 Fix bug in inlining of empty for() initializer
It was causing an exception. Fixed now.

Thanks to Cindy Reynaud for reporting the problem and sending a backtrace.
2019-02-02 00:50:05 +01:00
Sei Lisa
36ed90c9ba Revert _Pragma operator
Revert "Add support for C11-style _Pragma operator on processpre".
Revert "Add unit test for the _Pragma operator".

This reverts commits 31fcb331c7 and e261ac2121.

This should rather be the job of the preprocessor, which should generate #pragma lines. gcc does that.
2019-01-17 20:30:04 +01:00
Sei Lisa
fe2dd9a721 First baby steps towards dual Python2+3 compatibility 2019-01-16 00:15:06 +01:00
Sei Lisa
31fcb331c7 Add support for C11-style _Pragma operator on processpre
A minor difference is that strings and whitespace are parsed according to LSL rules, not to C rules, since this processing is performed in the lexer.

This could be fixed, but is it worth the trouble?
2019-01-13 19:03:10 +01:00
Sei Lisa
42f47d38f0 Fix swaps and other misoptimizations
This undoes 454d44e85f
2019-01-11 02:09:15 +01:00
Sei Lisa
c3bb056f05 Revert the optimization re-added in 1946acf3a4
That change needs much more analysis and thought. And we need more testcases.
2019-01-10 23:26:58 +01:00
Sei Lisa
06f8370886 Add a reference to the author name's origin
Add TODO on vectors and rotations, too.
2019-01-10 21:56:26 +01:00
Sei Lisa
1b3c8a4d89 Check min and max to determine truth value of condition
Also added some min/max values for a few functions.

This allows optimizing things like:

  ! llGetNumberOfPrims()  ->  0
2019-01-07 00:15:38 +01:00
Sei Lisa
5bfb218505 Perform redundant jump elimination in lastpass
This is a first try at redundant jump removal (jumps that target the very next instruction). It's too basic in several ways.

- The statement is replaced by a ';' instead of removed.
- If the jump was the only statement in an if, when the if becomes empty, it's not folded.
- Jumps that are last in the 'then' branch of if+else are not visible. This would need either to track multiple last statements, or to have some means to anticipate what the next statement is at every statement. A Control Flow Graph would help a lot.
- When a label is immediately followed by a jump, all jumps to that label should target the destination of that jump if it's in scope. Added to TODO.
- It misses some optimizations when not expanding WHILE and FOR into IF/JUMP.

Moving everything to an earlier stage would help with some of these, especially with ';' and 'if' folding. Unconditionally expanding WHILE and FOR would also help.
2019-01-06 22:32:19 +01:00
Sei Lisa
574f92d08e Allow #pragma OPT inline 2019-01-06 02:20:34 +01:00
Sei Lisa
c5fd4932f1 Finish inlining for all loop conditions 2019-01-06 01:59:36 +01:00
Sei Lisa
0ffe823c18 Move symbol existence check and creation to newId
ENameAlreadyExists has also been expanded to accept an inliner object, even though it's not used yet. Plans are the same as in EParse.
2019-01-06 01:33:49 +01:00
Sei Lisa
852fec2f26 Inliner: Add and use newSymtab() and newId() 2019-01-06 01:10:21 +01:00
Sei Lisa
55cb941234 Fix inlining of functions in conditions in DO loops 2019-01-06 00:52:54 +01:00
Sei Lisa
7fbde0269c Fix indentation typo (cosmetic) 2019-01-04 19:35:36 +01:00
Sei Lisa
1946acf3a4 Proper fix for unwanted substitutions in function calls
SymbolReplacedOrDeleted had an "emergency fix" that disabled several kinds of substitutions, because they generated code that didn't compile. The cause was actually elsewhere.

The actual problem was the marking of function parameters as being written to by function calls. This is true in a sense, but there's a big scope change that totally destroys the possibility of substituting identifiers, for example.

We were not removing the function parameters, anyway, therefore that code has just been disabled.

Note that removal of function parameters may be impossible if one parameter has side effects. Consider this:

f(string x, integer y, string z)
{
    llOwnerSay(x + z);
}
integer n = 2;
default{state_entry(){
  f("a" + (string)n, n=llSetRegionPos(<100,100,100>), "c" + (string)n);
}}

Even worse if the expression for the x argument has side effects too and x and y need to be performed in the right order.

Fortunately, such case is highly unlikely. But if we ever implement removal of function parameters, that's an additional difficulty to take care of.
2019-01-03 02:33:23 +01:00
Sei Lisa
454d44e85f Optimize chains of assignments
This allows optimizing, for example:

  integer a = 1;
  integer b = a;
  llOwnerSay((string)b);

which wasn't done before. This case is prone to happen with inlined functions, e.g. using the result of an inlined function as a parameter to another.
2019-01-03 02:32:14 +01:00
Sei Lisa
5a5635358f Add an assertion where we expect sym['W'] to have a symbol 2019-01-02 20:43:47 +01:00
Sei Lisa
aca4cc2721 OKtoRemoveSymbol: Return the symbol as specified, instead of True 2019-01-02 20:43:47 +01:00
Sei Lisa
8322284faa Rename SymbolReplacedOrDeleted to OKtoRemoveSymbol
It was an awful name but we couldn't think of anything better. That's what we have come up with as a substitute, which is not entirely accurate but it is MUCH more descriptive of what it actually does.
2019-01-02 20:43:47 +01:00
Sei Lisa
ffe55b38c3 Add the capability to show scope info to the output module
It's set in a variable local to that module. There's currently no way to enable it except by editing the code, but since it's mostly for internal purposes, it's OK like that.
2019-01-02 15:05:21 +01:00
Sei Lisa
ad71303eb0 Fix lack of 'ref' in symbol table entries for labels
These were expected by the constant folder, causing a crash.
2019-01-02 13:33:58 +01:00
Sei Lisa
2d583bb7e3 Fix scope of variables in inlined functions
The identifiers didn't point to the right scopes, causing optimization to produce wrong results.
2019-01-02 12:34:00 +01:00
Sei Lisa
42750c56d7 Fix some comments 2019-01-02 00:38:44 +01:00
Sei Lisa
f243f3a3c1 New copyright year 2019-01-01 22:54:34 +01:00
Sei Lisa
a4986f21df Add 'inline' directive to forcibly inline function calls 2019-01-01 22:30:18 +01:00
Sei Lisa
7bb07ecf38 Refine the places where void expressions are allowed
To prevent passing down a value in every call, the flag is a member. Yes, ugly.
2019-01-01 22:23:54 +01:00
Sei Lisa
660dcff65b Remove the symbol table's parent pointer
Instead of a tree of symbol tables, we keep a running stack of active symbol tables while parsing. The only case in which this causes problems is forward reference resolution for jump labels, which is solved by storing a copy of the stack at the point the jump was found.
2018-12-29 21:19:29 +01:00
Sei Lisa
76f483fc11 Add scope field to {} nodes
Since we need to add variables, we need to know which scope to add them to. Add this information to the {} node, which is what creates a new scope.

An alternative would be to scan for any variable or label declaration within the braces and use that or create a new one if none, which is more expensive and may waste symbol tables.
2018-12-29 21:10:14 +01:00
Sei Lisa
3542824d51 Add spaces after commas in vector/rotation constructors 2018-12-27 23:35:23 +01:00
Sei Lisa
6f32b4710a Remove handling of corner cases that we decided to not support
There was some remaining code trying to deal with labels in the single statement part of IFs and FORs.
2018-12-26 19:25:11 +01:00
Sei Lisa
b329b2b28e Avoid a possibly unnecessary expansion
When transforming if(!non-bool | !bool) to if(!(non-bool & -bool)), don't negate the bool if non-bool is actually an AND-bool.
2018-12-24 17:22:54 +01:00
Sei Lisa
c9f73bd429 Extend an optimization to cover some (admittedly rare) cases.
The value of bool|const is const when bit 0 of const is 1. This is a generalization of the case bool|1 = 1.
2018-12-24 17:22:54 +01:00
Sei Lisa
141301d7ff Reformatting 2018-12-24 17:22:54 +01:00
Sei Lisa
e123866e6e Apply DeMorgan in some cases that were not caught.
Example: the optimization of if(i < 2 || i) was suboptimal, because FoldTree was done before FoldCond. In a first stage, the || was removed: if(!!(i < 2 | i)); when folded, the inner ! was optimized first and the result was if(!(1 < i & (!i))), which FoldCond wasn't smart enough to handle.

The new optimization takes over from there, and converts if(!(!a & b)) with b boolean, in any order, to if(a | !b). When applied to the above expression, it gets folded to if(i < 2 | i) as expected, which is optimal.

Also, new function: IsAndBool (see docstring), used on b in the above example.
2018-12-24 17:18:48 +01:00
Sei Lisa
fa547cd9e8 Add blank lines to make the output somewhat prettier
Add blank lines between functions, between functions and states, between variables and functions or states, between states, and between events.

Or more concisely: add blank lines between events and between all elements at the global level except between variables (that actually describes the algorithm).

Some test cases expected no newlines; fix them.
2018-12-23 18:12:10 +01:00
Sei Lisa
0855b8ad1d Add 'listto' to fndata.txt; remove more magic names from the code
Rather than using a hardcoded table of list-to-type extraction function, add a 'ListTo' attribute to the function data. No error is raised if more than one function exists to convert to the same type.

This change is of questionable usefulness, but it should soothe those allergic to magic names/numbers. I cringed a bit myself.

While on it, change the syntax error that was raised when the corresponding conversion function did not exist, to a tailor-made error.
2018-12-22 15:44:14 +01:00
Sei Lisa
a052bf499c Reorganize the checks, and eliminate the fn call check
After more thought, we believe that transforming llDumpList2String into a sum of strings is a gain even if there is a list constructor containing function calls as first parameter. The rationale is explained in the comment.
2018-12-22 10:27:37 +01:00
Sei Lisa
0a6155bb13 Apply an associativity rule to strings
It's not general associativity, it just folds CONST + (CONST + expr). Some day we'll implement sum flattening, to make this possible.
2018-12-22 10:27:37 +01:00
Sei Lisa
e2d01e7825 Extend the 1-element optimization of llDumpList2String to non-SEF first arg 2018-12-22 10:27:37 +01:00
Sei Lisa
d4bcafa1f7 Add proper detection of functions inside the list
A recursive function checks every subnode.
2018-12-22 10:27:37 +01:00
Sei Lisa
e5c2c4057e Always cast the separator to string when expanding
If it is a key, it would otherwise cause an error on expansion.
2018-12-22 10:27:37 +01:00
Sei Lisa
1ebd029931 llDumpList2String: Always optimize SEF lists of 1 element 2018-12-22 10:27:37 +01:00
Sei Lisa
64c9c1db8b Optimize llDumpList2String for some non-constant cases
In particular, variables and fields. When we implement an analysis of functions contained in subexpressions, we can optimize more expressions, so add that as a TODO.
2018-12-22 10:27:37 +01:00
Sei Lisa
b9fe0b6c85 Add TODO item about side-effect analysis of UDFs 2018-11-19 21:15:52 +01:00
Sei Lisa
18d19b4653 Fix bug where 1 + (2 + function) was folded as 4 + function
After folding, our cached values changed under us. Re-cache them.
2018-11-19 20:43:58 +01:00
Sei Lisa
73599e0b04 More 32-bit negation-related overflow hazards patched.
Per bug report by Tonaie Resident.
2018-07-26 02:31:51 +02:00
Sei Lisa
960cbfaa09 Fix "Value of unknown type" on bit shift optimization
When converting x << constant to a product, we neglected to truncate the converted constant to S32.

Per bug report by Tonaie Resident.
2018-07-25 15:59:04 +02:00
Sei Lisa
6502a1a0b6 Remove TODO because there was nothing to do after checking. 2018-06-08 00:14:16 +02:00
Sei Lisa
e88e39127b Save a comparison (fixes a FIXME) 2018-06-08 00:07:36 +02:00
Sei Lisa
0808938894 Optimizations based on addition algebra for floats and integers
Float addition is commutative. Swap the constant to the left side if it's not there. This is a "cheap" version of a bigger change that is planned, to minimize stack usage as much as possible on savepoints.

Float addition is not associative, therefore we don't optimize e.g. 1 + (2 + float).

Integer addition is commutative and associative. If there's a constant, we swap it to the left side. If there's a chained summation of the form const + (const + expression), we apply associativity to turn it into (const + const) + expression and reoptimize.

This doesn't cover all possible cases. Expressions of the form (const + expr) + (const + expr) are not optimized. We need to flatten sums if we want to do the right thing here, but that's not yet implemented.

Get rid of some older code and TODOs that are no longer needed.
2018-06-07 23:57:21 +02:00
Sei Lisa
ac254efb77 Minor details
Assert that it can't be key where we say it can't, and that it is integer where we say it must.

Adjust some comments.
2018-06-07 02:14:15 +02:00
Sei Lisa
a33dc8540f Implement ~-~-~-expr -> expr + -3
Gets rid of a TODO
2018-06-07 02:14:15 +02:00
Sei Lisa
464797020a Remove commented-out print_node function (superseded by nr.__str__) 2018-06-07 02:14:15 +02:00
Sei Lisa
33fc38e42d Fix bug with key variable in list within globals
Mark the expression as simple also when ExtendedGlobalExpr is not set.
2018-05-17 19:05:00 +02:00
Sei Lisa
89c627b0c0 Remove jumps that just go to the next statement
This might help a bit with switch().
2018-05-17 09:46:06 +02:00
Sei Lisa
285c7172fd Get rid of DoesSomething() in favour of SEF
The missing bit was to mark labels are SEF when they are not referenced. Label references are now counted at parse time, enabling us to do this.

Also, make FoldStmt clearer when the node is an expression.
2018-05-17 09:08:48 +02:00
Sei Lisa
a93ea0ca19 Delete statements in a {} block when they are SEF
This is probably equivalent to what we did, as FoldStmt changes SEF statements to ';' statements, but it's simpler.
2018-05-17 09:08:35 +02:00