From: fennecdjay Date: Wed, 6 Mar 2019 17:05:50 +0000 (+0100) Subject: :art: Refactor nspc X-Git-Tag: nightly~2753^2~24 X-Git-Url: http://10.11.0.4:5575/?a=commitdiff_plain;h=a3851a61e6a304d6ab8e8c2b0e7bef8a50471b8d;p=gwion.git :art: Refactor nspc --- diff --git a/include/nspc.h b/include/nspc.h index 07c2d5de..e7f57524 100644 --- a/include/nspc.h +++ b/include/nspc.h @@ -1,10 +1,6 @@ #ifndef __NSPC #define __NSPC -struct Nspc_ { - m_str name; - Nspc parent; - struct VM_Code_* pre_ctor; - struct VM_Code_* dtor; +struct NspcInfo_ { m_bit* class_data; struct Vector_ vtable; struct Map_ op_map; @@ -13,6 +9,14 @@ struct Nspc_ { struct Scope_ func; size_t offset; size_t class_data_size; +}; + +struct Nspc_ { + m_str name; + Nspc parent; + struct VM_Code_* pre_ctor; + struct VM_Code_* dtor; + struct NspcInfo_* info; HAS_OBJ }; @@ -23,12 +27,12 @@ extern ANN void nspc_commit(const Nspc); #define describe_lookup0(A, b) \ static inline ANN A nspc_lookup_##b##0(const Nspc n, const Symbol s){ \ - return (A)scope_lookup0(&n->b, (vtype)s); \ + return (A)scope_lookup0(&n->info->b, (vtype)s); \ } #define describe_lookup1(A, b) \ static inline ANN A nspc_lookup_##b##1(const Nspc n, const Symbol s) { \ - const A a = (A)scope_lookup1(&n->b, (vtype)s); \ + const A a = (A)scope_lookup1(&n->info->b, (vtype)s); \ if(!a && n->parent) \ return nspc_lookup_##b##1(n->parent, s); \ return a; \ @@ -40,10 +44,10 @@ describe_lookup1(A, b) \ #define describe_nspc_func(A, b) \ static inline ANN void nspc_add_##b(const Nspc n, const Symbol s, const A a) { \ - scope_add(&n->b, (vtype)s, (vtype)a); \ + scope_add(&n->info->b, (vtype)s, (vtype)a); \ } \ -ANN static inline void nspc_push_##b(const Nspc n) { scope_push(&n->b); } \ -ANN inline static void nspc_pop_##b (const Nspc n) { scope_pop (&n->b); } \ +ANN static inline void nspc_push_##b(const Nspc n) { scope_push(&n->info->b); }\ +ANN inline static void nspc_pop_##b (const Nspc n) { scope_pop (&n->info->b); }\ describe_lookups(A, b) describe_nspc_func(Value, value) diff --git a/src/emit/emit.c b/src/emit/emit.c index d52bb5ac..3ce744d6 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -451,7 +451,7 @@ ANN static m_bool emit_exp_primary(const Emitter emit, const Exp_Primary* prim) ANN static m_bool emit_dot_static_data(const Emitter emit, const Value v, const uint emit_var) { GWDEBUG_EXE const m_uint size = v->type->size; const Instr instr = emit_kind(emit, size, emit_var, dotstatic); - instr->m_val = (m_uint)(v->owner_class->nspc->class_data + v->offset); + instr->m_val = (m_uint)(v->owner_class->nspc->info->class_data + v->offset); instr->m_val2 = size; return GW_OK; } @@ -1211,7 +1211,7 @@ ANN static m_bool emit_stmt_switch(const Emitter emit, const Stmt_Switch stmt) { ANN m_bool value_value(const Value v, m_int *value) { if((!GET_FLAG(v, builtin) && !GET_FLAG(v, enum)) || GET_FLAG(v, member)) return 0; - *value = v->d.ptr ? *(m_int*)v->d.ptr : v->owner->class_data[v->offset]; + *value = v->d.ptr ? *(m_int*)v->d.ptr : v->owner->info->class_data[v->offset]; return GW_OK; } @@ -1256,7 +1256,7 @@ ANN static m_bool emit_stmt_enum(const Emitter emit, const Stmt_Enum stmt) { GWD v->offset = emit_local(emit, SZ_INT, 0); v->d.ptr = addr; } else - *(m_uint*)(emit->env->class_def->nspc->class_data + v->offset) = i; + *(m_uint*)(emit->env->class_def->nspc->info->class_data + v->offset) = i; } return GW_OK; } @@ -1274,10 +1274,11 @@ ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) { G m_uint scope = emit->env->scope->depth; const m_bool global = GET_FLAG(stmt, global); if(stmt->xid) { - if(stmt->value->type->nspc->class_data_size && !stmt->value->type->nspc->class_data) - stmt->value->type->nspc->class_data = - (m_bit*)xcalloc(1, stmt->value->type->nspc->class_data_size); - stmt->value->type->nspc->offset = stmt->s; + if(stmt->value->type->nspc->info->class_data_size && + !stmt->value->type->nspc->info->class_data) + stmt->value->type->nspc->info->class_data = + (m_bit*)xcalloc(1, stmt->value->type->nspc->info->class_data_size); + stmt->value->type->nspc->info->offset = stmt->s; if(!stmt->value->type->p) stmt->value->type->p = mp_ini((uint32_t)stmt->value->type->size); Type_Decl *type_decl = new_type_decl(new_id_list(stmt->xid, stmt->self->pos), @@ -1298,10 +1299,11 @@ ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) { G } scope = emit_push_type(emit, stmt->value->type); } else if(stmt->type_xid) { - if(stmt->type->nspc->class_data_size && !stmt->type->nspc->class_data) - stmt->type->nspc->class_data = - (m_bit*)xcalloc(1, stmt->type->nspc->class_data_size); - stmt->type->nspc->offset = stmt->s; + if(stmt->type->nspc->info->class_data_size && + !stmt->type->nspc->info->class_data) + stmt->type->nspc->info->class_data = + (m_bit*)xcalloc(1, stmt->type->nspc->info->class_data_size); + stmt->type->nspc->info->offset = stmt->s; if(!stmt->type->p) stmt->type->p = mp_ini((uint32_t)stmt->type->size); scope = emit_push_type(emit, stmt->type); @@ -1657,8 +1659,8 @@ ANN static m_bool emit_class_def(const Emitter emit, const Class_Def class_def) if(!base->nspc->pre_ctor) CHECK_BB(emit_class_def(emit, base->def)) } - if(nspc->class_data_size) - nspc->class_data = (m_bit*)xcalloc(1, nspc->class_data_size); + if(nspc->info->class_data_size) + nspc->info->class_data = (m_bit*)xcalloc(1, nspc->info->class_data_size); emit_class_push(emit, type); emit_class_code(emit, type->name); if(class_def->ext && class_def->ext->array) diff --git a/src/lib/import.c b/src/lib/import.c index 397e6d3d..7a46aa61 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -194,9 +194,9 @@ ANN2(1,2) static void import_class_ini(const Env env, const Type type, if(dtor) mk_xtor(type, (m_uint)dtor, ae_flag_dtor); if(type->parent) { - type->nspc->offset = type->parent->nspc->offset; - if(type->parent->nspc->vtable.ptr) - vector_copy2(&type->parent->nspc->vtable, &type->nspc->vtable); + type->nspc->info->offset = type->parent->nspc->info->offset; + if(type->parent->nspc->info->vtable.ptr) + vector_copy2(&type->parent->nspc->info->vtable, &type->nspc->info->vtable); } type->owner = env->curr; SET_FLAG(type, checked); @@ -234,9 +234,9 @@ ANN m_int gwi_class_ext(const Gwi gwi, Type_Decl* td) { if(td->array) SET_FLAG(gwi->env->class_def, typedef); gwi->env->class_def->parent = t; - gwi->env->class_def->nspc->offset = t->nspc->offset; - if(t->nspc->vtable.ptr) - vector_copy2(&t->nspc->vtable, &gwi->env->class_def->nspc->vtable); + gwi->env->class_def->nspc->info->offset = t->nspc->info->offset; + if(t->nspc->info->vtable.ptr) + vector_copy2(&t->nspc->info->vtable, &gwi->env->class_def->nspc->info->vtable); CHECK_OB((gwi->emit->code = emit_class_code(gwi->emit, gwi->env->class_def->name))) if(td->array) @@ -256,8 +256,8 @@ ANN static m_int import_class_end(const Env env) { if(!env->class_def) ERR_B(0, "import: too many class_end called.") const Nspc nspc = env->class_def->nspc; - if(nspc->class_data_size && !nspc->class_data) - nspc->class_data = (m_bit*)xcalloc(1, nspc->class_data_size); + if(nspc->info->class_data_size && !nspc->info->class_data) + nspc->info->class_data = (m_bit*)xcalloc(1, nspc->info->class_data_size); env_pop(env, 0); return GW_OK; } @@ -266,8 +266,8 @@ ANN static m_int import_class_end(const Env env) { ANN m_int gwi_class_end(const Gwi gwi) { if(!gwi->env->class_def)return GW_ERROR; const Type t = gwi->env->class_def; - if(t->nspc && t->nspc->offset) - t->p = mp_ini((uint32_t)t->nspc->offset); + if(t->nspc && t->nspc->info->offset) + t->p = mp_ini((uint32_t)t->nspc->info->offset); return import_class_end(gwi->env); } @@ -582,7 +582,7 @@ ANN m_int gwi_union_end(const Gwi gwi, const ae_flag flag) { CHECK_BB(traverse_stmt_union(gwi->env, &stmt->d.stmt_union)) emit_union_offset(stmt->d.stmt_union.l, stmt->d.stmt_union.o); if(GET_FLAG((&stmt->d.stmt_union), member)) - gwi->env->class_def->nspc->offset = + gwi->env->class_def->nspc->info->offset = stmt->d.stmt_union.o + stmt->d.stmt_union.s; free_stmt(stmt); gwi->union_data.list = NULL; diff --git a/src/lib/object.c b/src/lib/object.c index 1094b003..e84be8f0 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -23,8 +23,8 @@ M_Object new_object(const VM_Shred shred, const Type t) { const M_Object a = mp_alloc(M_Object); a->ref = 1; a->type_ref = t; - a->vtable = &t->nspc->vtable; - if(t->nspc->offset) { + a->vtable = &t->nspc->info->vtable; + if(t->nspc->info->offset) { Type type = t; while(!type->p) type = type->parent; @@ -71,7 +71,7 @@ __attribute__((hot)) ANN void __release(const M_Object obj, const VM_Shred shred) { Type t = obj->type_ref; while(t->parent) { - struct scope_iter iter = { &t->nspc->value, 0, 0 };\ + struct scope_iter iter = { &t->nspc->info->value, 0, 0 };\ Value v; while(scope_iter(&iter, &v) > 0) { if(!GET_FLAG(v, static) && isa(v->type, t_object) > 0) diff --git a/src/oo/env.c b/src/oo/env.c index 7c319ad3..2b88b720 100644 --- a/src/oo/env.c +++ b/src/oo/env.c @@ -112,7 +112,7 @@ ANN m_bool type_engine_check_prog(const Env env, const Ast ast) { ANN m_bool env_add_op(const Env env, const struct Op_Import* opi) { const Nspc nspc = env->curr; - if(!nspc->op_map.ptr) - map_init(&nspc->op_map); + if(!nspc->info->op_map.ptr) + map_init(&nspc->info->op_map); return add_op(nspc, opi); } diff --git a/src/oo/nspc.c b/src/oo/nspc.c index 7d15ef37..8b39aa1a 100644 --- a/src/oo/nspc.c +++ b/src/oo/nspc.c @@ -13,18 +13,18 @@ #include "gwion.h" ANN void nspc_commit(const Nspc nspc) { - scope_commit(&nspc->value); - scope_commit(&nspc->func); - scope_commit(&nspc->type); + scope_commit(&nspc->info->value); + scope_commit(&nspc->info->func); + scope_commit(&nspc->info->type); } ANN static void nspc_release_object(const Nspc a, Value value) { - if(value->d.ptr || (GET_FLAG(value, static) && a->class_data) || + if(value->d.ptr || (GET_FLAG(value, static) && a->info->class_data) || (value->d.ptr && GET_FLAG(value, builtin))) { const VM_Code code = new_vm_code(NULL, 0, ae_flag_builtin, "in code dtor"); const VM_Shred s = new_vm_shred(code); const M_Object obj = value->d.ptr ? (M_Object)value->d.ptr : - *(M_Object*)(a->class_data + value->offset); + *(M_Object*)(a->info->class_data + value->offset); s->info->vm = value->gwion->vm; release(obj, s); free_vm_shred(s); @@ -32,7 +32,7 @@ ANN static void nspc_release_object(const Nspc a, Value value) { } ANN static void free_nspc_value(const Nspc a) { - struct scope_iter iter = { &a->value, 0, 0 }; + struct scope_iter iter = { &a->info->value, 0, 0 }; Value v; while(scope_iter(&iter, &v) > 0) { if(isa(v->type, t_object) > 0 || @@ -42,16 +42,16 @@ ANN static void free_nspc_value(const Nspc a) { } REM_REF(v); } - scope_release(&a->value); + scope_release(&a->info->value); } #define describe_nspc_free(A, b) \ ANN static void nspc_free_##b(Nspc n) {\ - struct scope_iter iter = { &n->b, 0, 0 };\ + struct scope_iter iter = { &n->info->b, 0, 0 };\ A a;\ while(scope_iter(&iter, &a) > 0) \ REM_REF(a);\ - scope_release(&n->b);\ + scope_release(&n->info->b);\ } describe_nspc_free(Func, func) @@ -62,25 +62,27 @@ ANN static void free_nspc(Nspc a) { nspc_free_type(a); free_nspc_value(a); - if(a->class_data) - free(a->class_data); - if(a->vtable.ptr) - vector_release(&a->vtable); + if(a->info->class_data) + free(a->info->class_data); + if(a->info->vtable.ptr) + vector_release(&a->info->vtable); + if(a->info->op_map.ptr) + free_op_map(&a->info->op_map); + mp_free(NspcInfo, a->info); if(a->pre_ctor) REM_REF(a->pre_ctor); if(a->dtor) REM_REF(a->dtor); - if(a->op_map.ptr) - free_op_map(&a->op_map); mp_free(Nspc, a); } ANN Nspc new_nspc(const m_str name) { const Nspc a = mp_alloc(Nspc); a->name = name; - scope_init(&a->value); - scope_init(&a->type); - scope_init(&a->func); + a->info = mp_alloc(NspcInfo); + scope_init(&a->info->value); + scope_init(&a->info->type); + scope_init(&a->info->func); INIT_OO(a, free_nspc); return a; } diff --git a/src/parse/check.c b/src/parse/check.c index 359b2a21..060338f1 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -49,8 +49,8 @@ ANN static inline m_bool check_exp_decl_parent(const Env env, const Var_Decl var #define describe_check_decl(a, b) \ ANN static inline void decl_##a(const Nspc nspc, const Value v) { \ SET_FLAG(v, a); \ - v->offset = nspc->b; \ - nspc->b += v->type->size; \ + v->offset = nspc->info->b; \ + nspc->info->b += v->type->size; \ } describe_check_decl(member, offset) describe_check_decl(static, class_data_size) @@ -960,7 +960,7 @@ ANN m_bool check_stmt_union(const Env env, const Stmt_Union stmt) { GWDEBUG_EXE } } else if(env->class_def) { if(!GET_FLAG(stmt, static)) - stmt->o = env->class_def->nspc->offset; + stmt->o = env->class_def->nspc->info->offset; else decl_static(env->curr, stmt->value); } @@ -1022,7 +1022,7 @@ ANN static m_bool parent_match_actual(const Env env, const restrict Func_Def f, CHECK_BB(check_signature_match(f, parent_func)) if(!f->tmpl) { f->func->vt_index = parent_func->vt_index; - vector_set(&env->curr->vtable, f->func->vt_index, (vtype)f->func); + vector_set(&env->curr->info->vtable, f->func->vt_index, (vtype)f->func); } return GW_OK; } @@ -1041,10 +1041,10 @@ ANN static m_bool check_parent_match(const Env env, const Func_Def f) { GWDEBUG_ return match; } } - if(!env->curr->vtable.ptr) - vector_init(&env->curr->vtable); - func->vt_index = vector_size(&env->curr->vtable); - vector_add(&env->curr->vtable, (vtype)func); + if(!env->curr->info->vtable.ptr) + vector_init(&env->curr->info->vtable); + func->vt_index = vector_size(&env->curr->info->vtable); + vector_add(&env->curr->info->vtable, (vtype)func); return GW_OK; } @@ -1183,9 +1183,9 @@ ANN static m_bool check_class_body(const Env env, const Class_Def class_def) { ANN static inline void inherit(const Type t) { const Nspc nspc = t->nspc, parent = t->parent->nspc; - nspc->offset = parent->offset; - if(parent->vtable.ptr) - vector_copy2(&parent->vtable, &nspc->vtable); + nspc->info->offset = parent->info->offset; + if(parent->info->vtable.ptr) + vector_copy2(&parent->info->vtable, &nspc->info->vtable); } ANN m_bool check_class_def(const Env env, const Class_Def class_def) { GWDEBUG_EXE @@ -1199,8 +1199,8 @@ ANN m_bool check_class_def(const Env env, const Class_Def class_def) { GWDEBUG_E inherit(the_class); if(class_def->body) CHECK_BB(check_class_body(env, class_def)) - if(!the_class->p && the_class->nspc->offset) - the_class->p = mp_ini((uint32_t)the_class->nspc->offset); + if(!the_class->p && the_class->nspc->info->offset) + the_class->p = mp_ini((uint32_t)the_class->nspc->info->offset); SET_FLAG(the_class, checked | ae_flag_check); return GW_OK; } diff --git a/src/parse/operator.c b/src/parse/operator.c index fb59741d..fbf24e09 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -77,11 +77,11 @@ ANN2(1) static M_Operator* operator_find(const Vector v, const restrict Type lhs } ANN m_bool add_op(const Nspc nspc, const struct Op_Import* opi) { - Vector v = (Vector)map_get(&nspc->op_map, (vtype)opi->op); + Vector v = (Vector)map_get(&nspc->info->op_map, (vtype)opi->op); M_Operator* mo; if(!v) { v = new_vector(); - map_set(&nspc->op_map, (vtype)opi->op, (vtype)v); + map_set(&nspc->info->op_map, (vtype)opi->op, (vtype)v); } if((mo = operator_find(v, opi->lhs, opi->rhs))) CHECK_BB((err_msg(0, "operator '%s', for type '%s' and '%s' already imported", @@ -139,11 +139,11 @@ static m_str type_name(const Type t) { ANN Type op_check(const Env env, struct Op_Import* opi) { Nspc nspc = env->curr; do { - if(nspc->op_map.ptr) { + if(nspc->info->op_map.ptr) { Type l = opi->lhs; do { struct Op_Import opi2 = { .op=opi->op, .lhs=l, .rhs=opi->rhs, .data=opi->data }; - Type ret = op_check_inner(env, &nspc->op_map, &opi2); + Type ret = op_check_inner(env, &nspc->info->op_map, &opi2); if(ret) { if(ret == t_null) break; @@ -163,7 +163,7 @@ 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->owner; - const Vector v = (Vector)map_get(&nspc->op_map, opi->op); + const Vector v = (Vector)map_get(&nspc->info->op_map, opi->op); M_Operator* mo = operator_find(v, opi->lhs, opi->rhs); CHECK_OB(mo) mo->func = (Func)opi->data; @@ -200,9 +200,9 @@ ANN m_bool op_emit(const Emitter emit, const struct Op_Import* opi) { do { Type r = opi->rhs; do { - if(!nspc->op_map.ptr) + if(!nspc->info->op_map.ptr) continue; - const Vector v = (Vector)map_get(&nspc->op_map, (vtype)opi->op); + const Vector v = (Vector)map_get(&nspc->info->op_map, (vtype)opi->op); const M_Operator* mo = operator_find(v, l, r); if(mo) { if(mo->em)