Commit 04008070

040080709c79Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: self-hosting works - fib(34) compiles through self-hosted binary

Major changes to enable self-hosting:

  • Add infer_class_body_call_types: scans class method bodies for calls to other same-class methods and propagates parameter types
  • Add scan_cls_method_calls for class-internal call site type inference
  • Add scan_locals_first_type (no poly marking) for inference passes
  • Fix scan_locals to not mark poly when new type is fallback "int"
  • Add locals declaration in infer_all_returns for class methods
  • Fix sp_str_split to return empty array for empty input (Ruby compat)
  • Fix ARGV[] type inference to return "string"
  • Add GC rooting (SP_GC_ROOT) for pointer-typed local variables
  • Increase GC stack max and initial threshold for compiler workloads
  • Convert [] initializers to "".split(",") for StrArray consistency

Pipeline: ruby spinel_parse.rb spinel_codegen.rb | ruby spinel_codegen.rb -> C binary that reads AST and generates correct C for fib(34)=5702887

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
040080709c79
Parent
7e134de5b6d8

spinel_codegen.rbmodifiedView file

@@ -1372,6 +1372,9 @@
13721372 if rt == "str_str_hash"
13731373 return "string"
13741374 end
1375+ if rt == "argv"
1376+ return "string"
1377+ end
13751378 end
13761379 return "int"
13771380 end
@@ -3037,7 +3040,7 @@
30373040 all_params = @cls_meth_params[init_ci].split("|")
30383041 if init_idx < all_ptypes.length
30393042 ptypes = all_ptypes[init_idx].split(",")
3040- pnames = []
3043+ pnames = "".split(",")
30413044 if init_idx < all_params.length
30423045 pnames = all_params[init_idx].split(",")
30433046 end
@@ -3200,8 +3203,9 @@
32003203 # Synthetic struct - update ivar types from init param types
32013204 all_params = @cls_meth_params[i].split("|")
32023205 all_ptypes = @cls_meth_ptypes[i].split("|")
3203- pnames = []
3204- ptypes = []
3206+ pnames = "".split(",")
3207+ ptypes = "".split(",")
3208+
32053209 if init_idx2 < all_params.length
32063210 pnames = all_params[init_idx2].split(",")
32073211 end
@@ -3232,8 +3236,9 @@
32323236 init_idx = mi
32333237 all_params = @cls_meth_params[i].split("|")
32343238 all_ptypes = @cls_meth_ptypes[i].split("|")
3235- pnames = []
3236- ptypes = []
3239+ pnames = "".split(",")
3240+ ptypes = "".split(",")
3241+
32373242 if init_idx < all_params.length
32383243 pnames = all_params[init_idx].split(",")
32393244 end
@@ -3307,8 +3312,9 @@
33073312 j = 0
33083313 while j < mnames.length
33093314 if mnames[j] != "initialize"
3310- pnames = []
3311- ptypes = []
3315+ pnames = "".split(",")
3316+ ptypes = "".split(",")
3317+
33123318 if j < all_params.length
33133319 pnames = all_params[j].split(",")
33143320 end
@@ -3455,8 +3461,9 @@
34553461 j = j + 1
34563462 end
34573463 if @meth_body_ids[i] >= 0
3458- ml = []
3459- mt = []
3464+ ml = "".split(",")
3465+ mt = "".split(",")
3466+
34603467 scan_locals(@meth_body_ids[i], ml, mt, pnames)
34613468 lk = 0
34623469 while lk < ml.length
@@ -3615,8 +3622,9 @@
36153622 j = 0
36163623 while j < mnames.length
36173624 push_scope
3618- pnames = []
3619- ptypes = []
3625+ pnames = "".split(",")
3626+ ptypes = "".split(",")
3627+
36203628 if j < all_params.length
36213629 pnames = all_params[j].split(",")
36223630 end
@@ -3657,6 +3665,28 @@
36573665 if j < bodies.length
36583666 bid = bodies[j].to_i
36593667 end
3668+ # Declare locals for better return type inference
3669+ if bid >= 0
3670+ rlnames = "".split(",")
3671+ rltypes = "".split(",")
3672+ scan_locals_first_type(bid, rlnames, rltypes, pnames)
3673+ rlk = 0
3674+ while rlk < rlnames.length
3675+ declare_var(rlnames[rlk], rltypes[rlk])
3676+ rlk = rlk + 1
3677+ end
3678+ # Second pass with locals in scope
3679+ rlnames2 = "".split(",")
3680+ rltypes2 = "".split(",")
3681+ scan_locals_first_type(bid, rlnames2, rltypes2, pnames)
3682+ rlk2 = 0
3683+ while rlk2 < rlnames2.length
3684+ if rltypes2[rlk2] != "int"
3685+ set_var_type(rlnames2[rlk2], rltypes2[rlk2])
3686+ end
3687+ rlk2 = rlk2 + 1
3688+ end
3689+ end
36603690 rt = "int"
36613691 if mnames[j] == "initialize"
36623692 rt = "void"
@@ -4456,10 +4486,268 @@
44564486 end
44574487 end
44584488
4489+ def scan_locals_first_type(nid, names, types, params)
4490+ # Like scan_locals but never marks poly - just keeps first type seen
4491+ if nid < 0
4492+ return
4493+ end
4494+ if @nd_type[nid] == "LocalVariableWriteNode"
4495+ lname = @nd_name[nid]
4496+ if not_in(lname, names) == 1
4497+ if not_in(lname, params) == 1
4498+ names.push(lname)
4499+ types.push(infer_type(@nd_expression[nid]))
4500+ end
4501+ end
4502+ end
4503+ if @nd_type[nid] == "LocalVariableOperatorWriteNode"
4504+ lname = @nd_name[nid]
4505+ if not_in(lname, names) == 1
4506+ if not_in(lname, params) == 1
4507+ names.push(lname)
4508+ types.push("int")
4509+ end
4510+ end
4511+ end
4512+ if @nd_type[nid] == "MultiWriteNode"
4513+ targets = parse_id_list(@nd_targets[nid])
4514+ k = 0
4515+ while k < targets.length
4516+ tid = targets[k]
4517+ if @nd_type[tid] == "LocalVariableTargetNode"
4518+ lname = @nd_name[tid]
4519+ if not_in(lname, names) == 1
4520+ if not_in(lname, params) == 1
4521+ names.push(lname)
4522+ types.push("int")
4523+ end
4524+ end
4525+ end
4526+ k = k + 1
4527+ end
4528+ end
4529+ # Recurse
4530+ if @nd_body[nid] >= 0
4531+ scan_locals_first_type(@nd_body[nid], names, types, params)
4532+ end
4533+ stmts = parse_id_list(@nd_stmts[nid])
4534+ k = 0
4535+ while k < stmts.length
4536+ scan_locals_first_type(stmts[k], names, types, params)
4537+ k = k + 1
4538+ end
4539+ if @nd_expression[nid] >= 0
4540+ scan_locals_first_type(@nd_expression[nid], names, types, params)
4541+ end
4542+ if @nd_predicate[nid] >= 0
4543+ scan_locals_first_type(@nd_predicate[nid], names, types, params)
4544+ end
4545+ if @nd_subsequent[nid] >= 0
4546+ scan_locals_first_type(@nd_subsequent[nid], names, types, params)
4547+ end
4548+ if @nd_else_clause[nid] >= 0
4549+ scan_locals_first_type(@nd_else_clause[nid], names, types, params)
4550+ end
4551+ if @nd_receiver[nid] >= 0
4552+ scan_locals_first_type(@nd_receiver[nid], names, types, params)
4553+ end
4554+ if @nd_arguments[nid] >= 0
4555+ scan_locals_first_type(@nd_arguments[nid], names, types, params)
4556+ end
4557+ args = parse_id_list(@nd_args[nid])
4558+ k = 0
4559+ while k < args.length
4560+ scan_locals_first_type(args[k], names, types, params)
4561+ k = k + 1
4562+ end
4563+ conds = parse_id_list(@nd_conditions[nid])
4564+ k = 0
4565+ while k < conds.length
4566+ scan_locals_first_type(conds[k], names, types, params)
4567+ k = k + 1
4568+ end
4569+ if @nd_left[nid] >= 0
4570+ scan_locals_first_type(@nd_left[nid], names, types, params)
4571+ end
4572+ if @nd_right[nid] >= 0
4573+ scan_locals_first_type(@nd_right[nid], names, types, params)
4574+ end
4575+ if @nd_block[nid] >= 0
4576+ scan_locals_first_type(@nd_block[nid], names, types, params)
4577+ end
4578+ end
4579+
4580+ def infer_class_body_call_types
4581+ # Scan class method bodies for calls to other methods in the same class.
4582+ # Update called method param types from argument types at call sites.
4583+ # Run multiple passes for propagation.
4584+ pass = 0
4585+ while pass < 3
4586+ ci = 0
4587+ while ci < @cls_names.length
4588+ mnames = @cls_meth_names[ci].split(";")
4589+ all_params = @cls_meth_params[ci].split("|")
4590+ all_ptypes = @cls_meth_ptypes[ci].split("|")
4591+ bodies = @cls_meth_bodies[ci].split(";")
4592+ mi = 0
4593+ while mi < mnames.length
4594+ bid = -1
4595+ if mi < bodies.length
4596+ bid = bodies[mi].to_i
4597+ end
4598+ if bid >= 0
4599+ @current_class_idx = ci
4600+ push_scope
4601+ # Declare params in scope with current types
4602+ pnames_arr = "".split(",")
4603+ if mi < all_params.length
4604+ pnames_arr = all_params[mi].split(",")
4605+ end
4606+ ptypes_arr = "".split(",")
4607+ if mi < all_ptypes.length
4608+ ptypes_arr = all_ptypes[mi].split(",")
4609+ end
4610+ pk = 0
4611+ while pk < pnames_arr.length
4612+ pt = "int"
4613+ if pk < ptypes_arr.length
4614+ pt = ptypes_arr[pk]
4615+ end
4616+ if pnames_arr[pk] != ""
4617+ declare_var(pnames_arr[pk], pt)
4618+ end
4619+ pk = pk + 1
4620+ end
4621+ # Scan locals using first-type-only (no poly marking)
4622+ lnames = "".split(",")
4623+ ltypes = "".split(",")
4624+ scan_locals_first_type(bid, lnames, ltypes, pnames_arr)
4625+ lk = 0
4626+ while lk < lnames.length
4627+ declare_var(lnames[lk], ltypes[lk])
4628+ lk = lk + 1
4629+ end
4630+ # Second pass: rescan with locals now in scope for better inference
4631+ lnames2 = "".split(",")
4632+ ltypes2 = "".split(",")
4633+ scan_locals_first_type(bid, lnames2, ltypes2, pnames_arr)
4634+ lk2 = 0
4635+ while lk2 < lnames2.length
4636+ if ltypes2[lk2] != "int"
4637+ set_var_type(lnames2[lk2], ltypes2[lk2])
4638+ end
4639+ lk2 = lk2 + 1
4640+ end
4641+ # Scan for calls to other methods in same class
4642+ scan_cls_method_calls(ci, bid)
4643+ pop_scope
4644+ @current_class_idx = -1
4645+ end
4646+ mi = mi + 1
4647+ end
4648+ ci = ci + 1
4649+ end
4650+ pass = pass + 1
4651+ end
4652+ end
4653+
4654+ def scan_cls_method_calls(ci, nid)
4655+ if nid < 0
4656+ return
4657+ end
4658+ if @nd_type[nid] == "CallNode"
4659+ mname = @nd_name[nid]
4660+ # Handle implicit self calls (no receiver) to same-class methods
4661+ if @nd_receiver[nid] < 0
4662+ midx = cls_find_method_direct(ci, mname)
4663+ if midx >= 0
4664+ args_id = @nd_arguments[nid]
4665+ if args_id >= 0
4666+ arg_ids = get_args(args_id)
4667+ all_ptypes = @cls_meth_ptypes[ci].split("|")
4668+ if midx < all_ptypes.length
4669+ ptypes = all_ptypes[midx].split(",")
4670+ kk = 0
4671+ while kk < arg_ids.length
4672+ at = infer_type(arg_ids[kk])
4673+ if kk < ptypes.length
4674+ if ptypes[kk] == "int"
4675+ if at != "int"
4676+ ptypes[kk] = at
4677+ end
4678+ end
4679+ end
4680+ kk = kk + 1
4681+ end
4682+ all_ptypes[midx] = join_sep(ptypes, ",")
4683+ @cls_meth_ptypes[ci] = join_sep(all_ptypes, "|")
4684+ end
4685+ end
4686+ end
4687+ end
4688+ end
4689+ # Recurse into children
4690+ if @nd_body[nid] >= 0
4691+ scan_cls_method_calls(ci, @nd_body[nid])
4692+ end
4693+ stmts = parse_id_list(@nd_stmts[nid])
4694+ k = 0
4695+ while k < stmts.length
4696+ scan_cls_method_calls(ci, stmts[k])
4697+ k = k + 1
4698+ end
4699+ if @nd_receiver[nid] >= 0
4700+ scan_cls_method_calls(ci, @nd_receiver[nid])
4701+ end
4702+ if @nd_arguments[nid] >= 0
4703+ scan_cls_method_calls(ci, @nd_arguments[nid])
4704+ end
4705+ args = parse_id_list(@nd_args[nid])
4706+ k = 0
4707+ while k < args.length
4708+ scan_cls_method_calls(ci, args[k])
4709+ k = k + 1
4710+ end
4711+ if @nd_expression[nid] >= 0
4712+ scan_cls_method_calls(ci, @nd_expression[nid])
4713+ end
4714+ if @nd_predicate[nid] >= 0
4715+ scan_cls_method_calls(ci, @nd_predicate[nid])
4716+ end
4717+ if @nd_subsequent[nid] >= 0
4718+ scan_cls_method_calls(ci, @nd_subsequent[nid])
4719+ end
4720+ if @nd_else_clause[nid] >= 0
4721+ scan_cls_method_calls(ci, @nd_else_clause[nid])
4722+ end
4723+ if @nd_left[nid] >= 0
4724+ scan_cls_method_calls(ci, @nd_left[nid])
4725+ end
4726+ if @nd_right[nid] >= 0
4727+ scan_cls_method_calls(ci, @nd_right[nid])
4728+ end
4729+ if @nd_block[nid] >= 0
4730+ scan_cls_method_calls(ci, @nd_block[nid])
4731+ end
4732+ elems = parse_id_list(@nd_elements[nid])
4733+ k = 0
4734+ while k < elems.length
4735+ scan_cls_method_calls(ci, elems[k])
4736+ k = k + 1
4737+ end
4738+ conds = parse_id_list(@nd_conditions[nid])
4739+ k = 0
4740+ while k < conds.length
4741+ scan_cls_method_calls(ci, conds[k])
4742+ k = k + 1
4743+ end
4744+ end
4745+
44594746 def compile
44604747 collect_all
44614748 infer_main_call_types
44624749 infer_function_body_call_types
4750+ infer_class_body_call_types
44634751 detect_poly_params
44644752 detect_poly_locals
44654753 # Re-infer return types after main call type fixes
@@ -4589,8 +4877,8 @@
45894877
45904878 def emit_gc_runtime
45914879 emit_raw("typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); unsigned marked : 1; } sp_gc_hdr;")
4592- emit_raw("static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;")
4593- emit_raw("#define SP_GC_STACK_MAX 8192")
4880+ 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;")
4881+ emit_raw("#define SP_GC_STACK_MAX 65536")
45944882 emit_raw("static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;")
45954883 emit_raw("#define SP_GC_SAVE() int _gc_saved = sp_gc_nroots")
45964884 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)")
@@ -4680,7 +4968,7 @@
46804968 emit_raw("static mrb_bool sp_str_include(const char*s,const char*sub){return strstr(s,sub)!=NULL;}")
46814969 emit_raw("static mrb_bool sp_str_start_with(const char*s,const char*p){return strncmp(s,p,strlen(p))==0;}")
46824970 emit_raw("static mrb_bool sp_str_end_with(const char*s,const char*suf){size_t ls=strlen(s),lsuf=strlen(suf);if(lsuf>ls)return FALSE;return strcmp(s+ls-lsuf,suf)==0;}")
4683- emit_raw("static sp_StrArray*sp_str_split(const char*s,const char*sep){sp_StrArray*a=sp_StrArray_new();size_t sl=strlen(sep);if(sl==0){for(size_t i=0;s[i];i++){char*c=(char*)malloc(2);c[0]=s[i];c[1]=0;sp_StrArray_push(a,c);}return a;}const char*p=s;while(1){const char*f=strstr(p,sep);if(!f){char*r=(char*)malloc(strlen(p)+1);strcpy(r,p);sp_StrArray_push(a,r);break;}size_t n=f-p;char*r=(char*)malloc(n+1);memcpy(r,p,n);r[n]=0;sp_StrArray_push(a,r);p=f+sl;}return a;}")
4971+ emit_raw("static sp_StrArray*sp_str_split(const char*s,const char*sep){sp_StrArray*a=sp_StrArray_new();if(*s==0)return a;size_t sl=strlen(sep);if(sl==0){for(size_t i=0;s[i];i++){char*c=(char*)malloc(2);c[0]=s[i];c[1]=0;sp_StrArray_push(a,c);}return a;}const char*p=s;while(1){const char*f=strstr(p,sep);if(!f){char*r=(char*)malloc(strlen(p)+1);strcpy(r,p);sp_StrArray_push(a,r);break;}size_t n=f-p;char*r=(char*)malloc(n+1);memcpy(r,p,n);r[n]=0;sp_StrArray_push(a,r);p=f+sl;}return a;}")
46844972 emit_raw("static const char*sp_str_gsub(const char*s,const char*pat,const char*rep){size_t pl=strlen(pat),rl=strlen(rep),sl=strlen(s);if(pl==0)return s;size_t cap=sl*2+1;char*out=(char*)malloc(cap);size_t ol=0;const char*p=s;while(*p){const char*f=strstr(p,pat);if(!f){size_t n=strlen(p);if(ol+n>=cap){cap=(ol+n)*2+1;out=(char*)realloc(out,cap);}memcpy(out+ol,p,n);ol+=n;break;}size_t n=f-p;if(ol+n+rl>=cap){cap=(ol+n+rl)*2+1;out=(char*)realloc(out,cap);}memcpy(out+ol,p,n);ol+=n;memcpy(out+ol,rep,rl);ol+=rl;p=f+pl;}out[ol]=0;return out;}")
46854973 emit_raw("static mrb_int sp_str_index(const char*s,const char*sub){const char*f=strstr(s,sub);if(!f)return -1;return(mrb_int)(f-s);}")
46864974 emit_raw("static const char*sp_str_sub_range(const char*s,mrb_int start,mrb_int len){mrb_int sl=(mrb_int)strlen(s);if(start<0)start+=sl;if(start<0)start=0;if(start>=sl)return\"\";if(len<0)len=0;if(start+len>sl)len=sl-start;char*r=(char*)malloc(len+1);memcpy(r,s+start,len);r[len]=0;return r;}")
@@ -5099,8 +5387,9 @@
50995387 init_idx = cls_find_method_direct(init_ci, "initialize")
51005388 all_params = @cls_meth_params[init_ci].split("|")
51015389 all_ptypes = @cls_meth_ptypes[init_ci].split("|")
5102- pnames = []
5103- ptypes = []
5390+ pnames = "".split(",")
5391+ ptypes = "".split(",")
5392+
51045393 if init_idx < all_params.length
51055394 pnames = all_params[init_idx].split(",")
51065395 end
@@ -5137,8 +5426,9 @@
51375426 end
51385427 all_params = @cls_meth_params[init_ci].split("|")
51395428 all_ptypes = @cls_meth_ptypes[init_ci].split("|")
5140- pnames = []
5141- ptypes = []
5429+ pnames = "".split(",")
5430+ ptypes = "".split(",")
5431+
51425432 if init_idx < all_params.length
51435433 pnames = all_params[init_idx].split(",")
51445434 end
@@ -5159,8 +5449,9 @@
51595449 end
51605450
51615451 def method_with_self_params(midx, all_params, all_ptypes)
5162- pnames = []
5163- ptypes = []
5452+ pnames = "".split(",")
5453+ ptypes = "".split(",")
5454+
51645455 if midx < all_params.length
51655456 pnames = all_params[midx].split(",")
51665457 end
@@ -5181,8 +5472,9 @@
51815472 end
51825473
51835474 def cls_method_params_decl(midx, all_params, all_ptypes)
5184- pnames = []
5185- ptypes = []
5475+ pnames = "".split(",")
5476+ ptypes = "".split(",")
5477+
51865478 if midx < all_params.length
51875479 pnames = all_params[midx].split(",")
51885480 end
@@ -5229,8 +5521,8 @@
52295521 if j < bodies.length
52305522 bid = bodies[j].to_i
52315523 end
5232- pnames = []
5233- ptypes = []
5524+ pnames = "".split(",")
5525+ ptypes = "".split(",")
52345526 if j < all_params.length
52355527 pnames = all_params[j].split(",")
52365528 end
@@ -5257,8 +5549,9 @@
52575549 if j < cm_bodies.length
52585550 bid = cm_bodies[j].to_i
52595551 end
5260- pnames = []
5261- ptypes = []
5552+ pnames = "".split(",")
5553+ ptypes = "".split(",")
5554+
52625555 if j < cm_params.length
52635556 pnames = cm_params[j].split(",")
52645557 end
@@ -5295,7 +5588,7 @@
52955588 if bid == -2
52965589 # Synthetic struct constructor
52975590 all_params = @cls_meth_params[ci].split("|")
5298- pnames2 = []
5591+ pnames2 = "".split(",")
52995592 if init_idx < all_params.length
53005593 pnames2 = all_params[init_idx].split(",")
53015594 end
@@ -5309,8 +5602,9 @@
53095602 @current_class_idx = ci
53105603 all_params = @cls_meth_params[ci].split("|")
53115604 all_ptypes = @cls_meth_ptypes[ci].split("|")
5312- pnames = []
5313- ptypes = []
5605+ pnames = "".split(",")
5606+ ptypes = "".split(",")
5607+
53145608 if init_idx < all_params.length
53155609 pnames = all_params[init_idx].split(",")
53165610 end
@@ -5370,7 +5664,7 @@
53705664 # Build param forwarding: forward all constructor params to parent init
53715665 pi_params = @cls_meth_params[init_ci].split("|")
53725666 pi_idx = cls_find_method_direct(init_ci, "initialize")
5373- pnames = []
5667+ pnames = "".split(",")
53745668 if pi_idx >= 0
53755669 if pi_idx < pi_params.length
53765670 pnames = pi_params[pi_idx].split(",")
@@ -5407,8 +5701,9 @@
54075701 @current_class_idx = ci
54085702 all_params = @cls_meth_params[ci].split("|")
54095703 all_ptypes = @cls_meth_ptypes[ci].split("|")
5410- pnames = []
5411- ptypes = []
5704+ pnames = "".split(",")
5705+ ptypes = "".split(",")
5706+
54125707 if init_idx < all_params.length
54135708 pnames = all_params[init_idx].split(",")
54145709 end
@@ -5644,8 +5939,9 @@
56445939 end
56455940
56465941 def declare_method_locals(bid, params)
5647- lnames = []
5648- ltypes = []
5942+ lnames = "".split(",")
5943+ ltypes = "".split(",")
5944+
56495945 scan_locals(bid, lnames, ltypes, params)
56505946 # Declare all locals first so block param inference can see them
56515947 j = 0
@@ -5654,8 +5950,9 @@
56545950 j = j + 1
56555951 end
56565952 # Second pass: re-scan with types now in scope for better block param inference
5657- lnames2 = []
5658- ltypes2 = []
5953+ lnames2 = "".split(",")
5954+ ltypes2 = "".split(",")
5955+
56595956 scan_locals(bid, lnames2, ltypes2, params)
56605957 # Update types that may have improved
56615958 j = 0
@@ -5674,12 +5971,36 @@
56745971 end
56755972 j = j + 1
56765973 end
5677- # Emit declarations
5974+ # Emit declarations and GC rooting for pointer locals
5975+ has_gc_locals = 0
5976+ j = 0
5977+ while j < lnames.length
5978+ if type_is_pointer(ltypes[j]) == 1
5979+ has_gc_locals = 1
5980+ end
5981+ j = j + 1
5982+ end
5983+ if has_gc_locals == 1
5984+ if @needs_gc == 1
5985+ emit(" SP_GC_SAVE();")
5986+ end
5987+ end
56785988 j = 0
56795989 while j < lnames.length
56805990 emit(" " + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
56815991 j = j + 1
56825992 end
5993+ if has_gc_locals == 1
5994+ if @needs_gc == 1
5995+ j = 0
5996+ while j < lnames.length
5997+ if type_is_pointer(ltypes[j]) == 1
5998+ emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")
5999+ end
6000+ j = j + 1
6001+ end
6002+ end
6003+ end
56836004 end
56846005
56856006 def scan_locals(nid, names, types, params)
@@ -5723,8 +6044,15 @@
57236044 if names[ki] == lname
57246045 if types[ki] != at
57256046 if types[ki] != "poly"
5726- types[ki] = "poly"
5727- @needs_rb_value = 1
6047+ # Don't mark poly if new type is fallback "int" and existing is richer
6048+ if at != "int"
6049+ if types[ki] == "int"
6050+ types[ki] = at
6051+ else
6052+ types[ki] = "poly"
6053+ @needs_rb_value = 1
6054+ end
6055+ end
57286056 end
57296057 end
57306058 end
@@ -5984,9 +6312,10 @@
59846312 push_scope
59856313
59866314 # Pre-declare main locals
5987- lnames = []
5988- ltypes = []
5989- empty_params = []
6315+ lnames = "".split(",")
6316+ ltypes = "".split(",")
6317+
6318+ empty_params = "".split(",")
59906319 i = 0
59916320 while i < stmts.length
59926321 sid = stmts[i]
@@ -6007,8 +6336,9 @@
60076336 j = j + 1
60086337 end
60096338 # Second pass with vars in scope
6010- lnames2 = []
6011- ltypes2 = []
6339+ lnames2 = "".split(",")
6340+ ltypes2 = "".split(",")
6341+
60126342 i = 0
60136343 while i < stmts.length
60146344 sid = stmts[i]
@@ -6437,7 +6767,7 @@
64376767 return "\"\""
64386768 end
64396769 fmt = ""
6440- arg_exprs = []
6770+ arg_exprs = "".split(",")
64416771 i = 0
64426772 while i < parts.length
64436773 pid = parts[i]
@@ -8244,7 +8574,7 @@
82448574 return compile_call_args(nid)
82458575 end
82468576 all_params = @cls_meth_params[init_ci].split("|")
8247- pnames = []
8577+ pnames = "".split(",")
82488578 if init_idx < all_params.length
82498579 pnames = all_params[init_idx].split(",")
82508580 end