From 2f077a83b26a19646e9e46dc3590e672a2f7f7dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Fri, 5 Jun 2020 22:41:11 +0200 Subject: [PATCH] :art: Improve array and auto loop --- src/emit/emit.c | 5 ++++- src/lib/array.c | 8 ++++++++ src/parse/check.c | 7 ++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index f44cca4d..87e7529e 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -264,6 +264,7 @@ ANN void emit_ext_ctor(const Emitter emit, const VM_Code code) { ANN m_bool emit_array_extend(const Emitter emit, const Type t, const Exp e) { CHECK_OB(emit_array_extend_inner(emit, t, e)) emit_add_instr(emit, PopArrayClass); + emit_add_instr(emit, RegAddRef); return GW_OK; } @@ -1567,6 +1568,7 @@ ANN static m_bool emit_stmt_jump(const Emitter emit, const Stmt_Jump stmt) { assert(stmt->data.v.ptr); const m_uint size = vector_size(&stmt->data.v); if(!size) +//return GW_OK; ERR_B(stmt_self(stmt)->pos, _("label '%s' defined but not used."), s_name(stmt->name)) LOOP_OPTIM for(m_uint i = size + 1; --i;) { @@ -1971,7 +1973,7 @@ ANN static m_bool emit_func_def(const Emitter emit, const Func_Def f) { const Func func = f->base->func; const Func_Def fdef = func->def; const Func former = emit->env->func; - if(tmpl_base(fdef->base->tmpl)) + if(func->code || tmpl_base(fdef->base->tmpl)) return GW_OK; if(SAFE_FLAG(emit->env->class_def, builtin) && GET_FLAG(emit->env->class_def, template)) return GW_OK; @@ -2049,6 +2051,7 @@ ANN static m_bool emit_class_def(const Emitter emit, const Class_Def cdef) { if(scanx_body(emit->env, cdef, (_exp_func)emit_section, emit) > 0) emit_class_finish(emit, t->nspc); else { + free_code(emit->gwion->mp, emit->code); emit_pop_code(emit); return GW_ERROR; } diff --git a/src/lib/array.c b/src/lib/array.c index ee5b6cc3..1a024eca 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -55,6 +55,8 @@ ANN static inline int is_array(const Type *types, const Type type) { static DTOR(array_dtor) { const Type t = !GET_FLAG(o->type_ref, nonnull) ? o->type_ref : o->type_ref->e->parent; + if(*(void**)(o->data + SZ_INT)) + xfree(*(void**)(o->data + SZ_INT)); struct M_Vector_* a = ARRAY(o); if(!a) return; @@ -325,6 +327,8 @@ GWION_IMPORT(array) { gwi_class_xtor(gwi, NULL, array_dtor); GWI_BB(gwi_item_ini(gwi, "@internal", "@array")) GWI_BB(gwi_item_end(gwi, 0, NULL)) + GWI_BB(gwi_item_ini(gwi, "@internal", "@ctor_data")) + GWI_BB(gwi_item_end(gwi, 0, NULL)) GWI_BB(gwi_func_ini(gwi, "int", "size")) GWI_BB(gwi_func_end(gwi, vm_vector_size, ae_flag_none)) @@ -373,6 +377,8 @@ INSTR(ArrayBottom) { INSTR(ArrayPost) { xfree(*(m_uint**)REG(0)); + const M_Object o = *(M_Object*)(REG(-SZ_INT)); + *(m_uint*)(o->data + SZ_INT) = 0; } INSTR(ArrayInit) {// for litteral array @@ -451,6 +457,8 @@ INSTR(ArrayAlloc) { vm_shred_exit(shred); return; // TODO make exception vararg } + *(void**)(ref->data + SZ_INT) = aai.data; + vector_add(&shred->gc, ref); if(!is_obj) { POP_REG(shred, SZ_INT * (info->depth - 1)); *(M_Object*)REG(-SZ_INT) = ref; diff --git a/src/parse/check.c b/src/parse/check.c index 14ddbe3d..74f902b5 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1003,15 +1003,12 @@ ANN static m_bool do_stmt_auto(const Env env, const Stmt_Auto stmt) { " This is not allowed in auto loop"), stmt->exp->info->type->name) if(stmt->is_ptr) { struct Type_List_ tl = {}; - struct Array_Sub_ array = {}; Type_Decl *td0 = type2td(env, ptr, stmt->exp->pos), td = { .xid=insert_symbol("Ptr"), .types=&tl, .pos=stmt->exp->pos }; tl.td = td0; - if(depth) { - array.depth = depth; - td0->array = &array; - } ptr = known_type(env, &td); + if(depth) + ptr = array_type(env, ptr, depth); td0->array = NULL; free_type_decl(env->gwion->mp, td0); const Type base = get_type(ptr); -- 2.43.0