From e1f3edb445c63658a4668a79dc50a2c9802995aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sat, 13 Jun 2020 14:44:10 +0200 Subject: [PATCH] :art: PushStatic code now as INSTR --- include/instr.h | 4 ++++ include/modify_instr.h | 8 ------- include/opcode.h | 2 -- opcode.txt | 1 - src/emit/emit.c | 19 +++++++++------ src/emit/modify_instr.c | 52 ----------------------------------------- src/lib/instr.c | 28 ++++++++++++++++++++++ src/lib/object_op.c | 15 +++++++----- src/parse/operator.c | 2 +- src/vm/vm.c | 6 +---- src/vm/vm_code.c | 4 ---- 11 files changed, 55 insertions(+), 86 deletions(-) delete mode 100644 include/modify_instr.h delete mode 100644 src/emit/modify_instr.c diff --git a/include/instr.h b/include/instr.h index 4e75bf5b..782ea2e4 100644 --- a/include/instr.h +++ b/include/instr.h @@ -65,6 +65,10 @@ struct dottmpl_ { }; ANN void free_dottmpl(struct dottmpl_*); ANN m_bool traverse_dot_tmpl(const Emitter emit, const struct dottmpl_ *dt); + +INSTR(SetFunc); +INSTR(SetRecurs); +INSTR(SetCtor); // optimizations #ifdef OPTIMIZE INSTR(PutArgsInMem); diff --git a/include/modify_instr.h b/include/modify_instr.h deleted file mode 100644 index 6a5a8803..00000000 --- a/include/modify_instr.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __MODIFY_INSTR -#define __MODIFY_INSTR -typedef void (*instr_modifier)(m_bit *bytecode, m_bit *reg); - -ANN void emit_mod_recurs(const Emitter); -ANN void emit_mod_func(const Emitter, const Func); -ANN void emit_mod_ctor(const Emitter, const Type); -#endif diff --git a/include/opcode.h b/include/opcode.h index 6fb4aba1..8a9327fc 100644 --- a/include/opcode.h +++ b/include/opcode.h @@ -168,7 +168,6 @@ enum { eDotStatic3, eDotFunc, eDotStaticFunc, - ePushStaticCode, eGcIni, eGcAdd, eGcEnd, @@ -348,7 +347,6 @@ enum { #define DotStatic3 (f_instr)eDotStatic3 #define DotFunc (f_instr)eDotFunc #define DotStaticFunc (f_instr)eDotStaticFunc -#define PushStaticCode (f_instr)ePushStaticCode #define GcIni (f_instr)eGcIni #define GcAdd (f_instr)eGcAdd #define GcEnd (f_instr)eGcEnd diff --git a/opcode.txt b/opcode.txt index f53ad365..ff33a876 100644 --- a/opcode.txt +++ b/opcode.txt @@ -165,7 +165,6 @@ DotStatic2 DotStatic3 DotFunc DotStaticFunc -PushStaticCode GcIni GcAdd GcEnd diff --git a/src/emit/emit.c b/src/emit/emit.c index e063d3eb..132a5046 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -18,7 +18,6 @@ #include "parser.h" #include "specialid.h" #include "vararg.h" -#include "modify_instr.h" #undef insert_symbol #define insert_symbol(a) insert_symbol(emit->gwion->st, (a)) @@ -252,8 +251,10 @@ ANN void emit_ext_ctor(const Emitter emit, const Type t) { if(t->nspc->pre_ctor) { const Instr set_code = regseti(emit, (m_uint)t->nspc->pre_ctor); set_code->m_val2 = SZ_INT; - } else - emit_mod_ctor(emit, t); + } else { + const Instr instr = emit_add_instr(emit, SetCtor); + instr->m_val = (m_uint)t; + } const m_uint offset = emit_code_offset(emit); const Instr regset = regseti(emit, offset); regset->m_val2 = SZ_INT *2; @@ -1049,7 +1050,8 @@ ANN Instr emit_exp_call1(const Emitter emit, const Func f) { else { // recursive function. (maybe should be used only for global funcs) const Instr back = (Instr) vector_size(&emit->code->instr) ? (Instr)vector_back(&emit->code->instr) : emit_add_instr(emit, RegPushImm); - back->opcode = ePushStaticCode; + back->opcode = eOP_MAX; + back->execute = SetRecurs; back->m_val = 0; } } else if(emit->env->func != f && !f->value_ref->from->owner_class && !f->code && !is_fptr(emit->gwion, f->value_ref->type)) { @@ -1085,14 +1087,17 @@ ANN Instr emit_exp_call1(const Emitter emit, const Func f) { instr->m_val = val; instr->m_val2 = val2; } else if(f != emit->env->func && !f->code && !is_fptr(emit->gwion, f->value_ref->type)){ - /* not yet emitted static func */ + // not yet emitted static func +/* if(f->value_ref->from->owner_class) { const Instr instr = vector_size(&emit->code->instr) ? (Instr)vector_back(&emit->code->instr) : emit_add_instr(emit, PushStaticCode); assert(instr->opcode == ePushStaticCode); instr->opcode = eRegPushImm; - } else - emit_mod_func(emit, f); + } else*/ + const Instr instr = emit_add_instr(emit, SetFunc); + instr->m_val = (m_uint)f; +// emit_mod_func(emit, f); } const m_uint offset = emit_code_offset(emit); regseti(emit, offset); diff --git a/src/emit/modify_instr.c b/src/emit/modify_instr.c deleted file mode 100644 index 488f5658..00000000 --- a/src/emit/modify_instr.c +++ /dev/null @@ -1,52 +0,0 @@ -#include "gwion_util.h" -#include "gwion_ast.h" -#include "gwion_env.h" -#include "vm.h" -#include "gwion.h" -#include "instr.h" -#include "emit.h" - -// should be in vm.h -#define VAL (*(m_uint*)(byte + SZ_INT)) -#define FVAL (*(m_float*)(byte + SZ_INT)) -#define VAL2 (*(m_uint*)(byte + SZ_INT*2)) -/* -ANN static void set_recurs(m_bit *byte, m_bit **reg) { - *(m_bit*)byte = eRegPushImm; - VAL = (*(m_uint*)(*reg-SZ_INT) = ); - VAL2 = -SZ_INT; -// copy from vm_code -} -*/ - -ANN static void set_func(m_bit *byte, m_bit **reg) { - *(m_bit*)byte = eRegSetImm; - VAL = (*(m_uint*)(*reg-SZ_INT) = (m_uint)((Func)VAL)->code); - VAL2 = -SZ_INT; -// copy from vm_code -} - -ANN static void set_ctor(m_bit *byte, m_bit *reg) { - *(m_bit*)byte = eRegSetImm; - VAL = (*(m_uint*)(reg+SZ_INT) = (m_uint)((Type)VAL)->nspc->pre_ctor); - VAL2 = SZ_INT; -} - -ANN void emit_mod_recurs(const Emitter emit) { - const Instr instr = vector_size(&emit->code->instr) ? - (Instr)vector_back(&emit->code->instr) : emit_add_instr(emit, RegPushImm); - instr->opcode = ePushStaticCode; - instr->m_val = 0; -} - -ANN void emit_mod_func(const Emitter emit, const Func f) { - const Instr instr = emit_add_instr(emit, PushStaticCode); - instr->m_val = (m_uint)f; - instr->m_val2 = (m_uint)set_func; -} - -ANN void emit_mod_ctor(const Emitter emit, const Type t) { - const Instr instr = emit_add_instr(emit, PushStaticCode); - instr->m_val = (m_uint)t; - instr->m_val2 = (m_uint)set_ctor; -} diff --git a/src/lib/instr.c b/src/lib/instr.c index bff0e35e..f661b6c6 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -111,3 +111,31 @@ INSTR(DotTmpl) { } while((t = t->e->parent)); Except(shred, "MissigTmplException[internal]"); } + +#define VAL (*(m_uint*)(byte + SZ_INT)) +#define FVAL (*(m_float*)(byte + SZ_INT)) +#define VAL2 (*(m_uint*)(byte + SZ_INT*2)) + +INSTR(SetFunc) { + const Func f = (Func)instr->m_val; + m_bit *byte = shred->code->bytecode + (shred->pc -1)* SZ_INT*3; + *(m_bit*)byte = eRegPushImm; + VAL = *(m_uint*)(shred->reg) = (m_uint)f->code; + shred->reg += SZ_INT; +} + +INSTR(SetRecurs) { + m_bit *byte = shred->code->bytecode + (shred->pc -1)* SZ_INT*3; + *(m_bit*)byte = eRegPushImm; + VAL = *(m_uint*)(shred->reg) = (m_uint)shred->code; + shred->reg += SZ_INT; +} + +INSTR(SetCtor) { + const Type t = (Type)instr->m_val; + m_bit *byte = shred->code->bytecode + (shred->pc -1)* SZ_INT*3; + *(m_bit*)byte = eRegSetImm; + VAL = *(m_uint*)(shred->reg + SZ_INT) = (m_uint)t->nspc->pre_ctor; + VAL2 = SZ_INT; +} + diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 7f9e3188..acfa26f0 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -151,21 +151,24 @@ static const f_instr dotmember[] = { DotMember, DotMember2, DotMember3, DotMemb ANN static void emit_member_func(const Emitter emit, const Exp_Dot* member) { const Func f = exp_self(member)->info->type->e->d.func; + if(f->def->base->tmpl) + emit_add_instr(emit, DotTmplVal); +else if(is_class(emit->gwion, member->t_base) || GET_FLAG(member->base->info->type, force)) { - const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : PushStaticCode); - func_i->m_val = (m_uint)f->code; + const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : SetFunc); + func_i->m_val = (m_uint)f->code ?: (m_uint)f; return; } - if(f->def->base->tmpl) - emit_add_instr(emit, DotTmplVal); +// if(f->def->base->tmpl) +// emit_add_instr(emit, DotTmplVal); else { if(GET_FLAG(member->t_base, struct)) { if(!GET_FLAG(f->def, static)) { exp_setvar(member->base, 1); emit_exp(emit, member->base); } - const Instr instr = emit_add_instr(emit, f->code ? RegPushImm : PushStaticCode); - instr->m_val = (m_uint)f->code; + const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : SetFunc); + func_i->m_val = (m_uint)f->code ?: (m_uint)f; return; } const Instr instr = emit_add_instr(emit, GET_FLAG(f, member) ? DotFunc : DotStaticFunc); diff --git a/src/parse/operator.c b/src/parse/operator.c index 1d8b5eca..e516d555 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -289,7 +289,7 @@ ANN m_bool operator_set_func(const struct Op_Import* opi) { ANN static Instr handle_instr(const Emitter emit, const M_Operator* mo) { if(mo->func) { - const Instr push = emit_add_instr(emit, mo->func->code ? RegPushImm : PushStaticCode); + const Instr push = emit_add_instr(emit, mo->func->code ? RegPushImm : SetFunc); push->m_val = ((m_uint)mo->func->code ?:(m_uint)mo->func); const Instr instr = emit_exp_call1(emit, mo->func); if(mo->func->def->base->xid == insert_symbol(emit->gwion->st, "@conditionnal")) diff --git a/src/vm/vm.c b/src/vm/vm.c index 06751199..8ab29370 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -14,7 +14,6 @@ #include "map_private.h" #include "gack.h" #include "array.h" -#include "modify_instr.h" static inline uint64_t splitmix64_stateless(uint64_t index) { uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15)); @@ -329,7 +328,7 @@ ANN void vm_run(const VM* vm) { // lgtm [cpp/use-of-goto] &&newobj, &&addref, &&addrefaddr, &&objassign, &&assign, &&remref, &&setobj, &&except, &&allocmemberaddr, &&dotmember, &&dotfloat, &&dotother, &&dotaddr, &&staticint, &&staticfloat, &&staticother, - &&dotfunc, &&dotstaticfunc, &&pushstaticcode, + &&dotfunc, &&dotstaticfunc, &&gcini, &&gcadd, &&gcend, &&gacktype, &&gackend, &&gack, &&noop, &&eoc, &&other, &®pushimm }; @@ -837,9 +836,6 @@ PRAGMA_PUSH() *(VM_Code*)(reg-SZ_INT) = ((Func)vector_at(a.obj->vtable, VAL))->code; PRAGMA_POP() DISPATCH() -pushstaticcode: // TODO: use external instr - ((instr_modifier)VAL2)(byte, reg); - DISPATCH() gcini: vector_add(&shred->gc, 0); DISPATCH(); diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index f571fe8b..f376fe65 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -43,10 +43,6 @@ ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { m_bit *ptr = _mp_malloc(p, sz * BYTECODE_SZ); for(m_uint i= 0; i < sz; ++i) { const Instr instr = (Instr)vector_at(v, i); - if(instr->opcode == ePushStaticCode && !instr->m_val) { - instr->opcode = eRegPushImm; - instr->m_val = (m_uint)code; - } if(instr->opcode < eOP_MAX) memcpy(ptr + i*BYTECODE_SZ, instr, BYTECODE_SZ); else { -- 2.43.0