diff libinterp/corefcn/call-stack.cc @ 32814:2eabd2567846 bytecode-interpreter

Refactor dynamic stack frame for bytecode interpreter (bug #65191) Split up the bytecode_fcn_stack_frame into fcn, script and nested to detangle unnecessary messiness. Also solve some bugs where handles to nested function did not look for symbols in the correct frame and did not push the closure frame properly. * call-stack.cc: Remove redundant check for bytecode frames Use new stack_frame::create_xxx fcns * stack-frame.cc: Refactor bytecode_fcn_stack_frame * stack-frame.h: New create_bytecode_nested () New create_bytecode_anon () New create_bytecode_script () * ov_base.h: New enum value OCT_NESTED_HANDLE * ov-fcn-handle.h: New get_closure_frame () * ov-fcn-handle.cc * ov-ref.cc: Use offset overload * ov.h: More friends * pt-bytecode-vm.cc: Handle bytecode calls to nested handles * pt-butecode-walk.cc: Remove fake offset workaround * bytecode_nested.m: Update tests
author Petter T. <petter.vilhelm@gmail.com>
date Mon, 22 Jan 2024 23:38:08 +0100
parents e4e83855224f
children e6814a319d4e
line wrap: on
line diff
--- a/libinterp/corefcn/call-stack.cc	Tue Jan 23 10:48:41 2024 -0500
+++ b/libinterp/corefcn/call-stack.cc	Mon Jan 22 23:38:08 2024 +0100
@@ -481,7 +481,7 @@
   get_new_frame_index_and_links (new_frame_idx, parent_link, static_link);
 
   std::shared_ptr<stack_frame> new_frame =
-    stack_frame::create_bytecode (
+    stack_frame::create_bytecode_script (
        m_evaluator,
        fcn,
        vm,
@@ -505,11 +505,21 @@
 
   get_new_frame_index_and_links (new_frame_idx, parent_link, static_link);
 
-  std::shared_ptr<stack_frame> new_frame
-    = stack_frame::create_bytecode (m_evaluator, fcn, vm,
-                                    new_frame_idx,
-                                    parent_link, static_link, closure_frames,
-                                    nargout, nargin);
+  std::shared_ptr<stack_frame> new_frame;
+
+  if (fcn->is_nested_function ())
+    new_frame = stack_frame::create_bytecode_nested (m_evaluator, fcn, vm,
+                                           new_frame_idx,
+                                           parent_link, static_link, closure_frames,
+                                           nargout, nargin);
+  else
+    {
+      CHECK_PANIC (fcn->is_anonymous_function ());
+      new_frame = stack_frame::create_bytecode_anon (m_evaluator, fcn, vm,
+                                                     new_frame_idx,
+                                                     parent_link, static_link, closure_frames,
+                                                     nargout, nargin);
+    }
 
   m_cs.push_back (new_frame);
 
@@ -524,15 +534,42 @@
 
   get_new_frame_index_and_links (new_frame_idx, parent_link, static_link);
 
-  std::shared_ptr<stack_frame> new_frame
-    = stack_frame::create_bytecode (m_evaluator, fcn, vm,
-                                    new_frame_idx, // ??? index
-                                    parent_link, static_link,
-                                    nargout, nargin);
+  if (fcn->is_nested_function())
+    {
+      std::shared_ptr<stack_frame> new_frame
+        = stack_frame::create_bytecode_nested (m_evaluator, fcn, vm,
+                                               new_frame_idx, // ??? index
+                                               parent_link, static_link, nullptr,
+                                               nargout, nargin);
+
+      m_cs.push_back (new_frame);
 
-  m_cs.push_back (new_frame);
+      m_curr_frame = new_frame_idx;
+    }
+  else if (fcn->is_anonymous_function())
+    {
+      std::shared_ptr<stack_frame> new_frame
+        = stack_frame::create_bytecode (m_evaluator, fcn, vm,
+                                        new_frame_idx, // ??? index
+                                        parent_link, static_link,
+                                        nargout, nargin);
+
+      m_cs.push_back (new_frame);
 
-  m_curr_frame = new_frame_idx;
+      m_curr_frame = new_frame_idx;
+    }
+  else
+    {
+      std::shared_ptr<stack_frame> new_frame
+        = stack_frame::create_bytecode (m_evaluator, fcn, vm,
+                                        new_frame_idx, // ??? index
+                                        parent_link, static_link,
+                                        nargout, nargin);
+
+      m_cs.push_back (new_frame);
+
+      m_curr_frame = new_frame_idx;
+    }
 }
 
 #endif
@@ -585,7 +622,7 @@
   std::shared_ptr<stack_frame> frm = m_cs[user_frame];
 
   if (! (frm->is_user_fcn_frame () || frm->is_user_script_frame ()
-         || frm->is_scope_frame () || frm->is_bytecode_fcn_frame()))
+         || frm->is_scope_frame ()))
     {
       frm = frm->static_link ();
 
@@ -627,8 +664,7 @@
 
   if (! (frm && (frm->is_user_fcn_frame ()
                  || frm->is_user_script_frame ()
-                 || frm->is_scope_frame ()
-                 || frm->is_bytecode_fcn_frame ())))
+                 || frm->is_scope_frame ())))
     error ("call_stack::dbupdown: invalid initial frame in call stack!");
 
   // Use index into the call stack to begin the search.  At this point
@@ -664,7 +700,7 @@
       frm = m_cs[xframe];
 
       if (frm->is_user_fcn_frame () || frm->is_user_script_frame ()
-          || frm->is_scope_frame () || frm->is_bytecode_fcn_frame ())
+          || frm->is_scope_frame ())
         {
           last_good_frame = xframe;
 
@@ -751,7 +787,7 @@
       std::shared_ptr<stack_frame> frm = m_cs[n];
 
       if (frm->is_user_script_frame () || frm->is_user_fcn_frame ()
-          || frm->is_scope_frame () || frm->is_bytecode_fcn_frame())
+          || frm->is_scope_frame ())
         {
           if (frm->index () == curr_frame)
             curr_user_frame = frames.size ();
@@ -786,7 +822,7 @@
   for (const auto& frm : frames)
     {
       if (frm->is_user_script_frame () || frm->is_user_fcn_frame ()
-          || frm->is_scope_frame () || frm->is_bytecode_fcn_frame())
+          || frm->is_scope_frame ())
         {
           retval.push_back (frame_info (frm->fcn_file_name (),
                                         frm->fcn_name (print_subfn),
@@ -826,7 +862,7 @@
   for (const auto& frm : frames)
     {
       if (frm->is_user_script_frame () || frm->is_user_fcn_frame ()
-          || frm->is_scope_frame () || frm->is_bytecode_fcn_frame())
+          || frm->is_scope_frame ())
         {
           file(k) = frm->fcn_file_name ();
           name(k) = frm->fcn_name (print_subfn);