Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created February 19, 2025 03:04

Revisions

  1. MasterDuke17 created this gist Feb 19, 2025.
    152 changes: 152 additions & 0 deletions MVM_VMArray_at_pos_typed.patch
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,152 @@
    diff --git src/6model/reprconv.c src/6model/reprconv.c
    index f3e04bad7..2b8810eb4 100644
    --- src/6model/reprconv.c
    +++ src/6model/reprconv.c
    @@ -95,8 +95,8 @@ MVM_PUBLIC MVMint64 MVM_repr_exists_pos(MVMThreadContext *tc, MVMObject *obj, MV
    MVMint64 MVM_repr_at_pos_i(MVMThreadContext *tc, MVMObject *obj, MVMint64 idx) {
    MVMRegister value;
    if (REPR(obj)->ID == MVM_REPR_ID_VMArray) {
    - MVM_VMArray_at_pos(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    - idx, &value, MVM_reg_int64);
    + MVM_VMArray_at_pos_i(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    + idx, &value);
    }
    else {
    REPR(obj)->pos_funcs.at_pos(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    @@ -134,8 +134,8 @@ MVMnum64 MVM_repr_at_pos_n(MVMThreadContext *tc, MVMObject *obj, MVMint64 idx) {
    MVMString * MVM_repr_at_pos_s(MVMThreadContext *tc, MVMObject *obj, MVMint64 idx) {
    MVMRegister value;
    if (REPR(obj)->ID == MVM_REPR_ID_VMArray) {
    - MVM_VMArray_at_pos(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    - idx, &value, MVM_reg_str);
    + MVM_VMArray_at_pos_s(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    + idx, &value);
    }
    else {
    REPR(obj)->pos_funcs.at_pos(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    @@ -148,8 +148,8 @@ MVMObject * MVM_repr_at_pos_o(MVMThreadContext *tc, MVMObject *obj, MVMint64 idx
    if (MVM_LIKELY(IS_CONCRETE(obj))) {
    MVMRegister value;
    if (REPR(obj)->ID == MVM_REPR_ID_VMArray) {
    - MVM_VMArray_at_pos(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    - idx, &value, MVM_reg_obj);
    + MVM_VMArray_at_pos_o(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    + idx, &value);
    }
    else if (REPR(obj)->ID == MVM_REPR_ID_P6opaque) {
    MVM_P6opaque_at_pos(tc, STABLE(obj), obj, OBJECT_BODY(obj),
    diff --git src/6model/reprs/VMArray.c src/6model/reprs/VMArray.c
    index 0f38c7c85..e7d87feb2 100644
    --- src/6model/reprs/VMArray.c
    +++ src/6model/reprs/VMArray.c
    @@ -143,6 +143,69 @@ static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *
    return &storage_spec;
    }

    +void MVM_VMArray_at_pos_s(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value) {
    + MVMArrayBody *body = (MVMArrayBody *)data;
    + MVMuint64 real_index;
    +
    + /* Handle negative indexes. */
    + if (index < 0) {
    + index += body->elems;
    + if (index < 0)
    + MVM_exception_throw_adhoc(tc, "MVMArray: Index out of bounds");
    + }
    +
    + real_index = (MVMuint64)index;
    +
    + /* Go by type. */
    + if (real_index >= body->elems)
    + value->s = NULL;
    + else
    + value->s = body->slots.s[body->start + real_index];
    +}
    +
    +void MVM_VMArray_at_pos_i(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value) {
    + MVMArrayBody *body = (MVMArrayBody *)data;
    + MVMuint64 real_index;
    +
    + /* Handle negative indexes. */
    + if (index < 0) {
    + index += body->elems;
    + if (index < 0)
    + MVM_exception_throw_adhoc(tc, "MVMArray: Index out of bounds");
    + }
    +
    + real_index = (MVMuint64)index;
    +
    + /* Go by type. */
    + if (real_index >= body->elems)
    + value->i64 = 0;
    + else
    + value->i64 = (MVMint64)body->slots.i64[body->start + real_index];
    +}
    +
    +void MVM_VMArray_at_pos_o(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value) {
    + MVMArrayBody *body = (MVMArrayBody *)data;
    + MVMuint64 real_index;
    +
    + /* Handle negative indexes. */
    + if (index < 0) {
    + index += body->elems;
    + if (index < 0)
    + MVM_exception_throw_adhoc(tc, "MVMArray: Index out of bounds");
    + }
    +
    + real_index = (MVMuint64)index;
    +
    + /* Go by type. */
    + if (real_index >= body->elems) {
    + value->o = tc->instance->VMNull;
    + }
    + else {
    + MVMObject *found = body->slots.o[body->start + real_index];
    + value->o = found ? found : tc->instance->VMNull;
    + }
    +}
    +
    void MVM_VMArray_at_pos(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value, MVMuint16 kind) {
    MVMArrayREPRData *repr_data = (MVMArrayREPRData *)st->REPR_data;
    MVMArrayBody *body = (MVMArrayBody *)data;
    diff --git src/6model/reprs/VMArray.h src/6model/reprs/VMArray.h
    index c333caf02..717c403b4 100644
    --- src/6model/reprs/VMArray.h
    +++ src/6model/reprs/VMArray.h
    @@ -76,6 +76,9 @@ struct MVMArrayREPRData {
    MVMObject *elem_type;
    };
    void MVM_VMArray_at_pos(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value, MVMuint16 kind);
    +void MVM_VMArray_at_pos_s(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value);
    +void MVM_VMArray_at_pos_i(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value);
    +void MVM_VMArray_at_pos_o(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value);
    void *MVM_VMArray_find_fast_impl_for_jit(MVMThreadContext *tc, MVMSTable *st, MVMint16 op, MVMuint16 kind);
    void MVM_VMArray_bind_pos(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister value, MVMuint16 kind);

    diff --git src/6model/sc.c src/6model/sc.c
    index 84fa01c9e..19a650d72 100644
    --- src/6model/sc.c
    +++ src/6model/sc.c
    @@ -157,7 +157,7 @@ MVMint64 MVM_sc_find_code_idx(MVMThreadContext *tc, MVMSerializationContext *sc,
    count = MVM_repr_elems(tc, roots);
    for (i = 0; i < count; i++) {
    MVMRegister test;
    - MVM_VMArray_at_pos(tc, STABLE(roots), roots, OBJECT_BODY(roots), i, &test, MVM_reg_obj);
    + MVM_VMArray_at_pos_o(tc, STABLE(roots), roots, OBJECT_BODY(roots), i, &test);
    if (test.o == obj)
    return i;
    }
    diff --git src/spesh/stats.c src/spesh/stats.c
    index 04b52c07a..7b4caf49d 100644
    --- src/spesh/stats.c
    +++ src/spesh/stats.c
    @@ -641,8 +641,7 @@ void MVM_spesh_stats_cleanup(MVMThreadContext *tc, MVMObject *check_frames) {
    MVMint64 i;
    for (i = 0; i < elems; i++) {
    MVMRegister sf_reg;
    - MVM_VMArray_at_pos(tc, check_frames_st, check_frames, check_frames_data,
    - i, &sf_reg, MVM_reg_obj);
    + MVM_VMArray_at_pos_o(tc, check_frames_st, check_frames, check_frames_data, i, &sf_reg);
    MVMStaticFrame *sf = (MVMStaticFrame *)sf_reg.o;
    MVMROOT(tc, sf) {
    MVMStaticFrameSpesh *spesh = sf->body.spesh;