Commit c4fcd37e

c4fcd37e5799Merged

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

Description

Merge pull request #377 from mame/pr/chained-or-assign-collection

fix(codegen): collection-element chained ||= lowers to a value-producing temp for sym/str_poly_hash

Details

Commit
c4fcd37e5799
Parent
c3b4e3229e31
Merge parent
a8e07e4326b4

spinel_codegen.rbmodifiedView file

@@ -21189,12 +21189,13 @@
2118921189 end
2119021190 if t == "IndexOrWriteNode"
2119121191 # `recv[k] ||= v` used as an expression (`x = recv[k] ||= v`,
21192- # method-body return, ternary arm, …). Lower the get-then-set
21193- # into a stmt-level emit and return the temp holding the
21194- # resulting value. Only the poly_poly_hash receiver shape needs
21195- # this expression form so far — typed hashes were always lowered
21196- # as stmts for backward compatibility, so falling through to the
21197- # default catch-all is fine for them.
21192+ # method-body return, parenthesized chain like
21193+ # `(@h[k] ||= []) << v`). Lower the get-then-set into a
21194+ # stmt-level emit and return the temp holding the resulting
21195+ # value (existing on hit, freshly-stored rhs on miss). Without
21196+ # this branch the expression-form falls through to the default
21197+ # catch-all and silently emits `0` as the value, collapsing
21198+ # any subsequent chain.
2119821199 recv_iow = @nd_receiver[nid]
2119921200 args_iow = @nd_arguments[nid]
2120021201 if recv_iow >= 0 && args_iow >= 0
@@ -21205,6 +21206,12 @@
2120521206 tg = compile_poly_poly_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow)
2120621207 return tg
2120721208 end
21209+ if rt_iow == "sym_poly_hash" || rt_iow == "str_poly_hash"
21210+ rc_iow = compile_expr_gc_rooted(recv_iow)
21211+ arg_ids_iow = get_args(args_iow)
21212+ tg = compile_typed_poly_hash_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)
21213+ return tg
21214+ end
2120821215 end
2120921216 end
2121021217 if t == "ConstantReadNode"
@@ -35246,6 +35253,10 @@
3524635253 compile_poly_poly_index_or_assign_to_temp(nid, rc, arg_ids)
3524735254 return
3524835255 end
35256+ if rt == "sym_poly_hash" || rt == "str_poly_hash"
35257+ compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, rt)
35258+ return
35259+ end
3524935260 end
3525035261
3525135262 # Emit the get-then-set body for `recv[k] ||= v` against a
@@ -35268,6 +35279,35 @@
3526835279 tg
3526935280 end
3527035281
35282+ # Same shape as `compile_poly_poly_index_or_assign_to_temp` for
35283+ # sym_poly_hash / str_poly_hash receivers — the keys are bare
35284+ # `sp_sym` / `const char *` (not boxed sp_RbVal) but the value
35285+ # slot is poly so the get-then-set still pivots on the
35286+ # `tag == SP_TAG_NIL` miss probe.
35287+ def compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, recv_type)
35288+ tt = new_temp
35289+ tk = new_temp
35290+ tg = new_temp
35291+ if recv_type == "sym_poly_hash"
35292+ hash_c = "sp_SymPolyHash"
35293+ key_t = "sp_sym"
35294+ key_expr = compile_expr(arg_ids[0])
35295+ else
35296+ hash_c = "sp_StrPolyHash"
35297+ key_t = "const char *"
35298+ key_expr = compile_expr_as_string(arg_ids[0])
35299+ end
35300+ emit(" " + hash_c + " *" + tt + " = " + rc + ";")
35301+ emit(" " + key_t + " " + tk + " = " + key_expr + ";")
35302+ emit(" sp_RbVal " + tg + " = " + hash_c + "_get(" + tt + ", " + tk + ");")
35303+ val_boxed = box_expr_to_poly(@nd_expression[nid])
35304+ emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
35305+ emit(" " + tg + " = " + val_boxed + ";")
35306+ emit(" " + hash_c + "_set(" + tt + ", " + tk + ", " + tg + ");")
35307+ emit(" }")
35308+ tg
35309+ end
35310+
3527135311 # Return a C expression that evaluates to the inspected form of `val`
3527235312 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
3527335313 # contract. Returns "" when `at` has no inspect implementation yet, so