Commit 9de9cf06
9de9cf069e0fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: nil-to-object ivar inference, GC scan functions, module ivars
- Scan class method bodies for attr writer calls to upgrade nil-typed ivars to their actual pointer types (fixes splay tree left/right)
- Emit GC scan functions for classes with pointer-type ivars so GC can properly trace object graph
- Use attribute((cleanup)) for automatic GC restore on function exit, preventing GC root stack overflow in recursive functions
- Add conservative stack scanning as additional GC safety net
- Support module-level class methods and ivars (e.g., TheModule.method)
- Module ivars compiled as static constants, class methods as functions
- Fixes bm_splay, bm_gcbench, bm_getivar_module benchmarks
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 9de9cf069e0f
- Parent
- 3244e074f44f
spinel_codegen.rbmodifiedView file
@@ -109,6 +109,7 @@
109109
@in_main = 0110110
@in_loop = 0111111
@in_yield_method = 0112+
@in_gc_scope = 0112113
113114
# Yield/block tracking (parallel with meth_names / cls_meth_names)114115
@meth_has_yield = []@@ -735,6 +736,13 @@
735736
@out = @out + s + "\n"736737
end737738
739+
def emit_return(val)740+
if @in_gc_scope == 1741+
emit(" SP_GC_RESTORE();")742+
end743+
emit(" return " + val + ";")744+
end745+
738746
# ---- Type inference ----739747
def infer_type(nid)740748
if nid < 0@@ -2848,11 +2856,36 @@
28482856
@const_types.push(ct)28492857
@const_expr_ids.push(expr_id)28502858
end2851-
# Don't collect module DefNodes as top-level methods -2852-
# they will be included into classes via collect_module_methods_into_class2853-
# if @nd_type[sid] == "DefNode"2854-
# collect_toplevel_method(sid)2855-
# end2859+
# Collect module class methods (def self.xxx) as top-level functions2860+
if @nd_type[sid] == "DefNode"2861+
if @nd_receiver[sid] >= 02862+
if @nd_type[@nd_receiver[sid]] == "SelfNode"2863+
dmname = @nd_name[sid]2864+
# Create as top-level method with module prefix for dispatch2865+
mi = @meth_names.length2866+
@meth_names.push(mname + "_cls_" + dmname)2867+
@meth_param_names.push(collect_params_str(sid))2868+
@meth_param_types.push(collect_ptypes_str(sid, -1))2869+
@meth_return_types.push("int")2870+
@meth_body_ids.push(@nd_body[sid])2871+
@meth_has_yield.push(0)2872+
@meth_has_defaults.push("0")2873+
end2874+
end2875+
end2876+
# Collect module-level ivar writes as global statics2877+
if @nd_type[sid] == "InstanceVariableWriteNode"2878+
iname = @nd_name[sid]2879+
cname2 = mname + "_" + iname[1, iname.length - 1]2880+
expr_id = @nd_expression[sid]2881+
ct = "int"2882+
if expr_id >= 02883+
ct = infer_type(expr_id)2884+
end2885+
@const_names.push(cname2)2886+
@const_types.push(ct)2887+
@const_expr_ids.push(expr_id)2888+
end28562889
j = j + 128572890
end28582891
end@@ -3581,6 +3614,52 @@
35813614
pop_scope35823615
i = i + 135833616
end3617+
# Scan class method bodies3618+
ci = 03619+
while ci < @cls_names.length3620+
@current_class_idx = ci3621+
bodies = @cls_meth_bodies[ci].split(";")3622+
mnames = @cls_meth_names[ci].split(";")3623+
all_params = @cls_meth_params[ci].split("|")3624+
all_ptypes = @cls_meth_ptypes[ci].split("|")3625+
bj = 03626+
while bj < bodies.length3627+
bid = bodies[bj].to_i3628+
if bid >= 03629+
push_scope3630+
pnames2 = "".split(",")3631+
ptypes2 = "".split(",")3632+
if bj < all_params.length3633+
pnames2 = all_params[bj].split(",")3634+
end3635+
if bj < all_ptypes.length3636+
ptypes2 = all_ptypes[bj].split(",")3637+
end3638+
pk = 03639+
while pk < pnames2.length3640+
pt = "int"3641+
if pk < ptypes2.length3642+
pt = ptypes2[pk]3643+
end3644+
declare_var(pnames2[pk], pt)3645+
pk = pk + 13646+
end3647+
ml2 = "".split(",")3648+
mt2 = "".split(",")3649+
scan_locals(bid, ml2, mt2, pnames2)3650+
lk2 = 03651+
while lk2 < ml2.length3652+
declare_var(ml2[lk2], mt2[lk2])3653+
lk2 = lk2 + 13654+
end3655+
scan_writer_calls(bid)3656+
pop_scope3657+
end3658+
bj = bj + 13659+
end3660+
ci = ci + 13661+
end3662+
@current_class_idx = -135843663
# Scan main-level code35853664
scan_writer_calls(@root_id)35863665
pop_scope@@ -4961,6 +5040,7 @@
49615040
emit_stringio_runtime49625041
end49635042
emit_class_structs5043+
emit_gc_scan_functions49645044
emit_forward_decls49655045
emit_global_constants49665046
emit_class_methods@@ -5015,11 +5095,14 @@
50155095
emit_raw("static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 4*1024*1024;")50165096
emit_raw("#define SP_GC_STACK_MAX 65536")50175097
emit_raw("static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;")5018-
emit_raw("#define SP_GC_SAVE() int _gc_saved = sp_gc_nroots")5098+
emit_raw("#define SP_GC_SAVE() int __attribute__((cleanup(sp_gc_cleanup))) _gc_saved = sp_gc_nroots")50195099
emit_raw("#define SP_GC_ROOT(v) do{if(sp_gc_nroots<SP_GC_STACK_MAX)sp_gc_roots[sp_gc_nroots++]=(void**)&(v);}while(0)")50205100
emit_raw("#define SP_GC_RESTORE() sp_gc_nroots = _gc_saved")5101+
emit_raw("static char *sp_gc_stack_bottom = NULL;")50215102
emit_raw("static void sp_gc_mark(void*obj){if(!obj)return;sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->marked)return;h->marked=1;if(h->scan)h->scan(obj);}")5022-
emit_raw("static void sp_gc_collect(void){for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=0;sp_gc_bytes+=sizeof(sp_gc_hdr);pp=&h->next;}}}")5103+
emit_raw("static void sp_gc_cleanup(int*p){sp_gc_nroots=*p;}")5104+
emit_raw("static void sp_gc_mark_stack(void){volatile char dummy;char*sp=(char*)&dummy;char*lo=sp,*hi=sp_gc_stack_bottom;if(lo>hi){char*t=lo;lo=hi;hi=t;}for(sp_gc_hdr*h=sp_gc_heap;h;h=h->next){void*obj=(char*)h+sizeof(sp_gc_hdr);void**p=(void**)lo;while((char*)p<hi){if(*p==obj){sp_gc_mark(obj);break;}p++;}}}")5105+
emit_raw("static void sp_gc_collect(void){for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}sp_gc_mark_stack();sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));free(h);}else{h->marked=0;sp_gc_bytes+=sizeof(sp_gc_hdr);pp=&h->next;}}}")50235106
emit_raw("static void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){sp_gc_collect();if(sp_gc_bytes>sp_gc_threshold/2)sp_gc_threshold*=2;}sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,sizeof(sp_gc_hdr)+sz);h->finalize=fin;h->scan=scn;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=sizeof(sp_gc_hdr)+sz;return(char*)h+sizeof(sp_gc_hdr);}")50245107
emit_raw("")50255108
end@@ -5374,6 +5457,69 @@
53745457
053755458
end53765459
5460+
def class_has_ptr_ivars(ci)5461+
names = @cls_ivar_names[ci].split(";")5462+
types = @cls_ivar_types[ci].split(";")5463+
j = 05464+
while j < names.length5465+
if j < types.length5466+
if is_obj_type(types[j]) == 15467+
return 15468+
end5469+
end5470+
j = j + 15471+
end5472+
if @cls_parents[ci] != ""5473+
pi = find_class_idx(@cls_parents[ci])5474+
if pi >= 05475+
return class_has_ptr_ivars(pi)5476+
end5477+
end5478+
05479+
end5480+
5481+
def emit_gc_scan_functions5482+
i = 05483+
while i < @cls_names.length5484+
if class_has_ptr_ivars(i) == 15485+
cname = @cls_names[i]5486+
emit_raw("static void sp_" + cname + "_gc_scan(void *p) {")5487+
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)p;")5488+
names = @cls_ivar_names[i].split(";")5489+
types = @cls_ivar_types[i].split(";")5490+
j = 05491+
while j < names.length5492+
if j < types.length5493+
if is_obj_type(types[j]) == 15494+
emit_raw(" if (self->" + sanitize_ivar(names[j]) + ") sp_gc_mark(self->" + sanitize_ivar(names[j]) + ");")5495+
end5496+
end5497+
j = j + 15498+
end5499+
# Also scan parent fields5500+
if @cls_parents[i] != ""5501+
pi = find_class_idx(@cls_parents[i])5502+
if pi >= 05503+
pnames = @cls_ivar_names[pi].split(";")5504+
ptypes = @cls_ivar_types[pi].split(";")5505+
pj = 05506+
while pj < pnames.length5507+
if pj < ptypes.length5508+
if is_obj_type(ptypes[pj]) == 15509+
emit_raw(" if (self->" + sanitize_ivar(pnames[pj]) + ") sp_gc_mark(self->" + sanitize_ivar(pnames[pj]) + ");")5510+
end5511+
end5512+
pj = pj + 15513+
end5514+
end5515+
end5516+
emit_raw("}")5517+
emit_raw("")5518+
end5519+
i = i + 15520+
end5521+
end5522+
53775523
# ---- Forward declarations ----53785524
def emit_forward_decls53795525
# Emit block helper functions accumulated during collection@@ -5734,7 +5880,11 @@
57345880
init_idx = cls_find_method_direct(ci, "initialize")57355881
emit_raw("static sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")57365882
emit_raw(" SP_GC_SAVE();")5737-
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)sp_gc_alloc(sizeof(sp_" + cname + "), NULL, NULL);")5883+
scan_fn = "NULL"5884+
if class_has_ptr_ivars(ci) == 15885+
scan_fn = "sp_" + cname + "_gc_scan"5886+
end5887+
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)sp_gc_alloc(sizeof(sp_" + cname + "), NULL, " + scan_fn + ");")57385888
emit_raw(" SP_GC_ROOT(self);")57395889
57405890
init_ci = find_init_class(ci)@@ -5909,6 +6059,7 @@
59096059
@current_method_name = mname59106060
@current_method_return = rt59116061
@indent = 16062+
@in_gc_scope = 059126063
59136064
midx = cls_find_method_direct(ci, mname)59146065
if midx >= 0@@ -5956,6 +6107,7 @@
59566107
@current_method_name = mname59576108
@current_method_return = rt59586109
@indent = 16110+
@in_gc_scope = 059596111
59606112
emit_raw("static " + c_type(rt) + " sp_" + cname + "_cls_" + sanitize_name(mname) + "(" + build_params_decl(pnames, ptypes) + ") {")59616113
@@ -6032,6 +6184,7 @@
60326184
@current_method_return = @meth_return_types[mi]60336185
@indent = 160346186
@in_main = 06187+
@in_gc_scope = 060356188
60366189
# Check if this is an open class method60376190
oc_type = ""@@ -6147,6 +6300,7 @@
61476300
if has_gc_locals == 161486301
if @needs_gc == 161496302
emit(" SP_GC_SAVE();")6303+
@in_gc_scope = 161506304
end61516305
end61526306
j = 0@@ -6469,6 +6623,9 @@
64696623
emit_raw("static sp_Argv sp_argv;")64706624
emit_raw("")64716625
emit_raw("int main(int argc,char**argv){")6626+
if @needs_gc == 16627+
emit_raw(" { volatile char _sb; sp_gc_stack_bottom = (char*)&_sb; }")6628+
end64726629
emit_raw(" sp_argv.data=(const char**)(argv+1);sp_argv.len=argc-1;")64736630
64746631
@in_main = 1@@ -6619,6 +6776,22 @@
66196776
return "lv_" + @nd_name[nid]66206777
end66216778
if t == "InstanceVariableReadNode"6779+
# Check if we're in a module class method6780+
mi3 = 06781+
while mi3 < @module_names.length6782+
mmod = @module_names[mi3]6783+
if mmod != ""6784+
if @current_method_name.start_with?(mmod + "_cls_")6785+
iname = @nd_name[nid]6786+
cname3 = mmod + "_" + iname[1, iname.length - 1]6787+
ci3 = find_const_idx(cname3)6788+
if ci3 >= 06789+
return "cst_" + cname36790+
end6791+
end6792+
end6793+
mi3 = mi3 + 16794+
end66226795
return "self->" + sanitize_ivar(@nd_name[nid])66236796
end66246797
if t == "ConstantReadNode"@@ -8331,6 +8504,41 @@
83318504
return "getenv(\"HOME\")"83328505
end83338506
end8507+
# Module class method dispatch8508+
mi2 = 08509+
while mi2 < @module_names.length8510+
if @module_names[mi2] == rcname8511+
# Look for module class method8512+
mfn = rcname + "_cls_" + mname8513+
mfi = find_method_idx(mfn)8514+
if mfi >= 08515+
ca = compile_call_args(nid)8516+
if ca != ""8517+
return "sp_" + sanitize_name(mfn) + "(" + ca + ")"8518+
else8519+
return "sp_" + sanitize_name(mfn) + "()"8520+
end8521+
end8522+
end8523+
mi2 = mi2 + 18524+
end8525+
# Class method dispatch (def self.xxx)8526+
ci3 = find_class_idx(rcname)8527+
if ci3 >= 08528+
cmnames = @cls_cmeth_names[ci3].split(";")8529+
cj = 08530+
while cj < cmnames.length8531+
if cmnames[cj] == mname8532+
ca = compile_call_args(nid)8533+
if ca != ""8534+
return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "(" + ca + ")"8535+
else8536+
return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "()"8537+
end8538+
end8539+
cj = cj + 18540+
end8541+
end83348542
end83358543
83368544
# to_a on range (including parenthesized range)@@ -9516,10 +9724,22 @@
95169724
if args_id >= 095179725
arg_ids = get_args(args_id)95189726
if arg_ids.length > 09519-
emit(" return " + compile_expr(arg_ids[0]) + ";")9727+
if @in_gc_scope == 19728+
# Save return value, restore GC, then return9729+
rt = infer_type(arg_ids[0])9730+
tmp = new_temp9731+
emit(" " + c_type(rt) + " " + tmp + " = " + compile_expr(arg_ids[0]) + ";")9732+
emit(" SP_GC_RESTORE();")9733+
emit(" return " + tmp + ";")9734+
else9735+
emit(" return " + compile_expr(arg_ids[0]) + ";")9736+
end95209737
return95219738
end95229739
end9740+
if @in_gc_scope == 19741+
emit(" SP_GC_RESTORE();")9742+
end95239743
emit(" return 0;")95249744
end95259745