[前][次][番号順一覧][スレッド一覧]

ruby-changes:53211

From: shyouhei <ko1@a...>
Date: Mon, 29 Oct 2018 12:21:25 +0900 (JST)
Subject: [ruby-changes:53211] shyouhei:r65426 (trunk): less verbose code by sharing attribute definitions

shyouhei	2018-10-29 12:21:22 +0900 (Mon, 29 Oct 2018)

  New Revision: 65426

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65426

  Log:
    less verbose code by sharing attribute definitions
    
    The idea behind this commit is that handles_sp and leaf are two
    concepts that are not mutually independent.  By making one explicitly
    depend another, we can reduces the number of lines of codes written,
    thus making things concise.

  Modified files:
    trunk/insns.def
    trunk/tool/ruby_vm/models/bare_instructions.rb
Index: tool/ruby_vm/models/bare_instructions.rb
===================================================================
--- tool/ruby_vm/models/bare_instructions.rb	(revision 65425)
+++ tool/ruby_vm/models/bare_instructions.rb	(revision 65426)
@@ -144,6 +144,7 @@ class RubyVM::BareInstructions https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/models/bare_instructions.rb#L144
   end
 
   def predefine_attributes
+    # Beware: order matters here because some attribute depends another.
     generate_attribute 'const char*', 'name', "insn_name(#{bin})"
     generate_attribute 'enum ruby_vminsn_type', 'bin', bin
     generate_attribute 'rb_num_t', 'open', opes.size
@@ -151,11 +152,24 @@ class RubyVM::BareInstructions https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/models/bare_instructions.rb#L152
     generate_attribute 'rb_num_t', 'retn', rets.size
     generate_attribute 'rb_num_t', 'width', width
     generate_attribute 'rb_snum_t', 'sp_inc', rets.size - pops.size
-    generate_attribute 'bool', 'handles_sp', false
-    generate_attribute 'bool', 'leaf', opes.all? {|o|
-      # Insn with ISEQ should yield it; can never be a leaf.
-      o[:type] != 'ISEQ'
-    }
+    generate_attribute 'bool', 'handles_sp', default_definition_of_handles_sp
+    generate_attribute 'bool', 'leaf', default_definition_of_leaf
+  end
+
+  def default_definition_of_handles_sp
+    # Insn with ISEQ should yield it; can handle sp.
+    return opes.any? {|o| o[:type] == 'ISEQ' }
+  end
+
+  def default_definition_of_leaf
+    # Insn that handles SP can never be a leaf.
+    if not has_attribute? 'handles_sp' then
+      return ! default_definition_of_handles_sp
+    elsif handles_sp? then
+      return "! #{call_attribute 'handles_sp'}"
+    else
+      return true
+    end
   end
 
   def typesplit a
Index: insns.def
===================================================================
--- insns.def	(revision 65425)
+++ insns.def	(revision 65426)
@@ -44,9 +44,11 @@ https://github.com/ruby/ruby/blob/trunk/insns.def#L44
          `insn_stack_increase`.
 
        * handles_sp: If it is true, VM deals with sp in the insn.
+         Default is if the instruction takes ISEQ operand or not.
 
        * leaf: indicates that the instruction is "leaf" i.e. it does
-         not introduce new stack frame on top of it.  Default true.
+         not introduce new stack frame on top of it.
+         If an instruction handles sp, that can never be a leaf.
 
    - Attributes can access operands, but not stack (push/pop) variables.
 
@@ -357,7 +359,7 @@ putiseq https://github.com/ruby/ruby/blob/trunk/insns.def#L359
 (ISEQ iseq)
 ()
 (VALUE ret)
-// attr bool leaf = true; /* yes it is */
+// attr bool handles_sp = false; /* of course it doesn't */
 {
     ret = (VALUE)iseq;
 }
@@ -713,7 +715,6 @@ defineclass https://github.com/ruby/ruby/blob/trunk/insns.def#L715
 (ID id, ISEQ class_iseq, rb_num_t flags)
 (VALUE cbase, VALUE super)
 (VALUE val)
-// attr bool handles_sp = true;
 {
     VALUE klass = vm_find_or_create_class_by_id(id, flags, cbase, super);
 
@@ -740,7 +741,6 @@ send https://github.com/ruby/ruby/blob/trunk/insns.def#L741
 (CALL_INFO ci, CALL_CACHE cc, ISEQ blockiseq)
 (...)
 (VALUE val)
-// attr bool handles_sp = true;
 // attr rb_snum_t sp_inc = - (int)(ci->orig_argc + ((ci->flag & VM_CALL_ARGS_BLOCKARG) ? 1 : 0));
 {
     struct rb_calling_info calling;
@@ -757,7 +757,6 @@ opt_send_without_block https://github.com/ruby/ruby/blob/trunk/insns.def#L757
 (CALL_INFO ci, CALL_CACHE cc)
 (...)
 (VALUE val)
-// attr bool leaf = false; /* Of course it isn't. */
 // attr bool handles_sp = true;
 // attr rb_snum_t sp_inc = -ci->orig_argc;
 {
@@ -828,7 +827,6 @@ invokesuper https://github.com/ruby/ruby/blob/trunk/insns.def#L827
 (CALL_INFO ci, CALL_CACHE cc, ISEQ blockiseq)
 (...)
 (VALUE val)
-// attr bool handles_sp = true;
 // attr rb_snum_t sp_inc = - (int)(ci->orig_argc + ((ci->flag & VM_CALL_ARGS_BLOCKARG) ? 1 : 0));
 {
     struct rb_calling_info calling;
@@ -845,7 +843,6 @@ invokeblock https://github.com/ruby/ruby/blob/trunk/insns.def#L843
 (CALL_INFO ci)
 (...)
 (VALUE val)
-// attr bool leaf = false; /* Of course it isn't. */
 // attr bool handles_sp = true;
 // attr rb_snum_t sp_inc = 1 - ci->orig_argc;
 {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]