Commit 62df9eb9

62df9eb9c0eaMerged

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

Description

fix(codegen): consistent box/unbox at poly type boundaries

Three related boxing/unboxing gaps that left poly slots and int slots crossing without conversion, producing C compile errors at sites that mix the two type universes. All caught by optcarrot's heterogeneous arrays (IntArray + Method) that widen surrounding ivars / locals to poly while individual reads still produce int.

  1. Built-in array [] poly index unbox. arr[poly] on a typed receiver (int_array_method_expr etc.) emitted sp_IntArray_get(arr, poly) directly. Add compile_arg0_as_int that unboxes via .v.i when the arg's static type is poly, and use it at the [] sites for IntArray, FloatArray, StrArray, PtrArray, PolyArray plus IntArray's push.

  2. Multi-write from int_array RHS misses the box step. compile_multi_write's int_array fallback emitted self->iv_X = sp_IntArray_get(...) directly for each target without going through emit_multi_write_target (which has the slot-poly box logic). Route ivar / local targets through the helper so int RHS into a poly slot boxes via sp_box_int.

  3. Local-variable write in statement / expression context. compile_stmt and compile_expr for LocalVariableWriteNode emitted the raw RHS into the slot. When the slot was poly (widened by other writers) and the RHS scalar, the C compile complained. Box via box_value_to_poly mirroring the InstanceVariableWriteNode logic.

  4. infer_type(LocalVariableWriteNode) returns slot type. compile_cond_expr reads the expression's static type to pick between sp_poly_truthy(...) and a direct test. Without an explicit branch, if (sprite = arr[i]) (assignment-as-expression in a conditional) inferred as the RHS scalar, no truthy wrap, and the boxed assignment then yielded if (struct_value) which gcc rejects. Report the slot type so cond-expr wraps correctly.

Combined effect on optcarrot spinel-compile errors: 9676 → 5 (remaining 5 are pre-existing class-method param-type inference and one apparent << codegen bug, both unrelated to box/unbox).

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

Details

Commit
62df9eb9c0ea
Parent
a8d909e5a878

spinel_codegen.rbmodifiedView file

@@ -2034,6 +2034,19 @@
20342034 end
20352035 return "int"
20362036 end
2037+ if t == "LocalVariableWriteNode"
2038+ # `var = expr` used as an expression — the value of the
2039+ # expression is the assigned slot's value (after any boxing
2040+ # done by the LocalVariableWriteNode emit path). Reporting the
2041+ # slot type lets compile_cond_expr know to wrap with
2042+ # sp_poly_truthy when the slot is poly (e.g.
2043+ # `if (sprite = arr[i])` where sprite is a sp_RbVal local).
2044+ vt = find_var_type(@nd_name[nid])
2045+ if vt != ""
2046+ return vt
2047+ end
2048+ return infer_type(@nd_expression[nid])
2049+ end
20372050 if t == "GlobalVariableReadNode"
20382051 # `alias $copy $orig` -- a $copy read must look up $orig's
20392052 # registered type so the C codegen sees the correct format
@@ -19423,13 +19436,22 @@
1942319436 end
1942419437 if t == "LocalVariableWriteNode"
1942519438 # `local = expr` used as an expression (e.g. inside a chained
19426- # `@a = local = expr` write). Without this branch compile_expr
19427- # falls through and returns the default "0", which silently
19428- # zeroed the outer write — every `@_p_nz = ___a__ = __data__`
19429- # in optcarrots OptimizedCodeBuilder CPU output became
19430- # `iv__p_nz = 0`. Lower as a parenthesized C-style assignment.
19439+ # `@a = local = expr` write, or `if (sprite = arr[i])` in a
19440+ # conditional). Without this branch compile_expr falls through
19441+ # and returns the default "0", which silently zeroed the outer
19442+ # write — every `@_p_nz = ___a__ = __data__` in optcarrots
19443+ # OptimizedCodeBuilder CPU output became `iv__p_nz = 0`.
19444+ # Lower as a parenthesized C-style assignment, boxing scalar
19445+ # RHS into poly when the local slot is poly (mirrors compile_stmt
19446+ # InstanceVariableWriteNode logic).
1943119447 val = compile_expr(@nd_expression[nid])
19432- return "(" + fiber_var_ref(@nd_name[nid]) + " = " + val + ")"
19448+ lname2 = @nd_name[nid]
19449+ vt = find_var_type(lname2)
19450+ rhs_t = infer_type(@nd_expression[nid])
19451+ if vt == "poly" && rhs_t != "" && rhs_t != "poly"
19452+ val = box_value_to_poly(rhs_t, val)
19453+ end
19454+ return "(" + fiber_var_ref(lname2) + " = " + val + ")"
1943319455 end
1943419456 if t == "LocalVariableOperatorWriteNode"
1943519457 # `local OP= expr` used as an expression (e.g. ORA's
@@ -20028,6 +20050,27 @@
2002820050 "0"
2002920051 end
2003020052
20053+ # Like compile_arg0, but unboxes the result to mrb_int when the
20054+ # argument's static type is poly. Use at call sites that pass the
20055+ # arg directly to a C function expecting `mrb_int` (sp_IntArray_get,
20056+ # sp_IntArray_push integer-element variants, runtime helpers that
20057+ # take an int index, etc.). Without unboxing, gcc rejects passing
20058+ # `sp_RbVal` to a `mrb_int` parameter.
20059+ def compile_arg0_as_int(nid)
20060+ args_id = @nd_arguments[nid]
20061+ if args_id >= 0
20062+ arg_ids = get_args(args_id)
20063+ if arg_ids.length > 0
20064+ ce = compile_expr(arg_ids[0])
20065+ if infer_type(arg_ids[0]) == "poly"
20066+ return "(" + ce + ").v.i"
20067+ end
20068+ return ce
20069+ end
20070+ end
20071+ "0"
20072+ end
20073+
2003120074 # Like compile_arg0, but converts symbol-typed arg to const char *
2003220075 # (sp_sym_to_s wrap). Use for callsites that need a string key.
2003320076 def compile_str_arg0(nid)
@@ -23799,10 +23842,10 @@
2379923842 return "sp_IntArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2380023843 end
2380123844 end
23802- return "sp_IntArray_get(" + rc + ", " + compile_arg0(nid) + ")"
23845+ return "sp_IntArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2380323846 end
2380423847 if mname == "push"
23805- return "(sp_IntArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
23848+ return "(sp_IntArray_push(" + rc + ", " + compile_arg0_as_int(nid) + "), 0)"
2380623849 end
2380723850 if mname == "pop"
2380823851 return "sp_IntArray_pop(" + rc + ")"
@@ -24059,7 +24102,7 @@
2405924102 return "sp_FloatArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2406024103 end
2406124104 end
24062- return "sp_FloatArray_get(" + rc + ", " + compile_arg0(nid) + ")"
24105+ return "sp_FloatArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2406324106 end
2406424107 if mname == "push"
2406524108 return "(sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
@@ -24111,7 +24154,7 @@
2411124154 return "sp_PtrArray_length(" + rc + ")"
2411224155 end
2411324156 if mname == "[]"
24114- return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0(nid) + "))"
24157+ return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + "))"
2411524158 end
2411624159 if mname == "push"
2411724160 return "(sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
@@ -24171,7 +24214,7 @@
2417124214 return "sp_StrArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2417224215 end
2417324216 end
24174- return "sp_StrArray_get(" + rc + ", " + compile_arg0(nid) + ")"
24217+ return "sp_StrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2417524218 end
2417624219 if mname == "first"
2417724220 return "sp_StrArray_get(" + rc + ", 0)"
@@ -24247,7 +24290,7 @@
2424724290 return "sp_PolyArray_length(" + rc + ")"
2424824291 end
2424924292 if mname == "[]"
24250- return "sp_PolyArray_get(" + rc + ", " + compile_arg0(nid) + ")"
24293+ return "sp_PolyArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2425124294 end
2425224295 if mname == "push"
2425324296 arg_id = -1
@@ -27767,6 +27810,13 @@
2776727810 emit(" " + vref + " = NULL;")
2776827811 else
2776927812 val = compile_expr(@nd_expression[nid])
27813+ # Box scalar RHS into poly when the local slot is poly. Without
27814+ # this, `lv = sp_IntArray_get(...)` (int RHS) into a sp_RbVal
27815+ # slot fails C compile. Mirrors the InstanceVariableWriteNode
27816+ # path's box-when-slot-is-poly logic.
27817+ if vt == "poly" && rhs_t != "" && rhs_t != "poly"
27818+ val = box_value_to_poly(rhs_t, val)
27819+ end
2777027820 emit(" " + vref + " = " + val + ";")
2777127821 end
2777227822 if rhs_t != "nil" || is_nullable_type(vt) == 0
@@ -28524,11 +28574,15 @@
2852428574 while k < targets.length
2852528575 tid = targets[k]
2852628576 rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
28577+ # Route ivar / local writes through emit_multi_write_target so
28578+ # the box-when-slot-is-poly logic fires. Without boxing, an int
28579+ # RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where
28580+ # @y is poly because other writers store other types).
2852728581 if @nd_type[tid] == "LocalVariableTargetNode"
28528- emit(" " + fiber_var_ref(@nd_name[tid]) + " = " + rhs + ";")
28582+ emit_multi_write_target(tid, rhs, "int")
2852928583 end
2853028584 if @nd_type[tid] == "InstanceVariableTargetNode"
28531- emit(" " + self_arrow + sanitize_ivar(@nd_name[tid]) + " = " + rhs + ";")
28585+ emit_multi_write_target(tid, rhs, "int")
2853228586 end
2853328587 if @nd_type[tid] == "ConstantTargetNode"
2853428588 if find_const_idx(@nd_name[tid]) >= 0