From 5ba64d93ac92121e710086e715cc7aab3366932b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Tue, 8 Dec 2020 17:55:45 +0100 Subject: [PATCH] :art: Improve globals --- src/emit/emit.c | 10 +++++++--- src/parse/check.c | 14 +++++++++++--- src/parse/scan1.c | 4 ++++ src/parse/scan2.c | 10 +++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index 1e2f334d..973bdbfc 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -747,7 +747,7 @@ ANN static m_bool emit_exp_decl_global(const Emitter emit, const Var_Decl var_de push->m_val = -(missing_depth) * SZ_INT; } assign->m_val = emit_var; -// (void)emit_addref(emit, emit_var); + (void)emit_addref(emit, emit_var); } else if(struct_ctor(v)) emit_struct_decl_finish(emit, v->type, emit_addr); return GW_OK; @@ -1920,7 +1920,7 @@ ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot* member) { return op_emit_bool(emit, &opi); } -ANN static inline void emit_func_def_global(const Emitter emit, const Value value) { +ANN static inline void emit_func_def_fglobal(const Emitter emit, const Value value) { const Instr set_mem = emit_add_instr(emit, MemSetImm); set_mem->m_val = value->from->offset; set_mem->m_val2 = (m_uint)value->d.func_ref->code; @@ -2070,7 +2070,7 @@ ANN static void emit_fdef_finish(const Emitter emit, const Func_Def fdef) { const Func func = fdef->base->func; func->code = emit_func_def_code(emit, func); if(fdef_is_file_global(emit, fdef)) - emit_func_def_global(emit, func->value_ref); + emit_func_def_fglobal(emit, func->value_ref); if(emit->info->memoize && fflag(func, fflag_pure)) func->code->memoize = memoize_ini(emit, func); } @@ -2085,6 +2085,8 @@ ANN static m_bool emit_func_def(const Emitter emit, const Func_Def f) { return GW_OK; if(fdef_is_file_global(emit, fdef)) func->value_ref->from->offset = emit_local(emit, emit->gwion->type[et_int]); + const uint global = GET_FLAG(f->base, global); + const m_uint scope = !global ? emit->env->scope->depth : env_push_global(emit->env); emit_func_def_init(emit, func); if(vflag(func->value_ref, vflag_member)) stack_alloc(emit); @@ -2102,6 +2104,8 @@ ANN static m_bool emit_func_def(const Emitter emit, const Func_Def f) { emit_fdef_finish(emit, fdef); else emit_pop_code(emit); + if(global) + env_pop(emit->env, scope); return ret; } diff --git a/src/parse/check.c b/src/parse/check.c index d6e0a5d2..361078af 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -440,8 +440,11 @@ ANN static m_bool func_match_inner(const Env env, const Exp e, const Type t, exp_setvar(e, 1); return check_lambda(env, t, &e->d.exp_lambda); } - if(implicit) - return check_implicit(env, e, t); + if(implicit) { + const m_bool ret = check_implicit(env, e, t); + if(ret == GW_OK) + return ret; + } } return match ? 1 : -1; } @@ -468,7 +471,12 @@ ANN2(1,2) static Func find_func_match_actual(const Env env, Func func, const Exp nspc_pop_type(env->gwion->mp, env->curr); CHECK_OO(e1->type) } - if(func_match_inner(env, e, e1->type, implicit, specific) < 0) + if(!func->def->base->tmpl && func->next) + env->context->error = 1; + const m_bool ret = func_match_inner(env, e, e1->type, implicit, specific); + if(func->next) + env->context->error = ret < 0; + if(ret < 0) break; e = e->next; e1 = e1->next; diff --git a/src/parse/scan1.c b/src/parse/scan1.c index b1a213ea..4ba9110e 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -562,6 +562,8 @@ ANN static inline m_bool scan1_fdef_defined(const Env env, const Func_Def fdef) } ANN m_bool scan1_func_def(const Env env, const Func_Def fdef) { + const uint global = GET_FLAG(fdef->base, global); + const m_uint scope = !global ? env->scope->depth : env_push_global(env); if(fdef->base->td) CHECK_BB(env_storage(env, fdef->base->flag, td_pos(fdef->base->td))) CHECK_BB(scan1_fdef_defined(env, fdef)) @@ -573,6 +575,8 @@ ANN m_bool scan1_func_def(const Env env, const Func_Def fdef) { const m_bool ret = scanx_fdef(env, env, fdef, (_exp_func)scan1_fdef); --env->scope->depth; env->func = former; + if(global) + env_pop(env, scope); return ret; } diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 4c17b75a..7f34c294 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -47,10 +47,13 @@ ANN m_bool scan2_exp_decl(const Env env, const Exp_Decl* decl) { ANN static m_bool scan2_args(const Func_Def f) { Arg_List list = f->base->args; + const uint global = GET_FLAG(f->base, global); do { const Value v = list->var_decl->value; v->from->offset = f->stack_depth; f->stack_depth += v->type->size; + if(global) + SET_FLAG(v, global); } while((list = list->next)); return GW_OK; } @@ -60,9 +63,10 @@ ANN static Value scan2_func_assign(const Env env, const Func_Def d, valuefrom(env, v->from); SET_FLAG(v, const); set_vflag(v, vflag_func); - if(!env->class_def) - set_vflag(v, vflag_fglobal); - else { + if(!env->class_def) { + if(!GET_FLAG(d->base, global)) + set_vflag(v, vflag_fglobal); + } else { if(GET_FLAG(d->base, static)) SET_FLAG(v, static); else -- 2.43.0