Commit graph

643 commits

Author SHA1 Message Date
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
Sei Lisa
caf50c4e14 Change while (cond) /*empty*/; into do /*empty*/; while (cond); 2018-05-17 06:38:33 +02:00
Sei Lisa
8f93386108 Get rid of label checks in some situations no longer supported 2018-05-17 06:38:12 +02:00
Sei Lisa
c62773ad3f Refine the denormal check for Mono
The threshold for returning 0 for denormals is slightly different.
2018-05-09 19:04:09 +02:00
Sei Lisa
091f06cccc Fix threshold value for denormal to be taken as 0
(float)"1.1754944e-38" is != 0
(float)"1.1754943e-38" is == 0

Yet, 1.1754944e-38 == 1.1754943e-38.

The fix is to perform the operations as doubles, and convert to F32 *after* comparing the denormal range.
2018-04-27 23:44:27 +02:00
Sei Lisa
1afe1643c0 Allow lists to contain void elements if not optimizing and not lazy
LSO allows this. The compiler does too, but it chokes in RAIL.

This affected a test, which has been adjusted too.

Untyped lazy list elements can no longer be used in isolation in expression lists (including FOR initializator and iterator).

Also rename the terribly named 'self.forbidlabels' to 'self.optenabled' which is more descriptive.
2018-04-09 18:49:47 +02:00
Sei Lisa
4a554d60e8 Remove a TODO item that was already done 2018-04-02 13:42:28 +02:00
Sei Lisa
6ef4c03994 Remove support for labels as immediate children of IF/ELSE/WHILE/FOR/DO.
This extremely uncommon coding pattern was becoming a hell to support. It has caused many bugs in past that need them being treated as special cases.

Getting rid of the possibility entirely seems like the best approach.

It's still supported if the code is not to be optimized (e.g. with --pretty).
2018-04-01 20:05:35 +02:00
Sei Lisa
4ebd84f0ed Raise EParseSyntax on wrong minus sign (not followed by int or float)
While not strictly a bug because it would be caught later in the function (it passes the tests either way), it made me nervous to leave a dangling NextToken().
2018-04-01 02:14:53 +02:00
Sei Lisa
dc30d461e2 Format some docstrings better; make one more precise about the grammar 2018-04-01 02:14:53 +02:00
Sei Lisa
8d0b995f07 Accept library function names as vars in simple_expr in globals
This may cause more trouble than it's worth, but it's how LSL behaves and one of our objectives is to document the darker corners of LSL. Mono chokes at the RAIL postprocessing stage, not in compilation proper. LSO chokes at runtime for string, key and list, and works fine for the other types.
2018-04-01 02:14:53 +02:00
Sei Lisa
8289c14c81 Fix bug where types of expressions in vectors/rotations were not checked
E.g. this was a valid vector literal: <"",0,0>

Parse_simple_expr didn't have that problem, though.
2018-04-01 02:14:53 +02: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
d890f0b5fa Comment-at-eol cleanup
Ensure every comment has a double space after the code.
2018-03-27 13:29:04 +02:00
Sei Lisa
f3c87299c2 Move comment to its correct place 2018-03-23 22:05:05 +01:00
Sei Lisa
2b14acc8a0 Follow-up fix to 82081b2 (#3)
Globals appearing in vectors or rotations inside a list still caused a crash.

Fixes #3 again.
2018-03-23 21:57:33 +01:00
Sei Lisa
877d5fc10c Optimize -a == -b, !(a - b), !(a + b)
-a == -b  ->  a == b

If both a and b either are constants or have a minus sign, negate both.

!(a - b) can be optimized to a == b.

!(a + b) can be optimized to -a == b, relying on the first optimization to remove redundant minus signs.
2018-03-23 19:02:42 +01: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
a1f1f13739 Better optimization of (non-)equality comparisons.
int != int was not properly optimized, because the != was transformed into the equivalent !(int == int) at an earlier stage. Fixed.

!(a ^ b) can be optimized to a == b, so do it.
2018-03-23 16:04:18 +01:00
Sei Lisa
aad4cf3bee Fix the fix for the bug introduced in 097c054
Our previous fix was incomplete, because it failed to detect the last IF in a chain of ELSE IFs. For example:

if (a == 2) llDie(); else if (a) llDie(); else if (a == 3) llDie();

That would be transformed by the IF swapper into:

if (a ^ 2)
    if (a)
        llDie();
    else if (a == 3)
        llDie();
else
    llDie();

Note that the last 'else' would bind to the last 'if', not to the first one. So the condition is actually like this:

child[1] of an 'if' statement needs to be guarded in {} if the 'else' may belong to the wrong 'if'.

It will belong to the wrong 'if' if child[1] is a (possibly empty) chain of 'if {whatever} else ...', followed by an 'if' without 'else', that is:

   if (cond) stmt;

(which was what our previous check did), but also e.g.:

   if (cond) stmt; else if (cond) stmt; else if (cond) stmt;

which we neglected to consider in our previous fix.
2017-12-14 21:32:00 +01:00
Sei Lisa
f492d3e291 Add --prettify option, to reformat a script (losing comments).
Since our syntax extensions transform the source at parse time, all syntax extensions are disabled. The optimizations are disabled too, as it doesn't make sense to prettify and optimize at the same time (the optimizer would remove the constants that we're trying to keep).

Addresses #4 in a more user-friendly way.
2017-11-20 20:59:45 +01:00
Sei Lisa
e42479756b Fix bug with nested if's; fix missing EXPR wrap.
097c054 introduced a bug that we hadn't caught until now.

In some occasions, it could swap nested conditions in such a way that the 'else' of the outer statement was made to belong to the inner one, like this:

    if (a)
        if (b)
            stuff;
    else
        stuff;

That is of course parsed with the 'else' belonging to if(b).

Fix implemented at output time, by detecting 'if(a) stmt; else y;' with stmt being an 'if' without 'else', and wrapping the stmt in {} like this: 'if(a){if(b) x;} else y;'. This has some similarity with parenthesis addition.

But the fix has the corner case that, since {} hides visibility of labels, when the inner 'if' has a label as direct child, it can't be swapped lest the label becomes out of scope. So these cases are detected and skipped in the constant folding module.

In the case of 'if(cond);', we transform it to 'cond;', but we forgot to wrap the cond in an EXPR node as required. Fixed too.
2017-11-13 04:04:13 +01:00
Sei Lisa
a4b3c1eadd Handle list+list more sanely. 2017-11-04 22:50:32 +01:00
Sei Lisa
0b1ad7c110 Add more results of operating an expression with itself.
Implemented a<a=0, a^a=0, a+-a=0, -a+a=0, a|a=a, a&a=a
2017-11-03 00:45:11 +01:00
Sei Lisa
4251d4c7f9 Improve CompareTrees.
Reorganize into different statements with early return.

Add constants, unary operators and binary operators. Check if operator is commutative and check with operands swapped when so.

Constant equality is somewhat sketchy at the moment: just compare the values with Python's ==.
2017-11-03 00:12:10 +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
93828b9286 Move a block. No functionality changes. 2017-11-02 21:50:55 +01:00
Sei Lisa
9c66380972 Add some early returns for performance. 2017-11-02 21:49:29 +01:00
Sei Lisa
a87022b73f Get rid of StSw craziness and use lsllastpass to make another pass.
This has been a TODO item for long. Now that we have lsllastpass, it's actually easy to implement.

Adds an LSLTypeDefaults dictionary to lslcommon, just in case the state-changing function returns a value and we need to insert a return statement.

We've also added subtree-local info to lsllastpass (lost when we return to the parent after visiting a subtree).

This fixes a bug where naked switch statements could appear as a result of optimization, and cause the compilation to fail.
2017-11-02 18:20:14 +01:00
Sei Lisa
e4eaab9e84 Move the print_node() subtree dump debug function to lslcommon. 2017-11-02 16:34:35 +01:00
Sei Lisa
ef6ed30536 Fix EParseCantChangeState so that it is always properly reported.
Still somewhat messy, but still reported as soon as it can be detected.

If an ELSE token is detected at the top level, for example, the error position will be rewound to the state change and reported there.

This means that in this situation:

x()
{
    if (1)
    {
        state default;
        x(2);
    }
    else ;
}
default{timer(){}}

an error will be reported in x(2), because the ELSE hasn't been found at that point, therefore the state change statement isn't found to be at fault yet.

However, in this case:

x()
{
    if (1)
        state default;
    else
        x(2);
}
default{timer(){}}

the error WILL be reported at the state change statement.

This commit also changes the position where the exception is reported, to be at the STATE token. As an inconsequential side effect, EParseCantChangeState takes precedence over undefined identifiers, in case the state change is to an undefined state, but only in cases where it can be immediately detected.
2017-11-02 13:45:01 +01:00
Sei Lisa
097c054494 Optimize 'if' statements better.
if (!cond) X; else Y;  ->  if (cond) Y; else X;

if (int1 == int2) X; else Y;  ->  if (int1 ^ int2) Y; else X;

When 'cond' is of a type other than 'key': if (cond) ; else X;  ->  if (!cond) X;  (this required changing if(str) to its compiled equivalent if(!(str == "")), so that 'cond' is always either key or integer).

if (cond) ;  ->  cond; and folds it as a statement, which may eliminate it if it's SEF. This is done after eliminating 'else ;' so that it also optimizes 'if (cond) ; else ;' the same way.

This removes a TODO item.
2017-10-30 19:23:01 +01:00
Sei Lisa
ed49170d87 Remove empty SEF events.
If the state becomes empty, add an empty timer() event. timer() is the parameter-less event with the shortest name.
2017-10-30 18:05:35 +01:00
Sei Lisa
e13f210695 Make DoesSomething apply to labels optionally.
Allows detection of empty events, for example, even if they have labels.

Also, it is OK if there's a label inserted in a nested {}; that case wasn't contemplated.
2017-10-30 18:05:28 +01:00
Sei Lisa
f2a6243695 Reuse state names for function parameters; restart UsedNames.
Gives us a few more opportunities for catching single-letter identifiers.

UsedNames was not restarted. It's unlikely that this had any detrimental effect on optimization, and it was certainly safe to not restart it. But it looks more correct like this.
2017-10-28 23:39:25 +02: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
b80b157489 Add a space between a minus sign and a negative constant.
When a constant was negative internally, it was output with the sign included. The code was not prepared to handle this, and could therefore cause double minus signs. For example, -2147483648 was output as --2147483648, and -4294967296 was output as --1.

Fixed by adding a space for floats, and by translating the number to the range 2147483648..4294967295 for integers (hex would have worked just as well).
2017-10-27 21:02:00 +02:00
Sei Lisa
14b13dc4e5 Implement flags aimed at removing "magic names" in the code.
strlen for llStringLength, detect for llDetected* and touch*/collision*/sensor, touch for touch*, grab for touch().
2017-10-27 18:49:58 +02:00
Sei Lisa
3bb5102f06 Improve llStringLength detection, to catch more cases.
For example, if (llStringLength(s) > 0)  ->  if (!(s == ""))
2017-10-27 16:07:36 +02:00
Sei Lisa
1dde2a1fb9 Use lslfuncs.less instead of > >= < <= to ensure proper type handling.
Float functions could be compared against integer args, or vice versa.
2017-10-27 11:20:42 +02:00
Sei Lisa
c0176ad738 Fix order change in comparisons when one side is not SEF.
The comment was wrong anyway. If one side changes x and the other side uses x, then order is still important, no matter whether one side is SEF.

But the reversal is safe when one side is a constant, so we still perform it, to enable optimization of some important cases.
2017-10-27 11:13:03 +02:00
Sei Lisa
106bb81543 Refine a comment.
Powers of two can't be zero anyway.
2017-10-26 01:07:48 +02:00
Sei Lisa
4ba71993a4 Optimize min/max in comparisons; undo problematic optimization.
For floats:

When const >= function.max, comparisons of function > const always yield FALSE.

When const <  function.min, comparisons of function > const always yield TRUE.

When const >  function.max, comparisons of function < const always yield TRUE.

When const <= function.min, comparisons of function < const always yield FALSE.

For integers:

When min = -1, cond(function > -1) is the same as cond(!~function).

When min = -1, cond(function < 0) is the same as cond(~function).

To implement the above, we got rid of the cond(x < 0)  ->  cond(x & 0x80000000) optimization, which has caused more trouble than it has solved for just 1 byte gain.

When min = 0, cond(function > 0) is the same as cond(function).

When min = 0, cond(function < 1) is the same as cond(!function).

Similar expressions can be obtained for max in [-1, 0], but it's not worth it, as there are no functions with -1 as maximum, and the ones with max=0 also have min=0 (always return 0).
2017-10-26 00:58:14 +02:00
Sei Lisa
22e057e2ec Move side-effect-free check for functions to a method.
This will in future allow us to check argument-dependent SEFness.
2017-10-25 19:24:12 +02:00
Sei Lisa
2580248c17 Minor reorganization and documentation of a section. 2017-10-25 18:04:27 +02:00
Sei Lisa
d5f5ab8b88 Add side-effect-free information for events. 2017-10-25 17:46:50 +02:00
Sei Lisa
2d823d8eae Fix Unicode in one error message. 2017-10-25 14:11:34 +02:00
Sei Lisa
7e39635b09 Transform !(x != y) into x == y.
Can happen with lists.
2017-10-21 20:38:54 +02:00
Sei Lisa
fd815f0bdb Fix compare and llListSort for LSO.
In LSO, they compare UTF-8 bytes, not Unicode codepoints.
2017-10-21 12:32:41 +02:00
Sei Lisa
a832f8d786 Remove min, max when min > max, and SEF when both SEF and delay present. 2017-10-21 11:47:29 +02:00
Sei Lisa
923309e4a1 Refine test for min and max.
We had dormant code to check for boolean-ness of functions, which is now active. But it didn't cover all possible booleans. Now it does.

An idea for the future is to associate ranges to expressions, and attach them to calculable functions. For example, (integer)llFrand(2) could be resolved to a boolean.
2017-10-21 11:31:53 +02:00
Sei Lisa
ea9711642c Add type casting comparison to CompareTrees.
It can now resolve e.g. (string)llGetPermissions() == (string)llGetPermissions() to TRUE.
2017-10-21 11:03:43 +02:00
Sei Lisa
dc117c2cbf Use CompareTrees to optimize the == operator.
Now we can.
2017-10-21 11:02:38 +02:00
Sei Lisa
e6b23a2d7a Fix CompareTrees to not compare unstable functions as equal. 2017-10-21 10:44:09 +02:00
Sei Lisa
4d92cc8838 Mark user functions as unstable.
Maybe in future we can perform further analysis to find some that are stable.
2017-10-21 10:42:46 +02:00
Sei Lisa
cdacc45bb0 Make functions unstable by default, for safety.
That ensures that if there's no data about a function, it is not optimizable.
2017-10-21 10:32:56 +02:00
Sei Lisa
ee8c2cde98 Cosmetic fixes. 2017-10-21 10:32:41 +02:00
Sei Lisa
b7700b7aab Remove debug code and seftable.txt 2017-10-21 10:29:12 +02:00
Sei Lisa
214d4a8a57 Add and read function properties table.
This solves a long-standing issue where we needed more data about LSL functions than just whether it's side-effect-free.

There's still some debug code, which is kept for history purposes.
2017-10-21 10:00:31 +02:00
Sei Lisa
3f6f8ed8ad Internal code reorganization.
- Separate library loading code into a new module. parser.__init__() no longer loads the library; it accepts (but does not depend on) a library as a parameter.
- Add an optional library argument to parse(). It's no longer mandatory to create a new parser for switching to a different builtins or seftable file.
- Move warning() and types from lslparse to lslcommon.
- Add .copy() to uses of base_keywords, to not rely on it being a frozen set.
- Adjust the test suite.
2017-10-20 18:19:48 +02:00
Sei Lisa
1a1531cb40 Use sys.getfilesystemencoding() to determine filename encoding. 2017-10-20 13:59:02 +02:00
Sei Lisa
cf3e4c21ec Comment fix.
Specify the name of the last llGetObjectDetails constant, to simplify manual checking.
2017-10-20 10:18:44 +02:00
Sei Lisa
36e336beee Add llGetStatus to lslextrafuncs. 2017-10-20 09:58:49 +02:00
Sei Lisa
3164a28c9b Add llGetAttachedList to lslextrafuncs.
Returns (list)"NOT FOUND" when passed a null key.
2017-10-18 21:57:33 +02:00
Sei Lisa
96a0aebe3a Fix the logic in llDetectedTouchST/UV.
They were returning TOUCH_INVALID_TEXCOORD for num <= idx <= 15 in detection events which were not touch events. That is incorrect.

Now it correctly returns:

- ZERO_VECTOR when idx < 0 or idx > 15 or the event is known not to be a detection event.
- TOUCH_INVALID_TEXCOORD when idx == 0 and the event is known to be a detection event that is not a touch event.
- Raises ELSLCantCompute otherwise.
2017-10-18 21:56:35 +02:00
Sei Lisa
028b244a9e lslbasefuncs: Rewrite some things in a different way.
Also some formatting changes. No functionality changes.
2017-10-18 15:20:19 +02:00
Sei Lisa
ea28e13e4a Implement llClearLinkMedia's known return values. 2017-10-18 13:43:53 +02:00
Sei Lisa
e051d49338 llAsin and llAcos return indet for indet input. 2017-10-18 02:42:58 +02:00
Sei Lisa
e8fb659592 Allow -2147483648 to count as a power of 2 in an optimization.
A case we oversought when implementing absorption of more than two flag tests into a single number.
2017-10-18 00:04:35 +02:00
Sei Lisa
2bee2db148 Remove duplicate check for '|'
Removed the 'if' and applied an indentation change; no further changes are done.
2017-10-17 23:59:41 +02:00
Sei Lisa
be767f24f0 Move things around to better places; undo 8d33746.
We oversought that the optimization that 8d33746 applied was already present, so no need to duplicate it.

A better place for handling '|' was under the code that already did so. No functionality change involved.
2017-10-17 23:53:24 +02:00
Sei Lisa
b4749d8fa4 Add debug code to display a subtree in a friendlier format than repr(). 2017-10-17 23:32:33 +02:00
Sei Lisa
4cbdbefe1b Fold again after applying distributibity or absortion.
Failure to do so was blocking some constant folding, like in:

if (a&1 || a&2 || a&4) -> if (a&(3|4))

Fixed. Now it correctly outputs if (a&7).
2017-10-17 23:28:14 +02:00
Sei Lisa
6c73ba95c2 Allow (x&r) && (x&s) && (x&t) to be collapsed into a single number.
This is an addendum to 3a849fe that allows more than two tests to be chained and optimized.
2017-10-17 23:26:13 +02:00
Sei Lisa
8d337467cb Fold the children of | as conditions when | is folded as condition. 2017-10-17 23:25:26 +02:00
Sei Lisa
a6a16c6e8f Fold ==0, ==1, ==-1 in general expressions, not just in conditions.
Also add a couple FIXMEs.
2017-10-17 02:38:08 +02:00
Sei Lisa
82081b2206 Fix crash when variables appear inside global lists.
Fixes #3.
2017-10-16 02:22:52 +02:00
Sei Lisa
cc96850f66 Fix bug where list elements of global declarations were removed.
CleanNode was too greedy, because children of global declarations (particularly lists) are not marked executable. Make a special case for them and don't recurse, since what matters is whether the declaration itself is executed. Its contents can't be cleaned up.
2017-10-16 02:22:40 +02:00
Sei Lisa
f77590df67 Fix sign of NaN*Indet and NaN/Indet.
In Python, NaN*Indet in any order returns the second operand, and NaN/Indet in any order returns the first operand. LSL consistently returns NaN in all cases.
2017-10-14 15:16:18 +02:00
Sei Lisa
0dc8629007 Raise ELSLInvalidType on invalid type passed to cond(). 2017-10-14 14:17:42 +02:00
Sei Lisa
a5ec12c9e9 Change (float)"NaN" to (-1e40*0) in output.
Solves both an inconsistency and the need to create a string.
2017-10-14 13:05:14 +02:00
Sei Lisa
6faa7816e6 Change ELSLInvalidType to ELSLTypeMismatch where appropriate.
The force type functions ff(), fi(), fs()... should normally trigger ELSLTypeMismatch when the input is not in the expected range of types, rather than ELSLInvalidType, which is reserved for the case where the type is not a valid LSL type.
2017-10-14 11:33:18 +02:00
Sei Lisa
3a849fe4b9 Optimize a common condition of (x & flag1) && (x & flag2)
It can't be done always: flag1 and flag2 must be nonzero powers of two. In that case, we can transform it to:

!~(x|~(flag1|flag2)) = !~(x|constant)

The -2147483648 case has trouble with the sign hack detector and I couldn't trigger it.
2017-10-13 02:59:32 +02:00
Sei Lisa
6ad2ef04d7 Add support for function calls in CompareTrees. 2017-10-13 00:30:47 +02:00
Sei Lisa
7ae7fa4396 Apply two optimizations to bitwise operations.
Based on basic Boolean algebra: distributive and absorption laws.
2017-10-12 22:48:43 +02:00
Sei Lisa
9e7a5d1cdf Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.

These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.

A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.

The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.

This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 17:16:16 +02:00
Sei Lisa
41d2c68cf8 Add some more functions with predictable results.
Also minor reformatting forgotten in a previous commit.
2017-10-12 12:43:54 +02:00
Sei Lisa
1071941301 Implement accurate error reporting through #line directives.
Also simplify and fix the matching expression for #line (gcc inserts numeric flags at the end).

It still has many problems. It's O(n^2). It's calculated at every EParse, and EParse can be triggered and ignored while scanning vectors or globals. UniConvScript doesn't read #line at all, thus failing to report a meaningful input line. But at least it's a start.
2017-10-11 05:04:13 +02:00
Sei Lisa
4ba0518353 Report EParseReturnIsEmpty at return, EParseReturnShouldBeEmpty at expression.
Also change idpos to savepos, for consistency, and add note on why EParseReturnShouldBeEmpty is named like that.
2017-10-09 11:35:59 +02:00
Sei Lisa
4633c87a7c Report EParseUndefined at the identifier causing it.
It was being reported at the next token in some cases, due to needing to resolve the scope.
2017-10-05 18:51:53 +02:00
Sei Lisa
c544b51e37 Rewrite ReportError() and change EParse to report columns in chars.
ReportError() needed to account for terminal encodings that don't support the characters being printed. It was also reporting an inaccurate column number and its corresponding marker position, because the count was in bytes, not in characters, so that has been fixed.

Now EParse.__init__() calls a new function GetErrLineCol() that calculates the line and column corresponding to an error position.

The algorithm for finding the start of the line has also been changed in both ReportError() and EParse.__init__(); as a result, function fieldpos() has been removed.

The exception's lno and cno fields have been changed to be 1-based, rather than 0-based.

Thanks to @Jomik for the report. Fixes #5.
2017-10-05 18:50:45 +02:00
Sei Lisa
08c69eee0f Simplify a = a = b -> a = b 2017-09-22 16:14:50 +02:00
Sei Lisa
bdd42199eb Fix bug where assignments were treated as statements.
This caused e.g. b = a = a to raise an exception, when a = a was folded into a ';' which isn't an expression.
2017-09-22 16:04:44 +02:00
Sei Lisa
a0d4c77081 Minor cleanups; no functional changes.
lslcleanup: Variables renamed, order changed, comments added.

Other changes: remove semicolon at end of sentence, use self.Cast instead of creating a CAST node on the fly.
2017-09-22 15:42:42 +02:00
Sei Lisa
0a7e2a9e1d Optimize llDeleteSubList(expr, 0, -1) to [] if expr is SEF
It also applies to llListReplaceList with [] as replacement.
2017-09-15 23:13:59 +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
de29a9aa07 Fix for,while,if,do condition test during optimization.
That's exactly what 'cond' is for. Things like a loop with a constant zero vector condition should be recognized.

It was correct before just by chance, because FoldCond currently transforms a constant node into an integer node, but let's not rely on that.
2017-09-15 01:36:13 +02:00
Sei Lisa
b97b2a78e8 Fix return value for some detection functions.
When the index is good, on non-touch functions:
 - llDetectedTouchFace returns -1.
 - llDetectedTouchST and llDetectedTouchUV return TOUCH_INVALID_TEXCOORD.

We were returning 0 and ZERO_VECTOR respectively.
2017-08-30 19:23:38 +02:00
Sei Lisa
62eebb8bef Remove line no longer used from lsloptimizer.py. 2017-08-30 19:22:12 +02:00
Sei Lisa
2edd139a5c Fix copy/paste error in comment. 2017-08-25 20:45:24 +02:00
Sei Lisa
03c0e326c6 Remove "OK" from llDialog button list when it's the only element.
"OK" is the default; an empty list works just the same.
2017-08-25 20:35:24 +02:00