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

ruby-changes:54707

From: shyouhei <ko1@a...>
Date: Fri, 25 Jan 2019 23:09:16 +0900 (JST)
Subject: [ruby-changes:54707] shyouhei:r66923 (trunk): vm.inc now in C99

shyouhei	2019-01-25 23:09:10 +0900 (Fri, 25 Jan 2019)

  New Revision: 66923

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

  Log:
    vm.inc now in C99
    
    This changeset modifies the VM generator so that vm.inc is written in
    C99.  Also added some comments in _insn_entry.erb so that the
    intention of each parts to be made more clear.  I think this improves
    overall readability of the generated VM.
    
    Confirmed that the exact same binary is generated before/after this
    changeset.

  Modified files:
    trunk/tool/ruby_vm/helpers/dumper.rb
    trunk/tool/ruby_vm/models/bare_instructions.rb
    trunk/tool/ruby_vm/models/operands_unifications.rb
    trunk/tool/ruby_vm/views/_insn_entry.erb
    trunk/tool/ruby_vm/views/_mjit_compile_insn.erb
    trunk/vm_insnhelper.h
Index: vm_insnhelper.h
===================================================================
--- vm_insnhelper.h	(revision 66922)
+++ vm_insnhelper.h	(revision 66923)
@@ -137,6 +137,7 @@ enum vm_regan_acttype { https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.h#L137
 
 #if VM_CHECK_MODE > 0
 #define SETUP_CANARY() \
+    VALUE * canary; \
     if (leaf) { \
         canary = GET_SP(); \
         SET_SV(vm_stack_canary); \
Index: tool/ruby_vm/views/_mjit_compile_insn.erb
===================================================================
--- tool/ruby_vm/views/_mjit_compile_insn.erb	(revision 66922)
+++ tool/ruby_vm/views/_mjit_compile_insn.erb	(revision 66923)
@@ -25,7 +25,7 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_mjit_compile_insn.erb#L25
 
 % # JIT: Set const expressions for `RubyVM::OperandsUnifications` insn
 % insn.preamble.each do |amble|
-        fprintf(f, "<%= amble.expr %>\n");
+        fprintf(f, "<%= amble.expr.sub(/const \S+\s+/, '') %>\n");
 % end
 %
 % # JIT: Initialize operands
Index: tool/ruby_vm/views/_insn_entry.erb
===================================================================
--- tool/ruby_vm/views/_insn_entry.erb	(revision 66922)
+++ tool/ruby_vm/views/_insn_entry.erb	(revision 66923)
@@ -6,32 +6,36 @@ https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_insn_entry.erb#L6
 %# conditions mentioned  in the  file COPYING  are met.   Consult the  file for
 %# details.
 %;
+% body = render_c_expr(insn.expr).gsub(/^#/, '#   ')
 
 /* insn <%= insn.pretty_name %> */
 INSN_ENTRY(<%= insn.name %>)
 {
-%#  NAME_OF_CURRENT_INSN is used in vm_exec.h
-#   define NAME_OF_CURRENT_INSN <%= insn.name %>
-#   define INSN_ATTR(x) <%= insn.call_attribute(' ## x ## ') %>
-    bool leaf;
-    MAYBE_UNUSED(VALUE *) canary;
-% unless insn.declarations.empty?
-    <%= insn.declarations.join(";\n    ") %>;
-
-% end
+    /* ###  Declare that we have just entered into an instruction. ### */
     START_OF_ORIGINAL_INSN(<%= insn.name %>);
+    DEBUG_ENTER_INSN(<%=cstr insn.name %>);
+
+    /* ###  Declare and assign variables. ### */
 % insn.preamble.each do |konst|
 <%= render_c_expr konst -%>
 % end
+%
 % insn.opes.each_with_index do |ope, i|
-    <%= ope[:name] %> = (<%= ope[:type] %>)GET_OPERAND(<%= i + 1 %>);
+    <%= ope[:decl] %> = (<%= ope[:type] %>)GET_OPERAND(<%= i + 1 %>);
 % end
-%
+#   define INSN_ATTR(x) <%= insn.call_attribute(' ## x ## ') %>
+    bool leaf = INSN_ATTR(leaf);
 % insn.pops.reverse_each.with_index.reverse_each do |pop, i|
-    <%= pop[:name] %> = <%= insn.cast_from_VALUE pop, "TOPN(#{i})"%>;
+    <%= pop[:decl] %> = <%= insn.cast_from_VALUE pop, "TOPN(#{i})"%>;
+% end
+%
+% insn.rets.each do |ret|
+%   next if insn.has_ope?(ret) or insn.has_pop?(ret)
+    <%= ret[:decl] %>;
 % end
-    DEBUG_ENTER_INSN(INSN_ATTR(name));
-    if (! (leaf = INSN_ATTR(leaf))) ADD_PC(INSN_ATTR(width));
+
+    /* ### Instruction preambles. ### */
+    if (! leaf) ADD_PC(INSN_ATTR(width));
 % if insn.handles_sp?
     POPN(INSN_ATTR(popn));
 % end
@@ -40,7 +44,16 @@ INSN_ENTRY(<%= insn.name %>) https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_insn_entry.erb#L44
 % insn.opes.each_with_index do |ope, i|
     COLLECT_USAGE_OPERAND(INSN_ATTR(bin), <%= i %>, <%= ope[:name] %>);
 % end
-<%= render_c_expr insn.expr -%>
+% unless body.empty?
+
+    /* ### Here we do the instruction body. ### */
+%#  NAME_OF_CURRENT_INSN is used in vm_exec.h
+#   define NAME_OF_CURRENT_INSN <%= insn.name %>
+<%= body -%>
+#   undef NAME_OF_CURRENT_INSN
+% end
+
+    /* ### Instruction trailers. ### */
     CHECK_VM_STACK_OVERFLOW_FOR_INSN(VM_REG_CFP, INSN_ATTR(retn));
 <%= insn.handle_canary "CHECK_CANARY()" -%>
 % if insn.handles_sp?
@@ -54,7 +67,8 @@ INSN_ENTRY(<%= insn.name %>) https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/views/_insn_entry.erb#L67
 %   end
 % end
     if (leaf) ADD_PC(INSN_ATTR(width));
-    END_INSN(<%= insn.name %>);
 #   undef INSN_ATTR
-#   undef NAME_OF_CURRENT_INSN
+
+    /* ### Leave the instruction. ### */
+    END_INSN(<%= insn.name %>);
 }
Index: tool/ruby_vm/helpers/dumper.rb
===================================================================
--- tool/ruby_vm/helpers/dumper.rb	(revision 66922)
+++ tool/ruby_vm/helpers/dumper.rb	(revision 66923)
@@ -49,8 +49,8 @@ class RubyVM::Dumper https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/helpers/dumper.rb#L49
   end
 
   def replace_pragma_line str, lineno
-    if str == "#pragma RubyVM reset source\n" then
-      return "#line #{lineno + 2} #{@file}\n"
+    if /#(\s*)pragma RubyVM reset source\n/ =~ str then
+      return "##{$1}line #{lineno + 2} #{@file}\n"
     else
       return str
     end
Index: tool/ruby_vm/models/operands_unifications.rb
===================================================================
--- tool/ruby_vm/models/operands_unifications.rb	(revision 66922)
+++ tool/ruby_vm/models/operands_unifications.rb	(revision 66923)
@@ -31,7 +31,8 @@ class RubyVM::OperandsUnifications < Rub https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/models/operands_unifications.rb#L31
     @preamble        = parts[:preamble]
     @spec            = parts[:spec]
     super json.merge(:template => template)
-    parts[:vars].each do |v|
+    @konsts = parts[:vars]
+    @konsts.each do |v|
       @variables[v[:name]] ||= v
     end
   end
@@ -63,6 +64,10 @@ class RubyVM::OperandsUnifications < Rub https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/models/operands_unifications.rb#L64
     end
   end
 
+  def has_ope? var
+    super or @konsts.any? {|i| i[:name] == var[:name] }
+  end
+
   private
 
   def namegen signature
@@ -101,7 +106,7 @@ class RubyVM::OperandsUnifications < Rub https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/models/operands_unifications.rb#L106
           vars << k
           src << {
             location: location,
-            expr: "    #{k[:name]} = #{j};"
+            expr: "    const #{k[:decl]} = #{j};"
           }
         end
       end
Index: tool/ruby_vm/models/bare_instructions.rb
===================================================================
--- tool/ruby_vm/models/bare_instructions.rb	(revision 66922)
+++ tool/ruby_vm/models/bare_instructions.rb	(revision 66923)
@@ -131,6 +131,14 @@ class RubyVM::BareInstructions https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/models/bare_instructions.rb#L131
     sprintf "#<%s %s@%s:%d>", self.class.name, @name, @loc[0], @loc[1]
   end
 
+  def has_ope? var
+    return @opes.any? {|i| i[:name] == var[:name] }
+  end
+
+  def has_pop? var
+    return @pops.any? {|i| i[:name] == var[:name] }
+  end
+
   private
 
   def generate_attribute t, k, v

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

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