From: Jérémie Astor Date: Mon, 14 Sep 2020 20:44:55 +0000 (+0200) Subject: :art: Improve gc and fix array X-Git-Tag: nightly~1297 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=47daacea8701474be80f1c780206f574a9a629e5;p=gwion.git :art: Improve gc and fix array --- diff --git a/include/emit.h b/include/emit.h index d7c2fc7d..049133a7 100644 --- a/include/emit.h +++ b/include/emit.h @@ -50,4 +50,9 @@ ANN static inline void emit_except(const Emitter emit, const Type t) { if(!GET_FLAG(t, nonnull)) emit_add_instr(emit, GWOP_EXCEPT); } +ANN static inline Instr emit_gc(const Emitter emit, const m_int offset) { + const Instr gc = emit_add_instr(emit, GcAdd); + gc->m_val = offset; + return gc; +} #endif diff --git a/src/emit/emit.c b/src/emit/emit.c index daa5cf1c..2a7fbaf7 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -405,7 +405,7 @@ ANN static m_bool emit_prim_array(const Emitter emit, const Array_Sub *data) { const Instr instr = emit_add_instr(emit, ArrayInit); instr->m_val = (m_uint)type; instr->m_val2 = type->array_depth == 1 ? array_base(type)->size : SZ_INT; - emit_add_instr(emit, GcAdd); + emit_gc(emit, -SZ_INT); emit_notpure(emit); return GW_OK; } @@ -432,7 +432,7 @@ ANN static m_bool emit_prim_range(const Emitter emit, Range **data) { struct Op_Import opi = { .op=sym, .rhs=e->info->type, .pos=e->pos, .data=(uintptr_t)prim_exp(data), .op_type=op_exp }; CHECK_OB(op_emit(emit, &opi)) - emit_add_instr(emit, GcAdd); + emit_gc(emit, -SZ_INT); return GW_OK; } diff --git a/src/lib/array.c b/src/lib/array.c index f3ad109a..7262064c 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -191,14 +191,7 @@ static OP_CHECK(opck_array_sr) { return check_array_shift(env, bin->rhs, bin->lhs, ">>", exp_self(bin)->pos); } -ANN static Instr emit_array_shift(const Emitter emit, - const Exp a, const Exp b, const f_instr exec) { - if(a->info->type->array_depth == b->info->type->array_depth + 1) { - const Type type = b->info->type; - const Instr pop = emit_add_instr(emit, RegPop); - pop->m_val = type->size; - return emit_add_instr(emit, ArrayAppend); - } +ANN static Instr emit_array_shift(const Emitter emit, const f_instr exec) { const Instr pop = emit_add_instr(emit, RegPop); pop->m_val = SZ_INT; return emit_add_instr(emit, exec); @@ -249,18 +242,20 @@ static INSTR(ArrayConcatRight) { static OP_EMIT(opem_array_sr) { const Exp_Binary* bin = (Exp_Binary*)data; if(bin->rhs->info->type->array_depth == bin->lhs->info->type->array_depth) - return emit_array_shift(emit, bin->rhs, bin->lhs, ArrayConcatRight); + return emit_array_shift(emit, ArrayConcatRight); const Instr pop = emit_add_instr(emit, RegPop); pop->m_val = SZ_INT; + (void)emit_gc(emit, 0); return emit_add_instr(emit, ArrayAppendFront); } static OP_EMIT(opem_array_sl) { const Exp_Binary* bin = (Exp_Binary*)data; if(bin->lhs->info->type->array_depth == bin->rhs->info->type->array_depth) - return emit_array_shift(emit, bin->lhs, bin->rhs, ArrayConcatLeft); + return emit_array_shift(emit, ArrayConcatLeft); const Instr pop = emit_add_instr(emit, RegPop); - pop->m_val = SZ_INT; + pop->m_val = bin->rhs->info->type->size; + (void)emit_gc(emit, -SZ_INT); return emit_add_instr(emit, ArrayAppend); } @@ -313,7 +308,7 @@ static INSTR(ArraySlice) { static OP_EMIT(opem_array_slice) { emit_add_instr(emit, ArraySlice); - return emit_add_instr(emit, GcAdd); + return emit_gc(emit, -SZ_INT); } static FREEARG(freearg_array) { @@ -376,7 +371,7 @@ ANN static void array_finish(const Emitter emit, const m_uint depth, } ANN static inline m_bool array_do(const Emitter emit, const Array_Sub array, const m_bool is_var) { - emit_add_instr(emit, GcAdd); + emit_gc(emit, -SZ_INT); CHECK_BB(emit_exp(emit, array->exp)) array_loop(emit, array->depth); array_finish(emit, array->depth, array->type->size, is_var); diff --git a/src/lib/opfunc.c b/src/lib/opfunc.c index 444b49c1..4acfaff4 100644 --- a/src/lib/opfunc.c +++ b/src/lib/opfunc.c @@ -102,5 +102,5 @@ OP_EMIT(opem_new) { const Exp_Unary* unary = (Exp_Unary*)data; CHECK_BO(emit_instantiate_object(emit, exp_self(unary)->info->type, unary->td->array, GET_FLAG(unary->td, ref))) - return emit_add_instr(emit, GcAdd); + return emit_gc(emit, -SZ_INT); } diff --git a/src/vm/vm.c b/src/vm/vm.c index 934e4fa5..3027e6ec 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -709,7 +709,6 @@ branchnefloat: BRANCH_DISPATCH(*(m_float*)reg); arrayappend: m_vector_add(ARRAY(*(M_Object*)(reg-SZ_INT)), reg); - release(*(M_Object*)(reg-SZ_INT), shred); DISPATCH() autoloop: m_vector_get(ARRAY(*(M_Object*)(reg-SZ_INT)), *(m_uint*)(mem + VAL), mem + VAL + SZ_INT); @@ -850,7 +849,7 @@ gcini: vector_add(&shred->gc, 0); DISPATCH(); gcadd: - vector_add(&shred->gc, *(vtype*)(reg-SZ_INT)); + vector_add(&shred->gc, *(vtype*)(reg+(m_int)VAL)); DISPATCH(); gcend: {