From 58d276fda46eb23e49e3059810d9d3e809a1dfd7 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 16 Jul 2019 14:45:43 +0200 Subject: [PATCH] :art: Improve emit_instantiate_object --- include/emit.h | 2 +- src/emit/emit.c | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/emit.h b/include/emit.h index fd7c8c4d..ba1822ef 100644 --- a/include/emit.h +++ b/include/emit.h @@ -37,5 +37,5 @@ ANN m_bool emit_array_extend(const Emitter, const Type, const Exp); ANN void emit_class_finish(const Emitter, const Nspc); ANN void emit_ext_ctor(const Emitter, const VM_Code); ANN void emit_union_offset(Decl_List, const m_uint); -ANN2(1,2) m_bool emit_instantiate_object(const Emitter, const Type, const Array_Sub, const uint); +ANN2(1,2) m_bool emit_instantiate_object(const Emitter, const Type, const Array_Sub, const m_bool); #endif diff --git a/src/emit/emit.c b/src/emit/emit.c index e3771af8..ed868e06 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -237,26 +237,23 @@ ANN static inline void emit_notpure(const Emitter emit) { ++VPTR(&emit->pure, VLEN(&emit->pure)); } +ANN static Array_Sub instantiate_typedef(MemPool p, const m_uint depth) { + const Exp base = new_exp_prim_int(p, 0, new_loc(p, __LINE__)); + Exp e = base; + for(m_uint i = 0; i < depth; ++i) + e = (e->next = new_exp_prim_int(p, 0, new_loc(p, __LINE__))); + return new_array_sub(p, base); +} + ANN2(1,2) m_bool emit_instantiate_object(const Emitter emit, const Type type, - const Array_Sub arr, const uint is_ref) { - Array_Sub array = arr; + const Array_Sub arr, const m_bool is_ref) { if(type->array_depth) { - if(!array || array->depth < type->array_depth) { // from typeof xxx[]... - Exp base = new_exp_prim_int(emit->gwion->mp, 0, new_loc(emit->gwion->mp, __LINE__)), e = base; - for(m_uint i = (array ? array->depth : 0); i < type->array_depth; ++i) - e = (e->next = new_exp_prim_int(emit->gwion->mp, 0, new_loc(emit->gwion->mp, __LINE__))); - if(array) { - Exp array_base = array->exp; - while(array_base->next) - array_base = array_base->next; - array_base->next = base; - } else - array = new_array_sub(emit->gwion->mp, base); - } + assert(!arr || arr->depth == type->array_depth); + const Array_Sub array = arr ?: instantiate_typedef(emit->gwion->mp, type->array_depth); assert(array->exp); DECL_OB(ArrayInfo*, info, = emit_array_extend_inner(emit, type, array->exp)) - info->is_ref = !!is_ref; - if(array != arr) + info->is_ref = is_ref; + if(!arr) free_array_sub(emit->gwion->mp, array); } else if(!is_ref) { const Instr instr = emit_add_instr(emit, ObjectInstantiate); -- 2.43.0