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 + 1
2564225642 end
2564325643 # 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 tmp
2564725647 end
@@ -25649,10 +25649,18 @@
2564925649 # Emit branches for the built-in (negative cls_id) entries. Each
2565025650 # 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 > 0
2565525655 a0 = arg_compiled[0]
25656+ # Unbox if poly — every built-in `[]` arm here passes a0 to a
25657+ # function expecting `mrb_int` (sp_IntArray_get etc., or the
25658+ # Method fn_ptr's int parameter). When the caller's arg is
25659+ # poly (e.g. `def fetch(addr); @fetch[addr][addr]; end` with
25660+ # 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+ end
2565625664 end
2565725665 # `[]` — element types differ per built-in. When the result temp
2565825666 # 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 return
3172031728 end
31729+ if rt == "poly_array"
31730+ vbox = val
31731+ if arg_ids.length >= 2
31732+ vt = infer_type(arg_ids[1])
31733+ if vt != "poly"
31734+ vbox = box_value_to_poly(vt, val)
31735+ end
31736+ end
31737+ emit(" sp_PolyArray_set(" + rc + ", " + idx + ", " + vbox + ");")
31738+ return
31739+ end
31740+ if rt == "poly"
31741+ # Slot is sp_RbVal — runtime cls_id determines which storage
31742+ # the underlying pointer points at. Optcarrot's `@fetch[a] =
31743+ # method(:peek_X)` lands here when @fetch was widened to poly
31744+ # (rather than poly_array) by the heterogeneous IntArray + Method
31745+ # writes. Dispatch by cls_id, mirroring sp_PolyArray_set's
31746+ # interface for the POLY_ARRAY case and using element-typed
31747+ # setters for the homogeneous storage variants. Extract the
31748+ # appropriate payload from the boxed value via .v.p / .v.i.
31749+ vbox = val
31750+ vt = "int"
31751+ if arg_ids.length >= 2
31752+ vt = infer_type(arg_ids[1])
31753+ if vt != "poly"
31754+ vbox = box_value_to_poly(vt, val)
31755+ end
31756+ end
31757+ vbox_tmp = new_temp
31758+ 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+ return
31763+ end
3172131764 if rt == "str_int_hash"
3172231765 emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");")
3172331766 return