Commit b2de8cb9

b2de8cb93610Merged

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

Description

Merge pull request #334 from mame/pr/default-arg-cast-and-transpose-gc

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

Details

Commit
b2de8cb93610
Parent
a8d909e5a878
Merge parent
0418454afe31

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