Commit 74799378

7479937893afMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): dispatch poly method calls on built-in (negative cls_id) types

compile_poly_method_call only emitted if (cls_id == N) branches for user classes. For a poly-typed parameter that received a built-in value (e.g. an Array literal boxed as SP_BUILTIN_INT_ARRAY), no branch matched and the result temp held its default value — so this program silently printed 0 instead of 100:

class Foo def ; 42; end end def read0(a); a[0]; end puts read0(Foo.new) # 42 user class puts read0([100, 200, 300]) # 100 IntArray, used to be 0

Split the dispatcher into two halves under if (tag == SP_TAG_OBJ):

  • User-class branches (cls_id >= 0, indices into @cls_names) — unchanged.
  • Built-in branches (cls_id < 0, SP_BUILTIN_*) — emitted by a new emit_poly_builtin_dispatch helper. Adding more methods or more built-in types is one more if per (type, method) pair.

The return-type lookup is also factored into poly_method_return_type so the built-in branch can contribute fallback types ([], length, size are int) when no user class defines the method.

Adds test/poly_dispatch_builtin.rb as a small reproducer.

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

Details

Commit
7479937893af
Parent
7839674216ed

spinel_codegen.rbmodifiedView file

@@ -15883,10 +15883,13 @@
1588315883 if mname == "to_s"
1588415884 return "sp_poly_to_s(" + rc + ")"
1588515885 end
15886- # For object method calls, dispatch based on cls_id. The result
15887- # temp is typed by the method's return type. If user classes
15888- # disagree on that type, the result is sp_RbVal and each branch
15889- # boxes its concrete return value.
15886+ # For object method calls, dispatch based on cls_id. Two namespaces
15887+ # of cls_id share SP_TAG_OBJ:
15888+ # - non-negative: index into @cls_names (user-defined classes)
15889+ # - negative SP_BUILTIN_*: built-in pointer types (IntArray, ...)
15890+ # The result temp is typed by the method's return type. If user
15891+ # classes disagree on that type, the result is sp_RbVal and each
15892+ # branch boxes its concrete return value.
1589015893 ret_type = poly_dispatch_return_type(mname)
1589115894 is_poly_ret = ret_type == "poly" ? 1 : 0
1589215895 ret_ct = c_type(ret_type)
@@ -15896,19 +15899,23 @@
1589615899 recv_tmp = new_temp
1589715900 emit(" sp_RbVal " + recv_tmp + " = " + rc + ";")
1589815901 # Compile the call's argument list once.
15902+ arg_compiled = "".split(",")
1589915903 arg_strs = ""
1590015904 args_id = @nd_arguments[nid]
1590115905 if args_id >= 0
1590215906 aargs = get_args(args_id)
1590315907 k = 0
1590415908 while k < aargs.length
15905- arg_strs = arg_strs + ", " + compile_expr(aargs[k])
15909+ ce = compile_expr(aargs[k])
15910+ arg_compiled.push(ce)
15911+ arg_strs = arg_strs + ", " + ce
1590615912 k = k + 1
1590715913 end
1590815914 end
1590915915 tmp = new_temp
1591015916 emit(" " + ret_ct + " " + tmp + " = " + ret_def + ";")
1591115917 emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {")
15918+ # User-class dispatch
1591215919 i = 0
1591315920 while i < @cls_names.length
1591415921 cname = @cls_names[i]
@@ -15924,10 +15931,33 @@
1592415931 end
1592515932 i = i + 1
1592615933 end
15934+ # Built-in type dispatch (cls_id < 0).
15935+ emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, tmp, is_poly_ret)
1592715936 emit(" }")
1592815937 tmp
1592915938 end
1593015939
15940+ # Emit branches for the built-in (negative cls_id) entries. Each
15941+ # entry maps a (SP_BUILTIN_*, method) pair to a C expression.
15942+ # Adding a new built-in type means one more `if` branch here.
15943+ def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, result_tmp, is_poly_ret)
15944+ a0 = ""
15945+ if arg_compiled.length > 0
15946+ a0 = arg_compiled[0]
15947+ end
15948+ # IntArray: `[]`, `length`, `size`
15949+ if mname == "[]" && arg_compiled.length >= 1
15950+ call = "sp_IntArray_get((sp_IntArray *)" + recv_tmp + ".v.p, " + a0 + ")"
15951+ rhs = is_poly_ret == 1 ? "sp_box_int(" + call + ")" : call
15952+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + result_tmp + " = " + rhs + ";")
15953+ end
15954+ if mname == "length" || mname == "size"
15955+ call = "sp_IntArray_length((sp_IntArray *)" + recv_tmp + ".v.p)"
15956+ rhs = is_poly_ret == 1 ? "sp_box_int(" + call + ")" : call
15957+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + result_tmp + " = " + rhs + ";")
15958+ end
15959+ end
15960+
1593115961 # Try to compile str[i] <op> "c" as direct char comparison
1593215962 # Returns "" if not applicable
1593315963 def try_char_cmp(nid, c_op)