Commit Graph

43 Commits

Author SHA1 Message Date
Andreas Kling
f5feb1d2cd LibJS: Very basic support for "new" construction in bytecode VM
This patch adds a CallType to the Bytecode::Op::Call instruction,
which can be either Call or Construct. We then generate Construct
calls for the NewExpression AST node.

When executed, these get fed into VM::construct().
2021-06-10 23:01:49 +02:00
Andreas Kling
c3c68399b5 LibJS: Generate bytecode for entering nested lexical environments
This adds a new PushLexicalEnvironment instruction that creates a new
LexicalEnvironment and pushes it on the VM's scope stack.

There is no corresponding PopLexicalEnvironment instruction yet,
so this will behave incorrectly with let/const scopes for example.
2021-06-10 22:20:23 +02:00
Andreas Kling
b3e6a6c1cd LibJS: Perform function instantiation in bytecode
This replaces Bytecode::Op::EnterScope with a new NewFunction op that
instantiates a ScriptFunction from a given FunctionNode (AST).

This is then used to instantiate the local functions directly from
bytecode when entering a ScopeNode. :^)
2021-06-10 21:59:49 +02:00
Gunnar Beutner
67cc31a74f LibJS: Implement bytecode generation for try..catch..finally
EnterUnwindContext pushes an unwind context (exception handler and/or
finalizer) onto a stack.

LeaveUnwindContext pops the unwind context from that stack.

Upon return to the interpreter loop we check whether the VM has an
exception pending. If no unwind context is available we return from the
loop. If an exception handler is available we clear the VM's exception,
put the exception value into the accumulator register, clear the unwind
context's handler and jump to the handler. If no handler is available
but a finalizer is available we save the exception value + metadata (for
 later use by ContinuePendingUnwind), clear the VM's exception, pop the
unwind context and jump to the finalizer.

ContinuePendingUnwind checks whether a saved exception is available. If
no saved exception is available it jumps to the resume label. Otherwise
it stores the exception into the VM.

The Jump after LeaveUnwindContext could be integrated into the
LeaveUnwindContext instruction. I've kept them separate for now to make
the bytecode more readable.

> try { 1; throw "x" } catch (e) { 2 } finally { 3 }; 4
1:
[   0] EnterScope
[  10] EnterUnwindContext handler:@4 finalizer:@3
[  38] EnterScope
[  48] LoadImmediate 1
[  60] NewString 1 ("x")
[  70] Throw
<for non-terminated blocks: insert LeaveUnwindContext + Jump @3 here>
2:
[   0] LoadImmediate 4
3:
[   0] EnterScope
[  10] LoadImmediate 3
[  28] ContinuePendingUnwind resume:@2
4:
[   0] SetVariable 0 (e)
[  10] EnterScope
[  20] LoadImmediate 2
[  38] LeaveUnwindContext
[  3c] Jump @3

String Table:
0: e
1: x
2021-06-10 21:59:46 +02:00
Gunnar Beutner
b78f1c1261 LibJS: Generate bytecode for throw statements 2021-06-09 20:04:11 +02:00
Gunnar Beutner
6a0d1fa259 LibJS: Store strings in a string table
Instead of using Strings in the bytecode ops this adds a global string
table to the Executable struct which individual operations can refer
to using indices. This brings bytecode ops one step closer to being
pointer free.
2021-06-09 17:42:52 +02:00
Andreas Kling
59eedd6de0 LibJS: Implement bytecode generation for UpdateExpression :^)
Added Increment and Decrement bytecode ops to support this. Postfix
updates use a temporary register to preserve the original value.

Note that this patch only implements Identifier updates. Member
expression updates are a TODO.
2021-06-09 11:40:38 +02:00
Andreas Kling
a8ccc9580d LibJS: Move Instruction::length() to the Op.h header
Make sure this gets inlined as well, as it's used by the bytecode
stream iterator and thus extremely hot.
2021-06-09 09:24:32 +02:00
Andreas Kling
48a8022cf6 LibJS: Move Bytecode::Instruction::execute() to the Op.h header
..and make sure it always gets inlined in the interpreter loop.
2021-06-09 09:24:32 +02:00
Ali Mohammad Pur
01e8f0889a LibJS: Generate bytecode in basic blocks instead of one big block
This limits the size of each block (currently set to 1K), and gets us
closer to a canonical, more easily analysable bytecode format.
As a result of this, "Labels" are now simply entries to basic blocks.
Since there is no more 'conditional' jump (as all jumps are always
taken), JumpIf{True,False} are unified to JumpConditional, and
JumpIfNullish is renamed to JumpNullish.
Also fixes #7914 as a result of reimplementing the loop logic.
2021-06-09 09:07:29 +02:00
Gunnar Beutner
a1e5711a27 LibJS: Generate bytecode for array expressions 2021-06-09 01:27:18 +02:00
Matthew Olsson
9bed2e4f4a LibJS: Introduce an accumulator register to Bytecode::Interpreter
This commit introduces the concept of an accumulator register to
LibJS's bytecode interpreter. The accumulator register is always
register 0, and most simple instructions use it for reading and
writing.

Not only does this slim down the AST, but it also simplifies a lot of
the code. For example, the generate_bytecode methods no longer need
to return an Optional<Register>, as any opcode which has a "return"
value will always put it into the accumulator.

This also renames the old Op::Load to Op::LoadImmediate, and uses
Op::Load to load from a register into the accumulator. There is
also an Op::Store to put the value in the accumulator into another
register.
2021-06-08 21:00:12 +02:00
Gunnar Beutner
50ece3dd1b LibJS: Implement bytecode generation for BigInts 2021-06-08 10:57:28 +01:00
Gunnar Beutner
ef83872f62 LibJS: Make JumpIf{True,False,Nullish} inherit from Jump
This saves a few lines in LogicalExpression::generate_bytecode.
2021-06-08 11:38:48 +02:00
Gunnar Beutner
6da587b59b LibJS: Implement bytecode ops for logical expressions 2021-06-08 10:42:45 +02:00
Andreas Kling
ebb40e7d7b LibJS: Use macros to generate the common unary/binary bytecode ops 2021-06-07 23:20:48 +02:00
Linus Groh
9c0d83d11d LibJS: Add bytecode generation for BinaryOp::InstanceOf 2021-06-07 21:18:35 +01:00
Linus Groh
5e996de8c6 LibJS: Add bytecode generation for BinaryOp::In 2021-06-07 21:18:35 +01:00
Gunnar Beutner
93eae063a1 LibJS: Make sure that if expressions yield the correct value
When evaluated as an expression "if (true) { 3 } else { 5 }"
should yield 3. This updates the bytecode interpreter to make
it so.
2021-06-07 22:10:57 +02:00
Luke
1e10965e61 LibJS: Add bytecode ops for <<, >> and >>> 2021-06-07 21:18:34 +02:00
Ryan Chandler
18ac7fde12 LibJS: Add support for typed equality checks 2021-06-07 21:17:29 +02:00
Linus Groh
fa9bad912e LibJS: Add bytecode instructions for a bunch of unary operators
~, !, +, -, typeof, and void.
2021-06-07 19:53:47 +01:00
Ryan Chandler
6e2b266534 LibJS: Fix AbstractInequals returning result of AbstractEquals 2021-06-07 20:25:56 +02:00
Luke
ae763f1ade LibJS: Add bytecode ops for &, | and ^ 2021-06-07 20:17:24 +02:00
Gunnar Beutner
4be3374b24 LibJS: Add bytecode ops for >, >= and <= 2021-06-07 20:09:23 +02:00
Gunnar Beutner
55f0791b13 LibJS: Add bytecode instructions for modulo and exponentiation 2021-06-07 19:40:27 +02:00
Gunnar Beutner
3c5ce9b5b7 LibJS: Add bytecode instructions for multiplication and division 2021-06-07 19:21:36 +02:00
Andreas Kling
e7d69c5d3c LibJS: Devirtualize and pack the bytecode stream :^)
This patch changes the LibJS bytecode to be a stream of instructions
packed one-after-the-other in contiguous memory, instead of a vector
of OwnPtr<Instruction>. This should be a lot more cache-friendly. :^)

Instructions are also devirtualized and instead have a type field
using a new Instruction::Type enum.

To iterate over a bytecode stream, one must now use
Bytecode::InstructionStreamIterator.
2021-06-07 18:11:59 +02:00
Andreas Kling
0cc9d47e1b LibJS: Add AbstractEquals bytecode instruction for == comparison :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
79eac08f5b LibJS: Add basic "if" statement support to the bytecode VM :^)
This also required making Bytecode::Op::Jump support lazy linking
to a target label.

I left a FIXME here about having the "if" statement return the result
value from the taken branch statement. That's what the AST interpreter
does but I'm not sure if it's actually required.
2021-06-07 18:11:59 +02:00
Andreas Kling
80b1604b0a LibJS: Compile ScriptFunctions into bytecode and run them that way :^)
If there's a current Bytecode::Interpreter in action, ScriptFunction
will now compile itself into bytecode and execute in that context.

This patch also adds the Return bytecode instruction so that we can
actually return values from called functions. :^)

Return values are propagated from callee to caller via the caller's
$0 register. Bytecode::Interpreter now keeps a stack of register
"windows". These are not very efficient, but it should be pretty
straightforward to convert them to e.g a sliding register window
architecture later on.

This is pretty dang cool! :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
dc63958478 LibJS: Support basic function calls in the bytecode world :^)
This patch adds the Call bytecode instruction which is emitted for the
CallExpression AST node.

It's pretty barebones and doesn't handle 'this' values properly, etc.
But it can perform basic function calls! :^)

Note that the called function will *not* execute as bytecode, but will
simply fall back into the old codepath and use the AST interpreter.
2021-06-07 18:11:59 +02:00
Andreas Kling
1eafaf67fe LibJS: Add a new EnterScope bytecode instruction
This is intended to perform the same duties as enter_scope() does in
the AST tree-walk interpreter:

- Hoisted function declaration processing
- Hoisted variable declaration processing
- ... maybe more

This first cut only implements the function declaration processing.
2021-06-07 18:11:59 +02:00
Andreas Kling
32561bb90d LibJS: Add GetById bytecode instruction for object property retrieval
Same as PutById but in the other direction. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
14cfc44855 LibJS: Add PutById bytecode instruction for object property assignment
Note that this is only used for non-computed accesses. Computed access
is not yet implemented. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
bea6e31ddc LibJS: Add a NewObject bytecode instruction for ObjectExpression :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
f2863b5a89 LibJS: Generate bytecode for do...while statements :^)
This was quite straightforward using the same label/jump machinery that
we added for while statements.

The main addition here is a new JumpIfTrue bytecode instruction.
2021-06-07 18:11:59 +02:00
Andreas Kling
bd1a5e282a LibJS: Add AbstractInequals bytecode instruction :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
6ae9346cd3 LibJS: Add basic support for while loops in the bytecode engine
This introduces two new instructions: Jump and JumpIfFalse.
Jumps are made to a Bytecode::Label, which is a simple object that
represents a location in the bytecode stream.

Note that you may not always know the target of a jump when adding the
jump instruction itself, but we can just update the instruction later
on during codegen once we know where the jump target is.

The Bytecode::Interpreter now implements jumping via a jump slot that
gets checked after each instruction to see if a jump is pending.
If not, we just increment the PC as usual.
2021-06-07 18:11:59 +02:00
Andreas Kling
91640d0727 LibJS: Add LessThan bytecode instruction :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
2b9fbd10ed LibJS: Add Sub bytecode instruction (subtract values) 2021-06-07 18:11:59 +02:00
Andreas Kling
37cb70836b LibJS: Some more opcodes for the bytecode VM
- NewString (allocates a new PrimitiveString from the GC heap)
- GetVariable (retrieves a variable in the current scope)
- SetVariable (assigns a variable in the current scope)
2021-06-07 18:11:59 +02:00
Andreas Kling
69dddd4ef5 LibJS: Start fleshing out a bytecode for the JavaScript engine :^)
This patch begins the work of implementing JavaScript execution in a
bytecode VM instead of an AST tree-walk interpreter.

It's probably quite naive, but we have to start somewhere.

The basic idea is that you call Bytecode::Generator::generate() on an
AST node and it hands you back a Bytecode::Block filled with
instructions that can then be interpreted by a Bytecode::Interpreter.

This first version only implements two instructions: Load and Add. :^)

Each bytecode block has infinity registers, and the interpreter resizes
its register file to fit the block being executed.

Two new `js` options are added in this patch as well:

`-d` will dump the generated bytecode
`-b` will execute the generated bytecode

Note that unless `-d` and/or `-b` are specified, none of the bytecode
related stuff in LibJS runs at all. This is implemented in parallel
with the existing AST interpreter. :^)
2021-06-07 18:11:59 +02:00