[Quipu-dev] quipu/quipu: Big refactor, use pointer instead of SP

Zurück zum Archiv-Index

scmno****@osdn***** scmno****@osdn*****
Sat Jun 2 05:08:07 JST 2018


changeset e5ce23e3e37d in quipu/quipu
details: http://hg.osdn.jp/view/quipu/quipu?cmd=changeset;node=e5ce23e3e37d
user: Agustina Arzille <avarz****@riseu*****>
date: Fri Jun 01 17:07:58 2018 -0300
description: Big refactor, use pointer instead of SP

diffstat:

 builtins.cpp     |    4 +-
 compiler.cpp     |  201 +++++++++++++++++++++++++++---------------------------
 continuation.cpp |   17 ++-
 function.h       |   22 +++---
 interp.cpp       |   32 ++++----
 interp.h         |   11 ++-
 memory.cpp       |    4 +-
 quipu.cpp        |    3 +-
 symbol.cpp       |    4 +-
 thread.h         |    2 +-
 utils/lstack.h   |    2 +-
 11 files changed, 157 insertions(+), 145 deletions(-)

diffs (truncated from 873 to 300 lines):

diff -r 4a20d9c7334b -r e5ce23e3e37d builtins.cpp
--- a/builtins.cpp	Fri Jun 01 12:39:49 2018 -0300
+++ b/builtins.cpp	Fri Jun 01 17:07:58 2018 -0300
@@ -1211,9 +1211,9 @@
   if (!continuation_p (cn))
     interp->raise2 ("arg-error", "iter-next: argument must be a generator");
 
-  uint32_t sp = interp->sp;
+  uint32_t sp = interp->stkdiff ();
   object ret = call_continuation (interp, cn);
-  interp->sp = sp;
+  interp->stkend = interp->stack + sp;
   qp_return (continuation_p (ret) ? ret : NIL);
 }
 
diff -r 4a20d9c7334b -r e5ce23e3e37d compiler.cpp
--- a/compiler.cpp	Fri Jun 01 12:39:49 2018 -0300
+++ b/compiler.cpp	Fri Jun 01 17:07:58 2018 -0300
@@ -81,7 +81,7 @@
   bvector *bytecode = nullptr;
   int bc_cap;
   int bc_len = 0;
-  int sp;
+  uint32_t sp;
   object ct_env;
   struct
     {
@@ -96,7 +96,7 @@
   clref *clrefs = nullptr;
   whl_block *whl = nullptr;
 
-  bc_emitter (interpreter *ip) : interp (ip), sp (ip->sp)
+  bc_emitter (interpreter *ip) : interp (ip), sp (ip->stkdiff ())
     {
       this->xdo.expr.car = UNBOUND;
       this->ct_env = NIL;
@@ -106,7 +106,7 @@
   ~bc_emitter ()
     {
       // Unwind the interpreter's stack.
-      this->interp->sp = sp;
+      this->interp->stkend = this->interp->stack + this->sp;
     }
 
   int next_label ()
@@ -1959,10 +1959,10 @@
   saved.len = QP_NELEM (saved_data);
   saved.data = saved_data;
 
-  saved.data[0] = interp->stack[interp->sp - 1];
-  saved.data[1] = interp->stack[interp->sp - 2];
-  saved.data[2] = interp->stack[interp->sp - 4];
-  saved.data[3] = interp->stack[interp->sp - 5];
+  saved.data[0] = *(interp->stkend - 1);
+  saved.data[1] = *(interp->stkend - 2);
+  saved.data[2] = *(interp->stkend - 4);
+  saved.data[3] = *(interp->stkend - 5);
   interp->aux = saved.as_obj ();
 
   for (ix = 0; ix < extra; ++ix)
@@ -2026,7 +2026,7 @@
     interp->stack + bp + ix, nrest);
 
   copy_objs (interp->stack + bp + nreq, argv, extra);
-  interp->sp = bp + nargs;
+  interp->stkend = interp->stack + bp + nargs;
   interp->push (saved.data[3]);
   interp->push (saved.data[2]);
   interp->push (intobj (nargs));
@@ -2034,7 +2034,7 @@
   interp->push (saved.data[0]);
 
   interp->aux = UNBOUND;
-  interp->cur_frame = interp->sp;
+  interp->cur_frame = interp->stkdiff ();
   return (nargs);
 }
 
@@ -2042,7 +2042,7 @@
 static object
 call_indexer (interpreter *interp, indexer_t fp, object seq, int n)
 {
-  return (fp (interp, seq, interp->stack[interp->sp - n],
+  return (fp (interp, seq, *(interp->stkend - n),
     n > 1 ? interp->stktop () : UNBOUND));
 }
 
@@ -2057,7 +2057,7 @@
   else if (n > 2)
     interp->raise_nargs ("index", 1, 2, n);
 
-  interp->stack[interp->sp - 1] = call_indexer (interp, fp, seq, n);
+  *(interp->stkend - 1) = call_indexer (interp, fp, seq, n);
   return (true);
 }
 
@@ -2181,8 +2181,8 @@
   int32_t sx = 0;
   const uint8_t *ip;
   object*& stack = interp->stack;
+  object*& stkend = interp->stkend;
   object& retval = interp->retval;
-  uint32_t& sp = interp->sp;
   object fn, *locals;
   locals_stack lstack (interp);
 
@@ -2215,9 +2215,10 @@
 #endif
 
 
-#define U_PUSH(val)   stack[sp++] = val
+#define U_PUSH(val)   *stkend++ = val
 #define U_PREV_LC()   \
   interp->dynframe_captured () ? &xaref(stack[bp], 0): stack + bp
+#define r_stkend(off)   *(stkend - (off))
 
   if (cdp != 0)
     { // Restored from a caught exception.
@@ -2243,10 +2244,10 @@
   else
     { // Regular function call.
     top:
-      fn = stack[sp - nargs - 1];
+      fn = r_stkend (nargs + 1);
       ip = as_bvector(fct_bcode (fn))->data;
       
-      bp = sp - nargs;
+      bp = interp->stkdiff () - nargs;
       interp->push_frame (as_fct(fn)->env, nargs, 0);
       lastf = interp->cur_frame;
 
@@ -2291,8 +2292,8 @@
               stack[bp + ix + 2] = stack[bp + nargs + 1];
               stack[bp + ix + 3] = intobj (ix + 1);
               stack[bp + ix + 5] = intobj (0);
-              sp = bp + ix + 6;
-              lastf = interp->cur_frame = sp;
+              stkend = stack + bp + ix + 6;
+              lastf = interp->cur_frame = interp->stkdiff ();
             }
         }
       else if (sx < 0)
@@ -2300,11 +2301,11 @@
       else
         {
           U_PUSH (intobj (0));
-          stack[sp - 3] = intobj (ix + 1);
-          stack[sp - 4] = stack[sp - 5];
-          stack[sp - 5] = stack[sp - 6];
-          stack[sp - 6] = NIL;
-          lastf = interp->cur_frame = sp;
+          r_stkend(3) = intobj (ix + 1);
+          r_stkend(4) = r_stkend (5);
+          r_stkend(5) = r_stkend (6);
+          r_stkend(6) = NIL;
+          lastf = interp->cur_frame = interp->stkdiff ();
         }
 
       nargs = ix + 1;
@@ -2316,7 +2317,7 @@
       NEXT_OP;
 
     OP_(DUP):
-      retval = stack[sp - 1];
+      retval = r_stkend(1);
       U_PUSH (retval);
       NEXT_OP;
 
@@ -2328,30 +2329,30 @@
     OP_(TCALLL):
       n = ip_ival (ip);
     do_tcall:
-      fn = stack[sp - n - 1];
+      fn = r_stkend (n + 1);
       if (fct_p (fn))
         {
           if (native_fct_p (fn))
             {
-              as_native_fct(fn)->call (interp, &stack[sp - n], n);
-              sp -= n;
-              stack[sp - 1] = retval;
+              as_native_fct(fn)->call (interp, stkend - n, n);
+              stkend -= n;
+              r_stkend(1) = retval;
               locals = U_PREV_LC ();
               NEXT_OP;
             }
 
           bp = lastf - interpreter::frame_overhead - nargs;
           interp->cur_frame = as_int (stack[lastf - 4]);
-          move_objs (&stack[bp - 1], &stack[sp - n - 1], n + 1);
-
-          sp = bp + n;
+          move_objs (&stack[bp - 1], stkend - n - 1, n + 1);
+
+          stkend = stack + bp + n;
           nargs = n;
           goto top;
         }
       else if (call_sequence (interp, fn, n))
         {
-          sp -= n;
-          stack[sp - 1] = retval;
+          stkend -= n;
+          r_stkend(1) = retval;
           locals = U_PREV_LC ();
           NEXT_OP;
         }
@@ -2363,14 +2364,14 @@
     OP_(CALLL):
       n = ip_ival (ip);
     do_call:
-      fn = stack[sp - n - 1];
+      fn = r_stkend (n + 1);
       if (fct_p (fn))
         {
           if (native_fct_p (fn))
             {
               as_native_fct(fn)->call (interp, n);
-              sp -= n;
-              stack[sp - 1] = retval;
+              stkend -= n;
+              r_stkend(1) = retval;
               locals = U_PREV_LC ();
               NEXT_OP;
             }
@@ -2382,8 +2383,8 @@
         }
       else if (call_sequence (interp, fn, n))
         {
-          sp -= n;
-          stack[sp - 1] = retval;
+          stkend -= n;
+          r_stkend(1) = retval;
           locals = U_PREV_LC ();
           NEXT_OP;
         }
@@ -2423,29 +2424,27 @@
       NEXT_OP;
 
     OP_(BRNEQ):
-      ip += stack[sp - 2] != stack[sp - 1] ?
+      ip += r_stkend (2) != r_stkend (1) ?
         (int16_t)get16 (ip) : sizeof (int16_t);
       interp->popn (2);
       NEXT_OP;
 
     OP_(BRNEQL):
-      ip += stack[sp - 2] != stack[sp - 1] ?
+      ip += r_stkend (2) != r_stkend (1) ?
         (int32_t)get32 (ip) : sizeof (int32_t);
       interp->popn (2);
       NEXT_OP;
 
     OP_(RET):
-      retval = stack[sp - 1];
-      sp = lastf;
-
-      if ((interp->cur_frame = as_int (stack[sp - 4])) == top_frame)
+      retval = r_stkend (1);
+      if ((interp->cur_frame = as_int (stack[lastf - 4])) == top_frame)
         return (retval);
 
-      sp -= interpreter::frame_overhead + as_int (stack[sp - 3]);
+      stkend = stack + lastf - (interpreter::frame_overhead +
+        as_int (stack[lastf - 3]));
       nargs = as_int (stack[interp->cur_frame - 3]);
-
       bp = interp->cur_frame - (interpreter::frame_overhead + nargs);
-      stack[sp - 1] = retval;
+      r_stkend(1) = retval;
       fn = stack[bp - 1];
 
       ip = as_bvector(fct_bcode (fn))->data +
@@ -2455,69 +2454,69 @@
       NEXT_OP;
 
     OP_(IS):
-      stack[sp - 2] = stack[sp - 2] == stack[sp - 1] ? QP_S(t) : NIL;
+      r_stkend(2) = r_stkend (2) == r_stkend (1) ? QP_S(t) : NIL;
       interp->popn ();
       NEXT_OP;
 
     OP_(NOT):
-      stack[sp - 1] = stack[sp - 1] == NIL ? QP_S(t) : NIL;
+      r_stkend(1) = r_stkend (1) == NIL ? QP_S(t) : NIL;
       NEXT_OP;
 
     OP_(CONS):
-      stack[sp - 2] = cons::make (interp, stack[sp - 2], stack[sp - 1]);
+      r_stkend(2) = cons::make (interp, r_stkend (2), r_stkend (1));
       interp->popn ();
       NEXT_OP;
 
     OP_(CAR):
-      if (!xcons_p (stack[sp - 1]))
+      if (!xcons_p (r_stkend (1)))
         interp->raise2 ("type-error",
           "apply: argument to 'car' is not a cons");
-      stack[sp - 1] = xcar (stack[sp - 1]);




More information about the Quipu-dev mailing list
Zurück zum Archiv-Index