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 end
30953095
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-object
3098+ # element types must be marked, otherwise the GC frees the inner object
3099+ # while the tuple keeps a dangling pointer.
3100+ def tuple_field_needs_mark(et)
3101+ if et == "poly"
3102+ return 1
3103+ end
3104+ if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"
3105+ return 0
3106+ end
3107+ type_is_pointer(et)
3108+ end
3109+
3110+ # Returns the scan function name for the tuple, or "NULL" if no field
3111+ # requires marking.
3112+ def tuple_scan_name(t)
3113+ parts = tuple_elem_types_str(t).split(",")
3114+ fi = 0
3115+ while fi < parts.length
3116+ if tuple_field_needs_mark(parts[fi]) == 1
3117+ return tuple_c_name(t) + "_scan"
3118+ end
3119+ fi = fi + 1
3120+ end
3121+ "NULL"
3122+ end
3123+
30963124 def register_tuple_type(t)
30973125 if is_tuple_type(t) == 1
30983126 k = 0
@@ -8099,6 +8127,33 @@
80998127 @deferred_tuple << " } "
81008128 @deferred_tuple << name
81018129 @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 tuple
8132+ # itself is still alive, leaving dangling pointers in the fields.
8133+ needs_scan = 0
8134+ fi = 0
8135+ while fi < parts.length
8136+ if tuple_field_needs_mark(parts[fi]) == 1
8137+ needs_scan = 1
8138+ end
8139+ fi = fi + 1
8140+ end
8141+ if needs_scan == 1
8142+ body = ""
8143+ fi = 0
8144+ while fi < parts.length
8145+ if tuple_field_needs_mark(parts[fi]) == 1
8146+ field = "_t->_" + fi.to_s
8147+ if parts[fi] == "poly"
8148+ body = body + " sp_mark_rbval(" + field + ");"
8149+ else
8150+ body = body + " sp_gc_mark((void *)" + field + ");"
8151+ end
8152+ end
8153+ fi = fi + 1
8154+ end
8155+ @deferred_tuple << "static void " + name + "_scan(void *_p) { " + name + " *_t = (" + name + " *)_p;" + body + " }\n"
8156+ end
81028157 k = k + 1
81038158 end
81048159 end
@@ -14449,7 +14504,7 @@
1444914504 tname = tuple_c_name(tt)
1445014505 sep = compile_arg0(nid)
1445114506 tmp = new_temp
14452- 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_temp
14465- 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_temp
14948- 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_temp
15048- 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 = 0
1513015185 while k < arg_rcs.length
@@ -15343,7 +15398,7 @@
1534315398 end
1534415399 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 end
1537315428 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_temp
1593915994 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_temp
1604516100 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) == 1
1736217417 name = tuple_c_name(arr_type)
1736317418 tmp = new_temp
17364- has_ptr = 0
1736517419 parts = tuple_elem_types_str(arr_type).split(",")
17366- fi = 0
17367- while fi < parts.length
17368- if type_is_pointer(parts[fi]) == 1
17369- has_ptr = 1
17370- end
17371- fi = fi + 1
17372- end
17373- scan_fn = "NULL"
17374- if has_ptr == 1
17375- scan_fn = name + "_gc_scan"
17376- end
17377- 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 = 0
1737917422 while k < elems.length && k < parts.length
1738017423 emit(" " + tmp + "->_" + k.to_s + " = " + compile_expr(elems[k]) + ";")