Commit 136bb797

136bb797f367Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #128 from dak2/fix/reduce-each-with-object-block-param-shadow

Fix block-param shadowing for reduce and each_with_object

Details

Commit
136bb797f367
Parent
d34baf443d0a
Merge parent
edc01a685617

spinel_codegen.rbmodifiedView file

@@ -23324,22 +23324,20 @@
2332423324 @in_loop = 1
2332523325 rt = infer_type(@nd_receiver[nid])
2332623326 rc = compile_expr_gc_rooted(@nd_receiver[nid])
23327+ # Bind the seed to an outer-scope temp so the expression form can
23328+ # surface the final accumulator. CallNode seeds get GC-rooted by
23329+ # compile_expr_gc_rooted; literal/local seeds rely on the caller
23330+ # already holding a reference for the duration of the loop.
2332723331 obj_ct = "mrb_int"
23332+ obj_t = "int"
23333+ obj_arg_nid = -1
2332823334 args_id = @nd_arguments[nid]
2332923335 if args_id >= 0
2333023336 aargs = get_args(args_id)
2333123337 if aargs.length > 0
23332- obj_ct = c_type(infer_type(aargs[0]))
23333- end
23334- end
23335- # Root the seed: it lives across every loop iteration, and any
23336- # allocation inside the block can trigger a GC that would otherwise
23337- # sweep the freshly constructed seed object before the loop ends.
23338- obj_arg_nid = -1
23339- if args_id >= 0
23340- aargs2 = get_args(args_id)
23341- if aargs2.length > 0
23342- obj_arg_nid = aargs2[0]
23338+ obj_arg_nid = aargs[0]
23339+ obj_t = infer_type(obj_arg_nid)
23340+ obj_ct = c_type(obj_t)
2334323341 end
2334423342 end
2334523343 obj_arg = compile_expr_gc_rooted(obj_arg_nid)
@@ -23366,6 +23364,7 @@
2336623364 @indent = @indent + 1
2336723365 push_scope
2336823366 declare_var(bp1, elem_type_of_array(rt))
23367+ declare_var(bp2, obj_t)
2336923368 compile_stmts_body(@nd_body[@nd_block[nid]])
2337023369 pop_scope
2337123370 @indent = @indent - 1
@@ -24659,18 +24658,25 @@
2465924658 end
2466024659
2466124660 def compile_reduce_expr(nid)
24662- # Emit the reduce loop as side effects, return accumulator
2466324661 compile_reduce_block(nid)
24664- bp1 = get_block_param(nid, 0)
24665- if bp1 == ""
24666- bp1 = "_acc"
24667- end
24668- "lv_" + bp1
2466924662 end
2467024663
2467124664 def compile_reduce_block(nid)
24665+ old = @in_loop
24666+ @in_loop = 1
24667+ @needs_gc = 1
2467224668 rc = compile_expr_gc_rooted(@nd_receiver[nid])
24673- init_val = compile_arg0(nid)
24669+ # Hold on to the seed AST nid so we can `infer_type` it later for
24670+ # the accumulator's inner-scope registration.
24671+ seed_nid = -1
24672+ args_id = @nd_arguments[nid]
24673+ if args_id >= 0
24674+ aargs = get_args(args_id)
24675+ if aargs.length > 0
24676+ seed_nid = aargs[0]
24677+ end
24678+ end
24679+ init_val = compile_expr_gc_rooted(seed_nid)
2467424680 bp1 = get_block_param(nid, 0)
2467524681 bp2 = get_block_param(nid, 1)
2467624682 if bp1 == ""
@@ -24679,16 +24685,51 @@
2467924685 if bp2 == ""
2468024686 bp2 = "_x"
2468124687 end
24682- emit(" lv_" + bp1 + " = " + init_val + ";")
2468324688 rt = infer_type(@nd_receiver[nid])
2468424689 pfx = array_c_prefix(rt)
2468524690 elem_t = elem_type_of_array(rt)
24691+ # bp1 takes the seed's type. Seed-less form is currently treated as
24692+ # 0-seeded (init_val resolves to "0" via compile_expr(-1)); seed_t
24693+ # falls back to elem_t to keep the type system consistent. This is
24694+ # not true Ruby first-element seeding — known limitation.
24695+ seed_t = elem_t
24696+ if seed_nid >= 0
24697+ seed_t = infer_type(seed_nid)
24698+ end
24699+ # See compile_each_slice_block for the typed-shadow + SP_GC_ROOT
24700+ # technique inside a C block scope. reduce additionally needs
24701+ # result_tmp because it's expression-form: the final value must
24702+ # survive past the inner scope's `}` for the caller to consume.
24703+ outer_t = find_var_type(bp1)
24704+ shadowed = 0
24705+ if outer_t != "" && outer_t != seed_t
24706+ shadowed = 1
24707+ end
24708+ acc_expr = "lv_" + bp1
24709+ if shadowed == 1
24710+ result_tmp = new_temp
24711+ seed_ct = c_type(seed_t)
24712+ # Evaluate init_val once and share it between result_tmp and the
24713+ # shadowed lv_<bp1>; some seed expressions (ArrayNode/HashNode
24714+ # literals) are not lifted to a temp by compile_expr_gc_rooted.
24715+ emit(" " + seed_ct + " " + result_tmp + " = " + init_val + ";")
24716+ emit(" {")
24717+ @indent = @indent + 1
24718+ emit(" SP_GC_SAVE();")
24719+ emit(" " + seed_ct + " lv_" + bp1 + " = " + result_tmp + ";")
24720+ if type_is_pointer(seed_t) == 1
24721+ emit(" SP_GC_ROOT(lv_" + bp1 + ");")
24722+ end
24723+ acc_expr = result_tmp
24724+ else
24725+ emit(" lv_" + bp1 + " = " + init_val + ";")
24726+ end
2468624727 tmp = new_temp
2468724728 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
2468824729 emit(" " + c_type(elem_t) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2468924730 @indent = @indent + 1
2469024731 push_scope
24691- declare_var(bp1, elem_t)
24732+ declare_var(bp1, seed_t)
2469224733 declare_var(bp2, elem_t)
2469324734 blk = @nd_block[nid]
2469424735 if blk >= 0
@@ -24705,6 +24746,13 @@
2470524746 pop_scope
2470624747 @indent = @indent - 1
2470724748 emit(" }")
24749+ if shadowed == 1
24750+ emit(" " + acc_expr + " = lv_" + bp1 + ";")
24751+ @indent = @indent - 1
24752+ emit(" }")
24753+ end
24754+ @in_loop = old
24755+ acc_expr
2470824756 end
2470924757
2471024758 def compile_reject_expr(nid)