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
end1789517895
return1789617896
end17897+
# `recv[idx] OP= value`. Without this case, the IndexOperatorWriteNode17898+
# the parser emits would fall through and the increment would be17899+
# silently dropped — symptoms include matmul / += accumulators17900+
# producing zero-valued output even though forward passes look fine.17901+
if t == "IndexOperatorWriteNode"17902+
compile_index_op_assign(nid)17903+
return17904+
end1789717905
if t == "IfNode"1789817906
compile_if_stmt(nid)1789917907
return@@ -20860,6 +20868,65 @@
2086020868
end2086120869
end2086220870
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 evaluated20875+
# exactly once. Falls through silently for receiver types we don't20876+
# handle yet — currently float_array, int_array, and the four numeric20877+
# hash variants. Compound-assign on string arrays / poly hashes / str20878+
# 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 < 120884+
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_temp20893+
ti = new_temp20894+
emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +20895+
"; sp_" + pfx + "_set(" + tt + ", " + ti +20896+
", sp_" + pfx + "_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")20897+
return20898+
end20899+
if rt == "str_int_hash"20900+
tt = new_temp20901+
ti = new_temp20902+
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+
return20907+
end20908+
if rt == "int_str_hash"20909+
# Concatenating strings (`+= str`) is the only sensible op here.20910+
if op == "+"20911+
tt = new_temp20912+
ti = new_temp20913+
@needs_string_helpers = 120914+
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+
return20918+
end20919+
end20920+
if rt == "sym_int_hash"20921+
tt = new_temp20922+
ti = new_temp20923+
emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +20924+
"; sp_SymIntHash_set(" + tt + ", " + ti +20925+
", sp_SymIntHash_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")20926+
return20927+
end20928+
end20929+
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#inspect2086520932
# contract. Returns "" when `at` has no inspect implementation yet, sospinel_parse.cmodifiedView file
Loading diff…
test/index_op_assign.rbaddedView file
Loading diff…