]> Nishi Git Mirror - gwion.git/commitdiff
:art: Improve constructor
authorJérémie Astor <fennecdjay@gmail.com>
Wed, 21 Jul 2021 18:55:12 +0000 (20:55 +0200)
committerJérémie Astor <fennecdjay@gmail.com>
Wed, 21 Jul 2021 18:56:49 +0000 (20:56 +0200)
include/opcode.h
opcode.txt
src/emit/emit.c
src/lib/object_op.c
src/lib/opfunc.c
src/parse/check.c
src/vm/vm.c

index 281fc9cd921d661a72b459f29d8a04dee9b1df59..3b2c28ed4bbdf2dc5978898b648f1f1bce336cf5 100644 (file)
@@ -30,7 +30,6 @@ enum {
   eRepeat,
   eRegPushMe,
   eRegPushMaybe,
-  eCtorReturn,
   eFuncReturn,
   eGoto,
   eAllocWord,
@@ -249,7 +248,6 @@ enum {
 #define  Repeat               (f_instr)eRepeat
 #define  RegPushMe            (f_instr)eRegPushMe
 #define  RegPushMaybe         (f_instr)eRegPushMaybe
-#define  CtorReturn           (f_instr)eCtorReturn
 #define  FuncReturn           (f_instr)eFuncReturn
 #define  Goto                 (f_instr)eGoto
 #define  AllocWord            (f_instr)eAllocWord
@@ -603,10 +601,6 @@ ANN static inline void dump_opcodes(const VM_Code code) {
         gw_out("{Y}┃{0}{-}% 4lu{0}: RegPushMaybe", j);
         gw_out("\n");
         break;
-      case eCtorReturn:
-        gw_out("{Y}┃{0}{-}% 4lu{0}: CtorReturn  ", j);
-        gw_out("\n");
-        break;
       case eFuncReturn:
         gw_out("{Y}┃{0}{-}% 4lu{0}: FuncReturn  ", j);
         gw_out("\n");
index b173ce4ca908ad03b44a5686c3fd45c2b01f654c..2bd04b7514e6a757d18947d547ac7079d5e5f9ca 100644 (file)
@@ -27,7 +27,6 @@ RepeatIdx~pc~u
 Repeat~pc
 RegPushMe
 RegPushMaybe
-CtorReturn
 FuncReturn
 Goto~pc
 AllocWord~u
index e0a3f600367706fe0eb983a473f2d568c94149f5..274dac81f3402e81a27d7d08efff5c669da37826 100644 (file)
@@ -1517,7 +1517,7 @@ ANN m_bool emit_exp_call1(const Emitter emit, const Func f,
   }
   const m_uint offset = emit_code_offset(emit);
   if (f != emit->env->func || !is_static)
-    regseti(emit, offset /*+ f->def->stack_depth + */ /*+ sizeof(frame_t)*/);
+    regseti(emit, offset /*+ f->def->stack_depth + sizeof(frame_t)*/);
   const Instr instr   = emit_call(emit, f, is_static);
   instr->m_val        = f->def->base->ret_type->size;
   instr->m_val2       = offset;
@@ -2560,8 +2560,7 @@ ANN static VM_Code emit_internal(const Emitter emit, const Func f) {
 ANN static inline VM_Code _emit_func_def_code(const Emitter emit,
                                               const Func    func) {
   if(!strcmp(s_name(func->def->base->xid), "new"))
-    return finalyze(emit, CtorReturn);
-//    emit_add_instr(emit, RegPushMem);
+    emit_add_instr(emit, RegPushMem);
   return !fbflag(func->def->base, fbflag_internal) ? finalyze(emit, FuncReturn)
                                                    : emit_internal(emit, func);
 }
index 6e6e829454c665cfd3694bb27d77db33b13d0cd0..db87b977eb73d6f660a6d837c307cf523d403038 100644 (file)
@@ -97,8 +97,10 @@ ANN static void emit_member_func(const Emitter emit, const Exp_Dot *member) {
   const Func f = exp_self(member)->type->info->func;
 
   if(!strcmp(s_name(f->def->base->xid), "new")) {
-    const Instr instr = emit_add_instr(emit, RegPushImm);
-    instr->m_val = (m_uint)f->code;
+    if(f != emit->env->func) {
+      const Instr instr = emit_add_instr(emit, f->code ? RegPushImm : SetFunc);
+      instr->m_val = (m_uint)f->code ?: (m_uint)f;
+    }
     return;
   }
   if (f->def->base->tmpl)
index 0d3cfd7b121ed4fca2f7d5542fd061503246edfb..cd3ee0d072f2dcbdb40f9fa9c640791a20ecf395 100644 (file)
@@ -128,9 +128,9 @@ OP_EMIT(opem_new) {
   const Exp_Unary *unary = (Exp_Unary *)data;
   CHECK_BB(emit_instantiate_object(emit, exp_self(unary)->type,
                                    unary->ctor.td->array, 0));
-// we don't need gc for arrays?
-// also when in rewrote exp
-//  if(!(unary->ctor.td->array || unary->ctor.exp))
-//    emit_gc(emit, -SZ_INT);
+  // we don't need gc for arrays?
+  // also when in rewrote exp
+  if(!(unary->ctor.td->array || unary->ctor.exp))
+    emit_gc(emit, -SZ_INT);
   return GW_OK;
 }
index 877a2608c41e8b501ae434c63099e0c0b06947cd..dba0fca76c1c9eb6d2f837619c68d003ad5e6160 100644 (file)
@@ -1220,11 +1220,14 @@ ANN static m_bool check_stmt_return(const Env env, const Stmt_Exp stmt) {
   if (!env->func)
     ERR_B(stmt_self(stmt)->pos,
           _("'return' statement found outside function definition"))
-  if (!strcmp(s_name(env->func->def->base->xid), "new"))
-    ERR_B(stmt_self(stmt)->pos,
-          _("'return' statement found inside constructor function"))
   if (env->scope->depth == 1) // so ops no dot set scope->depth ?
     set_fflag(env->func, fflag_return);
+  if (!strcmp(s_name(env->func->def->base->xid), "new")) {
+    if(stmt->val)
+      ERR_B(stmt_self(stmt)->pos,
+            _("'return' statement inside constructor function should have no expression"))
+    return GW_OK;
+  }
   DECL_OB(const Type, ret_type,
           = stmt->val ? check_exp(env, stmt->val) : env->gwion->type[et_void]);
   if (!env->func->def->base->ret_type) {
index f1db7ba395f0a7df1b0d547d7d16c56da3848db3..05b218edfc6cdfcd5b47fc0faa865e30e285a2cd 100644 (file)
@@ -412,7 +412,7 @@ vm_run(const VM *vm) { // lgtm [cpp/use-of-goto]
       &&baseaddr, &&regtoreg, &&regtoregother, &&regtoregaddr, &&regtoregderef,
       &&structmember, &&structmemberfloat, &&structmemberother,
       &&structmemberaddr, &&memsetimm, &&memaddimm, &&repeatidx, &&repeat,
-      &&regpushme, &&regpushmaybe, &&ctorreturn, &&funcreturn, &&_goto, &&allocint,
+      &&regpushme, &&regpushmaybe, &&funcreturn, &&_goto, &&allocint,
       &&allocfloat, &&allocother,
       &&intplus, &&intminus, &&intmul, &&intdiv, &&intmod,
       &&intplusimm, &&intminusimm, &&intmulimm, &&intdivimm, &&intmodimm,
@@ -584,9 +584,6 @@ vm_run(const VM *vm) { // lgtm [cpp/use-of-goto]
       *(m_uint *)reg = gw_rand((uint32_t *)vm->rand) > (UINT32_MAX / 2);
       reg += SZ_INT;
       DISPATCH();
-ctorreturn:
-*(M_Object*)(reg) = *(M_Object*)mem;
-reg += SZ_INT;
     funcreturn : {
       register frame_t frame = *(frame_t *)(mem - sizeof(frame_t));
       bytecode               = (code = frame.code)->bytecode;