From 3767c6a90d8970f9b39e9ed116a7b9bbac3f9f26 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 4 Sep 2022 21:53:46 -0700 Subject: [PATCH] Ruby MJIT (#6028) --- .gitignore | 1 + common.mk | 20 +- inits.c | 2 + lib/mjit/c_32.rb | 436 +++++++ lib/mjit/c_64.rb | 437 +++++++ lib/mjit/c_pointer.rb | 333 ++++++ lib/mjit/c_type.rb | 77 ++ lib/mjit/compiler.rb | 1019 +++++++++++++++++ mjit.c | 12 +- mjit_compiler.c | 588 ++-------- mjit_compiler.h | 58 + mjit_compiler.rb | 135 +++ ruby.c | 18 +- template/Makefile.in | 13 +- tool/mjit/.gitignore | 1 + tool/mjit/Gemfile | 4 + tool/mjit/bindgen.rb | 391 +++++++ tool/ruby_vm/helpers/dumper.rb | 2 +- .../views/_mjit_compile_getconstant_path.erb | 30 - tool/ruby_vm/views/_mjit_compile_insn.erb | 92 -- .../ruby_vm/views/_mjit_compile_insn_body.erb | 129 --- .../views/_mjit_compile_invokebuiltin.erb | 29 - tool/ruby_vm/views/_mjit_compile_ivar.erb | 110 -- .../ruby_vm/views/_mjit_compile_pc_and_sp.erb | 38 - tool/ruby_vm/views/_mjit_compile_send.erb | 119 -- tool/ruby_vm/views/mjit_compile.inc.erb | 110 -- tool/ruby_vm/views/mjit_compile_attr.inc.erb | 17 + tool/ruby_vm/views/mjit_instruction.rb.erb | 40 + tool/update-deps | 2 +- win32/Makefile.sub | 6 +- 30 files changed, 3078 insertions(+), 1191 deletions(-) create mode 100644 lib/mjit/c_32.rb create mode 100644 lib/mjit/c_64.rb create mode 100644 lib/mjit/c_pointer.rb create mode 100644 lib/mjit/c_type.rb create mode 100644 lib/mjit/compiler.rb create mode 100644 mjit_compiler.h create mode 100644 mjit_compiler.rb create mode 100644 tool/mjit/.gitignore create mode 100644 tool/mjit/Gemfile create mode 100755 tool/mjit/bindgen.rb delete mode 100644 tool/ruby_vm/views/_mjit_compile_getconstant_path.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_insn.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_insn_body.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_invokebuiltin.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_ivar.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb delete mode 100644 tool/ruby_vm/views/_mjit_compile_send.erb delete mode 100644 tool/ruby_vm/views/mjit_compile.inc.erb create mode 100644 tool/ruby_vm/views/mjit_compile_attr.inc.erb create mode 100644 tool/ruby_vm/views/mjit_instruction.rb.erb diff --git a/.gitignore b/.gitignore index c12ec27782..52089ac21c 100644 --- a/.gitignore +++ b/.gitignore @@ -236,6 +236,7 @@ lcov*.info /rb_mjit_header.h /mjit_config.h /include/ruby-*/*/rb_mjit_min_header-*.h +/mjit_instruction.rb # /wasm/ /wasm/tests/*.wasm diff --git a/common.mk b/common.mk index d5e69750d4..ec0ccc5402 100644 --- a/common.mk +++ b/common.mk @@ -1049,11 +1049,7 @@ $(srcs_vpath)insns_info.inc: $(tooldir)/ruby_vm/views/insns_info.inc.erb $(inc_c $(srcs_vpath)vmtc.inc: $(tooldir)/ruby_vm/views/vmtc.inc.erb $(inc_common_headers) $(srcs_vpath)vm.inc: $(tooldir)/ruby_vm/views/vm.inc.erb $(inc_common_headers) \ $(tooldir)/ruby_vm/views/_insn_entry.erb $(tooldir)/ruby_vm/views/_trace_instruction.erb -$(srcs_vpath)mjit_compile.inc: $(tooldir)/ruby_vm/views/mjit_compile.inc.erb $(inc_common_headers) \ - $(tooldir)/ruby_vm/views/_mjit_compile_insn.erb $(tooldir)/ruby_vm/views/_mjit_compile_send.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_ivar.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_insn_body.erb $(tooldir)/ruby_vm/views/_mjit_compile_pc_and_sp.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_invokebuiltin.erb $(tooldir)/ruby_vm/views/_mjit_compile_getconstant_path.erb +$(srcs_vpath)mjit_compile_attr.inc: $(tooldir)/ruby_vm/views/mjit_compile_attr.inc.erb BUILTIN_RB_SRCS = \ $(srcdir)/ast.rb \ @@ -1063,6 +1059,8 @@ BUILTIN_RB_SRCS = \ $(srcdir)/io.rb \ $(srcdir)/marshal.rb \ $(srcdir)/mjit.rb \ + $(srcdir)/mjit_compiler.rb \ + $(srcdir)/mjit_instruction.rb \ $(srcdir)/pack.rb \ $(srcdir)/trace_point.rb \ $(srcdir)/warning.rb \ @@ -1154,7 +1152,7 @@ vm_call_iseq_optimized.inc: $(srcdir)/template/call_iseq_optimized.inc.tmpl $(ECHO) generating $@ $(Q) $(BASERUBY) $(tooldir)/generic_erb.rb -c -o $@ $(srcdir)/template/call_iseq_optimized.inc.tmpl -$(MINIPRELUDE_C): $(COMPILE_PRELUDE) $(BUILTIN_RB_SRCS) +$(MINIPRELUDE_C): $(COMPILE_PRELUDE) $(BUILTIN_RB_SRCS) $(srcdir)/mjit_instruction.rb $(ECHO) generating $@ $(Q) $(BASERUBY) $(tooldir)/generic_erb.rb -I$(srcdir) -o $@ \ $(srcdir)/template/prelude.c.tmpl $(BUILTIN_RB_SRCS) @@ -9280,6 +9278,7 @@ miniinit.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h miniinit.$(OBJEXT): $(CCAN_DIR)/list/list.h miniinit.$(OBJEXT): $(CCAN_DIR)/str/str.h miniinit.$(OBJEXT): $(hdrdir)/ruby/ruby.h +miniinit.$(OBJEXT): $(srcdir)/mjit_instruction.rb miniinit.$(OBJEXT): $(top_srcdir)/internal/array.h miniinit.$(OBJEXT): $(top_srcdir)/internal/compilers.h miniinit.$(OBJEXT): $(top_srcdir)/internal/gc.h @@ -9470,6 +9469,8 @@ miniinit.$(OBJEXT): {$(VPATH)}miniinit.c miniinit.$(OBJEXT): {$(VPATH)}miniprelude.c miniinit.$(OBJEXT): {$(VPATH)}missing.h miniinit.$(OBJEXT): {$(VPATH)}mjit.rb +miniinit.$(OBJEXT): {$(VPATH)}mjit_compiler.rb +miniinit.$(OBJEXT): {$(VPATH)}mjit_instruction.rb miniinit.$(OBJEXT): {$(VPATH)}nilclass.rb miniinit.$(OBJEXT): {$(VPATH)}node.h miniinit.$(OBJEXT): {$(VPATH)}numeric.rb @@ -9723,6 +9724,7 @@ mjit_compiler.$(OBJEXT): $(CCAN_DIR)/list/list.h mjit_compiler.$(OBJEXT): $(CCAN_DIR)/str/str.h mjit_compiler.$(OBJEXT): $(hdrdir)/ruby.h mjit_compiler.$(OBJEXT): $(hdrdir)/ruby/ruby.h +mjit_compiler.$(OBJEXT): $(srcdir)/mjit_instruction.rb mjit_compiler.$(OBJEXT): $(top_srcdir)/internal/array.h mjit_compiler.$(OBJEXT): $(top_srcdir)/internal/class.h mjit_compiler.$(OBJEXT): $(top_srcdir)/internal/compile.h @@ -9903,8 +9905,12 @@ 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_compile.inc +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_instruction.rbinc mjit_compiler.$(OBJEXT): {$(VPATH)}mjit_unit.h mjit_compiler.$(OBJEXT): {$(VPATH)}node.h mjit_compiler.$(OBJEXT): {$(VPATH)}ruby_assert.h diff --git a/inits.c b/inits.c index 22ba6d5a8c..f43ece0e47 100644 --- a/inits.c +++ b/inits.c @@ -104,6 +104,8 @@ rb_call_builtin_inits(void) BUILTIN(marshal); #if USE_MJIT BUILTIN(mjit); + BUILTIN(mjit_compiler); + BUILTIN(mjit_instruction); #endif Init_builtin_prelude(); } diff --git a/lib/mjit/c_32.rb b/lib/mjit/c_32.rb new file mode 100644 index 0000000000..ee8324ba4d --- /dev/null +++ b/lib/mjit/c_32.rb @@ -0,0 +1,436 @@ +require_relative 'c_type' + +module RubyVM::MJIT + C = Object.new + + def C.NOT_COMPILED_STACK_SIZE = - 1 + + def C.USE_LAZY_LOAD = false + + def C.USE_RVARGC = true + + def C.VM_CALL_KW_SPLAT = ( 0x01 << self.VM_CALL_KW_SPLAT_bit ) + + def C.VM_CALL_TAILCALL = ( 0x01 << self.VM_CALL_TAILCALL_bit ) + + 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 + + def C.IC + @IC ||= self.iseq_inline_constant_cache + end + + def C.IVC + @IVC ||= self.iseq_inline_iv_cache_entry + end + + def C.RB_BUILTIN + @RB_BUILTIN ||= self.rb_builtin_function + end + + def C.VALUE + @VALUE ||= CType::Immediate.new(-5) + end + + def C.compile_branch + @compile_branch ||= CType::Struct.new( + "compile_branch", 8, + stack_size: [0, CType::Immediate.new(-4)], + finish_p: [32, self._Bool], + ) + end + + def C.compile_status + @compile_status ||= CType::Struct.new( + "compile_status", 68, + success: [0, self._Bool], + stack_size_for_pos: [32, CType::Pointer.new { CType::Immediate.new(4) }], + local_stack_p: [64, self._Bool], + is_entries: [96, CType::Pointer.new { self.iseq_inline_storage_entry }], + cc_entries_index: [128, CType::Immediate.new(4)], + compiled_iseq: [160, CType::Pointer.new { self.rb_iseq_constant_body }], + compiled_id: [192, CType::Immediate.new(4)], + compile_info: [224, CType::Pointer.new { self.rb_mjit_compile_info }], + merge_ivar_guards_p: [256, self._Bool], + ivar_serial: [288, self.rb_serial_t], + max_ivar_index: [352, CType::Immediate.new(-4)], + inlined_iseqs: [384, CType::Pointer.new { CType::Pointer.new { self.rb_iseq_constant_body } }], + inline_context: [416, self.inlined_call_context], + ) + end + + def C.inlined_call_context + @inlined_call_context ||= CType::Struct.new( + "inlined_call_context", 16, + orig_argc: [0, CType::Immediate.new(4)], + me: [32, self.VALUE], + param_size: [64, CType::Immediate.new(4)], + local_size: [96, CType::Immediate.new(4)], + ) + end + + def C.iseq_inline_constant_cache + @iseq_inline_constant_cache ||= CType::Struct.new( + "iseq_inline_constant_cache", 8, + entry: [0, CType::Pointer.new { self.iseq_inline_constant_cache_entry }], + segments: [32, CType::Pointer.new { self.ID }], + ) + end + + def C.iseq_inline_constant_cache_entry + @iseq_inline_constant_cache_entry ||= CType::Struct.new( + "iseq_inline_constant_cache_entry", 20, + flags: [0, self.VALUE], + value: [32, self.VALUE], + _unused1: [64, self.VALUE], + _unused2: [96, self.VALUE], + ic_cref: [128, CType::Pointer.new { self.rb_cref_t }], + ) + end + + def C.iseq_inline_iv_cache_entry + @iseq_inline_iv_cache_entry ||= CType::Struct.new( + "iseq_inline_iv_cache_entry", 4, + entry: [0, CType::Pointer.new { self.rb_iv_index_tbl_entry }], + ) + end + + def C.iseq_inline_storage_entry + @iseq_inline_storage_entry ||= CType::Union.new( + "iseq_inline_storage_entry", 8, + once: CType::Struct.new( + "", 8, + running_thread: [0, CType::Pointer.new { self.rb_thread_struct }], + value: [32, self.VALUE], + ), + ic_cache: self.iseq_inline_constant_cache, + iv_cache: self.iseq_inline_iv_cache_entry, + ) + end + + def C.mjit_options + @mjit_options ||= CType::Struct.new( + "mjit_options", 24, + on: [0, self._Bool], + save_temps: [8, self._Bool], + warnings: [16, self._Bool], + debug: [24, self._Bool], + debug_flags: [32, CType::Pointer.new { CType::Immediate.new(2) }], + wait: [64, self._Bool], + min_calls: [96, CType::Immediate.new(-4)], + verbose: [128, CType::Immediate.new(4)], + max_cache_size: [160, CType::Immediate.new(4)], + ) + end + + def C.rb_builtin_function + @rb_builtin_function ||= CType::Struct.new( + "rb_builtin_function", 20, + func_ptr: [0, CType::Pointer.new { CType::Immediate.new(0) }], + argc: [32, CType::Immediate.new(4)], + index: [64, CType::Immediate.new(4)], + name: [96, CType::Pointer.new { CType::Immediate.new(2) }], + compiler: [128, CType::Immediate.new(1)], + ) + end + + def C.rb_call_data + @rb_call_data ||= CType::Struct.new( + "rb_call_data", 8, + ci: [0, CType::Pointer.new { self.rb_callinfo }], + cc: [32, CType::Pointer.new { self.rb_callcache }], + ) + end + + def C.rb_callable_method_entry_struct + @rb_callable_method_entry_struct ||= CType::Struct.new( + "rb_callable_method_entry_struct", 20, + flags: [0, self.VALUE], + defined_class: [32, self.VALUE], + def: [64, CType::Pointer.new { self.rb_method_definition_struct }], + called_id: [96, self.ID], + owner: [128, self.VALUE], + ) + end + + def C.rb_callcache + @rb_callcache ||= CType::Struct.new( + "rb_callcache", 20, + flags: [0, self.VALUE], + klass: [32, self.VALUE], + cme_: [64, CType::Pointer.new { self.rb_callable_method_entry_struct }], + call_: [96, self.vm_call_handler], + aux_: [128, CType::Union.new( + "", 4, + attr_index: CType::Immediate.new(-4), + method_missing_reason: self.method_missing_reason, + v: self.VALUE, + )], + ) + end + + def C.rb_callinfo + @rb_callinfo ||= CType::Struct.new( + "rb_callinfo", 20, + flags: [0, self.VALUE], + kwarg: [32, CType::Pointer.new { self.rb_callinfo_kwarg }], + mid: [64, self.VALUE], + flag: [96, self.VALUE], + argc: [128, self.VALUE], + ) + end + + def C.rb_cref_t + @rb_cref_t ||= CType::Struct.new( + "rb_cref_struct", 20, + flags: [0, self.VALUE], + refinements: [32, self.VALUE], + klass_or_self: [64, self.VALUE], + next: [96, CType::Pointer.new { self.rb_cref_struct }], + scope_visi: [128, self.rb_scope_visibility_t], + ) + end + + def C.rb_iseq_constant_body + @rb_iseq_constant_body ||= CType::Struct.new( + "rb_iseq_constant_body", 204, + type: [0, self.rb_iseq_type], + iseq_size: [32, CType::Immediate.new(-4)], + iseq_encoded: [64, CType::Pointer.new { self.VALUE }], + param: [96, CType::Struct.new( + "", 40, + flags: [0, CType::Struct.new( + "", 4, + has_lead: [0, CType::BitField.new(1, 0)], + has_opt: [1, CType::BitField.new(1, 1)], + has_rest: [2, CType::BitField.new(1, 2)], + has_post: [3, CType::BitField.new(1, 3)], + has_kw: [4, CType::BitField.new(1, 4)], + has_kwrest: [5, CType::BitField.new(1, 5)], + has_block: [6, CType::BitField.new(1, 6)], + ambiguous_param0: [7, CType::BitField.new(1, 7)], + accepts_no_kwarg: [8, CType::BitField.new(1, 0)], + ruby2_keywords: [9, CType::BitField.new(1, 1)], + )], + size: [32, CType::Immediate.new(-4)], + lead_num: [64, CType::Immediate.new(4)], + opt_num: [96, CType::Immediate.new(4)], + rest_start: [128, CType::Immediate.new(4)], + post_start: [160, CType::Immediate.new(4)], + post_num: [192, CType::Immediate.new(4)], + block_start: [224, CType::Immediate.new(4)], + opt_table: [256, CType::Pointer.new { self.VALUE }], + keyword: [288, CType::Pointer.new { self.rb_iseq_param_keyword }], + )], + location: [416, self.rb_iseq_location_t], + insns_info: [704, self.iseq_insn_info], + local_table: [832, CType::Pointer.new { self.ID }], + catch_table: [864, CType::Pointer.new { self.iseq_catch_table }], + parent_iseq: [896, CType::Pointer.new { self.rb_iseq_struct }], + local_iseq: [928, CType::Pointer.new { self.rb_iseq_struct }], + is_entries: [960, CType::Pointer.new { self.iseq_inline_storage_entry }], + call_data: [992, CType::Pointer.new { self.rb_call_data }], + variable: [1024, CType::Struct.new( + "", 20, + flip_count: [0, self.rb_snum_t], + script_lines: [32, self.VALUE], + coverage: [64, self.VALUE], + pc2branchindex: [96, self.VALUE], + original_iseq: [128, CType::Pointer.new { self.VALUE }], + )], + local_table_size: [1184, CType::Immediate.new(-4)], + ic_size: [1216, CType::Immediate.new(-4)], + ise_size: [1248, CType::Immediate.new(-4)], + ivc_size: [1280, CType::Immediate.new(-4)], + icvarc_size: [1312, CType::Immediate.new(-4)], + ci_size: [1344, CType::Immediate.new(-4)], + stack_max: [1376, CType::Immediate.new(-4)], + mark_bits: [1408, CType::Union.new( + "", 4, + list: CType::Pointer.new { self.iseq_bits_t }, + single: self.iseq_bits_t, + )], + catch_except_p: [1440, self._Bool], + builtin_inline_p: [1448, self._Bool], + outer_variables: [1472, CType::Pointer.new { self.rb_id_table }], + mandatory_only_iseq: [1504, CType::Pointer.new { self.rb_iseq_t }], + jit_func: [1536, CType::Immediate.new(1)], + total_calls: [1568, CType::Immediate.new(-5)], + jit_unit: [1600, CType::Pointer.new { self.rb_mjit_unit }], + ) + end + + def C.rb_iseq_location_t + @rb_iseq_location_t ||= CType::Struct.new( + "rb_iseq_location_struct", 36, + pathobj: [0, self.VALUE, true], + base_label: [32, self.VALUE, true], + label: [64, self.VALUE, true], + first_lineno: [96, self.VALUE, true], + node_id: [128, CType::Immediate.new(4)], + code_location: [160, self.rb_code_location_t], + ) + end + + def C.rb_iseq_struct + @rb_iseq_struct ||= CType::Struct.new( + "rb_iseq_struct", 20, + flags: [0, self.VALUE], + wrapper: [32, self.VALUE], + body: [64, CType::Pointer.new { self.rb_iseq_constant_body }], + aux: [96, CType::Union.new( + "", 8, + compile_data: CType::Pointer.new { self.iseq_compile_data }, + loader: CType::Struct.new( + "", 8, + obj: [0, self.VALUE], + index: [32, CType::Immediate.new(4)], + ), + exec: CType::Struct.new( + "", 8, + local_hooks: [0, CType::Pointer.new { self.rb_hook_list_struct }], + global_trace_events: [32, self.rb_event_flag_t], + ), + )], + ) + end + + def C.rb_iseq_t + @rb_iseq_t ||= self.rb_iseq_struct + end + + def C.rb_iv_index_tbl_entry + @rb_iv_index_tbl_entry ||= CType::Struct.new( + "rb_iv_index_tbl_entry", 16, + index: [0, CType::Immediate.new(-4)], + class_serial: [32, self.rb_serial_t], + class_value: [96, self.VALUE], + ) + end + + def C.rb_method_definition_struct + @rb_method_definition_struct ||= CType::Struct.new( + "rb_method_definition_struct", 28, + type: [0, self.rb_method_type_t], + iseq_overload: [4, CType::BitField.new(1, 4)], + alias_count: [5, CType::BitField.new(27, 5)], + complemented_count: [32, CType::BitField.new(28, 0)], + no_redef_warning: [60, CType::BitField.new(1, 4)], + body: [64, CType::Union.new( + "", 12, + iseq: self.rb_method_iseq_t, + cfunc: self.rb_method_cfunc_t, + attr: self.rb_method_attr_t, + alias: self.rb_method_alias_t, + refined: self.rb_method_refined_t, + bmethod: self.rb_method_bmethod_t, + optimized: self.rb_method_optimized_t, + )], + original_id: [160, self.ID], + method_serial: [192, CType::Immediate.new(-4)], + ) + end + + def C.rb_method_iseq_t + @rb_method_iseq_t ||= CType::Struct.new( + "rb_method_iseq_struct", 8, + iseqptr: [0, CType::Pointer.new { self.rb_iseq_t }], + cref: [32, CType::Pointer.new { self.rb_cref_t }], + ) + end + + def C.rb_method_type_t + @rb_method_type_t ||= CType::Immediate.new(4) + end + + def C.rb_mjit_compile_info + @rb_mjit_compile_info ||= CType::Struct.new( + "rb_mjit_compile_info", 5, + disable_ivar_cache: [0, self._Bool], + disable_exivar_cache: [8, self._Bool], + disable_send_cache: [16, self._Bool], + disable_inlining: [24, self._Bool], + disable_const_cache: [32, self._Bool], + ) + end + + def C.rb_mjit_unit + @rb_mjit_unit ||= CType::Struct.new( + "rb_mjit_unit", 36, + unode: [0, self.ccan_list_node], + id: [64, CType::Immediate.new(4)], + handle: [96, CType::Pointer.new { CType::Immediate.new(0) }], + iseq: [128, CType::Pointer.new { self.rb_iseq_t }], + used_code_p: [160, self._Bool], + compact_p: [168, self._Bool], + compile_info: [176, self.rb_mjit_compile_info], + cc_entries: [224, CType::Pointer.new { CType::Pointer.new { self.rb_callcache } }], + cc_entries_size: [256, CType::Immediate.new(-4)], + ) + end + + def C.rb_serial_t + @rb_serial_t ||= CType::Immediate.new(-6) + end + + def C._Bool = CType::Bool.new + + def C.ID = CType::Stub.new(:ID) + + def C.rb_thread_struct = CType::Stub.new(:rb_thread_struct) + + def C.vm_call_handler = CType::Stub.new(:vm_call_handler) + + def C.method_missing_reason = CType::Stub.new(:method_missing_reason) + + def C.rb_callinfo_kwarg = CType::Stub.new(:rb_callinfo_kwarg) + + def C.rb_cref_struct = CType::Stub.new(:rb_cref_struct) + + def C.rb_scope_visibility_t = CType::Stub.new(:rb_scope_visibility_t) + + def C.rb_iseq_type = CType::Stub.new(:rb_iseq_type) + + def C.rb_iseq_param_keyword = CType::Stub.new(:rb_iseq_param_keyword) + + def C.iseq_insn_info = CType::Stub.new(:iseq_insn_info) + + def C.iseq_catch_table = CType::Stub.new(:iseq_catch_table) + + def C.rb_snum_t = CType::Stub.new(:rb_snum_t) + + def C.iseq_bits_t = CType::Stub.new(:iseq_bits_t) + + def C.rb_id_table = CType::Stub.new(:rb_id_table) + + def C.rb_code_location_t = CType::Stub.new(:rb_code_location_t) + + def C.iseq_compile_data = CType::Stub.new(:iseq_compile_data) + + def C.rb_hook_list_struct = CType::Stub.new(:rb_hook_list_struct) + + def C.rb_event_flag_t = CType::Stub.new(:rb_event_flag_t) + + def C.rb_method_cfunc_t = CType::Stub.new(:rb_method_cfunc_t) + + def C.rb_method_attr_t = CType::Stub.new(:rb_method_attr_t) + + def C.rb_method_alias_t = CType::Stub.new(:rb_method_alias_t) + + def C.rb_method_refined_t = CType::Stub.new(:rb_method_refined_t) + + def C.rb_method_bmethod_t = CType::Stub.new(:rb_method_bmethod_t) + + def C.rb_method_optimized_t = CType::Stub.new(:rb_method_optimized_t) + + def C.ccan_list_node = CType::Stub.new(:ccan_list_node) +end diff --git a/lib/mjit/c_64.rb b/lib/mjit/c_64.rb new file mode 100644 index 0000000000..cd0fd47e9e --- /dev/null +++ b/lib/mjit/c_64.rb @@ -0,0 +1,437 @@ +require_relative 'c_type' + +module RubyVM::MJIT + C = Object.new + + def C.NOT_COMPILED_STACK_SIZE = - 1 + + def C.USE_LAZY_LOAD = false + + def C.USE_RVARGC = true + + def C.VM_CALL_KW_SPLAT = ( 0x01 << self.VM_CALL_KW_SPLAT_bit ) + + def C.VM_CALL_TAILCALL = ( 0x01 << self.VM_CALL_TAILCALL_bit ) + + 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 + + def C.IC + @IC ||= self.iseq_inline_constant_cache + end + + def C.IVC + @IVC ||= self.iseq_inline_iv_cache_entry + end + + def C.RB_BUILTIN + @RB_BUILTIN ||= self.rb_builtin_function + end + + def C.VALUE + @VALUE ||= CType::Immediate.new(-5) + end + + def C.compile_branch + @compile_branch ||= CType::Struct.new( + "compile_branch", 8, + stack_size: [0, CType::Immediate.new(-4)], + finish_p: [32, self._Bool], + ) + end + + def C.compile_status + @compile_status ||= CType::Struct.new( + "compile_status", 120, + success: [0, self._Bool], + stack_size_for_pos: [64, CType::Pointer.new { CType::Immediate.new(4) }], + local_stack_p: [128, self._Bool], + is_entries: [192, CType::Pointer.new { self.iseq_inline_storage_entry }], + cc_entries_index: [256, CType::Immediate.new(4)], + compiled_iseq: [320, CType::Pointer.new { self.rb_iseq_constant_body }], + compiled_id: [384, CType::Immediate.new(4)], + compile_info: [448, CType::Pointer.new { self.rb_mjit_compile_info }], + merge_ivar_guards_p: [512, self._Bool], + ivar_serial: [576, self.rb_serial_t], + max_ivar_index: [640, CType::Immediate.new(-5)], + inlined_iseqs: [704, CType::Pointer.new { CType::Pointer.new { self.rb_iseq_constant_body } }], + inline_context: [768, self.inlined_call_context], + ) + end + + def C.inlined_call_context + @inlined_call_context ||= CType::Struct.new( + "inlined_call_context", 24, + orig_argc: [0, CType::Immediate.new(4)], + me: [64, self.VALUE], + param_size: [128, CType::Immediate.new(4)], + local_size: [160, CType::Immediate.new(4)], + ) + end + + def C.iseq_inline_constant_cache + @iseq_inline_constant_cache ||= CType::Struct.new( + "iseq_inline_constant_cache", 16, + entry: [0, CType::Pointer.new { self.iseq_inline_constant_cache_entry }], + segments: [64, CType::Pointer.new { self.ID }], + ) + end + + def C.iseq_inline_constant_cache_entry + @iseq_inline_constant_cache_entry ||= CType::Struct.new( + "iseq_inline_constant_cache_entry", 40, + flags: [0, self.VALUE], + value: [64, self.VALUE], + _unused1: [128, self.VALUE], + _unused2: [192, self.VALUE], + ic_cref: [256, CType::Pointer.new { self.rb_cref_t }], + ) + end + + def C.iseq_inline_iv_cache_entry + @iseq_inline_iv_cache_entry ||= CType::Struct.new( + "iseq_inline_iv_cache_entry", 8, + entry: [0, CType::Pointer.new { self.rb_iv_index_tbl_entry }], + ) + end + + def C.iseq_inline_storage_entry + @iseq_inline_storage_entry ||= CType::Union.new( + "iseq_inline_storage_entry", 16, + once: CType::Struct.new( + "", 16, + running_thread: [0, CType::Pointer.new { self.rb_thread_struct }], + value: [64, self.VALUE], + ), + ic_cache: self.iseq_inline_constant_cache, + iv_cache: self.iseq_inline_iv_cache_entry, + ) + end + + def C.mjit_options + @mjit_options ||= CType::Struct.new( + "mjit_options", 32, + on: [0, self._Bool], + save_temps: [8, self._Bool], + warnings: [16, self._Bool], + debug: [24, self._Bool], + debug_flags: [64, CType::Pointer.new { CType::Immediate.new(2) }], + wait: [128, self._Bool], + min_calls: [160, CType::Immediate.new(-4)], + verbose: [192, CType::Immediate.new(4)], + max_cache_size: [224, CType::Immediate.new(4)], + ) + end + + def C.rb_builtin_function + @rb_builtin_function ||= CType::Struct.new( + "rb_builtin_function", 32, + func_ptr: [0, CType::Pointer.new { CType::Immediate.new(0) }], + argc: [64, CType::Immediate.new(4)], + index: [96, CType::Immediate.new(4)], + name: [128, CType::Pointer.new { CType::Immediate.new(2) }], + compiler: [192, CType::Immediate.new(1)], + ) + end + + def C.rb_call_data + @rb_call_data ||= CType::Struct.new( + "rb_call_data", 16, + ci: [0, CType::Pointer.new { self.rb_callinfo }], + cc: [64, CType::Pointer.new { self.rb_callcache }], + ) + end + + def C.rb_callable_method_entry_struct + @rb_callable_method_entry_struct ||= CType::Struct.new( + "rb_callable_method_entry_struct", 40, + flags: [0, self.VALUE], + defined_class: [64, self.VALUE], + def: [128, CType::Pointer.new { self.rb_method_definition_struct }], + called_id: [192, self.ID], + owner: [256, self.VALUE], + ) + end + + def C.rb_callcache + @rb_callcache ||= CType::Struct.new( + "rb_callcache", 40, + flags: [0, self.VALUE], + klass: [64, self.VALUE], + cme_: [128, CType::Pointer.new { self.rb_callable_method_entry_struct }], + call_: [192, self.vm_call_handler], + aux_: [256, CType::Union.new( + "", 8, + attr_index: CType::Immediate.new(-4), + method_missing_reason: self.method_missing_reason, + v: self.VALUE, + )], + ) + end + + def C.rb_callinfo + @rb_callinfo ||= CType::Struct.new( + "rb_callinfo", 40, + flags: [0, self.VALUE], + kwarg: [64, CType::Pointer.new { self.rb_callinfo_kwarg }], + mid: [128, self.VALUE], + flag: [192, self.VALUE], + argc: [256, self.VALUE], + ) + end + + def C.rb_cref_t + @rb_cref_t ||= CType::Struct.new( + "rb_cref_struct", 40, + flags: [0, self.VALUE], + refinements: [64, self.VALUE], + klass_or_self: [128, self.VALUE], + next: [192, CType::Pointer.new { self.rb_cref_struct }], + scope_visi: [256, self.rb_scope_visibility_t], + ) + end + + def C.rb_iseq_constant_body + @rb_iseq_constant_body ||= CType::Struct.new( + "rb_iseq_constant_body", 336, + type: [0, self.rb_iseq_type], + iseq_size: [32, CType::Immediate.new(-4)], + iseq_encoded: [64, CType::Pointer.new { self.VALUE }], + param: [128, CType::Struct.new( + "", 48, + flags: [0, CType::Struct.new( + "", 4, + has_lead: [0, CType::BitField.new(1, 0)], + has_opt: [1, CType::BitField.new(1, 1)], + has_rest: [2, CType::BitField.new(1, 2)], + has_post: [3, CType::BitField.new(1, 3)], + has_kw: [4, CType::BitField.new(1, 4)], + has_kwrest: [5, CType::BitField.new(1, 5)], + has_block: [6, CType::BitField.new(1, 6)], + ambiguous_param0: [7, CType::BitField.new(1, 7)], + accepts_no_kwarg: [8, CType::BitField.new(1, 0)], + ruby2_keywords: [9, CType::BitField.new(1, 1)], + )], + size: [32, CType::Immediate.new(-4)], + lead_num: [64, CType::Immediate.new(4)], + opt_num: [96, CType::Immediate.new(4)], + rest_start: [128, CType::Immediate.new(4)], + post_start: [160, CType::Immediate.new(4)], + post_num: [192, CType::Immediate.new(4)], + block_start: [224, CType::Immediate.new(4)], + opt_table: [256, CType::Pointer.new { self.VALUE }], + keyword: [320, CType::Pointer.new { self.rb_iseq_param_keyword }], + )], + location: [512, self.rb_iseq_location_t], + insns_info: [960, self.iseq_insn_info], + local_table: [1216, CType::Pointer.new { self.ID }], + catch_table: [1280, CType::Pointer.new { self.iseq_catch_table }], + parent_iseq: [1344, CType::Pointer.new { self.rb_iseq_struct }], + local_iseq: [1408, CType::Pointer.new { self.rb_iseq_struct }], + is_entries: [1472, CType::Pointer.new { self.iseq_inline_storage_entry }], + call_data: [1536, CType::Pointer.new { self.rb_call_data }], + variable: [1600, CType::Struct.new( + "", 40, + flip_count: [0, self.rb_snum_t], + script_lines: [64, self.VALUE], + coverage: [128, self.VALUE], + pc2branchindex: [192, self.VALUE], + original_iseq: [256, CType::Pointer.new { self.VALUE }], + )], + local_table_size: [1920, CType::Immediate.new(-4)], + ic_size: [1952, CType::Immediate.new(-4)], + ise_size: [1984, CType::Immediate.new(-4)], + ivc_size: [2016, CType::Immediate.new(-4)], + icvarc_size: [2048, CType::Immediate.new(-4)], + ci_size: [2080, CType::Immediate.new(-4)], + stack_max: [2112, CType::Immediate.new(-4)], + mark_bits: [2176, CType::Union.new( + "", 8, + list: CType::Pointer.new { self.iseq_bits_t }, + single: self.iseq_bits_t, + )], + catch_except_p: [2240, self._Bool], + builtin_inline_p: [2248, self._Bool], + outer_variables: [2304, CType::Pointer.new { self.rb_id_table }], + mandatory_only_iseq: [2368, CType::Pointer.new { self.rb_iseq_t }], + jit_func: [2432, CType::Immediate.new(1)], + total_calls: [2496, CType::Immediate.new(-5)], + jit_unit: [2560, CType::Pointer.new { self.rb_mjit_unit }], + yjit_payload: [2624, CType::Pointer.new { CType::Immediate.new(0) }], + ) + end + + def C.rb_iseq_location_t + @rb_iseq_location_t ||= CType::Struct.new( + "rb_iseq_location_struct", 56, + pathobj: [0, self.VALUE, true], + base_label: [64, self.VALUE, true], + label: [128, self.VALUE, true], + first_lineno: [192, self.VALUE, true], + node_id: [256, CType::Immediate.new(4)], + code_location: [288, self.rb_code_location_t], + ) + end + + def C.rb_iseq_struct + @rb_iseq_struct ||= CType::Struct.new( + "rb_iseq_struct", 40, + flags: [0, self.VALUE], + wrapper: [64, self.VALUE], + body: [128, CType::Pointer.new { self.rb_iseq_constant_body }], + aux: [192, CType::Union.new( + "", 16, + compile_data: CType::Pointer.new { self.iseq_compile_data }, + loader: CType::Struct.new( + "", 16, + obj: [0, self.VALUE], + index: [64, CType::Immediate.new(4)], + ), + exec: CType::Struct.new( + "", 16, + local_hooks: [0, CType::Pointer.new { self.rb_hook_list_struct }], + global_trace_events: [64, self.rb_event_flag_t], + ), + )], + ) + end + + def C.rb_iseq_t + @rb_iseq_t ||= self.rb_iseq_struct + end + + def C.rb_iv_index_tbl_entry + @rb_iv_index_tbl_entry ||= CType::Struct.new( + "rb_iv_index_tbl_entry", 24, + index: [0, CType::Immediate.new(-4)], + class_serial: [64, self.rb_serial_t], + class_value: [128, self.VALUE], + ) + end + + def C.rb_method_definition_struct + @rb_method_definition_struct ||= CType::Struct.new( + "rb_method_definition_struct", 48, + type: [0, self.rb_method_type_t], + iseq_overload: [4, CType::BitField.new(1, 4)], + alias_count: [5, CType::BitField.new(27, 5)], + complemented_count: [32, CType::BitField.new(28, 0)], + no_redef_warning: [60, CType::BitField.new(1, 4)], + body: [64, CType::Union.new( + "", 24, + iseq: self.rb_method_iseq_t, + cfunc: self.rb_method_cfunc_t, + attr: self.rb_method_attr_t, + alias: self.rb_method_alias_t, + refined: self.rb_method_refined_t, + bmethod: self.rb_method_bmethod_t, + optimized: self.rb_method_optimized_t, + )], + original_id: [256, self.ID], + method_serial: [320, CType::Immediate.new(-5)], + ) + end + + def C.rb_method_iseq_t + @rb_method_iseq_t ||= CType::Struct.new( + "rb_method_iseq_struct", 16, + iseqptr: [0, CType::Pointer.new { self.rb_iseq_t }], + cref: [64, CType::Pointer.new { self.rb_cref_t }], + ) + end + + def C.rb_method_type_t + @rb_method_type_t ||= CType::Immediate.new(4) + end + + def C.rb_mjit_compile_info + @rb_mjit_compile_info ||= CType::Struct.new( + "rb_mjit_compile_info", 5, + disable_ivar_cache: [0, self._Bool], + disable_exivar_cache: [8, self._Bool], + disable_send_cache: [16, self._Bool], + disable_inlining: [24, self._Bool], + disable_const_cache: [32, self._Bool], + ) + end + + def C.rb_mjit_unit + @rb_mjit_unit ||= CType::Struct.new( + "rb_mjit_unit", 64, + unode: [0, self.ccan_list_node], + id: [128, CType::Immediate.new(4)], + handle: [192, CType::Pointer.new { CType::Immediate.new(0) }], + iseq: [256, CType::Pointer.new { self.rb_iseq_t }], + used_code_p: [320, self._Bool], + compact_p: [328, self._Bool], + compile_info: [336, self.rb_mjit_compile_info], + cc_entries: [384, CType::Pointer.new { CType::Pointer.new { self.rb_callcache } }], + cc_entries_size: [448, CType::Immediate.new(-4)], + ) + end + + def C.rb_serial_t + @rb_serial_t ||= CType::Immediate.new(-6) + end + + def C._Bool = CType::Bool.new + + def C.ID = CType::Stub.new(:ID) + + def C.rb_thread_struct = CType::Stub.new(:rb_thread_struct) + + def C.vm_call_handler = CType::Stub.new(:vm_call_handler) + + def C.method_missing_reason = CType::Stub.new(:method_missing_reason) + + def C.rb_callinfo_kwarg = CType::Stub.new(:rb_callinfo_kwarg) + + def C.rb_cref_struct = CType::Stub.new(:rb_cref_struct) + + def C.rb_scope_visibility_t = CType::Stub.new(:rb_scope_visibility_t) + + def C.rb_iseq_type = CType::Stub.new(:rb_iseq_type) + + def C.rb_iseq_param_keyword = CType::Stub.new(:rb_iseq_param_keyword) + + def C.iseq_insn_info = CType::Stub.new(:iseq_insn_info) + + def C.iseq_catch_table = CType::Stub.new(:iseq_catch_table) + + def C.rb_snum_t = CType::Stub.new(:rb_snum_t) + + def C.iseq_bits_t = CType::Stub.new(:iseq_bits_t) + + def C.rb_id_table = CType::Stub.new(:rb_id_table) + + def C.rb_code_location_t = CType::Stub.new(:rb_code_location_t) + + def C.iseq_compile_data = CType::Stub.new(:iseq_compile_data) + + def C.rb_hook_list_struct = CType::Stub.new(:rb_hook_list_struct) + + def C.rb_event_flag_t = CType::Stub.new(:rb_event_flag_t) + + def C.rb_method_cfunc_t = CType::Stub.new(:rb_method_cfunc_t) + + def C.rb_method_attr_t = CType::Stub.new(:rb_method_attr_t) + + def C.rb_method_alias_t = CType::Stub.new(:rb_method_alias_t) + + def C.rb_method_refined_t = CType::Stub.new(:rb_method_refined_t) + + def C.rb_method_bmethod_t = CType::Stub.new(:rb_method_bmethod_t) + + def C.rb_method_optimized_t = CType::Stub.new(:rb_method_optimized_t) + + def C.ccan_list_node = CType::Stub.new(:ccan_list_node) +end diff --git a/lib/mjit/c_pointer.rb b/lib/mjit/c_pointer.rb new file mode 100644 index 0000000000..75ed8babd5 --- /dev/null +++ b/lib/mjit/c_pointer.rb @@ -0,0 +1,333 @@ +module RubyVM::MJIT + # Every class under this namespace is a pointer. Even if the type is + # immediate, it shouldn't be dereferenced until `*` is called. + module CPointer + # Patched PACK_MAP to support unsigned operations + PACK_MAP = Fiddle::PackInfo::PACK_MAP.dup + PACK_MAP.keys.each do |type| + if type.negative? || type == Fiddle::TYPE_VOIDP + PACK_MAP[type] = PACK_MAP[type].upcase + end + end + + # Note: We'd like to avoid alphabetic method names to avoid a conflict + # with member methods. to_i and to_s are considered an exception. + class Struct + # @param name [String] + # @param sizeof [Integer] + # @param members [Hash{ Symbol => [Integer, RubyVM::MJIT::CType::*] }] + def initialize(addr, sizeof, members) + @addr = addr + @sizeof = sizeof + @members = members + end + + # Get a raw address + def to_i + @addr + end + + # Serialized address for generated code + def to_s + "0x#{@addr.to_s(16)}" + end + + # Pointer diff + def -(struct) + raise ArgumentError if self.class != struct.class + (@addr - struct.to_i) / @sizeof + end + + # Primitive API that does no automatic dereference + # TODO: remove this? + # @param member [Symbol] + def [](member) + offset, type = @members.fetch(member) + type.new(@addr + offset / 8) + end + + private + + # @param member [Symbol] + # @param value [Object] + def []=(member, value) + offset, type = @members.fetch(member) + type[@addr + offset / 8] = value + end + + # @param sizeof [Integer] + # @param members [Hash{ Symbol => [Integer, RubyVM::MJIT::CType::*] }] + def self.define(sizeof, members) + Class.new(self) do + # Return the size of this type + define_singleton_method(:sizeof) { sizeof } + + define_method(:initialize) do |addr = nil| + if addr.nil? # TODO: get rid of this feature later + addr = Fiddle.malloc(sizeof) + end + super(addr, sizeof, members) + end + + members.each do |member, (offset, type, to_ruby)| + # Intelligent API that does automatic dereference + define_method(member) do + value = self[member] + if value.respond_to?(:*) + value = value.* + end + if to_ruby + value = C.to_ruby(value) + end + value + end + + define_method("#{member}=") do |value| + self[member] = value + end + end + end + end + end + + # Note: We'd like to avoid alphabetic method names to avoid a conflict + # with member methods. to_i is considered an exception. + class Union + # @param _name [String] To be used when it starts defining a union pointer class + # @param sizeof [Integer] + # @param members [Hash{ Symbol => RubyVM::MJIT::CType::* }] + def initialize(addr, sizeof, members) + @addr = addr + @sizeof = sizeof + @members = members + end + + # Get a raw address + def to_i + @addr + end + + # Move addr to access this pointer like an array + def +(index) + raise ArgumentError unless index.is_a?(Integer) + self.class.new(@addr + index * @sizeof) + end + + # Pointer diff + def -(union) + raise ArgumentError if self.class != union.class + (@addr - union.instance_variable_get(:@addr)) / @sizeof + end + + # @param sizeof [Integer] + # @param members [Hash{ Symbol => RubyVM::MJIT::CType::* }] + def self.define(sizeof, members) + Class.new(self) do + # Return the size of this type + define_singleton_method(:sizeof) { sizeof } + + define_method(:initialize) do |addr| + super(addr, sizeof, members) + end + + members.each do |member, type| + # Intelligent API that does automatic dereference + define_method(member) do + value = type.new(@addr) + if value.respond_to?(:*) + value = value.* + end + value + end + end + end + end + end + + class Immediate + # @param addr [Integer] + # @param size [Integer] + # @param pack [String] + def initialize(addr, size, pack) + @addr = addr + @size = size + @pack = pack + end + + # Get a raw address + def to_i + @addr + end + + # Move addr to addess this pointer like an array + def +(index) + Immediate.new(@addr + index * @size, @size, @pack) + end + + # Dereference + def * + self[0] + end + + # Array access + def [](index) + return nil if @addr == 0 + Fiddle::Pointer.new(@addr + index * @size)[0, @size].unpack1(@pack) + end + + # Array set + def []=(index, value) + Fiddle::Pointer.new(@addr + index * @size)[0, @size] = [value].pack(@pack) + end + + # Serialized address for generated code. Used for embedding things like body->iseq_encoded. + def to_s + "0x#{Integer(@addr).to_s(16)}" + end + + # @param fiddle_type [Integer] Fiddle::TYPE_* + def self.define(fiddle_type) + size = Fiddle::PackInfo::SIZE_MAP.fetch(fiddle_type) + pack = PACK_MAP.fetch(fiddle_type) + + Class.new(self) do + define_method(:initialize) do |addr| + super(addr, size, pack) + end + + define_singleton_method(:size) do + size + end + + # Type-level []=: Used by struct fields + define_singleton_method(:[]=) do |addr, value| + Fiddle::Pointer.new(addr)[0, size] = [value].pack(pack) + end + end + end + end + + # -Fiddle::TYPE_CHAR Immediate with special handling of true/false + class Bool < Immediate.define(-Fiddle::TYPE_CHAR) + # Dereference + def * + return nil if @addr == 0 + super != 0 + end + + def self.[]=(addr, value) + super(addr, value ? 1 : 0) + end + end + + class Pointer + attr_reader :type + + # @param addr [Integer] + # @param type [Class] RubyVM::MJIT::CType::* + def initialize(addr, type) + @addr = addr + @type = type + end + + # Move addr to addess this pointer like an array + def +(index) + raise ArgumentError unless index.is_a?(Integer) + Pointer.new(@addr + index * Fiddle::SIZEOF_VOIDP, @type) + end + + # Dereference + def * + return nil if dest_addr == 0 + @type.new(dest_addr) + end + + # Array access + def [](index) + (self + index).* + end + + # Array set + # @param index [Integer] + # @param value [Integer, RubyVM::MJIT::CPointer::Struct] an address itself or an object that return an address with to_i + def []=(index, value) + Fiddle::Pointer.new(@addr + index * Fiddle::SIZEOF_VOIDP)[0, Fiddle::SIZEOF_VOIDP] = + [value.to_i].pack(PACK_MAP[Fiddle::TYPE_VOIDP]) + end + + private + + def dest_addr + Fiddle::Pointer.new(@addr)[0, Fiddle::SIZEOF_VOIDP].unpack1(PACK_MAP[Fiddle::TYPE_VOIDP]) + end + + def self.define(block) + Class.new(self) do + define_method(:initialize) do |addr| + super(addr, block.call) + end + + # Type-level []=: Used by struct fields + # @param addr [Integer] + # @param value [Integer, RubyVM::MJIT::CPointer::Struct] an address itself, or an object that return an address with to_i + define_singleton_method(:[]=) do |addr, value| + value = value.to_i + Fiddle::Pointer.new(addr)[0, Fiddle::SIZEOF_VOIDP] = [value].pack(PACK_MAP[Fiddle::TYPE_VOIDP]) + end + end + end + end + + class BitField + # @param addr [Integer] + # @param width [Integer] + # @param offset [Integer] + def initialize(addr, width, offset) + @addr = addr + @width = width + @offset = offset + end + + # Dereference + def * + if @width != 1 + raise NotImplementedError.new("Non-1 width is not implemented yet") + end + byte = Fiddle::Pointer.new(@addr)[0, Fiddle::SIZEOF_CHAR].unpack('c').first + bit = 1 & (byte >> @offset) + bit == 1 + end + + # @param width [Integer] + # @param offset [Integer] + def self.define(width, offset) + Class.new(self) do + define_method(:initialize) do |addr| + super(addr, width, offset) + end + end + end + end + + # Give a name to a dynamic CPointer class to see it on inspect + def self.with_class_name(prefix, name, cache: false, &block) + return block.call if name.empty? + + # Use a cached result only if cache: true + class_name = "#{prefix}_#{name}" + klass = + if cache && self.const_defined?(class_name) + self.const_get(class_name) + else + block.call + end + + # Give it a name unless it's already defined + unless self.const_defined?(class_name) + self.const_set(class_name, klass) + end + + klass + end + end +end diff --git a/lib/mjit/c_type.rb b/lib/mjit/c_type.rb new file mode 100644 index 0000000000..be93b36e90 --- /dev/null +++ b/lib/mjit/c_type.rb @@ -0,0 +1,77 @@ +require 'fiddle' +require 'fiddle/pack' +require_relative 'c_pointer' + +module RubyVM::MJIT + module CType + module Struct + # @param name [String] + # @param members [Hash{ Symbol => [Integer, RubyVM::MJIT::CType::*] }] + def self.new(name, sizeof, **members) + name = members.keys.join('_') if name.empty? + CPointer.with_class_name('Struct', name) do + CPointer::Struct.define(sizeof, members) + end + end + end + + module Union + # @param name [String] + # @param members [Hash{ Symbol => RubyVM::MJIT::CType::* }] + def self.new(name, sizeof, **members) + name = members.keys.join('_') if name.empty? + CPointer.with_class_name('Union', name) do + CPointer::Union.define(sizeof, members) + end + end + end + + module Immediate + # @param fiddle_type [Integer] Fiddle::TYPE_* + def self.new(fiddle_type) + name = Fiddle.constants.find do |const| + const.start_with?('TYPE_') && Fiddle.const_get(const) == fiddle_type.abs + end&.to_s + name.delete_prefix!('TYPE_') + if fiddle_type.negative? + name.prepend('U') + end + CPointer.with_class_name('Immediate', name, cache: true) do + CPointer::Immediate.define(fiddle_type) + end + end + end + + module Bool + def self.new + CPointer::Bool + end + end + + class Pointer + # This takes a block to avoid "stack level too deep" on a cyclic reference + # @param block [Proc] + def self.new(&block) + CPointer.with_class_name('Pointer', block.object_id.to_s) do + CPointer::Pointer.define(block) + end + end + end + + module BitField + # @param width [Integer] + # @param offset [Integer] + def self.new(width, offset) + CPointer.with_class_name('BitField', "#{offset}_#{width}") do + CPointer::BitField.define(width, offset) + end + end + end + + # Types that are referenced but not part of code generation targets + Stub = ::Struct.new(:name) + + # Types that it failed to figure out from the header + Unknown = Module.new + end +end diff --git a/lib/mjit/compiler.rb b/lib/mjit/compiler.rb new file mode 100644 index 0000000000..254ecc885f --- /dev/null +++ b/lib/mjit/compiler.rb @@ -0,0 +1,1019 @@ +module RubyVM::MJIT + USE_RVARGC = C.USE_RVARGC + ROBJECT_EMBED_LEN_MAX = C.ROBJECT_EMBED_LEN_MAX + + UNSUPPORTED_INSNS = [ + :defineclass, # low priority + ] + + # Available variables and macros in JIT-ed function: + # ec: the first argument of _mjitXXX + # reg_cfp: the second argument of _mjitXXX + # GET_CFP(): refers to `reg_cfp` + # GET_EP(): refers to `reg_cfp->ep` + # GET_SP(): refers to `reg_cfp->sp`, or `(stack + stack_size)` if local_stack_p + # GET_SELF(): refers to `cfp_self` + # GET_LEP(): refers to `VM_EP_LEP(reg_cfp->ep)` + # EXEC_EC_CFP(): refers to `val = vm_exec(ec, true)` with frame setup + # CALL_METHOD(): using `GET_CFP()` and `EXEC_EC_CFP()` + # TOPN(): refers to `reg_cfp->sp`, or `*(stack + (stack_size - num - 1))` if local_stack_p + # STACK_ADDR_FROM_TOP(): refers to `reg_cfp->sp`, or `stack + (stack_size - num)` if local_stack_p + # DISPATCH_ORIGINAL_INSN(): expanded in _mjit_compile_insn.erb + # THROW_EXCEPTION(): specially defined for JIT + # RESTORE_REGS(): specially defined for `leave` + class << Compiler = Module.new + # mjit_compile + # @param funcname [String] + def compile(f, iseq, funcname, id) + status = C.compile_status.new # not freed for now + status.compiled_iseq = iseq.body + status.compiled_id = id + init_compile_status(status, iseq.body, true) # not freed for now + if iseq.body.ci_size > 0 && status.cc_entries_index == -1 + return false + end + + init_ivar_compile_status(iseq.body, status) + + if !status.compile_info.disable_send_cache && !status.compile_info.disable_inlining + unless precompile_inlinable_iseqs(f, iseq, status) + return false + end + end + + C.fprintf(f, "VALUE\n#{funcname}(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp)\n{\n") + success = compile_body(f, iseq, status) + C.fprintf(f, "\n} // end of #{funcname}\n") + + return success + rescue => e # Should rb_rescue be called in C? + if C.mjit_opts.warnings || C.mjit_opts.verbose > 0 + $stderr.puts e.full_message + end + return false + end + + # mjit_compile_body + def compile_body(f, iseq, status) + status.success = true + status.local_stack_p = !iseq.body.catch_except_p + + if status.local_stack_p + src = +" VALUE stack[#{iseq.body.stack_max}];\n" + else + src = +" VALUE *stack = reg_cfp->sp;\n" + end + + unless status.inlined_iseqs.nil? # i.e. compile root + src << " static const rb_iseq_t *original_iseq = (const rb_iseq_t *)#{iseq};\n" + end + src << " static const VALUE *const original_body_iseq = (VALUE *)#{iseq.body.iseq_encoded};\n" + + src << " VALUE cfp_self = reg_cfp->self;\n" # cache self across the method + src << "#undef GET_SELF\n" + src << "#define GET_SELF() cfp_self\n" + + # Generate merged ivar guards first if needed + if !status.compile_info.disable_ivar_cache && status.merge_ivar_guards_p + src << " if (UNLIKELY(!(RB_TYPE_P(GET_SELF(), T_OBJECT) && (rb_serial_t)#{status.ivar_serial} == RCLASS_SERIAL(RBASIC(GET_SELF())->klass) &&" + if USE_RVARGC + src << "#{status.max_ivar_index} < ROBJECT_NUMIV(GET_SELF())" # index < ROBJECT_NUMIV(obj) + else + if status.max_ivar_index >= ROBJECT_EMBED_LEN_MAX + src << "#{status.max_ivar_index} < ROBJECT_NUMIV(GET_SELF())" # index < ROBJECT_NUMIV(obj) && !RB_FL_ANY_RAW(obj, ROBJECT_EMBED) + else + src << "ROBJECT_EMBED_LEN_MAX == ROBJECT_NUMIV(GET_SELF())" # index < ROBJECT_NUMIV(obj) && RB_FL_ANY_RAW(obj, ROBJECT_EMBED) + end + end + src << "))) {\n" + src << " goto ivar_cancel;\n" + src << " }\n" + end + + # Simulate `opt_pc` in setup_parameters_complex. Other PCs which may be passed by catch tables + # are not considered since vm_exec doesn't call jit_exec for catch tables. + if iseq.body.param.flags.has_opt + src << "\n" + src << " switch (reg_cfp->pc - ISEQ_BODY(reg_cfp->iseq)->iseq_encoded) {\n" + (0..iseq.body.param.opt_num).each do |i| + pc_offset = iseq.body.param.opt_table[i] + src << " case #{pc_offset}:\n" + src << " goto label_#{pc_offset};\n" + end + src << " }\n" + end + + C.fprintf(f, src) + compile_insns(0, 0, status, iseq.body, f) + compile_cancel_handler(f, iseq.body, status) + C.fprintf(f, "#undef GET_SELF\n") + return status.success + end + + # Compile one conditional branch. If it has branchXXX insn, this should be + # called multiple times for each branch. + def compile_insns(stack_size, pos, status, body, f) + branch = C.compile_branch.new # not freed for now + branch.stack_size = stack_size + branch.finish_p = false + + while pos < body.iseq_size && !already_compiled?(status, pos) && !branch.finish_p + insn = INSNS.fetch(C.rb_vm_insn_decode(body.iseq_encoded[pos])) + status.stack_size_for_pos[pos] = branch.stack_size + + C.fprintf(f, "\nlabel_#{pos}: /* #{insn.name} */\n") + pos = compile_insn(insn, pos, status, body.iseq_encoded + (pos+1), body, branch, f) + if status.success && branch.stack_size > body.stack_max + if mjit_opts.warnings || mjit_opts.verbose > 0 + $stderr.puts "MJIT warning: JIT stack size (#{branch.stack_size}) exceeded its max size (#{body.stack_max})" + end + status.success = false + end + break unless status.success + end + end + + # Main function of JIT compilation, vm_exec_core counterpart for JIT. Compile one insn to `f`, may modify + # b->stack_size and return next position. + # + # When you add a new instruction to insns.def, it would be nice to have JIT compilation support here but + # it's optional. This JIT compiler just ignores ISeq which includes unknown instruction, and ISeq which + # does not have it can be compiled as usual. + def compile_insn(insn, pos, status, operands, body, b, f) + sp_inc = C.mjit_call_attribute_sp_inc(insn.bin, operands) + next_pos = pos + insn.len + + result = compile_insn_entry(f, insn, b.stack_size, sp_inc, status.local_stack_p, pos, next_pos, insn.len, + status.inlined_iseqs.nil?, status, operands, body) + if result.nil? + if C.mjit_opts.warnings || C.mjit_opts.verbose > 0 + $stderr.puts "MJIT warning: Skipped to compile unsupported instruction: #{insn.name}" + end + status.success = false + else + src, next_pos, finish_p, compile_insns_p = result + + C.fprintf(f, src) + b.stack_size += sp_inc + + if finish_p + b.finish_p = true + end + if compile_insns_p + if already_compiled?(status, pos + insn.len) + C.fprintf(f, "goto label_#{pos + insn.len};\n") + else + compile_insns(b.stack_size, pos + insn.len, status, body, f) + end + end + end + + # If next_pos is already compiled and this branch is not finished yet, + # next instruction won't be compiled in C code next and will need `goto`. + if !b.finish_p && next_pos < body.iseq_size && already_compiled?(status, next_pos) + C.fprintf(f, "goto label_#{next_pos};\n") + + # Verify stack size assumption is the same among multiple branches + if status.stack_size_for_pos[next_pos] != b.stack_size + if mjit_opts.warnings || mjit_opts.verbose > 0 + $stderr.puts "MJIT warning: JIT stack assumption is not the same between branches (#{status.stack_size_for_pos[next_pos]} != #{b.stack_size})\n" + end + status.success = false + end + end + + return next_pos + end + + # mjit_comiple.inc.erb + def compile_insn_entry(f, insn, stack_size, sp_inc, local_stack_p, pos, next_pos, insn_len, inlined_iseq_p, status, operands, body) + finish_p = false + compile_insns = false + + # TODO: define this outside this method, or at least cache it + opt_send_without_block = INSNS.values.find { |i| i.name == :opt_send_without_block } + if opt_send_without_block.nil? + raise 'opt_send_without_block not found' + end + send_compatible_opt_insns = INSNS.values.select do |insn| + insn.name.start_with?('opt_') && opt_send_without_block.opes == insn.opes && + insn.expr.lines.any? { |l| l.match(/\A\s+CALL_SIMPLE_METHOD\(\);\s+\z/) } + end.map(&:name) + + case insn.name + when *UNSUPPORTED_INSNS + return nil + when :opt_send_without_block, :send + if src = compile_send(insn, stack_size, sp_inc, local_stack_p, pos, next_pos, status, operands, body) + return src, next_pos, finish_p, compile_insns + end + when *send_compatible_opt_insns + if C.has_cache_for_send(captured_cc_entries(status)[call_data_index(C.CALL_DATA.new(operands[0]), body)], insn.bin) && + src = compile_send(opt_send_without_block, stack_size, sp_inc, local_stack_p, pos, next_pos, status, operands, body) + return src, next_pos, finish_p, compile_insns + end + when :getinstancevariable, :setinstancevariable + if src = compile_ivar(insn.name, stack_size, pos, status, operands, body) + return src, next_pos, finish_p, compile_insns + end + when :invokebuiltin, :opt_invokebuiltin_delegate + if compile_invokebuiltin(f, insn, stack_size, sp_inc, body, operands) + return '', next_pos, finish_p, compile_insns + end + when :opt_getconstant_path + if src = compile_getconstant_path(stack_size, pos, insn_len, operands, status) + return src, next_pos, finish_p, compile_insns + end + when :leave, :opt_invokebuiltin_delegate_leave + src = +'' + + # opt_invokebuiltin_delegate_leave also implements leave insn. We need to handle it here for inlining. + if insn.name == :opt_invokebuiltin_delegate_leave + compile_invokebuiltin(f, insn, stack_size, sp_inc, body, operands) + else + if stack_size != 1 + $stderr.puts "MJIT warning: Unexpected JIT stack_size on leave: #{stack_size}" # TODO: check mjit_opts? + return nil + end + end + + # Skip vm_pop_frame for inlined call + unless inlined_iseq_p + # Cancel on interrupts to make leave insn leaf + src << " if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + src << " reg_cfp->pc = original_body_iseq + #{pos};\n" + src << " rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);\n" + src << " }\n" + src << " ec->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(reg_cfp);\n" # vm_pop_frame + end + src << " return stack[0];\n" + finish_p = true + return src, next_pos, finish_p, compile_insns + end + + return compile_insn_default(insn, stack_size, sp_inc, local_stack_p, pos, next_pos, insn_len, inlined_iseq_p, operands) + rescue => e + puts e.full_message + nil + end + + private + + # Optimized case of send / opt_send_without_block instructions. + # _mjit_compile_send.erb + def compile_send(insn, stack_size, sp_inc, local_stack_p, pos, next_pos, status, operands, body) + # compiler: Use captured cc to avoid race condition + cd = C.CALL_DATA.new(operands[0]) + cd_index = call_data_index(cd, body) + captured_cc = captured_cc_entries(status)[cd_index] + + # compiler: Inline send insn where some supported fastpath is used. + ci = cd.ci + kw_splat = (C.vm_ci_flag(ci) & C.VM_CALL_KW_SPLAT) > 0 + if !status.compile_info.disable_send_cache && has_valid_method_type?(captured_cc) && ( + # `CC_SET_FASTPATH(cd->cc, vm_call_cfunc_with_frame, ...)` in `vm_call_cfunc` + (vm_cc_cme(captured_cc).def.type == C.VM_METHOD_TYPE_CFUNC && !C.rb_splat_or_kwargs_p(ci) && !kw_splat) || + # `CC_SET_FASTPATH(cc, vm_call_iseq_setup_func(...), vm_call_iseq_optimizable_p(...))` in `vm_callee_setup_arg`, + # and support only non-VM_CALL_TAILCALL path inside it + (vm_cc_cme(captured_cc).def.type == C.VM_METHOD_TYPE_ISEQ && + C.fastpath_applied_iseq_p(ci, captured_cc, iseq = def_iseq_ptr(vm_cc_cme(captured_cc).def)) && + (C.vm_ci_flag(ci) & C.VM_CALL_TAILCALL) == 0) + ) + src = +"{\n" + + # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things. + src << " const struct rb_callcache *cc = (struct rb_callcache *)#{captured_cc};\n" + src << " const rb_callable_method_entry_t *cc_cme = (rb_callable_method_entry_t *)#{vm_cc_cme(captured_cc)};\n" + src << " const VALUE recv = stack[#{stack_size + sp_inc - 1}];\n" + # If opt_class_of is true, use RBASIC_CLASS instead of CLASS_OF to reduce code size + opt_class_of = !maybe_special_const?(captured_cc.klass) + src << " if (UNLIKELY(#{opt_class_of ? 'RB_SPECIAL_CONST_P(recv)' : 'false'} || !vm_cc_valid_p(cc, cc_cme, #{opt_class_of ? 'RBASIC_CLASS' : 'CLASS_OF'}(recv)))) {\n" + src << " reg_cfp->pc = original_body_iseq + #{pos};\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + src << " goto send_cancel;\n" + src << " }\n" + + # JIT: move sp and pc if necessary + pc_moved_p = compile_pc_and_sp(src, insn, stack_size, sp_inc, local_stack_p, next_pos) + + # JIT: If ISeq is inlinable, call the inlined method without pushing a frame. + if iseq && status.inlined_iseqs && iseq.body.to_i == status.inlined_iseqs[pos]&.to_i + src << " {\n" + src << " VALUE orig_self = reg_cfp->self;\n" + src << " reg_cfp->self = stack[#{stack_size + sp_inc - 1}];\n" + src << " stack[#{stack_size + sp_inc - 1}] = _mjit#{status.compiled_id}_inlined_#{pos}(ec, reg_cfp, orig_self, original_iseq);\n" + src << " reg_cfp->self = orig_self;\n" + src << " }\n" + else + # JIT: Forked `vm_sendish` (except method_explorer = vm_search_method_wrap) to inline various things + src << " {\n" + src << " VALUE val;\n" + src << " struct rb_calling_info calling;\n" + if insn.name == :send + src << " calling.block_handler = vm_caller_setup_arg_block(ec, reg_cfp, (const struct rb_callinfo *)#{ci}, (rb_iseq_t *)0x#{operands[1].to_s(16)}, FALSE);\n" + else + src << " calling.block_handler = VM_BLOCK_HANDLER_NONE;\n" + end + src << " calling.kw_splat = #{kw_splat ? 1 : 0};\n" + src << " calling.recv = stack[#{stack_size + sp_inc - 1}];\n" + src << " calling.argc = #{C.vm_ci_argc(ci)};\n" + + if vm_cc_cme(captured_cc).def.type == C.VM_METHOD_TYPE_CFUNC + # TODO: optimize this more + src << " calling.ci = (const struct rb_callinfo *)#{ci};\n" # creating local cd here because operand's cd->cc may not be the same as inlined cc. + src << " calling.cc = cc;" + src << " val = vm_call_cfunc_with_frame(ec, reg_cfp, &calling);\n" + else # :iseq + # fastpath_applied_iseq_p checks rb_simple_iseq_p, which ensures has_opt == FALSE + src << " vm_call_iseq_setup_normal(ec, reg_cfp, &calling, cc_cme, 0, #{iseq.body.param.size}, #{iseq.body.local_table_size});\n" + if iseq.body.catch_except_p + src << " VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);\n" + src << " val = vm_exec(ec, true);\n" + else + src << " if ((val = jit_exec(ec)) == Qundef) {\n" + src << " VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);\n" # This is vm_call0_body's code after vm_call_iseq_setup + src << " val = vm_exec(ec, false);\n" + src << " }\n" + end + end + src << " stack[#{stack_size + sp_inc - 1}] = val;\n" + src << " }\n" + + # JIT: We should evaluate ISeq modified for TracePoint if it's enabled. Note: This is slow. + src << " if (UNLIKELY(!mjit_call_p)) {\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size + sp_inc};\n" + if !pc_moved_p + src << " reg_cfp->pc = original_body_iseq + #{next_pos};\n" + end + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_invalidate_all);\n" + src << " goto cancel;\n" + src << " }\n" + end + + src << "}\n" + return src + else + return nil + end + end + + # _mjit_compile_ivar.erb + def compile_ivar(insn_name, stack_size, pos, status, operands, body) + ic_copy = (status.is_entries + (C.iseq_inline_storage_entry.new(operands[1]) - body.is_entries)).iv_cache + + src = +'' + if !status.compile_info.disable_ivar_cache && ic_copy.entry + # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`. + # compile_pc_and_sp(src, insn, stack_size, sp_inc, local_stack_p, next_pos) + + # JIT: prepare vm_getivar/vm_setivar arguments and variables + src << "{\n" + src << " VALUE obj = GET_SELF();\n" + src << " const uint32_t index = #{ic_copy.entry.index};\n" + if status.merge_ivar_guards_p + # JIT: Access ivar without checking these VM_ASSERTed prerequisites as we checked them in the beginning of `mjit_compile_body` + src << " VM_ASSERT(RB_TYPE_P(obj, T_OBJECT));\n" + src << " VM_ASSERT((rb_serial_t)#{ic_copy.entry.class_serial} == RCLASS_SERIAL(RBASIC(obj)->klass));\n" + src << " VM_ASSERT(index < ROBJECT_NUMIV(obj));\n" + if insn_name == :setinstancevariable + if USE_RVARGC + src << " if (LIKELY(!RB_OBJ_FROZEN_RAW(obj) && index < ROBJECT_NUMIV(obj))) {\n" + src << " RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[index], stack[#{stack_size - 1}]);\n" + else + heap_ivar_p = status.max_ivar_index >= ROBJECT_EMBED_LEN_MAX + src << " if (LIKELY(!RB_OBJ_FROZEN_RAW(obj) && #{heap_ivar_p ? 'true' : 'RB_FL_ANY_RAW(obj, ROBJECT_EMBED)'})) {\n" + src << " RB_OBJ_WRITE(obj, &ROBJECT(obj)->as.#{heap_ivar_p ? 'heap.ivptr[index]' : 'ary[index]'}, stack[#{stack_size - 1}]);\n" + end + src << " }\n" + else + src << " VALUE val;\n" + if USE_RVARGC + src << " if (LIKELY(index < ROBJECT_NUMIV(obj) && (val = ROBJECT_IVPTR(obj)[index]) != Qundef)) {\n" + else + heap_ivar_p = status.max_ivar_index >= ROBJECT_EMBED_LEN_MAX + src << " if (LIKELY(#{heap_ivar_p ? 'true' : 'RB_FL_ANY_RAW(obj, ROBJECT_EMBED)'} && (val = ROBJECT(obj)->as.#{heap_ivar_p ? 'heap.ivptr[index]' : 'ary[index]'}) != Qundef)) {\n" + end + src << " stack[#{stack_size}] = val;\n" + src << " }\n" + end + else + src << " const rb_serial_t ic_serial = (rb_serial_t)#{ic_copy.entry.class_serial};\n" + # JIT: cache hit path of vm_getivar/vm_setivar, or cancel JIT (recompile it with exivar) + if insn_name == :setinstancevariable + src << " if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj) && !RB_OBJ_FROZEN_RAW(obj))) {\n" + src << " VALUE *ptr = ROBJECT_IVPTR(obj);\n" + src << " RB_OBJ_WRITE(obj, &ptr[index], stack[#{stack_size - 1}]);\n" + src << " }\n" + else + src << " VALUE val;\n" + src << " if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj) && (val = ROBJECT_IVPTR(obj)[index]) != Qundef)) {\n" + src << " stack[#{stack_size}] = val;\n" + src << " }\n" + end + end + src << " else {\n" + src << " reg_cfp->pc = original_body_iseq + #{pos};\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + src << " goto ivar_cancel;\n" + src << " }\n" + src << "}\n" + return src + elsif insn_name == :getinstancevariable && !status.compile_info.disable_exivar_cache && ic_copy.entry + # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`. + # compile_pc_and_sp(src, insn, stack_size, sp_inc, local_stack_p, next_pos) + + # JIT: prepare vm_getivar's arguments and variables + src << "{\n" + src << " VALUE obj = GET_SELF();\n" + src << " const rb_serial_t ic_serial = (rb_serial_t)#{ic_copy.entry.class_serial};\n" + src << " const uint32_t index = #{ic_copy.entry.index};\n" + # JIT: cache hit path of vm_getivar, or cancel JIT (recompile it without any ivar optimization) + src << " struct gen_ivtbl *ivtbl;\n" + src << " VALUE val;\n" + src << " if (LIKELY(FL_TEST_RAW(obj, FL_EXIVAR) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && rb_ivar_generic_ivtbl_lookup(obj, &ivtbl) && index < ivtbl->numiv && (val = ivtbl->ivptr[index]) != Qundef)) {\n" + src << " stack[#{stack_size}] = val;\n" + src << " }\n" + src << " else {\n" + src << " reg_cfp->pc = original_body_iseq + #{pos};\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + src << " goto exivar_cancel;\n" + src << " }\n" + src << "}\n" + return src + else + return nil + end + end + + # _mjit_compile_invokebulitin.erb + def compile_invokebuiltin(f, insn, stack_size, sp_inc, body, operands) + bf = C.RB_BUILTIN.new(operands[0]) + if bf.compiler > 0 + C.fprintf(f, "{\n") + C.fprintf(f, " VALUE val;\n") + C.builtin_compiler(f, bf, operands[1], stack_size, body.builtin_inline_p) + C.fprintf(f, " stack[#{stack_size + sp_inc - 1}] = val;\n") + C.fprintf(f, "}\n") + return true + else + return false + end + end + + # _mjit_compile_getconstant_path.erb + def compile_getconstant_path(stack_size, pos, insn_len, operands, status) + ice = C.IC.new(operands[0]).entry + if !status.compile_info.disable_const_cache && ice + # JIT: Inline everything in IC, and cancel the slow path + src = +" if (vm_inlined_ic_hit_p(#{ice.flags}, #{ice.value}, (const rb_cref_t *)#{to_addr(ice.ic_cref)}, reg_cfp->ep)) {\n" + src << " stack[#{stack_size}] = #{ice.value};\n" + src << " }\n" + src << " else {\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + src << " reg_cfp->pc = original_body_iseq + #{pos};\n" + src << " goto const_cancel;\n" + src << " }\n" + return src + else + return nil + end + end + + # _mjit_compile_insn.erb + def compile_insn_default(insn, stack_size, sp_inc, local_stack_p, pos, next_pos, insn_len, inlined_iseq_p, operands) + src = +'' + finish_p = false + compile_insns = false + + # JIT: Declare stack_size to be used in some macro of _mjit_compile_insn_body.erb + src << "{\n" + if local_stack_p + src << " MAYBE_UNUSED(unsigned int) stack_size = #{stack_size};\n" + end + + # JIT: Declare variables for operands, popped values and return values + insn.declarations.each do |decl| + src << " #{decl};\n" + end + + # JIT: Set const expressions for `RubyVM::OperandsUnifications` insn + insn.preamble.each do |amble| + src << "#{amble.sub(/const \S+\s+/, '')}\n" + end + + # JIT: Initialize operands + insn.opes.each_with_index do |ope, i| + src << " #{ope.fetch(:name)} = (#{ope.fetch(:type)})#{operands[i]};\n" + # TODO: resurrect comment_id + end + + # JIT: Initialize popped values + insn.pops.reverse_each.with_index.reverse_each do |pop, i| + src << " #{pop.fetch(:name)} = stack[#{stack_size - (i + 1)}];\n" + end + + # JIT: move sp and pc if necessary + pc_moved_p = compile_pc_and_sp(src, insn, stack_size, sp_inc, local_stack_p, next_pos) + + # JIT: Print insn body in insns.def + next_pos = compile_insn_body(src, insn, pos, next_pos, insn_len, local_stack_p, stack_size, sp_inc, operands) + + # JIT: Set return values + insn.rets.reverse_each.with_index do |ret, i| + # TOPN(n) = ... + src << " stack[#{stack_size + sp_inc - (i + 1)}] = #{ret.fetch(:name)};\n" + end + + # JIT: We should evaluate ISeq modified for TracePoint if it's enabled. Note: This is slow. + # leaf insn may not cancel JIT. leaf_without_check_ints is covered in RUBY_VM_CHECK_INTS of _mjit_compile_insn_body.erb. + unless insn.always_leaf? || insn.leaf_without_check_ints? + src << " if (UNLIKELY(!mjit_call_p)) {\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size + sp_inc};\n" + if !pc_moved_p + src << " reg_cfp->pc = original_body_iseq + #{next_pos};\n" + end + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_invalidate_all);\n" + src << " goto cancel;\n" + src << " }\n" + end + + src << "}\n" + + # compiler: If insn has conditional JUMP, the code should go to the branch not targeted by JUMP next. + if insn.expr.match?(/if\s+\([^{}]+\)\s+\{[^{}]+JUMP\([^)]+\);[^{}]+\}/) + compile_insns = true + end + + # compiler: If insn returns (leave) or does longjmp (throw), the branch should no longer be compiled. TODO: create attr for it? + if insn.expr.match?(/\sTHROW_EXCEPTION\([^)]+\);/) || insn.expr.match?(/\bvm_pop_frame\(/) + finish_p = true + end + + return src, next_pos, finish_p, compile_insns + end + + # _mjit_compile_insn_body.erb + def compile_insn_body(src, insn, pos, next_pos, insn_len, local_stack_p, stack_size, sp_inc, operands) + # Print a body of insn, but with macro expansion. + expand_simple_macros(insn.expr).each_line do |line| + # Expand dynamic macro here + # TODO: support combination of following macros in the same line + case line + when /\A\s+RUBY_VM_CHECK_INTS\(ec\);\s+\z/ + if insn.leaf_without_check_ints? # lazily move PC and optionalize mjit_call_p here + src << " if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {\n" + src << " reg_cfp->pc = original_body_iseq + #{next_pos};\n" # ADD_PC(INSN_ATTR(width)); + src << " rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);\n" + src << " if (UNLIKELY(!mjit_call_p)) {\n" + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_invalidate_all);\n" + src << " goto cancel;\n" + src << " }\n" + src << " }\n" + else + src << to_cstr(line) + end + when /\A\s+JUMP\((?[^)]+)\);\s+\z/ + dest = Regexp.last_match[:dest] + if insn.name == :opt_case_dispatch # special case... TODO: use another macro to avoid checking name + else_offset = cast_offset(operands[1]) + cdhash = C.cdhash_to_hash(operands[0]) + base_pos = pos + insn_len + + src << " switch (#{dest}) {\n" + cdhash.each do |_key, offset| + src << " case #{offset}:\n" + src << " goto label_#{base_pos + offset};\n" + end + src << " case #{else_offset}:\n" + src << " goto label_#{base_pos + else_offset};\n" + src << " }\n" + else + # Before we `goto` next insn, we need to set return values, especially for getinlinecache + insn.rets.reverse_each.with_index do |ret, i| + # TOPN(n) = ... + src << " stack[#{stack_size + sp_inc - (i + 1)}] = #{ret.fetch(:name)};\n" + end + + next_pos = pos + insn_len + cast_offset(operands[0]) # workaround: assuming dest == operands[0]. TODO: avoid relying on it + src << " goto label_#{next_pos};\n" + end + when /\A\s+CALL_SIMPLE_METHOD\(\);\s+\z/ + # For `opt_xxx`'s fallbacks. + if local_stack_p + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + end + src << " reg_cfp->pc = original_body_iseq + #{pos};\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_opt_insn);\n" + src << " goto cancel;\n" + when /\A(?.+\b)INSN_LABEL\((?[^)]+)\)(?.+)\z/m + prefix, name, suffix = Regexp.last_match[:prefix], Regexp.last_match[:name], Regexp.last_match[:suffix] + src << "#{prefix}INSN_LABEL(#{name}_#{pos})#{suffix}" + else + if insn.handles_sp? + # If insn.handles_sp? is true, cfp->sp might be changed inside insns (like vm_caller_setup_arg_block) + # and thus we need to use cfp->sp, even when local_stack_p is TRUE. When insn.handles_sp? is true, + # cfp->sp should be available too because _mjit_compile_pc_and_sp.erb sets it. + src << to_cstr(line) + else + # If local_stack_p is TRUE and insn.handles_sp? is false, stack values are only available in local variables + # for stack. So we need to replace those macros if local_stack_p is TRUE here. + case line + when /\bGET_SP\(\)/ + # reg_cfp->sp + src << to_cstr(line.sub(/\bGET_SP\(\)/, local_stack_p ? '(stack + stack_size)' : 'GET_SP()')) + when /\bSTACK_ADDR_FROM_TOP\((?[^)]+)\)/ + # #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n)) + num = Regexp.last_match[:num] + src << to_cstr(line.sub(/\bSTACK_ADDR_FROM_TOP\(([^)]+)\)/, local_stack_p ? "(stack + (stack_size - (#{num})))" : "STACK_ADDR_FROM_TOP(#{num})")) + when /\bTOPN\((?[^)]+)\)/ + # #define TOPN(n) (*(GET_SP()-(n)-1)) + num = Regexp.last_match[:num] + src << to_cstr(line.sub(/\bTOPN\(([^)]+)\)/, local_stack_p ? "*(stack + (stack_size - (#{num}) - 1))" : "TOPN(#{num})")) + else + src << to_cstr(line) + end + end + end + end + return next_pos + end + + # _mjit_compile_pc_and_sp.erb + def compile_pc_and_sp(src, insn, stack_size, sp_inc, local_stack_p, next_pos) + # JIT: When an insn is leaf, we don't need to Move pc for a catch table on catch_except_p, #caller_locations, + # and rb_profile_frames. For check_ints, we lazily move PC when we have interruptions. + pc_moved_p = false + unless insn.always_leaf? || insn.leaf_without_check_ints? + src << " reg_cfp->pc = original_body_iseq + #{next_pos};\n" # ADD_PC(INSN_ATTR(width)); + pc_moved_p = true + end + + # JIT: move sp to use or preserve stack variables + if local_stack_p + # sp motion is optimized away for `handles_sp? #=> false` case. + # Thus sp should be set properly before `goto cancel`. + if insn.handles_sp? + # JIT-only behavior (pushing JIT's local variables to VM's stack): + push_size = -sp_inc + insn.rets.size - insn.pops.size + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{push_size};\n" + push_size.times do |i| + src << " *(reg_cfp->sp + #{i - push_size}) = stack[#{stack_size - push_size + i}];\n" + end + end + else + if insn.handles_sp? + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size - insn.pops.size};\n" # POPN(INSN_ATTR(popn)); + else + src << " reg_cfp->sp = vm_base_ptr(reg_cfp) + #{stack_size};\n" + end + end + return pc_moved_p + end + + # Print the block to cancel inlined method call. It's supporting only `opt_send_without_block` for now. + def compile_inlined_cancel_handler(f, body, inline_context) + src = +"\ncancel:\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel);\n" + src << " rb_mjit_recompile_inlining(original_iseq);\n" + + # Swap pc/sp set on cancel with original pc/sp. + src << " const VALUE *current_pc = reg_cfp->pc;\n" + src << " VALUE *current_sp = reg_cfp->sp;\n" + src << " reg_cfp->pc = orig_pc;\n" + src << " reg_cfp->sp = orig_sp;\n\n" + + # Lazily push the current call frame. + src << " struct rb_calling_info calling;\n" + src << " calling.block_handler = VM_BLOCK_HANDLER_NONE;\n" # assumes `opt_send_without_block` + src << " calling.argc = #{inline_context.orig_argc};\n" + src << " calling.recv = reg_cfp->self;\n" + src << " reg_cfp->self = orig_self;\n" + # fastpath_applied_iseq_p checks rb_simple_iseq_p, which ensures has_opt == FALSE + src << " vm_call_iseq_setup_normal(ec, reg_cfp, &calling, (const rb_callable_method_entry_t *)#{inline_context.me}, 0, #{inline_context.param_size}, #{inline_context.local_size});\n\n" + + # Start usual cancel from here. + src << " reg_cfp = ec->cfp;\n" # work on the new frame + src << " reg_cfp->pc = current_pc;\n" + src << " reg_cfp->sp = current_sp;\n" + (0...body.stack_max).each do |i| # should be always `status->local_stack_p` + src << " *(vm_base_ptr(reg_cfp) + #{i}) = stack[#{i}];\n" + end + # We're not just returning Qundef here so that caller's normal cancel handler can + # push back `stack` to `cfp->sp`. + src << " return vm_exec(ec, false);\n" + C.fprintf(f, src) + end + + # Print the block to cancel JIT execution. + def compile_cancel_handler(f, body, status) + if status.inlined_iseqs.nil? # the current ISeq is being inlined + compile_inlined_cancel_handler(f, body, status.inline_context) + return + end + + src = +"\nsend_cancel:\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_send_inline);\n" + src << " rb_mjit_recompile_send(original_iseq);\n" + src << " goto cancel;\n" + + src << "\nivar_cancel:\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_ivar_inline);\n" + src << " rb_mjit_recompile_ivar(original_iseq);\n" + src << " goto cancel;\n" + + src << "\nexivar_cancel:\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel_exivar_inline);\n" + src << " rb_mjit_recompile_exivar(original_iseq);\n" + src << " goto cancel;\n" + + src << "\nconst_cancel:\n" + src << " rb_mjit_recompile_const(original_iseq);\n" + src << " goto cancel;\n" + + src << "\ncancel:\n" + src << " RB_DEBUG_COUNTER_INC(mjit_cancel);\n" + if status.local_stack_p + (0...body.stack_max).each do |i| + src << " *(vm_base_ptr(reg_cfp) + #{i}) = stack[#{i}];\n" + end + end + src << " return Qundef;\n" + C.fprintf(f, src) + end + + def precompile_inlinable_child_iseq(f, child_iseq, status, ci, cc, pos) + child_status = C.compile_status.new # not freed for now + child_status.compiled_iseq = status.compiled_iseq + child_status.compiled_id = status.compiled_id + init_compile_status(child_status, child_iseq.body, false) # not freed for now + child_status.inline_context.orig_argc = C.vm_ci_argc(ci) + child_status.inline_context.me = vm_cc_cme(cc).to_i + child_status.inline_context.param_size = child_iseq.body.param.size + child_status.inline_context.local_size = child_iseq.body.local_table_size + if child_iseq.body.ci_size > 0 && child_status.cc_entries_index == -1 + return false + end + init_ivar_compile_status(child_iseq.body, child_status) + + src = +"ALWAYS_INLINE(static VALUE _mjit#{status.compiled_id}_inlined_#{pos}(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE orig_self, const rb_iseq_t *original_iseq));\n" + src << "static inline VALUE\n_mjit#{status.compiled_id}_inlined_#{pos}(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE orig_self, const rb_iseq_t *original_iseq)\n{\n" + src << " const VALUE *orig_pc = reg_cfp->pc;\n" + src << " VALUE *orig_sp = reg_cfp->sp;\n" + C.fprintf(f, src) + + success = compile_body(f, child_iseq, child_status) + + C.fprintf(f, "\n} /* end of _mjit#{status.compiled_id}_inlined_#{pos} */\n\n") + + return success; + end + + def precompile_inlinable_iseqs(f, iseq, status) + body = iseq.body + pos = 0 + while pos < body.iseq_size + insn = INSNS.fetch(C.rb_vm_insn_decode(body.iseq_encoded[pos])) + if insn.name == :opt_send_without_block || insn.name == :opt_size # `compile_inlined_cancel_handler` supports only `opt_send_without_block` + cd = C.CALL_DATA.new(body.iseq_encoded[pos + 1]) + ci = cd.ci + cc = captured_cc_entries(status)[call_data_index(cd, body)] # use copy to avoid race condition + + if (child_iseq = rb_mjit_inlinable_iseq(ci, cc)) != nil + status.inlined_iseqs[pos] = child_iseq.body + + if C.mjit_opts.verbose >= 1 # print beforehand because ISeq may be GCed during copy job. + child_location = child_iseq.body.location + $stderr.puts "JIT inline: #{child_location.label}@#{C.rb_iseq_path(child_iseq)}:#{child_location.first_lineno} " \ + "=> #{iseq.body.location.label}@#{C.rb_iseq_path(iseq)}:#{iseq.body.location.first_lineno}" + end + if !precompile_inlinable_child_iseq(f, child_iseq, status, ci, cc, pos) + return false + end + end + end + pos += insn.len + end + return true + end + + def init_compile_status(status, body, compile_root_p) + status.stack_size_for_pos = Fiddle.malloc(Fiddle::SIZEOF_INT * body.iseq_size) + body.iseq_size.times do |i| + status.stack_size_for_pos[i] = C.NOT_COMPILED_STACK_SIZE + end + if compile_root_p + status.inlined_iseqs = Fiddle.malloc(Fiddle::SIZEOF_VOIDP * body.iseq_size) + body.iseq_size.times do |i| + status.inlined_iseqs[i] = nil + end + end + if ISEQ_IS_SIZE(body) > 0 + status.is_entries = Fiddle.malloc(C.iseq_inline_storage_entry.sizeof * ISEQ_IS_SIZE(body)) + end + if body.ci_size > 0 + status.cc_entries_index = C.mjit_capture_cc_entries(status.compiled_iseq, body) + else + status.cc_entries_index = -1 + end + if compile_root_p + status.compile_info = rb_mjit_iseq_compile_info(body) + else + status.compile_info = Fiddle.malloc(C.rb_mjit_compile_info.sizeof) + status.compile_info.disable_ivar_cache = false + status.compile_info.disable_exivar_cache = false + status.compile_info.disable_send_cache = false + status.compile_info.disable_inlining = false + status.compile_info.disable_const_cache = false + end + end + + def init_ivar_compile_status(body, status) + C.mjit_capture_is_entries(body, status.is_entries) + + num_ivars = 0 + pos = 0 + status.max_ivar_index = 0 + status.ivar_serial = 0 + + while pos < body.iseq_size + insn = INSNS.fetch(C.rb_vm_insn_decode(body.iseq_encoded[pos])) + if insn.name == :getinstancevariable || insn.name == :setinstancevariable + ic = body.iseq_encoded[pos+2] + ic_copy = (status.is_entries + (C.iseq_inline_storage_entry.new(ic) - body.is_entries)).iv_cache + if ic_copy.entry # Only initialized (ic_serial > 0) IVCs are optimized + num_ivars += 1 + + if status.max_ivar_index < ic_copy.entry.index + status.max_ivar_index = ic_copy.entry.index + end + + if status.ivar_serial == 0 + status.ivar_serial = ic_copy.entry.class_serial + elsif status.ivar_serial != ic_copy.entry.class_serial + # Multiple classes have used this ISeq. Give up assuming one serial. + status.merge_ivar_guards_p = false + return + end + end + end + pos += insn.len + end + status.merge_ivar_guards_p = status.ivar_serial > 0 && num_ivars >= 2 + end + + # Expand simple macro that doesn't require dynamic C code. + def expand_simple_macros(arg_expr) + arg_expr.dup.tap do |expr| + # For `leave`. We can't proceed next ISeq in the same JIT function. + expr.gsub!(/^(?\s*)RESTORE_REGS\(\);\n/) do + indent = Regexp.last_match[:indent] + <<-end.gsub(/^ {12}/, '') + #if OPT_CALL_THREADED_CODE + #{indent}rb_ec_thread_ptr(ec)->retval = val; + #{indent}return 0; + #else + #{indent}return val; + #endif + end + end + expr.gsub!(/^(?\s*)NEXT_INSN\(\);\n/) do + indent = Regexp.last_match[:indent] + <<-end.gsub(/^ {12}/, '') + #{indent}UNREACHABLE_RETURN(Qundef); + end + end + end + end + + def to_cstr(expr) + expr.gsub(/^(?!#)/, ' ') # indent everything but preprocessor lines + end + + # Interpret unsigned long as signed long (VALUE -> OFFSET) + def cast_offset(offset) + bits = "%0#{8 * Fiddle::SIZEOF_VOIDP}d" % offset.to_s(2) + if bits[0] == '1' # negative + offset = -bits.chars.map { |i| i == '0' ? '1' : '0' }.join.to_i(2) - 1 + end + offset + end + + def captured_cc_entries(status) + status.compiled_iseq.jit_unit.cc_entries + status.cc_entries_index + end + + def call_data_index(cd, body) + cd - body.call_data + end + + def vm_cc_cme(cc) + # TODO: add VM_ASSERT like actual vm_cc_cme + cc.cme_ + end + + def def_iseq_ptr(method_def) + C.rb_iseq_check(method_def.body.iseq.iseqptr) + end + + def rb_mjit_iseq_compile_info(body) + body.jit_unit.compile_info + end + + def ISEQ_IS_SIZE(body) + body.ic_size + body.ivc_size + body.ise_size + body.icvarc_size + end + + # Return true if an object of the class may be a special const (immediate). + # It's "maybe" because Integer and Float are not guaranteed to be an immediate. + # If this returns false, rb_class_of could be optimzied to RBASIC_CLASS. + def maybe_special_const?(klass) + [ + C.rb_cFalseClass, + C.rb_cNilClass, + C.rb_cTrueClass, + C.rb_cInteger, + C.rb_cSymbol, + C.rb_cFloat, + ].include?(klass) + end + + def has_valid_method_type?(cc) + vm_cc_cme(cc) != nil + end + + def already_compiled?(status, pos) + status.stack_size_for_pos[pos] != C.NOT_COMPILED_STACK_SIZE + end + + # Return an iseq pointer if cc has inlinable iseq. + def rb_mjit_inlinable_iseq(ci, cc) + if has_valid_method_type?(cc) && + C.vm_ci_flag(ci) & C.VM_CALL_TAILCALL == 0 && # inlining only non-tailcall path + vm_cc_cme(cc).def.type == C.VM_METHOD_TYPE_ISEQ && + C.fastpath_applied_iseq_p(ci, cc, iseq = def_iseq_ptr(vm_cc_cme(cc).def)) && + inlinable_iseq_p(iseq.body) # CC_SET_FASTPATH in vm_callee_setup_arg + return iseq + end + return nil + end + + # Return true if the ISeq can be inlined without pushing a new control frame. + def inlinable_iseq_p(body) + # 1) If catch_except_p, caller frame should be preserved when callee catches an exception. + # Then we need to wrap `vm_exec()` but then we can't inline the call inside it. + # + # 2) If `body->catch_except_p` is false and `handles_sp?` of an insn is false, + # sp is not moved as we assume `status->local_stack_p = !body->catch_except_p`. + # + # 3) If `body->catch_except_p` is false and `always_leaf?` of an insn is true, + # pc is not moved. + if body.catch_except_p + return false + end + + pos = 0 + while pos < body.iseq_size + insn = INSNS.fetch(C.rb_vm_insn_decode(body.iseq_encoded[pos])) + # All insns in the ISeq except `leave` (to be overridden in the inlined code) + # should meet following strong assumptions: + # * Do not require `cfp->sp` motion + # * Do not move `cfp->pc` + # * Do not read any `cfp->pc` + if insn.name == :invokebuiltin || insn.name == :opt_invokebuiltin_delegate || insn.name == :opt_invokebuiltin_delegate_leave + # builtin insn's inlinability is handled by `Primitive.attr! 'inline'` per iseq + if !body.builtin_inline_p + return false; + end + elsif insn.name != :leave && C.insn_may_depend_on_sp_or_pc(insn.bin, body.iseq_encoded + (pos + 1)) + return false + end + # At this moment, `cfp->ep` in an inlined method is not working. + case insn.name + when :getlocal, + :getlocal_WC_0, + :getlocal_WC_1, + :setlocal, + :setlocal_WC_0, + :setlocal_WC_1, + :getblockparam, + :getblockparamproxy, + :setblockparam + return false + end + pos += insn.len + end + return true + end + + # CPointer::Struct could be nil on field reference, and this is a helper to + # handle that case while using CPointer::Struct#to_s in most cases. + # @param struct [RubyVM::MJIT::CPointer::Struct] + def to_addr(struct) + struct&.to_s || 'NULL' + end + end + + private_constant(*constants) +end diff --git a/mjit.c b/mjit.c index 0aa6d6e7cf..9687f759cf 100644 --- a/mjit.c +++ b/mjit.c @@ -1399,7 +1399,7 @@ mjit_target_iseq_p(const rb_iseq_t *iseq) struct rb_iseq_constant_body *body = ISEQ_BODY(iseq); return (body->type == ISEQ_TYPE_METHOD || body->type == ISEQ_TYPE_BLOCK) && !body->builtin_inline_p - && strcmp("", RSTRING_PTR(rb_iseq_path(iseq))); + && strcmp("", RSTRING_PTR(rb_iseq_path(iseq))) != 0; } // If recompile_p is true, the call is initiated by mjit_recompile. @@ -1439,7 +1439,7 @@ rb_mjit_add_iseq_to_process(const rb_iseq_t *iseq) } // For this timeout seconds, --jit-wait will wait for JIT compilation finish. -#define MJIT_WAIT_TIMEOUT_SECONDS 60 +#define MJIT_WAIT_TIMEOUT_SECONDS 600 static void mjit_wait(struct rb_iseq_constant_body *body) @@ -1804,8 +1804,8 @@ const struct ruby_opt_message mjit_option_messages[] = { void mjit_init(const struct mjit_options *opts) { + VM_ASSERT(mjit_enabled); mjit_opts = *opts; - mjit_enabled = true; mjit_call_p = true; mjit_pid = getpid(); @@ -1859,11 +1859,11 @@ mjit_init(const struct mjit_options *opts) // rb_fiber_init_mjit_cont again with mjit_enabled=true to set the root_fiber's mjit_cont. rb_fiber_init_mjit_cont(GET_EC()->fiber_ptr); - // Initialize worker thread - start_worker(); - // TODO: Consider running C compiler asynchronously make_pch(); + + // Enable MJIT compilation + start_worker(); } static void diff --git a/mjit_compiler.c b/mjit_compiler.c index c02397e53c..fccd6cfd2a 100644 --- a/mjit_compiler.c +++ b/mjit_compiler.c @@ -6,14 +6,11 @@ **********************************************************************/ -// NOTE: All functions in this file are executed on MJIT worker. So don't -// call Ruby methods (C functions that may call rb_funcall) or trigger -// GC (using ZALLOC, xmalloc, xfree, etc.) in this file. - #include "ruby/internal/config.h" // defines USE_MJIT #if USE_MJIT +#include "mjit_compiler.h" #include "internal.h" #include "internal/compile.h" #include "internal/hash.h" @@ -22,7 +19,6 @@ #include "mjit.h" #include "mjit_unit.h" #include "yjit.h" -#include "vm_core.h" #include "vm_callinfo.h" #include "vm_exec.h" #include "vm_insnhelper.h" @@ -31,72 +27,20 @@ #include "insns.inc" #include "insns_info.inc" -// Macros to check if a position is already compiled using compile_status.stack_size_for_pos -#define NOT_COMPILED_STACK_SIZE -1 -#define ALREADY_COMPILED_P(status, pos) (status->stack_size_for_pos[pos] != NOT_COMPILED_STACK_SIZE) - -// For propagating information needed for lazily pushing a frame. -struct inlined_call_context { - int orig_argc; // ci->orig_argc - VALUE me; // vm_cc_cme(cc) - int param_size; // def_iseq_ptr(vm_cc_cme(cc)->def)->body->param.size - int local_size; // def_iseq_ptr(vm_cc_cme(cc)->def)->body->local_table_size -}; - -// Storage to keep compiler's status. This should have information -// which is global during one `mjit_compile` call. Ones conditional -// in each branch should be stored in `compile_branch`. -struct compile_status { - bool success; // has true if compilation has had no issue - int *stack_size_for_pos; // stack_size_for_pos[pos] has stack size for the position (otherwise -1) - // If true, JIT-ed code will use local variables to store pushed values instead of - // using VM's stack and moving stack pointer. - bool local_stack_p; - // Safely-accessible ivar cache entries copied from main thread. - union iseq_inline_storage_entry *is_entries; - // Index of call cache entries captured to compiled_iseq to be marked on GC - int cc_entries_index; - // A pointer to root (i.e. not inlined) iseq being compiled. - const struct rb_iseq_constant_body *compiled_iseq; - int compiled_id; // Just a copy of compiled_iseq->jit_unit->id - // Mutated optimization levels - struct rb_mjit_compile_info *compile_info; - bool merge_ivar_guards_p; // If true, merge guards of ivar accesses - rb_serial_t ivar_serial; // ic_serial of IVC in is_entries (used only when merge_ivar_guards_p) - size_t max_ivar_index; // Max IVC index in is_entries (used only when merge_ivar_guards_p) - // If `inlined_iseqs[pos]` is not NULL, `mjit_compile_body` tries to inline ISeq there. - const struct rb_iseq_constant_body **inlined_iseqs; - struct inlined_call_context inline_context; -}; - -// Storage to keep data which is consistent in each conditional branch. -// This is created and used for one `compile_insns` call and its values -// should be copied for extra `compile_insns` call. -struct compile_branch { - unsigned int stack_size; // this simulates sp (stack pointer) of YARV - bool finish_p; // if true, compilation in this branch should stop and let another branch to be compiled -}; - struct case_dispatch_var { FILE *f; unsigned int base_pos; VALUE last_value; }; -static size_t -call_data_index(CALL_DATA cd, const struct rb_iseq_constant_body *body) +static VALUE +rb_ptr(const char *type, const void *ptr) { - return cd - body->call_data; -} - -// Using this function to refer to cc_entries allocated by `mjit_capture_cc_entries` -// instead of storing cc_entries in status directly so that we always refer to a new address -// returned by `realloc` inside it. -static const struct rb_callcache ** -captured_cc_entries(const struct compile_status *status) -{ - VM_ASSERT(status->cc_entries_index != -1); - return status->compiled_iseq->jit_unit->cc_entries + status->cc_entries_index; + // TODO: cache constant + VALUE rb_mMJIT = rb_const_get(rb_cRubyVM, rb_intern("MJIT")); + VALUE rb_mC = rb_const_get(rb_mMJIT, rb_intern("C")); + VALUE rb_type = rb_funcall(rb_mC, rb_intern(type), 0); + return rb_funcall(rb_type, rb_intern("new"), 1, ULONG2NUM((size_t)ptr)); } // Returns true if call cache is still not obsoleted and vm_cc_cme(cc)->def->type is available. @@ -106,15 +50,6 @@ has_valid_method_type(CALL_CACHE cc) return vm_cc_cme(cc) != NULL; } -// Returns true if MJIT thinks this cc's opt_* insn may fallback to opt_send_without_block. -static bool -has_cache_for_send(CALL_CACHE cc, int insn) -{ - extern bool rb_vm_opt_cfunc_p(CALL_CACHE cc, int insn); - return has_valid_method_type(cc) && - !(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_CFUNC && rb_vm_opt_cfunc_p(cc, insn)); -} - // Returns true if iseq can use fastpath for setup, otherwise NULL. This becomes true in the same condition // as CC_SET_FASTPATH (in vm_callee_setup_arg) is called from vm_call_iseq_setup. static bool @@ -127,194 +62,46 @@ fastpath_applied_iseq_p(const CALL_INFO ci, const CALL_CACHE cc, const rb_iseq_t && vm_call_iseq_optimizable_p(ci, cc); // CC_SET_FASTPATH condition } -// Return true if an object of the klass may be a special const. See: rb_class_of -static bool -maybe_special_const_class_p(const VALUE klass) +#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass) + +// TODO: Share this with iseq.c +static inline VALUE +obj_resurrect(VALUE obj) { - return klass == rb_cFalseClass - || klass == rb_cNilClass - || klass == rb_cTrueClass - || klass == rb_cInteger - || klass == rb_cSymbol - || klass == rb_cFloat; + if (hidden_obj_p(obj)) { + switch (BUILTIN_TYPE(obj)) { + case T_STRING: + obj = rb_str_resurrect(obj); + break; + case T_ARRAY: + obj = rb_ary_resurrect(obj); + break; + case T_HASH: + obj = rb_hash_resurrect(obj); + break; + default: + break; + } + } + return obj; } static int -compile_case_dispatch_each(VALUE key, VALUE value, VALUE arg) +cdhash_each(VALUE key, VALUE value, VALUE hash) { - struct case_dispatch_var *var = (struct case_dispatch_var *)arg; - unsigned int offset; - - if (var->last_value != value) { - offset = FIX2INT(value); - var->last_value = value; - fprintf(var->f, " case %d:\n", offset); - fprintf(var->f, " goto label_%d;\n", var->base_pos + offset); - fprintf(var->f, " break;\n"); - } + rb_hash_aset(hash, obj_resurrect(key), value); return ST_CONTINUE; } -// Calling rb_id2str in MJIT worker causes random SEGV. So this is disabled by default. -static void -comment_id(FILE *f, ID id) -{ -#ifdef MJIT_COMMENT_ID - VALUE name = rb_id2str(id); - const char *p, *e; - char c, prev = '\0'; +#include "mjit_compile_attr.inc" - if (!name) return; - p = RSTRING_PTR(name); - e = RSTRING_END(name); - fputs("/* :\"", f); - for (; p < e; ++p) { - switch (c = *p) { - case '*': case '/': if (prev != (c ^ ('/' ^ '*'))) break; - case '\\': case '"': fputc('\\', f); - } - fputc(c, f); - prev = c; - } - fputs("\" */", f); +#if SIZEOF_LONG == SIZEOF_VOIDP +#define NUM2PTR(x) NUM2ULONG(x) +#define PTR2NUM(x) ULONG2NUM(x) +#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP +#define NUM2PTR(x) NUM2ULL(x) +#define PTR2NUM(x) ULL2NUM(x) #endif -} - -static void compile_insns(FILE *f, const struct rb_iseq_constant_body *body, unsigned int stack_size, - unsigned int pos, struct compile_status *status); - -// Main function of JIT compilation, vm_exec_core counterpart for JIT. Compile one insn to `f`, may modify -// b->stack_size and return next position. -// -// When you add a new instruction to insns.def, it would be nice to have JIT compilation support here but -// it's optional. This JIT compiler just ignores ISeq which includes unknown instruction, and ISeq which -// does not have it can be compiled as usual. -static unsigned int -compile_insn(FILE *f, const struct rb_iseq_constant_body *body, const int insn, const VALUE *operands, - const unsigned int pos, struct compile_status *status, struct compile_branch *b) -{ - unsigned int next_pos = pos + insn_len(insn); - -/*****************/ - #include "mjit_compile.inc" -/*****************/ - - // If next_pos is already compiled and this branch is not finished yet, - // next instruction won't be compiled in C code next and will need `goto`. - if (!b->finish_p && next_pos < body->iseq_size && ALREADY_COMPILED_P(status, next_pos)) { - fprintf(f, "goto label_%d;\n", next_pos); - - // Verify stack size assumption is the same among multiple branches - if ((unsigned int)status->stack_size_for_pos[next_pos] != b->stack_size) { - if (mjit_opts.warnings || mjit_opts.verbose) - fprintf(stderr, "MJIT warning: JIT stack assumption is not the same between branches (%d != %u)\n", - status->stack_size_for_pos[next_pos], b->stack_size); - status->success = false; - } - } - - return next_pos; -} - -// Compile one conditional branch. If it has branchXXX insn, this should be -// called multiple times for each branch. -static void -compile_insns(FILE *f, const struct rb_iseq_constant_body *body, unsigned int stack_size, - unsigned int pos, struct compile_status *status) -{ - struct compile_branch branch; - - branch.stack_size = stack_size; - branch.finish_p = false; - - while (pos < body->iseq_size && !ALREADY_COMPILED_P(status, pos) && !branch.finish_p) { - int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); - status->stack_size_for_pos[pos] = (int)branch.stack_size; - - fprintf(f, "\nlabel_%d: /* %s */\n", pos, insn_name(insn)); - pos = compile_insn(f, body, insn, body->iseq_encoded + (pos+1), pos, status, &branch); - if (status->success && branch.stack_size > body->stack_max) { - if (mjit_opts.warnings || mjit_opts.verbose) - fprintf(stderr, "MJIT warning: JIT stack size (%d) exceeded its max size (%d)\n", branch.stack_size, body->stack_max); - status->success = false; - } - if (!status->success) - break; - } -} - -// Print the block to cancel inlined method call. It's supporting only `opt_send_without_block` for now. -static void -compile_inlined_cancel_handler(FILE *f, const struct rb_iseq_constant_body *body, struct inlined_call_context *inline_context) -{ - fprintf(f, "\ncancel:\n"); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel);\n"); - fprintf(f, " rb_mjit_recompile_inlining(original_iseq);\n"); - - // Swap pc/sp set on cancel with original pc/sp. - fprintf(f, " const VALUE *current_pc = reg_cfp->pc;\n"); - fprintf(f, " VALUE *current_sp = reg_cfp->sp;\n"); - fprintf(f, " reg_cfp->pc = orig_pc;\n"); - fprintf(f, " reg_cfp->sp = orig_sp;\n\n"); - - // Lazily push the current call frame. - fprintf(f, " struct rb_calling_info calling;\n"); - fprintf(f, " calling.block_handler = VM_BLOCK_HANDLER_NONE;\n"); // assumes `opt_send_without_block` - fprintf(f, " calling.argc = %d;\n", inline_context->orig_argc); - fprintf(f, " calling.recv = reg_cfp->self;\n"); - fprintf(f, " reg_cfp->self = orig_self;\n"); - fprintf(f, " vm_call_iseq_setup_normal(ec, reg_cfp, &calling, (const rb_callable_method_entry_t *)0x%"PRIxVALUE", 0, %d, %d);\n\n", - inline_context->me, inline_context->param_size, inline_context->local_size); // fastpath_applied_iseq_p checks rb_simple_iseq_p, which ensures has_opt == FALSE - - // Start usual cancel from here. - fprintf(f, " reg_cfp = ec->cfp;\n"); // work on the new frame - fprintf(f, " reg_cfp->pc = current_pc;\n"); - fprintf(f, " reg_cfp->sp = current_sp;\n"); - for (unsigned int i = 0; i < body->stack_max; i++) { // should be always `status->local_stack_p` - fprintf(f, " *(vm_base_ptr(reg_cfp) + %d) = stack[%d];\n", i, i); - } - // We're not just returning Qundef here so that caller's normal cancel handler can - // push back `stack` to `cfp->sp`. - fprintf(f, " return vm_exec(ec, false);\n"); -} - -// Print the block to cancel JIT execution. -static void -compile_cancel_handler(FILE *f, const struct rb_iseq_constant_body *body, struct compile_status *status) -{ - if (status->inlined_iseqs == NULL) { // the current ISeq is being inlined - compile_inlined_cancel_handler(f, body, &status->inline_context); - return; - } - - fprintf(f, "\nsend_cancel:\n"); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_send_inline);\n"); - fprintf(f, " rb_mjit_recompile_send(original_iseq);\n"); - fprintf(f, " goto cancel;\n"); - - fprintf(f, "\nivar_cancel:\n"); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_ivar_inline);\n"); - fprintf(f, " rb_mjit_recompile_ivar(original_iseq);\n"); - fprintf(f, " goto cancel;\n"); - - fprintf(f, "\nexivar_cancel:\n"); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_exivar_inline);\n"); - fprintf(f, " rb_mjit_recompile_exivar(original_iseq);\n"); - fprintf(f, " goto cancel;\n"); - - fprintf(f, "\nconst_cancel:\n"); - fprintf(f, " rb_mjit_recompile_const(original_iseq);\n"); - fprintf(f, " goto cancel;\n"); - - fprintf(f, "\ncancel:\n"); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel);\n"); - if (status->local_stack_p) { - for (unsigned int i = 0; i < body->stack_max; i++) { - fprintf(f, " *(vm_base_ptr(reg_cfp) + %d) = stack[%d];\n", i, i); - } - } - fprintf(f, " return Qundef;\n"); -} extern int mjit_capture_cc_entries(const struct rb_iseq_constant_body *compiled_iseq, const struct rb_iseq_constant_body *captured_iseq); @@ -330,267 +117,58 @@ mjit_capture_is_entries(const struct rb_iseq_constant_body *body, union iseq_inl memcpy(is_entries, body->is_entries, sizeof(union iseq_inline_storage_entry) * ISEQ_IS_SIZE(body)); } -static bool -mjit_compile_body(FILE *f, const rb_iseq_t *iseq, struct compile_status *status) -{ - const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq); - status->success = true; - status->local_stack_p = !body->catch_except_p; - - if (status->local_stack_p) { - fprintf(f, " VALUE stack[%d];\n", body->stack_max); - } - else { - fprintf(f, " VALUE *stack = reg_cfp->sp;\n"); - } - if (status->inlined_iseqs != NULL) // i.e. compile root - fprintf(f, " static const rb_iseq_t *original_iseq = (const rb_iseq_t *)0x%"PRIxVALUE";\n", (VALUE)iseq); - fprintf(f, " static const VALUE *const original_body_iseq = (VALUE *)0x%"PRIxVALUE";\n", - (VALUE)body->iseq_encoded); - fprintf(f, " VALUE cfp_self = reg_cfp->self;\n"); // cache self across the method - fprintf(f, "#undef GET_SELF\n"); - fprintf(f, "#define GET_SELF() cfp_self\n"); - - // Generate merged ivar guards first if needed - if (!status->compile_info->disable_ivar_cache && status->merge_ivar_guards_p) { - fprintf(f, " if (UNLIKELY(!(RB_TYPE_P(GET_SELF(), T_OBJECT) && (rb_serial_t)%"PRI_SERIALT_PREFIX"u == RCLASS_SERIAL(RBASIC(GET_SELF())->klass) &&", status->ivar_serial); -#if USE_RVARGC - fprintf(f, "%"PRIuSIZE" < ROBJECT_NUMIV(GET_SELF())", status->max_ivar_index); // index < ROBJECT_NUMIV(obj) -#else - if (status->max_ivar_index >= ROBJECT_EMBED_LEN_MAX) { - fprintf(f, "%"PRIuSIZE" < ROBJECT_NUMIV(GET_SELF())", status->max_ivar_index); // index < ROBJECT_NUMIV(obj) && !RB_FL_ANY_RAW(obj, ROBJECT_EMBED) - } - else { - fprintf(f, "ROBJECT_EMBED_LEN_MAX == ROBJECT_NUMIV(GET_SELF())"); // index < ROBJECT_NUMIV(obj) && RB_FL_ANY_RAW(obj, ROBJECT_EMBED) - } -#endif - fprintf(f, "))) {\n"); - fprintf(f, " goto ivar_cancel;\n"); - fprintf(f, " }\n"); - } - - // Simulate `opt_pc` in setup_parameters_complex. Other PCs which may be passed by catch tables - // are not considered since vm_exec doesn't call jit_exec for catch tables. - if (body->param.flags.has_opt) { - int i; - fprintf(f, "\n"); - fprintf(f, " switch (reg_cfp->pc - ISEQ_BODY(reg_cfp->iseq)->iseq_encoded) {\n"); - for (i = 0; i <= body->param.opt_num; i++) { - VALUE pc_offset = body->param.opt_table[i]; - fprintf(f, " case %"PRIdVALUE":\n", pc_offset); - fprintf(f, " goto label_%"PRIdVALUE";\n", pc_offset); - } - fprintf(f, " }\n"); - } - - compile_insns(f, body, 0, 0, status); - compile_cancel_handler(f, body, status); - fprintf(f, "#undef GET_SELF"); - return status->success; -} - -// Return true if the ISeq can be inlined without pushing a new control frame. -static bool -inlinable_iseq_p(const struct rb_iseq_constant_body *body) -{ - // 1) If catch_except_p, caller frame should be preserved when callee catches an exception. - // Then we need to wrap `vm_exec()` but then we can't inline the call inside it. - // - // 2) If `body->catch_except_p` is false and `handles_sp?` of an insn is false, - // sp is not moved as we assume `status->local_stack_p = !body->catch_except_p`. - // - // 3) If `body->catch_except_p` is false and `always_leaf?` of an insn is true, - // pc is not moved. - if (body->catch_except_p) - return false; - - unsigned int pos = 0; - while (pos < body->iseq_size) { - int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); - // All insns in the ISeq except `leave` (to be overridden in the inlined code) - // should meet following strong assumptions: - // * Do not require `cfp->sp` motion - // * Do not move `cfp->pc` - // * Do not read any `cfp->pc` - if (insn == BIN(invokebuiltin) || insn == BIN(opt_invokebuiltin_delegate) || insn == BIN(opt_invokebuiltin_delegate_leave)) { - // builtin insn's inlinability is handled by `Primitive.attr! 'inline'` per iseq - if (!body->builtin_inline_p) - return false; - } - else if (insn != BIN(leave) && insn_may_depend_on_sp_or_pc(insn, body->iseq_encoded + (pos + 1))) - return false; - // At this moment, `cfp->ep` in an inlined method is not working. - switch (insn) { - case BIN(getlocal): - case BIN(getlocal_WC_0): - case BIN(getlocal_WC_1): - case BIN(setlocal): - case BIN(setlocal_WC_0): - case BIN(setlocal_WC_1): - case BIN(getblockparam): - case BIN(getblockparamproxy): - case BIN(setblockparam): - return false; - } - pos += insn_len(insn); - } - return true; -} - -// Return an iseq pointer if cc has inlinable iseq. -const rb_iseq_t * -rb_mjit_inlinable_iseq(const struct rb_callinfo *ci, const struct rb_callcache *cc) -{ - const rb_iseq_t *iseq; - if (has_valid_method_type(cc) && - !(vm_ci_flag(ci) & VM_CALL_TAILCALL) && // inlining only non-tailcall path - vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_ISEQ && - fastpath_applied_iseq_p(ci, cc, iseq = def_iseq_ptr(vm_cc_cme(cc)->def)) && - // CC_SET_FASTPATH in vm_callee_setup_arg - inlinable_iseq_p(ISEQ_BODY(iseq))) { - return iseq; - } - return NULL; -} - -static void -init_ivar_compile_status(const struct rb_iseq_constant_body *body, struct compile_status *status) -{ - mjit_capture_is_entries(body, status->is_entries); - - int num_ivars = 0; - unsigned int pos = 0; - status->max_ivar_index = 0; - status->ivar_serial = 0; - - while (pos < body->iseq_size) { - int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); - if (insn == BIN(getinstancevariable) || insn == BIN(setinstancevariable)) { - IVC ic = (IVC)body->iseq_encoded[pos+2]; - IVC ic_copy = &(status->is_entries + ((union iseq_inline_storage_entry *)ic - body->is_entries))->iv_cache; - if (ic_copy->entry) { // Only initialized (ic_serial > 0) IVCs are optimized - num_ivars++; - - if (status->max_ivar_index < ic_copy->entry->index) { - status->max_ivar_index = ic_copy->entry->index; - } - - if (status->ivar_serial == 0) { - status->ivar_serial = ic_copy->entry->class_serial; - } - else if (status->ivar_serial != ic_copy->entry->class_serial) { - // Multiple classes have used this ISeq. Give up assuming one serial. - status->merge_ivar_guards_p = false; - return; - } - } - } - pos += insn_len(insn); - } - status->merge_ivar_guards_p = status->ivar_serial > 0 && num_ivars >= 2; -} - -// This needs to be macro instead of a function because it's using `alloca`. -#define INIT_COMPILE_STATUS(status, body, compile_root_p) do { \ - status = (struct compile_status){ \ - .stack_size_for_pos = (int *)alloca(sizeof(int) * body->iseq_size), \ - .inlined_iseqs = compile_root_p ? \ - alloca(sizeof(const struct rb_iseq_constant_body *) * body->iseq_size) : NULL, \ - .is_entries = (ISEQ_IS_SIZE(body) > 0) ? \ - alloca(sizeof(union iseq_inline_storage_entry) * ISEQ_IS_SIZE(body)) : NULL, \ - .cc_entries_index = (body->ci_size > 0) ? \ - mjit_capture_cc_entries(status.compiled_iseq, body) : -1, \ - .compiled_id = status.compiled_id, \ - .compiled_iseq = status.compiled_iseq, \ - .compile_info = compile_root_p ? \ - rb_mjit_iseq_compile_info(body) : alloca(sizeof(struct rb_mjit_compile_info)) \ - }; \ - memset(status.stack_size_for_pos, NOT_COMPILED_STACK_SIZE, sizeof(int) * body->iseq_size); \ - if (compile_root_p) \ - memset((void *)status.inlined_iseqs, 0, sizeof(const struct rb_iseq_constant_body *) * body->iseq_size); \ - else \ - memset(status.compile_info, 0, sizeof(struct rb_mjit_compile_info)); \ -} while (0) - -static bool -precompile_inlinable_child_iseq(FILE *f, const rb_iseq_t *child_iseq, struct compile_status *status, - const struct rb_callinfo *ci, const struct rb_callcache *cc, unsigned int pos) -{ - struct compile_status child_status = { .compiled_iseq = status->compiled_iseq, .compiled_id = status->compiled_id }; - INIT_COMPILE_STATUS(child_status, ISEQ_BODY(child_iseq), false); - child_status.inline_context = (struct inlined_call_context){ - .orig_argc = vm_ci_argc(ci), - .me = (VALUE)vm_cc_cme(cc), - .param_size = ISEQ_BODY(child_iseq)->param.size, - .local_size = ISEQ_BODY(child_iseq)->local_table_size - }; - if (ISEQ_BODY(child_iseq)->ci_size > 0 && child_status.cc_entries_index == -1) { - return false; - } - init_ivar_compile_status(ISEQ_BODY(child_iseq), &child_status); - - fprintf(f, "ALWAYS_INLINE(static VALUE _mjit%d_inlined_%d(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE orig_self, const rb_iseq_t *original_iseq));\n", status->compiled_id, pos); - fprintf(f, "static inline VALUE\n_mjit%d_inlined_%d(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE orig_self, const rb_iseq_t *original_iseq)\n{\n", status->compiled_id, pos); - fprintf(f, " const VALUE *orig_pc = reg_cfp->pc;\n"); - fprintf(f, " VALUE *orig_sp = reg_cfp->sp;\n"); - bool success = mjit_compile_body(f, child_iseq, &child_status); - fprintf(f, "\n} /* end of _mjit%d_inlined_%d */\n\n", status->compiled_id, pos); - - return success; -} - -// Compile inlinable ISeqs to C code in `f`. It returns true if it succeeds to compile them. -static bool -precompile_inlinable_iseqs(FILE *f, const rb_iseq_t *iseq, struct compile_status *status) -{ - const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq); - unsigned int pos = 0; - while (pos < body->iseq_size) { - int insn = rb_vm_insn_decode(body->iseq_encoded[pos]); - if (insn == BIN(opt_send_without_block) || insn == BIN(opt_size)) { // `compile_inlined_cancel_handler` supports only `opt_send_without_block` - CALL_DATA cd = (CALL_DATA)body->iseq_encoded[pos + 1]; - const struct rb_callinfo *ci = cd->ci; - const struct rb_callcache *cc = captured_cc_entries(status)[call_data_index(cd, body)]; // use copy to avoid race condition - - const rb_iseq_t *child_iseq; - if ((child_iseq = rb_mjit_inlinable_iseq(ci, cc)) != NULL) { - status->inlined_iseqs[pos] = ISEQ_BODY(child_iseq); - - if (mjit_opts.verbose >= 1) // print beforehand because ISeq may be GCed during copy job. - fprintf(stderr, "JIT inline: %s@%s:%d => %s@%s:%d\n", - RSTRING_PTR(ISEQ_BODY(child_iseq)->location.label), - RSTRING_PTR(rb_iseq_path(child_iseq)), FIX2INT(ISEQ_BODY(child_iseq)->location.first_lineno), - RSTRING_PTR(ISEQ_BODY(iseq)->location.label), - RSTRING_PTR(rb_iseq_path(iseq)), FIX2INT(ISEQ_BODY(iseq)->location.first_lineno)); - if (!precompile_inlinable_child_iseq(f, child_iseq, status, ci, cc, pos)) - return false; - } - } - pos += insn_len(insn); - } - return true; -} - // Compile ISeq to C code in `f`. It returns true if it succeeds to compile. bool mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id) { - struct compile_status status = { .compiled_iseq = ISEQ_BODY(iseq), .compiled_id = id }; - INIT_COMPILE_STATUS(status, ISEQ_BODY(iseq), true); - if (ISEQ_BODY(iseq)->ci_size > 0 && status.cc_entries_index == -1) { - return false; - } - init_ivar_compile_status(ISEQ_BODY(iseq), &status); + bool original_call_p = mjit_call_p; + mjit_call_p = false; // Avoid impacting JIT metrics by itself - if (!status.compile_info->disable_send_cache && !status.compile_info->disable_inlining) { - if (!precompile_inlinable_iseqs(f, iseq, &status)) - return false; - } + // TODO: initialize the constant in mjit_init and use it + VALUE rb_mMJIT = rb_const_get(rb_cRubyVM, rb_intern("MJIT")); + VALUE rb_mCompiler = rb_const_get(rb_mMJIT, rb_intern("Compiler")); + bool success = RTEST(rb_funcall(rb_mCompiler, rb_intern("compile"), 4, + PTR2NUM((VALUE)f), rb_ptr("rb_iseq_t", iseq), rb_str_new_cstr(funcname), INT2NUM(id))); - fprintf(f, "VALUE\n%s(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp)\n{\n", funcname); - bool success = mjit_compile_body(f, iseq, &status); - fprintf(f, "\n} // end of %s\n", funcname); + mjit_call_p = original_call_p; return success; } +// +// Primitive.methods +// + +static VALUE +cdhash_to_hash(rb_execution_context_t *ec, VALUE self, VALUE cdhash_addr) +{ + VALUE hash = rb_hash_new(); + rb_hash_foreach((VALUE)NUM2PTR(cdhash_addr), cdhash_each, hash); + return hash; +} + +static VALUE +builtin_compile(rb_execution_context_t *ec, VALUE self, VALUE f_addr, VALUE bf_addr, VALUE index, VALUE stack_size, VALUE builtin_inline_p) +{ + FILE *f = (FILE *)NUM2PTR(f_addr); + RB_BUILTIN bf = (RB_BUILTIN)NUM2PTR(bf_addr); + bf->compiler(f, NIL_P(index) ? -1 : NUM2LONG(index), NUM2UINT(stack_size), RTEST(builtin_inline_p)); + return Qnil; +} + +// Returns true if MJIT thinks this cc's opt_* insn may fallback to opt_send_without_block. +static VALUE +has_cache_for_send(rb_execution_context_t *ec, VALUE self, VALUE cc_addr, VALUE insn) +{ + extern bool rb_vm_opt_cfunc_p(CALL_CACHE cc, int insn); + CALL_CACHE cc = (CALL_CACHE)NUM2PTR(cc_addr); + bool has_cache = has_valid_method_type(cc) && + !(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_CFUNC && rb_vm_opt_cfunc_p(cc, NUM2INT(insn))); + return RBOOL(has_cache); +} + +extern bool rb_splat_or_kwargs_p(const struct rb_callinfo *restrict ci); + +#include "mjit_compiler.rbinc" +#include "mjit_instruction.rbinc" + #endif // USE_MJIT diff --git a/mjit_compiler.h b/mjit_compiler.h new file mode 100644 index 0000000000..da79054420 --- /dev/null +++ b/mjit_compiler.h @@ -0,0 +1,58 @@ +// This file is parsed by tool/mjit/generate.rb for MJIT's C/Ruby interop. +#ifndef MJIT_COMPILER_H +#define MJIT_COMPILER_H + +#include "ruby/internal/config.h" +#include "vm_core.h" +#include "vm_callinfo.h" +#include "builtin.h" +#include "mjit.h" +#include "mjit_unit.h" + +// Macros to check if a position is already compiled using compile_status.stack_size_for_pos +#define NOT_COMPILED_STACK_SIZE -1 +#define ALREADY_COMPILED_P(status, pos) (status->stack_size_for_pos[pos] != NOT_COMPILED_STACK_SIZE) + +// Storage to keep data which is consistent in each conditional branch. +// This is created and used for one `compile_insns` call and its values +// should be copied for extra `compile_insns` call. +struct compile_branch { + unsigned int stack_size; // this simulates sp (stack pointer) of YARV + bool finish_p; // if true, compilation in this branch should stop and let another branch to be compiled +}; + +// For propagating information needed for lazily pushing a frame. +struct inlined_call_context { + int orig_argc; // ci->orig_argc + VALUE me; // vm_cc_cme(cc) + int param_size; // def_iseq_ptr(vm_cc_cme(cc)->def)->body->param.size + int local_size; // def_iseq_ptr(vm_cc_cme(cc)->def)->body->local_table_size +}; + +// Storage to keep compiler's status. This should have information +// which is global during one `mjit_compile` call. Ones conditional +// in each branch should be stored in `compile_branch`. +struct compile_status { + bool success; // has true if compilation has had no issue + int *stack_size_for_pos; // stack_size_for_pos[pos] has stack size for the position (otherwise -1) + // If true, JIT-ed code will use local variables to store pushed values instead of + // using VM's stack and moving stack pointer. + bool local_stack_p; + // Safely-accessible ivar cache entries copied from main thread. + union iseq_inline_storage_entry *is_entries; + // Index of call cache entries captured to compiled_iseq to be marked on GC + int cc_entries_index; + // A pointer to root (i.e. not inlined) iseq being compiled. + const struct rb_iseq_constant_body *compiled_iseq; + int compiled_id; // Just a copy of compiled_iseq->jit_unit->id + // Mutated optimization levels + struct rb_mjit_compile_info *compile_info; + bool merge_ivar_guards_p; // If true, merge guards of ivar accesses + rb_serial_t ivar_serial; // ic_serial of IVC in is_entries (used only when merge_ivar_guards_p) + size_t max_ivar_index; // Max IVC index in is_entries (used only when merge_ivar_guards_p) + // If `inlined_iseqs[pos]` is not NULL, `mjit_compile_body` tries to inline ISeq there. + const struct rb_iseq_constant_body **inlined_iseqs; + struct inlined_call_context inline_context; +}; + +#endif /* MJIT_COMPILER_H */ diff --git a/mjit_compiler.rb b/mjit_compiler.rb new file mode 100644 index 0000000000..565649acbe --- /dev/null +++ b/mjit_compiler.rb @@ -0,0 +1,135 @@ +# frozen_string_literal: true +# TODO: Merge this to mjit.rb +if RubyVM::MJIT.enabled? + begin + require 'etc' + rescue LoadError + return # skip miniruby + end + + case RUBY_PLATFORM.split('-', 2).first + when 'x86_64', 'aarch64', 'arm64' + require 'mjit/c_64' + else + require 'mjit/c_32' + end + + class << RubyVM::MJIT::C + def ROBJECT_EMBED_LEN_MAX + Primitive.cexpr! 'INT2NUM(RBIMPL_EMBED_LEN_MAX_OF(VALUE))' + end + + def cdhash_to_hash(cdhash_addr) + Primitive.cdhash_to_hash(cdhash_addr) + end + + def builtin_compiler(f, bf, index, stack_size, builtin_inline_p) + bf_addr = bf.to_i + Primitive.builtin_compile(f, bf_addr, index, stack_size, builtin_inline_p) + end + + def has_cache_for_send(cc, insn) + cc_addr = cc.to_i + Primitive.has_cache_for_send(cc_addr, insn) + 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)))' + rb_iseq_t.new(iseq_addr) + end + + def rb_iseq_path(iseq) + iseq_addr = iseq.to_i + Primitive.cexpr! 'rb_iseq_path((rb_iseq_t *)NUM2PTR(iseq_addr))' + end + + def vm_ci_argc(ci) + ci_addr = ci.to_i + Primitive.cexpr! 'UINT2NUM(vm_ci_argc((CALL_INFO)NUM2PTR(ci_addr)))' + end + + def vm_ci_flag(ci) + ci_addr = ci.to_i + Primitive.cexpr! 'UINT2NUM(vm_ci_flag((CALL_INFO)NUM2PTR(ci_addr)))' + end + + def rb_splat_or_kwargs_p(ci) + ci_addr = ci.to_i + Primitive.cexpr! 'RBOOL(rb_splat_or_kwargs_p((CALL_INFO)NUM2PTR(ci_addr)))' + end + + def fastpath_applied_iseq_p(ci, cc, iseq) + ci_addr = ci.to_i + cc_addr = cc.to_i + iseq_addr = iseq.to_i + Primitive.cexpr! 'RBOOL(fastpath_applied_iseq_p((CALL_INFO)NUM2PTR(ci_addr), (CALL_CACHE)NUM2PTR(cc_addr), (rb_iseq_t *)NUM2PTR(iseq_addr)))' + end + + def mjit_opts + addr = Primitive.cexpr! 'PTR2NUM((VALUE)&mjit_opts)' + mjit_options.new(addr) + end + + def mjit_call_attribute_sp_inc(insn, operands) + operands_addr = operands.to_i + Primitive.cexpr! 'LONG2NUM(mjit_call_attribute_sp_inc(NUM2INT(insn), (VALUE *)NUM2PTR(operands_addr)))' + end + + def mjit_capture_cc_entries(compiled_body, captured_body) + compiled_body_addr = compiled_body.to_i + captured_body_addr = captured_body.to_i + Primitive.cexpr! 'INT2NUM(mjit_capture_cc_entries((struct rb_iseq_constant_body *)NUM2PTR(compiled_body_addr), (struct rb_iseq_constant_body *)NUM2PTR(captured_body_addr)))' + end + + #const struct rb_iseq_constant_body *body, union iseq_inline_storage_entry *is_entries + def mjit_capture_is_entries(body, is_entries) + body_addr = body.to_i + is_entries_addr = is_entries.to_i + Primitive.cstmt! %{ + mjit_capture_is_entries((struct rb_iseq_constant_body *)NUM2PTR(body_addr), (union iseq_inline_storage_entry *)NUM2PTR(is_entries_addr)); + return Qnil; + } + end + + def rb_vm_insn_decode(encoded) + Primitive.cexpr! 'INT2NUM(rb_vm_insn_decode(NUM2PTR(encoded)))' + end + + def insn_may_depend_on_sp_or_pc(insn, opes) + opes_addr = opes.to_i + Primitive.cexpr! 'RBOOL(insn_may_depend_on_sp_or_pc(NUM2INT(insn), (VALUE *)NUM2PTR(opes_addr)))' + end + + # Convert Integer VALUE to an actual Ruby object + def to_ruby(value) + Primitive.cexpr! '(VALUE)NUM2PTR(value)' + end + + def debug(status) + cc_entries_addr = status.compiled_iseq.jit_unit.cc_entries.instance_variable_get(:@addr) + Primitive.cstmt! %{ + const struct rb_callcache **cc_entries = (const struct rb_callcache **)NUM2PTR(cc_entries_addr); + fprintf(stderr, "debug: %p\n", cc_entries[0]); + return Qnil; + } + end + + # TODO: remove this after migration + def fprintf(f, str) + Primitive.cstmt! %{ + fprintf((FILE *)NUM2PTR(f), "%s", RSTRING_PTR(str)); + return Qnil; + } + end + + def rb_cFalseClass; Primitive.cexpr! 'PTR2NUM(rb_cFalseClass)' end + def rb_cNilClass; Primitive.cexpr! 'PTR2NUM(rb_cNilClass)' end + def rb_cTrueClass; Primitive.cexpr! 'PTR2NUM(rb_cTrueClass)' end + def rb_cInteger; Primitive.cexpr! 'PTR2NUM(rb_cInteger)' end + def rb_cSymbol; Primitive.cexpr! 'PTR2NUM(rb_cSymbol)' end + def rb_cFloat; Primitive.cexpr! 'PTR2NUM(rb_cFloat)' end + end + + require "mjit/compiler" +end diff --git a/ruby.c b/ruby.c index 4f33267d73..cefa774639 100644 --- a/ruby.c +++ b/ruby.c @@ -1563,11 +1563,23 @@ ruby_opt_init(ruby_cmdline_options_t *opt) rb_warning_category_update(opt->warn.mask, opt->warn.set); +#if USE_MJIT + // rb_call_builtin_inits depends on RubyVM::MJIT.enabled? + if (opt->mjit.on) + mjit_enabled = true; +#endif + Init_ext(); /* load statically linked extensions before rubygems */ Init_extra_exts(); rb_call_builtin_inits(); ruby_init_prelude(); +#if USE_MJIT + // mjit_init is safe only after rb_call_builtin_inits defines RubyVM::MJIT::Compiler + if (opt->mjit.on) + mjit_init(&opt->mjit); +#endif + ruby_set_script_name(opt->script_name); require_libraries(&opt->req_list); } @@ -1915,12 +1927,6 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) ruby_gc_set_params(); ruby_init_loadpath(); -#if USE_MJIT - if (opt->mjit.on) - /* Using TMP_RUBY_PREFIX created by ruby_init_loadpath(). */ - mjit_init(&opt->mjit); -#endif - Init_enc(); lenc = rb_locale_encoding(); rb_enc_associate(rb_progname, lenc); diff --git a/template/Makefile.in b/template/Makefile.in index 942f8fcb80..1e2a09139e 100644 --- a/template/Makefile.in +++ b/template/Makefile.in @@ -617,7 +617,7 @@ update-known-errors: $(IFCHANGE) $(srcdir)/defs/known_errors.def - INSNS = opt_sc.inc optinsn.inc optunifs.inc insns.inc insns_info.inc \ - vmtc.inc vm.inc mjit_compile.inc + vmtc.inc vm.inc mjit_compile_attr.inc $(INSNS): $(srcdir)/insns.def vm_opts.h \ $(srcdir)/defs/opt_operand.def $(srcdir)/defs/opt_insn_unif.def \ @@ -651,17 +651,12 @@ $(INSNS): $(srcdir)/insns.def vm_opts.h \ $(tooldir)/ruby_vm/views/_insn_sp_pc_dependency.erb \ $(tooldir)/ruby_vm/views/_insn_type_chars.erb \ $(tooldir)/ruby_vm/views/_leaf_helpers.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_insn.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_insn_body.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_ivar.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_pc_and_sp.erb \ - $(tooldir)/ruby_vm/views/_mjit_compile_send.erb \ $(tooldir)/ruby_vm/views/_notice.erb \ $(tooldir)/ruby_vm/views/_sp_inc_helpers.erb \ $(tooldir)/ruby_vm/views/_trace_instruction.erb \ $(tooldir)/ruby_vm/views/insns.inc.erb \ $(tooldir)/ruby_vm/views/insns_info.inc.erb \ - $(tooldir)/ruby_vm/views/mjit_compile.inc.erb \ + $(tooldir)/ruby_vm/views/mjit_compile_attr.inc.erb \ $(tooldir)/ruby_vm/views/opt_sc.inc.erb \ $(tooldir)/ruby_vm/views/optinsn.inc.erb \ $(tooldir)/ruby_vm/views/optunifs.inc.erb \ @@ -670,6 +665,10 @@ $(INSNS): $(srcdir)/insns.def vm_opts.h \ $(ECHO) generating $@ $(Q) $(BASERUBY) -Ku $(tooldir)/insns2vm.rb $(INSNS2VMOPT) $@ +$(srcdir)/mjit_instruction.rb: $(tooldir)/ruby_vm/views/mjit_instruction.rb.erb + $(ECHO) generating $@ + $(Q) $(BASERUBY) -Ku $(tooldir)/insns2vm.rb $(INSNS2VMOPT) $@ + loadpath: verconf.h @$(CPP) $(XCFLAGS) $(CPPFLAGS) $(srcdir)/loadpath.c | \ sed -e '1,/^const char ruby_initial_load_paths/d;/;/,$$d' \ diff --git a/tool/mjit/.gitignore b/tool/mjit/.gitignore new file mode 100644 index 0000000000..66f8ed35a4 --- /dev/null +++ b/tool/mjit/.gitignore @@ -0,0 +1 @@ +/Gemfile.lock diff --git a/tool/mjit/Gemfile b/tool/mjit/Gemfile new file mode 100644 index 0000000000..6a3d0aec81 --- /dev/null +++ b/tool/mjit/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'ffi-clang', git: 'https://github.com/ioquatix/ffi-clang' +gem 'pry-byebug' diff --git a/tool/mjit/bindgen.rb b/tool/mjit/bindgen.rb new file mode 100755 index 0000000000..d2a63581e9 --- /dev/null +++ b/tool/mjit/bindgen.rb @@ -0,0 +1,391 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true +require 'etc' +require 'fiddle/import' +require 'set' + +arch_bits = Integer(ARGV.first || 64) + +# Help ffi-clang find libclang +if arch_bits == 64 + # apt install libclang1 + ENV['LIBCLANG'] ||= Dir.glob("/lib/#{RUBY_PLATFORM}-gnu/libclang-*.so*").grep_v(/-cpp/).sort.last +else + # apt install libclang1:i386 + ENV['LIBCLANG'] ||= Dir.glob("/lib/i386-linux-gnu/libclang-*.so*").sort.last +end +require 'ffi/clang' + +class Node < Struct.new( + :kind, + :spelling, + :type, + :typedef_type, + :bitwidth, + :sizeof_type, + :offsetof, + :tokens, + :enum_value, + :children, + keyword_init: true, +) +end + +# Parse a C header with ffi-clang and return Node objects. +# To ease the maintenance, ffi-clang should be used only inside this class. +class HeaderParser + def initialize(header, cflags:) + @translation_unit = FFI::Clang::Index.new.parse_translation_unit( + header, cflags, [], { detailed_preprocessing_record: true } + ) + end + + def parse + parse_children(@translation_unit.cursor) + end + + private + + def parse_children(cursor) + children = [] + cursor.visit_children do |cursor, _parent| + child = parse_cursor(cursor) + if child.kind != :macro_expansion + children << child + end + next :continue + end + children + end + + def parse_cursor(cursor) + unless cursor.kind.start_with?('cursor_') + raise "unexpected cursor kind: #{cursor.kind}" + end + kind = cursor.kind.to_s.delete_prefix('cursor_').to_sym + children = parse_children(cursor) + + offsetof = {} + if kind == :struct + children.select { |c| c.kind == :field_decl }.each do |child| + offsetof[child.spelling] = cursor.type.offsetof(child.spelling) + end + end + + sizeof_type = nil + if %i[struct union].include?(kind) + sizeof_type = cursor.type.sizeof + end + + tokens = nil + if kind == :macro_definition + tokens = @translation_unit.tokenize(cursor.extent).map(&:spelling) + end + + enum_value = nil + if kind == :enum_constant_decl + enum_value = cursor.enum_value + end + + Node.new( + kind: kind, + spelling: cursor.spelling, + type: cursor.type.spelling, + typedef_type: cursor.typedef_type.spelling, + bitwidth: cursor.bitwidth, + sizeof_type: sizeof_type, + offsetof: offsetof, + tokens: tokens, + enum_value: enum_value, + children: children, + ) + end +end + +# Convert Node objects to a Ruby binding source. +class BindingGenerator + DEFAULTS = { '_Bool' => 'CType::Bool.new' } + DEFAULTS.default_proc = proc { |_h, k| "CType::Stub.new(:#{k})" } + + attr_reader :src + + # @param macros [Array] Imported macros + # @param enums [Hash{ Symbol => Array }] Imported enum values + # @param types [Array] Imported types + # @param ruby_fields [Hash{ Symbol => Array }] Struct VALUE fields that are considered Ruby objects + def initialize(macros:, enums:, types:, ruby_fields:) + @src = String.new + @macros = macros.sort + @enums = enums.transform_keys(&:to_s).transform_values(&:sort).sort.to_h + @types = types.sort + @ruby_fields = ruby_fields.transform_keys(&:to_s) + @references = Set.new + end + + def generate(nodes) + # TODO: Support nested declarations + nodes_index = nodes.group_by(&:spelling).transform_values(&:last) + + println "require_relative 'c_type'" + println + println "module RubyVM::MJIT" + println " C = Object.new" + println + + # Define macros + @macros.each do |macro| + unless definition = generate_macro(nodes_index[macro]) + raise "Failed to generate macro: #{macro}" + end + println " def C.#{macro} = #{definition}" + println + end + + # 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]) + raise "Failed to generate type: #{type}" + end + println " def C.#{type}" + println "@#{type} ||= #{definition}".gsub(/^/, " ").chomp + println " end" + println + end + + # Leave a stub for types that are referenced but not targeted + (@references - @types).each do |type| + println " def C.#{type} = #{DEFAULTS[type]}" + println + end + + chomp + println "end" + end + + private + + def generate_macro(node) + if node.spelling.start_with?('USE_') + # Special case: Always force USE_* to be true or false + case node + in Node[kind: :macro_definition, tokens: [_, '0' | '1' => token], children: []] + (Integer(token) == 1).to_s + end + else + # Otherwise, convert a C expression to a Ruby expression when possible + case node + in Node[kind: :macro_definition, tokens: tokens, children: []] + if tokens.first != node.spelling + raise "unexpected first token: '#{tokens.first}' != '#{node.spelling}'" + end + tokens.drop(1).map do |token| + case token + when /\A(0x)?\d+\z/, '(', '-', '<<', ')' + token + when *@enums.values.flatten + "self.#{token}" + else + raise "unexpected macro token: #{token}" + end + end.join(' ') + end + 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) + case node&.kind + when :struct, :union + # node.spelling is often empty for union, but we'd like to give it a name when it has one. + buf = +"CType::#{node.kind.to_s.sub(/\A[a-z]/, &:upcase)}.new(\n" + buf << " \"#{node.spelling}\", #{node.sizeof_type},\n" + node.children.each do |child| + field_builder = proc do |field, type| + if node.kind == :struct + to_ruby = @ruby_fields.fetch(node.spelling, []).include?(field) + " #{field}: [#{node.offsetof.fetch(field)}, #{type}#{', true' if to_ruby}],\n" + else + " #{field}: #{type},\n" + end + end + + case child + # BitField is struct-specific. So it must be handled here. + in Node[kind: :field_decl, spelling:, bitwidth:, children: [_grandchild]] if bitwidth > 0 + buf << field_builder.call(spelling, "CType::BitField.new(#{bitwidth}, #{node.offsetof.fetch(spelling) % 8})") + # In most cases, we'd like to let generate_type handle the type unless it's "(unnamed ...)". + in Node[kind: :field_decl, spelling:, type:] if !type.empty? && !type.match?(/\(unnamed [^)]+\)\z/) + buf << field_builder.call(spelling, generate_type(type)) + # Lastly, "(unnamed ...)" struct and union are handled here, which are also struct-specific. + in Node[kind: :field_decl, spelling:, children: [grandchild]] + buf << field_builder.call(spelling, generate_node(grandchild).gsub(/^/, ' ').sub(/\A +/, '')) + else # forward declarations are ignored + end + end + buf << ")" + when :typedef_decl + case node.children + in [child] + generate_node(child) + in [child, Node[kind: :integer_literal]] + generate_node(child) + in _ unless node.typedef_type.empty? + generate_type(node.typedef_type) + end + when :enum_decl + generate_type('int') + when :type_ref + generate_type(node.spelling) + end + end + + # Generate code from a type name. Used for resolving the name of a simple leaf node. + # @param type [String] + def generate_type(type) + if type.match?(/\[\d+\]\z/) + return "CType::Pointer.new { #{generate_type(type.sub!(/\[\d+\]\z/, ''))} }" + end + type = type.delete_suffix('const') + if type.end_with?('*') + return "CType::Pointer.new { #{generate_type(type.delete_suffix('*').rstrip)} }" + end + + type = type.gsub(/((const|volatile) )+/, '').rstrip + if type.start_with?(/(struct|union|enum) /) + target = type.split(' ', 2).last + push_target(target) + "self.#{target}" + else + begin + ctype = Fiddle::Importer.parse_ctype(type) + "CType::Immediate.new(#{ctype})" + rescue Fiddle::DLError + push_target(type) + "self.#{type}" + end + end + end + + def print(str) + @src << str + end + + def println(str = "") + @src << str << "\n" + end + + def chomp + @src.delete_suffix!("\n") + end + + def rstrip! + @src.rstrip! + end + + def push_target(target) + unless target.match?(/\A\w+\z/) + raise "invalid target: #{target}" + end + @references << target + end +end + +src_dir = File.expand_path('../..', __dir__) +if arch_bits == 64 + build_dir = File.join(src_dir, '.ruby') + ruby_platform = RUBY_PLATFORM +else + build_dir = File.join(src_dir, '.ruby-m32') + ruby_platform = 'i686-linux' +end +cflags = [ + src_dir, + build_dir, + File.join(src_dir, 'include'), + File.join(build_dir, ".ext/include/#{ruby_platform}"), +].map { |dir| "-I#{dir}" } + +nodes = HeaderParser.new(File.join(src_dir, 'mjit_compiler.h'), cflags: cflags).parse +generator = BindingGenerator.new( + macros: %w[ + USE_LAZY_LOAD + USE_RVARGC + VM_CALL_KW_SPLAT + VM_CALL_TAILCALL + NOT_COMPILED_STACK_SIZE + ], + enums: { + rb_method_type_t: %w[ + VM_METHOD_TYPE_ISEQ + VM_METHOD_TYPE_CFUNC + ], + vm_call_flag_bits: %w[ + VM_CALL_KW_SPLAT_bit + VM_CALL_TAILCALL_bit + ], + }, + types: %w[ + IC + IVC + RB_BUILTIN + VALUE + compile_status + iseq_inline_constant_cache + iseq_inline_constant_cache_entry + iseq_inline_iv_cache_entry + iseq_inline_storage_entry + rb_builtin_function + rb_cref_t + rb_iseq_constant_body + rb_iseq_struct + rb_iseq_t + rb_iv_index_tbl_entry + rb_mjit_compile_info + rb_serial_t + rb_mjit_unit + CALL_DATA + rb_call_data + rb_callcache + rb_callable_method_entry_struct + rb_method_definition_struct + rb_method_iseq_t + rb_callinfo + rb_method_type_t + mjit_options + compile_branch + inlined_call_context + rb_iseq_location_t + ], + ruby_fields: { + rb_iseq_location_struct: %w[ + pathobj + base_label + label + first_lineno + ] + }, +) +generator.generate(nodes) + +File.write(File.join(src_dir, "lib/mjit/c_#{arch_bits}.rb"), generator.src) diff --git a/tool/ruby_vm/helpers/dumper.rb b/tool/ruby_vm/helpers/dumper.rb index 98104f4b92..7aec9c7631 100644 --- a/tool/ruby_vm/helpers/dumper.rb +++ b/tool/ruby_vm/helpers/dumper.rb @@ -28,7 +28,7 @@ class RubyVM::Dumper path = Pathname.new(__FILE__) path = (path.relative_path_from(Pathname.pwd) rescue path).dirname path += '../views' - path += spec + path += File.basename(spec) src = path.read mode: 'rt:utf-8:utf-8' rescue Errno::ENOENT raise "don't know how to generate #{path}" diff --git a/tool/ruby_vm/views/_mjit_compile_getconstant_path.erb b/tool/ruby_vm/views/_mjit_compile_getconstant_path.erb deleted file mode 100644 index c321da9a52..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_getconstant_path.erb +++ /dev/null @@ -1,30 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2020 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% # compiler: Declare dst and ic -% insn.opes.each_with_index do |ope, i| - <%= ope.fetch(:decl) %> = (<%= ope.fetch(:type) %>)operands[<%= i %>]; -% end - -% # compiler: Capture IC values, locking getinlinecache - struct iseq_inline_constant_cache_entry *ice = ic->entry; - if (ice != NULL && !status->compile_info->disable_const_cache) { -% # JIT: Inline everything in IC, and cancel the slow path - fprintf(f, " if (vm_inlined_ic_hit_p(0x%"PRIxVALUE", 0x%"PRIxVALUE", (const rb_cref_t *)0x%"PRIxVALUE", reg_cfp->ep)) {", ice->flags, ice->value, (VALUE)ice->ic_cref); - fprintf(f, " stack[%d] = 0x%"PRIxVALUE";\n", b->stack_size, ice->value); - fprintf(f, " }"); - fprintf(f, " else {"); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " goto const_cancel;\n"); - fprintf(f, " }"); - -% # compiler: Move JIT compiler's internal stack pointer - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - break; - } diff --git a/tool/ruby_vm/views/_mjit_compile_insn.erb b/tool/ruby_vm/views/_mjit_compile_insn.erb deleted file mode 100644 index f54d1b0e0e..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_insn.erb +++ /dev/null @@ -1,92 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2018 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. - fprintf(f, "{\n"); - { -% # compiler: Prepare operands which may be used by `insn.call_attribute` -% insn.opes.each_with_index do |ope, i| - MAYBE_UNUSED(<%= ope.fetch(:decl) %>) = (<%= ope.fetch(:type) %>)operands[<%= i %>]; -% end -% -% # JIT: Declare stack_size to be used in some macro of _mjit_compile_insn_body.erb - if (status->local_stack_p) { - fprintf(f, " MAYBE_UNUSED(unsigned int) stack_size = %u;\n", b->stack_size); - } -% -% # JIT: Declare variables for operands, popped values and return values -% insn.declarations.each do |decl| - fprintf(f, " <%= decl %>;\n"); -% end - -% # JIT: Set const expressions for `RubyVM::OperandsUnifications` insn -% insn.preamble.each do |amble| - fprintf(f, "<%= amble.expr.sub(/const \S+\s+/, '') %>\n"); -% end -% -% # JIT: Initialize operands -% insn.opes.each_with_index do |ope, i| - fprintf(f, " <%= ope.fetch(:name) %> = (<%= ope.fetch(:type) %>)0x%"PRIxVALUE";", operands[<%= i %>]); -% case ope.fetch(:type) -% when 'ID' - comment_id(f, (ID)operands[<%= i %>]); -% when 'CALL_DATA' - comment_id(f, vm_ci_mid(((CALL_DATA)operands[<%= i %>])->ci)); -% when 'VALUE' - if (SYMBOL_P((VALUE)operands[<%= i %>])) comment_id(f, SYM2ID((VALUE)operands[<%= i %>])); -% end - fprintf(f, "\n"); -% end -% -% # JIT: Initialize popped values -% insn.pops.reverse_each.with_index.reverse_each do |pop, i| - fprintf(f, " <%= pop.fetch(:name) %> = stack[%d];\n", b->stack_size - <%= i + 1 %>); -% end -% -% # JIT: move sp and pc if necessary -<%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%> -% -% # JIT: Print insn body in insns.def -<%= render 'mjit_compile_insn_body', locals: { insn: insn } -%> -% -% # JIT: Set return values -% insn.rets.reverse_each.with_index do |ret, i| -% # TOPN(n) = ... - fprintf(f, " stack[%d] = <%= ret.fetch(:name) %>;\n", b->stack_size + (int)<%= insn.call_attribute('sp_inc') %> - <%= i + 1 %>); -% end -% -% # JIT: We should evaluate ISeq modified for TracePoint if it's enabled. Note: This is slow. -% # leaf insn may not cancel JIT. leaf_without_check_ints is covered in RUBY_VM_CHECK_INTS of _mjit_compile_insn_body.erb. -% unless insn.always_leaf? || insn.leaf_without_check_ints? - fprintf(f, " if (UNLIKELY(!mjit_call_p)) {\n"); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size + (int)<%= insn.call_attribute('sp_inc') %>); - if (!pc_moved_p) { - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", next_pos); - } - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_invalidate_all);\n"); - fprintf(f, " goto cancel;\n"); - fprintf(f, " }\n"); -% end -% -% # compiler: Move JIT compiler's internal stack pointer - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - } - fprintf(f, "}\n"); -% -% # compiler: If insn has conditional JUMP, the code should go to the branch not targeted by JUMP next. -% if insn.expr.expr =~ /if\s+\([^{}]+\)\s+\{[^{}]+JUMP\([^)]+\);[^{}]+\}/ - if (ALREADY_COMPILED_P(status, pos + insn_len(insn))) { - fprintf(f, "goto label_%d;\n", pos + insn_len(insn)); - } - else { - compile_insns(f, body, b->stack_size, pos + insn_len(insn), status); - } -% end -% -% # compiler: If insn returns (leave) or does longjmp (throw), the branch should no longer be compiled. TODO: create attr for it? -% if insn.expr.expr =~ /\sTHROW_EXCEPTION\([^)]+\);/ || insn.expr.expr =~ /\bvm_pop_frame\(/ - b->finish_p = TRUE; -% end diff --git a/tool/ruby_vm/views/_mjit_compile_insn_body.erb b/tool/ruby_vm/views/_mjit_compile_insn_body.erb deleted file mode 100644 index 187e043837..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_insn_body.erb +++ /dev/null @@ -1,129 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2018 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% to_cstr = lambda do |line| -% normalized = line.gsub(/\t/, ' ' * 8) -% indented = normalized.sub(/\A(?!#)/, ' ') # avoid indenting preprocessor -% rstring2cstr(indented.rstrip).sub(/"\z/, '\\n"') -% end -% -% # -% # Expand simple macro, which doesn't require dynamic C code. -% # -% expand_simple_macros = lambda do |arg_expr| -% arg_expr.dup.tap do |expr| -% # For `leave`. We can't proceed next ISeq in the same JIT function. -% expr.gsub!(/^(?\s*)RESTORE_REGS\(\);\n/) do -% indent = Regexp.last_match[:indent] -% <<-end.gsub(/^ +/, '') -% #if OPT_CALL_THREADED_CODE -% #{indent}rb_ec_thread_ptr(ec)->retval = val; -% #{indent}return 0; -% #else -% #{indent}return val; -% #endif -% end -% end -% expr.gsub!(/^(?\s*)NEXT_INSN\(\);\n/) do -% indent = Regexp.last_match[:indent] -% <<-end.gsub(/^ +/, '') -% #{indent}UNREACHABLE_RETURN(Qundef); -% end -% end -% end -% end -% -% # -% # Print a body of insn, but with macro expansion. -% # -% expand_simple_macros.call(insn.expr.expr).each_line do |line| -% # -% # Expand dynamic macro here (only JUMP for now) -% # -% # TODO: support combination of following macros in the same line -% case line -% when /\A\s+RUBY_VM_CHECK_INTS\(ec\);\s+\z/ -% if insn.leaf_without_check_ints? # lazily move PC and optionalize mjit_call_p here - fprintf(f, " if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {\n"); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", next_pos); /* ADD_PC(INSN_ATTR(width)); */ - fprintf(f, " rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);\n"); - fprintf(f, " if (UNLIKELY(!mjit_call_p)) {\n"); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_invalidate_all);\n"); - fprintf(f, " goto cancel;\n"); - fprintf(f, " }\n"); - fprintf(f, " }\n"); -% else - fprintf(f, <%= to_cstr.call(line) %>); -% end -% when /\A\s+JUMP\((?[^)]+)\);\s+\z/ -% dest = Regexp.last_match[:dest] -% -% if insn.name == 'opt_case_dispatch' # special case... TODO: use another macro to avoid checking name - { - struct case_dispatch_var arg; - arg.f = f; - arg.base_pos = pos + insn_len(insn); - arg.last_value = Qundef; - - fprintf(f, " switch (<%= dest %>) {\n"); - st_foreach(RHASH_TBL_RAW(hash), compile_case_dispatch_each, (VALUE)&arg); - fprintf(f, " case %lu:\n", else_offset); - fprintf(f, " goto label_%lu;\n", arg.base_pos + else_offset); - fprintf(f, " }\n"); - } -% else -% # Before we `goto` next insn, we need to set return values, especially for getinlinecache -% insn.rets.reverse_each.with_index do |ret, i| -% # TOPN(n) = ... - fprintf(f, " stack[%d] = <%= ret.fetch(:name) %>;\n", b->stack_size + (int)<%= insn.call_attribute('sp_inc') %> - <%= i + 1 %>); -% end -% - next_pos = pos + insn_len(insn) + (unsigned int)<%= dest %>; - fprintf(f, " goto label_%d;\n", next_pos); -% end -% when /\A\s+CALL_SIMPLE_METHOD\(\);\s+\z/ -% # For `opt_xxx`'s fallbacks. - if (status->local_stack_p) { - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - } - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_opt_insn);\n"); - fprintf(f, " goto cancel;\n"); -% when /\A(?.+\b)INSN_LABEL\((?[^)]+)\)(?.+)\z/m -% prefix, name, suffix = Regexp.last_match[:prefix], Regexp.last_match[:name], Regexp.last_match[:suffix] - fprintf(f, " <%= prefix.gsub(/\t/, ' ' * 8) %>INSN_LABEL(<%= name %>_%d)<%= suffix.sub(/\n/, '\n') %>", pos); -% else -% if insn.handles_sp? -% # If insn.handles_sp? is true, cfp->sp might be changed inside insns (like vm_caller_setup_arg_block) -% # and thus we need to use cfp->sp, even when local_stack_p is TRUE. When insn.handles_sp? is true, -% # cfp->sp should be available too because _mjit_compile_pc_and_sp.erb sets it. - fprintf(f, <%= to_cstr.call(line) %>); -% else -% # If local_stack_p is TRUE and insn.handles_sp? is false, stack values are only available in local variables -% # for stack. So we need to replace those macros if local_stack_p is TRUE here. -% case line -% when /\bGET_SP\(\)/ -% # reg_cfp->sp - fprintf(f, <%= to_cstr.call(line.sub(/\bGET_SP\(\)/, '%s')) %>, (status->local_stack_p ? "(stack + stack_size)" : "GET_SP()")); -% when /\bSTACK_ADDR_FROM_TOP\((?[^)]+)\)/ -% # #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n)) -% num = Regexp.last_match[:num] - fprintf(f, <%= to_cstr.call(line.sub(/\bSTACK_ADDR_FROM_TOP\(([^)]+)\)/, '%s')) %>, - (status->local_stack_p ? "(stack + (stack_size - (<%= num %>)))" : "STACK_ADDR_FROM_TOP(<%= num %>)")); -% when /\bTOPN\((?[^)]+)\)/ -% # #define TOPN(n) (*(GET_SP()-(n)-1)) -% num = Regexp.last_match[:num] - fprintf(f, <%= to_cstr.call(line.sub(/\bTOPN\(([^)]+)\)/, '%s')) %>, - (status->local_stack_p ? "*(stack + (stack_size - (<%= num %>) - 1))" : "TOPN(<%= num %>)")); -% else - fprintf(f, <%= to_cstr.call(line) %>); -% end -% end -% end -% end diff --git a/tool/ruby_vm/views/_mjit_compile_invokebuiltin.erb b/tool/ruby_vm/views/_mjit_compile_invokebuiltin.erb deleted file mode 100644 index a3796ffc5e..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_invokebuiltin.erb +++ /dev/null @@ -1,29 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2020 Urabe, Shyouhei. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% insn.opes.each_with_index do |ope, i| - <%= ope.fetch(:decl) %> = (<%= ope.fetch(:type) %>)operands[<%= i %>]; -% end - rb_snum_t sp_inc = <%= insn.call_attribute('sp_inc') %>; - unsigned sp = b->stack_size + (unsigned)sp_inc; - VM_ASSERT(b->stack_size > -sp_inc); - VM_ASSERT(sp_inc < UINT_MAX - b->stack_size); - - if (bf->compiler) { - fprintf(f, "{\n"); - fprintf(f, " VALUE val;\n"); - bf->compiler(f, <%= - insn.name == 'invokebuiltin' ? '-1' : '(rb_num_t)operands[1]' - %>, b->stack_size, body->builtin_inline_p); - fprintf(f, " stack[%u] = val;\n", sp - 1); - fprintf(f, "}\n"); -% if insn.name != 'opt_invokebuiltin_delegate_leave' - b->stack_size = sp; - break; -% end - } diff --git a/tool/ruby_vm/views/_mjit_compile_ivar.erb b/tool/ruby_vm/views/_mjit_compile_ivar.erb deleted file mode 100644 index 1425b3b055..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_ivar.erb +++ /dev/null @@ -1,110 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2018 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% # Optimized case of get_instancevariable instruction. -#if OPT_IC_FOR_IVAR -{ -% # compiler: Prepare operands which may be used by `insn.call_attribute` -% insn.opes.each_with_index do |ope, i| - MAYBE_UNUSED(<%= ope.fetch(:decl) %>) = (<%= ope.fetch(:type) %>)operands[<%= i %>]; -% end -% # compiler: Use copied IVC to avoid race condition - IVC ic_copy = &(status->is_entries + ((union iseq_inline_storage_entry *)ic - body->is_entries))->iv_cache; -% - if (!status->compile_info->disable_ivar_cache && ic_copy->entry) { // Only ic_copy is enabled. -% # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`. -% # <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%> -% -% # JIT: prepare vm_getivar/vm_setivar arguments and variables - fprintf(f, "{\n"); - fprintf(f, " VALUE obj = GET_SELF();\n"); - fprintf(f, " const uint32_t index = %u;\n", (ic_copy->entry->index)); - if (status->merge_ivar_guards_p) { -% # JIT: Access ivar without checking these VM_ASSERTed prerequisites as we checked them in the beginning of `mjit_compile_body` - fprintf(f, " VM_ASSERT(RB_TYPE_P(obj, T_OBJECT));\n"); - fprintf(f, " VM_ASSERT((rb_serial_t)%"PRI_SERIALT_PREFIX"u == RCLASS_SERIAL(RBASIC(obj)->klass));\n", ic_copy->entry->class_serial); - fprintf(f, " VM_ASSERT(index < ROBJECT_NUMIV(obj));\n"); -% if insn.name == 'setinstancevariable' -#if USE_RVARGC - fprintf(f, " if (LIKELY(!RB_OBJ_FROZEN_RAW(obj) && index < ROBJECT_NUMIV(obj))) {\n"); - fprintf(f, " RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[index], stack[%d]);\n", b->stack_size - 1); -#else - fprintf(f, " if (LIKELY(!RB_OBJ_FROZEN_RAW(obj) && %s)) {\n", status->max_ivar_index >= ROBJECT_EMBED_LEN_MAX ? "true" : "RB_FL_ANY_RAW(obj, ROBJECT_EMBED)"); - fprintf(f, " RB_OBJ_WRITE(obj, &ROBJECT(obj)->as.%s, stack[%d]);\n", - status->max_ivar_index >= ROBJECT_EMBED_LEN_MAX ? "heap.ivptr[index]" : "ary[index]", b->stack_size - 1); -#endif - fprintf(f, " }\n"); -% else - fprintf(f, " VALUE val;\n"); -#if USE_RVARGC - fprintf(f, " if (LIKELY(index < ROBJECT_NUMIV(obj) && (val = ROBJECT_IVPTR(obj)[index]) != Qundef)) {\n"); -#else - fprintf(f, " if (LIKELY(%s && (val = ROBJECT(obj)->as.%s) != Qundef)) {\n", - status->max_ivar_index >= ROBJECT_EMBED_LEN_MAX ? "true" : "RB_FL_ANY_RAW(obj, ROBJECT_EMBED)", - status->max_ivar_index >= ROBJECT_EMBED_LEN_MAX ? "heap.ivptr[index]" : "ary[index]"); -#endif - fprintf(f, " stack[%d] = val;\n", b->stack_size); - fprintf(f, " }\n"); -%end - } - else { - fprintf(f, " const rb_serial_t ic_serial = (rb_serial_t)%"PRI_SERIALT_PREFIX"u;\n", ic_copy->entry->class_serial); -% # JIT: cache hit path of vm_getivar/vm_setivar, or cancel JIT (recompile it with exivar) -% if insn.name == 'setinstancevariable' - fprintf(f, " if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj) && !RB_OBJ_FROZEN_RAW(obj))) {\n"); - fprintf(f, " VALUE *ptr = ROBJECT_IVPTR(obj);\n"); - fprintf(f, " RB_OBJ_WRITE(obj, &ptr[index], stack[%d]);\n", b->stack_size - 1); - fprintf(f, " }\n"); -% else - fprintf(f, " VALUE val;\n"); - fprintf(f, " if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj) && (val = ROBJECT_IVPTR(obj)[index]) != Qundef)) {\n"); - fprintf(f, " stack[%d] = val;\n", b->stack_size); - fprintf(f, " }\n"); -% end - } - fprintf(f, " else {\n"); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " goto ivar_cancel;\n"); - fprintf(f, " }\n"); - -% # compiler: Move JIT compiler's internal stack pointer - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - fprintf(f, "}\n"); - break; - } -% if insn.name == 'getinstancevariable' - else if (!status->compile_info->disable_exivar_cache && ic_copy->entry) { -% # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`. -% # <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%> -% -% # JIT: prepare vm_getivar's arguments and variables - fprintf(f, "{\n"); - fprintf(f, " VALUE obj = GET_SELF();\n"); - fprintf(f, " const rb_serial_t ic_serial = (rb_serial_t)%"PRI_SERIALT_PREFIX"u;\n", ic_copy->entry->class_serial); - fprintf(f, " const uint32_t index = %u;\n", ic_copy->entry->index); -% # JIT: cache hit path of vm_getivar, or cancel JIT (recompile it without any ivar optimization) - fprintf(f, " struct gen_ivtbl *ivtbl;\n"); - fprintf(f, " VALUE val;\n"); - fprintf(f, " if (LIKELY(FL_TEST_RAW(obj, FL_EXIVAR) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && rb_ivar_generic_ivtbl_lookup(obj, &ivtbl) && index < ivtbl->numiv && (val = ivtbl->ivptr[index]) != Qundef)) {\n"); - fprintf(f, " stack[%d] = val;\n", b->stack_size); - fprintf(f, " }\n"); - fprintf(f, " else {\n"); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " goto exivar_cancel;\n"); - fprintf(f, " }\n"); - -% # compiler: Move JIT compiler's internal stack pointer - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - fprintf(f, "}\n"); - break; - } -% end -} -#endif // OPT_IC_FOR_IVAR diff --git a/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb b/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb deleted file mode 100644 index 390b3ce525..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb +++ /dev/null @@ -1,38 +0,0 @@ -% # Copyright (c) 2018 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% # JIT: When an insn is leaf, we don't need to Move pc for a catch table on catch_except_p, #caller_locations, -% # and rb_profile_frames. For check_ints, we lazily move PC when we have interruptions. - MAYBE_UNUSED(bool pc_moved_p) = false; - if (<%= !(insn.always_leaf? || insn.leaf_without_check_ints?) %>) { - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", next_pos); /* ADD_PC(INSN_ATTR(width)); */ - pc_moved_p = true; - } -% -% # JIT: move sp to use or preserve stack variables - if (status->local_stack_p) { -% # sp motion is optimized away for `handles_sp? #=> false` case. -% # Thus sp should be set properly before `goto cancel`. -% if insn.handles_sp? -% # JIT-only behavior (pushing JIT's local variables to VM's stack): - { - rb_snum_t i, push_size; - push_size = -<%= insn.call_attribute('sp_inc') %> + <%= insn.rets.size %> - <%= insn.pops.size %>; - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %ld;\n", push_size); /* POPN(INSN_ATTR(popn)); */ - for (i = 0; i < push_size; i++) { - fprintf(f, " *(reg_cfp->sp + %ld) = stack[%ld];\n", i - push_size, (rb_snum_t)b->stack_size - push_size + i); - } - } -% end - } - else { -% if insn.handles_sp? - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size - <%= insn.pops.size %>); /* POPN(INSN_ATTR(popn)); */ -% else - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); -% end - } diff --git a/tool/ruby_vm/views/_mjit_compile_send.erb b/tool/ruby_vm/views/_mjit_compile_send.erb deleted file mode 100644 index 316974a7e6..0000000000 --- a/tool/ruby_vm/views/_mjit_compile_send.erb +++ /dev/null @@ -1,119 +0,0 @@ -% # -*- C -*- -% # Copyright (c) 2018 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -% -% # Optimized case of send / opt_send_without_block instructions. -{ -% # compiler: Prepare operands which may be used by `insn.call_attribute` -% insn.opes.each_with_index do |ope, i| - MAYBE_UNUSED(<%= ope.fetch(:decl) %>) = (<%= ope.fetch(:type) %>)operands[<%= i %>]; -% end -% # compiler: Use captured cc to avoid race condition - size_t cd_index = call_data_index(cd, body); - const struct rb_callcache **cc_entries = captured_cc_entries(status); - const struct rb_callcache *captured_cc = cc_entries[cd_index]; -% -% # compiler: Inline send insn where some supported fastpath is used. - const rb_iseq_t *iseq = NULL; - const CALL_INFO ci = cd->ci; - int kw_splat = IS_ARGS_KW_SPLAT(ci) > 0; - extern bool rb_splat_or_kwargs_p(const struct rb_callinfo *restrict ci); - if (!status->compile_info->disable_send_cache && has_valid_method_type(captured_cc) && ( -% # `CC_SET_FASTPATH(cd->cc, vm_call_cfunc_with_frame, ...)` in `vm_call_cfunc` - (vm_cc_cme(captured_cc)->def->type == VM_METHOD_TYPE_CFUNC - && !rb_splat_or_kwargs_p(ci) && !kw_splat) -% # `CC_SET_FASTPATH(cc, vm_call_iseq_setup_func(...), vm_call_iseq_optimizable_p(...))` in `vm_callee_setup_arg`, -% # and support only non-VM_CALL_TAILCALL path inside it - || (vm_cc_cme(captured_cc)->def->type == VM_METHOD_TYPE_ISEQ - && fastpath_applied_iseq_p(ci, captured_cc, iseq = def_iseq_ptr(vm_cc_cme(captured_cc)->def)) - && !(vm_ci_flag(ci) & VM_CALL_TAILCALL)) - )) { - const bool cfunc_debug = false; // Set true when you want to see inlined cfunc - if (cfunc_debug && vm_cc_cme(captured_cc)->def->type == VM_METHOD_TYPE_CFUNC) - fprintf(stderr, " * %s\n", rb_id2name(vm_ci_mid(ci))); - - int sp_inc = (int)sp_inc_of_sendish(ci); - fprintf(f, "{\n"); - -% # JIT: Invalidate call cache if it requires vm_search_method. This allows to inline some of following things. - bool opt_class_of = !maybe_special_const_class_p(captured_cc->klass); // If true, use RBASIC_CLASS instead of CLASS_OF to reduce code size - fprintf(f, " const struct rb_callcache *cc = (const struct rb_callcache *)0x%"PRIxVALUE";\n", (VALUE)captured_cc); - fprintf(f, " const rb_callable_method_entry_t *cc_cme = (const rb_callable_method_entry_t *)0x%"PRIxVALUE";\n", (VALUE)vm_cc_cme(captured_cc)); - fprintf(f, " const VALUE recv = stack[%d];\n", b->stack_size + sp_inc - 1); - fprintf(f, " if (UNLIKELY(%s || !vm_cc_valid_p(cc, cc_cme, %s(recv)))) {\n", opt_class_of ? "RB_SPECIAL_CONST_P(recv)" : "false", opt_class_of ? "RBASIC_CLASS" : "CLASS_OF"); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " goto send_cancel;\n"); - fprintf(f, " }\n"); - -% # JIT: move sp and pc if necessary -<%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%> - -% # JIT: If ISeq is inlinable, call the inlined method without pushing a frame. - if (iseq && status->inlined_iseqs != NULL && ISEQ_BODY(iseq) == status->inlined_iseqs[pos]) { - fprintf(f, " {\n"); - fprintf(f, " VALUE orig_self = reg_cfp->self;\n"); - fprintf(f, " reg_cfp->self = stack[%d];\n", b->stack_size + sp_inc - 1); - fprintf(f, " stack[%d] = _mjit%d_inlined_%d(ec, reg_cfp, orig_self, original_iseq);\n", b->stack_size + sp_inc - 1, status->compiled_id, pos); - fprintf(f, " reg_cfp->self = orig_self;\n"); - fprintf(f, " }\n"); - } - else { -% # JIT: Forked `vm_sendish` (except method_explorer = vm_search_method_wrap) to inline various things - fprintf(f, " {\n"); - fprintf(f, " VALUE val;\n"); - fprintf(f, " struct rb_calling_info calling;\n"); -% if insn.name == 'send' - fprintf(f, " calling.block_handler = vm_caller_setup_arg_block(ec, reg_cfp, (const struct rb_callinfo *)0x%"PRIxVALUE", (rb_iseq_t *)0x%"PRIxVALUE", FALSE);\n", (VALUE)ci, (VALUE)blockiseq); -% else - fprintf(f, " calling.block_handler = VM_BLOCK_HANDLER_NONE;\n"); -% end - fprintf(f, " calling.kw_splat = %d;\n", kw_splat); - fprintf(f, " calling.recv = stack[%d];\n", b->stack_size + sp_inc - 1); - fprintf(f, " calling.argc = %d;\n", vm_ci_argc(ci)); - - if (vm_cc_cme(captured_cc)->def->type == VM_METHOD_TYPE_CFUNC) { -% # TODO: optimize this more - fprintf(f, " calling.ci = (CALL_INFO)0x%"PRIxVALUE";\n", (VALUE)ci); // creating local cd here because operand's cd->cc may not be the same as inlined cc. - fprintf(f, " calling.cc = cc;"); - fprintf(f, " val = vm_call_cfunc_with_frame(ec, reg_cfp, &calling);\n"); - } - else { // VM_METHOD_TYPE_ISEQ -% # fastpath_applied_iseq_p checks rb_simple_iseq_p, which ensures has_opt == FALSE - fprintf(f, " vm_call_iseq_setup_normal(ec, reg_cfp, &calling, cc_cme, 0, %d, %d);\n", ISEQ_BODY(iseq)->param.size, ISEQ_BODY(iseq)->local_table_size); - if (ISEQ_BODY(iseq)->catch_except_p) { - fprintf(f, " VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);\n"); - fprintf(f, " val = vm_exec(ec, true);\n"); - } - else { - fprintf(f, " if ((val = jit_exec(ec)) == Qundef) {\n"); - fprintf(f, " VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);\n"); // This is vm_call0_body's code after vm_call_iseq_setup - fprintf(f, " val = vm_exec(ec, false);\n"); - fprintf(f, " }\n"); - } - } - fprintf(f, " stack[%d] = val;\n", b->stack_size + sp_inc - 1); - fprintf(f, " }\n"); - -% # JIT: We should evaluate ISeq modified for TracePoint if it's enabled. Note: This is slow. - fprintf(f, " if (UNLIKELY(!mjit_call_p)) {\n"); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size + (int)<%= insn.call_attribute('sp_inc') %>); - if (!pc_moved_p) { - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", next_pos); - } - fprintf(f, " RB_DEBUG_COUNTER_INC(mjit_cancel_invalidate_all);\n"); - fprintf(f, " goto cancel;\n"); - fprintf(f, " }\n"); - } - -% # compiler: Move JIT compiler's internal stack pointer - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - - fprintf(f, "}\n"); - break; - } -} diff --git a/tool/ruby_vm/views/mjit_compile.inc.erb b/tool/ruby_vm/views/mjit_compile.inc.erb deleted file mode 100644 index 00808b21ff..0000000000 --- a/tool/ruby_vm/views/mjit_compile.inc.erb +++ /dev/null @@ -1,110 +0,0 @@ -/* -*- C -*- */ - -% # Copyright (c) 2018 Takashi Kokubun. All rights reserved. -% # -% # This file is a part of the programming language Ruby. Permission is hereby -% # granted, to either redistribute and/or modify this file, provided that the -% # conditions mentioned in the file COPYING are met. Consult the file for -% # details. -<%= render 'copyright' %> -% -% # This is an ERB template that generates Ruby code that generates C code that -% # generates JIT-ed C code. -<%= render 'notice', locals: { - this_file: 'is the main part of compile_insn() in mjit_compiler.c', - edit: __FILE__, -} -%> -% -% unsupported_insns = [ -% 'defineclass', # low priority -% ] -% -% opt_send_without_block = RubyVM::Instructions.find { |i| i.name == 'opt_send_without_block' } -% if opt_send_without_block.nil? -% raise 'opt_send_without_block not found' -% end -% -% send_compatible_opt_insns = RubyVM::BareInstructions.to_a.select do |insn| -% insn.name.start_with?('opt_') && opt_send_without_block.opes == insn.opes && -% insn.expr.expr.lines.any? { |l| l.match(/\A\s+CALL_SIMPLE_METHOD\(\);\s+\z/) } -% end.map(&:name) -% -% # Available variables and macros in JIT-ed function: -% # ec: the first argument of _mjitXXX -% # reg_cfp: the second argument of _mjitXXX -% # GET_CFP(): refers to `reg_cfp` -% # GET_EP(): refers to `reg_cfp->ep` -% # GET_SP(): refers to `reg_cfp->sp`, or `(stack + stack_size)` if local_stack_p -% # GET_SELF(): refers to `cfp_self` -% # GET_LEP(): refers to `VM_EP_LEP(reg_cfp->ep)` -% # EXEC_EC_CFP(): refers to `val = vm_exec(ec, true)` with frame setup -% # CALL_METHOD(): using `GET_CFP()` and `EXEC_EC_CFP()` -% # TOPN(): refers to `reg_cfp->sp`, or `*(stack + (stack_size - num - 1))` if local_stack_p -% # STACK_ADDR_FROM_TOP(): refers to `reg_cfp->sp`, or `stack + (stack_size - num)` if local_stack_p -% # DISPATCH_ORIGINAL_INSN(): expanded in _mjit_compile_insn.erb -% # THROW_EXCEPTION(): specially defined for JIT -% # RESTORE_REGS(): specially defined for `leave` - -switch (insn) { -% (RubyVM::BareInstructions.to_a + RubyVM::OperandsUnifications.to_a).each do |insn| -% next if unsupported_insns.include?(insn.name) - case BIN(<%= insn.name %>): { -% # Instruction-specific behavior in JIT -% case insn.name -% when 'opt_send_without_block', 'send' -<%= render 'mjit_compile_send', locals: { insn: insn } -%> -% when *send_compatible_opt_insns -% # To avoid cancel, just emit `opt_send_without_block` instead of `opt_*` insn if call cache is populated. -% cd_index = insn.opes.index { |o| o.fetch(:type) == 'CALL_DATA' } - if (has_cache_for_send(captured_cc_entries(status)[call_data_index((CALL_DATA)operands[<%= cd_index %>], body)], BIN(<%= insn.name %>))) { -<%= render 'mjit_compile_send', locals: { insn: opt_send_without_block } -%> -<%= render 'mjit_compile_insn', locals: { insn: opt_send_without_block } -%> - break; - } -% when 'getinstancevariable', 'setinstancevariable' -<%= render 'mjit_compile_ivar', locals: { insn: insn } -%> -% when 'invokebuiltin', 'opt_invokebuiltin_delegate' -<%= render 'mjit_compile_invokebuiltin', locals: { insn: insn } -%> -% when 'opt_getconstant_path' -<%= render 'mjit_compile_getconstant_path', locals: { insn: insn } -%> -% when 'leave', 'opt_invokebuiltin_delegate_leave' -% # opt_invokebuiltin_delegate_leave also implements leave insn. We need to handle it here for inlining. -% if insn.name == 'opt_invokebuiltin_delegate_leave' -<%= render 'mjit_compile_invokebuiltin', locals: { insn: insn } -%> -% else - if (b->stack_size != 1) { - if (mjit_opts.warnings || mjit_opts.verbose) - fprintf(stderr, "MJIT warning: Unexpected JIT stack_size on leave: %d\n", b->stack_size); - status->success = false; - } -% end -% # Skip vm_pop_frame for inlined call - if (status->inlined_iseqs != NULL) { // the current ISeq is NOT being inlined -% # Cancel on interrupts to make leave insn leaf - fprintf(f, " if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {\n"); - fprintf(f, " reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size); - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); - fprintf(f, " rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);\n"); - fprintf(f, " }\n"); - fprintf(f, " ec->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(reg_cfp);\n"); // vm_pop_frame - } - fprintf(f, " return stack[0];\n"); - b->stack_size += <%= insn.call_attribute('sp_inc') %>; - b->finish_p = TRUE; - break; -% end -% -% # Main insn implementation generated by insns.def -<%= render 'mjit_compile_insn', locals: { insn: insn } -%> - break; - } -% end -% -% # We don't support InstructionsUnifications yet because it's not used for now. -% # We don't support TraceInstructions yet. There is no blocker for it but it's just not implemented. - default: - if (mjit_opts.warnings || mjit_opts.verbose) - fprintf(stderr, "MJIT warning: Skipped to compile unsupported instruction: %s\n", insn_name(insn)); - status->success = false; - break; -} diff --git a/tool/ruby_vm/views/mjit_compile_attr.inc.erb b/tool/ruby_vm/views/mjit_compile_attr.inc.erb new file mode 100644 index 0000000000..7b925420dd --- /dev/null +++ b/tool/ruby_vm/views/mjit_compile_attr.inc.erb @@ -0,0 +1,17 @@ +static rb_snum_t +mjit_call_attribute_sp_inc(const int insn, const VALUE *operands) +{ + switch (insn) { +% (RubyVM::BareInstructions.to_a + RubyVM::OperandsUnifications.to_a).each do |insn| + case BIN(<%= insn.name %>): { +% # compiler: Prepare operands which may be used by `insn.call_attribute` +% insn.opes.each_with_index do |ope, i| + MAYBE_UNUSED(<%= ope.fetch(:decl) %>) = (<%= ope.fetch(:type) %>)operands[<%= i %>]; +% end + return <%= insn.call_attribute('sp_inc') %>; + } +% end + default: + rb_bug("unexpected insn in mjit_call_attribute_sp_inc"); + } +} diff --git a/tool/ruby_vm/views/mjit_instruction.rb.erb b/tool/ruby_vm/views/mjit_instruction.rb.erb new file mode 100644 index 0000000000..1f49669fa5 --- /dev/null +++ b/tool/ruby_vm/views/mjit_instruction.rb.erb @@ -0,0 +1,40 @@ +module RubyVM::MJIT + Instruction = Struct.new( + :name, + :bin, + :len, + :expr, + :declarations, + :preamble, + :opes, + :pops, + :rets, + :always_leaf?, + :leaf_without_check_ints?, + :handles_sp?, + ) + + INSNS = { +% RubyVM::Instructions.each_with_index do |insn, i| +% next if insn.name.start_with?('trace_') + <%= i %> => Instruction.new( + name: :<%= insn.name %>, + bin: <%= i %>, # BIN(<%= insn.name %>) + len: <%= insn.width %>, # insn_len + expr: <<-EXPR, +<%= insn.expr.expr.dump.sub(/\A"/, '').sub(/"\z/, '').gsub(/\\n/, "\n").gsub(/\\t/, ' ' * 8) %> + EXPR + declarations: <%= insn.declarations.inspect %>, + preamble: <%= insn.preamble.map(&:expr).inspect %>, + opes: <%= insn.opes.inspect %>, + pops: <%= insn.pops.inspect %>, + rets: <%= insn.rets.inspect %>, + always_leaf?: <%= insn.always_leaf? %>, + leaf_without_check_ints?: <%= insn.leaf_without_check_ints? %>, + handles_sp?: <%= insn.handles_sp? %>, + ), +% end + } + + private_constant *constants +end if RubyVM::MJIT.enabled? diff --git a/tool/update-deps b/tool/update-deps index 27230c50e4..ba94840f65 100755 --- a/tool/update-deps +++ b/tool/update-deps @@ -120,7 +120,7 @@ FILES_NEED_VPATH = %w[ known_errors.inc lex.c miniprelude.c - mjit_compile.inc + mjit_compile_attr.inc newline.c node_name.inc opt_sc.inc diff --git a/win32/Makefile.sub b/win32/Makefile.sub index 05a088fe5c..2910901d7e 100644 --- a/win32/Makefile.sub +++ b/win32/Makefile.sub @@ -1313,7 +1313,7 @@ probes.h: {$(VPATH)}probes.dmyh < insns_rules.mk] !else if [for %I in ($(INSNS)) do \ @@ -1331,6 +1331,10 @@ INSNS = opt_sc.inc optinsn.inc optunifs.inc insns.inc insns_info.inc \ ! endif !endif +$(srcdir)/mjit_instruction.rb: $(tooldir)/ruby_vm/views/mjit_instruction.rb.erb + $(ECHO) generating $@ + $(Q) $(BASERUBY) -Ku $(tooldir)/insns2vm.rb $(INSNS2VMOPT) $@ + verconf.h: verconf.mk loadpath: verconf.h