Commit f26cd7ee

f26cd7ee8ceeMerged

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

Description

fix(codegen): exclude poly-returned classes from value-type optimization

A method that returns instances of two or more value-type-eligible classes per branch (e.g. flag ? A.new(...) : B.new(...)) crashed the C compile. A and B were detected as value-type (no attr_writer / no mutator), so sp_A_new(...) returned the struct by value; the poly-return path then emitted sp_box_obj(sp_A_new(...), 0) which fed a struct-by-value into sp_RbVal.v.p (void *) — a C type error. Even if that compiled, the unboxing dispatch passed void * to functions whose receiver was declared by value.

Mirror PR #87's approach (which excluded ptr_array-stored classes from the value-type optimization) for the poly-return case:

  • New detect_poly_returned_types walks every method whose inferred return type is poly and collects every Cls.new(...) class name reachable from the body via collect_constructed_class_names (a bounded subtree walk over the conventional child slots).
  • detect_value_types consults the new exclusion list before flipping @cls_is_value_type[i] on; classes constructed in a poly-returning method stay heap-allocated, so their constructors return sp_<C> * and the existing poly-return / sp_RbVal plumbing handles them as pointers without further changes.

test/poly_return_value_class.rb covers if/else and case/ when poly returns over two distinct value-type-eligible classes, plus a method-call-on-poly variant that exercises the cls_id-based dispatch table.

Closes #118

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

Details

Commit
f26cd7ee8cee
Parent
308afd30fb1f

spinel_codegen.rbmodifiedView file

@@ -9490,6 +9490,108 @@
94909490 ""
94919491 end
94929492
9493+ # Walk `nid`'s subtree and collect every `Cls.new(...)` class name
9494+ # into `out`. Used by detect_poly_returned_types to enumerate the
9495+ # classes returned (directly or via a temp) from a poly-returning
9496+ # method body.
9497+ def collect_constructed_class_names(nid, out)
9498+ if nid < 0
9499+ return
9500+ end
9501+ if @nd_type[nid] == "CallNode"
9502+ if @nd_name[nid] == "new"
9503+ recv = @nd_receiver[nid]
9504+ if recv >= 0
9505+ cname = constructor_class_name(recv)
9506+ if cname != "" && find_class_idx(cname) >= 0
9507+ obj_t = "obj_" + cname
9508+ if not_in(obj_t, out) == 1
9509+ out.push(obj_t)
9510+ end
9511+ end
9512+ end
9513+ end
9514+ end
9515+ # Recurse into the conventional child slots.
9516+ if @nd_body[nid] >= 0
9517+ collect_constructed_class_names(@nd_body[nid], out)
9518+ end
9519+ stmts = parse_id_list(@nd_stmts[nid])
9520+ k = 0
9521+ while k < stmts.length
9522+ collect_constructed_class_names(stmts[k], out)
9523+ k = k + 1
9524+ end
9525+ if @nd_expression[nid] >= 0
9526+ collect_constructed_class_names(@nd_expression[nid], out)
9527+ end
9528+ if @nd_predicate[nid] >= 0
9529+ collect_constructed_class_names(@nd_predicate[nid], out)
9530+ end
9531+ if @nd_subsequent[nid] >= 0
9532+ collect_constructed_class_names(@nd_subsequent[nid], out)
9533+ end
9534+ if @nd_else_clause[nid] >= 0
9535+ collect_constructed_class_names(@nd_else_clause[nid], out)
9536+ end
9537+ if @nd_receiver[nid] >= 0
9538+ collect_constructed_class_names(@nd_receiver[nid], out)
9539+ end
9540+ if @nd_arguments[nid] >= 0
9541+ collect_constructed_class_names(@nd_arguments[nid], out)
9542+ end
9543+ args = parse_id_list(@nd_args[nid])
9544+ k = 0
9545+ while k < args.length
9546+ collect_constructed_class_names(args[k], out)
9547+ k = k + 1
9548+ end
9549+ if @nd_left[nid] >= 0
9550+ collect_constructed_class_names(@nd_left[nid], out)
9551+ end
9552+ if @nd_right[nid] >= 0
9553+ collect_constructed_class_names(@nd_right[nid], out)
9554+ end
9555+ end
9556+
9557+ def detect_poly_returned_types
9558+ # Find object types `obj_<C>` constructed inside a method whose
9559+ # inferred return type is `poly`. The return path boxes the value
9560+ # into an `sp_RbVal` (`void *` payload); a value-type-eligible
9561+ # class would emit `sp_box_obj(sp_<C>_new(...), ci)` which feeds a
9562+ # struct-by-value into a `void *` slot — a C type error. Excluding
9563+ # such classes from the value-type optimization keeps `<C>` heap-
9564+ # allocated, so the constructor returns `sp_<C> *` and boxing is
9565+ # well-typed. Mirrors the ptr_array exclusion (PR #87).
9566+ @poly_returned_types = "".split(",")
9567+ mi = 0
9568+ while mi < @meth_names.length
9569+ if mi < @meth_return_types.length && @meth_return_types[mi] == "poly"
9570+ bid = @meth_body_ids[mi]
9571+ if bid >= 0
9572+ collect_constructed_class_names(bid, @poly_returned_types)
9573+ end
9574+ end
9575+ mi = mi + 1
9576+ end
9577+ ci = 0
9578+ while ci < @cls_names.length
9579+ bodies = @cls_meth_bodies[ci].split(";")
9580+ returns = @cls_meth_returns[ci].split(";")
9581+ mj = 0
9582+ while mj < bodies.length
9583+ if mj < returns.length && returns[mj] == "poly"
9584+ bid = bodies[mj].to_i
9585+ if bid >= 0
9586+ collect_constructed_class_names(bid, @poly_returned_types)
9587+ end
9588+ end
9589+ mj = mj + 1
9590+ end
9591+ ci = ci + 1
9592+ end
9593+ end
9594+
94939595 def detect_ptr_array_stored_types
94949596 # Find object types `obj_<C>` that appear as the element type of an
94959597 # array literal. Such an array becomes a `sp_PtrArray *` whose
@@ -9647,6 +9749,7 @@
96479749 auto_register_attr_writers
96489750 detect_param_mutated_types
96499751 detect_ptr_array_stored_types
9752+ detect_poly_returned_types
96509753 # Multiple passes: value type detection depends on other classes
96519754 2.times do
96529755 i = 0
@@ -9714,6 +9817,20 @@
97149817 psi = psi + 1
97159818 end
97169819 end
9820+ # Exclude classes constructed inside a method whose inferred
9821+ # return type is `poly`. The poly return path boxes via
9822+ # `sp_box_obj(sp_<C>_new(...), ci)` — same struct-by-value /
9823+ # void* mismatch as the ptr_array case (issue #118).
9824+ if all_val == 1
9825+ type_str = "obj_" + @cls_names[i]
9826+ pri = 0
9827+ while pri < @poly_returned_types.length
9828+ if @poly_returned_types[pri] == type_str
9829+ all_val = 0
9830+ end
9831+ pri = pri + 1
9832+ end
9833+ end
97179834 if all_val == 1
97189835 @cls_is_value_type[i] = 1
97199836 end