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

ruby-changes:73696

From: Takashi <ko1@a...>
Date: Fri, 23 Sep 2022 06:45:08 +0900 (JST)
Subject: [ruby-changes:73696] 5cda5938f8 (master): Bindgen enum with builtin

https://git.ruby-lang.org/ruby.git/commit/?id=5cda5938f8

From 5cda5938f899ca9ac72f1e44c216f63826516422 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Sun, 18 Sep 2022 23:43:24 +0900
Subject: Bindgen enum with builtin

---
 common.mk            |  3 ++-
 lib/mjit/c_32.rb     |  8 ------
 lib/mjit/c_64.rb     |  8 ------
 mjit_c.rb            | 24 ++++++++++++++---
 tool/mjit/bindgen.rb | 76 ++++++++++++++++++----------------------------------
 5 files changed, 48 insertions(+), 71 deletions(-)

diff --git a/common.mk b/common.mk
index 52f132a2f8..4678fd8940 100644
--- a/common.mk
+++ b/common.mk
@@ -9938,12 +9938,13 @@ mjit_compiler.$(OBJEXT): {$(VPATH)}iseq.h https://github.com/ruby/ruby/blob/trunk/common.mk#L9938
 mjit_compiler.$(OBJEXT): {$(VPATH)}method.h
 mjit_compiler.$(OBJEXT): {$(VPATH)}missing.h
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit.h
+mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_c.rb
+mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_c.rbinc
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compile_attr.inc
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.c
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.h
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.rb
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.rbinc
-mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_c.rbinc
 mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_unit.h
 mjit_compiler.$(OBJEXT): {$(VPATH)}node.h
 mjit_compiler.$(OBJEXT): {$(VPATH)}ruby_assert.h
diff --git a/lib/mjit/c_32.rb b/lib/mjit/c_32.rb
index f2acfd1761..59ca977093 100644
--- a/lib/mjit/c_32.rb
+++ b/lib/mjit/c_32.rb
@@ -1,14 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/lib/mjit/c_32.rb#L1
 require_relative 'c_type'
 
 module RubyVM::MJIT
-  def C.VM_METHOD_TYPE_CFUNC = 1
-
-  def C.VM_METHOD_TYPE_ISEQ = 0
-
-  def C.VM_CALL_KW_SPLAT_bit = 7
-
-  def C.VM_CALL_TAILCALL_bit = 8
-
   def C.CALL_DATA
     @CALL_DATA ||= self.rb_call_data
   end
diff --git a/lib/mjit/c_64.rb b/lib/mjit/c_64.rb
index 4b0d5276eb..9ea32bd327 100644
--- a/lib/mjit/c_64.rb
+++ b/lib/mjit/c_64.rb
@@ -1,14 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/lib/mjit/c_64.rb#L1
 require_relative 'c_type'
 
 module RubyVM::MJIT
-  def C.VM_METHOD_TYPE_CFUNC = 1
-
-  def C.VM_METHOD_TYPE_ISEQ = 0
-
-  def C.VM_CALL_KW_SPLAT_bit = 7
-
-  def C.VM_CALL_TAILCALL_bit = 8
-
   def C.CALL_DATA
     @CALL_DATA ||= self.rb_call_data
   end
diff --git a/mjit_c.rb b/mjit_c.rb
index ac3b2a2ff2..452b534bd3 100644
--- a/mjit_c.rb
+++ b/mjit_c.rb
@@ -123,10 +123,6 @@ module RubyVM::MJIT https://github.com/ruby/ruby/blob/trunk/mjit_c.rb#L123
 
   ### MJIT bindgen begin ###
 
-  def C.NOT_COMPILED_STACK_SIZE
-    Primitive.cexpr! %q{ INT2NUM(NOT_COMPILED_STACK_SIZE) }
-  end
-
   def C.USE_LAZY_LOAD
     Primitive.cexpr! %q{ RBOOL(USE_LAZY_LOAD != 0) }
   end
@@ -135,13 +131,33 @@ module RubyVM::MJIT https://github.com/ruby/ruby/blob/trunk/mjit_c.rb#L131
     Primitive.cexpr! %q{ RBOOL(USE_RVARGC != 0) }
   end
 
+  def C.NOT_COMPILED_STACK_SIZE
+    Primitive.cexpr! %q{ INT2NUM(NOT_COMPILED_STACK_SIZE) }
+  end
+
   def C.VM_CALL_KW_SPLAT
     Primitive.cexpr! %q{ INT2NUM(VM_CALL_KW_SPLAT) }
   end
 
+  def C.VM_CALL_KW_SPLAT_bit
+    Primitive.cexpr! %q{ INT2NUM(VM_CALL_KW_SPLAT_bit) }
+  end
+
   def C.VM_CALL_TAILCALL
     Primitive.cexpr! %q{ INT2NUM(VM_CALL_TAILCALL) }
   end
 
+  def C.VM_CALL_TAILCALL_bit
+    Primitive.cexpr! %q{ INT2NUM(VM_CALL_TAILCALL_bit) }
+  end
+
+  def C.VM_METHOD_TYPE_CFUNC
+    Primitive.cexpr! %q{ INT2NUM(VM_METHOD_TYPE_CFUNC) }
+  end
+
+  def C.VM_METHOD_TYPE_ISEQ
+    Primitive.cexpr! %q{ INT2NUM(VM_METHOD_TYPE_ISEQ) }
+  end
+
   ### MJIT bindgen end ###
 end if RubyVM::MJIT.enabled?
diff --git a/tool/mjit/bindgen.rb b/tool/mjit/bindgen.rb
index 98d89e17a4..ac95bbe639 100755
--- a/tool/mjit/bindgen.rb
+++ b/tool/mjit/bindgen.rb
@@ -111,16 +111,16 @@ class BindingGenerator https://github.com/ruby/ruby/blob/trunk/tool/mjit/bindgen.rb#L111
 
   attr_reader :src
 
-  # @param src_path [String] Source path used for preamble/postamble
-  # @param macros [Array<String>] Imported macros
-  # @param enums [Hash{ Symbol => Array<String> }] Imported enum values
+  # @param src_path [String]
+  # @param uses [Array<String>]
+  # @param ints [Array<String>]
   # @param types [Array<String>] Imported types
   # @param ruby_fields [Hash{ Symbol => Array<String> }] Struct VALUE fields that are considered Ruby objects
-  def initialize(src_path:, macros:, enums:, types:, ruby_fields:)
+  def initialize(src_path:, uses:, ints:, types:, ruby_fields:)
     @preamble, @postamble = split_ambles(src_path)
     @src = String.new
-    @macros = macros.sort
-    @enums = enums.transform_keys(&:to_s).transform_values(&:sort).sort.to_h
+    @uses = uses.sort
+    @ints = ints.sort
     @types = types.sort
     @ruby_fields = ruby_fields.transform_keys(&:to_s)
     @references = Set.new
@@ -129,10 +129,18 @@ class BindingGenerator https://github.com/ruby/ruby/blob/trunk/tool/mjit/bindgen.rb#L129
   def generate(_nodes)
     println @preamble
 
-    # Define macros
-    @macros.each do |macro|
-      println "  def C.#{macro}"
-      println "    #{generate_macro(macro)}"
+    # Define USE_* macros
+    @uses.each do |use|
+      println "  def C.#{use}"
+      println "    Primitive.cexpr! %q{ RBOOL(#{use} != 0) }"
+      println "  end"
+      println
+    end
+
+    # Define int macros/enums
+    @ints.each do |int|
+      println "  def C.#{int}"
+      println "    Primitive.cexpr! %q{ INT2NUM(#{int}) }"
       println "  end"
       println
     end
@@ -149,17 +157,6 @@ class BindingGenerator https://github.com/ruby/ruby/blob/trunk/tool/mjit/bindgen.rb#L157
     println
     println "module RubyVM::MJIT"
 
-    # Define enum values
-    @enums.each do |enum, values|
-      values.each do |value|
-        unless definition = generate_enum(nodes_index[enum], value)
-          raise "Failed to generate enum value: #{value}"
-        end
-        println "  def C.#{value} = #{definition}"
-        println
-      end
-    end
-
     # Define types
     @types.each do |type|
       unless definition = generate_node(nodes_index[type])
@@ -197,23 +194,6 @@ class BindingGenerator https://github.com/ruby/ruby/blob/trunk/tool/mjit/bindgen.rb#L194
     return lines[0..preamble_end].join, lines[postamble_beg..-1].join
   end
 
-  def generate_macro(macro)
-    if macro.start_with?('USE_')
-      "Primitive.cexpr! %q{ RBOOL(#{macro} != 0) }"
-    else
-      "Primitive.cexpr! %q{ INT2NUM(#{macro}) }"
-    end
-  end
-
-  def generate_enum(node, value)
-    case node
-    in Node[kind: :enum_decl, children:]
-      children.find { |c| c.spelling == value }&.enum_value
-    in Node[kind: :typedef_decl, children: [child]]
-      generate_enum(child, value)
-    end
-  end
-
   # Generate code from a node. Used for constructing a complex nested node.
   # @param node [Node]
   def generate_node(node)
@@ -326,23 +306,19 @@ cflags = [ https://github.com/ruby/ruby/blob/trunk/tool/mjit/bindgen.rb#L306
 nodes = HeaderParser.new(File.join(src_dir, 'mjit_compiler.h'), cflags: cflags).parse
 generator = BindingGenerator.new(
   src_path: src_path,
-  macros: %w[
-    NOT_COMPILED_STACK_SIZE
+  uses: %w[
     USE_LAZY_LOAD
     USE_RVARGC
+  ],
+  ints: %w[
+    NOT_COMPILED_STACK_SIZE
     VM_CALL_KW_SPLAT
+    VM_CALL_KW_SPLAT_bit
     VM_CALL_TAILCALL
+    VM_CALL_TAILCALL_bit
+    VM_METHOD_TYPE_CFUNC
+    VM_METHOD_TYPE_ISEQ
   ],
-  enums: {
-    rb_method_type_t: %w[
-      VM_METHOD_TYPE_CFUNC
-      VM_METHOD_TYPE_ISEQ
-    ],
-    vm_call_flag_bits: %w[
-      VM_CALL_KW_SPLAT_bit
-      VM_CALL_TAILCALL_bit
-    ],
-  },
   types: %w[
     CALL_DATA
     IC
-- 
cgit v1.2.1


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

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