From: Jérémie Astor Date: Wed, 20 Jan 2021 21:37:38 +0000 (+0100) Subject: :art: Improve arrays and foreach X-Git-Tag: nightly~1036 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=b93f3459f1531a479dd65fa3ef6a7134d1dd7f22;p=gwion.git :art: Improve arrays and foreach --- diff --git a/include/array.h b/include/array.h index 385421a7..f8a770cd 100644 --- a/include/array.h +++ b/include/array.h @@ -7,7 +7,15 @@ struct ArrayAccessInfo { const m_bool is_var; }; -typedef struct M_Vector_ * M_Vector; +typedef struct M_Vector_ { + m_bit* ptr; +} * M_Vector; +#define ARRAY_OFFSET SZ_INT * 4 +#define ARRAY_PTR(array) (array->ptr + ARRAY_OFFSET) +#define ARRAY_LEN(array) *(m_uint*)(array->ptr) +#define ARRAY_SIZE(array) *(m_uint*)(array->ptr + SZ_INT) +#define ARRAY_CAP(array) *(m_uint*)(array->ptr + SZ_INT*2) + typedef struct ArrayInfo_ { m_int depth; struct Vector_ type; @@ -20,11 +28,21 @@ typedef struct ArrayInfo_ { uint is_obj; } ArrayInfo; -ANN m_uint m_vector_size(const M_Vector); +ANN static inline m_uint m_vector_size(const M_Vector v) { + return ARRAY_LEN(v); +} + +ANN static inline void m_vector_get(const M_Vector v, const m_uint i, void* c) { + const m_uint size = ARRAY_SIZE(v); + memcpy(c, ARRAY_PTR(v) + i * size, size); +} + +ANN static inline m_bit* m_vector_addr(const M_Vector v, const m_uint i) { + return &*(m_bit*)(ARRAY_PTR(v) + i * ARRAY_SIZE(v)); +} + ANN void m_vector_set(const M_Vector, const m_uint, const void*); -ANN void m_vector_get(const M_Vector, const m_uint, void*); ANN void m_vector_add(const M_Vector, const void*); -ANN m_bit* m_vector_addr(const M_Vector, const m_uint); ANN void m_vector_rem(const M_Vector, const m_uint); ANEW M_Vector new_m_vector(MemPool, const m_uint size, const m_uint len); ANN void free_m_vector(MemPool, M_Vector); diff --git a/include/opcode.h b/include/opcode.h index 22ed841f..9a695e72 100644 --- a/include/opcode.h +++ b/include/opcode.h @@ -143,8 +143,6 @@ enum { eBranchNeqFloat, eArrayAppend, eAutoLoop, - eAutoLoopPtr, - eAutoLoopCount, eArrayTop, eArrayAccess, eArrayGet, @@ -331,8 +329,6 @@ enum { #define BranchNeqFloat (f_instr)eBranchNeqFloat #define ArrayAppend (f_instr)eArrayAppend #define AutoLoop (f_instr)eAutoLoop -#define AutoLoopPtr (f_instr)eAutoLoopPtr -#define AutoLoopCount (f_instr)eAutoLoopCount #define ArrayTop (f_instr)eArrayTop #define ArrayAccess (f_instr)eArrayAccess #define ArrayGet (f_instr)eArrayGet diff --git a/opcode.txt b/opcode.txt index 11c30d5e..aafc48c9 100644 --- a/opcode.txt +++ b/opcode.txt @@ -140,8 +140,6 @@ BranchEqFloat BranchNeqFloat ArrayAppend AutoLoop -AutoLoopPtr -AutoLoopCount ArrayTop ArrayAccess ArrayGet diff --git a/src/emit/emit.c b/src/emit/emit.c index 4203d5a4..c35d6beb 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -737,8 +737,11 @@ 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) { emit_add_instr(emit, RegPushMem); const Instr instr = emit_kind(emit, v->type->size, emit_addr, structmember); - if(!emit_addr) - regpush(emit, v->type->size - SZ_INT); + if(!emit_addr) { + const m_int sz = v->type->size - SZ_INT; + if(sz) + regpush(emit, v->type->size - SZ_INT); + } return instr; } @@ -1640,6 +1643,12 @@ ANN static m_bool emit_stmt_for(const Emitter emit, const Stmt_For stmt) { } ANN static m_bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt, m_uint *end_pc) { + CHECK_BB(emit_exp(emit, stmt->exp)) + const m_uint _offset = emit_local(emit, emit->gwion->type[et_int]);//array? + const Instr tomem = emit_add_instr(emit, Reg2Mem); + tomem->m_val = _offset + SZ_INT; + tomem->m_val2 = -SZ_INT; + regpop(emit, SZ_INT); const Instr s1 = emit_add_instr(emit, MemSetImm); Instr cpy = emit_add_instr(emit, MemSetImm); emit_local(emit, emit->gwion->type[et_int]); @@ -1647,7 +1656,7 @@ ANN static m_bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt, m_ui emit_local(emit, emit->gwion->type[et_int]); stmt->v->from->offset = offset + SZ_INT; const m_uint ini_pc = emit_code_size(emit); - const Instr loop = emit_add_instr(emit, AutoLoopPtr); + const Instr loop = emit_add_instr(emit, AutoLoop); const Instr end = emit_add_instr(emit, BranchEqInt); const m_bool ret = scoped_stmt(emit, stmt->body, 1); *end_pc = emit_code_size(emit); @@ -1661,12 +1670,10 @@ ANN static m_bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt, m_ui } ANN static m_bool emit_stmt_each(const Emitter emit, const Stmt_Each stmt) { - CHECK_BB(emit_exp(emit, stmt->exp)) emit_push_stack(emit); m_uint end_pc = 0; const m_bool ret = _emit_stmt_each(emit, stmt, &end_pc); emit_pop_stack(emit, end_pc); - regpop(emit, SZ_INT); return ret; } diff --git a/src/lib/array.c b/src/lib/array.c index d5a1a13b..f0d0d124 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -16,19 +16,6 @@ #include "gwi.h" #include "emit.h" -struct M_Vector_ { - m_bit* ptr; -}; -#define ARRAY_OFFSET SZ_INT * 4 -#define ARRAY_PTR(array) (array->ptr + ARRAY_OFFSET) -#define ARRAY_LEN(array) *(m_uint*)(array->ptr) -#define ARRAY_SIZE(array) *(m_uint*)(array->ptr + SZ_INT) -#define ARRAY_CAP(array) *(m_uint*)(array->ptr + SZ_INT*2) - -ANN m_uint m_vector_size(const M_Vector v) { - return ARRAY_LEN(v); -} - M_Vector new_m_vector(MemPool p, const m_uint size, const m_uint len) { const M_Vector array = mp_calloc(p, M_Vector); const size_t sz = (ARRAY_OFFSET*SZ_INT) + (len*size); @@ -74,11 +61,6 @@ ANN M_Object new_array(MemPool p, const Type t, const m_uint length) { return a; } -ANN void m_vector_get(const M_Vector v, const m_uint i, void* c) { - const m_uint size = ARRAY_SIZE(v); - memcpy(c, ARRAY_PTR(v) + i * size, size); -} - ANN void m_vector_add(const M_Vector v, const void* data) { const m_uint size = ARRAY_SIZE(v); if(++ARRAY_LEN(v) >= ARRAY_CAP(v)) { @@ -186,10 +168,6 @@ static MFUN(vm_vector_insert_struct) { struct_addref(shred->info->vm->gwion, array_base(o->type_ref), shred->mem + SZ_INT*2); } -ANN m_bit* m_vector_addr(const M_Vector v, const m_uint i) { - return &*(m_bit*)(ARRAY_PTR(v) + i * ARRAY_SIZE(v)); -} - static MFUN(vm_vector_size) { *(m_uint*)RETURN = ARRAY_LEN(ARRAY(o)); } diff --git a/src/vm/vm.c b/src/vm/vm.c index c1d15490..479ddb03 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -323,7 +323,7 @@ ANN void vm_run(const VM* vm) { // lgtm [cpp/use-of-goto] &®move, &®tomem, &®tomemother, &&overflow, &&funcusrend, &&funcmemberend, &&sporkini, &&forkini, &&sporkfunc, &&sporkmemberfptr, &&sporkexp, &&sporkend, &&brancheqint, &&branchneint, &&brancheqfloat, &&branchnefloat, - &&arrayappend, &&autoloop, &&autoloopptr, &&autoloopcount, &&arraytop, &&arrayaccess, &&arrayget, &&arrayaddr, &&arrayvalid, + &&arrayappend, &&autoloop, &&arraytop, &&arrayaccess, &&arrayget, &&arrayaddr, &&arrayvalid, &&newobj, &&addref, &&addrefaddr, &&structaddref, &&structaddrefaddr, &&objassign, &&assign, &&remref, &&except, &&allocmemberaddr, &&dotmember, &&dotfloat, &&dotother, &&dotaddr, &&unioncheck, &&unionint, &&unionfloat, &&unionother, &&unionaddr, @@ -715,12 +715,8 @@ arrayappend: m_vector_add(ARRAY(*(M_Object*)(reg-SZ_INT)), reg); DISPATCH() autoloop: - m_vector_get(ARRAY(*(M_Object*)(reg-SZ_INT)), *(m_uint*)(mem + VAL), mem + VAL + SZ_INT); - goto autoloopcount; -autoloopptr: - *(m_bit**)(mem + VAL + SZ_INT) = m_vector_addr(ARRAY(*(M_Object*)(reg-SZ_INT)), *(m_uint*)(mem + VAL)); -autoloopcount: - *(m_uint*)reg = m_vector_size(ARRAY(*(M_Object*)(reg-SZ_INT))) - (*(m_uint*)(mem + VAL))++; + *(m_bit**)(mem + VAL + SZ_INT) = m_vector_addr(ARRAY(*(M_Object*)(mem+VAL-SZ_INT)), *(m_uint*)(mem + VAL)); + *(m_uint*)reg = m_vector_size(ARRAY(*(M_Object*)(mem+VAL-SZ_INT))) - (*(m_uint*)(mem + VAL))++; reg += SZ_INT; DISPATCH() arraytop: