From f6c8e576c7394f2f5871e0aad01c6a0f73798231 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Thu, 13 May 2021 23:21:55 +0200 Subject: [PATCH] :art: Updates and cleanups --- include/vm.h | 5 +++-- plug | 2 +- src/compile.c | 42 +++++++++++++++++++++++------------------- src/emit/emit.c | 10 ++++------ src/lib/shred.c | 6 +++--- src/vm/shreduler.c | 42 +++++++++++++++++------------------------- src/vm/vm_code.c | 9 +++++---- src/vm/vm_name.c | 2 +- src/vm/vm_shred.c | 10 ++++++---- 9 files changed, 63 insertions(+), 65 deletions(-) diff --git a/include/vm.h b/include/vm.h index ec62936c..a10821d7 100644 --- a/include/vm.h +++ b/include/vm.h @@ -61,12 +61,13 @@ typedef struct ShredDebugger_ { struct ShredInfo_ { VM* vm; - struct M_Object_* me; - Vector args; MemPool mp; + struct M_Object_* me; VM_Code orig; struct Vector_ frame; + struct Vector_ args; struct Vector_ line; + ShredDebugger *dbg; }; struct ShredTick_ { diff --git a/plug b/plug index 62ce1764..4587cd02 160000 --- a/plug +++ b/plug @@ -1 +1 @@ -Subproject commit 62ce17641d575fb1c0a16194234be257b4a6b85f +Subproject commit 4587cd028705ac2766c1897bb5d1e398b0b1de46 diff --git a/src/compile.c b/src/compile.c index 56f69059..0c9d6e50 100644 --- a/src/compile.c +++ b/src/compile.c @@ -21,28 +21,29 @@ struct Compiler { m_str data; FILE* file; Ast ast; - Vector args; + struct Vector_ args; enum compile_type type; }; -ANN static void compiler_name(MemPool p, struct Compiler* c) { +ANN static void compiler_name(struct Compiler* c) { m_str d = strdup(c->base); c->name = strsep(&d, ":"); if(d) - c->args = new_vector(p); + vector_init(&c->args); while(d) - vector_add(c->args, (vtype)strdup(strsep(&d, ":"))); + vector_add(&c->args, (vtype)strdup(strsep(&d, ":"))); free(d); } -ANN static inline void compiler_error(MemPool p, const struct Compiler* c) { - if(c->args) { - for(m_uint i = 0; i < vector_size(c->args); ++i) { - const m_str str = (m_str)vector_at(c->args, i); +ANN static inline void compiler_error(struct Compiler*const c) { + if(c->args.ptr) { + const Vector v = &c->args; + for(m_uint i = 0; i < vector_size(v); ++i) { + const m_str str = (m_str)vector_at(v, i); if(str) - xfree((m_str)vector_at(c->args, i)); + xfree((m_str)vector_at(v, i)); } - free_vector(p, c->args); + vector_release(v); } } @@ -84,7 +85,7 @@ ANN static m_bool is_reg(const m_str path) { } #endif -ANN static inline m_bool compiler_open(MemPool p, struct Compiler* c) { +ANN static inline m_bool compiler_open(struct Compiler* c) { char name[strlen(c->name) + 1]; strcpy(name, c->name); #ifndef __FUZZING__ @@ -94,7 +95,7 @@ ANN static inline m_bool compiler_open(MemPool p, struct Compiler* c) { } #endif if(_compiler_open(c) < 0) { - compiler_error(p, c); + compiler_error(c); gw_err(_("can't open '%s'\n"), name); return GW_ERROR; } @@ -120,8 +121,13 @@ ANN static inline m_bool passes(struct Gwion_* gwion, struct Compiler* c) { nspc_commit(env->curr); if(ret > 0 || env->context->global) vector_add(&env->scope->known_ctx, (vtype)ctx); - else //nspc_rollback(env->global_nspc); + else {//nspc_rollback(env->global_nspc); + if(!ctx->error) { + gw_err(_("{-}while compiling file `{0}{/}%s{-}`{0}\n"), c->base); + ctx->error = 1; + } context_remref(ctx, env->gwion); + } unload_context(ctx, env); return ret; } @@ -137,15 +143,13 @@ ANN static inline m_bool _check(struct Gwion_* gwion, struct Compiler* c) { } ANN static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { - if(compiler_open(gwion->mp, c) < 0) + if(compiler_open(c) < 0) return 0; - if(_check(gwion, c) < 0) { - gw_err(_("{-}while compiling file `{0}{/}%s{-}`{0}\n"), c->base); + if(_check(gwion, c) < 0) return 0; - } if(gwion->emit->info->code) { const VM_Shred shred = new_vm_shred(gwion->mp, gwion->emit->info->code); - shred->info->args = c->args; + shred->info->args.ptr = c->args.ptr; vm_add_shred(gwion->vm, shred); gwion->emit->info->code = NULL; return shred->tick->xid; @@ -154,7 +158,7 @@ ANN static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { } ANN static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { - compiler_name(gwion->mp, c); + compiler_name(c); MUTEX_LOCK(gwion->data->mutex); const m_uint ret = _compile(gwion, c); MUTEX_UNLOCK(gwion->data->mutex); diff --git a/src/emit/emit.c b/src/emit/emit.c index 29901afa..0a6737ae 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1027,19 +1027,17 @@ ANN static m_bool emit_exp_binary(const Emitter emit, const Exp_Binary* bin) { } ANN static m_bool emit_exp_cast(const Emitter emit, const Exp_Cast* cast) { - // no pos ? - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=cast->exp->type, .rhs=exp_self(cast)->type, - .data=(uintptr_t)cast }; CHECK_BB(emit_exp(emit, cast->exp)); + struct Op_Import opi = { .op=insert_symbol("$"), .lhs=cast->exp->type, .rhs=exp_self(cast)->type, + .data=(uintptr_t)cast }; // no pos ? (void)op_emit(emit, &opi); return GW_OK; } ANN static m_bool emit_exp_post(const Emitter emit, const Exp_Postfix* post) { - // no pos ? - struct Op_Import opi = { .op=post->op, .lhs=post->exp->type, - .data=(uintptr_t)post }; CHECK_BB(emit_exp(emit, post->exp)); + struct Op_Import opi = { .op=post->op, .lhs=post->exp->type, + .data=(uintptr_t)post }; // no pos ? return op_emit(emit, &opi); } diff --git a/src/lib/shred.c b/src/lib/shred.c index 756d6e70..875ab6a6 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -99,14 +99,14 @@ static SFUN(vm_shred_from_id) { static MFUN(shred_args) { const VM_Shred s = ME(o); - *(m_uint*)RETURN = s->info->args ? vector_size(s->info->args) : 0; + *(m_uint*)RETURN = s->info->args.ptr ? vector_size(&s->info->args) : 0; } static MFUN(shred_arg) { const VM_Shred s = ME(o); const m_int idx = *(m_int*)MEM(SZ_INT); - if(s->info->args && idx >= 0) { - const m_str str = (m_str)vector_at(s->info->args, *(m_uint*)MEM(SZ_INT)); + if(s->info->args.ptr && idx >= 0) { + const m_str str = (m_str)vector_at(&s->info->args, *(m_uint*)MEM(SZ_INT)); *(M_Object*)RETURN = str ? new_string(shred->info->mp, shred, str) : NULL; } else *(m_uint*)RETURN = 0; diff --git a/src/vm/shreduler.c b/src/vm/shreduler.c index 31f5153a..db051de9 100644 --- a/src/vm/shreduler.c +++ b/src/vm/shreduler.c @@ -15,30 +15,21 @@ ANN void shreduler_set_loop(const Shreduler s, const bool loop) { ANN VM_Shred shreduler_get(const Shreduler s) { Driver *const bbq = s->bbq; struct ShredTick_ *const tk = s->list; - if(!tk) { - if(!vector_size(&s->shreds) && !s->loop) - bbq->is_running = 0; - return NULL; - } - const m_float time = (m_float)bbq->pos + (m_float)GWION_EPSILON; - if(tk->wake_time <= time) { - if((s->list = tk->next)) - s->list->prev = NULL; - tk->next = tk->prev = NULL; - s->curr = tk; - return tk->self; + if(tk) { + const m_float time = (m_float)bbq->pos + (m_float)GWION_EPSILON; + if(tk->wake_time <= time) { + if((s->list = tk->next)) + s->list->prev = NULL; + tk->next = tk->prev = NULL; + s->curr = tk; + return tk->self; + } } + if(!s->loop && !vector_size(&s->shreds)) + bbq->is_running = 0; return NULL; } -ANN static void shreduler_parent(const VM_Shred out, const Vector v) { - vector_rem2(v, (vtype)out); - if(!vector_size(v)) { - vector_release(v); - out->tick->parent->child.ptr = NULL; - } -} - ANN static inline void shreduler_child(const Vector v) { for(m_uint i = vector_size(v) + 1; --i;) { const VM_Shred child = (VM_Shred)vector_at(v, i - 1); @@ -46,9 +37,9 @@ ANN static inline void shreduler_child(const Vector v) { } } -ANN static void shreduler_erase(const Shreduler s, struct ShredTick_ *tk) { +ANN static void shreduler_erase(const Shreduler s, struct ShredTick_ *const tk) { if(tk->parent) - shreduler_parent(tk->self, &tk->parent->child); + vector_rem2(&tk->parent->child, (vtype)tk->self); if(tk->child.ptr) shreduler_child(&tk->child); vector_rem2(&s->shreds, (vtype)tk->self); @@ -56,7 +47,7 @@ ANN static void shreduler_erase(const Shreduler s, struct ShredTick_ *tk) { ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const bool erase) { MUTEX_LOCK(s->mutex); - struct ShredTick_ *tk = out->tick; + struct ShredTick_ *const tk = out->tick; if(tk == s->curr) s->curr = NULL; else if(tk == s->list) @@ -65,8 +56,9 @@ ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const bool eras tk->prev->next = tk->next; if(tk->next) tk->next->prev = tk->prev; - tk->prev = tk->next = NULL; - if(erase) { + if(!erase) + tk->prev = tk->next = NULL; + else { shreduler_erase(s, tk); _release(out->info->me, out); } diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index 8b176601..453bc295 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -48,14 +48,14 @@ static inline uint isgoto(const unsigned opcode) { opcode == eHandleEffect; } -static inline void setpc(const m_bit *data, const m_uint i) { +ANN static inline void setpc(const m_bit *data, const m_uint i) { *(unsigned*)(data+1) = i + 1; } ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { const Vector v = &code->instr; const m_uint sz = vector_size(v); - m_bit *ptr = _mp_malloc(p, sz * BYTECODE_SZ); + m_bit *const ptr = _mp_malloc(p, sz * BYTECODE_SZ); struct Vector_ nop; vector_init(&nop); for(m_uint i= 0; i < sz; ++i) { @@ -122,7 +122,7 @@ ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { m_bit *const final = _mp_malloc(p, sz * BYTECODE_SZ); // could use smaller size for(m_uint i= 0, j = 0; i < sz; ++i) { const Instr instr = (Instr)vector_at(v, i); - unsigned opcode = instr->opcode; + const unsigned opcode = instr->opcode; if(opcode != eNoOp) { m_bit *const base = ptr + i*BYTECODE_SZ, *const data = final + j*BYTECODE_SZ; @@ -172,7 +172,8 @@ VM_Code vmcode_callback(MemPool mp, VM_Code base) { sprintf(name, "%s(callback)", base->name); const Instr instr = (Instr)vector_back(&base->instr); instr->opcode = eEOC; - VM_Code code = new_vmcode(mp, &base->instr, base->stack_depth, base->builtin, name); + const VM_Code code = new_vmcode(mp, &base->instr, + base->stack_depth, base->builtin, name); code->closure = base->closure; code->callback = 1; instr->opcode = eFuncReturn; diff --git a/src/vm/vm_name.c b/src/vm/vm_name.c index aaca8e9f..d80d22d9 100644 --- a/src/vm/vm_name.c +++ b/src/vm/vm_name.c @@ -3,7 +3,7 @@ m_str code_name_set(MemPool p, const m_str name, const m_str file) { const size_t len = strlen(name) + strlen(file) + 2; - m_str str = (const m_str)_mp_malloc(p, len); + const m_str str = (const m_str)_mp_malloc(p, len); sprintf(str, "%s$%s", name, file); return str; } diff --git a/src/vm/vm_shred.c b/src/vm/vm_shred.c index b81a4e54..c98a0d56 100644 --- a/src/vm/vm_shred.c +++ b/src/vm/vm_shred.c @@ -12,19 +12,19 @@ struct Stack_ { }; static inline struct ShredInfo_ *new_shredinfo(MemPool p, const VM_Code c) { - struct ShredInfo_ *info = mp_calloc(p, ShredInfo); + struct ShredInfo_ *const info = mp_calloc(p, ShredInfo); info->mp = p; info->orig = c; return info; } static inline void free_shredinfo(MemPool mp, struct ShredInfo_ *info) { - if(info->args) { // could be a struct - const Vector v = info->args; + if(info->args.ptr) { + const Vector v = &info->args; LOOP_OPTIM for(m_uint i = vector_size(v) + 1; --i;) xfree((void*)vector_at(v, i - 1)); - free_vector(mp, v); + vector_release(v); } if(info->line.ptr) vector_release(&info->line); @@ -47,6 +47,8 @@ void free_vm_shred(VM_Shred shred) { vector_release(&shred->gc); if(shred->info->frame.ptr) vector_release(&shred->info->frame); + if(shred->tick->child.ptr) + vector_release(&shred->tick->child); vmcode_remref(shred->info->orig, shred->info->vm->gwion); const MemPool mp = shred->info->mp; mp_free(mp, ShredTick, shred->tick); -- 2.43.0