1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Bindgen enum with builtin

This commit is contained in:
Takashi Kokubun 2022-09-18 23:43:24 +09:00
parent 2f5b37533e
commit 5cda5938f8
Notes: git 2022-09-23 06:45:07 +09:00
5 changed files with 50 additions and 73 deletions

View file

@ -9938,12 +9938,13 @@ mjit_compiler.$(OBJEXT): {$(VPATH)}iseq.h
mjit_compiler.$(OBJEXT): {$(VPATH)}method.h mjit_compiler.$(OBJEXT): {$(VPATH)}method.h
mjit_compiler.$(OBJEXT): {$(VPATH)}missing.h mjit_compiler.$(OBJEXT): {$(VPATH)}missing.h
mjit_compiler.$(OBJEXT): {$(VPATH)}mjit.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_compile_attr.inc
mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.c mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.c
mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.h mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.h
mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.rb mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.rb
mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_compiler.rbinc 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)}mjit_unit.h
mjit_compiler.$(OBJEXT): {$(VPATH)}node.h mjit_compiler.$(OBJEXT): {$(VPATH)}node.h
mjit_compiler.$(OBJEXT): {$(VPATH)}ruby_assert.h mjit_compiler.$(OBJEXT): {$(VPATH)}ruby_assert.h

View file

@ -1,14 +1,6 @@
require_relative 'c_type' require_relative 'c_type'
module RubyVM::MJIT 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 def C.CALL_DATA
@CALL_DATA ||= self.rb_call_data @CALL_DATA ||= self.rb_call_data
end end

View file

@ -1,14 +1,6 @@
require_relative 'c_type' require_relative 'c_type'
module RubyVM::MJIT 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 def C.CALL_DATA
@CALL_DATA ||= self.rb_call_data @CALL_DATA ||= self.rb_call_data
end end

View file

@ -123,10 +123,6 @@ module RubyVM::MJIT
### MJIT bindgen begin ### ### MJIT bindgen begin ###
def C.NOT_COMPILED_STACK_SIZE
Primitive.cexpr! %q{ INT2NUM(NOT_COMPILED_STACK_SIZE) }
end
def C.USE_LAZY_LOAD def C.USE_LAZY_LOAD
Primitive.cexpr! %q{ RBOOL(USE_LAZY_LOAD != 0) } Primitive.cexpr! %q{ RBOOL(USE_LAZY_LOAD != 0) }
end end
@ -135,13 +131,33 @@ module RubyVM::MJIT
Primitive.cexpr! %q{ RBOOL(USE_RVARGC != 0) } Primitive.cexpr! %q{ RBOOL(USE_RVARGC != 0) }
end end
def C.NOT_COMPILED_STACK_SIZE
Primitive.cexpr! %q{ INT2NUM(NOT_COMPILED_STACK_SIZE) }
end
def C.VM_CALL_KW_SPLAT def C.VM_CALL_KW_SPLAT
Primitive.cexpr! %q{ INT2NUM(VM_CALL_KW_SPLAT) } Primitive.cexpr! %q{ INT2NUM(VM_CALL_KW_SPLAT) }
end end
def C.VM_CALL_KW_SPLAT_bit
Primitive.cexpr! %q{ INT2NUM(VM_CALL_KW_SPLAT_bit) }
end
def C.VM_CALL_TAILCALL def C.VM_CALL_TAILCALL
Primitive.cexpr! %q{ INT2NUM(VM_CALL_TAILCALL) } Primitive.cexpr! %q{ INT2NUM(VM_CALL_TAILCALL) }
end 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 ### ### MJIT bindgen end ###
end if RubyVM::MJIT.enabled? end if RubyVM::MJIT.enabled?

View file

@ -111,16 +111,16 @@ class BindingGenerator
attr_reader :src attr_reader :src
# @param src_path [String] Source path used for preamble/postamble # @param src_path [String]
# @param macros [Array<String>] Imported macros # @param uses [Array<String>]
# @param enums [Hash{ Symbol => Array<String> }] Imported enum values # @param ints [Array<String>]
# @param types [Array<String>] Imported types # @param types [Array<String>] Imported types
# @param ruby_fields [Hash{ Symbol => Array<String> }] Struct VALUE fields that are considered Ruby objects # @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) @preamble, @postamble = split_ambles(src_path)
@src = String.new @src = String.new
@macros = macros.sort @uses = uses.sort
@enums = enums.transform_keys(&:to_s).transform_values(&:sort).sort.to_h @ints = ints.sort
@types = types.sort @types = types.sort
@ruby_fields = ruby_fields.transform_keys(&:to_s) @ruby_fields = ruby_fields.transform_keys(&:to_s)
@references = Set.new @references = Set.new
@ -129,10 +129,18 @@ class BindingGenerator
def generate(_nodes) def generate(_nodes)
println @preamble println @preamble
# Define macros # Define USE_* macros
@macros.each do |macro| @uses.each do |use|
println " def C.#{macro}" println " def C.#{use}"
println " #{generate_macro(macro)}" 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"
println println
end end
@ -149,17 +157,6 @@ class BindingGenerator
println println
println "module RubyVM::MJIT" 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 # Define types
@types.each do |type| @types.each do |type|
unless definition = generate_node(nodes_index[type]) unless definition = generate_node(nodes_index[type])
@ -197,23 +194,6 @@ class BindingGenerator
return lines[0..preamble_end].join, lines[postamble_beg..-1].join return lines[0..preamble_end].join, lines[postamble_beg..-1].join
end 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. # Generate code from a node. Used for constructing a complex nested node.
# @param node [Node] # @param node [Node]
def generate_node(node) def generate_node(node)
@ -326,23 +306,19 @@ cflags = [
nodes = HeaderParser.new(File.join(src_dir, 'mjit_compiler.h'), cflags: cflags).parse nodes = HeaderParser.new(File.join(src_dir, 'mjit_compiler.h'), cflags: cflags).parse
generator = BindingGenerator.new( generator = BindingGenerator.new(
src_path: src_path, src_path: src_path,
macros: %w[ uses: %w[
NOT_COMPILED_STACK_SIZE
USE_LAZY_LOAD USE_LAZY_LOAD
USE_RVARGC USE_RVARGC
VM_CALL_KW_SPLAT
VM_CALL_TAILCALL
], ],
enums: { ints: %w[
rb_method_type_t: %w[ NOT_COMPILED_STACK_SIZE
VM_METHOD_TYPE_CFUNC VM_CALL_KW_SPLAT
VM_METHOD_TYPE_ISEQ VM_CALL_KW_SPLAT_bit
], VM_CALL_TAILCALL
vm_call_flag_bits: %w[ VM_CALL_TAILCALL_bit
VM_CALL_KW_SPLAT_bit VM_METHOD_TYPE_CFUNC
VM_CALL_TAILCALL_bit VM_METHOD_TYPE_ISEQ
], ],
},
types: %w[ types: %w[
CALL_DATA CALL_DATA
IC IC