Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.0

0.6.0 / src / engine / resolve_labels_peephole.lucb

#==============================================================================================
#
#   resolve_labels_peephole - The peephole optimizations of phase 3
#
#   DESCRIPTION:
#       Port of the cases of quickjs.c resolve_labels' switch (lines 35080-35532) that
#       rewrite constants, variable accesses, increments, property stores and typeof
#       tests into shorter or fused opcodes (OPTIMIZE and SHORT_OPCODES are on in
#       QuickJS). Each case answers the LabelStep C's case jumps to; resolve_labels.lucb
#       holds the loop, the jumps and the labels.
#
#==============================================================================================

# mark: Constants ===============================================================================

# null (after its own tests), push_false, push_true: a constant boolean tested by
# if_false/if_true.
func peephole_push_bool(p: ResolveLabelsState*) -> LabelStep:
    p.val = (i32)(p.op == (i32)Op.push_true)
    if code_match(&p.cc, p.pos_next, [ops2(Op.if_false, Op.if_true), -1]):
        return LabelStep.has_constant_test
    return LabelStep.no_change

# null: fused null tests.
func peephole_null(p: ResolveLabelsState*) -> LabelStep:
    # transform null strict_eq into is_null
    if code_match(&p.cc, p.pos_next, [(i32)Op.strict_eq, -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.is_null)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # transform null strict_neq if_false/if_true -> is_null if_true/if_false
    if code_match(&p.cc, p.pos_next,
                  [(i32)Op.strict_neq, ops2(Op.if_false, Op.if_true), -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.is_null)
        p.pos_next = p.cc.pos
        p.label = p.cc.label
        p.op = p.cc.op ^ (i32)Op.if_false ^ (i32)Op.if_true
        return LabelStep.has_label
    return peephole_push_bool(p)

# push_i32: negated constants, dropped constants, constant tests and the short forms.
func peephole_push_i32(p: ResolveLabelsState*) -> LabelStep:
    # transform i32(val) neg -> i32(-val)
    let val = cutils.get_i32(p.bc_buf + p.pos + 1)
    p.val = val
    if ((val != -2147483648 and val != 0) and code_match(&p.cc, p.pos_next, [(i32)Op.neg, -1])):
        state_match_line(p)
        if code_match(&p.cc, p.cc.pos, [(i32)Op.drop, -1]):
            state_match_line(p)
        else:
            state_pc2line(p)
            push_short_int(&p.bc_out, -val)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # remove push/drop pairs generated by the parser
    if code_match(&p.cc, p.pos_next, [(i32)Op.drop, -1]):
        state_match_line(p)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # Optimize constant tests: `if (0)`, `if (1)`, `if (!0)`...
    if code_match(&p.cc, p.pos_next, [ops2(Op.if_false, Op.if_true), -1]):
        p.val = (i32)(val != 0)
        return LabelStep.has_constant_test
    state_pc2line(p)
    push_short_int(&p.bc_out, val)
    return LabelStep.done

# push_bigint_i32: negated constants.
func peephole_push_bigint_i32(p: ResolveLabelsState*) -> LabelStep:
    # transform i32(val) neg -> i32(-val)
    let val = cutils.get_i32(p.bc_buf + p.pos + 1)
    if val != -2147483648 and code_match(&p.cc, p.pos_next, [(i32)Op.neg, -1]):
        state_match_line(p)
        if code_match(&p.cc, p.cc.pos, [(i32)Op.drop, -1]):
            state_match_line(p)
        else:
            state_pc2line(p)
            dbuf_op(&p.bc_out, Op.push_bigint_i32)
            dbuf_u32(&p.bc_out, (u32)-val)
        p.pos_next = p.cc.pos
        return LabelStep.done
    return LabelStep.no_change

# push_atom_value: dropped atoms and the empty string.
func peephole_push_atom_value(p: ResolveLabelsState*) -> LabelStep:
    let atom = cutils.get_u32(p.bc_buf + p.pos + 1)
    # remove push/drop pairs generated by the parser
    if code_match(&p.cc, p.pos_next, [(i32)Op.drop, -1]):
        js_free_atom(p.ctx, atom)
        state_match_line(p)
        p.pos_next = p.cc.pos
        return LabelStep.done
    if atom == atom_empty_string:
        js_free_atom(p.ctx, atom)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.push_empty_string)
        return LabelStep.done
    return LabelStep.no_change

# undefined: dropped, returned, tested and compared undefined.
func peephole_undefined(p: ResolveLabelsState*) -> LabelStep:
    # remove push/drop pairs generated by the parser
    if code_match(&p.cc, p.pos_next, [(i32)Op.drop, -1]):
        state_match_line(p)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # transform undefined return -> return_undefined
    if code_match(&p.cc, p.pos_next, [(i32)Op.return_, -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.return_undef)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # transform undefined if_true(l1)/if_false(l1) -> nop/goto(l1)
    if code_match(&p.cc, p.pos_next, [ops2(Op.if_false, Op.if_true), -1]):
        p.val = 0
        return LabelStep.has_constant_test
    # transform undefined strict_eq -> is_undefined
    if code_match(&p.cc, p.pos_next, [(i32)Op.strict_eq, -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.is_undefined)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # transform undefined strict_neq if_false/if_true -> is_undefined if_true/if_false
    if code_match(&p.cc, p.pos_next,
                  [(i32)Op.strict_neq, ops2(Op.if_false, Op.if_true), -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.is_undefined)
        p.pos_next = p.cc.pos
        p.label = p.cc.label
        p.op = p.cc.op ^ (i32)Op.if_false ^ (i32)Op.if_true
        return LabelStep.has_label
    return LabelStep.no_change

# mark: Variables ===============================================================================

# dup: stores whose value is kept or dropped.
func peephole_dup(p: ResolveLabelsState*) -> LabelStep:
    # Transformation: dup put_x(n) drop -> put_x(n)
    var line2: i32 = -1
    # Transformation: dup put_x(n) -> set_x(n)
    if code_match(&p.cc, p.pos_next,
                  [ops4(Op.put_loc, Op.put_loc_check, Op.put_arg, Op.put_var_ref), -1, -1]):
        state_match_line(p)
        var op1 = p.cc.op + 1  # put_x -> set_x
        p.pos_next = p.cc.pos
        if code_match(&p.cc, p.cc.pos, [(i32)Op.drop, -1]):
            state_match_line(p)
            op1 -= 1  # set_x drop -> put_x
            p.pos_next = p.cc.pos
            if code_match(&p.cc, p.cc.pos, [op1 - 1, p.cc.idx, -1]):
                line2 = p.cc.line_num  # delay line number update
                op1 += 1  # put_x(n) get_x(n) -> set_x(n)
                p.pos_next = p.cc.pos
        state_pc2line(p)
        put_short_code(&p.bc_out, op1, p.cc.idx)
        if line2 >= 0:
            p.line_num = line2
        return LabelStep.done
    return LabelStep.no_change

# Emit add_loc(idx) after the value the matched pattern pushes, and skip the pattern.
func emit_add_loc(p: ResolveLabelsState*, idx: i32):
    dbuf_op(&p.bc_out, Op.add_loc)
    dbuf_byte(&p.bc_out, (u8)idx)
    p.pos_next = p.cc.pos

# get_loc: increments, additions to a local and the short forms.
func peephole_get_loc(p: ResolveLabelsState*) -> LabelStep:
    # transformation:
    # get_loc(n) post_dec put_loc(n) drop -> dec_loc(n)
    # get_loc(n) post_inc put_loc(n) drop -> inc_loc(n)
    # get_loc(n) dec dup put_loc(n) drop -> dec_loc(n)
    # get_loc(n) inc dup put_loc(n) drop -> inc_loc(n)
    let idx = (i32)cutils.get_u16(p.bc_buf + p.pos + 1)
    if idx >= 256:
        return LabelStep.no_change
    if (code_match(&p.cc, p.pos_next,
                   [ops2(Op.post_dec, Op.post_inc), (i32)Op.put_loc, idx, (i32)Op.drop, -1]) or code_match(&p.cc, p.pos_next,
                                                                                                           [ops2(Op.dec, Op.inc), (i32)Op.dup, (i32)Op.put_loc, idx, (i32)Op.drop, -1])):
        state_match_line(p)
        state_pc2line(p)
        if p.cc.op == (i32)Op.inc or p.cc.op == (i32)Op.post_inc:
            dbuf_op(&p.bc_out, Op.inc_loc)
        else:
            dbuf_op(&p.bc_out, Op.dec_loc)
        dbuf_byte(&p.bc_out, (u8)idx)
        p.pos_next = p.cc.pos
        return LabelStep.done
    # transformation:
    # get_loc(n) push_atom_value(x) add dup put_loc(n) drop -> push_atom_value(x) add_loc(n)
    if code_match(&p.cc, p.pos_next,
                  [(i32)Op.push_atom_value, (i32)Op.add, (i32)Op.dup, (i32)Op.put_loc, idx,
                   (i32)Op.drop, -1]):
        state_match_line(p)
        state_pc2line(p)
        if p.cc.atom == atom_empty_string:
            js_free_atom(p.ctx, p.cc.atom)
            dbuf_op(&p.bc_out, Op.push_empty_string)
        else:
            dbuf_op(&p.bc_out, Op.push_atom_value)
            dbuf_u32(&p.bc_out, p.cc.atom)
        emit_add_loc(p, idx)
        return LabelStep.done
    # transformation:
    # get_loc(n) push_i32(x) add dup put_loc(n) drop -> push_i32(x) add_loc(n)
    if code_match(&p.cc, p.pos_next,
                  [(i32)Op.push_i32, (i32)Op.add, (i32)Op.dup, (i32)Op.put_loc, idx,
                   (i32)Op.drop, -1]):
        state_match_line(p)
        state_pc2line(p)
        push_short_int(&p.bc_out, p.cc.label)
        emit_add_loc(p, idx)
        return LabelStep.done
    # transformation: XXX: also do these:
    # get_loc(n) get_loc(x) add dup put_loc(n) drop -> get_loc(x) add_loc(n)
    # get_loc(n) get_arg(x) add dup put_loc(n) drop -> get_arg(x) add_loc(n)
    # get_loc(n) get_var_ref(x) add dup put_loc(n) drop -> get_var_ref(x) add_loc(n)
    if code_match(&p.cc, p.pos_next,
                  [ops3(Op.get_loc, Op.get_arg, Op.get_var_ref), -1, (i32)Op.add, (i32)Op.dup,
                   (i32)Op.put_loc, idx, (i32)Op.drop, -1]):
        state_match_line(p)
        state_pc2line(p)
        put_short_code(&p.bc_out, p.cc.op, p.cc.idx)
        emit_add_loc(p, idx)
        return LabelStep.done
    state_pc2line(p)
    put_short_code(&p.bc_out, p.op, idx)
    return LabelStep.done

# put_loc, put_loc_check, put_arg, put_var_ref: a store followed by a load of the same
# variable, and the short forms.
func peephole_put_var(p: ResolveLabelsState*) -> LabelStep:
    # transformation: put_x(n) get_x(n) -> set_x(n)
    let idx = (i32)cutils.get_u16(p.bc_buf + p.pos + 1)
    if code_match(&p.cc, p.pos_next, [p.op - 1, idx, -1]):
        state_match_line(p)
        state_pc2line(p)
        put_short_code(&p.bc_out, p.op + 1, idx)
        p.pos_next = p.cc.pos
        return LabelStep.done
    state_pc2line(p)
    put_short_code(&p.bc_out, p.op, idx)
    return LabelStep.done

# post_inc, post_dec: increments whose old value is dropped.
func peephole_post_inc(p: ResolveLabelsState*) -> LabelStep:
    # transformation:
    # post_inc put_x drop -> inc put_x
    # post_inc perm3 put_field drop -> inc put_field
    # post_inc perm4 put_array_el drop -> inc put_array_el
    let inc_op = (u8)((i32)Op.dec + (p.op - (i32)Op.post_dec))
    if code_match(&p.cc, p.pos_next,
                  [ops3(Op.put_loc, Op.put_arg, Op.put_var_ref), -1, (i32)Op.drop, -1]):
        state_match_line(p)
        var op1 = p.cc.op
        let idx = p.cc.idx
        p.pos_next = p.cc.pos
        if code_match(&p.cc, p.cc.pos, [op1 - 1, idx, -1]):
            state_match_line(p)
            op1 += 1  # put_x(n) get_x(n) -> set_x(n)
            p.pos_next = p.cc.pos
        state_pc2line(p)
        dbuf_byte(&p.bc_out, inc_op)
        put_short_code(&p.bc_out, op1, idx)
        return LabelStep.done
    if code_match(&p.cc, p.pos_next, [(i32)Op.perm3, (i32)Op.put_field, (i32)Op.drop, -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_byte(&p.bc_out, inc_op)
        dbuf_op(&p.bc_out, Op.put_field)
        dbuf_u32(&p.bc_out, p.cc.atom)
        p.pos_next = p.cc.pos
        return LabelStep.done
    if code_match(&p.cc, p.pos_next, [(i32)Op.perm4, (i32)Op.put_array_el, (i32)Op.drop, -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_byte(&p.bc_out, inc_op)
        dbuf_op(&p.bc_out, Op.put_array_el)
        p.pos_next = p.cc.pos
        return LabelStep.done
    return LabelStep.no_change

# mark: Properties and typeof ===================================================================

# to_propkey: remove redundant to_propkey opcodes when storing simple data
func peephole_to_propkey(p: ResolveLabelsState*) -> LabelStep:
    if (code_match(&p.cc, p.pos_next,
                   [ops3(Op.get_loc, Op.get_arg, Op.get_var_ref), -1, (i32)Op.put_array_el, -1]) or code_match(&p.cc, p.pos_next,
                                                                                                               [ops3(Op.push_i32, Op.push_const, Op.push_atom_value), (i32)Op.put_array_el,
                                                                                                                -1]) or code_match(&p.cc, p.pos_next,
                                                                                                                                   [ops4(Op.undefined, Op.null, Op.push_true, Op.push_false),
                                                                                                                                    (i32)Op.put_array_el, -1])):
        return LabelStep.done
    return LabelStep.no_change

# insert2: insert2 put_field(a) drop -> put_field(a)
func peephole_insert2(p: ResolveLabelsState*) -> LabelStep:
    if code_match(&p.cc, p.pos_next, [(i32)Op.put_field, (i32)Op.drop, -1]):
        state_match_line(p)
        state_pc2line(p)
        dbuf_op(&p.bc_out, Op.put_field)
        dbuf_u32(&p.bc_out, p.cc.atom)
        p.pos_next = p.cc.pos
        return LabelStep.done
    return LabelStep.no_change

# typeof: simplify typeof tests
func peephole_typeof(p: ResolveLabelsState*) -> LabelStep:
    if code_match(&p.cc, p.pos_next,
                  [(i32)Op.push_atom_value,
                   ops4(Op.strict_eq, Op.strict_neq, Op.eq, Op.neq), -1]):
        state_match_line(p)
        var op1 = Op.strict_neq
        if p.cc.op == (i32)Op.strict_eq or p.cc.op == (i32)Op.eq:
            op1 = Op.strict_eq
        var op2: i32 = -1
        if p.cc.atom == atom_undefined:
            op2 = (i32)Op.typeof_is_undefined
        elif p.cc.atom == atom_function:
            op2 = (i32)Op.typeof_is_function
        if op2 >= 0:
            # transform typeof(s) == "<type>" into is_<type>
            if op1 == Op.strict_eq:
                state_pc2line(p)
                dbuf_byte(&p.bc_out, (u8)op2)
                js_free_atom(p.ctx, p.cc.atom)
                p.pos_next = p.cc.pos
                return LabelStep.done
            if op1 == Op.strict_neq and code_match(&p.cc, p.cc.pos, [(i32)Op.if_false, -1]):
                # transform typeof(s) != "<type>" if_false into is_<type> if_true
                state_match_line(p)
                state_pc2line(p)
                dbuf_byte(&p.bc_out, (u8)op2)
                js_free_atom(p.ctx, p.cc.atom)
                p.pos_next = p.cc.pos
                p.label = p.cc.label
                p.op = (i32)Op.if_true
                return LabelStep.has_label
    return LabelStep.no_change

# mark: The other opcodes =======================================================================

# The cases of resolve_labels' switch that are not labels or jumps.
func resolve_labels_peephole(p: ResolveLabelsState*) -> LabelStep:
    match (Op)(u8)p.op:
        .drop:
            # remove useless drops before return
            if code_match(&p.cc, p.pos_next, [(i32)Op.return_undef, -1]):
                state_match_line(p)
                return LabelStep.done
            return LabelStep.no_change
        .null:
            return peephole_null(p)
        .push_false, .push_true:
            return peephole_push_bool(p)
        .push_i32:
            return peephole_push_i32(p)
        .push_bigint_i32:
            return peephole_push_bigint_i32(p)
        .push_const, .fclosure:
            let idx = (i32)cutils.get_u32(p.bc_buf + p.pos + 1)
            if idx < 256:
                state_pc2line(p)
                dbuf_byte(&p.bc_out, (u8)((i32)Op.push_const8 + p.op - (i32)Op.push_const))
                dbuf_byte(&p.bc_out, (u8)idx)
                return LabelStep.done
            return LabelStep.no_change
        .get_field:
            let atom = cutils.get_u32(p.bc_buf + p.pos + 1)
            if atom == atom_length:
                js_free_atom(p.ctx, atom)
                state_pc2line(p)
                dbuf_op(&p.bc_out, Op.get_length)
                return LabelStep.done
            return LabelStep.no_change
        .push_atom_value:
            return peephole_push_atom_value(p)
        .to_propkey:
            return peephole_to_propkey(p)
        .undefined:
            return peephole_undefined(p)
        .insert2:
            return peephole_insert2(p)
        .dup:
            return peephole_dup(p)
        .get_loc:
            return peephole_get_loc(p)
        .get_arg, .get_var_ref:
            let idx = (i32)cutils.get_u16(p.bc_buf + p.pos + 1)
            state_pc2line(p)
            put_short_code(&p.bc_out, p.op, idx)
            return LabelStep.done
        .put_loc, .put_loc_check, .put_arg, .put_var_ref:
            return peephole_put_var(p)
        .post_inc, .post_dec:
            return peephole_post_inc(p)
        .typeof:
            return peephole_typeof(p)
        _:
            return LabelStep.no_change