From ed8a2e26a45a54bec9ee536bbfbca121935be216 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Sat, 30 Jul 2022 07:33:37 +0200 Subject: [PATCH] :art: Changes to builtin func, list types for variadic builtin --- include/env/func.h | 2 +- include/vm.h | 11 ++++++++--- src/emit/emit.c | 2 +- src/env/func.c | 14 ++++++++++++-- src/import/import_fdef.c | 2 +- src/lib/array.c | 4 ++-- src/lib/instr.c | 2 +- src/parse/func_resolve_tmpl.c | 2 +- src/parse/template.c | 2 +- 9 files changed, 28 insertions(+), 13 deletions(-) diff --git a/include/env/func.h b/include/env/func.h index 3316d789..7f3a7425 100644 --- a/include/env/func.h +++ b/include/env/func.h @@ -35,7 +35,7 @@ Symbol func_symbol(const Env, const m_str, const m_str, const m_str, const m_uint); ANN m_bool check_lambda(const Env, const Type, Exp_Lambda *); ANN Type check_op_call(const Env env, Exp_Call *const exp); -ANN void builtin_func(const MemPool mp, const Func f, void *func_ptr); +ANN void builtin_func(const Gwion gwion, const Func f, void *func_ptr); ANN static inline Value upvalues_lookup(const Upvalues *upvalues, const Symbol sym) { const Value v = (Value)scope_lookup1(upvalues->values, (m_uint)sym); diff --git a/include/vm.h b/include/vm.h index e57b036d..f66f431f 100644 --- a/include/vm.h +++ b/include/vm.h @@ -3,10 +3,15 @@ typedef struct VM_Code_ *VM_Code; struct VM_Code_ { - m_bit *bytecode; union { - struct Vector_ instr; - m_uint native_func; + struct { + m_bit *bytecode; + struct Vector_ instr; + }; + struct { + m_uint native_func; + MP_Vector *types; + }; }; Type ret_type; // could be `struct Vector_ tmpl_types;` void * memoize; diff --git a/src/emit/emit.c b/src/emit/emit.c index e3c7c897..b73e635c 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -2821,7 +2821,7 @@ ANN static m_bool _emit_func_def(const Emitter emit, const Func_Def f) { safe_tflag(emit->env->class_def, tflag_tmpl)) || (fdef->base->tmpl && !strcmp(s_name(f->base->xid), "new"))) { const Func base = nspc_lookup_func1(func->value_ref->from->owner, f->base->xid); - builtin_func(emit->gwion->mp, func, (f_xfun)base->code->native_func); + builtin_func(emit->gwion, func, (f_xfun)base->code->native_func); return GW_OK; } const uint global = GET_FLAG(f->base, global); diff --git a/src/env/func.c b/src/env/func.c index 4334b258..6e550e9d 100644 --- a/src/env/func.c +++ b/src/env/func.c @@ -34,9 +34,19 @@ Symbol func_symbol(const Env env, const m_str nspc, const m_str base, return insert_symbol(env->gwion->st, name); } -ANN void builtin_func(const MemPool mp, const Func f, void *func_ptr) { +ANN void builtin_func(const Gwion gwion, const Func f, void *func_ptr) { set_vflag(f->value_ref, vflag_builtin); - f->code = new_vmcode(mp, NULL, NULL, f->name, f->def->stack_depth, true, false); + f->code = new_vmcode(gwion->mp, NULL, NULL, f->name, f->def->stack_depth, true, false); f->code->native_func = (m_uint)func_ptr; f->code->ret_type = f->def->base->ret_type; + if(f->def->base->tmpl && f->def->base->tmpl->call) { + const Specialized *spec = mp_vector_at(f->def->base->tmpl->list, Specialized, f->def->base->tmpl->list->len - 1); + if(!strcmp(s_name(spec->xid), "...")) { + f->code->types = new_mp_vector(gwion->mp, Type_Decl*, f->def->base->tmpl->call->len); + for(uint32_t i = 0; i < f->def->base->tmpl->call->len; i++) { + Type_Decl *const td = *mp_vector_at(f->def->base->tmpl->call, Type_Decl*, i); + mp_vector_set(f->code->types, Type, i, known_type(gwion->env, td)); + } + } + } } diff --git a/src/import/import_fdef.c b/src/import/import_fdef.c index b45eb979..9e643b41 100644 --- a/src/import/import_fdef.c +++ b/src/import/import_fdef.c @@ -82,7 +82,7 @@ ANN m_int gwi_func_valid(const Gwi gwi, ImportCK *ck) { } if (traverse_func_def(gwi->gwion->env, fdef) < 0) return error_fdef(gwi, fdef); - builtin_func(gwi->gwion->mp, fdef->base->func, ck->addr); + builtin_func(gwi->gwion, fdef->base->func, ck->addr); return GW_OK; } diff --git a/src/lib/array.c b/src/lib/array.c index 9e73daa2..bfb6c87c 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -654,7 +654,7 @@ static MFUN(vm_vector_foldr) { #include "template.h" static void array_func(const Env env, const Type t, const m_str name, f_xfun fun) { const Value v = nspc_lookup_value0(t->nspc, insert_symbol(name)); - builtin_func(env->gwion->mp, v->d.func_ref, fun); + builtin_func(env->gwion, v->d.func_ref, fun); } static OP_CHECK(opck_array_scan) { @@ -712,7 +712,7 @@ static OP_CHECK(opck_array_scan) { ? !tflag(base, tflag_struct) ? vm_vector_rem_obj : vm_vector_rem_struct : vm_vector_rem; - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 0), rem); + builtin_func(env->gwion, (Func)vector_at(&t->nspc->vtable, 0), rem); void *insert = isa(base, env->gwion->type[et_compound]) > 0 ? !tflag(base, tflag_struct) ? vm_vector_insert_obj : vm_vector_insert_struct diff --git a/src/lib/instr.c b/src/lib/instr.c index 5e476494..328a57d5 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -27,7 +27,7 @@ ANN static Func_Def traverse_tmpl(const Emitter emit, Func_Def fdef, Func_Def fb if (vflag(v, vflag_builtin)) v->d.func_ref->def->d.dl_func_ptr = xfun; def->base->tmpl->call = cpy_type_list(env->gwion->mp, fbase->base->tmpl->call); CHECK_BO(traverse_dot_tmpl(emit, def, v)); - if (vflag(v, vflag_builtin)) builtin_func(emit->gwion->mp, def->base->func, xfun); + if (vflag(v, vflag_builtin)) builtin_func(emit->gwion, def->base->func, xfun); return def; } diff --git a/src/parse/func_resolve_tmpl.c b/src/parse/func_resolve_tmpl.c index 1978584e..23dccba0 100644 --- a/src/parse/func_resolve_tmpl.c +++ b/src/parse/func_resolve_tmpl.c @@ -114,7 +114,7 @@ ANN static Func create_tmpl(const Env env, struct ResolverArgs *ra, const Func func = ensure_tmpl(env, fdef, ra->e, ra->v->from->filename); if (func && func->def->builtin) { - builtin_func(env->gwion->mp, func, (void*)ra->v->d.func_ref->code->native_func); + builtin_func(env->gwion, func, (void*)ra->v->d.func_ref->code->native_func); set_vflag(func->value_ref, vflag_builtin); struct Op_Import opi = { .lhs = ra->v->d.func_ref->value_ref->type, .rhs = func->value_ref->type }; op_cpy(env, &opi); diff --git a/src/parse/template.c b/src/parse/template.c index a482747b..30262609 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -120,7 +120,7 @@ static ANN Type scan_func(const Env env, const Type t, const Type_Decl *td) { def->base->func = func; nspc_add_value_front(t->info->value->from->owner, sym, value); if (vflag(t->info->func->value_ref, vflag_builtin)) { - builtin_func(env->gwion->mp, func, func_ptr); + builtin_func(env->gwion, func, func_ptr); t->info->func->def->d.dl_func_ptr = func_ptr; } return ret; -- 2.43.0