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.
-
Built-in array
[]poly index unbox.arr[poly]on a typed receiver (int_array_method_expretc.) emittedsp_IntArray_get(arr, poly)directly. Addcompile_arg0_as_intthat unboxes via.v.iwhen the arg's static type is poly, and use it at the[]sites for IntArray, FloatArray, StrArray, PtrArray, PolyArray plus IntArray'spush. -
Multi-write from int_array RHS misses the box step.
compile_multi_write's int_array fallback emittedself->iv_X = sp_IntArray_get(...)directly for each target without going throughemit_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 viasp_box_int. -
Local-variable write in statement / expression context.
compile_stmtandcompile_exprforLocalVariableWriteNodeemitted the raw RHS into the slot. When the slot was poly (widened by other writers) and the RHS scalar, the C compile complained. Box viabox_value_to_polymirroring the InstanceVariableWriteNode logic. -
infer_type(LocalVariableWriteNode)returns slot type.compile_cond_exprreads the expression's static type to pick betweensp_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 yieldedif (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
end return "int" end if t == "LocalVariableWriteNode" # `var = expr` used as an expression — the value of the # expression is the assigned slot's value (after any boxing # done by the LocalVariableWriteNode emit path). Reporting the # slot type lets compile_cond_expr know to wrap with # sp_poly_truthy when the slot is poly (e.g. # `if (sprite = arr[i])` where sprite is a sp_RbVal local). vt = find_var_type(@nd_name[nid]) if vt != "" return vt end return infer_type(@nd_expression[nid]) end if t == "GlobalVariableReadNode" # `alias $copy $orig` -- a $copy read must look up $orig's # registered type so the C codegen sees the correct format end if t == "LocalVariableWriteNode" # `local = expr` used as an expression (e.g. inside a chained # `@a = local = expr` write). Without this branch compile_expr # falls through and returns the default "0", which silently # zeroed the outer write — every `@_p_nz = ___a__ = __data__` # in optcarrots OptimizedCodeBuilder CPU output became # `iv__p_nz = 0`. Lower as a parenthesized C-style assignment. # `@a = local = expr` write, or `if (sprite = arr[i])` in a # conditional). Without this branch compile_expr falls through # and returns the default "0", which silently zeroed the outer # write — every `@_p_nz = ___a__ = __data__` in optcarrots # OptimizedCodeBuilder CPU output became `iv__p_nz = 0`. # Lower as a parenthesized C-style assignment, boxing scalar # RHS into poly when the local slot is poly (mirrors compile_stmt # InstanceVariableWriteNode logic). val = compile_expr(@nd_expression[nid]) return "(" + fiber_var_ref(@nd_name[nid]) + " = " + val + ")" lname2 = @nd_name[nid] vt = find_var_type(lname2) rhs_t = infer_type(@nd_expression[nid]) if vt == "poly" && rhs_t != "" && rhs_t != "poly" val = box_value_to_poly(rhs_t, val) end return "(" + fiber_var_ref(lname2) + " = " + val + ")" end if t == "LocalVariableOperatorWriteNode" # `local OP= expr` used as an expression (e.g. ORA's "0" end # Like compile_arg0, but unboxes the result to mrb_int when the # argument's static type is poly. Use at call sites that pass the # arg directly to a C function expecting `mrb_int` (sp_IntArray_get, # sp_IntArray_push integer-element variants, runtime helpers that # take an int index, etc.). Without unboxing, gcc rejects passing # `sp_RbVal` to a `mrb_int` parameter. def compile_arg0_as_int(nid) args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) if arg_ids.length > 0 ce = compile_expr(arg_ids[0]) if infer_type(arg_ids[0]) == "poly" return "(" + ce + ").v.i" end return ce end end "0" end # Like compile_arg0, but converts symbol-typed arg to const char * # (sp_sym_to_s wrap). Use for callsites that need a string key. def compile_str_arg0(nid) return "sp_IntArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")" end end return "sp_IntArray_get(" + rc + ", " + compile_arg0(nid) + ")" return "sp_IntArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")" end if mname == "push" return "(sp_IntArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)" return "(sp_IntArray_push(" + rc + ", " + compile_arg0_as_int(nid) + "), 0)" end if mname == "pop" return "sp_IntArray_pop(" + rc + ")" return "sp_FloatArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")" end end return "sp_FloatArray_get(" + rc + ", " + compile_arg0(nid) + ")" return "sp_FloatArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")" end if mname == "push" return "(sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)" return "sp_PtrArray_length(" + rc + ")" end if mname == "[]" return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0(nid) + "))" return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + "))" end if mname == "push" return "(sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)" return "sp_StrArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")" end end return "sp_StrArray_get(" + rc + ", " + compile_arg0(nid) + ")" return "sp_StrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")" end if mname == "first" return "sp_StrArray_get(" + rc + ", 0)" return "sp_PolyArray_length(" + rc + ")" end if mname == "[]" return "sp_PolyArray_get(" + rc + ", " + compile_arg0(nid) + ")" return "sp_PolyArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")" end if mname == "push" arg_id = -1 emit(" " + vref + " = NULL;") else val = compile_expr(@nd_expression[nid]) # Box scalar RHS into poly when the local slot is poly. Without # this, `lv = sp_IntArray_get(...)` (int RHS) into a sp_RbVal # slot fails C compile. Mirrors the InstanceVariableWriteNode # path's box-when-slot-is-poly logic. if vt == "poly" && rhs_t != "" && rhs_t != "poly" val = box_value_to_poly(rhs_t, val) end emit(" " + vref + " = " + val + ";") end if rhs_t != "nil" || is_nullable_type(vt) == 0 while k < targets.length tid = targets[k] rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")" # Route ivar / local writes through emit_multi_write_target so # the box-when-slot-is-poly logic fires. Without boxing, an int # RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where # @y is poly because other writers store other types). if @nd_type[tid] == "LocalVariableTargetNode" emit(" " + fiber_var_ref(@nd_name[tid]) + " = " + rhs + ";") emit_multi_write_target(tid, rhs, "int") end if @nd_type[tid] == "InstanceVariableTargetNode" emit(" " + self_arrow + sanitize_ivar(@nd_name[tid]) + " = " + rhs + ";") emit_multi_write_target(tid, rhs, "int") end if @nd_type[tid] == "ConstantTargetNode" if find_const_idx(@nd_name[tid]) >= 0test/box_unbox_local_assign_cond.rbaddedView file
Loading diff…
test/box_unbox_local_assign_cond.rb.expectedaddedView file
Loading diff…