Commit a8e07e43

a8e07e4326b4Merged

Yusuke Endoh <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

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

The IndexOrWriteNode expression-form (x = h[k] ||= v, parenthesised chains like (@h[k] ||= []) << v, ternary arms …) only handled poly_poly_hash receivers — every other typed hash fell through to the default catch-all and silently emitted literal 0 as the chain receiver. Subsequent calls in the chain (<<, [], etc.) then landed on an int instead of the just-stored or pre-existing value, and the C compiler rejected e.g. sp_poly_shl(0, …) because the runtime helper expects a boxed sp_RbVal recv.

Add the same get-then-set lowering for sym_poly_hash and str_poly_hash. The shape mirrors the existing compile_poly_poly_index_or_assign_to_temp: stash the hash / key / get-result in three temps, branch on tag == SP_TAG_NIL to set the rhs and overwrite the result. The two typed variants need a different key C type (sp_sym / const char *) but reuse the same value-poly machinery, so factor them into one helper (compile_typed_poly_hash_index_or_assign_to_temp) and dispatch in both the expression-form (compile_expr) and the statement-form (compile_index_or_assign).

Details

Commit
a8e07e4326b4
Parent
31a59e30af5e

spinel_codegen.rbmodifiedView file

@@ -21087,12 +21087,13 @@
2108721087 end
2108821088 if t == "IndexOrWriteNode"
2108921089 # `recv[k] ||= v` used as an expression (`x = recv[k] ||= v`,
21090- # method-body return, ternary arm, …). Lower the get-then-set
21091- # into a stmt-level emit and return the temp holding the
21092- # resulting value. Only the poly_poly_hash receiver shape needs
21093- # this expression form so far — typed hashes were always lowered
21094- # as stmts for backward compatibility, so falling through to the
21095- # default catch-all is fine for them.
21090+ # method-body return, parenthesized chain like
21091+ # `(@h[k] ||= []) << v`). Lower the get-then-set into a
21092+ # stmt-level emit and return the temp holding the resulting
21093+ # value (existing on hit, freshly-stored rhs on miss). Without
21094+ # this branch the expression-form falls through to the default
21095+ # catch-all and silently emits `0` as the value, collapsing
21096+ # any subsequent chain.
2109621097 recv_iow = @nd_receiver[nid]
2109721098 args_iow = @nd_arguments[nid]
2109821099 if recv_iow >= 0 && args_iow >= 0
@@ -21103,6 +21104,12 @@
2110321104 tg = compile_poly_poly_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow)
2110421105 return tg
2110521106 end
21107+ if rt_iow == "sym_poly_hash" || rt_iow == "str_poly_hash"
21108+ rc_iow = compile_expr_gc_rooted(recv_iow)
21109+ arg_ids_iow = get_args(args_iow)
21110+ tg = compile_typed_poly_hash_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)
21111+ return tg
21112+ end
2110621113 end
2110721114 end
2110821115 if t == "ConstantReadNode"
@@ -35052,6 +35059,10 @@
3505235059 compile_poly_poly_index_or_assign_to_temp(nid, rc, arg_ids)
3505335060 return
3505435061 end
35062+ if rt == "sym_poly_hash" || rt == "str_poly_hash"
35063+ compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, rt)
35064+ return
35065+ end
3505535066 end
3505635067
3505735068 # Emit the get-then-set body for `recv[k] ||= v` against a
@@ -35074,6 +35085,35 @@
3507435085 tg
3507535086 end
3507635087
35088+ # Same shape as `compile_poly_poly_index_or_assign_to_temp` for
35089+ # sym_poly_hash / str_poly_hash receivers — the keys are bare
35090+ # `sp_sym` / `const char *` (not boxed sp_RbVal) but the value
35091+ # slot is poly so the get-then-set still pivots on the
35092+ # `tag == SP_TAG_NIL` miss probe.
35093+ def compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, recv_type)
35094+ tt = new_temp
35095+ tk = new_temp
35096+ tg = new_temp
35097+ if recv_type == "sym_poly_hash"
35098+ hash_c = "sp_SymPolyHash"
35099+ key_t = "sp_sym"
35100+ key_expr = compile_expr(arg_ids[0])
35101+ else
35102+ hash_c = "sp_StrPolyHash"
35103+ key_t = "const char *"
35104+ key_expr = compile_expr_as_string(arg_ids[0])
35105+ end
35106+ emit(" " + hash_c + " *" + tt + " = " + rc + ";")
35107+ emit(" " + key_t + " " + tk + " = " + key_expr + ";")
35108+ emit(" sp_RbVal " + tg + " = " + hash_c + "_get(" + tt + ", " + tk + ");")
35109+ val_boxed = box_expr_to_poly(@nd_expression[nid])
35110+ emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
35111+ emit(" " + tg + " = " + val_boxed + ";")
35112+ emit(" " + hash_c + "_set(" + tt + ", " + tk + ", " + tg + ");")
35113+ emit(" }")
35114+ tg
35115+ end
35116+
3507735117 # Return a C expression that evaluates to the inspected form of `val`
3507835118 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
3507935119 # contract. Returns "" when `at` has no inspect implementation yet, so