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

ruby-changes:73980

From: Aaron <ko1@a...>
Date: Fri, 14 Oct 2022 05:11:49 +0900 (JST)
Subject: [ruby-changes:73980] e5058b58c2 (master): Only expose Ruby Shape API if VM_CHECK_MODE is enabled

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

From e5058b58c22e19d559b1122d94af5af3931aa416 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <tenderlove@r...>
Date: Wed, 12 Oct 2022 14:37:02 -0700
Subject: Only expose Ruby Shape API if VM_CHECK_MODE is enabled

---
 lib/mjit/compiler.rb     |  2 +-
 mjit_c.rb                | 25 +++++++++++++++++++++++--
 shape.c                  | 22 +++++++++++++---------
 test/ruby/test_shapes.rb |  2 +-
 tool/mjit/bindgen.rb     |  2 ++
 5 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/lib/mjit/compiler.rb b/lib/mjit/compiler.rb
index 0d2910bf4e..55fcee6b87 100644
--- a/lib/mjit/compiler.rb
+++ b/lib/mjit/compiler.rb
@@ -356,7 +356,7 @@ module RubyVM::MJIT https://github.com/ruby/ruby/blob/trunk/lib/mjit/compiler.rb#L356
       source_shape_id = if dest_shape_id == C.INVALID_SHAPE_ID
                           dest_shape_id
                         else
-                          RubyVM::Shape.find_by_id(dest_shape_id).parent_id
+                          C.rb_shape_get_shape_by_id(dest_shape_id).parent_id
                         end
 
       src = +''
diff --git a/mjit_c.rb b/mjit_c.rb
index 1858f86e4d..4a68ec12ae 100644
--- a/mjit_c.rb
+++ b/mjit_c.rb
@@ -6,11 +6,11 @@ module RubyVM::MJIT https://github.com/ruby/ruby/blob/trunk/mjit_c.rb#L6
 
   class << C
     def SHAPE_BITS
-      RubyVM::Shape::SHAPE_BITS
+      Primitive.cexpr! 'UINT2NUM(SHAPE_BITS)'
     end
 
     def SHAPE_FLAG_SHIFT
-      RubyVM::Shape::SHAPE_FLAG_SHIFT
+      Primitive.cexpr! 'UINT2NUM(SHAPE_FLAG_SHIFT)'
     end
 
     def ROBJECT_EMBED_LEN_MAX
@@ -29,6 +29,12 @@ module RubyVM::MJIT https://github.com/ruby/ruby/blob/trunk/mjit_c.rb#L29
       Primitive.has_cache_for_send(cc.to_i, insn)
     end
 
+    def rb_shape_get_shape_by_id(shape_id)
+      _shape_id = shape_id.to_i
+      shape_addr = Primitive.cexpr! 'PTR2NUM((VALUE)rb_shape_get_shape_by_id((shape_id_t)NUM2UINT(_shape_id)))'
+      rb_shape_t.new(shape_addr)
+    end
+
     def rb_iseq_check(iseq)
       _iseq_addr = iseq.to_i
       iseq_addr = Primitive.cexpr! 'PTR2NUM((VALUE)rb_iseq_check((rb_iseq_t *)NUM2PTR(_iseq_addr)))'
@@ -595,6 +601,21 @@ module RubyVM::MJIT https://github.com/ruby/ruby/blob/trunk/mjit_c.rb#L601
     @rb_serial_t ||= CType::Immediate.parse("unsigned long long")
   end
 
+  def C.rb_shape
+    @rb_shape ||= CType::Struct.new(
+      "rb_shape", Primitive.cexpr!("SIZEOF(struct rb_shape)"),
+      edges: [CType::Pointer.new { self.rb_id_table }, Primitive.cexpr!("OFFSETOF((*((struct rb_shape *)NULL)), edges)")],
+      edge_name: [self.ID, Primitive.cexpr!("OFFSETOF((*((struct rb_shape *)NULL)), edge_name)")],
+      iv_count: [self.attr_index_t, Primitive.cexpr!("OFFSETOF((*((struct rb_shape *)NULL)), iv_count)")],
+      type: [CType::Immediate.parse("uint8_t"), Primitive.cexpr!("OFFSETOF((*((struct rb_shape *)NULL)), type)")],
+      parent_id: [self.shape_id_t, Primitive.cexpr!("OFFSETOF((*((struct rb_shape *)NULL)), parent_id)")],
+    )
+  end
+
+  def C.rb_shape_t
+    @rb_shape_t ||= self.rb_shape
+  end
+
   def C.VALUE
     @VALUE ||= CType::Immediate.find(Primitive.cexpr!("SIZEOF(VALUE)"), Primitive.cexpr!("SIGNED_TYPE_P(VALUE)"))
   end
diff --git a/shape.c b/shape.c
index f21b9a4ece..1b0f1a5dc9 100644
--- a/shape.c
+++ b/shape.c
@@ -306,6 +306,13 @@ rb_shape_set_shape(VALUE obj, rb_shape_t* shape) https://github.com/ruby/ruby/blob/trunk/shape.c#L306
     rb_shape_set_shape_id(obj, rb_shape_id(shape));
 }
 
+VALUE
+rb_shape_flags_mask(void)
+{
+    return SHAPE_FLAG_MASK;
+}
+
+#if VM_CHECK_MODE > 0
 VALUE rb_cShape;
 
 /*
@@ -440,19 +447,19 @@ rb_shape_parent(VALUE self) https://github.com/ruby/ruby/blob/trunk/shape.c#L447
     }
 }
 
-VALUE
+static VALUE
 rb_shape_debug_shape(VALUE self, VALUE obj)
 {
     return rb_shape_t_to_rb_cShape(rb_shape_get_shape(obj));
 }
 
-VALUE
+static VALUE
 rb_shape_root_shape(VALUE self)
 {
     return rb_shape_t_to_rb_cShape(rb_shape_get_root_shape());
 }
 
-VALUE
+static VALUE
 rb_shape_frozen_root_shape(VALUE self)
 {
     return rb_shape_t_to_rb_cShape(rb_shape_get_frozen_root_shape());
@@ -505,12 +512,6 @@ next_shape_id(VALUE self) https://github.com/ruby/ruby/blob/trunk/shape.c#L512
     return INT2NUM(GET_VM()->next_shape_id);
 }
 
-VALUE
-rb_shape_flags_mask(void)
-{
-    return SHAPE_FLAG_MASK;
-}
-
 static VALUE
 rb_shape_find_by_id(VALUE mod, VALUE id)
 {
@@ -520,10 +521,12 @@ rb_shape_find_by_id(VALUE mod, VALUE id) https://github.com/ruby/ruby/blob/trunk/shape.c#L521
     }
     return rb_shape_t_to_rb_cShape(rb_shape_get_shape_by_id(shape_id));
 }
+#endif
 
 void
 Init_shape(void)
 {
+#if VM_CHECK_MODE > 0
     rb_cShape = rb_define_class_under(rb_cRubyVM, "Shape", rb_cObject);
     rb_undef_alloc_func(rb_cShape);
 
@@ -548,4 +551,5 @@ Init_shape(void) https://github.com/ruby/ruby/blob/trunk/shape.c#L551
     rb_define_singleton_method(rb_cShape, "of", rb_shape_debug_shape, 1);
     rb_define_singleton_method(rb_cShape, "root_shape", rb_shape_root_shape, 0);
     rb_define_singleton_method(rb_cShape, "frozen_root_shape", rb_shape_frozen_root_shape, 0);
+#endif
 }
diff --git a/test/ruby/test_shapes.rb b/test/ruby/test_shapes.rb
index 7142c30cd5..0da296189d 100644
--- a/test/ruby/test_shapes.rb
+++ b/test/ruby/test_shapes.rb
@@ -179,4 +179,4 @@ class TestShapes < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_shapes.rb#L179
       RubyVM::Shape.find_by_id(-1)
     end
   end
-end
+end if defined?(RubyVM::Shape)
diff --git a/tool/mjit/bindgen.rb b/tool/mjit/bindgen.rb
index 8c21d42449..77b81814e3 100755
--- a/tool/mjit/bindgen.rb
+++ b/tool/mjit/bindgen.rb
@@ -380,6 +380,8 @@ generator = BindingGenerator.new( https://github.com/ruby/ruby/blob/trunk/tool/mjit/bindgen.rb#L380
     rb_mjit_compile_info
     rb_mjit_unit
     rb_serial_t
+    rb_shape
+    rb_shape_t
   ],
   dynamic_types: %w[
     VALUE
-- 
cgit v1.2.1


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

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