From e0acd11969b6524b11935b94d7e4332d19122f64 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 25 Apr 2022 15:13:07 +0200 Subject: [PATCH] :art: Add explicit captures --- ast | 2 +- include/env/func.h | 1 - include/env/type.h | 2 ++ include/env/value.h | 7 ++-- include/vm.h | 2 +- src/clean.c | 8 +++++ src/emit/emit.c | 53 +++++++++++++++-------------- src/env/func.c | 1 - src/lib/array.c | 7 ++-- src/lib/deep_equal.c | 4 +-- src/lib/lib_func.c | 32 ++++++++++++------ src/lib/object_op.c | 4 +-- src/lib/ref.c | 6 ++++ src/lib/string.c | 6 +--- src/parse/check.c | 79 +++++++++++++++++++++++++++++++++----------- src/parse/scan1.c | 8 +++-- src/vm/closure.c | 5 ++- 17 files changed, 144 insertions(+), 83 deletions(-) diff --git a/ast b/ast index f2438438..da676f0e 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit f2438438e053f26be2f768a01d0ac5fc2f96a7e6 +Subproject commit da676f0e85d9b0a6286a86f5385ff50ed3fd5fc5 diff --git a/include/env/func.h b/include/env/func.h index 286a4773..00ad9690 100644 --- a/include/env/func.h +++ b/include/env/func.h @@ -17,7 +17,6 @@ struct Func_ { Value value_ref; Func next; m_str name; - struct Map_ upvalues; float inline_mult; uint16_t weight; uint16_t memoize; diff --git a/include/env/type.h b/include/env/type.h index 6a3e1a3a..5a30a69f 100644 --- a/include/env/type.h +++ b/include/env/type.h @@ -114,6 +114,8 @@ ANN static inline Symbol miss_traits(const Type t, const Specialized *spec) { return NULL; } +ANN Type ref_type(const Gwion gwion, const Type t, const loc_t loc); + typedef enum { et_void, et_int, diff --git a/include/env/value.h b/include/env/value.h index 6c305aaf..8b93a5ca 100644 --- a/include/env/value.h +++ b/include/env/value.h @@ -17,10 +17,9 @@ enum vflag { vflag_direct = 1 << 4, vflag_builtin = 1 << 5, vflag_member = 1 << 6, - vflag_closed = 1 << 7, - vflag_inner = 1 << 8, // value is in a scope - vflag_release = 1 << 9, - vflag_assigned = 1 << 10 + vflag_inner = 1 << 7, // value is in a scope + vflag_release = 1 << 8, + vflag_assigned = 1 << 9 // vflag_used = 1 << 3 } __attribute__((packed)); diff --git a/include/vm.h b/include/vm.h index 515e2b67..c5d4ae22 100644 --- a/include/vm.h +++ b/include/vm.h @@ -2,9 +2,9 @@ #define __VM typedef struct Closure_ { - m_bit * data; struct Map_ m; m_uint sz; + m_bit data[]; } Closure; ANN Closure *new_closure(MemPool mp, const m_uint sz); ANN void free_closure(Closure *a, const Gwion gwion); diff --git a/src/clean.c b/src/clean.c index b8058983..2e04d5d5 100644 --- a/src/clean.c +++ b/src/clean.c @@ -275,8 +275,16 @@ ANN static void clean_func_base(Clean *a, Func_Base *b) { if (b->tmpl) clean_tmpl(a, b->tmpl); } +ANN static void clean_captures(Clean *a, Capture_List b) { + for(uint32_t i = 0; i < b->len; i++) { + const Capture *cap = mp_vector_at(b, Capture, i); + if(cap->v) value_remref(cap->v, a->gwion); + } +} + ANN static void clean_func_def(Clean *a, Func_Def b) { clean_func_base(a, b->base); + if(b->captures) clean_captures(a, b->captures); ++a->scope; if (!b->builtin && b->d.code && !(b->base->func && safe_vflag(b->base->func->value_ref, vflag_builtin))) diff --git a/src/emit/emit.c b/src/emit/emit.c index 5d5ce858..dc7d90c0 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -787,13 +787,14 @@ static const f_instr upvalue[] = {UpvalueInt, UpvalueFloat, UpvalueOther, UpvalueAddr}; ANN static m_bool emit_prim_id(const Emitter emit, const Symbol *data) { const Exp_Primary *prim = prim_self(data); - if (prim->value && emit->env->func && emit->env->func->upvalues.ptr) { - const Map map = &emit->env->func->upvalues; - for (m_uint i = 0; i < map_size(map); ++i) { - if (prim->value == (Value)((Exp_Primary *)VKEY(map, i))->value) { + if (prim->value && emit->env->func && emit->env->func->def->captures) { + const Capture_List caps = emit->env->func->def->captures; + for (uint32_t i = 0; i < caps->len; ++i) { + Capture *cap = mp_vector_at(caps, Capture, i); + if (!strcmp(prim->value->name, cap->v->name)) { const Instr instr = emit_kind(emit, prim->value->type->size, exp_getvar(exp_self(prim)), upvalue); - instr->m_val = i ? VVAL(map, i) : 0; + instr->m_val = cap->offset; return GW_OK; } } @@ -1227,10 +1228,7 @@ ANN static void emit_func_arg_vararg(const Emitter emit, } ANN static m_bool emit_func_args(const Emitter emit, const Exp_Call *exp_call) { - if (exp_call->args) { - CHECK_BB(emit_exp(emit, exp_call->args)); -// emit_exp_addref_array(emit, exp_call->args, -exp_totalsize(exp_call->args)); - } + if (exp_call->args) CHECK_BB(emit_exp(emit, exp_call->args)); const Type t = actual_type(emit->gwion, exp_call->func->type); if (is_func(emit->gwion, t) && fbflag(t->info->func->def->base, fbflag_variadic)) @@ -2084,26 +2082,33 @@ ANN static inline m_bool emit_prim_novar(const Emitter emit, } ANN static m_bool emit_upvalues(const Emitter emit, const Func func) { - const Map map = &func->upvalues; - for (m_uint i = 0; i < map_size(map); ++i) { - const Exp_Primary *prim = (Exp_Primary *)VKEY(map, i); - const Value v = prim->value; - CHECK_BB(emit_prim_novar(emit, prim)); - if (isa(prim->value->type, emit->gwion->type[et_compound]) > 0) { - if (vflag(v, vflag_fglobal) && !vflag(v, vflag_closed)) - emit_exp_addref1(emit, exp_self(prim), -v->type->size); - map_set(&func->code->closure->m, (vtype)v->type, VVAL(map, i)); + const Capture_List caps = func->def->captures; + for (uint32_t i = 0; i < caps->len; ++i) { + Capture *cap = mp_vector_at(caps, Capture, i); + const Value value = cap->v; + struct Exp_ exp = { + .d = { .prim = { + .d = { .var = cap->xid }, + .value = value, + .prim_type = ae_prim_id + }}, + .type = value->type, + .exp_type = ae_exp_primary, + .pos = cap->pos + }; + if(cap->is_ref) exp_setvar(&exp, true); + CHECK_BB(emit_exp(emit, &exp)); + if (isa(value->type, emit->gwion->type[et_compound]) > 0) { + emit_exp_addref1(emit, &exp, -value->type->size); + map_set(&func->code->closure->m, (vtype)value->type, cap->offset); } - set_vflag(v, vflag_closed); } return GW_OK; } ANN static m_bool emit_closure(const Emitter emit, const Func func) { - const Map map = &func->upvalues; - const m_uint sz = - VVAL(map, VLEN(map) - 1) + - ((Exp_Primary *)VKEY(map, VLEN(map) - 1))->value->type->size; + const Capture *cap = mp_vector_at(func->def->captures, Capture, (func->def->captures->len - 1)); + const m_uint sz = cap->offset + cap->v->type->size; func->code->closure = new_closure(emit->gwion->mp, sz); regpushi(emit, (m_uint)func->code->closure->data); CHECK_BB(emit_upvalues(emit, func)); @@ -2116,7 +2121,7 @@ ANN static m_bool emit_closure(const Emitter emit, const Func func) { ANN static m_bool emit_lambda(const Emitter emit, const Exp_Lambda *lambda) { CHECK_BB(emit_func_def(emit, lambda->def)); - if (lambda->def->base->func->upvalues.ptr) + if (lambda->def->captures) CHECK_BB(emit_closure(emit, lambda->def->base->func)); if (vflag(lambda->def->base->func->value_ref, vflag_member) && !exp_getvar(exp_self(lambda))) diff --git a/src/env/func.c b/src/env/func.c index 3dc20b34..4334b258 100644 --- a/src/env/func.c +++ b/src/env/func.c @@ -8,7 +8,6 @@ ANN void free_func(Func a, Gwion gwion) { if (fflag(a, fflag_tmpl)) func_def_cleaner(gwion, a->def); if (a->code) vmcode_remref(a->code, gwion); - if (a->upvalues.ptr) map_release(&a->upvalues); mp_free(gwion->mp, Func, a); } diff --git a/src/lib/array.c b/src/lib/array.c index bed713ec..3614edbe 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -662,7 +662,7 @@ static OP_CHECK(opck_array_scan) { env_set_error(env); return env->gwion->type[et_error]; } - if (!strncmp(base->name, "Ref:[", 5)) { + if (tflag(base, tflag_ref)) { gwerr_basic("Can't use ref types as array base", NULL, NULL, "/dev/null", (loc_t) {}, 0); env_set_error(env); @@ -772,10 +772,7 @@ static OP_CHECK(opck_array_each_val) { const Exp exp = (const Exp) data; DECL_ON(const Type, base, = foreach_type(env, exp)); CHECK_BN(ensure_traverse(env, base)); - const m_str basename = type2str(env->gwion, base, exp->pos); - char c[15 + strlen(basename)]; - sprintf(c, "Ref:[%s]", basename); - return str2type(env->gwion, c, exp->pos); + return ref_type(env->gwion, base, exp->pos); } static OP_EMIT(opem_array_each) { diff --git a/src/lib/deep_equal.c b/src/lib/deep_equal.c index 6d71f3d6..34fd7afe 100644 --- a/src/lib/deep_equal.c +++ b/src/lib/deep_equal.c @@ -142,9 +142,7 @@ struct DeepEmit { ANN static inline Type deep_type(const Gwion gwion, const Type t) { if(!tflag(t, tflag_struct)) return t; - char c[128]; - sprintf(c, "Ref:[%s]", t->name); - return str2type(gwion, c, t->info->value->from->loc); + return ref_type(gwion, t, t->info->value->from->loc); } ANN static void deep_emit_init(const Emitter emit, struct DeepEmit *d, const m_int offset) { diff --git a/src/lib/lib_func.c b/src/lib/lib_func.c index 0433c872..834b5634 100644 --- a/src/lib/lib_func.c +++ b/src/lib/lib_func.c @@ -225,19 +225,31 @@ ANN static Type fptr_type(const Env env, struct FptrInfo *info) { } ANN static m_bool _check_lambda(const Env env, Exp_Lambda *l, - const Func_Def def) { + const Func_Def fdef) { // if(l->def->base->func) return GW_OK; - Arg_List bases = def->base->args; + Arg_List bases = fdef->base->args; Arg_List args = l->def->base->args; // arity match if ((bases ? bases->len : 0) != (args ? args->len : 0)) ERR_B(exp_self(l)->pos, _("argument number does not match for lambda")) + if(fdef->captures) { + // here move to arguments + uint32_t offset = 0; + for(uint32_t i = 0; i < fdef->captures->len; i++) { + Capture *cap = mp_vector_at(fdef->captures, Capture, i); + const Value v = nspc_lookup_value1(env->curr, cap->xid); + if(!v) ERR_B(cap->pos, _("unknown value in capture")); + offset += (!cap->is_ref ? SZ_INT : v->type->size); + cap->v = v; + cap->offset = offset; + } + } const bool is_tmpl = - safe_tflag(def->base->func->value_ref->from->owner_class, tflag_tmpl); + safe_tflag(fdef->base->func->value_ref->from->owner_class, tflag_tmpl); if (is_tmpl) template_push_types( env, - def->base->func->value_ref->from->owner_class->info->cdef->base.tmpl); + fdef->base->func->value_ref->from->owner_class->info->cdef->base.tmpl); if(bases) { for(uint32_t i = 0; i < bases->len; i++) { Arg *base = mp_vector_at(bases, Arg, i); @@ -247,22 +259,22 @@ ANN static m_bool _check_lambda(const Env env, Exp_Lambda *l, } l->def->base->td = - type2td(env->gwion, known_type(env, def->base->td), exp_self(l)->pos); + type2td(env->gwion, known_type(env, fdef->base->td), exp_self(l)->pos); if (is_tmpl) nspc_pop_type(env->gwion->mp, env->curr); - l->def->base->flag = def->base->flag; + l->def->base->flag = fdef->base->flag; // if(GET_FLAG(def->base, global) && !l->owner && // def->base->func->value_ref->from->owner_class) UNSET_FLAG(l->def->base, global); l->def->base->values = env->curr->info->value; const m_uint scope = env->scope->depth; - if(GET_FLAG(def->base, global) && !l->owner && - def->base->func->value_ref->from->owner_class) + if(GET_FLAG(fdef->base, global) && !l->owner && + fdef->base->func->value_ref->from->owner_class) env_push(env, NULL, env->context->nspc); env->scope->depth = 0; const m_bool ret = traverse_func_def(env, l->def); env->scope->depth = scope; - if(GET_FLAG(def->base, global) && !l->owner && - def->base->func->value_ref->from->owner_class) + if(GET_FLAG(fdef->base, global) && !l->owner && + fdef->base->func->value_ref->from->owner_class) env_pop(env, scope); if (l->def->base->func) { diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 055aacb2..99aad2bd 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -239,9 +239,7 @@ OP_CHECK(opck_object_dot) { ANN static Type member_type(const Gwion gwion, const Type base) { const Type t = actual_type(gwion, base); - if(strncmp(t->name, "Ref:[", 5)) - return t; - return (Type)vector_front(&t->info->tuple->contains); + return !tflag(t, tflag_ref) ? t: (Type)vector_front(&t->info->tuple->contains); } OP_EMIT(opem_object_dot) { diff --git a/src/lib/ref.c b/src/lib/ref.c index 67a1b245..504e0644 100644 --- a/src/lib/ref.c +++ b/src/lib/ref.c @@ -17,6 +17,12 @@ #include "gwi.h" #include "tmpl_info.h" +ANN Type ref_type(const Gwion gwion, const Type t, const loc_t loc) { + char c[7 + strlen(t->name)]; + sprintf(c, "Ref:[%s]", t->name); + return str2type(gwion, c, loc); +} + static m_bool ref_access(const Env env, const Exp e) { const m_str access = exp_access(e); if (!access) return GW_OK; diff --git a/src/lib/string.c b/src/lib/string.c index 7d4c4138..0ef3a389 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -521,11 +521,7 @@ GWION_IMPORT(string) { gwi_func_ini(gwi, "float", "atof"); GWI_BB(gwi_func_end(gwi, string_atof, ae_flag_none)) -/* - gwi_func_ini(gwi, "int", "atoi"); - gwi_func_arg(gwi, "Ref:[int]", "idx"); - GWI_BB(gwi_func_end(gwi, string_atoi2, ae_flag_none)) -*/ + GWI_BB(gwi_class_end(gwi)) GWI_BB(gwi_oper_ini(gwi, "string", "string", "bool")) diff --git a/src/parse/check.c b/src/parse/check.c index fab2bc2e..81587e2e 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -331,23 +331,20 @@ static inline Nspc value_owner(const Env env, const Value v) { return v ? v->from->owner : env->curr; } -ANN static void check_upvalue(const Env env, const Exp_Primary *prim) { - const Value v = prim->value; - if (GET_FLAG(v, global) || vflag(v, vflag_fglobal) || +ANN bool not_upvalue(const Env env, const Value v) { + return GET_FLAG(v, global) || vflag(v, vflag_fglobal) || (v->from->owner_class && isa(v->from->owner_class, env->class_def) > 0) || - nspc_lookup_value1(env->curr, insert_symbol(v->name))) - return; - const Map map = &env->func->upvalues; - if (!map->ptr) { - map_init(map); - map_set(&env->func->upvalues, (vtype)prim, 0); - } else { - if (map_get(map, (vtype)v)) return; - const m_uint offset = - VVAL(map, VLEN(map) - 1) + - ((Exp_Primary *)VKEY(map, VLEN(map) - 1))->value->type->size; - map_set(&env->func->upvalues, (vtype)prim, offset); - } + nspc_lookup_value1(env->curr, insert_symbol(v->name)); +} + +ANN static m_bool check_upvalue(const Env env, const Exp_Primary *prim) { + const Value v = prim->value; + if(not_upvalue(env, v)) + return GW_OK; + gwerr_basic(_("value not in lambda scope"), NULL, NULL, env->name, exp_self(prim)->pos, 4242); + gwerr_warn("declared here", NULL, _("{-}try adding it to capture list{0}"), v->from->filename, v->from->loc); + env->context->error = true; + return GW_ERROR; } ANN static Type prim_owned(const Env env, const Symbol *data) { @@ -385,7 +382,7 @@ ANN static Type prim_id_non_res(const Env env, const Symbol *data) { if (!GET_FLAG(v, const) && v->from->owner) unset_fflag(env->func, fflag_pure); if (fbflag(env->func->def->base, fbflag_lambda)) - check_upvalue(env, prim_self(data)); + CHECK_BO(check_upvalue(env, prim_self(data))); } // set_vflag(v->vflag, vflag_used); return v->type; @@ -727,8 +724,49 @@ ANN static Type check_exp_call_template(const Env env, Exp_Call *exp) { func->def->base->ret_type : exp->func->d.exp_dot.base->type; } -ANN static Type check_lambda_call(const Env env, const Exp_Call *exp) { - if (exp->args) CHECK_OO(check_exp(env, exp->args)); +ANN static Type check_lambda_call(const Env env, Exp_Call *const exp) { + const Func_Def fdef = exp->func->d.exp_lambda.def; + if (exp->args) { + Exp e = exp->args; + CHECK_OO(check_exp(env, exp->args)); + do if(tflag(e->type, tflag_ref) && !safe_tflag(exp_self(e)->cast_to, tflag_ref)) + exp_setvar(e, true); + while((e = e->next)); + } + Exp _args = NULL, tmp; + if(fdef->captures) { + if(!fdef->base->args) + fdef->base->args = new_mp_vector(env->gwion->mp, sizeof(Arg), 0); + for(uint32_t i = 0; i < fdef->captures->len; i++) { + Capture *const cap = mp_vector_at(fdef->captures, Capture, i); + const Value v = nspc_lookup_value1(env->curr, cap->xid); + if(!v)exit(3); + if(cap->is_ref && not_upvalue(env, v)) + ERR_O(cap->pos, _("can't take ref of a scoped value")); + cap->v = v; + const Type base_type = !tflag(v->type, tflag_ref) ? v->type : (Type)vector_front(&v->type->info->tuple->contains); + const Type t = !cap->is_ref ? base_type : ref_type(env->gwion, base_type, cap->pos); + Arg arg = { + .td = type2td(env->gwion, t, exp->func->pos), + .var_decl = { .xid = cap->xid } + }; + mp_vector_add(env->gwion->mp, &fdef->base->args, Arg, arg); + const Exp exp = new_prim_id(env->gwion->mp, cap->xid, cap->pos); + if(_args) tmp = tmp->next = exp; + else _args = tmp = exp; + } + free_mp_vector(env->gwion->mp, sizeof(Capture), fdef->captures); + fdef->captures = NULL; + } + if(_args) { + if (exp->args) { + puts("here"); + Exp e = exp->args; + while(e->next) e = e->next; + e->next = _args; + } else exp->args = _args; + CHECK_OO(traverse_exp(env, _args)); + } Exp_Lambda *l = &exp->func->d.exp_lambda; Arg_List args = l->def->base->args; Exp e = exp->args; @@ -1077,6 +1115,7 @@ ANN m_bool check_type_def(const Env env, const Type_Def tdef) { const Exp ret_id = new_prim_id(env->gwion->mp, insert_symbol("self"), when->pos); ret_id->d.prim.value = new_value(env->gwion->mp, tdef->type, "self"); + // valuefrom? struct Stmt_ ret = { .stmt_type = ae_stmt_return, .d = { .stmt_exp = { .val = ret_id }}, .pos = when->pos @@ -1215,6 +1254,7 @@ ANN static m_bool do_stmt_each(const Env env, const Stmt_Each stmt) { DECL_OB(const Type, ret, = check_each_val(env, stmt->exp)); stmt->v = new_value(env->gwion->mp, ret, s_name(stmt->sym)); valid_value(env, stmt->sym, stmt->v); + valuefrom(env, stmt->v->from, stmt->vpos); return check_conts(env, stmt_self(stmt), stmt->body); } @@ -1330,6 +1370,7 @@ ANN static Value match_value(const Env env, const Type base, const Exp_Primary *prim) { const Symbol sym = prim->d.var; const Value v = new_value(env->gwion->mp, base, s_name(sym)); + // valuefrom? valid_value(env, sym, v); return v; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index c9e6783a..128519f1 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -239,6 +239,8 @@ ANN static inline m_bool scan1_exp_unary(const restrict Env env, const loc_t pos = exp_self(unary)->pos; const Symbol sym = lambda_name(env->gwion->st, pos.first); Exp lambda = new_exp_lambda(env->gwion->mp, sym, NULL, unary->code, pos); + lambda->d.exp_lambda.def->captures = unary->captures; + unary->captures = NULL; mp_free(env->gwion->mp, Stmt, unary->code); unary->exp = new_exp_call(env->gwion->mp, lambda, NULL, pos); unary->unary_type = unary_exp; @@ -394,10 +396,10 @@ ANN static Value arg_value(const Env env, Arg *const arg) { vd->xid ? s_name(vd->xid) : (m_str) __func__); if (vd->array) v->type = arg->type = array_type(env, arg->type, vd->array->depth); - if (arg->td) { + if (arg->td) v->flag = arg->td->flag; - // SET_FLAG(v, global); ??? - } + v->from->loc = arg->var_decl.pos; + v->from->filename = env->name; return v; } diff --git a/src/vm/closure.c b/src/vm/closure.c index 6d49a172..910e6331 100644 --- a/src/vm/closure.c +++ b/src/vm/closure.c @@ -11,8 +11,7 @@ #include "import.h" ANN Closure *new_closure(MemPool mp, const m_uint sz) { - Closure *a = mp_malloc(mp, Closure); - a->data = mp_malloc2(mp, sz); + Closure *a = mp_malloc2(mp, sizeof(Closure) + sz); map_init(&a->m); a->sz = sz; return a; @@ -24,5 +23,5 @@ ANN void free_closure(Closure *a, const Gwion gwion) { compound_release(gwion->vm->cleaner_shred, (Type)VKEY(m, i), a->data + VVAL(m, i)); map_release(m); - _mp_free(gwion->mp, a->sz, a->data); + _mp_free(gwion->mp, sizeof(Closure) + a->sz, a); } -- 2.43.0