Commit 4b438d05

4b438d05e7d2Merged

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

Description

Merge pull request #670 from fc-anjos/codex/fix-value-type-gc

Fix value-type GC tracing

Details

Commit
4b438d05e7d2
Parent
0fbdf33a3bd3
Merge parent
2238b26d2f8f

spinel_codegen.rbmodifiedView file

@@ -3625,13 +3625,10 @@
36253625 # element types must be marked, otherwise the GC frees the inner object
36263626 # while the tuple keeps a dangling pointer.
36273627 def tuple_field_needs_mark(et)
3628- if et == "poly"
3628+ if gc_trace_kind(et) != "none"
36293629 return 1
36303630 end
3631- if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"
3632- return 0
3633- end
3634- type_is_pointer(et)
3631+ 0
36353632 end
36363633
36373634 # Returns the scan function name for the tuple, or "NULL" if no field
@@ -5026,8 +5023,12 @@
50265023 emit(" SP_GC_SAVE();")
50275024 @in_gc_scope = 1
50285025 end
5029- if @needs_gc == 1 && @cls_is_value_type[ci] == 0
5030- emit(" SP_GC_ROOT(self);")
5026+ if @needs_gc == 1
5027+ if @cls_is_value_type[ci] == 0
5028+ emit(" SP_GC_ROOT(self);")
5029+ else
5030+ emit_gc_root_for_expr("self", "obj_" + cname)
5031+ end
50315032 end
50325033 compile_body_return(bid, ret_type)
50335034 end
@@ -7295,6 +7296,7 @@
72957296 # forward declaration.
72967297 emit_class_runtime
72977298 emit_class_structs
7299+ emit_gc_scan_function_prototypes
72987300 emit_raw("/*TUPLE_INSERT_POINT*/")
72997301 emit_gc_scan_functions
73007302 emit_toplevel_ivar_decls
@@ -7380,10 +7382,9 @@
73807382 while fi < parts.length
73817383 if tuple_field_needs_mark(parts[fi]) == 1
73827384 field = "_t->_" + fi.to_s
7383- if parts[fi] == "poly"
7384- body = body + " sp_mark_rbval(" + field + ");"
7385- else
7386- body = body + " sp_gc_mark((void *)" + field + ");"
7385+ stmt = gc_mark_stmt_for_expr(field, parts[fi])
7386+ if stmt != ""
7387+ body = body + " " + stmt
73877388 end
73887389 end
73897390 fi = fi + 1
@@ -8671,13 +8672,133 @@
86718672 0
86728673 end
86738674
8675+ def ivar_needs_gc_mark(t)
8676+ if gc_trace_kind(t) != "none"
8677+ return 1
8678+ end
8679+ 0
8680+ end
8681+
8682+ def value_type_scan_class_name(t)
8683+ bt = base_type(t)
8684+ if is_value_type_obj(bt) == 1
8685+ return bt[4, bt.length - 4]
8686+ end
8687+ ""
8688+ end
8689+
8690+ def value_type_needs_gc_scan(t)
8691+ cname = value_type_scan_class_name(t)
8692+ if cname == ""
8693+ return 0
8694+ end
8695+ ci = find_class_idx(cname)
8696+ if ci >= 0 && class_has_ptr_ivars(ci) == 1
8697+ return 1
8698+ end
8699+ 0
8700+ end
8701+
8702+ def gc_trace_kind(t)
8703+ bt = base_type(t)
8704+ if bt == "poly"
8705+ return "poly"
8706+ end
8707+ if value_type_needs_gc_scan(bt) == 1
8708+ return "value_type"
8709+ end
8710+ if type_is_pointer(bt) == 1
8711+ return "ptr"
8712+ end
8713+ "none"
8714+ end
8715+
8716+ def emit_gc_root_line(stmt, raw)
8717+ if raw == 1
8718+ emit_raw(" " + stmt)
8719+ else
8720+ emit(" " + stmt)
8721+ end
8722+ 0
8723+ end
8724+
8725+ def emit_value_type_field_roots(expr, t, raw, global)
8726+ cname = value_type_scan_class_name(t)
8727+ if cname == ""
8728+ return
8729+ end
8730+ ci = find_class_idx(cname)
8731+ if ci < 0
8732+ return
8733+ end
8734+ names = @cls_ivar_names[ci].split(";")
8735+ types = @cls_ivar_types[ci].split(";")
8736+ j = 0
8737+ while j < names.length
8738+ if j < types.length
8739+ emit_gc_root_for_expr_impl(expr + "." + sanitize_ivar(names[j]), types[j], raw, global)
8740+ end
8741+ j = j + 1
8742+ end
8743+ 0
8744+ end
8745+
8746+ def emit_gc_root_for_expr_impl(expr, t, raw, global)
8747+ kind = gc_trace_kind(t)
8748+ if kind == "ptr"
8749+ if global == 1
8750+ emit_gc_root_line("_sp_gc_root_push((void**)&" + expr + ");", raw)
8751+ else
8752+ emit_gc_root_line("SP_GC_ROOT(" + expr + ");", raw)
8753+ end
8754+ elsif kind == "value_type"
8755+ emit_value_type_field_roots(expr, t, raw, global)
8756+ end
8757+ 0
8758+ end
8759+
8760+ def gc_mark_stmt_for_expr(expr, t)
8761+ kind = gc_trace_kind(t)
8762+ if kind == "poly"
8763+ return "sp_mark_rbval(" + expr + ");"
8764+ end
8765+ if kind == "value_type"
8766+ cname = value_type_scan_class_name(t)
8767+ return "sp_" + cname + "_gc_scan(&" + expr + ");"
8768+ end
8769+ if kind == "ptr"
8770+ return "if (" + expr + ") sp_gc_mark((void *)" + expr + ");"
8771+ end
8772+ ""
8773+ end
8774+
8775+ def emit_gc_root_for_expr(expr, t)
8776+ emit_gc_root_for_expr_impl(expr, t, 0, 0)
8777+ end
8778+
8779+ def emit_raw_gc_root_for_expr(expr, t)
8780+ emit_gc_root_for_expr_impl(expr, t, 1, 0)
8781+ end
8782+
8783+ def emit_global_gc_roots_for_expr(expr, t)
8784+ emit_gc_root_for_expr_impl(expr, t, 0, 1)
8785+ end
8786+
8787+ def type_needs_gc_root(t)
8788+ kind = gc_trace_kind(t)
8789+ if kind == "ptr" || kind == "value_type"
8790+ return 1
8791+ end
8792+ 0
8793+ end
8794+
86748795 def class_has_ptr_ivars(ci)
86758796 names = @cls_ivar_names[ci].split(";")
86768797 types = @cls_ivar_types[ci].split(";")
86778798 j = 0
86788799 while j < names.length
86798800 if j < types.length
8680- if ivar_is_gc_ptr(types[j]) == 1
8801+ if ivar_needs_gc_mark(types[j]) == 1
86818802 return 1
86828803 end
86838804 end
@@ -8692,8 +8813,7 @@
86928813 0
86938814 end
86948815
8695- # Emit `self->iv_<name> = NULL;` / `sp_box_nil();` for each
8696- # GC-traced ivar on the class chain. Called right after
8816+ # Clear each GC-traced field on the class chain. Called right after
86978817 # SP_POOL_NEW + SP_GC_ROOT(self) to defuse the recycled-slot
86988818 # stale-pointer hazard: if a sub-allocation in the initialize
86998819 # body triggers GC before the body writes every pointer ivar,
@@ -8715,13 +8835,14 @@
87158835 j = 0
87168836 while j < names.length
87178837 if j < types.length
8718- if ivar_is_gc_ptr(types[j]) == 1
8719- field = "self->" + sanitize_ivar(names[j])
8720- if types[j] == "poly"
8721- emit_raw(" " + field + " = sp_box_nil();")
8722- else
8723- emit_raw(" " + field + " = NULL;")
8724- end
8838+ kind = gc_trace_kind(types[j])
8839+ field = "self->" + sanitize_ivar(names[j])
8840+ if kind == "poly"
8841+ emit_raw(" " + field + " = sp_box_nil();")
8842+ elsif kind == "ptr"
8843+ emit_raw(" " + field + " = NULL;")
8844+ elsif kind == "value_type"
8845+ emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};")
87258846 end
87268847 end
87278848 j = j + 1
@@ -8729,10 +8850,24 @@
87298850 end
87308851
87318852 def emit_gc_mark_ivar(field, t)
8732- if t == "poly"
8733- emit_raw(" sp_mark_rbval(" + field + ");")
8734- else
8735- emit_raw(" if (" + field + ") sp_gc_mark((void *)" + field + ");")
8853+ stmt = gc_mark_stmt_for_expr(field, t)
8854+ if stmt != ""
8855+ emit_raw(" " + stmt)
8856+ end
8857+ end
8858+
8859+ def emit_gc_scan_function_prototypes
8860+ emitted_proto = 0
8861+ i = 0
8862+ while i < @cls_names.length
8863+ if cls_emit_skipped(i) == 0 && class_has_ptr_ivars(i) == 1
8864+ emit_raw("static void sp_" + @cls_names[i] + "_gc_scan(void *p);")
8865+ emitted_proto = 1
8866+ end
8867+ i = i + 1
8868+ end
8869+ if emitted_proto == 1
8870+ emit_raw("")
87368871 end
87378872 end
87388873
@@ -8768,7 +8903,7 @@
87688903 end
87698904 end
87708905 end
8771- if skip_inherited == 0 && ivar_is_gc_ptr(types[j]) == 1
8906+ if skip_inherited == 0 && ivar_needs_gc_mark(types[j]) == 1
87728907 emit_gc_mark_ivar("self->" + sanitize_ivar(names[j]), types[j])
87738908 end
87748909 end
@@ -8783,7 +8918,7 @@
87838918 pj = 0
87848919 while pj < pnames.length
87858920 if pj < ptypes.length
8786- if ivar_is_gc_ptr(ptypes[pj]) == 1
8921+ if ivar_needs_gc_mark(ptypes[pj]) == 1
87878922 emit_gc_mark_ivar("self->" + sanitize_ivar(pnames[pj]), ptypes[pj])
87888923 end
87898924 end
@@ -9724,6 +9859,29 @@
97249859 # to that DefNode's own class context, neither of which we want
97259860 # to attribute to the outer cls method).
97269861
9862+ def emit_constructor_param_roots(ci, init_idx)
9863+ if init_idx < 0
9864+ return
9865+ end
9866+ all_params_str = @cls_meth_params[ci].split("|")
9867+ all_ptypes_str = @cls_meth_ptypes[ci].split("|")
9868+ if init_idx >= all_params_str.length
9869+ return
9870+ end
9871+ cp_names = all_params_str[init_idx].split(",")
9872+ cp_types = "".split(",")
9873+ if init_idx < all_ptypes_str.length
9874+ cp_types = all_ptypes_str[init_idx].split(",")
9875+ end
9876+ cpi = 0
9877+ while cpi < cp_names.length
9878+ if cpi < cp_types.length
9879+ emit_raw_gc_root_for_expr("lv_" + cp_names[cpi], cp_types[cpi])
9880+ end
9881+ cpi = cpi + 1
9882+ end
9883+ end
9884+
97279885 def emit_constructor(ci)
97289886 saved_gc_scope = @in_gc_scope
97299887 cname = @cls_names[ci]
@@ -9732,6 +9890,7 @@
97329890 emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")
97339891 emit_raw(" sp_" + cname + " self = {0};")
97349892 @in_gc_scope = 0
9893+ emit_constructor_param_roots(ci, init_idx)
97359894 else
97369895 # Per-class free-list pool. Unmarked instances are pushed onto a
97379896 # bounded pool by sp_gc_collect (via the recycle hook in sp_gc_hdr)
@@ -9742,6 +9901,7 @@
97429901 emit_raw("static inline sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")
97439902 emit_raw(" SP_GC_SAVE();")
97449903 @in_gc_scope = 1
9904+ emit_constructor_param_roots(ci, init_idx)
97459905 scan_fn = "NULL"
97469906 if class_has_ptr_ivars(ci) == 1
97479907 scan_fn = "sp_" + cname + "_gc_scan"
@@ -9765,28 +9925,6 @@
97659925 # a stale .v.p, so reset it to a tagged nil that sp_mark_rbval
97669926 # treats as inert.
97679927 emit_clear_ptr_ivars_for_new(ci)
9768- end
9769-
9770- # Root pointer-type constructor parameters
9771- if init_idx >= 0
9772- all_params_str = @cls_meth_params[ci].split("|")
9773- all_ptypes_str = @cls_meth_ptypes[ci].split("|")
9774- if init_idx < all_params_str.length
9775- cp_names = all_params_str[init_idx].split(",")
9776- cp_types = "".split(",")
9777- if init_idx < all_ptypes_str.length
9778- cp_types = all_ptypes_str[init_idx].split(",")
9779- end
9780- cpi = 0
9781- while cpi < cp_names.length
9782- if cpi < cp_types.length
9783- if type_is_pointer(cp_types[cpi]) == 1
9784- emit_raw(" SP_GC_ROOT(lv_" + cp_names[cpi] + ");")
9785- end
9786- end
9787- cpi = cpi + 1
9788- end
9789- end
97909928 end
97919929
97929930 init_ci = find_init_class(ci)
@@ -10296,13 +10434,13 @@
1029610434 if @in_gc_scope == 1
1029710435 if @cls_is_value_type[ci] == 0
1029810436 emit(" SP_GC_ROOT(self);")
10437+ else
10438+ emit_gc_root_for_expr("self", "obj_" + cname)
1029910439 end
1030010440 j = 0
1030110441 while j < pnames.length
1030210442 if j < ptypes.length
10303- if type_is_pointer(ptypes[j]) == 1
10304- emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")
10305- end
10443+ emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])
1030610444 end
1030710445 j = j + 1
1030810446 end
@@ -10355,6 +10493,21 @@
1035510493 else
1035610494 declare_method_locals(bid, pnames)
1035710495 end
10496+ if @in_gc_scope == 0
10497+ if @needs_gc == 1
10498+ emit(" SP_GC_SAVE();")
10499+ @in_gc_scope = 1
10500+ end
10501+ end
10502+ if @in_gc_scope == 1
10503+ j = 0
10504+ while j < pnames.length
10505+ if j < ptypes.length
10506+ emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])
10507+ end
10508+ j = j + 1
10509+ end
10510+ end
1035810511 compile_body_return(bid, rt)
1035910512 end
1036010513
@@ -10387,7 +10540,7 @@
1038710540 has_gc_locals = 0
1038810541 j = 0
1038910542 while j < lnames.length
10390- if type_is_pointer(ltypes[j]) == 1
10543+ if type_needs_gc_root(ltypes[j]) == 1
1039110544 has_gc_locals = 1
1039210545 end
1039310546 j = j + 1
@@ -10604,9 +10757,7 @@
1060410757 j = 0
1060510758 while j < pnames.length
1060610759 if j < ptypes.length
10607- if type_is_pointer(ptypes[j]) == 1
10608- emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")
10609- end
10760+ emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])
1061010761 end
1061110762 j = j + 1
1061210763 end
@@ -10657,7 +10808,7 @@
1065710808 has_gc_locals = 0
1065810809 j = 0
1065910810 while j < lnames.length
10660- if type_is_pointer(ltypes[j]) == 1
10811+ if type_needs_gc_root(ltypes[j]) == 1
1066110812 has_gc_locals = 1
1066210813 end
1066310814 j = j + 1
@@ -10750,9 +10901,7 @@
1075010901 def emit_gc_roots(lnames, ltypes)
1075110902 j = 0
1075210903 while j < lnames.length
10753- if type_is_pointer(ltypes[j]) == 1
10754- emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")
10755- end
10904+ emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])
1075610905 j = j + 1
1075710906 end
1075810907 end
@@ -11304,9 +11453,9 @@
1130411453 # for the program's lifetime.
1130511454 ti = 0
1130611455 while ti < @toplevel_ivar_names.length
11307- if type_is_pointer(@toplevel_ivar_types[ti]) == 1
11308- emit(" _sp_gc_root_push((void**)&" + sanitize_ivar(@toplevel_ivar_names[ti]) + ");")
11309- end
11456+ tivar = sanitize_ivar(@toplevel_ivar_names[ti])
11457+ ttype = @toplevel_ivar_types[ti]
11458+ emit_global_gc_roots_for_expr(tivar, ttype)
1131011459 ti = ti + 1
1131111460 end
1131211461 if @needs_gc == 1
@@ -11342,9 +11491,9 @@
1134211491 # runtime helper signatures (sp_str_length, sp_IntArray_length,
1134311492 # etc.) without affecting correctness. Mirrors the per-method
1134411493 # logic in declare_method_locals (#548).
11345- if type_is_pointer(ltypes[j]) == 1
11346- emit(" " + ctp + "lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
11347- emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")
11494+ if type_needs_gc_root(ltypes[j]) == 1
11495+ emit(" " + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
11496+ emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])
1134811497 else
1134911498 vol = ""
1135011499 if @needs_setjmp == 1
@@ -11462,10 +11611,8 @@
1146211611 emit(" cst_" + @const_names[i] + " = " + val + ";")
1146311612 if guarded_646
1146411613 emit(" sp_init_in_progress_" + @const_names[i] + " = 0;")
11465- end
11466- if type_is_pointer(@const_types[i]) == 1
11467- emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")
1146811614 end
11615+ emit_gc_root_for_expr("cst_" + @const_names[i], @const_types[i])
1146911616 end
1147011617 i = i + 1
1147111618 end
@@ -11615,9 +11762,7 @@
1161511762 val_b = "sp_bigint_to_int((sp_Bigint *)" + val_b + ")"
1161611763 end
1161711764 emit(" cst_" + cwn_name + " = " + val_b + ";")
11618- if type_is_pointer(@const_types[cwn_idx]) == 1
11619- emit(" SP_GC_ROOT(cst_" + cwn_name + ");")
11620- end
11765+ emit_gc_root_for_expr("cst_" + cwn_name, @const_types[cwn_idx])
1162111766 end
1162211767 end
1162311768 end
@@ -13235,6 +13380,9 @@
1323513380 if is_nullable_type(t) == 1
1323613381 t = base_type(t)
1323713382 end
13383+ if value_type_needs_gc_scan(t) == 1
13384+ return 1
13385+ end
1323813386 if t == "string" || t == "mutable_str"
1323913387 return 1
1324013388 end
@@ -13251,7 +13399,7 @@
1325113399 return 1
1325213400 end
1325313401 if is_obj_type(t) == 1
13254- return 1
13402+ return type_is_pointer(t)
1325513403 end
1325613404 if is_tuple_type(t) == 1
1325713405 return 1
@@ -13282,7 +13430,7 @@
1328213430 @needs_gc = 1
1328313431 tmp = new_temp
1328413432 emit(" " + c_type(t) + " " + tmp + " = " + val + ";")
13285- emit(" SP_GC_ROOT(" + tmp + ");")
13433+ emit_gc_root_for_expr(tmp, t)
1328613434 tmp
1328713435 end
1328813436
@@ -14774,7 +14922,7 @@
1477414922 end
1477514923 end
1477614924 # Root receiver if it may be collected during argument evaluation
14777- if expr_may_gc(recv) == 1 && type_is_pointer(recv_type) == 1
14925+ if expr_may_gc(recv) == 1 && type_needs_gc_root(recv_type) == 1
1477814926 args_id = @nd_arguments[nid]
1477914927 if args_id >= 0
1478014928 aargs = get_args(args_id)
@@ -14789,7 +14937,7 @@
1478914937 if has_gc_arg == 1
1479014938 tmp = new_temp
1479114939 emit(" " + c_type(recv_type) + " " + tmp + " = " + rc + ";")
14792- emit(" SP_GC_ROOT(" + tmp + ");")
14940+ emit_gc_root_for_expr(tmp, recv_type)
1479314941 rc = tmp
1479414942 end
1479514943 end
@@ -24824,9 +24972,7 @@
2482424972 @needs_gc = 1
2482524973 src_tmp = new_temp
2482624974 emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")
24827- if type_is_pointer(src_t) == 1
24828- emit(" SP_GC_ROOT(" + src_tmp + ");")
24829- end
24975+ emit_gc_root_for_expr(src_tmp, src_t)
2483024976 src_v = src_tmp
2483124977 end
2483224978 src_len_expr = length_c_expr(src_t, src_v)
@@ -25676,9 +25822,7 @@
2567625822 at = infer_type(arg_ids[k])
2567725823 ct = c_type(at)
2567825824 emit(" " + ct + " " + tmp + " = " + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) + ";")
25679- if type_is_pointer(at) == 1
25680- emit(" SP_GC_ROOT(" + tmp + ");")
25681- end
25825+ emit_gc_root_for_expr(tmp, at)
2568225826 temps.push(tmp)
2568325827 else
2568425828 temps.push(cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])))
@@ -25699,13 +25843,7 @@
2569925843
2570025844 def constructor_arg_needs_root(arg_type)
2570125845 bt = base_type(arg_type)
25702- if is_obj_type(bt) == 1
25703- return 1
25704- end
25705- if is_array_type(bt) == 1 || is_ptr_array_type(bt) == 1 || is_tuple_type(bt) == 1
25706- return 1
25707- end
25708- if bt == "fiber" || bt == "bigint" || bt == "lambda" || bt == "poly_array"
25846+ if type_needs_gc_root(bt) == 1
2570925847 return 1
2571025848 end
2571125849 0
@@ -25734,7 +25872,7 @@
2573425872 @needs_gc = 1
2573525873 tmp = new_temp
2573625874 emit(" " + c_type(arg_type) + " " + tmp + " = " + expr + ";")
25737- emit(" SP_GC_ROOT(" + tmp + ");")
25875+ emit_gc_root_for_expr(tmp, arg_type)
2573825876 tmp
2573925877 end
2574025878