From 7ee6589cc5a034d282552d6808b45404a4126bf9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 13 Jun 2021 22:35:16 +0200 Subject: [PATCH] :art: clean code --- include/emit.h | 13 ++++- include/instr.h | 2 +- src/emit/emit.c | 127 +++++++++++++++++--------------------------- src/emit/kind.c | 57 ++++++++++++++++++++ src/lib/object_op.c | 26 +++------ src/lib/union.c | 12 ++--- 6 files changed, 129 insertions(+), 108 deletions(-) create mode 100644 src/emit/kind.c diff --git a/include/emit.h b/include/emit.h index 31a61794..1b90044a 100644 --- a/include/emit.h +++ b/include/emit.h @@ -64,9 +64,9 @@ ANN static inline void emit_gc(const Emitter emit, const m_int offset) { } ANN Instr emit_object_addref(const Emitter emit, const m_int size, - const m_bool emit_var); + const bool emit_var); ANN Instr emit_struct_addref(const Emitter emit, const Type t, const m_int size, - const m_bool emit_var); + const bool emit_var); ANN static inline Instr emit_compound_addref(const Emitter emit, const Type t, const m_int size, const m_bool emit_var) { @@ -81,4 +81,13 @@ ANN static inline bool is_static_call(const Emitter emit, const Exp e) { is_class(emit->gwion, member->base->type) || member->base->exp_type == ae_exp_cast; } + +ANN Instr emit_kind(Emitter, const m_uint size, const bool addr, const f_instr func[]); +ANN Instr emit_regpushimm(Emitter, const m_uint, const bool); +ANN Instr emit_regpushmem(Emitter, const m_uint, const bool); +ANN Instr emit_regpushbase(Emitter, const m_uint, const bool); +ANN Instr emit_dotstatic(Emitter, const m_uint, const bool); +ANN Instr emit_dotmember(Emitter, const m_uint, const bool); +ANN Instr emit_structmember(Emitter, const m_uint, const bool); +ANN Instr emit_unionmember(Emitter, const m_uint, const bool); #endif diff --git a/include/instr.h b/include/instr.h index 8cc6d2a5..b2887574 100644 --- a/include/instr.h +++ b/include/instr.h @@ -11,7 +11,7 @@ __attribute__((hot)) ANN2(1) void a(const VM_Shred shred NUSED, \ const Instr instr NUSED) -enum Kind { KIND_INT, KIND_FLOAT, KIND_OTHER, KIND_ADDR }; +//enum Kind { KIND_INT, KIND_FLOAT, KIND_OTHER, KIND_ADDR }; typedef struct Instr_ *Instr; typedef void (*f_instr)(const VM_Shred, const Instr); diff --git a/src/emit/emit.c b/src/emit/emit.c index 09c4317f..f5df1413 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -99,20 +99,10 @@ ANN static inline void frame_push(Frame *frame) { vector_add(&frame->defer, (vtype)NULL); } -static const f_instr regpushimm[] = {RegPushImm, RegPushImm2, RegPushImm3, - RegPushImm4}; -static const f_instr regpushmem[] = {RegPushMem, RegPushMem2, RegPushMem3, - RegPushMem4}; -static const f_instr regpushbase[] = {RegPushBase, RegPushBase2, RegPushBase3, - RegPushBase4}; -static const f_instr dotstatic[] = {DotStatic, DotStatic2, DotStatic3, - RegPushImm}; -static const f_instr allocmember[] = {RegPushImm, RegPushImm2, RegPushImm3, +static const f_instr allocmember[] = {RegPushImm, RegPushImm2, RegPushImm3, AllocMember4}; -static const f_instr allocword[] = {AllocWord, AllocWord2, AllocWord3, +static const f_instr allocword[] = {AllocWord, AllocWord2, AllocWord3, RegPushMem4}; -static const f_instr structmember[] = {StructMember, StructMemberFloat, - StructMemberOther, StructMemberAddr}; #define regmove(name, op) \ ANN static inline Instr reg##name(const Emitter emit, const m_uint sz) { \ @@ -120,27 +110,15 @@ static const f_instr structmember[] = {StructMember, StructMemberFloat, instr->m_val = op sz; \ return instr; \ } -regmove(pop, -) regmove(push, ) +regmove(pop, -) regmove(push, ); + #define regxxx(name, instr) \ ANN static inline Instr reg##name(const Emitter emit, const m_uint sz) { \ const Instr instr = emit_add_instr(emit, Reg##instr); \ instr->m_val = sz; \ return instr; \ } - regxxx(pushi, PushImm) regxxx(seti, SetImm) - - static inline enum Kind kindof(const m_uint size, const uint emit_var) { - if (emit_var) return KIND_ADDR; - return size == SZ_INT ? KIND_INT : size == SZ_FLOAT ? KIND_FLOAT : KIND_OTHER; -} - -ANN /*static */ Instr emit_kind(Emitter emit, const m_uint size, - const uint addr, const f_instr func[]) { - const enum Kind kind = kindof(size, addr); - const Instr instr = emit_add_instr(emit, func[kind]); - instr->m_val2 = size; - return instr; -} +regxxx(pushi, PushImm) regxxx(seti, SetImm); ANN static m_bool emit_class_def(const Emitter, const Class_Def); ANN /*static */ m_bool emit_cdef(const Emitter, const Type); @@ -464,14 +442,12 @@ m_bool emit_instantiate_decl(const Emitter emit, const Type type, ANN static m_bool emit_symbol_builtin(const Emitter emit, const Symbol *data) { const Value v = prim_self(data)->value; if (vflag(v, vflag_direct)) { - const m_uint size = v->type->size; - const Instr instr = - emit_kind(emit, size, exp_getvar(prim_exp(data)), dotstatic); - instr->m_val = (m_uint)&v->d.ptr; + const m_uint size = v->type->size; + const Instr instr = emit_dotstatic(emit, size, exp_getvar(prim_exp(data))); + instr->m_val = (m_uint)&v->d.ptr; } else { const m_uint size = v->type->size; - const Instr instr = - emit_kind(emit, size, exp_getvar(prim_exp(data)), regpushimm); + const Instr instr = emit_regpushimm(emit, size, exp_getvar(prim_exp(data))); if (v->type->size == SZ_FLOAT) instr->f = v->d.fnum; else @@ -503,8 +479,9 @@ ANN static m_bool _emit_symbol(const Emitter emit, const Symbol *data) { } const m_uint size = v->type->size; const Instr instr = - emit_kind(emit, size, exp_getvar(prim_exp(data)), - !vflag(v, vflag_fglobal) ? regpushmem : regpushbase); + !vflag(v, vflag_fglobal) + ? emit_regpushmem(emit, size, exp_getvar(prim_exp(data))) + : emit_regpushbase(emit, size, exp_getvar(prim_exp(data))); instr->m_val = v->from->offset; if (GET_FLAG(v, late) && !exp_getvar(prim_exp(data)) && isa(v->type, emit->gwion->type[et_object]) > 0) { @@ -540,7 +517,7 @@ ANN static inline m_uint exp_totalsize(Exp e) { } ANN Instr emit_object_addref(const Emitter emit, const m_int size, - const m_bool emit_var) { + const bool emit_var) { const f_instr exec = !emit_var ? RegAddRef : RegAddRefAddr; const Instr instr = emit_add_instr(emit, exec); instr->m_val = size; @@ -548,7 +525,7 @@ ANN Instr emit_object_addref(const Emitter emit, const m_int size, } ANN Instr emit_struct_addref(const Emitter emit, const Type t, const m_int size, - const m_bool emit_var) { + const bool emit_var) { const Instr instr = emit_add_instr(emit, !emit_var ? StructRegAddRef : StructRegAddRefAddr); instr->m_val = (m_uint)t; @@ -729,7 +706,7 @@ ANN static m_bool emit_prim_str(const Emitter emit, const m_str *str) { ANN static void interp_multi(const Emitter emit, const Exp e) { Var_Decl_List list = e->d.exp_decl.list; - const int emit_var = exp_getvar(e); + const bool emit_var = exp_getvar(e); m_uint offset = 0; while ((list = list->next)) offset += !emit_var ? list->self->value->type->size : SZ_INT; @@ -790,9 +767,9 @@ ANN static m_bool emit_prim(const Emitter emit, Exp_Primary *const prim) { } ANN static m_bool emit_dot_static_data(const Emitter emit, const Value v, - const uint emit_var) { + const bool emit_var) { const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_var, dotstatic); + const Instr instr = emit_dotstatic(emit, size, emit_var); instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); instr->m_val2 = size; return GW_OK; @@ -823,7 +800,7 @@ ANN static void decl_expand(const Emitter emit, const Type t) { } ANN static void emit_struct_decl_finish(const Emitter emit, const Type t, - const uint emit_addr) { + const bool emit_addr) { emit->code->frame->curr_offset += t->size + SZ_INT; emit_ext_ctor(emit, t); if (!emit_addr) decl_expand(emit, t); @@ -832,8 +809,8 @@ ANN static void emit_struct_decl_finish(const Emitter emit, const Type t, ANN static m_bool emit_exp_decl_static(const Emitter emit, const Exp_Decl *decl, const Var_Decl var_decl, - const uint is_ref, - const uint emit_addr) { + const bool is_ref, + const bool emit_addr) { const Value v = var_decl->value; if (isa(v->type, emit->gwion->type[et_object]) > 0 && !is_ref) CHECK_BB(decl_static(emit, decl, var_decl, 0)); @@ -843,9 +820,9 @@ ANN static m_bool emit_exp_decl_static(const Emitter emit, const Exp_Decl *decl, } ANN static Instr emit_struct_decl(const Emitter emit, const Value v, - const m_bool emit_addr) { + const bool emit_addr) { emit_add_instr(emit, RegPushMem); - const Instr instr = emit_kind(emit, v->type->size, emit_addr, structmember); + const Instr instr = emit_structmember(emit, v->type->size, emit_addr); if (!emit_addr) { const m_int sz = v->type->size - SZ_INT; if (sz) regpush(emit, v->type->size - SZ_INT); @@ -858,12 +835,12 @@ ANN static m_bool emit_exp_decl_non_static(const Emitter emit, const Var_Decl var_decl, const uint is_ref, const uint emit_var) { - const Value v = var_decl->value; - const Type type = v->type; - const Array_Sub array = var_decl->array; - const m_bool is_array = array && array->exp; - const m_bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; - const uint emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : 1; + const Value v = var_decl->value; + const Type type = v->type; + const Array_Sub array = var_decl->array; + const bool is_array = array && array->exp; + const bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; + const bool emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : true; if (is_obj && (is_array || !is_ref)) CHECK_BB(emit_instantiate_decl(emit, type, decl->td, array, is_ref)); f_instr *exec = (f_instr *)allocmember; @@ -879,7 +856,7 @@ ANN static m_bool emit_exp_decl_non_static(const Emitter emit, const Instr instr = !(safe_tflag(emit->env->class_def, tflag_struct) && !emit->env->scope->depth) - ? emit_kind(emit, v->type->size, !struct_ctor(v) ? emit_addr : 1, + ? emit_kind(emit, v->type->size, !struct_ctor(v) ? emit_addr : true, exec) : emit_struct_decl(emit, v, !struct_ctor(v) ? emit_addr : 1); instr->m_val = v->from->offset; @@ -898,17 +875,17 @@ ANN static m_bool emit_exp_decl_non_static(const Emitter emit, ANN static m_bool emit_exp_decl_global(const Emitter emit, const Exp_Decl *decl, const Var_Decl var_decl, - const uint is_ref, const uint emit_var) { - const Value v = var_decl->value; - const Type type = v->type; - const Array_Sub array = var_decl->array; - const m_bool is_array = array && array->exp; - const m_bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; - const uint emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : 1; + const uint is_ref, const bool emit_var) { + const Value v = var_decl->value; + const Type type = v->type; + const Array_Sub array = var_decl->array; + const bool is_array = array && array->exp; + const bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; + const bool emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : true; if (is_obj && (is_array || !is_ref)) CHECK_BB(emit_instantiate_decl(emit, type, decl->td, array, is_ref)); - const Instr instr = emit_kind(emit, v->type->size, - !struct_ctor(v) ? emit_addr : 1, dotstatic); + const Instr instr = + emit_dotstatic(emit, v->type->size, !struct_ctor(v) ? emit_addr : 1); if (type->size > SZ_INT) //{ v->d.ptr = mp_calloc2(emit->gwion->mp, v->type->size); instr->m_val = (m_uint)&v->d.ptr; @@ -1176,7 +1153,7 @@ ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { return GW_OK; } -ANN static m_uint get_decl_size(Var_Decl_List a, uint emit_addr) { +ANN static m_uint get_decl_size(Var_Decl_List a, bool emit_addr) { m_uint size = 0; do // if(GET_FLAG(a->self->value, used)) size += !emit_addr ? a->self->value->type->size : SZ_INT; @@ -1185,7 +1162,7 @@ ANN static m_uint get_decl_size(Var_Decl_List a, uint emit_addr) { } ANN static m_uint pop_exp_size(Exp e) { - const uint emit_addr = exp_getvar(e); + const bool emit_addr = exp_getvar(e); m_uint size = 0; do { // account for emit_var ? size += (e->exp_type == ae_exp_decl @@ -1368,7 +1345,7 @@ typedef struct { } MemoizeEmitter; ANN static Instr me_push(const MemoizeEmitter *me, const m_uint sz) { - const Instr instr = emit_kind(me->emit, sz, 0, regpushmem); + const Instr instr = emit_regpushmem(me->emit, sz, false); instr->m_val = me->arg_offset; return instr; } @@ -1556,12 +1533,12 @@ static void push_spork_code(const Emitter emit, const m_str prefix, } struct Sporker { - const Stmt code; - const Exp exp; - VM_Code vm_code; - const Type type; - const m_bool emit_var; - const m_bool is_spork; + const Stmt code; + const Exp exp; + VM_Code vm_code; + const Type type; + const bool emit_var; + const bool is_spork; }; ANN static m_bool spork_prepare_code(const Emitter emit, @@ -2306,10 +2283,6 @@ ANN static m_bool case_value(const Emitter emit, const Exp base, const Exp e) { return GW_OK; } -// should be in src/lib/union.c -static const f_instr unionmember[] = {UnionMember, UnionMember2, UnionMember3, - UnionMember4}; - #define CASE_PASS (Symbol)1 ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, const Vector vec, const uint n) { @@ -2343,7 +2316,7 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, check->m_val2 = i; vector_add(vec, (m_uint)check); const Instr instr = - emit_kind(emit, v->type->size, 0 /*emit_addr*/, unionmember); + emit_unionmember(emit, v->type->size, false /*emit_addr*/); instr->m_val = i; regpop(emit, v->type->size); case_op(emit, e->d.exp_call.args, e->d.exp_call.args, vec, i + 1); @@ -2514,7 +2487,7 @@ ANN static void emit_func_def_args(const Emitter emit, Arg_List a) { ANN static void emit_func_def_ensure(const Emitter emit, const Func_Def fdef) { const m_uint size = fdef->base->ret_type->size; if (size) { - const Instr instr = emit_kind(emit, size, 0, regpushimm); + const Instr instr = emit_regpushimm(emit, size, false); instr->m_val2 = size; } vector_add(&emit->code->stack_return, (vtype)emit_add_instr(emit, Goto)); @@ -2597,7 +2570,7 @@ ANN static void me_bottom(MemoizeEmitter *me, const m_uint pc) { ANN static void me_ret(MemoizeEmitter *me) { const Instr instr = - emit_kind(me->emit, me->fdef->base->ret_type->size, 0, regpushmem); + emit_regpushmem(me->emit, me->fdef->base->ret_type->size, false); instr->m_val = (me->offset + SZ_INT) * 2; emit_add_instr(me->emit, FuncReturn); } diff --git a/src/emit/kind.c b/src/emit/kind.c new file mode 100644 index 00000000..0f228f56 --- /dev/null +++ b/src/emit/kind.c @@ -0,0 +1,57 @@ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "gwion_env.h" +#include "vm.h" +#include "gwion.h" +#include "instr.h" +#include "emit.h" + +enum Kind { KIND_INT, KIND_FLOAT, KIND_OTHER, KIND_ADDR, KIND_END }; + +static inline enum Kind kindof(const m_uint size, const uint emit_var) { + if (emit_var) return KIND_ADDR; + return size == SZ_INT ? KIND_INT : size == SZ_FLOAT ? KIND_FLOAT : KIND_OTHER; +} + +ANN Instr emit_kind(Emitter emit, const m_uint size, const bool addr, + const f_instr func[]) { + const enum Kind kind = kindof(size, addr); + const Instr instr = emit_add_instr(emit, func[kind]); + instr->m_val2 = size; + return instr; +} + +static const f_instr regpushimm[KIND_END] = {RegPushImm, RegPushImm2, + RegPushImm3, RegPushImm4}; + +static const f_instr regpushmem[KIND_END] = {RegPushMem, RegPushMem2, + RegPushMem3, RegPushMem4}; + +static const f_instr regpushbase[KIND_END] = {RegPushBase, RegPushBase2, + RegPushBase3, RegPushBase4}; + +static const f_instr dotstatic[KIND_END] = {DotStatic, DotStatic2, DotStatic3, + RegPushImm}; + +static const f_instr dotmember[KIND_END] = {DotMember, DotMember2, DotMember3, + DotMember4}; + +static const f_instr structmember[KIND_END] = { + StructMember, StructMemberFloat, StructMemberOther, StructMemberAddr}; + +static const f_instr unionmember[KIND_END] = {UnionMember, UnionMember2, + UnionMember3, UnionMember4}; + +// function factory +#define kind_func(name) \ + ANN Instr emit_##name(Emitter emit, const m_uint size, const bool addr) { \ + return emit_kind(emit, size, addr, name); \ + } + +kind_func(regpushimm); +kind_func(regpushmem); +kind_func(regpushbase); +kind_func(dotstatic); +kind_func(dotmember); +kind_func(structmember); +kind_func(unionmember); diff --git a/src/lib/object_op.c b/src/lib/object_op.c index e49af714..92aead95 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -73,37 +73,25 @@ static OP_CHECK(opck_struct_scan) { return opck_object_scan(env, ts); } -static const f_instr dotstatic[] = {DotStatic, DotStatic2, DotStatic3, - RegPushImm}; -static const f_instr structmember[] = {StructMember, StructMemberFloat, - StructMemberOther, StructMemberAddr}; - -ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, - const f_instr func[]); ANN static void emit_dot_static_data(const Emitter emit, const Value v, - const uint emit_var) { + const bool emit_addr) { const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_var, dotstatic); + const Instr instr = emit_dotstatic(emit, size, emit_addr); instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); instr->m_val2 = size; } -static const f_instr regpushimm[] = {RegPushImm, RegPushImm2, RegPushImm3, - RegPushImm4}; ANN static void emit_dot_static_import_data(const Emitter emit, const Value v, - const uint emit_addr) { + const bool emit_addr) { // if(v->d.ptr && vflag(v, vflag_builtin) && GET_FLAG(v, const)) { if (vflag(v, vflag_builtin) && GET_FLAG(v, const)) { const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_addr, regpushimm); + const Instr instr = emit_regpushimm(emit, size, emit_addr); instr->m_val = (m_uint)v->d.ptr; instr->m_val2 = size; } else emit_dot_static_data(emit, v, emit_addr); } -static const f_instr dotmember[] = {DotMember, DotMember2, DotMember3, - DotMember4}; - ANN static void emit_member_func(const Emitter emit, const Exp_Dot *member) { const Func f = exp_self(member)->type->info->func; if (f->def->base->tmpl) @@ -138,14 +126,14 @@ ANN static void emit_member_func(const Emitter emit, const Exp_Dot *member) { ANN static inline void emit_member(const Emitter emit, const Value v, const uint emit_addr) { const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_addr, dotmember); + const Instr instr = emit_dotmember(emit, size, emit_addr); instr->m_val = v->from->offset; instr->m_val2 = size; } ANN static inline void emit_struct_data(const Emitter emit, const Value v, - const uint emit_addr) { - const Instr instr = emit_kind(emit, v->type->size, emit_addr, structmember); + 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); diff --git a/src/lib/union.c b/src/lib/union.c index 2ed7e311..66bcb05d 100644 --- a/src/lib/union.c +++ b/src/lib/union.c @@ -28,11 +28,6 @@ static OP_EMIT(opem_none) { return GW_OK; } -static const f_instr unionmember[] = {UnionMember, UnionMember2, UnionMember3, - UnionMember4}; -ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, - const f_instr func[]); - static OP_EMIT(opem_union_dot) { const Exp_Dot *member = (Exp_Dot *)data; const Map map = &member->base->type->nspc->info->value->map; @@ -52,10 +47,9 @@ static OP_EMIT(opem_union_dot) { if (VKEY(map, i) == (m_uint)member->xid) { const Value v = (Value)VVAL(map, i); const uint emit_addr = exp_getvar(exp_self(member)); - const Instr instr = - emit_kind(emit, v->type->size, emit_addr, unionmember); - instr->m_val = i; // + 1; - instr->m_val2 = v->type->size; + const Instr instr = emit_unionmember(emit, v->type->size, emit_addr); + instr->m_val = i; // + 1; + instr->m_val2 = v->type->size; return GW_OK; } } -- 2.43.0