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_dispatchhelper. Adding more methods or more built-in types is one moreifper (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
if mname == "to_s" return "sp_poly_to_s(" + rc + ")" end # For object method calls, dispatch based on cls_id. The result # temp is typed by the method's return type. If user classes # disagree on that type, the result is sp_RbVal and each branch # boxes its concrete return value. # For object method calls, dispatch based on cls_id. Two namespaces # of cls_id share SP_TAG_OBJ: # - non-negative: index into @cls_names (user-defined classes) # - negative SP_BUILTIN_*: built-in pointer types (IntArray, ...) # The result temp is typed by the method's return type. If user # classes disagree on that type, the result is sp_RbVal and each # branch boxes its concrete return value. ret_type = poly_dispatch_return_type(mname) is_poly_ret = ret_type == "poly" ? 1 : 0 ret_ct = c_type(ret_type) recv_tmp = new_temp emit(" sp_RbVal " + recv_tmp + " = " + rc + ";") # Compile the call's argument list once. arg_compiled = "".split(",") arg_strs = "" args_id = @nd_arguments[nid] if args_id >= 0 aargs = get_args(args_id) k = 0 while k < aargs.length arg_strs = arg_strs + ", " + compile_expr(aargs[k]) ce = compile_expr(aargs[k]) arg_compiled.push(ce) arg_strs = arg_strs + ", " + ce k = k + 1 end end tmp = new_temp emit(" " + ret_ct + " " + tmp + " = " + ret_def + ";") emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {") # User-class dispatch i = 0 while i < @cls_names.length cname = @cls_names[i] end i = i + 1 end # Built-in type dispatch (cls_id < 0). emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, tmp, is_poly_ret) emit(" }") tmp end # Emit branches for the built-in (negative cls_id) entries. Each # entry maps a (SP_BUILTIN_*, method) pair to a C expression. # Adding a new built-in type means one more `if` branch here. def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, result_tmp, is_poly_ret) a0 = "" if arg_compiled.length > 0 a0 = arg_compiled[0] end # IntArray: `[]`, `length`, `size` if mname == "[]" && arg_compiled.length >= 1 call = "sp_IntArray_get((sp_IntArray *)" + recv_tmp + ".v.p, " + a0 + ")" rhs = is_poly_ret == 1 ? "sp_box_int(" + call + ")" : call emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + result_tmp + " = " + rhs + ";") end if mname == "length" || mname == "size" call = "sp_IntArray_length((sp_IntArray *)" + recv_tmp + ".v.p)" rhs = is_poly_ret == 1 ? "sp_box_int(" + call + ")" : call emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + result_tmp + " = " + rhs + ";") end end # Try to compile str[i] <op> "c" as direct char comparison # Returns "" if not applicable def try_char_cmp(nid, c_op)test/poly_dispatch_builtin.rbaddedView file
Loading diff…