ladybird/Userland/Libraries/LibJS/Bytecode/Instruction.h

95 lines
2.5 KiB
C
Raw Normal View History

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <LibJS/Forward.h>
#define ENUMERATE_BYTECODE_OPS(O) \
O(Load) \
O(LoadImmediate) \
O(Store) \
O(Add) \
O(Sub) \
O(Mul) \
O(Div) \
O(Mod) \
O(Exp) \
O(GreaterThan) \
O(GreaterThanEquals) \
O(LessThan) \
O(LessThanEquals) \
O(AbstractInequals) \
O(AbstractEquals) \
O(TypedInequals) \
O(TypedEquals) \
O(NewBigInt) \
O(NewArray) \
O(NewString) \
O(NewObject) \
O(GetVariable) \
O(SetVariable) \
O(PutById) \
O(GetById) \
O(Jump) \
O(JumpConditional) \
O(JumpNullish) \
O(Call) \
O(NewFunction) \
2021-06-07 21:16:04 +03:00
O(Return) \
O(BitwiseAnd) \
O(BitwiseOr) \
O(BitwiseXor) \
O(BitwiseNot) \
O(Not) \
O(UnaryPlus) \
O(UnaryMinus) \
O(Typeof) \
O(LeftShift) \
O(RightShift) \
O(UnsignedRightShift) \
O(In) \
O(InstanceOf) \
O(ConcatString) \
O(Increment) \
O(Decrement) \
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 16:04:38 +03:00
O(Throw) \
O(EnterUnwindContext) \
O(LeaveUnwindContext) \
O(ContinuePendingUnwind)
namespace JS::Bytecode {
class Instruction {
public:
constexpr static bool IsTerminator = false;
enum class Type {
#define __BYTECODE_OP(op) \
op,
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
#undef __BYTECODE_OP
};
Type type() const { return m_type; }
size_t length() const;
String to_string(Bytecode::Executable const&) const;
void execute(Bytecode::Interpreter&) const;
static void destroy(Instruction&);
protected:
explicit Instruction(Type type)
: m_type(type)
{
}
private:
Type m_type {};
};
}