Commit graph

5308 commits

Author SHA1 Message Date
Andreas Kling
9d57b55f24 LibJS/Bytecode: Make Generator aware of the function it's compiling 2024-06-01 09:39:50 +02:00
Andreas Kling
59c2637fdc LibJS/Bytecode: Rename emit_function_body_bytecode() -> compile()
This function isn't really one of the "emit_foo" family -- it's the main
compilation driver!
2024-06-01 09:39:50 +02:00
Andreas Kling
a48fc971c6 LibJS: Rearrange ExecutionContext members to shrink the class
Just a minor tweak to make the class 8 bytes smaller.
2024-05-31 16:31:33 +02:00
Andreas Kling
a3782782fa LibJS: Remove two unused members from ExecutionContext 2024-05-31 16:31:33 +02:00
Matthew Olsson
9ea6ab0ad4 LibJS+LibWeb: Fix a ton of JS_CELL-like macro issues 2024-05-30 09:29:20 -06:00
Matthew Olsson
3e221fbb2d IDLGenerators: Handle restricted/unrestricted floating point types 2024-05-29 08:18:24 +02:00
Andreas Kling
2aab56bf71 LibJS: Null-check current executable in VM::dump_backtrace()
If there is no current executable (because we're in a native function
call), we shouldn't try to dereference it.
2024-05-27 17:33:29 +02:00
Shannon Booth
f28bb90d9b LibJS: Remove non-spec compliant code from internal_construct
It seems that we are now spec compliant enough to be able to remove this
code block :^)

Diff Tests:
    +2     -2 
2024-05-26 12:29:41 +02:00
Aliaksandr Kalenik
63a56a77a2 LibJS: Add fast ExecutionContext allocator
...that maintains a list of allocated execution contexts so malloc()
does not have to be called every time we need to get a new one.

20% improvement in Octane/typescript.js
16% improvement in Kraken/imaging-darkroom.js
2024-05-23 13:36:02 +02:00
Aliaksandr Kalenik
f29ac8517e LibJS: Skip ordinary_call_bind_this() when possible
If during parsing it was found that function won't use `this` then
there is no need to initialise `this_value` during call.
2024-05-23 09:53:31 +02:00
Aliaksandr Kalenik
e934132442 LibJS+LibWeb: Pass function metadata collected in parsing using a struct
By using separate struct we can avoid updating AST node and
ECMAScriptFunctionObject constructors every time there is a need to
add or remove some additional information colllected during parsing.
2024-05-23 09:53:31 +02:00
Matthew Olsson
a98ad191c7 Userland: Add ESCAPING annotations to a bunch of places
This isn't comprehensive; just a result of a simple grep search.
2024-05-22 21:55:34 -06:00
Matthew Olsson
a5f4c9a632 AK+Userland: Remove NOESCAPE
See the next commit for an explanation
2024-05-22 21:55:34 -06:00
Aliaksandr Kalenik
ebb3d8025c LibJS: Get this from execution context for non-arrow functions
Allows to skip function environment allocation for non-arrow functions
if the only reason it is needed is to hold `this` binding.

The parser is changed to do following:
- If a function is an arrow function and uses `this` then all functions
  in a scope chain are marked to allocate function environment for
  `this` binding.
- If a function uses `new.target` then all functions in a scope chain
  are marked to allocate function environment.

`ordinary_call_bind_this()` is changed to put `this` value in execution
context when function environment allocation is skipped.

35% improvement in Octane/typescript.js
50% improvement in Octane/deltablue.js
19% improvement in Octane/raytrace.js
2024-05-22 18:30:13 +02:00
Aliaksandr Kalenik
4d37b9e715 LibJS: Do not count arguments as env binding when local variable is used
This is a follow up for 210a5d77dc that
actually allows to skip function environment allocation.
2024-05-21 15:49:29 +00:00
Aliaksandr Kalenik
210a5d77dc LibJS: Use a local variable for arguments object when possible
This allows us to skip allocating a function environment in cases where
it was previously impossible because the arguments object needed a
binding.

This change does not bring visible improvement in Kraken or Octane
benchmarks but seems useful to have anyway.
2024-05-21 11:24:50 +02:00
Andreas Kling
448b7ca87b LibJS/Bytecode: Add dedicated instruction for getting length property
By doing this, we can remove all the special-case checks for `length`
from the generic GetById and GetByIdWithThis code paths, making every
other property lookup a bit faster.

Small progressions on most benchmarks, and some larger progressions:

- 12% on Octane/crypto.js
- 6% on Kraken/ai-astar.js
2024-05-20 12:51:56 +02:00
Hendiadyoin1
d79347dd4a LibJS: Remove an old VERIFY from break handling in try-finally blocks
The VERIFY assumed that we either have a finally block which we already
visited through a previous boundary or have no finally block what so
ever; But since 301a1fc763 we propagate
finalizers through unwind scopes breaking that assumption.
2024-05-19 17:35:04 +02:00
Andreas Kling
b2e6843055 LibJS+AK: Fix integer overflow UB on (any Int32 - -2147483648)
It wasn't safe to use addition_would_overflow(a, -b) to check if
subtraction (a - b) would overflow, since it doesn't cover this case.

I don't know why we didn't have subtraction_would_overflow(), so this
patch adds it. :^)
2024-05-18 18:11:50 +02:00
Hendiadyoin1
1de475b404 LibJS: Prepare yield object before re-routing it through finally 2024-05-18 18:11:10 +02:00
Hendiadyoin1
c8e4499b08 LibJS: Make return control flow more static
With this only `ContinuePendingUnwind` needs to dynamically check if a
scheduled return needs to go through a `finally` block, making the
interpreter loop a bit nicer
2024-05-15 04:25:14 +02:00
Hendiadyoin1
73fdd31124 LibJS: Avoid returning Completions from more Bytecode instructions 2024-05-15 04:25:14 +02:00
Andreas Kling
ce7c925924 LibJS/Bytecode: Allow assignment expression to write directly to locals 2024-05-14 21:46:36 +02:00
Andreas Kling
d22a06d671 LibJS/Bytecode: Remove all the unreachable execute_impl() functions
There are no calls to these anywhere anymore.
2024-05-14 21:46:36 +02:00
Andreas Kling
6ca94bd0b1 LibJS/Bytecode: Rename GetVariable => GetBinding 2024-05-14 21:46:36 +02:00
Andreas Kling
b7c04f999a LibJS/Bytecode: Split SetVariable into four separate instructions
Instead of SetVariable having 2x2 modes for variable/lexical and
initialize/set, those 4 modes are now separate instructions, which
makes each instruction much less branchy.
2024-05-14 21:46:36 +02:00
Andreas Kling
640f195a70 LibJS/Bytecode: Sort ENUMERATE_BYTECODE_OPS list 2024-05-14 21:46:36 +02:00
Andreas Kling
9265385807 LibJS/Bytecode: Don't bother propagating completion values in functions
The last completion value in a function is not exposed to the language,
since functions always either return something, or undefined.

Given this, we can avoid emitting code that propagates the completion
value from various statements, as long as we know we're generating code
for a context where the completion value is not accessible. In practical
terms, this means that function code gets to do less completion
shuffling, while global and eval code has to keep doing it.
2024-05-14 21:46:36 +02:00
Andreas Kling
ed50eb0aaa LibJS/Bytecode: Add environment coordinate caching to SetVariable
This means that SetVariable instructions will now remember which
(relative) environment contains the targeted binding, letting it bypass
the full binding resolution machinery on subsequent accesses.
2024-05-14 06:39:27 +02:00
Aliaksandr Kalenik
caffd485b8 LibJS: Replace SetLocal instruction usage with Mov
No need for separate instuction to set a local.
2024-05-14 06:39:16 +02:00
Matthew Olsson
4ae7bbda52 Lagom: Add ClangPlugins to the build system 2024-05-13 16:50:54 -06:00
Aliaksandr Kalenik
d79438a2a6 LibJS: Join locals, constants and registers into single vector
Merging registers, constants and locals into single vector means:
- Better data locality
- No need to check type in Interpreter::get() and Interpreter::set()
  which are very hot functions

Performance improvement is visible in almost all Octane and Kraken
tests.
2024-05-13 19:54:11 +02:00
Andreas Kling
6f5201b759 LibJS: Add fast paths in TypedArrayPrototype.fill()
Filling a typed array with an integer shouldn't have to go through the
generic Set for every element.

This knocks a 7% item down to <1% in profiles of Another World JS at
https://cyxx.github.io/another_js/
2024-05-13 17:29:37 +02:00
Andreas Kling
d79353a477 LibJS/Bytecode: Add fast paths for compare-and-jump with 2 numbers
When comparing two numbers, we can avoid a lot of implicit type
conversion nonsense and go straight to comparison, saving time in the
most common case.
2024-05-13 17:29:37 +02:00
Andreas Kling
855f6417df LibJS/Bytecode: Move environment variable caches into instructions
These were out-of-line because we had some ideas about marking
instruction streams PROT_READ only, but that seems pretty arbitrary and
there's a lot of performance to be gained by putting these inline.
2024-05-13 09:22:14 +02:00
Andreas Kling
a06441c88c LibJS/Bytecode: Defer GetGlobal identifier lookup until cache misses
This way we avoid looking up the identifier when the cache hits.
2024-05-13 09:22:14 +02:00
Andreas Kling
6ec4d6f668 LibJS/Bytecode: Cache the running execution context in interpreter 2024-05-13 09:22:14 +02:00
Andreas Kling
8447f6f6da LibJS: Inline more of cached environment variable access in interpreter
And stop passing VM strictness to direct access, since it doesn't care
about strictness anyway.
2024-05-13 09:22:14 +02:00
Aliaksandr Kalenik
8bcaf68023 LibJS: Remove VM::execute_ast_node() 2024-05-12 19:10:25 +02:00
Aliaksandr Kalenik
6fb1d9e516 LibJS: Stop using execute_ast_node() for class property evaluation
Instead, generate bytecode to execute their AST nodes and save the
resulting operands inside the NewClass instruction.

Moving property expression evaluation to happen before NewClass
execution also moves along creation of new private environment and
its population with private members (private members should be visible
during property evaluation).

Before:
- NewClass

After:
- CreatePrivateEnvironment
- AddPrivateName
- ...
- AddPrivateName
- NewClass
- LeavePrivateEnvironment
2024-05-12 19:10:25 +02:00
Aliaksandr Kalenik
b46fc47bf7 LibJS: Remove execute_ast_node() usage for ClassMethod function creation
Call ECMAScriptFunctionObject constructor directly instead of using
execute_ast_node() to emit NewFunction instruction.
2024-05-12 19:10:25 +02:00
Aliaksandr Kalenik
0c8e76cbd7 LibJS: Delete named_evaluation_if_anonymous_function()
This code was a leftover from AST interpreter.
2024-05-11 18:16:15 +02:00
Andreas Kling
6ad6e09be1 LibJS: Make Environment::is_declarative_environment() non-virtual
This function was moderately hot (2%) on the clusters demo, and making
it non-virtual costs us nothing.
2024-05-11 15:22:36 +02:00
Andreas Kling
b5a070e8ce LibJS/Bytecode: Don't create empty lexical environments
This patch stops emitting the BlockDeclarationInstantiation instruction
when there are no locals, and no function declarations in the scope.

We were spending 20% of CPU time on https://ventrella.com/Clusters/ just
creating empty environments for no reason.
2024-05-11 15:22:36 +02:00
Andreas Kling
239b9d8662 LibJS: Manually limit the recursion depth in Proxy
Instead of relying on native stack overflows to kick us out of circular
proxy chains, we now keep track of the recursion depth and kick
ourselves out if it exceeds 10'000.

This fixes an issue where compiler tail/sibling call optimizations would
turn infinite recursion into infinite loops, and thus never hit a stack
overflow to kick itself out.

For whatever reason, we've only seen the issue on SerenityOS with UBSAN,
but it could theoretically happen on any platform.
2024-05-11 13:00:46 +02:00
Aliaksandr Kalenik
1e361db3f3 LibJS: Remove VM::binding_initialization()
This function was used for function params initialization but no longer
used after it got replaced by emitting bytecode.
2024-05-11 11:43:05 +02:00
Aliaksandr Kalenik
3d4b13a01c LibJS: Ensure capacity for created lexical and variable environments
If the minimal amount of required bindings is known in advance, it could
be used to ensure capacity to avoid resizing the internal vector that
holds bindings.
2024-05-11 11:43:05 +02:00
Aliaksandr Kalenik
a4f70986a0 LibJS: Emit bytecode for function declaration instantiation
By doing that all instructions required for instantiation are emitted
once in compilation and then reused for subsequent calls, instead of
running generic instantiation process for each call.
2024-05-11 11:43:05 +02:00
Aliaksandr Kalenik
89a007327a LibJS: Change NewFunction instruction to accept FunctionNode
Preparation for upcoming changes where NewFunction will have to be used
with FunctionDeclaration node.
2024-05-11 11:43:05 +02:00
Aliaksandr Kalenik
00018ad415 LibJS: Move BindingPattern bytecode generation into a method
Preparation for upcoming function where binding pattern will have to be
used outside of ASTCodegen.cpp
2024-05-11 11:43:05 +02:00