-Subproject commit f2438438e053f26be2f768a01d0ac5fc2f96a7e6
+Subproject commit da676f0e85d9b0a6286a86f5385ff50ed3fd5fc5
Value value_ref;
Func next;
m_str name;
- struct Map_ upvalues;
float inline_mult;
uint16_t weight;
uint16_t memoize;
return NULL;
}
+ANN Type ref_type(const Gwion gwion, const Type t, const loc_t loc);
+
typedef enum {
et_void,
et_int,
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));
#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);
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)))
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;
}
}
}
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))
}
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));
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)))
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);
}
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);
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) {
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) {
}
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);
}
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) {
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) {
#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;
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"))
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) {
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;
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;
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
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);
}
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;
}
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;
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;
}
#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;
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);
}