From 901c649afdb10a8bb98b113588f72d1c6af960f0 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Sat, 11 May 2019 14:58:26 +0200 Subject: [PATCH] :art: Introduce GwionData :art: Introduce GwioData->mutex :art: Use Gwion->reserved :art: Put GwionData in its own file :shirt: Typedef GwionData --- include/gwion.h | 5 +++-- include/gwiondata.h | 13 +++++++++++++ include/import.h | 2 ++ src/compile.c | 7 ++++++- src/gwion.c | 10 +++++----- src/gwiondata.c | 17 +++++++++++++++++ src/lib/func.c | 1 + src/lib/import.c | 6 +++++- src/lib/object.c | 1 + src/lib/prim.c | 5 ++++- src/lib/shred.c | 9 +++++---- src/lib/vararg.c | 1 + src/parse/scan1.c | 3 +-- src/parse/type_utils.c | 16 ++++++++++------ src/vm/vm_code.c | 4 ++-- 15 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 include/gwiondata.h create mode 100644 src/gwiondata.c diff --git a/include/gwion.h b/include/gwion.h index 6bdc1070..18046403 100644 --- a/include/gwion.h +++ b/include/gwion.h @@ -4,16 +4,17 @@ typedef struct Gwion_* Gwion; #include "plug.h" #include "driver.h" +#include "gwiondata.h" struct Arg_; + struct Gwion_ { PlugInfo* plug; Env env; Emitter emit; VM* vm; - struct Map_ freearg; + struct GwionData_ *data; SymTable *st; MemPool mp; - struct Vector_ child; }; ANN m_bool gwion_ini(const Gwion, struct Arg_*); diff --git a/include/gwiondata.h b/include/gwiondata.h new file mode 100644 index 00000000..5136b21c --- /dev/null +++ b/include/gwiondata.h @@ -0,0 +1,13 @@ +#ifndef __GWIONDATA +#define __GWIONDATA + +typedef struct GwionData_ { + struct Map_ freearg; + MUTEX_TYPE mutex; + struct Vector_ child; + struct Vector_ reserved; +} GwionData; + +ANN GwionData* new_gwiondata(MemPool); +ANN void free_gwiondata(MemPool, GwionData*); +#endif diff --git a/include/import.h b/include/import.h index c4882418..3d1c3bb7 100644 --- a/include/import.h +++ b/include/import.h @@ -80,4 +80,6 @@ ANN Type_List str2tl(const Env env, const m_str s, m_uint *depth); #define FREEARG(a) ANN void a(Instr instr NUSED, void *gwion NUSED) typedef void (*f_freearg)(Instr, void*); ANN void register_freearg(const Gwi, const f_instr, void(*)(const Instr,void*)); +ANN void gwi_reserve(const Gwi, const m_str); + #endif diff --git a/src/compile.c b/src/compile.c index 053dae00..3b668e77 100644 --- a/src/compile.c +++ b/src/compile.c @@ -71,15 +71,19 @@ static inline m_bool compiler_open(struct Compiler* c) { static m_bool check(struct Gwion_* gwion, struct Compiler* c) { CHECK_BB(compiler_open(c)) struct ScannerArg_ arg = { c->name, c->file, gwion->st }; + MUTEX_LOCK(gwion->data->mutex); CHECK_OB((c->ast = parse(&arg))) gwion->env->name = c->name; - return type_engine_check_prog(gwion->env, c->ast); + const m_bool ret = type_engine_check_prog(gwion->env, c->ast); + MUTEX_UNLOCK(gwion->data->mutex); + return ret; } static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { VM_Shred shred = NULL; VM_Code code; compiler_name(gwion->mp, c); + MUTEX_LOCK(gwion->data->mutex); if(check(gwion, c) < 0 || !(code = emit_ast(gwion->emit, c->ast))) gw_err("while compiling file '%s'\n", c->base); @@ -88,6 +92,7 @@ static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { shred->info->args = c->args; vm_add_shred(gwion->vm, shred); } + MUTEX_UNLOCK(gwion->data->mutex); compiler_clean(gwion->mp, c); return shred ? shred->tick->xid : 0; } diff --git a/src/gwion.c b/src/gwion.c index 735d12fa..00fdd99e 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -51,7 +51,7 @@ ANN VM* gwion_cpy(const VM* src) { gwion->vm->bbq->si = soundinfo_cpy(src->gwion->mp, src->bbq->si); gwion->emit = src->gwion->emit; gwion->env = src->gwion->env; - gwion->freearg = src->gwion->freearg; + gwion->data = src->gwion->data; gwion->st = src->gwion->st; gwion->mp = src->gwion->mp; return gwion->vm; @@ -72,7 +72,7 @@ ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { arg_parse(arg); gwion->emit->memoize = arg->memoize; gwion->plug = new_plug(gwion->mp, &arg->lib); - map_init(&gwion->freearg); + gwion->data = new_gwiondata(gwion->mp); shreduler_set_loop(gwion->vm->shreduler, arg->loop); if(gwion_audio(gwion) > 0 && gwion_engine(gwion)) { gwion_compile(gwion, &arg->add); @@ -92,15 +92,15 @@ ANN void gwion_end(const Gwion gwion) { gwion->vm->cleaner_shred = new_vm_shred(gwion->mp, code); vm_add_shred(gwion->vm, gwion->vm->cleaner_shred); MUTEX_LOCK(gwion->vm->shreduler->mutex); - if(gwion->child.ptr) - fork_clean(gwion->vm, &gwion->child); + if(gwion->data->child.ptr) + fork_clean(gwion->vm, &gwion->data->child); MUTEX_UNLOCK(gwion->vm->shreduler->mutex); free_env(gwion->env); free_vm_shred(gwion->vm->cleaner_shred); free_emitter(gwion->emit); free_vm(gwion->vm); free_plug(gwion); - map_release(&gwion->freearg); + free_gwiondata(gwion->mp, gwion->data); free_symbols(gwion->st); mempool_end(gwion->mp); } diff --git a/src/gwiondata.c b/src/gwiondata.c new file mode 100644 index 00000000..653c8d9c --- /dev/null +++ b/src/gwiondata.c @@ -0,0 +1,17 @@ +#include "gwion_util.h" +#include "gwiondata.h" + +ANN GwionData* new_gwiondata(MemPool mp) { + struct GwionData_ *data = mp_alloc(mp, GwionData); + map_init(&data->freearg); + vector_init(&data->reserved); + MUTEX_SETUP(data->mutex); + return data; +} + +ANN void free_gwiondata(MemPool mp, GwionData *data) { + map_release(&data->freearg); + vector_release(&data->reserved); + MUTEX_CLEANUP(data->mutex); + mp_free(mp, GwionData, data); +} diff --git a/src/lib/func.c b/src/lib/func.c index 1b75547e..36adb8d6 100644 --- a/src/lib/func.c +++ b/src/lib/func.c @@ -212,5 +212,6 @@ GWION_IMPORT(func) { register_freearg(gwi, SporkIni, freearg_xork); register_freearg(gwi, ForkIni, freearg_xork); register_freearg(gwi, DotTmpl, freearg_dottmpl); + gwi_reserve(gwi, "__func__"); return GW_OK; } diff --git a/src/lib/import.c b/src/lib/import.c index 275a0f03..33957e31 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -672,5 +672,9 @@ ANN Type gwi_enum_end(const Gwi gwi) { } ANN void register_freearg(const Gwi gwi, const f_instr _exec, void(*_free)(const Instr, void*)) { - map_set(&gwi->gwion->freearg, (vtype)_exec, (vtype)_free); + map_set(&gwi->gwion->data->freearg, (vtype)_exec, (vtype)_free); +} + +ANN void gwi_reserve(const Gwi gwi, const m_str str) { + vector_add(&gwi->gwion->data->reserved, (vtype)insert_symbol(gwi->gwion->st, str)); } diff --git a/src/lib/object.c b/src/lib/object.c index 6c032173..80d64c9f 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -176,5 +176,6 @@ GWION_IMPORT(object) { CHECK_BB(gwi_oper_end(gwi, op_not, IntNot)) gwi_item_ini(gwi, "@null", "null"); gwi_item_end(gwi, 0, NULL); + gwi_reserve(gwi, "this"); return GW_OK; } diff --git a/src/lib/prim.c b/src/lib/prim.c index 087656ca..5b3119be 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -78,7 +78,9 @@ static GWION_IMPORT(int_values) { CHECK_BB(gwi_enum_add(gwi, "true", 1)) t_bool = gwi_enum_end(gwi); CHECK_BB(gwi_item_ini(gwi, "bool", "maybe")) - return gwi_item_end(gwi, 0, NULL); + CHECK_BB(gwi_item_end(gwi, 0, NULL)) + gwi_reserve(gwi, "maybe"); + return GW_OK; } static GWION_IMPORT(int) { @@ -121,6 +123,7 @@ static GWION_IMPORT(values) { gwi_item_end(gwi, ae_flag_const, t_zero); gwi_item_ini(gwi, "@now", "now"); gwi_item_end(gwi, ae_flag_const, NULL); + gwi_reserve(gwi, "now"); return GW_OK; } diff --git a/src/lib/shred.c b/src/lib/shred.c index 67a6dadf..48636387 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -176,7 +176,7 @@ static ANN void* fork_run(void* data) { fork_retval(me); MUTEX_LOCK(vm->shreduler->mutex); // MUTEX_LOCK(FORK_ORIG(me)->shreduler->mutex); - vector_rem2(&FORK_ORIG(me)->gwion->child, (vtype)me); + vector_rem2(&FORK_ORIG(me)->gwion->data->child, (vtype)me); // MUTEX_UNLOCK(FORK_ORIG(me)->shreduler->mutex); *(m_int*)(me->data + o_fork_done) = 1; broadcast(*(M_Object*)(me->data + o_fork_ev)); @@ -195,9 +195,9 @@ ANN void fork_clean(const VM *vm, const Vector v) { void fork_launch(const VM* vm, const M_Object o, const m_uint sz) { o->ref += 2; - if(!vm->gwion->child.ptr) - vector_init(&vm->gwion->child); - vector_add(&vm->gwion->child, (vtype)o); + if(!vm->gwion->data->child.ptr) + vector_init(&vm->gwion->data->child); + vector_add(&vm->gwion->data->child, (vtype)o); FORK_ORIG(o) = (VM*)vm; FORK_RETSIZE(o) = sz; THREAD_CREATE(FORK_THREAD(o), fork_run, o); @@ -291,5 +291,6 @@ GWION_IMPORT(shred) { CHECK_BB(gwi_func_end(gwi, 0)) CHECK_BB(gwi_class_end(gwi)) SET_FLAG((t_fork), abstract); + gwi_reserve(gwi, "me"); return GW_OK; } diff --git a/src/lib/vararg.c b/src/lib/vararg.c index 91a063a0..42444dd9 100644 --- a/src/lib/vararg.c +++ b/src/lib/vararg.c @@ -108,5 +108,6 @@ GWION_IMPORT(vararg) { CHECK_BB(gwi_oper_add(gwi, at_varobj)) CHECK_BB(gwi_oper_end(gwi, op_ref, VarargAssign)) register_freearg(gwi, VarargIni, freearg_vararg); + gwi_reserve(gwi, "vararg"); return GW_OK; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 1ace639d..67bbb679 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -56,8 +56,7 @@ ANN m_bool scan1_exp_decl(const Env env, const Exp_Decl* decl) { Type t = decl->type; const Var_Decl var = list->self; const Value former = nspc_lookup_value0(env->curr, var->xid); - if(!GET_FLAG(decl->td, builtin)) - CHECK_BB(isres(env, var->xid, exp_self(decl)->pos)) + CHECK_BB(isres(env, var->xid, exp_self(decl)->pos)) if(former && !decl->td->exp && (!env->class_def || !(GET_FLAG(env->class_def, template) || GET_FLAG(env->class_def, scan1)))) ERR_B(var->pos, "variable %s has already been defined in the same scope...", diff --git a/src/parse/type_utils.c b/src/parse/type_utils.c index 0f41cabe..69f240db 100644 --- a/src/parse/type_utils.c +++ b/src/parse/type_utils.c @@ -10,15 +10,19 @@ #include "parse.h" ANN m_bool isres(const Env env, const Symbol xid, const loc_t pos) { - const m_str s = s_name(xid); - if(!strcmp(s, "this") || - !strcmp(s, "me") || - !strcmp(s, "now") || - !strcmp(s, "maybe") || - !strcmp(s, "vararg") || +// const m_str s = s_name(xid); + if(vector_find(&env->gwion->data->reserved, (vtype)xid) > -1) + ERR_B(pos, "%s is reserved.", s_name(xid)); +/* + if(!strcmp(s, "this") || // + !strcmp(s, "me") || // + !strcmp(s, "now") || // + !strcmp(s, "maybe") || // + !strcmp(s, "vararg") || // !strcmp(s, "__func__") || !name2op(s)) ERR_B(pos, "%s is reserved.", s_name(xid)); +*/ return GW_OK; } diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index 32ba8168..95a4f8fe 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -17,8 +17,8 @@ ANN /*static*/ void free_code_instr(const Vector v, const Gwion gwion) { for(m_uint i = vector_size(v) + 1; --i;) { const Instr instr = (Instr)vector_at(v, i - 1); - const f_freearg f = (f_freearg)(map_get(&gwion->freearg, instr->opcode) ?: - map_get(&gwion->freearg, (vtype)instr->execute)); + const f_freearg f = (f_freearg)(map_get(&gwion->data->freearg, instr->opcode) ?: + map_get(&gwion->data->freearg, (vtype)instr->execute)); if(f) f(instr, gwion); mp_free(gwion->mp, Instr, instr); -- 2.43.0