Skip to content

Commit

Permalink
Only expose Ruby Shape API if VM_CHECK_MODE is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 13, 2022
1 parent 1b0c9d0 commit e5058b5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/mjit/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def compile_ivar(insn_name, stack_size, pos, status, operands, body)
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 = +''
Expand Down
25 changes: 23 additions & 2 deletions mjit_c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module RubyVM::MJIT

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
Expand All @@ -29,6 +29,12 @@ def has_cache_for_send(cc, insn)
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)))'
Expand Down Expand Up @@ -595,6 +601,21 @@ def C.rb_serial_t
@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
Expand Down
22 changes: 13 additions & 9 deletions shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ rb_shape_set_shape(VALUE obj, rb_shape_t* shape)
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;

/*
Expand Down Expand Up @@ -440,19 +447,19 @@ rb_shape_parent(VALUE self)
}
}

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());
Expand Down Expand Up @@ -505,12 +512,6 @@ next_shape_id(VALUE self)
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)
{
Expand All @@ -520,10 +521,12 @@ rb_shape_find_by_id(VALUE mod, VALUE id)
}
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);

Expand All @@ -548,4 +551,5 @@ Init_shape(void)
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
}
2 changes: 1 addition & 1 deletion test/ruby/test_shapes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ def test_out_of_bounds_shape
RubyVM::Shape.find_by_id(-1)
end
end
end
end if defined?(RubyVM::Shape)
2 changes: 2 additions & 0 deletions tool/mjit/bindgen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ def push_target(target)
rb_mjit_compile_info
rb_mjit_unit
rb_serial_t
rb_shape
rb_shape_t
],
dynamic_types: %w[
VALUE
Expand Down

0 comments on commit e5058b5

Please sign in to comment.