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:
		
							parent
							
								
									2f5b37533e
								
							
						
					
					
						commit
						5cda5938f8
					
				
				
				Notes:
				
					git
				
				2022-09-23 06:45:07 +09:00 
				
			
			
			
		
		
					 5 changed files with 50 additions and 73 deletions
				
			
		| 
						 | 
				
			
			@ -9938,12 +9938,13 @@ mjit_compiler.$(OBJEXT): {$(VPATH)}iseq.h
 | 
			
		|||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,6 @@
 | 
			
		|||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,6 @@
 | 
			
		|||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										24
									
								
								mjit_c.rb
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								mjit_c.rb
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -123,10 +123,6 @@ module RubyVM::MJIT
 | 
			
		|||
 | 
			
		||||
  ### 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
 | 
			
		|||
    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?
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -111,16 +111,16 @@ class BindingGenerator
 | 
			
		|||
 | 
			
		||||
  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
 | 
			
		|||
  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
 | 
			
		|||
    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
 | 
			
		|||
    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 = [
 | 
			
		|||
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
 | 
			
		||||
    VM_CALL_KW_SPLAT
 | 
			
		||||
    VM_CALL_TAILCALL
 | 
			
		||||
  ],
 | 
			
		||||
  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
 | 
			
		||||
    ],
 | 
			
		||||
  },
 | 
			
		||||
  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
 | 
			
		||||
  ],
 | 
			
		||||
  types: %w[
 | 
			
		||||
    CALL_DATA
 | 
			
		||||
    IC
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue