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

79 lines
1.9 KiB
C
Raw Normal View History

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/String.h>
#include <LibJS/Forward.h>
namespace JS::Bytecode {
class InstructionStreamIterator {
public:
explicit InstructionStreamIterator(ReadonlyBytes bytes)
: m_bytes(bytes)
{
}
size_t offset() const { return m_offset; }
bool at_end() const { return m_offset >= m_bytes.size(); }
void jump(size_t offset)
{
VERIFY(offset <= m_bytes.size());
m_offset = offset;
}
Instruction const& operator*() const { return dereference(); }
void operator++();
private:
Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
ReadonlyBytes m_bytes;
size_t m_offset { 0 };
};
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
struct UnwindInfo {
BasicBlock const* handler;
BasicBlock const* finalizer;
};
class BasicBlock {
AK_MAKE_NONCOPYABLE(BasicBlock);
public:
static NonnullOwnPtr<BasicBlock> create(String name, size_t size = 4 * KiB);
~BasicBlock();
void seal();
void dump(Executable const&) const;
ReadonlyBytes instruction_stream() const { return ReadonlyBytes { m_buffer, m_buffer_size }; }
size_t size() const { return m_buffer_size; }
void* next_slot() { return m_buffer + m_buffer_size; }
bool can_grow(size_t additional_size) const { return m_buffer_size + additional_size <= m_buffer_capacity; }
void grow(size_t additional_size);
void terminate(Badge<Generator>) { m_is_terminated = true; }
bool is_terminated() const { return m_is_terminated; }
String const& name() const { return m_name; }
private:
BasicBlock(String name, size_t size);
u8* m_buffer { nullptr };
size_t m_buffer_capacity { 0 };
size_t m_buffer_size { 0 };
bool m_is_terminated { false };
String m_name;
};
}