Commit f137975e
f137975e5e4eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #123 from ryanseys/rs-yield-multi-arg
feat(codegen): support multi-argument yield + zero-unset block params fix
Details
- Commit
- f137975e5e4e
- Parent
- 0d38bda5b6d9
- Merge parent
- 56dc3d1c1e04
spinel_codegen.rbmodifiedView file
@@ -155,6 +155,7 @@
155155
@hoisted_strlen_var = ""156156
@hoisted_strlen_recv = ""157157
@in_yield_method = 0158+
@current_method_yield_arity = 1158159
@in_gc_scope = 0159160
160161
# Yield/block tracking (parallel with meth_names / cls_meth_names)@@ -5253,6 +5254,80 @@
52535254
052545255
end52555256
5257+
# Walks `nid` for YieldNodes and returns max(`current`, max_args_of_yields).5258+
# Mirrors body_has_yield's recursion shape. `current` carries the running5259+
# max so callers can seed a floor (1, since every yield-using method needs5260+
# at least one mrb_int slot in `_block`'s signature).5261+
def body_max_yield_arity(nid, current)5262+
if nid < 05263+
return current5264+
end5265+
if @nd_type[nid] == "YieldNode"5266+
n = 05267+
if @nd_arguments[nid] >= 05268+
n = get_args(@nd_arguments[nid]).length5269+
end5270+
if n < 15271+
n = 15272+
end5273+
if n > current5274+
current = n5275+
end5276+
end5277+
if @nd_type[nid] == "DefNode"5278+
return current5279+
end5280+
if @nd_body[nid] >= 05281+
current = body_max_yield_arity(@nd_body[nid], current)5282+
end5283+
stmts = parse_id_list(@nd_stmts[nid])5284+
k = 05285+
while k < stmts.length5286+
current = body_max_yield_arity(stmts[k], current)5287+
k = k + 15288+
end5289+
if @nd_expression[nid] >= 05290+
current = body_max_yield_arity(@nd_expression[nid], current)5291+
end5292+
if @nd_predicate[nid] >= 05293+
current = body_max_yield_arity(@nd_predicate[nid], current)5294+
end5295+
if @nd_subsequent[nid] >= 05296+
current = body_max_yield_arity(@nd_subsequent[nid], current)5297+
end5298+
if @nd_else_clause[nid] >= 05299+
current = body_max_yield_arity(@nd_else_clause[nid], current)5300+
end5301+
if @nd_left[nid] >= 05302+
current = body_max_yield_arity(@nd_left[nid], current)5303+
end5304+
if @nd_right[nid] >= 05305+
current = body_max_yield_arity(@nd_right[nid], current)5306+
end5307+
if @nd_block[nid] >= 05308+
current = body_max_yield_arity(@nd_block[nid], current)5309+
end5310+
conds = parse_id_list(@nd_conditions[nid])5311+
k = 05312+
while k < conds.length5313+
current = body_max_yield_arity(conds[k], current)5314+
k = k + 15315+
end5316+
args = parse_id_list(@nd_args[nid])5317+
k = 05318+
while k < args.length5319+
current = body_max_yield_arity(args[k], current)5320+
k = k + 15321+
end5322+
if @nd_arguments[nid] >= 05323+
current = body_max_yield_arity(@nd_arguments[nid], current)5324+
end5325+
if @nd_receiver[nid] >= 05326+
current = body_max_yield_arity(@nd_receiver[nid], current)5327+
end5328+
current5329+
end5330+
52565331
# ---- Return type inference ----52575332
def infer_constructor_types52585333
# Scan AST for ClassName.new(args) calls and infer param types@@ -10441,16 +10516,18 @@
1044110516
end1044210517
1044310518
def yield_params_suffix(mi)10519+
csig = block_params_csig(method_yield_arity(mi))1044410520
pd = method_params_decl(mi)1044510521
if pd == ""10446-
return "void (*_block)(mrb_int, void*), void *_benv"10522+
return "void (*_block)(" + csig + ", void*), void *_benv"1044710523
end10448-
return ", void (*_block)(mrb_int, void*), void *_benv"10524+
return ", void (*_block)(" + csig + ", void*), void *_benv"1044910525
end1045010526
1045110527
def yield_params_suffix_cls(ci, midx)1045210528
# For class instance methods (always have self first)10453-
return ", void (*_block)(mrb_int, void*), void *_benv"10529+
csig = block_params_csig(cls_method_yield_arity(ci, midx))10530+
return ", void (*_block)(" + csig + ", void*), void *_benv"1045410531
end1045510532
1045610533
def cls_method_has_yield(ci, midx)@@ -10463,6 +10540,42 @@
1046310540
01046410541
end1046510542
10543+
# Max number of args used in any `yield` inside the top-level method10544+
# at @meth_body_ids[mi]. Floor of 1 — yield-using methods always have10545+
# at least one mrb_int slot (the no-arg `yield` form is padded to 0).10546+
def method_yield_arity(mi)10547+
if mi < 0 || mi >= @meth_body_ids.length10548+
return 110549+
end10550+
body_max_yield_arity(@meth_body_ids[mi], 1)10551+
end10552+
10553+
# Same as method_yield_arity, but resolved through the class method10554+
# body table @cls_meth_bodies (parallel to @cls_meth_has_yield).10555+
def cls_method_yield_arity(ci, midx)10556+
if ci < 0 || midx < 010557+
return 110558+
end10559+
bodies = @cls_meth_bodies[ci].split(";")10560+
if midx >= bodies.length10561+
return 110562+
end10563+
bid = bodies[midx].to_i10564+
body_max_yield_arity(bid, 1)10565+
end10566+
10567+
# Comma-joined string of `arity` mrb_int slots — the variable-arity10568+
# portion of the `_block` function-pointer signature.10569+
def block_params_csig(arity)10570+
csig = "mrb_int"10571+
k = 110572+
while k < arity10573+
csig = csig + ", mrb_int"10574+
k = k + 110575+
end10576+
csig10577+
end10578+
1046610579
# Returns 1 if the (ci, midx) method declares a `&block` parameter,1046710580
# 0 otherwise. Ruby syntax requires `&block` to be the trailing1046810581
# param, so we check only the last slot — a proc-typed slot in any@@ -11024,6 +11137,7 @@
1102411137
if midx >= 01102511138
if cls_method_has_yield(ci, midx) == 11102611139
@in_yield_method = 111140+
@current_method_yield_arity = cls_method_yield_arity(ci, midx)1102711141
else1102811142
@in_yield_method = 01102911143
end@@ -11081,6 +11195,7 @@
1108111195
@current_method_name = ""1108211196
@current_method_block_param = ""1108311197
@in_yield_method = 011198+
@current_method_yield_arity = 11108411199
@indent = 01108511200
emit_raw(" return " + c_return_default(rt) + ";")1108611201
emit_raw("}")@@ -11204,6 +11319,7 @@
1120411319
1120511320
if @meth_has_yield[mi] == 11120611321
@in_yield_method = 111322+
@current_method_yield_arity = method_yield_arity(mi)1120711323
else1120811324
@in_yield_method = 01120911325
end@@ -11274,6 +11390,7 @@
1127411390
@current_method_name = ""1127511391
@current_method_block_param = ""1127611392
@in_yield_method = 011393+
@current_method_yield_arity = 11127711394
@indent = 01127811395
emit_raw(" return " + c_return_default(rt) + ";")1127911396
emit_raw("}")@@ -24206,23 +24323,21 @@
2420624323
2420724324
def compile_yield_stmt(nid)2420824325
args_id = @nd_arguments[nid]24209-
arg_exprs = ""24326+
emitted = "".split(",")2421024327
if args_id >= 02421124328
aids = get_args(args_id)2421224329
k = 02421324330
while k < aids.length24214-
if k > 024215-
arg_exprs = arg_exprs + ", "24216-
end24217-
arg_exprs = arg_exprs + compile_expr(aids[k])24331+
emitted.push(compile_expr(aids[k]))2421824332
k = k + 12421924333
end2422024334
end24221-
if arg_exprs != ""24222-
emit(" if (_block) _block(" + arg_exprs + ", _benv);")24223-
else24224-
emit(" if (_block) _block(0, _benv);")24335+
# Pad to the enclosing method's max yield arity so the call matches24336+
# the function-pointer signature emitted by yield_params_suffix.24337+
while emitted.length < @current_method_yield_arity24338+
emitted.push("0")2422524339
end24340+
emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")2422624341
end2422724342
2422824343
def compile_yield_call_stmt(nid, mi)@@ -24317,16 +24432,26 @@
2431724432
if t == "YieldNode"2431824433
# Replace yield with the block body2431924434
args_id = @nd_arguments[nid]24435+
assigned = 02432024436
if args_id >= 02432124437
aids = get_args(args_id)2432224438
k = 02432324439
while k < aids.length2432424440
if k < bp_names.length2432524441
emit(" lv_" + bp_names[k] + " = " + compile_expr_remap(aids[k], map_from, map_to) + ";")24442+
assigned = assigned + 12432624443
end2432724444
k = k + 12432824445
end2432924446
end24447+
# Reset any block params that didn't receive a yield arg so a24448+
# later smaller-arity yield in the same method doesn't leak the24449+
# previous yield's values. Mirrors compile_yield_stmt's "0"24450+
# padding on the function-pointer dispatch path.24451+
while assigned < bp_names.length24452+
emit(" lv_" + bp_names[assigned] + " = 0;")24453+
assigned = assigned + 124454+
end2433024455
body = @nd_body[blk]2433124456
if body >= 02433224457
stmts = get_stmts(body)test/multi_arg_yield.rbaddedView file
Loading diff…