From: fennecdjay Date: Sun, 23 Oct 2022 23:03:18 +0000 (+0200) Subject: :art: convenience emit functio X-Git-Tag: nightly~207^2~133 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=59aed7b9e97ac54111e7f393aa34a707ca28e1e4;p=gwion.git :art: convenience emit functio --- diff --git a/include/emit.h b/include/emit.h index 32fdd692..12c95058 100644 --- a/include/emit.h +++ b/include/emit.h @@ -116,4 +116,10 @@ ANN void emit_pop_scope(const Emitter emit); ANN m_bool ensure_emit(const Emitter, const Type); ANN m_bool emit_ensure_func(const Emitter emit, const Func f); ANN m_bool get_emit_var(const Emitter emit, const Type t, bool is_var); + +ANN static inline void emit_regmove(const Emitter emit, const m_uint i) { + const Instr instr = emit_add_instr(emit, RegMove); + instr->m_val = i; +} + #endif diff --git a/src/emit/emit.c b/src/emit/emit.c index a3d78ade..28df8a44 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -389,12 +389,12 @@ ANN static m_bool emit_pre_constructor_array(const Emitter emit, if (!tflag(type, tflag_struct)) emit_add_instr(emit, ArrayBottom); else - regpop(emit, SZ_INT); - regpop(emit, SZ_INT); + emit_regmove(emit, -SZ_INT); + emit_regmove(emit, -SZ_INT); const Instr pc = emit_add_instr(emit, Goto); pc->m_val = start_index; top->m_val = emit_code_size(emit); - regpop(emit, SZ_INT * 3); + emit_regmove(emit, -SZ_INT*3); emit_add_instr(emit, ArrayPost); return GW_OK; } @@ -635,7 +635,7 @@ ANN static inline m_bool emit_exp1(const Emitter emit, const Exp e) { ANN static m_bool emit_prim_array_exp(const Emitter emit, const Type t, Exp e) { do { CHECK_BB(emit_exp1(emit, e)); - regpop(emit, t->size - t->actual_size); + emit_regmove(emit, - t->size + t->actual_size); } while((e = e->next)); return GW_OK; } @@ -734,7 +734,7 @@ ANN static m_bool emit_prim_dict(const Emitter emit, Exp *data) { init->m_val = int2pow2(count); init->m_val2 = (m_uint)t; const m_uint sz = (key->size + val->size + SZ_INT) * count; - regpop(emit, sz); + emit_regmove(emit, -sz); const Instr ctor = emit_add_instr(emit, dict_lit_ctor); ctor->m_val = sz; ctor->m_val2 = count; @@ -924,7 +924,7 @@ ANN static m_bool emit_prim_locale(const Emitter emit, const Symbol *id) { regpushi(emit, (m_uint)string); regpushi(emit, (m_uint)emit->locale->code); emit_exp_call1(emit, emit->locale, true); - regpop(emit, emit->locale->def->base->ret_type->size); + emit_regmove(emit, -emit->locale->def->base->ret_type->size); const VM_Code code = finalyze(emit, EOC); const VM_Shred shred = new_vm_shred(emit->gwion->mp, code); vm_add_shred(emit->gwion->vm, shred); @@ -962,7 +962,7 @@ ANN static m_bool _decl_static(const Emitter emit, const Exp_Decl *decl, emit_add_instr(emit, Assign); // if(get_depth(var_decl->value->type) && !is_ref) // (void)emit_object_addref(emit, -SZ_INT, 0); - regpop(emit, SZ_INT); + emit_regmove(emit, -SZ_INT); return GW_OK; } @@ -1077,7 +1077,7 @@ ANN static m_bool emit_exp_decl_non_static(const Emitter emit, if (!emit_var) emit_add_instr(emit, Assign); else { - regpop(emit, SZ_INT); + emit_regmove(emit, -SZ_INT); const Instr instr = emit_add_instr(emit, Reg2Reg); instr->m_val = -SZ_INT; } @@ -1275,7 +1275,7 @@ ANN static inline void inline_args_ini(const Emitter emit, const Func f, value->from->offset = emit_local(emit, value->type); nspc_add_value(emit->env->curr, arg->var_decl.xid, value); } - regpop(emit, f->code->stack_depth); + emit_regmove(emit, -f->code->stack_depth); const Instr cpy = emit_add_instr(emit, Reg2Mem4); cpy->m_val2 = f->code->stack_depth; cpy->m_val = start_offset; @@ -1375,7 +1375,7 @@ ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { CHECK_BB(_emit_exp_call(emit, exp_call)); const Type t = exp_self(exp_call)->type; if (exp_getvar(e)) { - regpop(emit, exp_self(exp_call)->type->size); + emit_regmove(emit, -exp_self(exp_call)->type->size); if(isa(t, emit->gwion->type[et_object]) > 0) emit_local_exp(emit, e); const Instr instr = emit_add_instr(emit, Reg2RegAddr); @@ -1383,7 +1383,7 @@ ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { } else { if (!is_func(emit->gwion, exp_call->func->type) && // is_callable tflag(e->type, tflag_struct)) - regpop(emit, SZ_INT); + emit_regmove(emit, -SZ_INT); } // if(isa(e->type, emit->gwion->type[et_object]) > 0) // emit_local_exp(emit, e); @@ -1408,7 +1408,7 @@ ANN static m_uint pop_exp_size(Exp e) { ANN static inline void pop_exp(const Emitter emit, Exp e) { const m_uint size = pop_exp_size(e); - if (size) regpop(emit, size); + if (size) emit_regmove(emit, -size); } ANN static inline m_bool emit_exp_pop_next(const Emitter emit, Exp e) { @@ -1681,7 +1681,7 @@ ANN m_bool emit_exp_call1(const Emitter emit, const Func f, } ANN static void emit_exp_spork_finish(const Emitter emit, const m_uint depth) { - regpop(emit, depth); + emit_regmove(emit, -depth); const Instr spork = emit_add_instr(emit, SporkFunc); spork->m_val = depth + SZ_INT; spork->m_val2 = -SZ_INT; @@ -1824,7 +1824,7 @@ ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary *unary) { } } if(offset) { - regpop(emit, offset); + regpop(emit, offset);// emit_regmove(emit, offset); const Instr args = emit_add_instr(emit, SporkCode); args->m_val = offset; } @@ -2042,7 +2042,7 @@ ANN static m_bool optimize_tail_call(const Emitter emit, const Exp_Call *e) { if (e->args) { emit_func_args(emit, e); const Func f = e->func->type->info->func; - regpop(emit, f->def->stack_depth); + emit_regmove(emit, -f->def->stack_depth); emit_args(emit, f); } emit_add_instr(emit, Goto); @@ -2319,7 +2319,7 @@ ANN static m_bool emit_stmt_each(const Emitter emit, const Stmt_Each stmt) { nspc_push_value(emit->gwion->mp, emit->env->curr); CHECK_BB(emit_exp(emit, stmt->exp)); // add ref? - regpop(emit, SZ_INT); + emit_regmove(emit, -SZ_INT); if (n) { unroll_init(emit, n); @@ -2365,7 +2365,7 @@ ANN static m_bool _emit_stmt_loop(const Emitter emit, const Stmt_Loop stmt, idx->m_val2 = -1; } CHECK_BB(emit_exp_pop_next(emit, stmt->cond)); - regpop(emit, SZ_INT); + emit_regmove(emit, -SZ_INT); const Instr tomem = emit_add_instr(emit, Reg2Mem); tomem->m_val = offset + (!stmt->idx ? 0 : SZ_INT); *index = emit_code_size(emit); @@ -2520,7 +2520,7 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, if (!nspc_lookup_value1(emit->env->curr, e->d.prim.d.var)) { if (!n) { CHECK_BO(emit_exp(emit, base)); - regpop(emit, base->type->size); + emit_regmove(emit, -base->type->size); } CHECK_BO(case_value(emit, base, e)); return CASE_PASS; @@ -2545,7 +2545,7 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, const Instr instr = emit_unionmember(emit, v->type->size, false /*emit_addr*/); instr->m_val = i; - regpop(emit, v->type->size); + emit_regmove(emit, -v->type->size); case_op(emit, e->d.exp_call.args, e->d.exp_call.args, vec, i + 1); return CASE_PASS; } @@ -2725,7 +2725,7 @@ ANN static VM_Code emit_internal(const Emitter emit, const Func f) { vmcode_addref(f->code); return f->code; } else if (f->def->base->xid == insert_symbol("@gack")) { - regpop(emit, SZ_INT + f->value_ref->from->owner_class->size); + emit_regmove(emit, -SZ_INT - f->value_ref->from->owner_class->size); const Instr instr = emit_add_instr(emit, RegPushMem); instr->m_val = SZ_INT; f->code = finalyze(emit, FuncReturn); @@ -2788,7 +2788,7 @@ ANN static void me_bottom(MemoizeEmitter *me, const m_uint pc) { const Instr idx = emit_add_instr(me->emit, RegPushMem4); idx->m_val = me->offset; emit_add_instr(me->emit, int_pre_inc); - regpop(me->emit, SZ_INT); + emit_regmove(me->emit, -SZ_INT); const Instr loop = emit_add_instr(me->emit, Goto); loop->m_val = pc; } diff --git a/src/lib/array.c b/src/lib/array.c index eb7b6f67..bb4ea30c 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -161,8 +161,7 @@ static OP_CHECK(opck_array_sr) { ANN static inline m_bool emit_array_shift(const Emitter emit, const f_instr exec) { - const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); (void)emit_add_instr(emit, exec); return GW_OK; } @@ -212,8 +211,7 @@ static OP_EMIT(opem_array_sr) { const Exp_Binary *bin = (Exp_Binary *)data; if (shift_match(bin->lhs->type, bin->rhs->type)) return emit_array_shift(emit, ArrayConcatRight); - const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); if (isa(bin->lhs->type, emit->gwion->type[et_compound]) > 0) emit_compound_addref(emit, bin->lhs->type, -SZ_INT*2, false); (void)emit_add_instr(emit, ArrayAppendFront); @@ -226,8 +224,7 @@ static OP_EMIT(opem_array_sl) { return emit_array_shift(emit, ArrayConcatLeft); if (isa(bin->rhs->type, emit->gwion->type[et_compound]) > 0) emit_compound_addref(emit, bin->rhs->type, -SZ_INT, false); - const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -bin->rhs->type->size; + emit_regmove(emit, -bin->rhs->type->size); emit_add_instr(emit, ArrayAppend); return GW_OK; } @@ -310,8 +307,7 @@ static OP_CHECK(opck_array) { } ANN static void array_loop(const Emitter emit, const m_uint depth) { - const Instr pre_pop = emit_add_instr(emit, RegMove); - pre_pop->m_val = -depth * SZ_INT; + emit_regmove(emit, -depth * SZ_INT); for (m_uint i = 0; i < depth - 1; ++i) { const Instr access = emit_add_instr(emit, ArrayAccess); access->m_val = i * SZ_INT; @@ -322,8 +318,7 @@ ANN static void array_loop(const Emitter emit, const m_uint depth) { const Instr ex = emit_add_instr(emit, GWOP_EXCEPT); ex->m_val = -SZ_INT; } - const Instr post_pop = emit_add_instr(emit, RegMove); - post_pop->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); const Instr access = emit_add_instr(emit, ArrayAccess); access->m_val = depth * SZ_INT; } @@ -336,8 +331,7 @@ ANN static void array_finish(const Emitter emit, const Array_Sub array, const m_ emit_add_instr(emit, GWOP_EXCEPT); } get->m_val = array->depth * SZ_INT; - const Instr push = emit_add_instr(emit, RegMove); - push->m_val = is_var ? SZ_INT : t->size; + emit_regmove(emit, is_var ? SZ_INT : t->size); } ANN static inline m_bool array_do(const Emitter emit, const Array_Sub array, diff --git a/src/lib/deep_equal.c b/src/lib/deep_equal.c index 343c702a..d5d0f293 100644 --- a/src/lib/deep_equal.c +++ b/src/lib/deep_equal.c @@ -183,7 +183,7 @@ struct DeepEmits { }; static void deep_emits_init(const Emitter emit, struct DeepEmits *ds) { - emit_add_instr(emit, RegMove)->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); deep_emit_init(emit, ds->lhs, -SZ_INT); deep_emit_init(emit, ds->rhs, 0); vector_init(&ds->acc); diff --git a/src/lib/dict.c b/src/lib/dict.c index 693e2ba9..a1ee9ece 100644 --- a/src/lib/dict.c +++ b/src/lib/dict.c @@ -375,8 +375,7 @@ ANN static m_bool emit_dict_iter(const Emitter emit, const HMapInfo *hinfo, const Instr top = emit_add_instr(emit, Goto); top->m_val = pc; ok->m_val = emit_code_size(emit); - const Instr _pop = emit_add_instr(emit, RegMove); - _pop->m_val = -SZ_INT;// - key->size; + emit_regmove(emit, -SZ_INT); return GW_OK; } diff --git a/src/lib/lib_gack.c b/src/lib/lib_gack.c index 623b3f7a..fc0edcd9 100644 --- a/src/lib/lib_gack.c +++ b/src/lib/lib_gack.c @@ -24,17 +24,14 @@ static OP_EMIT(opem_gack_implicit) { if(t == imp->t) { const Instr cpy = emit_add_instr(emit, Reg2RegOther2); // kind cpy->m_val2 = SZ_INT; - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = imp->t->size - SZ_INT; + emit_regmove(emit, imp->t->size - SZ_INT); } else { - const Instr push = emit_add_instr(emit, RegMove); - push->m_val = SZ_INT; + emit_regmove(emit, SZ_INT); struct Op_Import opi = {.lhs = t, .op = insert_symbol(emit->gwion->st, "@implicit"), .rhs = imp->t}; CHECK_BB(op_emit(emit, &opi)); - const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); const Instr cpy = emit_add_instr(emit, Reg2RegOther2); // kind cpy->m_val = cpy->m_val2 = imp->t->size; } diff --git a/src/lib/object_op.c b/src/lib/object_op.c index a42dad6b..cb88ad91 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -140,10 +140,8 @@ ANN static void emit_member_func(const Emitter emit, const Exp_Dot *member) { if (f->def->base->tmpl) { if(member->is_call) emit_dottmpl(emit, f); else { - if(vflag(f->value_ref, vflag_member)) { - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = -SZ_INT; - } + if(vflag(f->value_ref, vflag_member)) + emit_regmove(emit, -SZ_INT); const Instr instr = emit_add_instr(emit, RegPushImm); instr->m_val = (m_uint)f; return; @@ -164,10 +162,8 @@ ANN static void emit_member_func(const Emitter emit, const Exp_Dot *member) { if (!vflag(f->value_ref, vflag_member)) instr->m_val2 = -SZ_INT; else { - if(member->is_call){ - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = SZ_INT; - } else instr->m_val2 = -SZ_INT; + if(member->is_call) emit_regmove(emit, SZ_INT); + else instr->m_val2 = -SZ_INT; } } return; @@ -184,10 +180,7 @@ ANN static inline void emit_struct_data(const Emitter emit, const Value v, const bool emit_addr) { const Instr instr = emit_structmember(emit, v->type->size, emit_addr); instr->m_val = v->from->offset; - if (!emit_addr) { - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = v->type->size - SZ_INT; - } + if (!emit_addr) emit_regmove(emit, v->type->size - SZ_INT); } ANN m_bool not_from_owner_class(const Env env, const Type t, const Value v, diff --git a/src/lib/ref.c b/src/lib/ref.c index 8d9e620a..bbc19bb8 100644 --- a/src/lib/ref.c +++ b/src/lib/ref.c @@ -55,8 +55,7 @@ static OP_EMIT(opem_ref_implicit_similar) { .rhs = imp->t, .data = (m_uint)imp}; CHECK_BB(op_emit(emit, &opi)); - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = -imp->e->type->size; + emit_regmove(emit, -imp->e->type->size); exp_setvar(imp->e, true); imp->e->cast_to = NULL; return emit_exp(emit, imp->e); @@ -91,8 +90,7 @@ static OP_EMIT(opem_ref_contract_similar) { .rhs = base, .data = (m_uint)&cast}; CHECK_BB(op_emit(emit, &opi)); - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = -imp->e->type->size; + emit_regmove(emit, -imp->e->type->size); exp_setvar(imp->e, true); imp->e->cast_to = NULL; emit_exp(emit, imp->e); diff --git a/src/lib/union.c b/src/lib/union.c index d41660b6..defac498 100644 --- a/src/lib/union.c +++ b/src/lib/union.c @@ -23,8 +23,7 @@ static OP_CHECK(opck_none) { } static OP_EMIT(opem_none) { - const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); return GW_OK; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 16e0e3d6..be407834 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -523,8 +523,7 @@ static OP_EMIT(opem_struct_assign) { const Instr instr = emit_add_instr(emit, Reg2RegOther); instr->m_val = -(t->size + SZ_INT); instr->m_val2 = t->size; - const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + emit_regmove(emit, -SZ_INT); } emit_struct_addref(emit, t, 0, false); return GW_OK;