[Quipu-dev] quipu/quipu: Improve compiler definitions (incomplete)

Back to archive index

scmno****@osdn***** scmno****@osdn*****
Mon Jun 11 11:31:33 JST 2018


changeset dc2dd55b5f9a in quipu/quipu
details: http://hg.osdn.jp/view/quipu/quipu?cmd=changeset;node=dc2dd55b5f9a
user: Agustina Arzille <avarz****@riseu*****>
date: Mon Jun 11 02:31:13 2018 +0000
description: Improve compiler definitions (incomplete)

diffstat:

 compiler.cpp |  87 ++++++++++++++++++++++-------------------------------------
 1 files changed, 33 insertions(+), 54 deletions(-)

diffs (252 lines):

diff -r ae8d402ece24 -r dc2dd55b5f9a compiler.cpp
--- a/compiler.cpp	Sun Jun 10 03:59:07 2018 +0000
+++ b/compiler.cpp	Mon Jun 11 02:31:13 2018 +0000
@@ -37,8 +37,6 @@
   return (ret);
 }
 
-class whl_block;
-
 class xcmp_call
 {
 public:
@@ -80,6 +78,13 @@
 class bc_emitter
 {
 public:
+  enum
+    {
+      flg_outer_ref       = 0x01,
+      flg_captured        = 0x02,
+      flg_emitted_captenv = 0x04
+    };
+
   typedef sorted_list<xcmp_call> ctable_t;
 
   class frame_data
@@ -98,7 +103,6 @@
   ctable_t ctable;
   std::vector<object> code;
   int nconst = 0;
-  int cdepth = INT_MAX;
   int lbl_cnt = 0;
   bvector *bytecode = nullptr;
   int bc_cap;
@@ -115,9 +119,7 @@
 
   whl_block *whl = nullptr;
   std::vector<frame_data> frames;
-  bool outer_ref = false;
-  bool captured = false;
-  bool emitted_captenv = false;
+  uint32_t rflags = 0;
 
   int cur_stkpos () const
     {
@@ -184,11 +186,6 @@
       this->whl = this->whl->next;
     }
 
-  void set_cdepth (int val)
-    {
-      this->cdepth = min (this->cdepth, val);
-    }
-
   int index (object val)
     {
       auto it = this->ctable.find (val);
@@ -319,9 +316,8 @@
   int compile_in (object env, bool tail, object expr);
   void emit_optargs_init (object env, object opta, object vars, int idx);
 
-  object compile_fct (object env, object expr, int& depth);
-  object compile_fct_body (object env, object expr,
-    int& depth, uint32_t flags = 0);
+  object compile_fct (object env, object expr);
+  object compile_fct_body (object env, object expr, uint32_t flags = 0);
   int compile_let (object env, bool tail, object expr);
 
   int last_idx () const
@@ -577,7 +573,11 @@
           continue;
         }
       else if (vi == OPX_(FAKECAPT))
-        continue;
+        {
+          if (this->rflags & flg_captured)
+            this->bytecode_write ((uint8_t)OP_CAPTENV);
+          continue;
+        }
 
       const bcode_instr *instrp = bcode_get (as_int (vi));
       if (large && !instrp->long_p () && instrp->branch_p ())
@@ -655,18 +655,6 @@
   return (-1);
 }
 
-static inline int
-nnn (object obj)
-{
-  // Count the number of non-nil's.
-  int ret = 0;
-  for (; obj != NIL; obj = xcdr (obj))
-    if (xcar (obj) != NIL)
-      ++ret;
-
-  return (ret);
-}
-
 enum
 {
   SPECFORM_DOTTED,
@@ -879,16 +867,15 @@
     }
   else if (depth >= (int)this->frames.size ())
     {
-      this->outer_ref = true;
+      this->rflags |= flg_outer_ref;
       this->emit (ixs[1], intobj (loc),
         intobj (depth - (int)this->frames.size ()));
-      this->set_cdepth (nnn (xcdr (env)) - 1 - depth);
     }
   else
     {
       auto& f = this->frames[this->frames.size () - depth - 1];
-      if (this->captured)
-        this->emit (ixs[3], intobj (f.acc + f.nargs - 1), intobj (loc));
+      if (this->rflags & flg_captured)
+        this->emit (ixs[3], intobj (f.acc + f.nargs), intobj (loc));
       else
         this->emit (ixs[0], intobj (f.acc + loc));
     }
@@ -1321,17 +1308,15 @@
           else if (xcdr (expr) == NIL)
             specform_error (this->interp, "fct", SPECFORM_TOOFEW);
           
-          int depth;
           bc_emitter bc (this->interp);
-          object fn = bc.compile_fct (env, xcdr (expr), depth);
+          object fn = bc.compile_fct (env, xcdr (expr));
 
           this->interp->push (fn);
           this->emit (OPX_(LOADV), fn);
-          this->set_cdepth (depth);
-          if (depth < nnn (env) || bc.outer_ref)
+          if (bc.rflags & flg_outer_ref)
             {
               this->emit (OPX_(CLOSURE));
-              this->captured = true;
+              this->rflags |= flg_captured;
             }
 
           return (EVR_ATOM_SE);
@@ -1422,23 +1407,20 @@
 
       case SF_CALLCC:
         {
-          int depth;
           bc_emitter bc (this->interp);
           cons cc_env;
 
           cc_env.car = xcadr (expr);
           cc_env.cdr = env;
-          object cls =
-              bc.compile_fct_body (cc_env.as_obj (), xcddr (expr), depth);
+          object cls = bc.compile_fct_body (cc_env.as_obj (), xcddr (expr));
 
           this->interp->push (cls);
           this->emit (OPX_(LOADV), cls);
-          this->set_cdepth (depth);
           this->emit (OPX_(CLOSURE));
           this->emit (OPX_(LOADNIL));  
           this->emit (OPX_(MKCONT), intobj (2));
           this->emit (tail ? OPX_(TCALL) : OPX_(CALL), intobj (1));
-          this->captured = true;
+          this->rflags |= flg_captured;
           break;
         }
 
@@ -1600,7 +1582,7 @@
   return (interp->alval);
 }
 
-object bc_emitter::compile_fct (object env, object expr, int& depth)
+object bc_emitter::compile_fct (object env, object expr)
 {
   object atail = NIL;
   object vars, args = xcar (expr);
@@ -1684,11 +1666,10 @@
   tmp.car = vars;
   tmp.cdr = env;
 
-  return (this->compile_fct_body (tmp.as_obj (), xcdr (expr), depth));
+  return (this->compile_fct_body (tmp.as_obj (), xcdr (expr)));
 }
 
-object bc_emitter::compile_fct_body (object env,
-  object expr, int& depth, uint32_t flags)
+object bc_emitter::compile_fct_body (object env, object expr, uint32_t flags)
 {
   int r;
 
@@ -1711,7 +1692,6 @@
   retp->bcode = this->encode ();
   retp->vals = this->idxvec ();
   retp->max_sp = retp->max_stack ();
-  depth = this->cdepth;
   return (this->interp->pop ());
 }
 
@@ -1731,7 +1711,6 @@
         eval (self.interp, xcadr (ev));
       else
         {
-          int d;
           bc_emitter tmp (self.interp);
           object prev = NIL;
 
@@ -1742,7 +1721,7 @@
               ctvs[nctvs - 1].cdr = NIL;
             }
 
-          tmp.compile_fct (NIL, xcdr (ev), d);
+          tmp.compile_fct (NIL, xcdr (ev));
           if (nctvs > 0)
             ctvs[nctvs - 1].cdr = prev;
         }
@@ -1854,10 +1833,10 @@
            * an environment capture in case the body refers to a variable
            * from the outer frame. Here we emit a few placeholders that we'll
            * eventually patch or remove. */
-          if (!this->emitted_captenv)
+          if (!(this->rflags & flg_emitted_captenv))
             {
               this->code.push_back (OPX_(FAKECAPT));
-              this->emitted_captenv = true;
+              this->rflags |= flg_emitted_captenv;
             }
 
           this->emit (OPX_(MKFRAME), intobj (0));
@@ -1920,9 +1899,8 @@
 
 object compile_fct (interpreter *interp, object expr)
 {
-  int depth;
   bc_emitter bc (interp);
-  return (bc.compile_fct (NIL, expr, depth));
+  return (bc.compile_fct (NIL, expr));
 }
 
 object compile_expr (interpreter *interp, object expr)
@@ -2785,8 +2763,9 @@
       if (sx > 0 && !interp->dynframe_captured ())
         {
           array *ap = as_array (alloc_array (interp, sx + 1, NIL));
-          copy_objs (ap->data, &stack[bp], sx + 1);
-          stack[bp] = stack[interp->cur_frame - 5] = interp->alval;
+          int nbp = interp->cur_frame - interpreter::frame_size - sx;
+          copy_objs (ap->data, &stack[nbp], sx + 1);
+          stack[nbp] = stack[interp->cur_frame - 5] = interp->alval;
           interp->dynframe_set_captured ();
         }
 




More information about the Quipu-dev mailing list
Back to archive index