Commit 2238b26d

2238b26d2f8fMerged

fc-anjos <[email protected]> · 6 months ago · 9 files · +4 added · ~5 modified

Description

root value-type fields in codegen

Co-Authored-By: OpenAI Codex 5.5

Details

Commit
2238b26d2f8f
Parent
45fae0a0846a

spinel_codegen.rbmodifiedView file

@@ -3494,13 +3494,10 @@
34943494 # element types must be marked, otherwise the GC frees the inner object
34953495 # while the tuple keeps a dangling pointer.
34963496 def tuple_field_needs_mark(et)
3497- if et == "poly"
3497+ if gc_trace_kind(et) != "none"
34983498 return 1
34993499 end
3500- if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"
3501- return 0
3502- end
3503- type_is_pointer(et)
3500+ 0
35043501 end
35053502
35063503 # Returns the scan function name for the tuple, or "NULL" if no field
@@ -4867,8 +4864,12 @@
48674864 emit(" SP_GC_SAVE();")
48684865 @in_gc_scope = 1
48694866 end
4870- if @needs_gc == 1 && @cls_is_value_type[ci] == 0
4871- emit(" SP_GC_ROOT(self);")
4867+ if @needs_gc == 1
4868+ if @cls_is_value_type[ci] == 0
4869+ emit(" SP_GC_ROOT(self);")
4870+ else
4871+ emit_gc_root_for_expr("self", "obj_" + cname)
4872+ end
48724873 end
48734874 compile_body_return(bid, ret_type)
48744875 end
@@ -7136,6 +7137,7 @@
71367137 # forward declaration.
71377138 emit_class_runtime
71387139 emit_class_structs
7140+ emit_gc_scan_function_prototypes
71397141 emit_raw("/*TUPLE_INSERT_POINT*/")
71407142 emit_gc_scan_functions
71417143 emit_toplevel_ivar_decls
@@ -7221,10 +7223,9 @@
72217223 while fi < parts.length
72227224 if tuple_field_needs_mark(parts[fi]) == 1
72237225 field = "_t->_" + fi.to_s
7224- if parts[fi] == "poly"
7225- body = body + " sp_mark_rbval(" + field + ");"
7226- else
7227- body = body + " sp_gc_mark((void *)" + field + ");"
7226+ stmt = gc_mark_stmt_for_expr(field, parts[fi])
7227+ if stmt != ""
7228+ body = body + " " + stmt
72287229 end
72297230 end
72307231 fi = fi + 1
@@ -8509,19 +8510,125 @@
85098510 end
85108511
85118512 def ivar_needs_gc_mark(t)
8512- if ivar_is_gc_ptr(t) == 1
8513+ if gc_trace_kind(t) != "none"
8514+ return 1
8515+ end
8516+ 0
8517+ end
8518+
8519+ def value_type_scan_class_name(t)
8520+ bt = base_type(t)
8521+ if is_value_type_obj(bt) == 1
8522+ return bt[4, bt.length - 4]
8523+ end
8524+ ""
8525+ end
8526+
8527+ def value_type_needs_gc_scan(t)
8528+ cname = value_type_scan_class_name(t)
8529+ if cname == ""
8530+ return 0
8531+ end
8532+ ci = find_class_idx(cname)
8533+ if ci >= 0 && class_has_ptr_ivars(ci) == 1
85138534 return 1
85148535 end
8515- if is_value_type_obj(t) == 1
8516- cname = t[4, t.length - 4]
8517- ci = find_class_idx(cname)
8518- if ci >= 0 && class_has_ptr_ivars(ci) == 1
8519- return 1
8536+ 0
8537+ end
8538+
8539+ def gc_trace_kind(t)
8540+ bt = base_type(t)
8541+ if bt == "poly"
8542+ return "poly"
8543+ end
8544+ if value_type_needs_gc_scan(bt) == 1
8545+ return "value_type"
8546+ end
8547+ if type_is_pointer(bt) == 1
8548+ return "ptr"
8549+ end
8550+ "none"
8551+ end
8552+
8553+ def emit_gc_root_line(stmt, raw)
8554+ if raw == 1
8555+ emit_raw(" " + stmt)
8556+ else
8557+ emit(" " + stmt)
8558+ end
8559+ 0
8560+ end
8561+
8562+ def emit_value_type_field_roots(expr, t, raw, global)
8563+ cname = value_type_scan_class_name(t)
8564+ if cname == ""
8565+ return
8566+ end
8567+ ci = find_class_idx(cname)
8568+ if ci < 0
8569+ return
8570+ end
8571+ names = @cls_ivar_names[ci].split(";")
8572+ types = @cls_ivar_types[ci].split(";")
8573+ j = 0
8574+ while j < names.length
8575+ if j < types.length
8576+ emit_gc_root_for_expr_impl(expr + "." + sanitize_ivar(names[j]), types[j], raw, global)
8577+ end
8578+ j = j + 1
8579+ end
8580+ 0
8581+ end
8582+
8583+ def emit_gc_root_for_expr_impl(expr, t, raw, global)
8584+ kind = gc_trace_kind(t)
8585+ if kind == "ptr"
8586+ if global == 1
8587+ emit_gc_root_line("_sp_gc_root_push((void**)&" + expr + ");", raw)
8588+ else
8589+ emit_gc_root_line("SP_GC_ROOT(" + expr + ");", raw)
85208590 end
8591+ elsif kind == "value_type"
8592+ emit_value_type_field_roots(expr, t, raw, global)
85218593 end
85228594 0
85238595 end
85248596
8597+ def gc_mark_stmt_for_expr(expr, t)
8598+ kind = gc_trace_kind(t)
8599+ if kind == "poly"
8600+ return "sp_mark_rbval(" + expr + ");"
8601+ end
8602+ if kind == "value_type"
8603+ cname = value_type_scan_class_name(t)
8604+ return "sp_" + cname + "_gc_scan(&" + expr + ");"
8605+ end
8606+ if kind == "ptr"
8607+ return "if (" + expr + ") sp_gc_mark((void *)" + expr + ");"
8608+ end
8609+ ""
8610+ end
8611+
8612+ def emit_gc_root_for_expr(expr, t)
8613+ emit_gc_root_for_expr_impl(expr, t, 0, 0)
8614+ end
8615+
8616+ def emit_raw_gc_root_for_expr(expr, t)
8617+ emit_gc_root_for_expr_impl(expr, t, 1, 0)
8618+ end
8619+
8620+ def emit_global_gc_roots_for_expr(expr, t)
8621+ emit_gc_root_for_expr_impl(expr, t, 0, 1)
8622+ end
8623+
8624+ def type_needs_gc_root(t)
8625+ kind = gc_trace_kind(t)
8626+ if kind == "ptr" || kind == "value_type"
8627+ return 1
8628+ end
8629+ 0
8630+ end
8631+
85258632 def class_has_ptr_ivars(ci)
85268633 names = @cls_ivar_names[ci].split(";")
85278634 types = @cls_ivar_types[ci].split(";")
@@ -8543,8 +8650,7 @@
85438650 0
85448651 end
85458652
8546- # Emit `self->iv_<name> = NULL;` / `sp_box_nil();` for each
8547- # GC-traced ivar on the class chain. Called right after
8653+ # Clear each GC-traced field on the class chain. Called right after
85488654 # SP_POOL_NEW + SP_GC_ROOT(self) to defuse the recycled-slot
85498655 # stale-pointer hazard: if a sub-allocation in the initialize
85508656 # body triggers GC before the body writes every pointer ivar,
@@ -8566,13 +8672,14 @@
85668672 j = 0
85678673 while j < names.length
85688674 if j < types.length
8569- if ivar_is_gc_ptr(types[j]) == 1
8570- field = "self->" + sanitize_ivar(names[j])
8571- if types[j] == "poly"
8572- emit_raw(" " + field + " = sp_box_nil();")
8573- else
8574- emit_raw(" " + field + " = NULL;")
8575- end
8675+ kind = gc_trace_kind(types[j])
8676+ field = "self->" + sanitize_ivar(names[j])
8677+ if kind == "poly"
8678+ emit_raw(" " + field + " = sp_box_nil();")
8679+ elsif kind == "ptr"
8680+ emit_raw(" " + field + " = NULL;")
8681+ elsif kind == "value_type"
8682+ emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};")
85768683 end
85778684 end
85788685 j = j + 1
@@ -8580,17 +8687,13 @@
85808687 end
85818688
85828689 def emit_gc_mark_ivar(field, t)
8583- if t == "poly"
8584- emit_raw(" sp_mark_rbval(" + field + ");")
8585- elsif is_value_type_obj(t) == 1
8586- cname = t[4, t.length - 4]
8587- emit_raw(" sp_" + cname + "_gc_scan(&" + field + ");")
8588- else
8589- emit_raw(" if (" + field + ") sp_gc_mark((void *)" + field + ");")
8690+ stmt = gc_mark_stmt_for_expr(field, t)
8691+ if stmt != ""
8692+ emit_raw(" " + stmt)
85908693 end
85918694 end
85928695
8593- def emit_gc_scan_functions
8696+ def emit_gc_scan_function_prototypes
85948697 emitted_proto = 0
85958698 i = 0
85968699 while i < @cls_names.length
@@ -8603,7 +8706,9 @@
86038706 if emitted_proto == 1
86048707 emit_raw("")
86058708 end
8709+ end
86068710
8711+ def emit_gc_scan_functions
86078712 i = 0
86088713 while i < @cls_names.length
86098714 if cls_emit_skipped(i) == 1
@@ -9591,6 +9696,29 @@
95919696 # to that DefNode's own class context, neither of which we want
95929697 # to attribute to the outer cls method).
95939698
9699+ def emit_constructor_param_roots(ci, init_idx)
9700+ if init_idx < 0
9701+ return
9702+ end
9703+ all_params_str = @cls_meth_params[ci].split("|")
9704+ all_ptypes_str = @cls_meth_ptypes[ci].split("|")
9705+ if init_idx >= all_params_str.length
9706+ return
9707+ end
9708+ cp_names = all_params_str[init_idx].split(",")
9709+ cp_types = "".split(",")
9710+ if init_idx < all_ptypes_str.length
9711+ cp_types = all_ptypes_str[init_idx].split(",")
9712+ end
9713+ cpi = 0
9714+ while cpi < cp_names.length
9715+ if cpi < cp_types.length
9716+ emit_raw_gc_root_for_expr("lv_" + cp_names[cpi], cp_types[cpi])
9717+ end
9718+ cpi = cpi + 1
9719+ end
9720+ end
9721+
95949722 def emit_constructor(ci)
95959723 saved_gc_scope = @in_gc_scope
95969724 cname = @cls_names[ci]
@@ -9599,6 +9727,7 @@
95999727 emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")
96009728 emit_raw(" sp_" + cname + " self = {0};")
96019729 @in_gc_scope = 0
9730+ emit_constructor_param_roots(ci, init_idx)
96029731 else
96039732 # Per-class free-list pool. Unmarked instances are pushed onto a
96049733 # bounded pool by sp_gc_collect (via the recycle hook in sp_gc_hdr)
@@ -9609,6 +9738,7 @@
96099738 emit_raw("static inline sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")
96109739 emit_raw(" SP_GC_SAVE();")
96119740 @in_gc_scope = 1
9741+ emit_constructor_param_roots(ci, init_idx)
96129742 scan_fn = "NULL"
96139743 if class_has_ptr_ivars(ci) == 1
96149744 scan_fn = "sp_" + cname + "_gc_scan"
@@ -9634,28 +9764,6 @@
96349764 emit_clear_ptr_ivars_for_new(ci)
96359765 end
96369766
9637- # Root pointer-type constructor parameters
9638- if init_idx >= 0
9639- all_params_str = @cls_meth_params[ci].split("|")
9640- all_ptypes_str = @cls_meth_ptypes[ci].split("|")
9641- if init_idx < all_params_str.length
9642- cp_names = all_params_str[init_idx].split(",")
9643- cp_types = "".split(",")
9644- if init_idx < all_ptypes_str.length
9645- cp_types = all_ptypes_str[init_idx].split(",")
9646- end
9647- cpi = 0
9648- while cpi < cp_names.length
9649- if cpi < cp_types.length
9650- if type_is_pointer(cp_types[cpi]) == 1
9651- emit_raw(" SP_GC_ROOT(lv_" + cp_names[cpi] + ");")
9652- end
9653- end
9654- cpi = cpi + 1
9655- end
9656- end
9657- end
9658-
96599767 init_ci = find_init_class(ci)
96609768
96619769 if init_idx >= 0
@@ -10150,13 +10258,13 @@
1015010258 if @in_gc_scope == 1
1015110259 if @cls_is_value_type[ci] == 0
1015210260 emit(" SP_GC_ROOT(self);")
10261+ else
10262+ emit_gc_root_for_expr("self", "obj_" + cname)
1015310263 end
1015410264 j = 0
1015510265 while j < pnames.length
1015610266 if j < ptypes.length
10157- if type_is_pointer(ptypes[j]) == 1
10158- emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")
10159- end
10267+ emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])
1016010268 end
1016110269 j = j + 1
1016210270 end
@@ -10209,6 +10317,21 @@
1020910317 else
1021010318 declare_method_locals(bid, pnames)
1021110319 end
10320+ if @in_gc_scope == 0
10321+ if @needs_gc == 1
10322+ emit(" SP_GC_SAVE();")
10323+ @in_gc_scope = 1
10324+ end
10325+ end
10326+ if @in_gc_scope == 1
10327+ j = 0
10328+ while j < pnames.length
10329+ if j < ptypes.length
10330+ emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])
10331+ end
10332+ j = j + 1
10333+ end
10334+ end
1021210335 compile_body_return(bid, rt)
1021310336 end
1021410337
@@ -10241,7 +10364,7 @@
1024110364 has_gc_locals = 0
1024210365 j = 0
1024310366 while j < lnames.length
10244- if type_is_pointer(ltypes[j]) == 1
10367+ if type_needs_gc_root(ltypes[j]) == 1
1024510368 has_gc_locals = 1
1024610369 end
1024710370 j = j + 1
@@ -10458,9 +10581,7 @@
1045810581 j = 0
1045910582 while j < pnames.length
1046010583 if j < ptypes.length
10461- if type_is_pointer(ptypes[j]) == 1
10462- emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")
10463- end
10584+ emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])
1046410585 end
1046510586 j = j + 1
1046610587 end
@@ -10511,7 +10632,7 @@
1051110632 has_gc_locals = 0
1051210633 j = 0
1051310634 while j < lnames.length
10514- if type_is_pointer(ltypes[j]) == 1
10635+ if type_needs_gc_root(ltypes[j]) == 1
1051510636 has_gc_locals = 1
1051610637 end
1051710638 j = j + 1
@@ -10604,9 +10725,7 @@
1060410725 def emit_gc_roots(lnames, ltypes)
1060510726 j = 0
1060610727 while j < lnames.length
10607- if type_is_pointer(ltypes[j]) == 1
10608- emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")
10609- end
10728+ emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])
1061010729 j = j + 1
1061110730 end
1061210731 end
@@ -11158,9 +11277,9 @@
1115811277 # for the program's lifetime.
1115911278 ti = 0
1116011279 while ti < @toplevel_ivar_names.length
11161- if type_is_pointer(@toplevel_ivar_types[ti]) == 1
11162- emit(" _sp_gc_root_push((void**)&" + sanitize_ivar(@toplevel_ivar_names[ti]) + ");")
11163- end
11280+ tivar = sanitize_ivar(@toplevel_ivar_names[ti])
11281+ ttype = @toplevel_ivar_types[ti]
11282+ emit_global_gc_roots_for_expr(tivar, ttype)
1116411283 ti = ti + 1
1116511284 end
1116611285 if @needs_gc == 1
@@ -11196,9 +11315,9 @@
1119611315 # runtime helper signatures (sp_str_length, sp_IntArray_length,
1119711316 # etc.) without affecting correctness. Mirrors the per-method
1119811317 # logic in declare_method_locals (#548).
11199- if type_is_pointer(ltypes[j]) == 1
11200- emit(" " + ctp + "lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
11201- emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")
11318+ if type_needs_gc_root(ltypes[j]) == 1
11319+ emit(" " + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
11320+ emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])
1120211321 else
1120311322 vol = ""
1120411323 if @needs_setjmp == 1
@@ -11316,10 +11435,8 @@
1131611435 emit(" cst_" + @const_names[i] + " = " + val + ";")
1131711436 if guarded_646
1131811437 emit(" sp_init_in_progress_" + @const_names[i] + " = 0;")
11319- end
11320- if type_is_pointer(@const_types[i]) == 1
11321- emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")
1132211438 end
11439+ emit_gc_root_for_expr("cst_" + @const_names[i], @const_types[i])
1132311440 end
1132411441 i = i + 1
1132511442 end
@@ -11469,9 +11586,7 @@
1146911586 val_b = "sp_bigint_to_int((sp_Bigint *)" + val_b + ")"
1147011587 end
1147111588 emit(" cst_" + cwn_name + " = " + val_b + ";")
11472- if type_is_pointer(@const_types[cwn_idx]) == 1
11473- emit(" SP_GC_ROOT(cst_" + cwn_name + ");")
11474- end
11589+ emit_gc_root_for_expr("cst_" + cwn_name, @const_types[cwn_idx])
1147511590 end
1147611591 end
1147711592 end
@@ -12840,6 +12955,9 @@
1284012955 if is_nullable_type(t) == 1
1284112956 t = base_type(t)
1284212957 end
12958+ if value_type_needs_gc_scan(t) == 1
12959+ return 1
12960+ end
1284312961 if t == "string" || t == "mutable_str"
1284412962 return 1
1284512963 end
@@ -12856,7 +12974,7 @@
1285612974 return 1
1285712975 end
1285812976 if is_obj_type(t) == 1
12859- return 1
12977+ return type_is_pointer(t)
1286012978 end
1286112979 if is_tuple_type(t) == 1
1286212980 return 1
@@ -12887,7 +13005,7 @@
1288713005 @needs_gc = 1
1288813006 tmp = new_temp
1288913007 emit(" " + c_type(t) + " " + tmp + " = " + val + ";")
12890- emit(" SP_GC_ROOT(" + tmp + ");")
13008+ emit_gc_root_for_expr(tmp, t)
1289113009 tmp
1289213010 end
1289313011
@@ -14367,7 +14485,7 @@
1436714485 end
1436814486 end
1436914487 # Root receiver if it may be collected during argument evaluation
14370- if expr_may_gc(recv) == 1 && type_is_pointer(recv_type) == 1
14488+ if expr_may_gc(recv) == 1 && type_needs_gc_root(recv_type) == 1
1437114489 args_id = @nd_arguments[nid]
1437214490 if args_id >= 0
1437314491 aargs = get_args(args_id)
@@ -14382,7 +14500,7 @@
1438214500 if has_gc_arg == 1
1438314501 tmp = new_temp
1438414502 emit(" " + c_type(recv_type) + " " + tmp + " = " + rc + ";")
14385- emit(" SP_GC_ROOT(" + tmp + ");")
14503+ emit_gc_root_for_expr(tmp, recv_type)
1438614504 rc = tmp
1438714505 end
1438814506 end
@@ -24114,9 +24232,7 @@
2411424232 @needs_gc = 1
2411524233 src_tmp = new_temp
2411624234 emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")
24117- if type_is_pointer(src_t) == 1
24118- emit(" SP_GC_ROOT(" + src_tmp + ");")
24119- end
24235+ emit_gc_root_for_expr(src_tmp, src_t)
2412024236 src_v = src_tmp
2412124237 end
2412224238 src_len_expr = length_c_expr(src_t, src_v)
@@ -24914,9 +25030,7 @@
2491425030 at = infer_type(arg_ids[k])
2491525031 ct = c_type(at)
2491625032 emit(" " + ct + " " + tmp + " = " + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) + ";")
24917- if type_is_pointer(at) == 1
24918- emit(" SP_GC_ROOT(" + tmp + ");")
24919- end
25033+ emit_gc_root_for_expr(tmp, at)
2492025034 temps.push(tmp)
2492125035 else
2492225036 temps.push(cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])))
@@ -24937,13 +25051,7 @@
2493725051
2493825052 def constructor_arg_needs_root(arg_type)
2493925053 bt = base_type(arg_type)
24940- if is_obj_type(bt) == 1
24941- return type_is_pointer(bt)
24942- end
24943- if is_array_type(bt) == 1 || is_ptr_array_type(bt) == 1 || is_tuple_type(bt) == 1
24944- return 1
24945- end
24946- if bt == "fiber" || bt == "bigint" || bt == "lambda" || bt == "poly_array"
25054+ if type_needs_gc_root(bt) == 1
2494725055 return 1
2494825056 end
2494925057 0
@@ -24963,7 +25071,7 @@
2496325071 @needs_gc = 1
2496425072 tmp = new_temp
2496525073 emit(" " + c_type(arg_type) + " " + tmp + " = " + expr + ";")
24966- emit(" SP_GC_ROOT(" + tmp + ");")
25074+ emit_gc_root_for_expr(tmp, arg_type)
2496725075 tmp
2496825076 end
2496925077