Commit 56443c23

56443c2314c9Merged

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

Description

Merge pull request #97 from OriPekelman/arr_flat

Compile recv[idx] OP= value (IndexOperatorWriteNode)

Details

Commit
56443c2314c9
Parent
385a3aa68af6
Merge parent
1755099e73a6

spinel_codegen.rbmodifiedView file

@@ -17894,6 +17894,14 @@
1789417894 end
1789517895 return
1789617896 end
17897+ # `recv[idx] OP= value`. Without this case, the IndexOperatorWriteNode
17898+ # the parser emits would fall through and the increment would be
17899+ # silently dropped — symptoms include matmul / += accumulators
17900+ # producing zero-valued output even though forward passes look fine.
17901+ if t == "IndexOperatorWriteNode"
17902+ compile_index_op_assign(nid)
17903+ return
17904+ end
1789717905 if t == "IfNode"
1789817906 compile_if_stmt(nid)
1789917907 return
@@ -20860,6 +20868,65 @@
2086020868 end
2086120869 end
2086220870
20871+ # Compile `recv[idx] OP= value` (IndexOperatorWriteNode).
20872+ #
20873+ # Emitted as a get-modify-set against the appropriate typed container,
20874+ # in a block scope so that the receiver and index are each evaluated
20875+ # exactly once. Falls through silently for receiver types we don't
20876+ # handle yet — currently float_array, int_array, and the four numeric
20877+ # hash variants. Compound-assign on string arrays / poly hashes / str
20878+ # hashes is rarely useful and would need per-type semantics.
20879+ def compile_index_op_assign(nid)
20880+ recv = @nd_receiver[nid]
20881+ args_id = @nd_arguments[nid]
20882+ arg_ids = args_id >= 0 ? get_args(args_id) : []
20883+ return if arg_ids.length < 1
20884+ op = @nd_binop[nid]
20885+ rt = infer_type(recv)
20886+ rc = compile_expr_gc_rooted(recv)
20887+ idx = compile_expr(arg_ids[0])
20888+ val = compile_expr(@nd_expression[nid])
20889+
20890+ if rt == "float_array" || rt == "int_array"
20891+ pfx = array_c_prefix(rt)
20892+ tt = new_temp
20893+ ti = new_temp
20894+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
20895+ "; sp_" + pfx + "_set(" + tt + ", " + ti +
20896+ ", sp_" + pfx + "_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
20897+ return
20898+ end
20899+ if rt == "str_int_hash"
20900+ tt = new_temp
20901+ ti = new_temp
20902+ idx_s = compile_expr_as_string(arg_ids[0])
20903+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s +
20904+ "; sp_StrIntHash_set(" + tt + ", " + ti +
20905+ ", sp_StrIntHash_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
20906+ return
20907+ end
20908+ if rt == "int_str_hash"
20909+ # Concatenating strings (`+= str`) is the only sensible op here.
20910+ if op == "+"
20911+ tt = new_temp
20912+ ti = new_temp
20913+ @needs_string_helpers = 1
20914+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
20915+ "; sp_IntStrHash_set(" + tt + ", " + ti +
20916+ ", sp_str_concat(sp_IntStrHash_get(" + tt + ", " + ti + "), " + val + ")); }")
20917+ return
20918+ end
20919+ end
20920+ if rt == "sym_int_hash"
20921+ tt = new_temp
20922+ ti = new_temp
20923+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +
20924+ "; sp_SymIntHash_set(" + tt + ", " + ti +
20925+ ", sp_SymIntHash_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
20926+ return
20927+ end
20928+ end
20929+
2086320930 # Return a C expression that evaluates to the inspected form of `val`
2086420931 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
2086520932 # contract. Returns "" when `at` has no inspect implementation yet, so