From 7719bfdb989a7ea5f55002b52b62b7b01e534f6d Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Thu, 4 Apr 2019 01:34:14 +0200 Subject: [PATCH] :art: More clean --- ast | 2 +- include/parse.h | 5 +++-- src/emit/emit.c | 15 ++++++++++----- src/gwion.c | 2 +- src/lib/instr.c | 18 ++++++++++++++---- src/lib/object.c | 7 +------ src/oo/env.c | 2 +- src/oo/switch.c | 35 ++++++++++++++++++++--------------- src/parse/scan1.c | 2 +- src/vm/vm.c | 26 ++++++++++++++++++++------ util | 2 +- 11 files changed, 73 insertions(+), 43 deletions(-) diff --git a/ast b/ast index 95d45403..fbd0bdbb 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 95d45403dd3a01edfe7dc5e817a931f5c62c98e6 +Subproject commit fbd0bdbb2950dbfc8f62167c795390500d660cfa diff --git a/include/parse.h b/include/parse.h index 0f9c07ba..65baed41 100644 --- a/include/parse.h +++ b/include/parse.h @@ -34,8 +34,9 @@ static const _exp_func exp_func[] = { static const _exp_func section_func[] = { \ (_exp_func)prefix##_stmt_list, (_exp_func)prefix##_func_def, (_exp_func)prefix##_class_def \ }; \ -ANN static inline m_bool prefix##_section(const void* a, const Section* section) { GWDEBUG_EXE \ - return section_func[section->section_type](a, *(void**)§ion->d); \ +ANN static inline m_bool prefix##_section(const void* a, /* const */ Section* section) { GWDEBUG_EXE \ + void* d = §ion->d.stmt_list; \ + return section_func[section->section_type](a, *(void**)d); \ } #define HANDLE_EXP_FUNC(prefix, type, ret) \ diff --git a/src/emit/emit.c b/src/emit/emit.c index 11d4adcb..dbf53dc2 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -317,7 +317,7 @@ ANN static m_bool emit_symbol(const Emitter emit, const Exp_Primary* prim) { GWD const Value v = prim->value; if(v->owner_class) return emit_symbol_owned(emit, prim); - if(GET_FLAG(v, builtin) || GET_FLAG(v, union)) + if(GET_FLAG(v, builtin) || GET_FLAG(v, union) || GET_FLAG(v, enum)) return emit_symbol_builtin(emit, prim); const m_uint size = v->type->size; const Instr instr = emit_kind(emit, size, prim->self->emit_var, !GET_FLAG(v, global) ? regpushmem : regpushbase); @@ -451,9 +451,11 @@ ANN static m_bool prim_char(const Emitter emit, const Exp_Primary* prim) { } ANN static m_bool prim_str(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE - char c[strlen(prim->d.str)]; - strcpy(c, prim->d.str); - CHECK_BB(escape_str(c, prim->self->pos)); + char c[strlen(prim->d.str) + 1]; + if(strlen(prim->d.str)) { + strcpy(c, prim->d.str); + CHECK_BB(escape_str(c, prim->self->pos)); + } else c[0] = '\0'; const Instr instr = emit_add_instr(emit, RegPushStr); instr->m_val = (m_uint)s_name(insert_symbol(c)); return GW_OK; @@ -1332,7 +1334,9 @@ 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->info->class_data + v->offset) = i; +// *(m_uint*)(emit->env->class_def->nspc->info->class_data + v->offset) = i; +// alignement + *(m_bit*)(emit->env->class_def->nspc->info->class_data + v->offset) = i; } return GW_OK; } @@ -1654,6 +1658,7 @@ ANN static void emit_func_def_code(const Emitter emit, const Func func) { GWDEBU Instr instr = (Instr)vector_back(func->code->instr); instr->opcode = eOP_MAX; instr->execute = DTOR_EOC; + instr->m_val = (m_uint)emit->gwion->p; ADD_REF(func->code) } } diff --git a/src/gwion.c b/src/gwion.c index 8abcbb94..13fe101d 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -99,7 +99,7 @@ ANN void gwion_run(const Gwion gwion) { ANN void gwion_end(const Gwion gwion) { const VM_Code code = new_vm_code(gwion->p, NULL, 0, ae_flag_builtin, "in code dtor"); gwion->vm->cleaner_shred = new_vm_shred(gwion->p, code); - gwion->vm->cleaner_shred->info->vm = gwion->vm; + vm_add_shred(gwion->vm, gwion->vm->cleaner_shred); free_env(gwion->env); free_vm_shred(gwion->vm->cleaner_shred); free_emitter(gwion->emit); diff --git a/src/lib/instr.c b/src/lib/instr.c index 4564ab38..8eb7cfbb 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -12,17 +12,27 @@ #include "func.h" #include "array.h" #include "nspc.h" +#include "shreduler_private.h" INSTR(EOC) { GWDEBUG_EXE vm_shred_exit(shred); } - +#include "gwion.h" INSTR(DTOR_EOC) { GWDEBUG_EXE + // TODO: we should be able to use shred->info->mp directly + shred->info->mp = (MemPool)instr->m_val; const M_Object o = *(M_Object*)MEM(0); o->type_ref = o->type_ref->parent; - _release(o, shred); - if(shred->info->me->ref > 1) // ??? - _release(shred->info->me, shred); +printf("'obj %p %p %s\n", o, shred->info->me, shred->info->me->type_ref->name); +//free_object( + __release(o, shred); +//shreduler_remove(shred->tick->shreduler, shred, 0); +//vector_rem2(&shred->tick->shreduler->shreds, shred); +//++shred->info->me->ref; +//REM_REF(shred->code, shred->info->vm->gwion); +// _release(shred->info->me, shred); +// _release(shred->info->me, shred); +//free_vm_shred(shred); vm_shred_exit(shred); } diff --git a/src/lib/object.c b/src/lib/object.c index e43ff7b9..44214a81 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -59,19 +59,14 @@ ANN void instantiate_object(const VM_Shred shred, const Type type) { ANN static void handle_dtor(const M_Object object, const VM_Shred shred) { const Type t = object->type_ref; const VM_Shred sh = new_vm_shred(shred->info->mp, t->nspc->dtor); - ADD_REF(t->nspc->dtor); sh->base = shred->base; - memcpy(sh->reg, shred->reg, SIZEOF_REG); *(M_Object*)sh->mem = object; vm_add_shred(shred->info->vm, sh); - ++sh->info->me->ref; } __attribute__((hot)) ANN void __release(const M_Object obj, const VM_Shred shred) { - // TODO: find what's going on here - // we should be able to use shred->info->mp directly - MemPool p = shred->info->mp= shred->info->vm->gwion->p; + MemPool p = shred->info->mp;// = shred->info->vm->gwion->p; Type t = obj->type_ref; while(t->parent) { struct scope_iter iter = { &t->nspc->info->value, 0, 0 };\ diff --git a/src/oo/env.c b/src/oo/env.c index 36391262..a535ad36 100644 --- a/src/oo/env.c +++ b/src/oo/env.c @@ -23,7 +23,7 @@ ANN static struct Env_Scope_ *new_scope(MemPool p) { vector_init(&a->class_stack); vector_init(&a->nspc_stack); vector_init(&a->known_ctx); - vector_init((Vector)&a->swi); + _scope_init(&a->swi); map_init(&a->swi.map); return a; } diff --git a/src/oo/switch.c b/src/oo/switch.c index cc5e0434..e1859a54 100644 --- a/src/oo/switch.c +++ b/src/oo/switch.c @@ -12,6 +12,11 @@ #include "context.h" #include "nspc.h" +static inline void _scope_add(Scope s, Switch sw) { vector_add((Vector)(void*)s, (vtype)sw); } +static inline vtype _scope_pop(Scope s) { return vector_pop((Vector)(void*)s); } +static inline vtype _scope_back(Scope s) { return vector_back((Vector)(void*)s); } +static inline void _scope_clear(Scope s) { vector_clear((Vector)(void*)s); } + static Switch new_switch(MemPool p) { Switch sw = mp_alloc(p, Switch); sw->cases = new_map(p); // could be struct ? @@ -60,14 +65,14 @@ ANN Switch swinfo_get(const Env env, const struct SwInfo_ *info) { ANN m_bool switch_add(const Env env, const Stmt_Switch stmt) { const struct SwInfo_ info = { stmt, env->class_def, env->func }; Switch sw = (Switch)swinfo_get(env, &info) ?: new_swinfo(env, stmt); - vector_add((Vector)&env->scope->swi, (vtype)sw); + _scope_add(&env->scope->swi, sw); return GW_OK; } ANN void switch_get(const Env env, const Stmt_Switch stmt) { const struct SwInfo_ info = { stmt, env->class_def, env->func }; const Switch sw = swinfo_get(env, &info); - vector_add((Vector)&env->scope->swi, (vtype)sw); + _scope_add(&env->scope->swi, sw); } void switch_reset(const Env env) { @@ -78,7 +83,7 @@ void switch_reset(const Env env) { free_map(env->gwion->p, sw->cases); free_switch(env->gwion->p, sw); } - vector_clear((Vector)&env->scope->swi); + _scope_clear(&env->scope->swi); map_clear(&env->scope->swi.map); } @@ -89,12 +94,12 @@ ANN void switch_release(const Scope sw) { } ANN void switch_expset(const Env env, const Exp e) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); vector_add(&sw->exp, (vtype)e); } ANN Exp switch_expget(const Env env) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); return (Exp)vector_at(&sw->exp, sw->iter++); } @@ -104,29 +109,29 @@ ANN m_bool switch_inside(const Env env, const uint pos) { return GW_OK; } ANN m_bool switch_dup(const Env env, const m_int value, const uint pos) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); if(map_get(sw->cases, (vtype)value)) ERR_B(pos, "duplicated cases value %i", value) return GW_OK; } ANN void switch_pc(const Env env, const m_uint pc) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); vector_add(sw->vec, pc); } ANN void switch_dynpc(const Env env, const m_int val, const m_uint pc) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); map_set(sw->cases, val, pc); } ANN m_bool switch_dyn(const Env env) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); return vector_size(&sw->exp); } ANN m_bool switch_default(const Env env, const m_uint pc, const uint pos) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); if(sw->default_case_index) ERR_B(pos, "default case already defined") sw->default_case_index = pc; @@ -134,26 +139,26 @@ ANN m_bool switch_default(const Env env, const m_uint pc, const uint pos) { } ANN Map switch_map(const Env env) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); return sw->cases; } ANN Vector switch_vec(const Env env) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); return vector_copy(env->gwion->p, sw->vec); // new_vector(); // dyn only } ANN m_uint switch_idx(const Env env) { - const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_back(&env->scope->swi); return sw->default_case_index; } ANN void switch_pop(const Env env) { - vector_pop((Vector)&env->scope->swi); + _scope_pop(&env->scope->swi); } ANN void switch_end(const Env env) { - const Switch sw = (Switch)vector_pop((Vector)&env->scope->swi); + const Switch sw = (Switch)_scope_pop(&env->scope->swi); const vtype index = VKEY(&env->scope->swi.map, VLEN(&env->scope->swi.map) - 1); map_remove(&env->scope->swi.map, index); free_switch(env->gwion->p, sw); diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 8a0c53a5..8dd975be 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -53,10 +53,10 @@ ANN m_bool scan1_exp_decl(const Env env, Exp_Decl* decl) { GWDEBUG_EXE CHECK_BB(env_access(env, decl->td->flag)) env_storage(env, &decl->td->flag); Var_Decl_List list = decl->list; - m_uint scope; ((Exp_Decl*)decl)->type = scan1_exp_decl_type(env, (Exp_Decl*)decl); CHECK_OB(decl->type) const m_bool global = GET_FLAG(decl->td, global); + m_uint scope = !global ? env->scope->depth : env_push_global(env); const Nspc nspc = !global ? env->curr : env->global_nspc; if(global) scope = env_push_global(env); diff --git a/src/vm/vm.c b/src/vm/vm.c index 9b6a9f25..560e2a83 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -86,6 +86,7 @@ ANN m_uint vm_add_shred(const VM* vm, const VM_Shred shred) { shredule(vm->shreduler, shred, .5); return shred->tick->xid; } + #include "gwion.h" ANN m_uint vm_fork(const VM* src, const VM_Shred shred) { VM* vm = shred->info->vm = gwion_cpy(src); @@ -232,8 +233,9 @@ __attribute__((hot)) *(m_int*)(reg + SZ_FLOAT-SZ_INT)); \ DISPATCH() -#define FI_R(op) \ +#define FI_R(op, ...) \ reg -= SZ_FLOAT; \ + __VA_ARGS__ \ *(m_int*)(reg-SZ_INT) = (**(m_int**)(reg+SZ_FLOAT -SZ_INT) op##= \ (m_int)(*(m_float*)(reg-SZ_INT))); \ DISPATCH() @@ -344,13 +346,25 @@ regpushmemfloat: DISPATCH(); regpushmemother: // LOOP_OPTIM - for(m_uint i = 0; i <= instr->m_val2; i+= SZ_INT) - *(m_uint*)(reg+i) = *(m_uint*)((mem + instr->m_val) + i); + for(m_uint i = 0; i <= instr->m_val2; i+= SZ_INT) { + m_uint* m0 = __builtin_assume_aligned((m_bit*)mem+i, SZ_INT); +printf("%p\n", m0); +// m_uint* m = __builtin_assume_aligned((m_bit*)mem+instr->m_val+i, SZ_INT); + m_uint* r = __builtin_assume_aligned(reg+i, SZ_INT); +// *(m_uint*)(reg+i) = *(m_uint*)((m_bit*)(mem + instr->m_val) + i); +// *(m_uint*)(r) = *(m_uint*)(m); + *(m_uint*)(r) = *(m_uint*)(m0+instr->m_val); + } reg += instr->m_val2; DISPATCH(); -regpushmemaddr: - *(m_bit**)reg = (mem + instr->m_val); +regpushmemaddr: { +m_bit* r0 __attribute__((aligned(SZ_INT))) = __builtin_assume_aligned(reg, SZ_INT); +m_bit* m0 __attribute__((aligned(SZ_INT))) = mem + instr->m_val; +// m_uint* r __attribute__((aligned(SZ_INT))) = r0;//__builtin_assume_aligned(reg, SZ_INT); +// *(m_bit**)reg = __builtin_assume_aligned(mem + instr->m_val, SZ_INT); + *(void**)r0 = m0; reg += SZ_INT; + } DISPATCH() pushnow: *(m_float*)reg = vm->bbq->pos; @@ -543,7 +557,7 @@ firassign: firadd: FI_R(+) firsub: FI_R(-) firmul: FI_R(*) -firdiv: FI_R(/) +firdiv: FI_R(/, TEST0(m_int, -SZ_INT)) itof: reg -= SZ_INT - SZ_FLOAT; diff --git a/util b/util index 30f6ebb7..df286d4a 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit 30f6ebb77737c1542c8eb677bf2b4d6acc0d166a +Subproject commit df286d4a0bf588e8b02e6864ea12382137f3accc -- 2.43.0