From 3491220af1b332ecb06258aeb9e0ebc7eaae80cc Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Wed, 11 Jan 2023 20:58:27 +0100 Subject: [PATCH] :art: memory layout changes :) --- include/env/nspc.h | 9 ++++-- plug | 2 +- src/emit/emit.c | 4 --- src/env/nspc.c | 8 ++++-- src/import/import_cdef.c | 6 +--- src/parse/operator.c | 60 +++++++++++++++++++++++----------------- src/parse/scan1.c | 4 ++- 7 files changed, 50 insertions(+), 43 deletions(-) diff --git a/include/env/nspc.h b/include/env/nspc.h index e63fc131..10377682 100644 --- a/include/env/nspc.h +++ b/include/env/nspc.h @@ -1,19 +1,22 @@ #ifndef __NSPC #define __NSPC typedef struct NspcInfo_ { - struct Map_ op_map; Scope value; Scope type; Scope func; Scope trait; - struct Vector_ op_tmpl; } NspcInfo; +typedef struct NspcOp_ { + struct Map_ map; + struct Vector_ tmpl; +} NspcOp; + struct Nspc_ { struct Vector_ vtable; m_bit * class_data; - struct VM_Code_ * pre_ctor; struct VM_Code_ * dtor; + NspcOp *operators; Nspc parent; m_str name; NspcInfo *info; diff --git a/plug b/plug index 1ac7900f..d6c702c8 160000 --- a/plug +++ b/plug @@ -1 +1 @@ -Subproject commit 1ac7900fc1cd7b2dc9cb0ad802582fff2fdf240b +Subproject commit d6c702c806d2598cfed50d3e6335ba4fb529b195 diff --git a/src/emit/emit.c b/src/emit/emit.c index 01f1dbab..e5ac30ea 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -2723,8 +2723,6 @@ ANN static inline VM_Code _emit_func_def_code(const Emitter emit, } const VM_Code code = !fbflag(func->def->base, fbflag_internal) ? finalyze(emit, FuncReturn) : emit_internal(emit, func); - if(is_ctor(func->def) && !emit->env->class_def->nspc->pre_ctor) - emit->env->class_def->nspc->pre_ctor = code; return code; } @@ -2848,8 +2846,6 @@ ANN static m_bool _emit_func_def(const Emitter emit, const Func_Def f) { if(fdef->builtin) { fdef->base->func->code = new_vmcode(emit->gwion->mp, NULL, NULL, func->name, fdef->stack_depth, true, false); fdef->base->func->code->native_func = (m_uint)fdef->d.dl_func_ptr; - if(is_ctor(fdef)) - emit->env->class_def->nspc->pre_ctor = fdef->base->func->code; return GW_OK; } if ((vflag(func->value_ref, vflag_builtin) && diff --git a/src/env/nspc.c b/src/env/nspc.c index f34326df..b1472bd3 100644 --- a/src/env/nspc.c +++ b/src/env/nspc.c @@ -71,14 +71,16 @@ ANN void free_nspc(const Nspc a, const Gwion gwion) { nspc_free_value(a, gwion); nspc_free_func(a, gwion); nspc_free_trait(a, gwion); - if (a->info->op_map.ptr) free_op_map(&a->info->op_map, gwion); - if (a->info->op_tmpl.ptr) free_op_tmpl(&a->info->op_tmpl, gwion); + if(a->operators) { + if (a->operators->map.ptr) free_op_map(&a->operators->map, gwion); + if (a->operators->tmpl.ptr) free_op_tmpl(&a->operators->tmpl, gwion); + mp_free(gwion->mp, NspcOp, a->operators); + } nspc_free_type(a, gwion); if (a->class_data && a->class_data_size) mp_free2(gwion->mp, a->class_data_size, a->class_data); if (a->vtable.ptr) vector_release(&a->vtable); mp_free(gwion->mp, NspcInfo, a->info); - if (a->pre_ctor) vmcode_remref(a->pre_ctor, gwion); if (a->dtor) vmcode_remref(a->dtor, gwion); mp_free(gwion->mp, Nspc, a); } diff --git a/src/import/import_cdef.c b/src/import/import_cdef.c index 61140ef6..9c6aa441 100644 --- a/src/import/import_cdef.c +++ b/src/import/import_cdef.c @@ -47,11 +47,7 @@ void gwi_class_xtor(const Gwi gwi, const f_xtor ctor, const f_xtor dtor) { if (ctor) { gwi_func_ini(gwi, "void", "@ctor"); gwi_func_end(gwi, ctor, ae_flag_none); - if(t->nspc->vtable.ptr) { - set_tflag(t, tflag_ctor); - const Func f = (Func)vector_front(&t->nspc->vtable); - t->nspc->pre_ctor = f->code; - } + if(t->nspc->vtable.ptr) set_tflag(t, tflag_ctor); } } diff --git a/src/parse/operator.c b/src/parse/operator.c index 23e9db12..c054b103 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -80,8 +80,8 @@ static M_Operator *operator_find2(const Vector v, const restrict Type lhs, } ANN void operator_suspend(const Nspc n, struct Op_Import *opi) { - const m_int idx = map_index(&n->info->op_map, (vtype)opi->op); - const Vector v = (Vector)&VVAL(&n->info->op_map, idx); + const m_int idx = map_index(&n->operators->map, (vtype)opi->op); + const Vector v = (Vector)&VVAL(&n->operators->map, idx); for (m_uint i = vector_size(v) + 1; --i;) { M_Operator *mo = (M_Operator *)vector_at(v, i - 1); if (opi->lhs == mo->lhs && opi->rhs == mo->rhs) { @@ -125,7 +125,7 @@ op_vector(const struct OpChecker *ock) { } ANN static m_bool _op_exist(const struct OpChecker *ock, const Nspc n) { - const m_int idx = map_index(&n->info->op_map, (vtype)ock->opi->op); + const m_int idx = map_index(&n->operators->map, (vtype)ock->opi->op); if (idx == -1 || !operator_find2((Vector)&VVAL(ock->map, idx), ock->opi->lhs, ock->opi->rhs)) return GW_OK; @@ -137,20 +137,24 @@ ANN static m_bool _op_exist(const struct OpChecker *ock, const Nspc n) { } ANN static m_bool op_exist(const struct OpChecker *ock, const Nspc n) { - return n->info->op_map.ptr ? _op_exist(ock, n) : GW_OK; + return n->operators->map.ptr ? _op_exist(ock, n) : GW_OK; } ANN m_bool add_op(const Gwion gwion, const struct Op_Import *opi) { Nspc n = gwion->env->curr; do { - struct OpChecker ock = { - .env = gwion->env, .map = &n->info->op_map, .opi = opi}; - CHECK_BB(op_exist(&ock, n)); + if (n->operators) { + struct OpChecker ock = { + .env = gwion->env, .map = &n->operators->map, .opi = opi}; + CHECK_BB(op_exist(&ock, n)); + } } while ((n = n->parent)); - if (!gwion->env->curr->info->op_map.ptr) - map_init(&gwion->env->curr->info->op_map); + if (!gwion->env->curr->operators) + gwion->env->curr->operators = mp_calloc(gwion->mp, NspcOp); + if (!gwion->env->curr->operators->map.ptr) + map_init(&gwion->env->curr->operators->map); struct OpChecker ock = { - .env = gwion->env, .map = &gwion->env->curr->info->op_map, .opi = opi}; + .env = gwion->env, .map = &gwion->env->curr->operators->map, .opi = opi}; const Vector v = op_vector(&ock); const M_Operator *mo = new_mo(gwion->mp, opi); vector_add(v, (vtype)mo); @@ -261,8 +265,8 @@ ANN static Type op_def(const Env env, struct Op_Import *const opi, ANN static Type op_check_tmpl(const Env env, struct Op_Import *opi) { Nspc nspc = env->curr; do { - if (!nspc->info->op_tmpl.ptr) continue; - const Vector v = &nspc->info->op_tmpl; + if (!nspc->operators || !nspc->operators->tmpl.ptr) continue; + const Vector v = &nspc->operators->tmpl; for (m_uint i = vector_size(v) + 1; --i;) { const Func_Def fdef = (Func_Def)vector_at(v, i - 1); if (opi->op != fdef->base->xid) continue; @@ -278,8 +282,9 @@ ANN void* op_get(const Env env, struct Op_Import *opi) { Nspc nspc = env->curr; do { Type l = opi->lhs; - if (!nspc->info->op_map.ptr) continue; - const Map map = &nspc->info->op_map; + if (!nspc->operators) continue; + if (!nspc->operators->map.ptr) continue; + const Map map = &nspc->operators->map; do { Type r = opi->rhs; do { @@ -326,13 +331,14 @@ ANN Type op_check(const Env env, struct Op_Import *opi) { for (int i = 0; i < 2; ++i) { Nspc nspc = env->curr; do { - if (!nspc->info->op_map.ptr) continue; + if (!nspc->operators) continue; + if (!nspc->operators->map.ptr) continue; Type l = opi->lhs; do { struct Op_Import opi2 = { .op = opi->op, .lhs = l, .rhs = opi->rhs, .data = opi->data}; struct OpChecker ock = { - .env = env, .map = &nspc->info->op_map, .opi = &opi2}; + .env = env, .map = &nspc->operators->map, .opi = &opi2}; const Type ret = op_check_inner(env, &ock, i); if (ret) { if (ret == env->gwion->type[et_error]) return NULL; @@ -370,8 +376,8 @@ ANN Type op_check(const Env env, struct Op_Import *opi) { ANN m_bool operator_set_func(const struct Op_Import *opi) { const Nspc nspc = ((Func)opi->data)->value_ref->from->owner; - const m_int idx = map_index(&nspc->info->op_map, (vtype)opi->op); - const Vector v = (Vector)&VVAL(&nspc->info->op_map, idx); + const m_int idx = map_index(&nspc->operators->map, (vtype)opi->op); + const Vector v = (Vector)&VVAL(&nspc->operators->map, idx); DECL_OB(M_Operator *, mo, = operator_find(v, opi->lhs, opi->rhs)); mo->func = (Func)opi->data; return GW_OK; @@ -397,14 +403,15 @@ ANN m_bool op_emit(const Emitter emit, const struct Op_Import *opi) { for (int i = 0; i < 2; ++i) { Nspc nspc = emit->env->curr; do { - if (!nspc->info->op_map.ptr) continue; + if (!nspc->operators) continue; + if (!nspc->operators->map.ptr) continue; Type l = opi->lhs; do { Type r = opi->rhs; do { - const m_int idx = map_index(&nspc->info->op_map, (vtype)opi->op); + const m_int idx = map_index(&nspc->operators->map, (vtype)opi->op); if (idx == -1) continue; - const Vector v = (Vector)&VVAL(&nspc->info->op_map, idx); + const Vector v = (Vector)&VVAL(&nspc->operators->map, idx); const M_Operator *mo = !i ? operator_find2(v, l, r) : operator_find(v, l, r); if (mo) { @@ -440,8 +447,9 @@ ANN static M_Operator *cpy_mo(MemPool p, M_Operator *const base, } #undef CONVERT -ANN static inline Map ensure_map(const Nspc nspc) { - const Map map = &nspc->info->op_map; +ANN static inline Map ensure_map(MemPool mp, const Nspc nspc) { + if (!nspc->operators) nspc->operators = mp_calloc(mp, NspcOp); + const Map map = &nspc->operators->map; if (!map->ptr) map_init(map); return map; } @@ -459,9 +467,9 @@ ANN static void op_visit(const MemPool mp, const Nspc nspc, const struct Op_Import *opi, const Vector visited) { if (vector_find(visited, (m_uint)nspc) != -1) return; vector_add(visited, (m_uint)nspc); - if (nspc->info->op_map.ptr) { - const Map map = &nspc->info->op_map; - const Map base_map = ensure_map(opi->rhs->info->value->from->owner); + if (nspc->operators && nspc->operators->map.ptr) { + const Map map = &nspc->operators->map; + const Map base_map = ensure_map(mp, opi->rhs->info->value->from->owner); for (m_uint i = 0; i < map_size(map); i++) { const Vector v = (Vector)&VVAL(map, i); const m_uint sz = vector_size(v); diff --git a/src/parse/scan1.c b/src/parse/scan1.c index b128801a..5a9dc501 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -478,7 +478,9 @@ ANN static m_bool scan1_fdef_base_tmpl(const Env env, const Func_Def fdef) { if (!arg->td->next && spec && arg->td->xid == spec->xid) { j++; } } if (j < sl->len) ERR_B(base->pos, "too many template types for operator"); - const Vector v = &env->curr->info->op_tmpl; + if (!env->curr->operators) + env->curr->operators = mp_calloc(env->gwion->mp, NspcOp); + const Vector v = &env->curr->operators->tmpl; if (!v->ptr) vector_init(v); vector_add(v, (m_uint)cpy_func_def(env->gwion->mp, fdef)); return GW_OK; -- 2.43.0