Commit 167cac04
167cac045044Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: Phase 5 - blocks, yield, block_given?, map, select
Add yield/block support via inline expansion at call sites. Yield-using functions get _block/_benv params; yield becomes _block(val, _benv). block_given? compiles to (_block != NULL). Add map/select block compilation returning new arrays. Fix param type inference by scanning main-level call sites.
Tests passing: bm_yield, bm_block2, bm_enumerable
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 167cac045044
- Parent
- ff9eca606f09
spinel_codegen.rbmodifiedView file
@@ -108,6 +108,15 @@
108108
@current_method_return = ""109109
@in_main = 0110110
@in_loop = 0111+
@in_yield_method = 0112+
113+
# Yield/block tracking (parallel with meth_names / cls_meth_names)114+
@meth_has_yield = []115+
@cls_meth_has_yield = "".split(",")116+
117+
# Block function accumulator (emitted before forward decls)118+
@block_funcs = ""119+
@block_counter = 0111120
112121
# Feature flags113122
@needs_gc = 0@@ -116,6 +125,10 @@
116125
@needs_str_int_hash = 0117126
@needs_str_str_hash = 0118127
@needs_string_helpers = 0128+
@needs_setjmp = 0129+
@needs_file_io = 0130+
@needs_mutable_str = 0131+
@needs_rb_value = 0119132
end120133
121134
def join_sep(arr, sep)@@ -1020,6 +1033,18 @@
10201033
end10211034
return "int_array"10221035
end1036+
if mname == "map"1037+
if recv >= 01038+
return infer_type(recv)1039+
end1040+
return "int_array"1041+
end1042+
if mname == "select"1043+
if recv >= 01044+
return infer_type(recv)1045+
end1046+
return "int_array"1047+
end10231048
if mname == "[]"10241049
if recv >= 010251050
rt = infer_type(recv)@@ -1316,6 +1341,7 @@
13161341
@cls_cmeth_ptypes.push("")13171342
@cls_cmeth_returns.push("")13181343
@cls_cmeth_bodies.push("")1344+
@cls_meth_has_yield.push("")13191345
@needs_gc = 113201346
13211347
# Collect class body@@ -1359,7 +1385,14 @@
13591385
params_str = collect_params_str(nid)13601386
ptypes_str = collect_ptypes_str(nid, ci)13611387
defaults_str = collect_defaults_str(nid)1388+
has_y = body_has_yield(body_id)13621389
append_cls_meth(ci, mname, params_str, ptypes_str, "int", body_id, defaults_str)1390+
# Track yield info1391+
if @cls_meth_has_yield[ci] != ""1392+
@cls_meth_has_yield[ci] = @cls_meth_has_yield[ci] + ";" + has_y.to_s1393+
else1394+
@cls_meth_has_yield[ci] = has_y.to_s1395+
end13631396
return13641397
end13651398
@@ -1701,7 +1734,14 @@
17011734
return infer_hash_val_type(nid)17021735
end17031736
if t == "CallNode"1704-
if @nd_name[nid] == "new"1737+
mname = @nd_name[nid]1738+
if mname == "to_a"1739+
return "int_array"1740+
end1741+
if mname == "split"1742+
return "str_array"1743+
end1744+
if mname == "new"17051745
r = @nd_receiver[nid]17061746
if r >= 017071747
if @nd_type[r] == "ConstantReadNode"@@ -1760,6 +1800,7 @@
17601800
@meth_return_types.push("int")17611801
@meth_body_ids.push(body_id)17621802
@meth_has_defaults.push(defaults_str)1803+
@meth_has_yield.push(body_has_yield(body_id))17631804
end17641805
17651806
def collect_constant(nid)@@ -1773,6 +1814,105 @@
17731814
@const_expr_ids.push(expr_id)17741815
end17751816
1817+
# ---- Yield detection ----1818+
def body_has_yield(nid)1819+
if nid < 01820+
return 01821+
end1822+
if @nd_type[nid] == "YieldNode"1823+
return 11824+
end1825+
if @nd_type[nid] == "CallNode"1826+
if @nd_name[nid] == "block_given?"1827+
return 11828+
end1829+
end1830+
# Don't recurse into nested DefNode (that's a different method)1831+
if @nd_type[nid] == "DefNode"1832+
return 01833+
end1834+
if @nd_type[nid] == "BlockNode"1835+
# Don't look inside blocks of other calls for yield1836+
# Actually we do - yield inside a block still belongs to enclosing method1837+
end1838+
# Recurse children1839+
if @nd_body[nid] >= 01840+
if body_has_yield(@nd_body[nid]) == 11841+
return 11842+
end1843+
end1844+
stmts = parse_id_list(@nd_stmts[nid])1845+
k = 01846+
while k < stmts.length1847+
if body_has_yield(stmts[k]) == 11848+
return 11849+
end1850+
k = k + 11851+
end1852+
if @nd_expression[nid] >= 01853+
if body_has_yield(@nd_expression[nid]) == 11854+
return 11855+
end1856+
end1857+
if @nd_predicate[nid] >= 01858+
if body_has_yield(@nd_predicate[nid]) == 11859+
return 11860+
end1861+
end1862+
if @nd_subsequent[nid] >= 01863+
if body_has_yield(@nd_subsequent[nid]) == 11864+
return 11865+
end1866+
end1867+
if @nd_else_clause[nid] >= 01868+
if body_has_yield(@nd_else_clause[nid]) == 11869+
return 11870+
end1871+
end1872+
if @nd_left[nid] >= 01873+
if body_has_yield(@nd_left[nid]) == 11874+
return 11875+
end1876+
end1877+
if @nd_right[nid] >= 01878+
if body_has_yield(@nd_right[nid]) == 11879+
return 11880+
end1881+
end1882+
if @nd_block[nid] >= 01883+
if body_has_yield(@nd_block[nid]) == 11884+
return 11885+
end1886+
end1887+
conds = parse_id_list(@nd_conditions[nid])1888+
k = 01889+
while k < conds.length1890+
if body_has_yield(conds[k]) == 11891+
return 11892+
end1893+
k = k + 11894+
end1895+
args = parse_id_list(@nd_args[nid])1896+
k = 01897+
while k < args.length1898+
if body_has_yield(args[k]) == 11899+
return 11900+
end1901+
k = k + 11902+
end1903+
if @nd_arguments[nid] >= 01904+
if body_has_yield(@nd_arguments[nid]) == 11905+
return 11906+
end1907+
end1908+
if @nd_receiver[nid] >= 01909+
if body_has_yield(@nd_receiver[nid]) == 11910+
return 11911+
end1912+
end1913+
01914+
end1915+
17761916
# ---- Return type inference ----17771917
def infer_constructor_types17781918
# Scan AST for ClassName.new(args) calls and infer param types@@ -2458,8 +2598,41 @@
24582598
end24592599
24602600
# ---- Code generation ----2601+
def infer_main_call_types2602+
# Scan main-level code for function calls and infer param types from arguments2603+
stmts = get_body_stmts(@root_id)2604+
# First, figure out main local types2605+
push_scope2606+
lnames = "".split(",")2607+
ltypes = "".split(",")2608+
empty_p = "".split(",")2609+
i = 02610+
while i < stmts.length2611+
sid = stmts[i]2612+
if @nd_type[sid] != "DefNode"2613+
if @nd_type[sid] != "ClassNode"2614+
if @nd_type[sid] != "ConstantWriteNode"2615+
scan_locals(sid, lnames, ltypes, empty_p)2616+
end2617+
end2618+
end2619+
i = i + 12620+
end2621+
k = 02622+
while k < lnames.length2623+
declare_var(lnames[k], ltypes[k])2624+
k = k + 12625+
end2626+
# Now scan call sites to update param types2627+
scan_new_calls(@root_id)2628+
pop_scope2629+
end2630+
24612631
def compile24622632
collect_all2633+
infer_main_call_types2634+
# Re-infer return types after main call type fixes2635+
infer_all_returns24632636
detect_features24642637
generate_code24652638
end@@ -2740,10 +2913,18 @@
27402913
27412914
# ---- Forward declarations ----27422915
def emit_forward_decls2916+
# Emit block helper functions accumulated during collection2917+
if @block_funcs != ""2918+
emit_raw(@block_funcs)2919+
end27432920
# Top-level methods27442921
i = 027452922
while i < @meth_names.length2746-
emit_raw("static " + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + ");")2923+
yp = ""2924+
if @meth_has_yield[i] == 12925+
yp = yield_params_suffix(i)2926+
end2927+
emit_raw("static " + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + yp + ");")27472928
i = i + 127482929
end27492930
# Class methods@@ -2768,7 +2949,11 @@
27682949
if j < returns.length27692950
rt = returns[j]27702951
end2771-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + " *self" + method_with_self_params(j, all_params, all_ptypes) + ");")2952+
yp = ""2953+
if cls_method_has_yield(i, j) == 12954+
yp = yield_params_suffix_cls(i, j)2955+
end2956+
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + " *self" + method_with_self_params(j, all_params, all_ptypes) + yp + ");")27722957
end27732958
j = j + 127742959
end@@ -2791,6 +2976,29 @@
27912976
emit_raw("")27922977
end27932978
2979+
def yield_params_suffix(mi)2980+
pnames = @meth_param_names[mi].split(",")2981+
if pnames.length == 02982+
return "void (*_block)(mrb_int, void*), void *_benv"2983+
end2984+
return ", void (*_block)(mrb_int, void*), void *_benv"2985+
end2986+
2987+
def yield_params_suffix_cls(ci, midx)2988+
# For class instance methods (always have self first)2989+
return ", void (*_block)(mrb_int, void*), void *_benv"2990+
end2991+
2992+
def cls_method_has_yield(ci, midx)2993+
ystr = @cls_meth_has_yield[ci].split(";")2994+
if midx < ystr.length2995+
if ystr[midx] == "1"2996+
return 12997+
end2998+
end2999+
03000+
end3001+
27943002
def cls_find_method_direct(ci, mname)27953003
mnames = @cls_meth_names[ci].split(";")27963004
j = 0@@ -3188,7 +3396,20 @@
31883396
@current_method_return = rt31893397
@indent = 131903398
3191-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + ") {")3399+
midx = cls_find_method_direct(ci, mname)3400+
if midx >= 03401+
if cls_method_has_yield(ci, midx) == 13402+
@in_yield_method = 13403+
else3404+
@in_yield_method = 03405+
end3406+
end3407+
3408+
yp = ""3409+
if @in_yield_method == 13410+
yp = yield_params_suffix_cls(ci, midx)3411+
end3412+
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + yp + ") {")31923413
31933414
push_scope31943415
j = 0@@ -3209,6 +3430,7 @@
32093430
pop_scope32103431
@current_class_idx = -132113432
@current_method_name = ""3433+
@in_yield_method = 032123434
@indent = 032133435
emit_raw("}")32143436
emit_raw("")@@ -3295,11 +3517,20 @@
32953517
@current_method_return = @meth_return_types[mi]32963518
@indent = 132973519
@in_main = 03520+
if @meth_has_yield[mi] == 13521+
@in_yield_method = 13522+
else3523+
@in_yield_method = 03524+
end32983525
32993526
pnames = @meth_param_names[mi].split(",")33003527
ptypes = @meth_param_types[mi].split(",")33013528
3302-
emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(@meth_names[mi]) + "(" + method_params_decl(mi) + ") {")3529+
yp = ""3530+
if @meth_has_yield[mi] == 13531+
yp = yield_params_suffix(mi)3532+
end3533+
emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(@meth_names[mi]) + "(" + method_params_decl(mi) + yp + ") {")33033534
33043535
push_scope33053536
j = 0@@ -3320,6 +3551,7 @@
33203551
33213552
pop_scope33223553
@current_method_name = ""3554+
@in_yield_method = 033233555
@indent = 033243556
emit_raw("}")33253557
emit_raw("")@@ -3901,9 +4133,19 @@
39014133
39024134
# No receiver39034135
if recv < 04136+
if mname == "block_given?"4137+
if @in_yield_method == 14138+
return "(_block != NULL)"4139+
end4140+
return "0"4141+
end39044142
mi = find_method_idx(mname)39054143
if mi >= 03906-
return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + ")"4144+
yargs = ""4145+
if @meth_has_yield[mi] == 14146+
yargs = ", NULL, NULL"4147+
end4148+
return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"39074149
end39084150
if @current_class_idx >= 039094151
cidx = cls_find_method(@current_class_idx, mname)@@ -4180,8 +4422,22 @@
41804422
end41814423
end41824424
4183-
# reject as expression4184-
if mname == "reject"4425+
# map as expression4426+
if mname == "map"4427+
if @nd_block[nid] >= 04428+
return compile_map_expr(nid)4429+
end4430+
end4431+
4432+
# select as expression4433+
if mname == "select"4434+
if @nd_block[nid] >= 04435+
return compile_select_expr(nid)4436+
end4437+
end4438+
4439+
# reject as expression4440+
if mname == "reject"41854441
if @nd_block[nid] >= 041864442
return compile_reject_expr(nid)41874443
end@@ -4599,6 +4855,10 @@
45994855
emit(" continue;")46004856
return46014857
end4858+
if t == "YieldNode"4859+
compile_yield_stmt(nid)4860+
return4861+
end46024862
if t == "CallNode"46034863
compile_call_stmt(nid)46044864
return@@ -4906,8 +5166,19 @@
49065166
# each with block49075167
if mname == "each"49085168
if @nd_block[nid] >= 04909-
compile_each_block(nid)4910-
return5169+
# For object types with yield-using each, use yield method call5170+
if recv >= 05171+
ert = infer_type(recv)5172+
if is_obj_type(ert) == 15173+
# Fall through to yield method handler below5174+
else5175+
compile_each_block(nid)5176+
return5177+
end5178+
else5179+
compile_each_block(nid)5180+
return5181+
end49115182
end49125183
end49135184
@@ -4992,6 +5263,65 @@
49925263
end49935264
end49945265
5266+
# map with block5267+
if mname == "map"5268+
if @nd_block[nid] >= 05269+
compile_map_block(nid)5270+
return5271+
end5272+
end5273+
5274+
# select with block5275+
if mname == "select"5276+
if @nd_block[nid] >= 05277+
compile_select_block(nid)5278+
return5279+
end5280+
end5281+
5282+
# User-defined yield function called with block5283+
if @nd_block[nid] >= 05284+
if recv < 05285+
mi = find_method_idx(mname)5286+
if mi >= 05287+
if @meth_has_yield[mi] == 15288+
compile_yield_call_stmt(nid, mi)5289+
return5290+
end5291+
end5292+
end5293+
# Class method with yield5294+
if recv >= 05295+
rtype = infer_type(recv)5296+
if is_obj_type(rtype) == 15297+
cn = rtype[4, rtype.length - 4]5298+
cci = find_class_idx(cn)5299+
if cci >= 05300+
midx = cls_find_method_direct(cci, mname)5301+
if midx >= 05302+
if cls_method_has_yield(cci, midx) == 15303+
compile_yield_method_call_stmt(nid, cci, midx, mname)5304+
return5305+
end5306+
end5307+
# Check parent5308+
if @cls_parents[cci] != ""5309+
pci = find_class_idx(@cls_parents[cci])5310+
if pci >= 05311+
pidx = cls_find_method_direct(pci, mname)5312+
if pidx >= 05313+
if cls_method_has_yield(pci, pidx) == 15314+
compile_yield_method_call_stmt(nid, pci, pidx, mname)5315+
return5316+
end5317+
end5318+
end5319+
end5320+
end5321+
end5322+
end5323+
end5324+
49955325
# General49965326
val = compile_expr(nid)49975327
if val != "0"@@ -5254,6 +5584,81 @@
52545584
@in_loop = old52555585
end52565586
5587+
def compile_map_expr(nid)5588+
# Emit the map loop as side effects, return result array5589+
compile_map_block(nid)5590+
# The map_block stores result in a temp - we need to return it5591+
# Actually, compile_map_block emits into current output but doesn't return the temp name.5592+
# Let's use a different approach: inline and return temp5593+
rt = infer_type(@nd_receiver[nid])5594+
rc = compile_expr(@nd_receiver[nid])5595+
bp1 = get_block_param(nid, 0)5596+
if bp1 == ""5597+
bp1 = "_x"5598+
end5599+
tmp_arr = new_temp5600+
tmp_i = new_temp5601+
if rt == "int_array"5602+
@needs_int_array = 15603+
@needs_gc = 15604+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")5605+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")5606+
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")5607+
@indent = @indent + 15608+
blk = @nd_block[nid]5609+
if blk >= 05610+
body = @nd_body[blk]5611+
if body >= 05612+
stmts = get_stmts(body)5613+
if stmts.length > 05614+
last = stmts[stmts.length - 1]5615+
val = compile_expr(last)5616+
emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")5617+
end5618+
end5619+
end5620+
@indent = @indent - 15621+
emit(" }")5622+
return tmp_arr5623+
end5624+
"0"5625+
end5626+
5627+
def compile_select_expr(nid)5628+
rt = infer_type(@nd_receiver[nid])5629+
rc = compile_expr(@nd_receiver[nid])5630+
bp1 = get_block_param(nid, 0)5631+
if bp1 == ""5632+
bp1 = "_x"5633+
end5634+
tmp_arr = new_temp5635+
tmp_i = new_temp5636+
if rt == "int_array"5637+
@needs_int_array = 15638+
@needs_gc = 15639+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")5640+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")5641+
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")5642+
@indent = @indent + 15643+
blk = @nd_block[nid]5644+
if blk >= 05645+
body = @nd_body[blk]5646+
if body >= 05647+
stmts = get_stmts(body)5648+
if stmts.length > 05649+
last = stmts[stmts.length - 1]5650+
cond = compile_expr(last)5651+
emit(" if (" + cond + ") sp_IntArray_push(" + tmp_arr + ", lv_" + bp1 + ");")5652+
end5653+
end5654+
end5655+
@indent = @indent - 15656+
emit(" }")5657+
return tmp_arr5658+
end5659+
"0"5660+
end5661+
52575662
def compile_reduce_expr(nid)52585663
# Emit the reduce loop as side effects, return accumulator52595664
compile_reduce_block(nid)@@ -5365,57 +5770,778 @@
53655770
end53665771
end53675772
5368-
def compile_stmts_body(nid)5369-
if nid < 05370-
return5773+
def compile_yield_stmt(nid)5774+
args_id = @nd_arguments[nid]5775+
arg_exprs = ""5776+
if args_id >= 05777+
aids = get_args(args_id)5778+
k = 05779+
while k < aids.length5780+
if k > 05781+
arg_exprs = arg_exprs + ", "5782+
end5783+
arg_exprs = arg_exprs + compile_expr(aids[k])5784+
k = k + 15785+
end53715786
end5372-
stmts = get_stmts(nid)5373-
k = 05374-
while k < stmts.length5375-
compile_stmt(stmts[k])5376-
k = k + 15787+
if arg_exprs != ""5788+
emit(" if (_block) _block(" + arg_exprs + ", _benv);")5789+
else5790+
emit(" if (_block) _block(0, _benv);")53775791
end53785792
end53795793
5380-
def compile_body_return(body_id, return_type)5381-
if body_id < 05382-
if return_type != "void"5383-
emit(" return " + c_default_val(return_type) + ";")5384-
end5794+
def compile_yield_call_stmt(nid, mi)5795+
# Call a yield-using top-level function with a block5796+
# Inline the function body, replacing yield with block body5797+
blk = @nd_block[nid]5798+
if blk < 053855799
return53865800
end5387-
stmts = get_stmts(body_id)5388-
if stmts.length == 05389-
if return_type != "void"5390-
emit(" return " + c_default_val(return_type) + ";")5801+
5802+
# Get block params5803+
bp_names = "".split(",")5804+
bp = @nd_parameters[blk]5805+
if bp >= 05806+
inner = @nd_parameters[bp]5807+
if inner >= 05808+
reqs = parse_id_list(@nd_requireds[inner])5809+
k = 05810+
while k < reqs.length5811+
bp_names.push(@nd_name[reqs[k]])5812+
k = k + 15813+
end53915814
end5392-
return53935815
end5394-
# All but last5395-
i = 05396-
while i < stmts.length - 15397-
compile_stmt(stmts[i])5398-
i = i + 15816+
5817+
args_id = @nd_arguments[nid]5818+
arg_ids = []5819+
if args_id >= 05820+
arg_ids = get_args(args_id)53995821
end5400-
last = stmts[stmts.length - 1]5401-
if @nd_type[last] == "ReturnNode"5402-
compile_return_stmt(last)5403-
return5404-
end5405-
if @nd_type[last] == "IfNode"5406-
compile_if_return(last, return_type)5407-
return5408-
end5409-
if @nd_type[last] == "CaseNode"5410-
compile_case_return(last, return_type)5411-
return5412-
end5413-
if @nd_type[last] == "WhileNode"5414-
compile_while_stmt(last)5415-
if return_type != "void"5416-
emit(" return " + c_default_val(return_type) + ";")5417-
end5418-
return5822+
5823+
# Declare and set the function's params as new temp vars5824+
pnames = @meth_param_names[mi].split(",")5825+
ptypes = @meth_param_types[mi].split(",")5826+
# Create unique temp names for function params to avoid collision5827+
@block_counter = @block_counter + 15828+
suffix = "_y" + @block_counter.to_s5829+
param_map_from = "".split(",")5830+
param_map_to = "".split(",")5831+
k = 05832+
while k < pnames.length5833+
pt = "int"5834+
if k < ptypes.length5835+
pt = ptypes[k]5836+
end5837+
tname = pnames[k] + suffix5838+
val = "0"5839+
if k < arg_ids.length5840+
val = compile_expr(arg_ids[k])5841+
end5842+
emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")5843+
param_map_from.push(pnames[k])5844+
param_map_to.push(tname)5845+
declare_var(tname, pt)5846+
k = k + 15847+
end5848+
5849+
# Also need to declare the function's local vars5850+
bid = @meth_body_ids[mi]5851+
if bid >= 05852+
flocals_n = "".split(",")5853+
flocals_t = "".split(",")5854+
scan_locals(bid, flocals_n, flocals_t, pnames)5855+
k = 05856+
while k < flocals_n.length5857+
tname = flocals_n[k] + suffix5858+
emit(" " + c_type(flocals_t[k]) + " lv_" + tname + " = " + c_default_val(flocals_t[k]) + ";")5859+
param_map_from.push(flocals_n[k])5860+
param_map_to.push(tname)5861+
declare_var(tname, flocals_t[k])5862+
k = k + 15863+
end5864+
end5865+
5866+
# Compile function body, replacing yield with block body5867+
# and renaming function locals to temp names5868+
if bid >= 05869+
stmts = get_stmts(bid)5870+
k = 05871+
while k < stmts.length5872+
compile_stmt_with_block(stmts[k], blk, bp_names, param_map_from, param_map_to)5873+
k = k + 15874+
end5875+
end5876+
end5877+
5878+
def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to)5879+
if nid < 05880+
return5881+
end5882+
t = @nd_type[nid]5883+
if t == "YieldNode"5884+
# Replace yield with the block body5885+
args_id = @nd_arguments[nid]5886+
if args_id >= 05887+
aids = get_args(args_id)5888+
k = 05889+
while k < aids.length5890+
if k < bp_names.length5891+
emit(" lv_" + bp_names[k] + " = " + compile_expr_remap(aids[k], map_from, map_to) + ";")5892+
end5893+
k = k + 15894+
end5895+
end5896+
body = @nd_body[blk]5897+
if body >= 05898+
stmts = get_stmts(body)5899+
sk = 05900+
while sk < stmts.length5901+
compile_stmt(stmts[sk])5902+
sk = sk + 15903+
end5904+
end5905+
return5906+
end5907+
if t == "LocalVariableWriteNode"5908+
lname = @nd_name[nid]5909+
rname = remap_local(lname, map_from, map_to)5910+
val = compile_expr_remap(@nd_expression[nid], map_from, map_to)5911+
emit(" lv_" + rname + " = " + val + ";")5912+
return5913+
end5914+
if t == "LocalVariableOperatorWriteNode"5915+
lname = @nd_name[nid]5916+
rname = remap_local(lname, map_from, map_to)5917+
op = @nd_binop[nid]5918+
val = compile_expr_remap(@nd_expression[nid], map_from, map_to)5919+
if op == "+"5920+
emit(" lv_" + rname + " += " + val + ";")5921+
end5922+
if op == "-"5923+
emit(" lv_" + rname + " -= " + val + ";")5924+
end5925+
if op == "*"5926+
emit(" lv_" + rname + " *= " + val + ";")5927+
end5928+
return5929+
end5930+
if t == "WhileNode"5931+
old = @in_loop5932+
@in_loop = 15933+
cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)5934+
emit(" while (" + cond + ") {")5935+
@indent = @indent + 15936+
body = @nd_body[nid]5937+
if body >= 05938+
stmts = get_stmts(body)5939+
sk = 05940+
while sk < stmts.length5941+
compile_stmt_with_block(stmts[sk], blk, bp_names, map_from, map_to)5942+
sk = sk + 15943+
end5944+
end5945+
@indent = @indent - 15946+
emit(" }")5947+
@in_loop = old5948+
return5949+
end5950+
if t == "IfNode"5951+
cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)5952+
emit(" if (" + cond + ") {")5953+
@indent = @indent + 15954+
body = @nd_body[nid]5955+
if body >= 05956+
stmts = get_stmts(body)5957+
sk = 05958+
while sk < stmts.length5959+
compile_stmt_with_block(stmts[sk], blk, bp_names, map_from, map_to)5960+
sk = sk + 15961+
end5962+
end5963+
@indent = @indent - 15964+
sub = @nd_subsequent[nid]5965+
if sub >= 05966+
if @nd_type[sub] == "ElseNode"5967+
emit(" } else {")5968+
@indent = @indent + 15969+
eb = @nd_body[sub]5970+
if eb >= 05971+
estmts = get_stmts(eb)5972+
sk = 05973+
while sk < estmts.length5974+
compile_stmt_with_block(estmts[sk], blk, bp_names, map_from, map_to)5975+
sk = sk + 15976+
end5977+
end5978+
@indent = @indent - 15979+
else5980+
emit(" } else")5981+
compile_stmt_with_block(sub, blk, bp_names, map_from, map_to)5982+
return5983+
end5984+
end5985+
emit(" }")5986+
return5987+
end5988+
if t == "CallNode"5989+
# Check if block_given? with remap5990+
if @nd_name[nid] == "block_given?"5991+
if @nd_receiver[nid] < 05992+
# In inlined context, block IS given, do nothing5993+
return5994+
end5995+
end5996+
# Handle nested each/times with yield inside block5997+
if @nd_block[nid] >= 05998+
mname2 = @nd_name[nid]5999+
if mname2 == "each"6000+
# Compile the each loop, but inside the block body, recurse with yield replacement6001+
compile_each_with_yield_inline(nid, blk, bp_names, map_from, map_to)6002+
return6003+
end6004+
end6005+
# Compile call with remapping for args6006+
val = compile_expr_remap(nid, map_from, map_to)6007+
if val != "0"6008+
emit(" " + val + ";")6009+
end6010+
return6011+
end6012+
if t == "ReturnNode"6013+
# In inlined context, return becomes a value assignment6014+
# but for simplicity, just skip6015+
return6016+
end6017+
# Default: for expression statements, use remap6018+
val = compile_expr_remap(nid, map_from, map_to)6019+
if val != "0"6020+
emit(" " + val + ";")6021+
end6022+
end6023+
6024+
def remap_local(name, map_from, map_to)6025+
k = 06026+
while k < map_from.length6027+
if map_from[k] == name6028+
return map_to[k]6029+
end6030+
k = k + 16031+
end6032+
name6033+
end6034+
6035+
def compile_expr_remap(nid, map_from, map_to)6036+
if nid < 06037+
return "0"6038+
end6039+
t = @nd_type[nid]6040+
if t == "LocalVariableReadNode"6041+
rname = remap_local(@nd_name[nid], map_from, map_to)6042+
return "lv_" + rname6043+
end6044+
if t == "InstanceVariableReadNode"6045+
# Remap self to the _yself variable6046+
self_name = remap_local("_self_", map_from, map_to)6047+
return self_name + "->" + sanitize_ivar(@nd_name[nid])6048+
end6049+
if t == "SelfNode"6050+
return remap_local("_self_", map_from, map_to)6051+
end6052+
if t == "CallNode"6053+
if @nd_name[nid] == "block_given?"6054+
if @nd_receiver[nid] < 06055+
return "1"6056+
end6057+
end6058+
# For operators with remapped locals6059+
mname = @nd_name[nid]6060+
recv = @nd_receiver[nid]6061+
if recv >= 06062+
if mname == "+"6063+
return "(" + compile_expr_remap(recv, map_from, map_to) + " + " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6064+
end6065+
if mname == "-"6066+
args_id = @nd_arguments[nid]6067+
if args_id < 06068+
return "(-" + compile_expr_remap(recv, map_from, map_to) + ")"6069+
end6070+
return "(" + compile_expr_remap(recv, map_from, map_to) + " - " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6071+
end6072+
if mname == "*"6073+
return "(" + compile_expr_remap(recv, map_from, map_to) + " * " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6074+
end6075+
if mname == "/"6076+
return "(" + compile_expr_remap(recv, map_from, map_to) + " / " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6077+
end6078+
if mname == "%"6079+
return "sp_imod(" + compile_expr_remap(recv, map_from, map_to) + ", " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6080+
end6081+
if mname == "<"6082+
return "(" + compile_expr_remap(recv, map_from, map_to) + " < " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6083+
end6084+
if mname == ">"6085+
return "(" + compile_expr_remap(recv, map_from, map_to) + " > " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6086+
end6087+
if mname == "<="6088+
return "(" + compile_expr_remap(recv, map_from, map_to) + " <= " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6089+
end6090+
if mname == ">="6091+
return "(" + compile_expr_remap(recv, map_from, map_to) + " >= " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6092+
end6093+
if mname == "=="6094+
return "(" + compile_expr_remap(recv, map_from, map_to) + " == " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6095+
end6096+
if mname == "!="6097+
return "(" + compile_expr_remap(recv, map_from, map_to) + " != " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"6098+
end6099+
end6100+
end6101+
# Fallback: compile normally6102+
compile_expr(nid)6103+
end6104+
6105+
def compile_expr_remap_arg0(nid, map_from, map_to)6106+
args_id = @nd_arguments[nid]6107+
if args_id >= 06108+
arg_ids = get_args(args_id)6109+
if arg_ids.length > 06110+
return compile_expr_remap(arg_ids[0], map_from, map_to)6111+
end6112+
end6113+
"0"6114+
end6115+
6116+
def compile_yield_method_call_stmt(nid, cci, midx, mname)6117+
# Call a yield-using class method with a block - inline the method body6118+
blk = @nd_block[nid]6119+
if blk < 06120+
return6121+
end6122+
bp_names = "".split(",")6123+
bp = @nd_parameters[blk]6124+
if bp >= 06125+
inner = @nd_parameters[bp]6126+
if inner >= 06127+
reqs = parse_id_list(@nd_requireds[inner])6128+
k = 06129+
while k < reqs.length6130+
bp_names.push(@nd_name[reqs[k]])6131+
k = k + 16132+
end6133+
end6134+
end6135+
6136+
recv = @nd_receiver[nid]6137+
rc = compile_expr(recv)6138+
6139+
bodies = @cls_meth_bodies[cci].split(";")6140+
bid = -16141+
if midx < bodies.length6142+
bid = bodies[midx].to_i6143+
end6144+
6145+
saved_ci = @current_class_idx6146+
@current_class_idx = cci6147+
6148+
cname = @cls_names[cci]6149+
@block_counter = @block_counter + 16150+
suffix = "_y" + @block_counter.to_s6151+
6152+
# For the method params and locals, create remapped names6153+
all_params = @cls_meth_params[cci].split("|")6154+
all_ptypes = @cls_meth_ptypes[cci].split("|")6155+
pnames = "".split(",")6156+
ptypes = "".split(",")6157+
if midx < all_params.length6158+
pnames = all_params[midx].split(",")6159+
end6160+
if midx < all_ptypes.length6161+
ptypes = all_ptypes[midx].split(",")6162+
end6163+
6164+
map_from = "".split(",")6165+
map_to = "".split(",")6166+
6167+
# Map self to the receiver expression6168+
map_from.push("_self_")6169+
map_to.push(rc)6170+
6171+
args_id = @nd_arguments[nid]6172+
arg_ids = []6173+
if args_id >= 06174+
arg_ids = get_args(args_id)6175+
end6176+
6177+
k = 06178+
while k < pnames.length6179+
pt = "int"6180+
if k < ptypes.length6181+
pt = ptypes[k]6182+
end6183+
tname = pnames[k] + suffix6184+
val = "0"6185+
if k < arg_ids.length6186+
val = compile_expr(arg_ids[k])6187+
end6188+
emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")6189+
map_from.push(pnames[k])6190+
map_to.push(tname)6191+
declare_var(tname, pt)6192+
k = k + 16193+
end6194+
6195+
# Declare function locals6196+
if bid >= 06197+
flocals_n = "".split(",")6198+
flocals_t = "".split(",")6199+
scan_locals(bid, flocals_n, flocals_t, pnames)6200+
k = 06201+
while k < flocals_n.length6202+
tname = flocals_n[k] + suffix6203+
emit(" " + c_type(flocals_t[k]) + " lv_" + tname + " = " + c_default_val(flocals_t[k]) + ";")6204+
map_from.push(flocals_n[k])6205+
map_to.push(tname)6206+
declare_var(tname, flocals_t[k])6207+
k = k + 16208+
end6209+
end6210+
6211+
# Compile the method body inline with yield -> block body6212+
if bid >= 06213+
stmts = get_stmts(bid)6214+
k = 06215+
while k < stmts.length6216+
compile_stmt_with_block(stmts[k], blk, bp_names, map_from, map_to)6217+
k = k + 16218+
end6219+
end6220+
6221+
@current_class_idx = saved_ci6222+
end6223+
6224+
def compile_each_with_yield_inline(nid, outer_blk, outer_bp_names, map_from, map_to)6225+
# An each call on an array inside an inlined yield function6226+
# The each block contains yield statements that should be replaced with outer block body6227+
recv = @nd_receiver[nid]6228+
recv_expr = compile_expr_remap(recv, map_from, map_to)6229+
rt = infer_type(recv)6230+
# If recv is remapped, get the actual type6231+
if @nd_type[recv] == "InstanceVariableReadNode"6232+
if @current_class_idx >= 06233+
rt = cls_ivar_type(@current_class_idx, @nd_name[recv])6234+
end6235+
end6236+
6237+
inner_bp1 = get_block_param(nid, 0)6238+
if inner_bp1 == ""6239+
inner_bp1 = "_ex"6240+
end6241+
# The inner block param might collide with outer names, so remap it6242+
inner_bp_remapped = remap_local(inner_bp1, map_from, map_to)6243+
6244+
old = @in_loop6245+
@in_loop = 16246+
tmp = new_temp6247+
6248+
if rt == "int_array"6249+
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + recv_expr + "); " + tmp + "++) {")6250+
emit(" lv_" + inner_bp_remapped + " = sp_IntArray_get(" + recv_expr + ", " + tmp + ");")6251+
@indent = @indent + 16252+
# Compile inner block body, replacing yield with outer block body6253+
inner_blk = @nd_block[nid]6254+
if inner_blk >= 06255+
ibody = @nd_body[inner_blk]6256+
if ibody >= 06257+
istmts = get_stmts(ibody)6258+
sk = 06259+
while sk < istmts.length6260+
# In the inner block, yield should be replaced with outer block body6261+
inner_nid = istmts[sk]6262+
if @nd_type[inner_nid] == "YieldNode"6263+
# yield x -> set outer bp from inner bp, then run outer block body6264+
yargs = @nd_arguments[inner_nid]6265+
if yargs >= 06266+
yaids = get_args(yargs)6267+
yk = 06268+
while yk < yaids.length6269+
if yk < outer_bp_names.length6270+
emit(" lv_" + outer_bp_names[yk] + " = " + compile_expr_remap(yaids[yk], map_from, map_to) + ";")6271+
end6272+
yk = yk + 16273+
end6274+
end6275+
obody = @nd_body[outer_blk]6276+
if obody >= 06277+
ostmts = get_stmts(obody)6278+
ok = 06279+
while ok < ostmts.length6280+
compile_stmt(ostmts[ok])6281+
ok = ok + 16282+
end6283+
end6284+
else6285+
compile_stmt_with_block(inner_nid, outer_blk, outer_bp_names, map_from, map_to)6286+
end6287+
sk = sk + 16288+
end6289+
end6290+
end6291+
@indent = @indent - 16292+
emit(" }")6293+
end6294+
@in_loop = old6295+
end6296+
6297+
def scan_captured_locals(nid, excludes, names, types)6298+
if nid < 06299+
return6300+
end6301+
if @nd_type[nid] == "LocalVariableReadNode"6302+
lname = @nd_name[nid]6303+
if not_in(lname, excludes) == 16304+
if not_in(lname, names) == 16305+
names.push(lname)6306+
vt = find_var_type(lname)6307+
if vt == ""6308+
vt = "int"6309+
end6310+
types.push(vt)6311+
end6312+
end6313+
end6314+
if @nd_type[nid] == "LocalVariableWriteNode"6315+
lname = @nd_name[nid]6316+
if not_in(lname, excludes) == 16317+
if not_in(lname, names) == 16318+
names.push(lname)6319+
vt = find_var_type(lname)6320+
if vt == ""6321+
vt = "int"6322+
end6323+
types.push(vt)6324+
end6325+
end6326+
end6327+
# Recurse6328+
if @nd_body[nid] >= 06329+
scan_captured_locals(@nd_body[nid], excludes, names, types)6330+
end6331+
stmts = parse_id_list(@nd_stmts[nid])6332+
k = 06333+
while k < stmts.length6334+
scan_captured_locals(stmts[k], excludes, names, types)6335+
k = k + 16336+
end6337+
if @nd_expression[nid] >= 06338+
scan_captured_locals(@nd_expression[nid], excludes, names, types)6339+
end6340+
if @nd_predicate[nid] >= 06341+
scan_captured_locals(@nd_predicate[nid], excludes, names, types)6342+
end6343+
if @nd_left[nid] >= 06344+
scan_captured_locals(@nd_left[nid], excludes, names, types)6345+
end6346+
if @nd_right[nid] >= 06347+
scan_captured_locals(@nd_right[nid], excludes, names, types)6348+
end6349+
if @nd_subsequent[nid] >= 06350+
scan_captured_locals(@nd_subsequent[nid], excludes, names, types)6351+
end6352+
if @nd_else_clause[nid] >= 06353+
scan_captured_locals(@nd_else_clause[nid], excludes, names, types)6354+
end6355+
if @nd_arguments[nid] >= 06356+
scan_captured_locals(@nd_arguments[nid], excludes, names, types)6357+
end6358+
args = parse_id_list(@nd_args[nid])6359+
k = 06360+
while k < args.length6361+
scan_captured_locals(args[k], excludes, names, types)6362+
k = k + 16363+
end6364+
if @nd_receiver[nid] >= 06365+
scan_captured_locals(@nd_receiver[nid], excludes, names, types)6366+
end6367+
end6368+
6369+
def compile_map_block(nid)6370+
@needs_int_array = 16371+
@needs_gc = 16372+
old = @in_loop6373+
@in_loop = 16374+
rt = infer_type(@nd_receiver[nid])6375+
rc = compile_expr(@nd_receiver[nid])6376+
bp1 = get_block_param(nid, 0)6377+
if bp1 == ""6378+
bp1 = "_x"6379+
end6380+
tmp_arr = new_temp6381+
tmp_i = new_temp6382+
if rt == "int_array"6383+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")6384+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")6385+
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")6386+
@indent = @indent + 16387+
blk = @nd_block[nid]6388+
if blk >= 06389+
body = @nd_body[blk]6390+
if body >= 06391+
stmts = get_stmts(body)6392+
if stmts.length > 06393+
last = stmts[stmts.length - 1]6394+
val = compile_expr(last)6395+
emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")6396+
end6397+
end6398+
end6399+
@indent = @indent - 16400+
emit(" }")6401+
end6402+
if rt == "str_array"6403+
@needs_str_array = 16404+
emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")6405+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")6406+
emit(" lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")6407+
@indent = @indent + 16408+
blk = @nd_block[nid]6409+
if blk >= 06410+
body = @nd_body[blk]6411+
if body >= 06412+
stmts = get_stmts(body)6413+
if stmts.length > 06414+
last = stmts[stmts.length - 1]6415+
val = compile_expr(last)6416+
emit(" sp_StrArray_push(" + tmp_arr + ", " + val + ");")6417+
end6418+
end6419+
end6420+
@indent = @indent - 16421+
emit(" }")6422+
end6423+
@in_loop = old6424+
end6425+
6426+
def compile_select_block(nid)6427+
@needs_int_array = 16428+
@needs_gc = 16429+
old = @in_loop6430+
@in_loop = 16431+
rt = infer_type(@nd_receiver[nid])6432+
rc = compile_expr(@nd_receiver[nid])6433+
bp1 = get_block_param(nid, 0)6434+
if bp1 == ""6435+
bp1 = "_x"6436+
end6437+
tmp_arr = new_temp6438+
tmp_i = new_temp6439+
if rt == "int_array"6440+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")6441+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")6442+
emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")6443+
@indent = @indent + 16444+
blk = @nd_block[nid]6445+
if blk >= 06446+
body = @nd_body[blk]6447+
if body >= 06448+
stmts = get_stmts(body)6449+
if stmts.length > 06450+
last = stmts[stmts.length - 1]6451+
cond = compile_expr(last)6452+
emit(" if (" + cond + ") sp_IntArray_push(" + tmp_arr + ", lv_" + bp1 + ");")6453+
end6454+
end6455+
end6456+
@indent = @indent - 16457+
emit(" }")6458+
end6459+
@in_loop = old6460+
end6461+
6462+
def compile_stmts_body(nid)6463+
if nid < 06464+
return6465+
end6466+
stmts = get_stmts(nid)6467+
k = 06468+
while k < stmts.length6469+
compile_stmt(stmts[k])6470+
k = k + 16471+
end6472+
end6473+
6474+
def compile_body_return(body_id, return_type)6475+
if body_id < 06476+
if return_type != "void"6477+
emit(" return " + c_default_val(return_type) + ";")6478+
end6479+
return6480+
end6481+
stmts = get_stmts(body_id)6482+
if stmts.length == 06483+
if return_type != "void"6484+
emit(" return " + c_default_val(return_type) + ";")6485+
end6486+
return6487+
end6488+
# All but last6489+
i = 06490+
while i < stmts.length - 16491+
compile_stmt(stmts[i])6492+
i = i + 16493+
end6494+
last = stmts[stmts.length - 1]6495+
if @nd_type[last] == "ReturnNode"6496+
compile_return_stmt(last)6497+
return6498+
end6499+
if @nd_type[last] == "IfNode"6500+
compile_if_return(last, return_type)6501+
return6502+
end6503+
if @nd_type[last] == "CaseNode"6504+
compile_case_return(last, return_type)6505+
return6506+
end6507+
if @nd_type[last] == "WhileNode"6508+
compile_while_stmt(last)6509+
if return_type != "void"6510+
emit(" return " + c_default_val(return_type) + ";")6511+
end6512+
return6513+
end6514+
if @nd_type[last] == "YieldNode"6515+
compile_yield_stmt(last)6516+
if return_type != "void"6517+
emit(" return " + c_default_val(return_type) + ";")6518+
end6519+
return6520+
end6521+
# If last statement is a CallNode with a block, handle map/select as expressions6522+
if @nd_type[last] == "CallNode"6523+
if @nd_block[last] >= 06524+
lmname = @nd_name[last]6525+
if lmname == "map"6526+
if return_type != "void"6527+
val = compile_map_expr(last)6528+
emit(" return " + val + ";")6529+
return6530+
end6531+
end6532+
if lmname == "select"6533+
if return_type != "void"6534+
val = compile_select_expr(last)6535+
emit(" return " + val + ";")6536+
return6537+
end6538+
end6539+
compile_stmt(last)6540+
if return_type != "void"6541+
emit(" return " + c_default_val(return_type) + ";")6542+
end6543+
return6544+
end54196545
end54206546
if return_type != "void"54216547
emit(" return " + compile_expr(last) + ";")