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.
-
Default-arg substitution emits unsynthesized cast. When a callee's default arg expression references
@ivarand the call site's recv has a different declared type (e.g. parentOscillator's@apuivar slot ismrb_intbecause static inference doesn't propagate throughsuperchains, while the actual runtime value issp_Optcarrot_APU *), the substituted(<recv>)->iv_<x>reference omits the cast — gcc rejects the->onmrb_int.compile_args_with_defaultsnow castsrecv_cto the target class's pointer type ((sp_<TargetCls> *) recv_c) when the recv's static type isn't already that pointer. -
Array#transposecodegen drops the result PtrArray and per-column IntArray on the GC. The temp PtrArray and inner IntArray were created viasp_PtrArray_new()/sp_IntArray_new()withoutSP_GC_ROOT. The nextsp_IntArray_new()inside the inner loop could triggersp_gc_collect, freeing the unrooted result PtrArray; the subsequentsp_PtrArray_push(result, col)then dereferenced a freed pointer. Optcarrot'sTILE_LUT = [...].map { (0..7).map { (0...0x10000).map {...} }.transpose }allocates millions of ints across the transpose loop — the bug was guaranteed to fire there. AddSP_GC_ROOTfor 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
jtmp = new_temp cols = new_temp emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();") # GC-root the result PtrArray and the per-column IntArray. Both # outlive the inner allocation calls (`sp_IntArray_new`, # `sp_PtrArray_push`'s realloc) that may trigger collection. Without # the root, an inner sp_IntArray_new could free `tmp` mid-loop and # the next push dereferences a freed pointer. emit(" SP_GC_ROOT(" + tmp + ");") emit(" if (sp_PtrArray_length(" + rc + ") > 0) {") emit(" mrb_int " + cols + " = sp_IntArray_length((sp_IntArray *)sp_PtrArray_get(" + rc + ", 0));") emit(" for (mrb_int " + jtmp + " = 0; " + jtmp + " < " + cols + "; " + jtmp + "++) {") emit(" sp_IntArray *" + col + " = sp_IntArray_new();") emit(" SP_GC_ROOT(" + col + ");") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_PtrArray_length(" + rc + "); " + itmp + "++) {") emit(" sp_IntArray_push(" + col + ", sp_IntArray_get((sp_IntArray *)sp_PtrArray_get(" + rc + ", " + itmp + "), " + jtmp + "));") emit(" }") # `self_arrow`) or as the first arg of a same-class # method call (via `self_expr`). recv_c = compile_expr_gc_rooted(recv_for_default) # Cast the recv expression to the target class's # pointer type when the static type of the recv is not # already that pointer (e.g. an inherited @apu ivar # whose slot was inferred as mrb_int but the runtime # value is sp_Optcarrot_APU *). Without the cast, an # `@cpu` reference inside the default expression # lowers to `(<recv>)->iv_cpu` which gcc rejects when # `<recv>` is mrb_int. recv_t = infer_type(recv_for_default) target_obj_t = "obj_" + @cls_names[target_ci] if base_type(recv_t) != target_obj_t && @cls_is_value_type[target_ci] != 1 recv_c = "(sp_" + @cls_names[target_ci] + " *)" + recv_c end @self_override = "(" + recv_c + ")" @current_class_idx = target_ci endtest/transpose_gc_root.rbaddedView file
Loading diff…
test/transpose_gc_root.rb.expectedaddedView file
Loading diff…