Commit 1755099e

1755099e73a6Merged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

Compile recv[idx] OP= value (IndexOperatorWriteNode)

Prism parses arr[i] += x, arr[i] -= x, etc. as a dedicated IndexOperatorWriteNode (it does NOT desugar to a []= CallNode). Spinel had no handler for this node, so the compound assignment fell through silently — the inner-loop body would emit only the loop-index increment, leaving the array untouched.

Symptoms: matmul-style accumulators or any tight arr[i] += val loop produced zero-valued output even though the rest of the program looked fine.

Three small additions:

  • spinel_parse.c: serialize PM_INDEX_OPERATOR_WRITE_NODE, exposing receiver, arguments, binary_operator, value.

  • spinel_codegen.rb (compile_stmt): dispatch the new node to a fresh compile_index_op_assign helper.

  • spinel_codegen.rb (compile_index_op_assign): get-modify-set against the typed container, in a block scope so receiver and index are each evaluated exactly once. Handles float_array, int_array, str_int_hash, int_str_hash (+= only, via sp_str_concat), and sym_int_hash. Other receiver types fall through silently for now — compound-assign on string arrays / poly hashes / str hashes is rarely useful and would need per-type semantics.

test/index_op_assign.rb covers +=, -=, *=, /= on FloatArray and IntArray (locals and through an obj.attr chain) plus += on a str_int_hash. All 132 tests pass; bootstrap closes.

Details

Commit
1755099e73a6
Parent
2c82182b70bb

spinel_codegen.rbmodifiedView file

@@ -17675,6 +17675,14 @@
1767517675 end
1767617676 return
1767717677 end
17678+ # `recv[idx] OP= value`. Without this case, the IndexOperatorWriteNode
17679+ # the parser emits would fall through and the increment would be
17680+ # silently dropped — symptoms include matmul / += accumulators
17681+ # producing zero-valued output even though forward passes look fine.
17682+ if t == "IndexOperatorWriteNode"
17683+ compile_index_op_assign(nid)
17684+ return
17685+ end
1767817686 if t == "IfNode"
1767917687 compile_if_stmt(nid)
1768017688 return
@@ -20630,6 +20638,65 @@
2063020638 end
2063120639 end
2063220640
20641+ # Compile `recv[idx] OP= value` (IndexOperatorWriteNode).
20642+ #
20643+ # Emitted as a get-modify-set against the appropriate typed container,
20644+ # in a block scope so that the receiver and index are each evaluated
20645+ # exactly once. Falls through silently for receiver types we don't
20646+ # handle yet — currently float_array, int_array, and the four numeric
20647+ # hash variants. Compound-assign on string arrays / poly hashes / str
20648+ # hashes is rarely useful and would need per-type semantics.
20649+ def compile_index_op_assign(nid)
20650+ recv = @nd_receiver[nid]
20651+ args_id = @nd_arguments[nid]
20652+ arg_ids = args_id >= 0 ? get_args(args_id) : []
20653+ return if arg_ids.length < 1
20654+ op = @nd_binop[nid]
20655+ rt = infer_type(recv)
20656+ rc = compile_expr_gc_rooted(recv)
20657+ idx = compile_expr(arg_ids[0])
20658+ val = compile_expr(@nd_expression[nid])
20659+
20660+ if rt == "float_array" || rt == "int_array"
20661+ pfx = array_c_prefix(rt)
20662+ tt = new_temp
20663+ ti = new_temp
20664+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
20665+ "; sp_" + pfx + "_set(" + tt + ", " + ti +
20666+ ", sp_" + pfx + "_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
20667+ return
20668+ end
20669+ if rt == "str_int_hash"
20670+ tt = new_temp
20671+ ti = new_temp
20672+ idx_s = compile_expr_as_string(arg_ids[0])
20673+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s +
20674+ "; sp_StrIntHash_set(" + tt + ", " + ti +
20675+ ", sp_StrIntHash_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
20676+ return
20677+ end
20678+ if rt == "int_str_hash"
20679+ # Concatenating strings (`+= str`) is the only sensible op here.
20680+ if op == "+"
20681+ tt = new_temp
20682+ ti = new_temp
20683+ @needs_string_helpers = 1
20684+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
20685+ "; sp_IntStrHash_set(" + tt + ", " + ti +
20686+ ", sp_str_concat(sp_IntStrHash_get(" + tt + ", " + ti + "), " + val + ")); }")
20687+ return
20688+ end
20689+ end
20690+ if rt == "sym_int_hash"
20691+ tt = new_temp
20692+ ti = new_temp
20693+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +
20694+ "; sp_SymIntHash_set(" + tt + ", " + ti +
20695+ ", sp_SymIntHash_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
20696+ return
20697+ end
20698+ end
20699+
2063320700 # Return a C expression that evaluates to the inspected form of `val`
2063420701 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
2063520702 # contract. Returns "" when `at` has no inspect implementation yet, so