Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.0

0.6.0 / src / engine / interpreter.lucb

#==============================================================================================
#
#   interpreter - JS_CallInternal: the bytecode interpreter's dispatch loop
#
#   DESCRIPTION:
#       Port of quickjs.c lines 17746-20709: js_call_internal and its dispatch loop. The
#       frame setup of a bytecode call (or the resumption of a suspended generator frame),
#       the exception unwinding and the frame's release are in interp_frames.lucb; the
#       opcodes the loop does not run itself go through interp_outlined.lucb to their
#       bodies in the interp_ops_*.lucb fragments.
#
#       State. C keeps pc, sp, the frame buffers, b, ctx, ... in locals of JS_CallInternal.
#       Here pc and sp are locals of the dispatch loop, and the frame's constants (ctx, the
#       variable and argument buffers, the closure variables) locals of js_call_internal;
#       all of them are also fields of one InterpState (interpreter_support.lucb), which
#       the outlined bodies take a pointer to. pc and sp are stored to it before an
#       outlined body runs and read back after.
#
#       Frame. js_call_internal's native frame is the stack one level of JavaScript
#       recursion takes (the call opcodes call it from the loop, as C does): about 1.3 KB
#       with the 256 bytes of frame_space. So the loop runs in place only what C's loop
#       runs most, and in a form that adds little to the frame: values are written as tag
#       and payload where they lie and reference counts tested in place (no Value
#       locals, no calls of helpers with parameters or results, whose expansions each keep
#       a frame slot), and a case the fast path does not take falls through, unchanged, to
#       the outlined body. With every body inline the frame was 48 KB.
#
#       Structure. C dispatches with computed goto (or a switch) and leaves each opcode
#       with `BREAK`, `goto exception`, `goto done` or `goto done_generator`. Base has no
#       goto, so the function is two nested loops:
#
#           frame: while true:                  # C: `restart:` + the `exception:` label
#               if not throwing:
#                   dispatch: while true:       # C: `for(;;) { SWITCH(pc) { ... } }`
#                       op = (Op)*pc; pc += 1
#                       match op:               # the fast paths
#                           .push_i32: ...; continue dispatch
#                           .add: if both ints and no overflow: ...; continue dispatch
#                           .return_: ... exit = .done; break frame      # goto done
#                           _: ...
#                       st.pc = pc; st.sp = sp  # everything else
#                       match interp_outlined(&st):
#                           .next: pc = st.pc; sp = st.sp
#                           .exception: break dispatch                   # goto exception
#                           .done: exit = .done; break frame
#                           .done_generator: exit = .done_generator; break frame
#               # exception: interp_unwind unwinds the stack to the innermost catch
#               # offset; when one is found, `continue frame` runs its handler;
#               # otherwise `exit = .exception; break frame`.
#           interp_leave(&st, exit)             # done, done_generator or the exception
#
#       - An opcode body that raises answers false: a fallible callee failed (`f(...)
#         catch: return false`), or a `js_throw_*` helper stored the exception. The stack
#         pointer is left exactly where C leaves it at its `goto exception`, so the
#         unwinding frees the same values; a C push of JS_EXCEPTION (`*sp++ = f(); if
#         (JS_IsException(sp[-1])) goto exception;`) pushes nothing, which the unwinding
#         cannot tell apart. Where C does cleanup between the call and its exception
#         test, the call recovers C's status and the C test follows unchanged:
#         `bool_status(f(...) catch: recover none)` gives C's -1/0/1 for a `bool!`
#         callee, `catch: recover js_exception()` C's JS_EXCEPTION for a `Value!` one,
#         and a unit callee sets `ret = -1` in its handler.
#       - `goto done` and `goto done_generator` set `exit` (and st.ret_val) and leave with
#         `break frame` (interp_outlined answers .done or .done_generator): the returns,
#         the tail calls and the generator suspensions stay in the loop. A generator resumed with `throw_flag` starts at the exception
#         handler (`throwing`).
#       - Shared C labels inside an opcode (`add_slow_case`, `binary_arith_slow`,
#         `mul_fp_res`, the `*_slow_path` labels, `set_true`, ...) are a local flag that
#         selects the shared tail of the body. The opcodes that share a body (call0 to
#         call3 with call, get_var_undef with get_var, ...) are one arm, as in C.
#       - The large macros are inline functions of interpreter_support.lucb.
#       - pc, sp and the frame buffers are pointers, exactly as in C: `sp[-1]` is the top
#         of the stack.
#       - C allocates the frame with alloca; here js_call_internal takes it from its own
#         frame with `memory.frame`, sized to the function (`frame_values`), up to
#         js_call_inline_frame_values values; a larger one is js_malloc'ed and released
#         when the call returns.
#
#==============================================================================================

## The most values a bytecode call takes from its own frame (`memory.frame`, at most 4096
## bytes) before it allocates the locals, stack and variable references on the heap (C
## uses alloca for all of them). A call takes only the values its function needs.
let js_call_inline_frame_values: usize = 16

# mark: JS_CallInternal =========================================================================

# argv[] is modified if (flags & JS_CALL_FLAG_COPY_ARGV) = 0.
## Call `func_obj` with `this_obj`, `new_target` and the `argc` arguments at `argv`: a
## bytecode function runs in the interpreter, another object through its class's call
## hook; with call_flag_generator, `func_obj` holds the AsyncFunctionState of a
## suspended generator frame, which is resumed (JS_CallInternal).
func js_call_internal(caller_ctx: Context*, func_obj: Value, this_obj: Value, new_target: Value, argc: i32, argv: Value*, flags: i32) -> Value!:
    # A C function (or another class's call hook) is called before a frame is set up, as C
    # does; interp_enter takes the other cases (and the interrupt handler's call).
    if func_obj.tag == Tag.object and func_obj.u.obj.class_id != ClassId.bytecode_function and caller_ctx.interrupt_counter > 1:
        if let class_array = caller_ctx.rt.class_array:
            if let call_func = class_array[(usize)(u16)func_obj.u.obj.class_id].call:
                caller_ctx.interrupt_counter -%= 1
                return try call_func(caller_ctx, func_obj, this_obj, argc, argv, flags)
    var st: InterpState = ---
    var sf_s: StackFrame = ---
    # the frame when it fits, sized to the function, as C allocates it with alloca
    let frame_space = memory.frame[Value](frame_values(func_obj, argc, flags), js_call_inline_frame_values)
    st.sf = &sf_s
    st.caller_ctx = caller_ctx
    st.func_obj = func_obj
    st.this_obj = this_obj
    st.new_target = new_target
    st.argc = argc
    st.argv = argv
    let entry = interp_enter(&st, flags, frame_space.data)
    if entry == InterpEntry.returned:
        return st.ret_val
    if entry == InterpEntry.failed:
        error(st.failure_code, st.failure_message)
    # set when the frame starts at the exception handler
    var throwing = entry == InterpEntry.throwing
    var exit = FrameExit.exception

    # The frame's constants, which the fast paths read where the register allocator puts
    # them (C's locals of JS_CallInternal).
    let ctx = st.ctx
    let var_buf = st.var_buf
    let arg_buf = st.arg_buf
    # the closure variables, read as C reads them (see var_ref_at)
    let var_refs = *(VarRef***)&st.var_refs
    # one address for the countdown of js_poll_interrupts, which the jumps take
    let interrupt_counter = &ctx.interrupt_counter
    # the `this` and new.target of plain calls: one slot rather than one per argument
    let undefined_value = js_undefined()
    let sf = st.sf

    # restart:
    frame: while true:
        if not throwing:
            # C's pc and sp, which st holds while an outlined body runs
            var pc = st.pc
            var sp = st.sp
            dispatch: while true:
                let op = (Op)*pc
                pc += 1
                # The fast paths: the opcodes C's loop runs most, on `pc`, `sp` and the
                # frame's constants, with values written and counted in place (see the
                # header: no Value locals and no helper calls, which would grow the
                # frame). JS_FreeValue's last release calls free_counted. An arm that
                # finishes leaves with `continue dispatch`; one that meets a case it does
                # not handle has changed nothing and falls through to the outlined body
                # below, which runs the opcode from its start (C's `*_slow_path` labels and
                # the slow calls of its macros).
                match op:
                    .push_i32:
                        sp.u.uint64 = (u64)(u32)((i32)((const Operand32*)pc).v)
                        sp.tag = Tag.int
                        sp += 1
                        pc += 4
                        continue dispatch
                    # SHORT_OPCODES
                    .push_minus1, .push_0, .push_1, .push_2, .push_3, .push_4, .push_5, .push_6, .push_7:
                        sp.u.uint64 = (u64)(u32)((i32)(u8)op - (i32)(u8)Op.push_0)
                        sp.tag = Tag.int
                        sp += 1
                        continue dispatch
                    .push_i8:
                        sp.u.uint64 = (u64)(u32)((i32)(i8)*pc)
                        sp.tag = Tag.int
                        sp += 1
                        pc += 1
                        continue dispatch
                    .push_i16:
                        sp.u.uint64 = (u64)(u32)((i16)((const Operand16*)pc).v)
                        sp.tag = Tag.int
                        sp += 1
                        pc += 2
                        continue dispatch
                    # end SHORT_OPCODES
                    .undefined:
                        sp.u.uint64 = 0
                        sp.tag = Tag.undefined
                        sp += 1
                        continue dispatch
                    .null:
                        sp.u.uint64 = 0
                        sp.tag = Tag.null
                        sp += 1
                        continue dispatch
                    .push_false:
                        sp.u.uint64 = 0
                        sp.tag = Tag.boolean
                        sp += 1
                        continue dispatch
                    .push_true:
                        sp.u.uint64 = 1
                        sp.tag = Tag.boolean
                        sp += 1
                        continue dispatch
                    .drop:
                        sp -= 1
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) -%= 1
                            if *(i32*)((usize)sp.u.uint64 -% 4) <= 0:
                                free_counted(ctx, sp.tag, sp.u.uint64)
                        continue dispatch
                    .nip:
                        if (i64)sp[-2].tag < 0:
                            *(i32*)((usize)sp[-2].u.uint64 -% 4) -%= 1
                            if *(i32*)((usize)sp[-2].u.uint64 -% 4) <= 0:
                                free_counted(ctx, sp[-2].tag, sp[-2].u.uint64)
                        sp[-2] = sp[-1]
                        sp -= 1
                        continue dispatch
                    .dup:
                        *sp = sp[-1]
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        continue dispatch
                    .swap:  # a b -> b a
                        let tag = sp[-2].tag
                        let bits = sp[-2].u.uint64
                        sp[-2] = sp[-1]
                        sp[-1].u.uint64 = bits
                        sp[-1].tag = tag
                        continue dispatch
                    .return_:
                        sp -= 1
                        st.ret_val = *sp
                        st.pc = pc
                        st.sp = sp
                        exit = FrameExit.done
                        break frame
                    .return_undef:
                        st.ret_val.u.uint64 = 0
                        st.ret_val.tag = Tag.undefined
                        st.pc = pc
                        st.sp = sp
                        exit = FrameExit.done
                        break frame
                    # an object `this` (the outlined body converts the others)
                    .push_this:
                        if st.this_obj.tag == Tag.object:
                            *sp = st.this_obj
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                            sp += 1
                            continue dispatch
                    .nop:
                        continue dispatch

                    # Calls, from here as C makes them: one level of JavaScript recursion is
                    # this frame alone. The stack is in st for the callee's backtrace and for
                    # the unwinding of a failure; interp_call_done releases the function and
                    # the arguments and pushes the result.
                    # call0 to call3 are SHORT_OPCODES (C: goto has_call_argc)
                    .call0, .call1, .call2, .call3, .call:
                        var call_argc = (i32)(u8)op - (i32)(u8)Op.call0
                        if op == Op.call:
                            call_argc = (i32)((const Operand16*)pc).v
                            pc += 2
                        let call_argv = sp - (usize)call_argc
                        st.pc = pc
                        st.sp = sp
                        sf.cur_pc = pc
                        st.ret_val = js_call_internal(ctx, call_argv[-1], undefined_value, undefined_value, call_argc, call_argv, 0) catch: break dispatch
                        interp_call_done(&st, call_argc, 1)
                        sp = st.sp
                        continue dispatch
                    .call_method:
                        let call_argc = (i32)((const Operand16*)pc).v
                        pc += 2
                        let call_argv = sp - (usize)call_argc
                        st.pc = pc
                        st.sp = sp
                        sf.cur_pc = pc
                        st.ret_val = js_call_internal(ctx, call_argv[-1], call_argv[-2], undefined_value, call_argc, call_argv, 0) catch: break dispatch
                        interp_call_done(&st, call_argc, 2)
                        sp = st.sp
                        continue dispatch

                    # Locals, arguments and closure variables. `put` moves the top of the
                    # stack into the variable, `set` stores a new reference and keeps it;
                    # the old value is released after the store (set_value).
                    .get_loc:
                        *sp = var_buf[(usize)((const Operand16*)pc).v]
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        pc += 2
                        continue dispatch
                    .put_loc:
                        let pv = var_buf + (usize)((const Operand16*)pc).v
                        pc += 2
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .set_loc:
                        let pv = var_buf + (usize)((const Operand16*)pc).v
                        pc += 2
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        *pv = sp[-1]
                        if (i64)pv.tag < 0:
                            *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .get_arg:
                        *sp = arg_buf[(usize)((const Operand16*)pc).v]
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        pc += 2
                        continue dispatch
                    .put_arg:
                        let pv = arg_buf + (usize)((const Operand16*)pc).v
                        pc += 2
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .set_arg:
                        let pv = arg_buf + (usize)((const Operand16*)pc).v
                        pc += 2
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        *pv = sp[-1]
                        if (i64)pv.tag < 0:
                            *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    # SHORT_OPCODES
                    .get_loc8:
                        *sp = var_buf[(usize)*pc]
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        pc += 1
                        continue dispatch
                    .put_loc8:
                        let pv = var_buf + (usize)*pc
                        pc += 1
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .set_loc8:
                        let pv = var_buf + (usize)*pc
                        pc += 1
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        *pv = sp[-1]
                        if (i64)pv.tag < 0:
                            *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .get_loc0, .get_loc1, .get_loc2, .get_loc3:
                        *sp = var_buf[(usize)((u8)op - (u8)Op.get_loc0)]
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        continue dispatch
                    .put_loc0, .put_loc1, .put_loc2, .put_loc3:
                        let pv = var_buf + (usize)((u8)op - (u8)Op.put_loc0)
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .set_loc0, .set_loc1, .set_loc2, .set_loc3:
                        let pv = var_buf + (usize)((u8)op - (u8)Op.set_loc0)
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        *pv = sp[-1]
                        if (i64)pv.tag < 0:
                            *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .get_arg0, .get_arg1, .get_arg2, .get_arg3:
                        *sp = arg_buf[(usize)((u8)op - (u8)Op.get_arg0)]
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        continue dispatch
                    .put_arg0, .put_arg1, .put_arg2, .put_arg3:
                        let pv = arg_buf + (usize)((u8)op - (u8)Op.put_arg0)
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .set_arg0, .set_arg1, .set_arg2, .set_arg3:
                        let pv = arg_buf + (usize)((u8)op - (u8)Op.set_arg0)
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        *pv = sp[-1]
                        if (i64)pv.tag < 0:
                            *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .get_var_ref0, .get_var_ref1, .get_var_ref2, .get_var_ref3:
                        *sp = *var_refs[(usize)((u8)op - (u8)Op.get_var_ref0)].pvalue
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        continue dispatch
                    # end SHORT_OPCODES
                    .get_var_ref:
                        *sp = *var_refs[(usize)((const Operand16*)pc).v].pvalue
                        if (i64)sp.tag < 0:
                            *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                        sp += 1
                        pc += 2
                        continue dispatch
                    .put_var_ref:
                        let pv = var_refs[(usize)((const Operand16*)pc).v].pvalue
                        pc += 2
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .put_var_ref0, .put_var_ref1, .put_var_ref2, .put_var_ref3:
                        let pv = var_refs[(usize)((u8)op - (u8)Op.put_var_ref0)].pvalue
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        sp -= 1
                        *pv = *sp
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .set_var_ref0, .set_var_ref1, .set_var_ref2, .set_var_ref3, .set_var_ref:
                        var idx: usize = 0
                        if op == Op.set_var_ref:
                            idx = (usize)((const Operand16*)pc).v
                            pc += 2
                        else:
                            idx = (usize)((u8)op - (u8)Op.set_var_ref0)
                        let pv = var_refs[idx].pvalue
                        let old_tag = pv.tag
                        let old_bits = pv.u.uint64
                        *pv = sp[-1]
                        if (i64)pv.tag < 0:
                            *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                        if (i64)old_tag < 0:
                            *(i32*)((usize)old_bits -% 4) -%= 1
                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                free_counted(ctx, old_tag, old_bits)
                        continue dispatch
                    .get_var_ref_check:
                        # an uninitialized variable: the outlined body throws
                        let pv = var_refs[(usize)((const Operand16*)pc).v].pvalue
                        if pv.tag != Tag.uninitialized:
                            *sp = *pv
                            if (i64)sp.tag < 0:
                                *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                            sp += 1
                            pc += 2
                            continue dispatch
                    .get_loc_check:
                        let pv = var_buf + (usize)((const Operand16*)pc).v
                        if pv.tag != Tag.uninitialized:
                            *sp = *pv
                            if (i64)sp.tag < 0:
                                *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                            sp += 1
                            pc += 2
                            continue dispatch
                    # Global variables (their references): an initialized one, and for a
                    # write one that is not const; the rest (the global object's
                    # properties, the errors) is the outlined body's.
                    .get_var_undef, .get_var:
                        let pv = var_refs[(usize)((const Operand16*)pc).v].pvalue
                        if pv.tag != Tag.uninitialized:
                            *sp = *pv
                            if (i64)sp.tag < 0:
                                *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                            sp += 1
                            pc += 2
                            continue dispatch
                    .put_var, .put_var_init:
                        let var_ref = var_refs[(usize)((const Operand16*)pc).v]
                        let pv = var_ref.pvalue
                        if pv.tag != Tag.uninitialized and var_ref.is_const == 0:
                            pc += 2
                            let old_tag = pv.tag
                            let old_bits = pv.u.uint64
                            sp -= 1
                            *pv = *sp
                            if (i64)old_tag < 0:
                                *(i32*)((usize)old_bits -% 4) -%= 1
                                if *(i32*)((usize)old_bits -% 4) <= 0:
                                    free_counted(ctx, old_tag, old_bits)
                            continue dispatch

                    # Jumps. js_poll_interrupts counts down to the interrupt handler's call;
                    # while that call is more than one step away the count is taken here,
                    # and the step that reaches it runs the outlined body.
                    .goto_:
                        if *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            pc += (isize)(i32)((const Operand32*)pc).v
                            continue dispatch
                    # SHORT_OPCODES
                    .goto16:
                        if *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            pc += (isize)(i16)((const Operand16*)pc).v
                            continue dispatch
                    .goto8:
                        if *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            pc += (isize)(i8)*pc
                            continue dispatch
                    # end SHORT_OPCODES
                    # An int, bool, null or undefined condition is its payload (C's quick and
                    # dirty test); another value is converted by the outlined body.
                    .if_true:
                        if (u64)sp[-1].tag <= (u64)Tag.undefined and *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            sp -= 1
                            if (i32)sp.u.uint64 != 0:
                                pc += (isize)(i32)((const Operand32*)pc).v
                            else:
                                pc += 4
                            continue dispatch
                    .if_false:
                        if (u64)sp[-1].tag <= (u64)Tag.undefined and *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            sp -= 1
                            if (i32)sp.u.uint64 == 0:
                                pc += (isize)(i32)((const Operand32*)pc).v
                            else:
                                pc += 4
                            continue dispatch
                    # SHORT_OPCODES
                    .if_true8:
                        if (u64)sp[-1].tag <= (u64)Tag.undefined and *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            sp -= 1
                            if (i32)sp.u.uint64 != 0:
                                pc += (isize)(i8)*pc
                            else:
                                pc += 1
                            continue dispatch
                    .if_false8:
                        if (u64)sp[-1].tag <= (u64)Tag.undefined and *interrupt_counter > 1:
                            *interrupt_counter -= 1
                            sp -= 1
                            if (i32)sp.u.uint64 == 0:
                                pc += (isize)(i8)*pc
                            else:
                                pc += 1
                            continue dispatch
                    # end SHORT_OPCODES
                    .lnot:
                        if (u64)sp[-1].tag <= (u64)Tag.undefined:
                            sp[-1].u.uint64 = (u64)((i32)sp[-1].u.uint64 == 0)
                            sp[-1].tag = Tag.boolean
                            continue dispatch

                    # Arithmetic, comparisons and bitwise operators on two ints, and (for
                    # add, sub, mul and the comparisons, as in C) on numbers of which one is a
                    # float: the float result of two ints outside the int range (or -0) is
                    # the same exact value rounded once. Other operands take the outlined
                    # body.
                    .add:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            let r = (i64)(i32)sp[-2].u.uint64 + (i64)(i32)sp[-1].u.uint64
                            if r == (i64)(i32)r:
                                sp[-2].u.uint64 = (u64)(u32)((i32)r)
                                sp[-2].tag = Tag.int
                                sp -= 1
                                continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.float64 = d1 + d2
                            sp[-2].tag = Tag.float64
                            sp -= 1
                            continue dispatch
                    .add_loc:
                        let pv = var_buf + (usize)*pc
                        if ((i64)pv.tag | (i64)sp[-1].tag) == 0:
                            let r = (i64)(i32)pv.u.uint64 + (i64)(i32)sp[-1].u.uint64
                            if r == (i64)(i32)r:
                                pv.u.uint64 = (u64)(u32)((i32)r)
                                pv.tag = Tag.int
                                sp -= 1
                                pc += 1
                                continue dispatch
                        elif pv.tag == Tag.float64 and sp[-1].tag == Tag.float64:
                            pv.u.float64 = pv.u.float64 + sp[-1].u.float64
                            sp -= 1
                            pc += 1
                            continue dispatch
                    .sub:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            let r = (i64)(i32)sp[-2].u.uint64 - (i64)(i32)sp[-1].u.uint64
                            if r == (i64)(i32)r:
                                sp[-2].u.uint64 = (u64)(u32)((i32)r)
                                sp[-2].tag = Tag.int
                                sp -= 1
                                continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.float64 = d1 - d2
                            sp[-2].tag = Tag.float64
                            sp -= 1
                            continue dispatch
                    .mul:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            let v1 = (i32)sp[-2].u.uint64
                            let v2 = (i32)sp[-1].u.uint64
                            let r = (i64)v1 * (i64)v2
                            # a zero from a negative operand is -0, a float
                            if r == (i64)(i32)r and (r != 0 or (v1 | v2) >= 0):
                                sp[-2].u.uint64 = (u64)(u32)((i32)r)
                                sp[-2].tag = Tag.int
                                sp -= 1
                                continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.float64 = d1 * d2
                            sp[-2].tag = Tag.float64
                            sp -= 1
                            continue dispatch
                    .mod:
                        # v2 = 0, v1 = INT32_MIN with v2 = -1 and the results that are -0 take
                        # the slow path
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0 and (i32)sp[-2].u.uint64 >= 0 and (i32)sp[-1].u.uint64 > 0:
                            sp[-2].u.uint64 = (u64)(u32)((i32)sp[-2].u.uint64 % (i32)sp[-1].u.uint64)
                            sp -= 1
                            continue dispatch
                    .inc:
                        if sp[-1].tag == Tag.int and (i32)sp[-1].u.uint64 != 2147483647:
                            sp[-1].u.uint64 = (u64)(u32)((i32)sp[-1].u.uint64 + 1)
                            sp[-1].tag = Tag.int
                            continue dispatch
                    .dec:
                        if sp[-1].tag == Tag.int and (i32)sp[-1].u.uint64 != -2147483648:
                            sp[-1].u.uint64 = (u64)(u32)((i32)sp[-1].u.uint64 - 1)
                            sp[-1].tag = Tag.int
                            continue dispatch
                    .post_inc:
                        if sp[-1].tag == Tag.int and (i32)sp[-1].u.uint64 != 2147483647:
                            sp.u.uint64 = (u64)(u32)((i32)sp[-1].u.uint64 + 1)
                            sp.tag = Tag.int
                            sp += 1
                            continue dispatch
                    .post_dec:
                        if sp[-1].tag == Tag.int and (i32)sp[-1].u.uint64 != -2147483648:
                            sp.u.uint64 = (u64)(u32)((i32)sp[-1].u.uint64 - 1)
                            sp.tag = Tag.int
                            sp += 1
                            continue dispatch
                    .inc_loc:
                        let pv = var_buf + (usize)*pc
                        if pv.tag == Tag.int and (i32)pv.u.uint64 != 2147483647:
                            pv.u.uint64 = (u64)(u32)((i32)pv.u.uint64 + 1)
                            pv.tag = Tag.int
                            pc += 1
                            continue dispatch
                    .dec_loc:
                        let pv = var_buf + (usize)*pc
                        if pv.tag == Tag.int and (i32)pv.u.uint64 != -2147483648:
                            pv.u.uint64 = (u64)(u32)((i32)pv.u.uint64 - 1)
                            pv.tag = Tag.int
                            pc += 1
                            continue dispatch
                    .shl:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            let v2 = (u32)(i32)sp[-1].u.uint64 & 0x1f
                            sp[-2].u.uint64 = (u64)(u32)((i32)((u32)(i32)sp[-2].u.uint64 << v2))
                            sp[-2].tag = Tag.int
                            sp -= 1
                            continue dispatch
                    .sar:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            let v2 = (u32)(i32)sp[-1].u.uint64 & 0x1f
                            sp[-2].u.uint64 = (u64)(u32)((i32)sp[-2].u.uint64 >> v2)
                            sp[-2].tag = Tag.int
                            sp -= 1
                            continue dispatch
                    .and_:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)(u32)((i32)sp[-2].u.uint64 & (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.int
                            sp -= 1
                            continue dispatch
                    .or_:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)(u32)((i32)sp[-2].u.uint64 | (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.int
                            sp -= 1
                            continue dispatch
                    .xor:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)(u32)((i32)sp[-2].u.uint64 ^ (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.int
                            sp -= 1
                            continue dispatch
                    .lt:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)((i32)sp[-2].u.uint64 < (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.uint64 = (u64)(d1 < d2)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                    .lte:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)((i32)sp[-2].u.uint64 <= (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.uint64 = (u64)(d1 <= d2)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                    .gt:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)((i32)sp[-2].u.uint64 > (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.uint64 = (u64)(d1 > d2)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                    .gte:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)((i32)sp[-2].u.uint64 >= (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.uint64 = (u64)(d1 >= d2)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                    .eq, .strict_eq:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)((i32)sp[-2].u.uint64 == (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.uint64 = (u64)(d1 == d2)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                    .neq, .strict_neq:
                        if ((i64)sp[-2].tag | (i64)sp[-1].tag) == 0:
                            sp[-2].u.uint64 = (u64)((i32)sp[-2].u.uint64 != (i32)sp[-1].u.uint64)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch
                        if (sp[-2].tag == Tag.float64 or sp[-2].tag == Tag.int) and (sp[-1].tag == Tag.float64 or sp[-1].tag == Tag.int):
                            let d1 = sp[-2].u.float64 if sp[-2].tag == Tag.float64 else f64((i32)sp[-2].u.uint64)
                            let d2 = sp[-1].u.float64 if sp[-1].tag == Tag.float64 else f64((i32)sp[-1].u.uint64)
                            sp[-2].u.uint64 = (u64)(d1 != d2)
                            sp[-2].tag = Tag.boolean
                            sp -= 1
                            continue dispatch

                    # Named properties (GET_FIELD_INLINE, OP_put_field): a data property of a
                    # plain object, own or (for reads) inherited, found by find_own_property's
                    # hash-chain walk written out here as C inlines it. An accessor, an exotic
                    # object on the way, a read-only or missing property (for writes) and a
                    # primitive take the opcode's slow path (interp_get_field_slow,
                    # interp_put_field_slow).
                    .get_field, .get_field2, .get_length:
                        let atom = atom_length if op == Op.get_length else ((const Operand32*)pc).v
                        # the exotic object the walk stops at, where the slow path starts
                        var exotic: Object*? = none
                        if sp[-1].tag == Tag.object:
                            var p = sp[-1].u.obj
                            search: while true:
                                let sh = p.shape
                                let hash_table = (usize)sh +% memory.size_of(Shape)
                                var h = *(u32*)(hash_table +% (usize)(atom & sh.prop_hash_mask) *% 4)
                                let prop_base = hash_table +% (usize)sh.prop_hash_mask *% 4 +% 4 -% memory.size_of(ShapeProperty)
                                while h != 0:
                                    let prs = (ShapeProperty*)(prop_base +% (usize)h *% memory.size_of(ShapeProperty))
                                    if prs.atom == atom:
                                        if ((i32)prs.flags & prop_tmask) != 0:
                                            break search
                                        let pv = &(p.prop + (usize)(h -% 1)).u.value
                                        if op == Op.get_field2:
                                            *sp = *pv
                                            if (i64)sp.tag < 0:
                                                *(i32*)((usize)sp.u.uint64 -% 4) +%= 1
                                            sp += 1
                                        else:
                                            let obj_bits = sp[-1].u.uint64
                                            sp[-1] = *pv
                                            if (i64)sp[-1].tag < 0:
                                                *(i32*)((usize)sp[-1].u.uint64 -% 4) +%= 1
                                            *(i32*)((usize)obj_bits -% 4) -%= 1
                                            if *(i32*)((usize)obj_bits -% 4) <= 0:
                                                free_counted(ctx, Tag.object, obj_bits)
                                        if op != Op.get_length:
                                            pc += 4
                                        continue dispatch
                                    h = prs.hash_next
                                if p.is_exotic:
                                    exotic = p
                                    break search
                                if let proto = sh.proto:
                                    p = proto
                                else:
                                    if op == Op.get_field2:
                                        sp.u.uint64 = 0
                                        sp.tag = Tag.undefined
                                        sp += 1
                                    else:
                                        let obj_bits = sp[-1].u.uint64
                                        sp[-1].u.uint64 = 0
                                        sp[-1].tag = Tag.undefined
                                        *(i32*)((usize)obj_bits -% 4) -%= 1
                                        if *(i32*)((usize)obj_bits -% 4) <= 0:
                                            free_counted(ctx, Tag.object, obj_bits)
                                    if op != Op.get_length:
                                        pc += 4
                                    continue dispatch
                        # the slow path, without the outlined body's second walk
                        st.pc = pc if op == Op.get_length else pc + 4
                        st.sp = sp
                        st.opcode = op
                        if not interp_get_field_slow(&st, exotic, atom):
                            break dispatch
                        pc = st.pc
                        sp = st.sp
                        continue dispatch
                    .put_field:
                        let atom = ((const Operand32*)pc).v
                        if sp[-2].tag == Tag.object:
                            let p = sp[-2].u.obj
                            let sh = p.shape
                            let hash_table = (usize)sh +% memory.size_of(Shape)
                            var h = *(u32*)(hash_table +% (usize)(atom & sh.prop_hash_mask) *% 4)
                            let prop_base = hash_table +% (usize)sh.prop_hash_mask *% 4 +% 4 -% memory.size_of(ShapeProperty)
                            while h != 0:
                                let prs = (ShapeProperty*)(prop_base +% (usize)h *% memory.size_of(ShapeProperty))
                                if prs.atom == atom:
                                    if ((i32)prs.flags & (prop_tmask | prop_writable | prop_length)) == prop_writable:
                                        # set_value, then the object's release
                                        let pv = &(p.prop + (usize)(h -% 1)).u.value
                                        let old_tag = pv.tag
                                        let old_bits = pv.u.uint64
                                        *pv = sp[-1]
                                        if (i64)old_tag < 0:
                                            *(i32*)((usize)old_bits -% 4) -%= 1
                                            if *(i32*)((usize)old_bits -% 4) <= 0:
                                                free_counted(ctx, old_tag, old_bits)
                                        *(i32*)((usize)sp[-2].u.uint64 -% 4) -%= 1
                                        if *(i32*)((usize)sp[-2].u.uint64 -% 4) <= 0:
                                            free_counted(ctx, Tag.object, sp[-2].u.uint64)
                                        sp -= 2
                                        pc += 4
                                        continue dispatch
                                    h = 0
                                else:
                                    h = prs.hash_next
                        # put_field_slow_path, without the outlined body's second search
                        st.pc = pc + 4
                        st.sp = sp
                        if not interp_put_field_slow(&st, atom):
                            break dispatch
                        pc = st.pc
                        sp = st.sp
                        continue dispatch

                    # Elements of fast arrays at an int index inside them
                    # (GET_ARRAY_EL_INLINE, OP_get_array_el3, OP_put_array_el).
                    .get_array_el:
                        if sp[-2].tag == Tag.object and sp[-1].tag == Tag.int:
                            let p = sp[-2].u.obj
                            let idx = (u32)(i32)sp[-1].u.uint64
                            if p.class_id == ClassId.array and idx < p.u.array.count:
                                # the element's new reference is taken before the array's
                                # is released
                                let obj_bits = sp[-2].u.uint64
                                sp[-2] = p.u.array.u.values[(usize)idx]
                                if (i64)sp[-2].tag < 0:
                                    *(i32*)((usize)sp[-2].u.uint64 -% 4) +%= 1
                                *(i32*)((usize)obj_bits -% 4) -%= 1
                                if *(i32*)((usize)obj_bits -% 4) <= 0:
                                    free_counted(ctx, Tag.object, obj_bits)
                                sp -= 1
                                continue dispatch
                    # get_array_el2 keeps the array and replaces the index, get_array_el3
                    # keeps both and pushes the element
                    .get_array_el2, .get_array_el3:
                        if sp[-2].tag == Tag.object and sp[-1].tag == Tag.int:
                            let p = sp[-2].u.obj
                            let idx = (u32)(i32)sp[-1].u.uint64
                            if p.class_id == ClassId.array and idx < p.u.array.count:
                                let pv = sp - 1 if op == Op.get_array_el2 else sp
                                *pv = p.u.array.u.values[(usize)idx]
                                if (i64)pv.tag < 0:
                                    *(i32*)((usize)pv.u.uint64 -% 4) +%= 1
                                if op == Op.get_array_el3:
                                    sp += 1
                                continue dispatch
                    .put_array_el:
                        if sp[-3].tag == Tag.object and sp[-2].tag == Tag.int:
                            let p = sp[-3].u.obj
                            let idx = (u32)(i32)sp[-2].u.uint64
                            if p.class_id == ClassId.array and idx < p.u.array.count:
                                let pv = p.u.array.u.values + (usize)idx
                                let old_tag = pv.tag
                                let old_bits = pv.u.uint64
                                *pv = sp[-1]
                                if (i64)old_tag < 0:
                                    *(i32*)((usize)old_bits -% 4) -%= 1
                                    if *(i32*)((usize)old_bits -% 4) <= 0:
                                        free_counted(ctx, old_tag, old_bits)
                                *(i32*)((usize)sp[-3].u.uint64 -% 4) -%= 1
                                if *(i32*)((usize)sp[-3].u.uint64 -% 4) <= 0:
                                    free_counted(ctx, Tag.object, sp[-3].u.uint64)
                                sp -= 3
                                continue dispatch
                    _:
                        _ = 0

                # Every other opcode, and the cases the fast paths leave: the outlined bodies
                # work on `st`, which holds pc and sp while they run.
                st.pc = pc
                st.sp = sp
                st.opcode = op
                match interp_outlined(&st):
                    .next:
                        pc = st.pc
                        sp = st.sp
                    .exception:
                        break dispatch
                    .done:
                        exit = FrameExit.done
                        break frame
                    .done_generator:
                        exit = FrameExit.done_generator
                        break frame

        # exception:
        throwing = false
        if interp_unwind(&st):
            continue frame
        exit = FrameExit.exception
        break frame

    interp_leave(&st, exit)
    if exit == FrameExit.exception:
        error(exception, "")
    return st.ret_val