Commit cebd3e60

cebd3e604997Merged

Ryan Seys <[email protected]> · 6 months ago · 8 files · +6 added · ~2 modified

Description

feat(parse,codegen): support Index{And,Or,Target}WriteNode (3 nodes)

Bundle of the three remaining Index* compound forms; the Operator form (`a[i] += v`) is already supported via compile_index_op_assign. The new helpers compile_index_and_assign and compile_index_or_assign mirror the existing op-assign helper's per-receiver-type dispatch (int_array, float_array, str_int_hash, sym_int_hash, int_str_hash for OrWrite) but route the new value through a C `if (cur)` / `if (!cur)` guard instead of an arithmetic op.

Critical for correctness: the temp pattern keeps the receiver and index evaluated exactly ONCE, even though source like `a[i] ||= v` conceptually expands to `a[i] = a[i] || v`. Each helper emits:

{ sp_<TYPE> *_t = recv; mrb_int _i = idx;
  if (sp_<TYPE>_get(_t, _i)) sp_<TYPE>_set(_t, _i, val); }

so a side-effecting receiver (e.g. `grow_array(...)[i] &&= ...`) fires only once.

IndexAndWriteNode (`a[i] &&= val`): Sets a[i] only if the current value is truthy. Inherits the C-truthy vs Ruby-truthy gap (numeric 0 is C-falsy but Ruby-truthy); test fixture stays in the agreement region.

IndexOrWriteNode (`a[i] ||= val`): Sets a[i] only if the current value is falsy. Includes the int_str_hash arm for the common `hash[key] ||= "default"` idiom -- NULL is falsy in both C and Ruby for string-valued slots, so no divergence there.

IndexTargetNode (`a[i], b[j] = 1, 2`): Multi-assign LHS. New arm in emit_multi_write_target alongside the existing LocalVariable/InstanceVariable/GlobalVariable/ ClassVariable cases. Same per-receiver-type dispatch but no read-then-modify dance -- the value comes from the caller.

Test exercises:

  • index_or_write: str_int_hash with mix of pre-existing, pre-existing-zero, and never-set keys; ||= initializes the never-set key without overwriting the existing values.
  • index_and_write: int_array with non-zero slots, demonstrating truthy-fires; str_int_hash with the never-set-key case exercising the don't-fire path. Also confirms receiver/index evaluated once via `xs[4] &&= xs[4] * 100`.
  • index_target: cross-receiver swap (a[0], b[2] = ...); same-array swap (zs[0], zs[4] = zs[4], zs[0]) confirms RHS evaluation precedes any write; mixed array+hash multi-assign.

Out of scope: str_str_hash and other hash variants the existing compile_index_op_assign also doesn't cover; safe-navigation on the receiver (e.g. `obj&.attr ||= v`) -- the PM_CALL_NODE_FLAGS_SAFE_NAVIGATION flag exists in Prism's struct but Spinel's index-write helpers don't yet branch on it. Bigint arithmetic for IndexOperatorWriteNode also remains an existing gap (not introduced here).

Tests: 266 pass. Bootstrap: gen2.c == gen3.c.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
cebd3e604997
Parent
fea5369dddb0

spinel_codegen.rbmodifiedView file

@@ -24783,6 +24783,14 @@
2478324783 compile_index_op_assign(nid)
2478424784 return
2478524785 end
24786+ if t == "IndexAndWriteNode"
24787+ compile_index_and_assign(nid)
24788+ return
24789+ end
24790+ if t == "IndexOrWriteNode"
24791+ compile_index_or_assign(nid)
24792+ return
24793+ end
2478624794 if t == "IfNode"
2478724795 compile_if_stmt(nid)
2478824796 return
@@ -24939,6 +24947,49 @@
2493924947 end
2494024948 end
2494124949 end
24950+ if @nd_type[tid] == "IndexTargetNode"
24951+ # Multi-assign LHS: `a[0], b[1] = 1, 2`. Each LHS is an
24952+ # IndexTargetNode with a receiver and an arguments slot.
24953+ # Routes through the same per-receiver-type sp_<TYPE>_set
24954+ # dispatch as compile_index_op_assign, but without the
24955+ # read-then-modify dance -- the value is supplied by the
24956+ # caller. The temp-receiver pattern keeps `a` and the index
24957+ # evaluated exactly once.
24958+ tgt_recv = @nd_receiver[tid]
24959+ tgt_args_id = @nd_arguments[tid]
24960+ tgt_arg_ids = tgt_args_id >= 0 ? get_args(tgt_args_id) : []
24961+ if tgt_arg_ids.length >= 1
24962+ rt = infer_type(tgt_recv)
24963+ rc = compile_expr_gc_rooted(tgt_recv)
24964+ idx = compile_expr(tgt_arg_ids[0])
24965+ if rt == "float_array" || rt == "int_array"
24966+ pfx = array_c_prefix(rt)
24967+ tt = new_temp
24968+ ti = new_temp
24969+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
24970+ "; sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
24971+ end
24972+ if rt == "str_int_hash"
24973+ tt = new_temp
24974+ ti = new_temp
24975+ idx_s = compile_expr_as_string(tgt_arg_ids[0])
24976+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s +
24977+ "; sp_StrIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
24978+ end
24979+ if rt == "sym_int_hash"
24980+ tt = new_temp
24981+ ti = new_temp
24982+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +
24983+ "; sp_SymIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
24984+ end
24985+ if rt == "int_str_hash"
24986+ tt = new_temp
24987+ ti = new_temp
24988+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
24989+ "; sp_IntStrHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
24990+ end
24991+ end
24992+ end
2494224993 end
2494324994
2494424995 # Handle `a, *b = rhs` / `*a, b = rhs` / `a, *b, c = rhs`.
@@ -28323,6 +28374,137 @@
2832328374 end
2832428375 end
2832528376
28377+ # `a[i] &&= val` -- read once, conditionally write once. Mirrors
28378+ # compile_index_op_assign's per-receiver-type dispatch but routes
28379+ # the new value through a C `if (cur)` guard instead of an
28380+ # arithmetic op. The temp pattern keeps `a` and `i` evaluated
28381+ # exactly once, matching CRuby's `a[i] = a[i] && val` evaluation
28382+ # order.
28383+ def compile_index_and_assign(nid)
28384+ recv = @nd_receiver[nid]
28385+ args_id = @nd_arguments[nid]
28386+ arg_ids = args_id >= 0 ? get_args(args_id) : []
28387+ return if arg_ids.length < 1
28388+ rt = infer_type(recv)
28389+ rc = compile_expr_gc_rooted(recv)
28390+ idx = compile_expr(arg_ids[0])
28391+ # Note: defer compile_expr(@nd_expression[nid]) to inside each
28392+ # if-branch so RHS side effects only fire when the get is truthy
28393+ # (Ruby `&&=` short-circuit semantics).
28394+
28395+ if rt == "float_array" || rt == "int_array"
28396+ pfx = array_c_prefix(rt)
28397+ tt = new_temp
28398+ ti = new_temp
28399+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28400+ emit(" if (sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")
28401+ val = compile_expr(@nd_expression[nid])
28402+ emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")
28403+ emit(" }")
28404+ emit(" }")
28405+ return
28406+ end
28407+ if rt == "str_int_hash"
28408+ tt = new_temp
28409+ ti = new_temp
28410+ idx_s = compile_expr_as_string(arg_ids[0])
28411+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")
28412+ emit(" if (sp_StrIntHash_get(" + tt + ", " + ti + ")) {")
28413+ val = compile_expr(@nd_expression[nid])
28414+ emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28415+ emit(" }")
28416+ emit(" }")
28417+ return
28418+ end
28419+ if rt == "int_str_hash"
28420+ tt = new_temp
28421+ ti = new_temp
28422+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28423+ emit(" if (sp_IntStrHash_get(" + tt + ", " + ti + ")) {")
28424+ val = compile_expr(@nd_expression[nid])
28425+ emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")
28426+ emit(" }")
28427+ emit(" }")
28428+ return
28429+ end
28430+ if rt == "sym_int_hash"
28431+ tt = new_temp
28432+ ti = new_temp
28433+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")
28434+ emit(" if (sp_SymIntHash_get(" + tt + ", " + ti + ")) {")
28435+ val = compile_expr(@nd_expression[nid])
28436+ emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28437+ emit(" }")
28438+ emit(" }")
28439+ return
28440+ end
28441+ end
28442+
28443+ # `a[i] ||= val` -- read once, write only if current is falsy.
28444+ # Same C-truthy vs Ruby-truthy gap as the LocalVariable/Global/
28445+ # Class compound forms (numeric 0 is C-falsy; documented in
28446+ # test/global_var_or_write.rb). For string-valued slots (NULL
28447+ # falsy, anything else truthy) the C and Ruby semantics agree.
28448+ def compile_index_or_assign(nid)
28449+ recv = @nd_receiver[nid]
28450+ args_id = @nd_arguments[nid]
28451+ arg_ids = args_id >= 0 ? get_args(args_id) : []
28452+ return if arg_ids.length < 1
28453+ rt = infer_type(recv)
28454+ rc = compile_expr_gc_rooted(recv)
28455+ idx = compile_expr(arg_ids[0])
28456+ # Note: defer compile_expr(@nd_expression[nid]) to inside each
28457+ # if-branch so RHS side effects only fire when the get is falsy
28458+ # (Ruby `||=` short-circuit semantics).
28459+
28460+ if rt == "float_array" || rt == "int_array"
28461+ pfx = array_c_prefix(rt)
28462+ tt = new_temp
28463+ ti = new_temp
28464+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28465+ emit(" if (!sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")
28466+ val = compile_expr(@nd_expression[nid])
28467+ emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")
28468+ emit(" }")
28469+ emit(" }")
28470+ return
28471+ end
28472+ if rt == "str_int_hash"
28473+ tt = new_temp
28474+ ti = new_temp
28475+ idx_s = compile_expr_as_string(arg_ids[0])
28476+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")
28477+ emit(" if (!sp_StrIntHash_get(" + tt + ", " + ti + ")) {")
28478+ val = compile_expr(@nd_expression[nid])
28479+ emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28480+ emit(" }")
28481+ emit(" }")
28482+ return
28483+ end
28484+ if rt == "int_str_hash"
28485+ tt = new_temp
28486+ ti = new_temp
28487+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28488+ emit(" if (!sp_IntStrHash_get(" + tt + ", " + ti + ")) {")
28489+ val = compile_expr(@nd_expression[nid])
28490+ emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")
28491+ emit(" }")
28492+ emit(" }")
28493+ return
28494+ end
28495+ if rt == "sym_int_hash"
28496+ tt = new_temp
28497+ ti = new_temp
28498+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")
28499+ emit(" if (!sp_SymIntHash_get(" + tt + ", " + ti + ")) {")
28500+ val = compile_expr(@nd_expression[nid])
28501+ emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28502+ emit(" }")
28503+ emit(" }")
28504+ return
28505+ end
28506+ end
28507+
2832628508 # Return a C expression that evaluates to the inspected form of `val`
2832728509 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
2832828510 # contract. Returns "" when `at` has no inspect implementation yet, so