Commit ad9c4fd5
ad9c4fd529b4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #93 from mame/fix-tuple-gc-scan
fix(codegen): emit GC scan function for tuples holding GC pointers
Details
- Commit
- ad9c4fd529b4
- Parent
- c98048e104bd
- Merge parent
- 650670341b24
spinel_codegen.rbmodifiedView file
@@ -3093,6 +3093,34 @@
30933093
"sp_Tuple_" + tuple_elem_types_str(t).split(",").join("_")30943094
end30953095
3096+
# Whether a tuple element type must be traced by the GC scan function.3097+
# Scalars (int/float/bool/symbol) are pure values; pointer-to-GC-object3098+
# element types must be marked, otherwise the GC frees the inner object3099+
# while the tuple keeps a dangling pointer.3100+
def tuple_field_needs_mark(et)3101+
if et == "poly"3102+
return 13103+
end3104+
if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"3105+
return 03106+
end3107+
type_is_pointer(et)3108+
end3109+
3110+
# Returns the scan function name for the tuple, or "NULL" if no field3111+
# requires marking.3112+
def tuple_scan_name(t)3113+
parts = tuple_elem_types_str(t).split(",")3114+
fi = 03115+
while fi < parts.length3116+
if tuple_field_needs_mark(parts[fi]) == 13117+
return tuple_c_name(t) + "_scan"3118+
end3119+
fi = fi + 13120+
end3121+
"NULL"3122+
end3123+
30963124
def register_tuple_type(t)30973125
if is_tuple_type(t) == 130983126
k = 0@@ -8099,6 +8127,33 @@
80998127
@deferred_tuple << " } "81008128
@deferred_tuple << name81018129
@deferred_tuple << ";\n"8130+
# GC scan function — only emit when at least one field is a GC ref.8131+
# Without this the tuple's children are collected while the tuple8132+
# itself is still alive, leaving dangling pointers in the fields.8133+
needs_scan = 08134+
fi = 08135+
while fi < parts.length8136+
if tuple_field_needs_mark(parts[fi]) == 18137+
needs_scan = 18138+
end8139+
fi = fi + 18140+
end8141+
if needs_scan == 18142+
body = ""8143+
fi = 08144+
while fi < parts.length8145+
if tuple_field_needs_mark(parts[fi]) == 18146+
field = "_t->_" + fi.to_s8147+
if parts[fi] == "poly"8148+
body = body + " sp_mark_rbval(" + field + ");"8149+
else8150+
body = body + " sp_gc_mark((void *)" + field + ");"8151+
end8152+
end8153+
fi = fi + 18154+
end8155+
@deferred_tuple << "static void " + name + "_scan(void *_p) { " + name + " *_t = (" + name + " *)_p;" + body + " }\n"8156+
end81028157
k = k + 181038158
end81048159
end@@ -14449,7 +14504,7 @@
1444914504
tname = tuple_c_name(tt)1445014505
sep = compile_arg0(nid)1445114506
tmp = new_temp14452-
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")14507+
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")1445314508
emit(" { const char *_p = strstr(" + rc + ", " + sep + ");")1445414509
emit(" if (_p) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _p - " + rc + "); " + tmp + "->_1 = " + sep + "; " + tmp + "->_2 = sp_str_substr(" + rc + ", _p - " + rc + " + strlen(" + sep + "), strlen(_p) - strlen(" + sep + ")); }")1445514510
emit(" else { " + tmp + "->_0 = " + rc + "; " + tmp + "->_1 = \"\"; " + tmp + "->_2 = \"\"; } }")@@ -14462,7 +14517,7 @@
1446214517
tname = tuple_c_name(tt)1446314518
sep = compile_arg0(nid)1446414519
tmp = new_temp14465-
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")14520+
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")1446614521
emit(" { size_t _sl = strlen(" + rc + "), _pl = strlen(" + sep + "); const char *_last = NULL;")1446714522
emit(" for (const char *_p = " + rc + "; (_p = strstr(_p, " + sep + ")); _p += _pl) _last = _p;")1446814523
emit(" if (_last) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _last - " + rc + "); " + tmp + "->_1 = " + sep + "; " + tmp + "->_2 = sp_str_substr(" + rc + ", _last - " + rc + " + _pl, _sl - (_last - " + rc + ") - _pl); }")@@ -14945,7 +15000,7 @@
1494515000
name = tuple_c_name(tt)1494615001
arg = compile_arg0(nid)1494715002
tmp = new_temp14948-
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")15003+
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")1494915004
emit(" " + tmp + "->_0 = " + rc + " / " + arg + ";")1495015005
emit(" " + tmp + "->_1 = sp_imod(" + rc + ", " + arg + ");")1495115006
return tmp@@ -15045,7 +15100,7 @@
1504515100
tname = tuple_c_name(tt)1504615101
arg = compile_arg0(nid)1504715102
tmp = new_temp15048-
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")15103+
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")1504915104
emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");")1505015105
emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";")1505115106
return tmp@@ -15124,7 +15179,7 @@
1512415179
tt = "tuple:" + parts.join(",")1512515180
register_tuple_type(tt)1512615181
tname = tuple_c_name(tt)15127-
emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")15182+
emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")1512815183
emit(" " + pair_tmp + "->_0 = sp_" + pfx_recv + "_get(" + rc + ", " + itmp + ");")1512915184
k = 01513015185
while k < arg_rcs.length@@ -15343,7 +15398,7 @@
1534315398
end1534415399
emit(" if (" + bexpr + ") sp_" + pfx + "_push(" + tmp_t + ", lv_" + bp1 + "); else sp_" + pfx + "_push(" + tmp_f + ", lv_" + bp1 + ");")1534515400
emit(" }")15346-
emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")15401+
emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")1534715402
emit(" " + tmp_res + "->_0 = " + tmp_t + ";")1534815403
emit(" " + tmp_res + "->_1 = " + tmp_f + ";")1534915404
return tmp_res@@ -15371,7 +15426,7 @@
1537115426
emit(" if (_v > " + tmp_max + ") " + tmp_max + " = _v;")1537215427
end1537315428
emit(" }")15374-
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")15429+
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")1537515430
emit(" " + tmp + "->_0 = " + tmp_min + ";")1537615431
emit(" " + tmp + "->_1 = " + tmp_max + ";")1537715432
return tmp@@ -15938,7 +15993,7 @@
1593815993
itmp = new_temp1593915994
emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")1594015995
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")15941-
emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")15996+
emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")1594215997
emit(" _tp->_0 = " + rc + "->order[" + itmp + "];")1594315998
emit(" _tp->_1 = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + itmp + "]);")1594415999
emit(" sp_PtrArray_push(" + tmp + ", _tp);")@@ -16044,7 +16099,7 @@
1604416099
itmp = new_temp1604516100
emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")1604616101
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")16047-
emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")16102+
emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")1604816103
emit(" _tp->_0 = " + rc + "->order[" + itmp + "];")1604916104
emit(" _tp->_1 = sp_StrStrHash_get(" + rc + ", " + rc + "->order[" + itmp + "]);")1605016105
emit(" sp_PtrArray_push(" + tmp + ", _tp);")@@ -17361,20 +17416,8 @@
1736117416
if is_tuple_type(arr_type) == 11736217417
name = tuple_c_name(arr_type)1736317418
tmp = new_temp17364-
has_ptr = 01736517419
parts = tuple_elem_types_str(arr_type).split(",")17366-
fi = 017367-
while fi < parts.length17368-
if type_is_pointer(parts[fi]) == 117369-
has_ptr = 117370-
end17371-
fi = fi + 117372-
end17373-
scan_fn = "NULL"17374-
if has_ptr == 117375-
scan_fn = name + "_gc_scan"17376-
end17377-
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + scan_fn + ");")17420+
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(arr_type) + ");")1737817421
k = 01737917422
while k < elems.length && k < parts.length1738017423
emit(" " + tmp + "->_" + k.to_s + " = " + compile_expr(elems[k]) + ";")test/array_partition_gc.rbaddedView file
Loading diff…