Commit d35f926a

d35f926a9d3dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

feat(codegen): support <arr>.method(:op) on built-in array types

Previously obj.method(:op) only resolved on user-class receivers; on a built-in array (@ram.method(:[]=)-style) the dispatcher fell through to the unresolved-call warning and emitted literal 0 — a real footgun for any workload that stored the captured Method into a poly-array slot, since the later m.call(...) would null-pointer-deref the fn slot.

Lowering: emit a static per-(recv_type, mname) trampoline that matches the Method dispatch ABI (void *self, mrb_int...) -> mrb_int, forwarding to the corresponding sp_<Pfx>_<op> runtime fn. The receiver's heap pointer plays the role of iv_self_obj; sp_gc_mark dispatches via the GC header's scan field set at the array's allocation, so the type lie at the C level is GC-safe.

First cut covers int_array x []/[]=/push. Other (recv_type, mname) pairs still warn until added to emit_builtin_array_method_adapter; extend as workloads need them.

Surfaced via optcarrot's CPU memory-mapping shape add_mappings(0x0000..0x07ff, @ram, @ram.method(:[]=)). The optcarrot checksum was already correct because the test ROM happens not to write through this @store path on the boot frame the harness checks; the warning was masking a real correctness gap that a different ROM would exercise.

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

Details

Commit
d35f926a9d3d
Parent
dcd875a5b754

spinel_codegen.rbmodifiedView file

@@ -2099,6 +2099,85 @@
20992099 -1
21002100 end
21012101
2102+ # `<arr>.method(:op)` for built-in array types: which (recv_type,
2103+ # mname) pairs we can lower into a Method-dispatch adapter. Limited
2104+ # to int_array's bracket ops + push for now (the optcarrot CPU
2105+ # memory-mapping shape `@ram.method(:[]=)`); extend the body to
2106+ # cover more (recv_type, mname) pairs as workloads need them.
2107+ def builtin_array_method_supported(recv_type, mname)
2108+ if recv_type == "int_array"
2109+ if mname == "[]" || mname == "[]=" || mname == "push"
2110+ return 1
2111+ end
2112+ end
2113+ 0
2114+ end
2115+
2116+ def builtin_array_type_pfx(recv_type)
2117+ if recv_type == "int_array"
2118+ return "IntArray"
2119+ end
2120+ ""
2121+ end
2122+
2123+ # Emit a per-(array_type, mname) trampoline that fits the Method
2124+ # dispatch ABI `(void *self, mrb_int...) -> mrb_int`. The body
2125+ # forwards to the corresponding `sp_<Pfx>_<op>` runtime function;
2126+ # write-style ops (`[]=`, `push`) return the rhs the way Ruby's
2127+ # bracket-write does. Idempotent: re-emitting the same key is a
2128+ # no-op. Reuses @cls_method_adapters with a `@@`-prefixed key so
2129+ # it cannot collide with a user-class method adapter entry.
2130+ def emit_builtin_array_method_adapter(recv_type, mname)
2131+ key = "@@" + recv_type + "::" + mname
2132+ k = 0
2133+ while k < @cls_method_adapters.length
2134+ if @cls_method_adapters[k] == key
2135+ return
2136+ end
2137+ k = k + 1
2138+ end
2139+ @cls_method_adapters.push(key)
2140+ pfx = builtin_array_type_pfx(recv_type)
2141+ if pfx == ""
2142+ return
2143+ end
2144+ cast = "(sp_" + pfx + " *)"
2145+ adapter_name = "sp_" + pfx + "_" + sanitize_name(mname) + "_builtin_method_adapter"
2146+ if recv_type == "int_array" && mname == "[]"
2147+ @lambda_funcs << "static mrb_int "
2148+ @lambda_funcs << adapter_name
2149+ @lambda_funcs << "(void *_self, mrb_int _i) {\n"
2150+ @lambda_funcs << " return sp_IntArray_get("
2151+ @lambda_funcs << cast
2152+ @lambda_funcs << "_self, _i);\n"
2153+ @lambda_funcs << "}\n"
2154+ return
2155+ end
2156+ if recv_type == "int_array" && mname == "[]="
2157+ @lambda_funcs << "static mrb_int "
2158+ @lambda_funcs << adapter_name
2159+ @lambda_funcs << "(void *_self, mrb_int _i, mrb_int _v) {\n"
2160+ @lambda_funcs << " sp_IntArray_set("
2161+ @lambda_funcs << cast
2162+ @lambda_funcs << "_self, _i, _v);\n"
2163+ @lambda_funcs << " return _v;\n"
2164+ @lambda_funcs << "}\n"
2165+ return
2166+ end
2167+ if recv_type == "int_array" && mname == "push"
2168+ @lambda_funcs << "static mrb_int "
2169+ @lambda_funcs << adapter_name
2170+ @lambda_funcs << "(void *_self, mrb_int _v) {\n"
2171+ @lambda_funcs << " sp_IntArray_push("
2172+ @lambda_funcs << cast
2173+ @lambda_funcs << "_self, _v);\n"
2174+ @lambda_funcs << " return _v;\n"
2175+ @lambda_funcs << "}\n"
2176+ return
2177+ end
2178+ nil
2179+ end
2180+
21022181 # Emit a one-off adapter that wraps `sp_<Klass>_cls_<mname>` so the
21032182 # Method dispatch ABI `(void *self, mrb_int...)` works on a class
21042183 # method (which has no self param). Idempotent: re-emitting the
@@ -3062,8 +3141,27 @@
30623141 if recv < 0 && @current_class_idx >= 0
30633142 return "obj_Method"
30643143 end
3065- if recv >= 0 && is_obj_type(infer_type(recv)) == 1
3066- return "obj_Method"
3144+ if recv >= 0
3145+ rt_meth = infer_type(recv)
3146+ if is_obj_type(rt_meth) == 1
3147+ return "obj_Method"
3148+ end
3149+ # `<arr>.method(:op)` on a supported built-in array type
3150+ # also produces a Method (lowered through a per-(type, op)
3151+ # adapter — see emit_builtin_array_method_adapter).
3152+ args_id_meth = @nd_arguments[nid]
3153+ if args_id_meth >= 0
3154+ arg_ids_meth = get_args(args_id_meth)
3155+ if arg_ids_meth.length >= 1
3156+ mref_meth = @nd_content[arg_ids_meth[0]]
3157+ if mref_meth == ""
3158+ mref_meth = @nd_name[arg_ids_meth[0]]
3159+ end
3160+ if builtin_array_method_supported(rt_meth, mref_meth) == 1
3161+ return "obj_Method"
3162+ end
3163+ end
3164+ end
30673165 end
30683166 end
30693167
@@ -22981,6 +23079,23 @@
2298123079 return "sp_Method_new((sp_Method *)(" + rc + "), (mrb_int)0)"
2298223080 end
2298323081 end
23082+ # Built-in array `<arr>.method(:op)`: emit a trampoline
23083+ # matching the Method dispatch ABI and bind. The receiver
23084+ # heap-allocated buffer pointer plays the role of `iv_self_obj`
23085+ # — sp_gc_mark dispatches via the GC header's scan field
23086+ # set at the array's allocation, so the type lie at the C
23087+ # level is GC-safe.
23088+ mref_b = @nd_content[arg_ids[0]]
23089+ if mref_b == ""
23090+ mref_b = @nd_name[arg_ids[0]]
23091+ end
23092+ if builtin_array_method_supported(recv_t, mref_b) == 1
23093+ emit_builtin_array_method_adapter(recv_t, mref_b)
23094+ pfx_b = builtin_array_type_pfx(recv_t)
23095+ adapter_name_b = "sp_" + pfx_b + "_" + sanitize_name(mref_b) + "_builtin_method_adapter"
23096+ rc_b = compile_expr(recv)
23097+ return "sp_Method_new((sp_Method *)(" + rc_b + "), (mrb_int)(uintptr_t)&" + adapter_name_b + ")"
23098+ end
2298423099 end
2298523100 end
2298623101 end