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 = 0
110110 @in_loop = 0
111111 @in_yield_method = 0
112+ @in_gc_scope = 0
112113
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 end
737738
739+ def emit_return(val)
740+ if @in_gc_scope == 1
741+ emit(" SP_GC_RESTORE();")
742+ end
743+ emit(" return " + val + ";")
744+ end
745+
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 end
2851- # Don't collect module DefNodes as top-level methods -
2852- # they will be included into classes via collect_module_methods_into_class
2853- # if @nd_type[sid] == "DefNode"
2854- # collect_toplevel_method(sid)
2855- # end
2859+ # Collect module class methods (def self.xxx) as top-level functions
2860+ if @nd_type[sid] == "DefNode"
2861+ if @nd_receiver[sid] >= 0
2862+ if @nd_type[@nd_receiver[sid]] == "SelfNode"
2863+ dmname = @nd_name[sid]
2864+ # Create as top-level method with module prefix for dispatch
2865+ mi = @meth_names.length
2866+ @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+ end
2874+ end
2875+ end
2876+ # Collect module-level ivar writes as global statics
2877+ 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 >= 0
2883+ ct = infer_type(expr_id)
2884+ end
2885+ @const_names.push(cname2)
2886+ @const_types.push(ct)
2887+ @const_expr_ids.push(expr_id)
2888+ end
28562889 j = j + 1
28572890 end
28582891 end
@@ -3581,6 +3614,52 @@
35813614 pop_scope
35823615 i = i + 1
35833616 end
3617+ # Scan class method bodies
3618+ ci = 0
3619+ while ci < @cls_names.length
3620+ @current_class_idx = ci
3621+ 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 = 0
3626+ while bj < bodies.length
3627+ bid = bodies[bj].to_i
3628+ if bid >= 0
3629+ push_scope
3630+ pnames2 = "".split(",")
3631+ ptypes2 = "".split(",")
3632+ if bj < all_params.length
3633+ pnames2 = all_params[bj].split(",")
3634+ end
3635+ if bj < all_ptypes.length
3636+ ptypes2 = all_ptypes[bj].split(",")
3637+ end
3638+ pk = 0
3639+ while pk < pnames2.length
3640+ pt = "int"
3641+ if pk < ptypes2.length
3642+ pt = ptypes2[pk]
3643+ end
3644+ declare_var(pnames2[pk], pt)
3645+ pk = pk + 1
3646+ end
3647+ ml2 = "".split(",")
3648+ mt2 = "".split(",")
3649+ scan_locals(bid, ml2, mt2, pnames2)
3650+ lk2 = 0
3651+ while lk2 < ml2.length
3652+ declare_var(ml2[lk2], mt2[lk2])
3653+ lk2 = lk2 + 1
3654+ end
3655+ scan_writer_calls(bid)
3656+ pop_scope
3657+ end
3658+ bj = bj + 1
3659+ end
3660+ ci = ci + 1
3661+ end
3662+ @current_class_idx = -1
35843663 # Scan main-level code
35853664 scan_writer_calls(@root_id)
35863665 pop_scope
@@ -4961,6 +5040,7 @@
49615040 emit_stringio_runtime
49625041 end
49635042 emit_class_structs
5043+ emit_gc_scan_functions
49645044 emit_forward_decls
49655045 emit_global_constants
49665046 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 0
53755458 end
53765459
5460+ def class_has_ptr_ivars(ci)
5461+ names = @cls_ivar_names[ci].split(";")
5462+ types = @cls_ivar_types[ci].split(";")
5463+ j = 0
5464+ while j < names.length
5465+ if j < types.length
5466+ if is_obj_type(types[j]) == 1
5467+ return 1
5468+ end
5469+ end
5470+ j = j + 1
5471+ end
5472+ if @cls_parents[ci] != ""
5473+ pi = find_class_idx(@cls_parents[ci])
5474+ if pi >= 0
5475+ return class_has_ptr_ivars(pi)
5476+ end
5477+ end
5478+ 0
5479+ end
5480+
5481+ def emit_gc_scan_functions
5482+ i = 0
5483+ while i < @cls_names.length
5484+ if class_has_ptr_ivars(i) == 1
5485+ 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 = 0
5491+ while j < names.length
5492+ if j < types.length
5493+ if is_obj_type(types[j]) == 1
5494+ emit_raw(" if (self->" + sanitize_ivar(names[j]) + ") sp_gc_mark(self->" + sanitize_ivar(names[j]) + ");")
5495+ end
5496+ end
5497+ j = j + 1
5498+ end
5499+ # Also scan parent fields
5500+ if @cls_parents[i] != ""
5501+ pi = find_class_idx(@cls_parents[i])
5502+ if pi >= 0
5503+ pnames = @cls_ivar_names[pi].split(";")
5504+ ptypes = @cls_ivar_types[pi].split(";")
5505+ pj = 0
5506+ while pj < pnames.length
5507+ if pj < ptypes.length
5508+ if is_obj_type(ptypes[pj]) == 1
5509+ emit_raw(" if (self->" + sanitize_ivar(pnames[pj]) + ") sp_gc_mark(self->" + sanitize_ivar(pnames[pj]) + ");")
5510+ end
5511+ end
5512+ pj = pj + 1
5513+ end
5514+ end
5515+ end
5516+ emit_raw("}")
5517+ emit_raw("")
5518+ end
5519+ i = i + 1
5520+ end
5521+ end
5522+
53775523 # ---- Forward declarations ----
53785524 def emit_forward_decls
53795525 # 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) == 1
5885+ scan_fn = "sp_" + cname + "_gc_scan"
5886+ end
5887+ 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 = mname
59106060 @current_method_return = rt
59116061 @indent = 1
6062+ @in_gc_scope = 0
59126063
59136064 midx = cls_find_method_direct(ci, mname)
59146065 if midx >= 0
@@ -5956,6 +6107,7 @@
59566107 @current_method_name = mname
59576108 @current_method_return = rt
59586109 @indent = 1
6110+ @in_gc_scope = 0
59596111
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 = 1
60346186 @in_main = 0
6187+ @in_gc_scope = 0
60356188
60366189 # Check if this is an open class method
60376190 oc_type = ""
@@ -6147,6 +6300,7 @@
61476300 if has_gc_locals == 1
61486301 if @needs_gc == 1
61496302 emit(" SP_GC_SAVE();")
6303+ @in_gc_scope = 1
61506304 end
61516305 end
61526306 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 == 1
6627+ emit_raw(" { volatile char _sb; sp_gc_stack_bottom = (char*)&_sb; }")
6628+ end
64726629 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 end
66216778 if t == "InstanceVariableReadNode"
6779+ # Check if we're in a module class method
6780+ mi3 = 0
6781+ while mi3 < @module_names.length
6782+ 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 >= 0
6789+ return "cst_" + cname3
6790+ end
6791+ end
6792+ end
6793+ mi3 = mi3 + 1
6794+ end
66226795 return "self->" + sanitize_ivar(@nd_name[nid])
66236796 end
66246797 if t == "ConstantReadNode"
@@ -8331,6 +8504,41 @@
83318504 return "getenv(\"HOME\")"
83328505 end
83338506 end
8507+ # Module class method dispatch
8508+ mi2 = 0
8509+ while mi2 < @module_names.length
8510+ if @module_names[mi2] == rcname
8511+ # Look for module class method
8512+ mfn = rcname + "_cls_" + mname
8513+ mfi = find_method_idx(mfn)
8514+ if mfi >= 0
8515+ ca = compile_call_args(nid)
8516+ if ca != ""
8517+ return "sp_" + sanitize_name(mfn) + "(" + ca + ")"
8518+ else
8519+ return "sp_" + sanitize_name(mfn) + "()"
8520+ end
8521+ end
8522+ end
8523+ mi2 = mi2 + 1
8524+ end
8525+ # Class method dispatch (def self.xxx)
8526+ ci3 = find_class_idx(rcname)
8527+ if ci3 >= 0
8528+ cmnames = @cls_cmeth_names[ci3].split(";")
8529+ cj = 0
8530+ while cj < cmnames.length
8531+ if cmnames[cj] == mname
8532+ ca = compile_call_args(nid)
8533+ if ca != ""
8534+ return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "(" + ca + ")"
8535+ else
8536+ return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "()"
8537+ end
8538+ end
8539+ cj = cj + 1
8540+ end
8541+ end
83348542 end
83358543
83368544 # to_a on range (including parenthesized range)
@@ -9516,10 +9724,22 @@
95169724 if args_id >= 0
95179725 arg_ids = get_args(args_id)
95189726 if arg_ids.length > 0
9519- emit(" return " + compile_expr(arg_ids[0]) + ";")
9727+ if @in_gc_scope == 1
9728+ # Save return value, restore GC, then return
9729+ rt = infer_type(arg_ids[0])
9730+ tmp = new_temp
9731+ emit(" " + c_type(rt) + " " + tmp + " = " + compile_expr(arg_ids[0]) + ";")
9732+ emit(" SP_GC_RESTORE();")
9733+ emit(" return " + tmp + ";")
9734+ else
9735+ emit(" return " + compile_expr(arg_ids[0]) + ";")
9736+ end
95209737 return
95219738 end
95229739 end
9740+ if @in_gc_scope == 1
9741+ emit(" SP_GC_RESTORE();")
9742+ end
95239743 emit(" return 0;")
95249744 end
95259745