Commit e724a55b

e724a55bc348Merged

Ryan Seys <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified

Description

analyze,codegen: direct-path instance_exec lift to sp_iexec_

Adds the analyze-time direct-call lift sibling of ieval_rewrite_call. A recv.instance_exec(args) { |params| body } call site (outside a trampoline method) gets the block detached, lifted to a file-scope static function

static void sp_iexec_(sp_ *self, T0 lv_p0, T1 lv_p1, ...)

and the call rewritten in place to __sp_iexec_<N>(recv, arg0, ...). Codegen consumes the parallel @iexec_class_idxs / @iexec_body_ids / @iexec_block_pnames / @iexec_block_ptypes / @iexec_return_types registries to emit prototypes, function bodies, and the call-site dispatch hooks (expression context comma-wraps for now, statement context emits a bare call). The NM / NB AST-carry path the ieval rewrite uses is extended to recognise the __sp_iexec_ prefix so the rewrite survives the IR round-trip into codegen mode.

Per-block param types come from inferring each call-site arg -- LocalVariableRead via find_var_type, literals via direct mapping (IntegerNode -> int, StringNode -> str, etc.), InstanceVariableRead via cls_ivar_type. Anything outside that strict-enumerable arg shape bails the rewrite silently and the call falls through to ordinary dispatch (which warns and emits 0). A follow-up upgrades those silent bails to hard analyze-time errors.

Adds an infer_iexec_body_call_types pass that runs alongside infer_ieval_body_call_types in analyze_phase. It walks each lifted block body with @current_class_idx set to the receiver class AND declares block params in scope with their inferred types, so bare calls inside the block propagate arg types to the receiver class's methods (Routes#get(path) learns path: str from app.instance_exec("/") { |p| get(p) }).

@iexec_return_types is locked to "void" in this commit -- expression-position usage (x = recv.instance_exec(...) { ... }) returns the recv pointer via the comma-expression hack until infer_iexec_body_return_types is wired up in a follow-up. Outer-local capture stays unsupported on the direct path here: the lifted function cannot see the caller's locals; a follow-up adds pointer capture, or convert to the trampoline pattern for capture.

test/instance_exec_direct.rb covers single-arg, two-arg, and zero-arg direct calls across single and inherited classes. CRuby reference matches the .expected output verbatim.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
e724a55bc348
Parent
602a8318620f

compiler_helpers.rbmodifiedView file

@@ -93,7 +93,7 @@
9393
9494 # ---- Compile-time class-body declaration helpers ----
9595
96- compiler_state_int :nd_count, :root_id, :analysis_frozen, :ieval_counter, :needs_gc, :needs_system
96+ compiler_state_int :nd_count, :root_id, :analysis_frozen, :ieval_counter, :iexec_counter, :needs_gc, :needs_system
9797 compiler_state_int :needs_int_array, :needs_float_array, :needs_str_array, :needs_str_int_hash, :needs_str_str_hash, :needs_int_str_hash
9898 compiler_state_int :needs_sym_int_hash, :needs_sym_str_hash, :needs_sym_intern, :needs_setjmp, :needs_mutable_str, :needs_rb_value
9999 compiler_state_int :needs_regexp, :needs_rand, :needs_stringio, :needs_lambda, :needs_fiber, :needs_bigint
@@ -116,11 +116,13 @@
116116 compiler_state_sa :sym_names, :tuple_types, :poly_funcs, :poly_param_types, :ieval_return_types, :ieval_self_param_names
117117 compiler_state_sa :ieval_extra_param_names, :compile_time_subst_keys, :compile_time_subst_vals, :cls_with_internal_ieval_lift, :toplevel_ivar_names, :toplevel_ivar_types
118118 compiler_state_sa :lambda_var_ret_names, :lambda_var_ret_types, :multi_const_inits, :meth_blk_param_types, :cls_cmeth_blk_param_types
119+ compiler_state_sa :iexec_block_pnames, :iexec_block_ptypes
119120
120121 compiler_state_ia :meth_body_ids, :meth_rest_index, :meth_kwrest_index, :meth_has_yield, :cls_rest_idxs, :cls_is_value_type
121122 compiler_state_ia :cls_is_sra, :const_expr_ids, :const_mutated, :gvar_written, :module_body_ids, :ffi_buf_sizes, :ffi_reader_offsets
122123 compiler_state_ia :dyn_regex_node_ids, :local_regex_idx, :local_regex_call_nids, :undef_class_idx, :ieval_class_idxs, :ieval_body_ids
123124 compiler_state_ia :pre_execution_blocks, :post_execution_blocks
125+ compiler_state_ia :iexec_class_idxs, :iexec_body_ids
124126
125127 def compiler_state_init_body_id; -31; end
126128 def compiler_state_dump_body_id; -32; end