Commit 4e598bc1
4e598bc18818Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #331 from mame/pr/poly-recv-bracket-assign
fix(codegen): dispatch poly-recv arr[i] = v by runtime cls_id
Details
- Commit
- 4e598bc18818
- Parent
- 56d6290e6c19
- Merge parent
- f311b8dbe59e
spinel_codegen.rbmodifiedView file
@@ -25641,7 +25641,7 @@
2564125641
i = i + 12564225642
end2564325643
# Built-in type dispatch (cls_id < 0).25644-
emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, tmp, is_poly_ret)25644+
emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, tmp, is_poly_ret)2564525645
emit(" }")2564625646
tmp2564725647
end@@ -25649,10 +25649,18 @@
2564925649
# Emit branches for the built-in (negative cls_id) entries. Each2565025650
# entry maps a (SP_BUILTIN_*, method) pair to a C expression.2565125651
# Adding a new built-in type means one more `if` branch here.25652-
def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, result_tmp, is_poly_ret)25652+
def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, result_tmp, is_poly_ret)2565325653
a0 = ""2565425654
if arg_compiled.length > 02565525655
a0 = arg_compiled[0]25656+
# Unbox if poly — every built-in `[]` arm here passes a0 to a25657+
# function expecting `mrb_int` (sp_IntArray_get etc., or the25658+
# Method fn_ptr's int parameter). When the caller's arg is25659+
# poly (e.g. `def fetch(addr); @fetch[addr][addr]; end` with25660+
# addr widened), we must unbox before the C call.25661+
if arg_types.length > 0 && arg_types[0] == "poly"25662+
a0 = "(" + a0 + ").v.i"25663+
end2565625664
end2565725665
# `[]` — element types differ per built-in. When the result temp2565825666
# is sp_RbVal (poly return), every branch can box into it. When@@ -31718,6 +31726,41 @@
3171831726
emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");")3171931727
return3172031728
end31729+
if rt == "poly_array"31730+
vbox = val31731+
if arg_ids.length >= 231732+
vt = infer_type(arg_ids[1])31733+
if vt != "poly"31734+
vbox = box_value_to_poly(vt, val)31735+
end31736+
end31737+
emit(" sp_PolyArray_set(" + rc + ", " + idx + ", " + vbox + ");")31738+
return31739+
end31740+
if rt == "poly"31741+
# Slot is sp_RbVal — runtime cls_id determines which storage31742+
# the underlying pointer points at. Optcarrot's `@fetch[a] =31743+
# method(:peek_X)` lands here when @fetch was widened to poly31744+
# (rather than poly_array) by the heterogeneous IntArray + Method31745+
# writes. Dispatch by cls_id, mirroring sp_PolyArray_set's31746+
# interface for the POLY_ARRAY case and using element-typed31747+
# setters for the homogeneous storage variants. Extract the31748+
# appropriate payload from the boxed value via .v.p / .v.i.31749+
vbox = val31750+
vt = "int"31751+
if arg_ids.length >= 231752+
vt = infer_type(arg_ids[1])31753+
if vt != "poly"31754+
vbox = box_value_to_poly(vt, val)31755+
end31756+
end31757+
vbox_tmp = new_temp31758+
emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";")31759+
emit(" if (" + rc + ".cls_id == SP_BUILTIN_POLY_ARRAY) sp_PolyArray_set((sp_PolyArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ");")31760+
emit(" else if (" + rc + ".cls_id == SP_BUILTIN_PTR_ARRAY) sp_PtrArray_set((sp_PtrArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.p);")31761+
emit(" else if (" + rc + ".cls_id == SP_BUILTIN_INT_ARRAY) sp_IntArray_set((sp_IntArray *)" + rc + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")31762+
return31763+
end3172131764
if rt == "str_int_hash"3172231765
emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");")3172331766
returntest/poly_recv_bracket_assign.rbaddedView file
Loading diff…
test/poly_recv_bracket_assign.rb.expectedaddedView file
Loading diff…