diff libinterp/corefcn/symrec.cc @ 26661:cf9e10ce3351

move variable values from symbol_record objects to stack_frame objects Apologies for the massive commit. I see no way to untangle these changes into a set of smaller incremental changes in a way that would be more useful. Previously, handling data for recursive function calls was managed by a stack of values in the symbol_record class and an auxiliary integer variable was used for managing the recursion depth (context_id). Now, values for local variables are in the stack_frame class and recursion is handled naturally by the call_stack as a new stack frame is added to the call_stack object for any call to a function or a script. Values for internal function call information (nargin, nargout, etc.) are now stored specially in the stack_frame object. Values for global variables are now stored in a map in the call_stack object. Values for persistent variables are stored in the corresponding octave_user_function object. Access to non-local variables inside nested functions is managed through pointers to stack_frame objects for the parent function scopes. The new implementation more closely resembles the techniques described in standard compiler literature. These changes should make it easier to create proper closures and finally solve bug #39257 (handles to nested functions are not yet supported). They may also make it easier to implement JIT compiler, though that is probably still a long way off. The new stack-frame.h file has some details about the new implementation of stack frames that should help in understanding how things work now. Describing each change to each file and function will probably not provide much greater understanding of the changes and would be quite tedious to write so I am omitting them.
author John W. Eaton <jwe@octave.org>
date Mon, 28 Jan 2019 18:01:46 +0000
parents 00f796120a6d
children 711f23332204
line wrap: on
line diff
--- a/libinterp/corefcn/symrec.cc	Sat Jan 26 15:53:29 2019 +0000
+++ b/libinterp/corefcn/symrec.cc	Mon Jan 28 18:01:46 2019 +0000
@@ -37,85 +37,25 @@
 #include "interpreter.h"
 #include "symrec.h"
 #include "symscope.h"
-#include "symtab.h"
 
 namespace octave
 {
-  symbol_record::context_id
-  symbol_record::symbol_record_rep::get_fwd_scope_context (void) const
-  {
-    // This should NOT recurse.  We only want to know the current
-    // context of the immediately forwarded rep object.  This is used
-    // only in symbol_record::symbol_record_rep::varref and
-    // symbol_record::symbol_record_rep::varval.
-
-    auto t_fwd_scope = m_fwd_scope.lock ();
-    return t_fwd_scope ? t_fwd_scope->current_context () : 0;
-  }
-
-  void
-  symbol_record::symbol_record_rep::init_persistent (void)
+  std::shared_ptr<symbol_record::symbol_record_rep>
+  symbol_record::symbol_record_rep::dup (void) const
   {
-    if (auto t_fwd_rep = m_fwd_rep.lock ())
-      {
-        t_fwd_rep->init_persistent ();
-        return;
-      }
-
-    mark_persistent ();
-  }
-
-  std::shared_ptr<symbol_record::symbol_record_rep>
-  symbol_record::symbol_record_rep::dup (const std::shared_ptr<symbol_scope_rep>& new_scope) const
-  {
-    // FIXME: is this the right thing do to?
-    if (auto t_fwd_rep = m_fwd_rep.lock ())
-      return t_fwd_rep->dup (new_scope);
-
-    static const context_id FIXME_CONTEXT = 0;
-
-    return std::shared_ptr<symbol_record_rep>
-      (new symbol_record_rep (m_name, varval (FIXME_CONTEXT),
-                              m_storage_class));
+    return std::shared_ptr<symbol_record::symbol_record_rep> (new symbol_record_rep (*this));
   }
 
   octave_value
-  symbol_record::symbol_record_rep::dump (context_id context) const
+  symbol_record::symbol_record_rep::dump (void) const
   {
-    if (auto t_fwd_rep = m_fwd_rep.lock ())
-      return t_fwd_rep->dump (context);
-
     std::map<std::string, octave_value> m
-      = {{ "name", m_name },
+      = {{ "frame_offset", m_frame_offset },
+         { "data_offset", m_data_offset },
+         { "name", m_name },
          { "local", is_local () },
-         { "automatic", is_automatic () },
-         { "formal", is_formal () },
-         { "hidden", is_hidden () },
-         { "inherited", is_inherited () },
-         { "global", is_global () },
-         { "persistent", is_persistent () }};
-
-    octave_value val = varval (context);
-
-    if (val.is_defined ())
-      m["value"] = val;
+         { "formal", is_formal () }};
 
     return octave_value (m);
   }
-
-  octave_value
-  symbol_record::find_function (const std::string& name,
-                                const octave_value_list& args) const
-  {
-    // FIXME: it would be better if the symbol_record object did not
-    // look back to the symbol_table when the value is undefined.  More
-    // refactoring is needed...
-
-    symbol_table& symtab
-      = __get_symbol_table__ ("symbol_record::find_function");
-
-    return symtab.find_function (name, args);
-  }
-
-  octave_value symbol_record::dummy_octave_value;
 }