Commit 0418454a

0418454afe31Merged

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

Description

fix(codegen): cast recv to target class on default-arg substitution + GC-root transpose temps

Two related codegen bugs that surfaced once optcarrot's natural @apu.update (with target = @cpu.update default) and TILE_LUT.transpose paths started reaching real C compile.

  1. Default-arg substitution emits unsynthesized cast. When a callee's default arg expression references @ivar and the call site's recv has a different declared type (e.g. parent Oscillator's @apu ivar slot is mrb_int because static inference doesn't propagate through super chains, while the actual runtime value is sp_Optcarrot_APU *), the substituted (<recv>)->iv_<x> reference omits the cast — gcc rejects the -> on mrb_int. compile_args_with_defaults now casts recv_c to the target class's pointer type ((sp_<TargetCls> *) recv_c) when the recv's static type isn't already that pointer.

  2. Array#transpose codegen drops the result PtrArray and per-column IntArray on the GC. The temp PtrArray and inner IntArray were created via sp_PtrArray_new() / sp_IntArray_new() without SP_GC_ROOT. The next sp_IntArray_new() inside the inner loop could trigger sp_gc_collect, freeing the unrooted result PtrArray; the subsequent sp_PtrArray_push(result, col) then dereferenced a freed pointer. Optcarrot's TILE_LUT = [...].map { (0..7).map { (0...0x10000).map {...} }.transpose } allocates millions of ints across the transpose loop — the bug was guaranteed to fire there. Add SP_GC_ROOT for both temps.

Combined effect: all 5 remaining optcarrot spinel-compile errors resolved (4 -> errors from issue 1, 0 errors total). The spinel-compiled optcarrot binary now links and executes end-to-end on Lan_Master.nes / 180 frames.

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

Details

Commit
0418454afe31
Parent
a8d909e5a878

spinel_codegen.rbmodifiedView file

@@ -24130,10 +24130,17 @@
2413024130 jtmp = new_temp
2413124131 cols = new_temp
2413224132 emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
24133+ # GC-root the result PtrArray and the per-column IntArray. Both
24134+ # outlive the inner allocation calls (`sp_IntArray_new`,
24135+ # `sp_PtrArray_push`'s realloc) that may trigger collection. Without
24136+ # the root, an inner sp_IntArray_new could free `tmp` mid-loop and
24137+ # the next push dereferences a freed pointer.
24138+ emit(" SP_GC_ROOT(" + tmp + ");")
2413324139 emit(" if (sp_PtrArray_length(" + rc + ") > 0) {")
2413424140 emit(" mrb_int " + cols + " = sp_IntArray_length((sp_IntArray *)sp_PtrArray_get(" + rc + ", 0));")
2413524141 emit(" for (mrb_int " + jtmp + " = 0; " + jtmp + " < " + cols + "; " + jtmp + "++) {")
2413624142 emit(" sp_IntArray *" + col + " = sp_IntArray_new();")
24143+ emit(" SP_GC_ROOT(" + col + ");")
2413724144 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_PtrArray_length(" + rc + "); " + itmp + "++) {")
2413824145 emit(" sp_IntArray_push(" + col + ", sp_IntArray_get((sp_IntArray *)sp_PtrArray_get(" + rc + ", " + itmp + "), " + jtmp + "));")
2413924146 emit(" }")
@@ -27076,6 +27083,19 @@
2707627083 # `self_arrow`) or as the first arg of a same-class
2707727084 # method call (via `self_expr`).
2707827085 recv_c = compile_expr_gc_rooted(recv_for_default)
27086+ # Cast the recv expression to the target class's
27087+ # pointer type when the static type of the recv is not
27088+ # already that pointer (e.g. an inherited @apu ivar
27089+ # whose slot was inferred as mrb_int but the runtime
27090+ # value is sp_Optcarrot_APU *). Without the cast, an
27091+ # `@cpu` reference inside the default expression
27092+ # lowers to `(<recv>)->iv_cpu` which gcc rejects when
27093+ # `<recv>` is mrb_int.
27094+ recv_t = infer_type(recv_for_default)
27095+ target_obj_t = "obj_" + @cls_names[target_ci]
27096+ if base_type(recv_t) != target_obj_t && @cls_is_value_type[target_ci] != 1
27097+ recv_c = "(sp_" + @cls_names[target_ci] + " *)" + recv_c
27098+ end
2707927099 @self_override = "(" + recv_c + ")"
2708027100 @current_class_idx = target_ci
2708127101 end