Commit 168b6c93

168b6c9336f7Merged

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

Description

Merge pull request #278 from ryanseys/rs-node-index-writes

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

Details

Commit
168b6c9336f7
Parent
538a8eeedd5c
Merge parent
cebd3e604997

spinel_codegen.rbmodifiedView file

@@ -25010,6 +25010,14 @@
2501025010 compile_index_op_assign(nid)
2501125011 return
2501225012 end
25013+ if t == "IndexAndWriteNode"
25014+ compile_index_and_assign(nid)
25015+ return
25016+ end
25017+ if t == "IndexOrWriteNode"
25018+ compile_index_or_assign(nid)
25019+ return
25020+ end
2501325021 if t == "IfNode"
2501425022 compile_if_stmt(nid)
2501525023 return
@@ -25166,6 +25174,49 @@
2516625174 end
2516725175 end
2516825176 end
25177+ if @nd_type[tid] == "IndexTargetNode"
25178+ # Multi-assign LHS: `a[0], b[1] = 1, 2`. Each LHS is an
25179+ # IndexTargetNode with a receiver and an arguments slot.
25180+ # Routes through the same per-receiver-type sp_<TYPE>_set
25181+ # dispatch as compile_index_op_assign, but without the
25182+ # read-then-modify dance -- the value is supplied by the
25183+ # caller. The temp-receiver pattern keeps `a` and the index
25184+ # evaluated exactly once.
25185+ tgt_recv = @nd_receiver[tid]
25186+ tgt_args_id = @nd_arguments[tid]
25187+ tgt_arg_ids = tgt_args_id >= 0 ? get_args(tgt_args_id) : []
25188+ if tgt_arg_ids.length >= 1
25189+ rt = infer_type(tgt_recv)
25190+ rc = compile_expr_gc_rooted(tgt_recv)
25191+ idx = compile_expr(tgt_arg_ids[0])
25192+ if rt == "float_array" || rt == "int_array"
25193+ pfx = array_c_prefix(rt)
25194+ tt = new_temp
25195+ ti = new_temp
25196+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
25197+ "; sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
25198+ end
25199+ if rt == "str_int_hash"
25200+ tt = new_temp
25201+ ti = new_temp
25202+ idx_s = compile_expr_as_string(tgt_arg_ids[0])
25203+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s +
25204+ "; sp_StrIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
25205+ end
25206+ if rt == "sym_int_hash"
25207+ tt = new_temp
25208+ ti = new_temp
25209+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +
25210+ "; sp_SymIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
25211+ end
25212+ if rt == "int_str_hash"
25213+ tt = new_temp
25214+ ti = new_temp
25215+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
25216+ "; sp_IntStrHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
25217+ end
25218+ end
25219+ end
2516925220 end
2517025221
2517125222 # Handle `a, *b = rhs` / `*a, b = rhs` / `a, *b, c = rhs`.
@@ -28550,6 +28601,137 @@
2855028601 end
2855128602 end
2855228603
28604+ # `a[i] &&= val` -- read once, conditionally write once. Mirrors
28605+ # compile_index_op_assign's per-receiver-type dispatch but routes
28606+ # the new value through a C `if (cur)` guard instead of an
28607+ # arithmetic op. The temp pattern keeps `a` and `i` evaluated
28608+ # exactly once, matching CRuby's `a[i] = a[i] && val` evaluation
28609+ # order.
28610+ def compile_index_and_assign(nid)
28611+ recv = @nd_receiver[nid]
28612+ args_id = @nd_arguments[nid]
28613+ arg_ids = args_id >= 0 ? get_args(args_id) : []
28614+ return if arg_ids.length < 1
28615+ rt = infer_type(recv)
28616+ rc = compile_expr_gc_rooted(recv)
28617+ idx = compile_expr(arg_ids[0])
28618+ # Note: defer compile_expr(@nd_expression[nid]) to inside each
28619+ # if-branch so RHS side effects only fire when the get is truthy
28620+ # (Ruby `&&=` short-circuit semantics).
28621+
28622+ if rt == "float_array" || rt == "int_array"
28623+ pfx = array_c_prefix(rt)
28624+ tt = new_temp
28625+ ti = new_temp
28626+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28627+ emit(" if (sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")
28628+ val = compile_expr(@nd_expression[nid])
28629+ emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")
28630+ emit(" }")
28631+ emit(" }")
28632+ return
28633+ end
28634+ if rt == "str_int_hash"
28635+ tt = new_temp
28636+ ti = new_temp
28637+ idx_s = compile_expr_as_string(arg_ids[0])
28638+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")
28639+ emit(" if (sp_StrIntHash_get(" + tt + ", " + ti + ")) {")
28640+ val = compile_expr(@nd_expression[nid])
28641+ emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28642+ emit(" }")
28643+ emit(" }")
28644+ return
28645+ end
28646+ if rt == "int_str_hash"
28647+ tt = new_temp
28648+ ti = new_temp
28649+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28650+ emit(" if (sp_IntStrHash_get(" + tt + ", " + ti + ")) {")
28651+ val = compile_expr(@nd_expression[nid])
28652+ emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")
28653+ emit(" }")
28654+ emit(" }")
28655+ return
28656+ end
28657+ if rt == "sym_int_hash"
28658+ tt = new_temp
28659+ ti = new_temp
28660+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")
28661+ emit(" if (sp_SymIntHash_get(" + tt + ", " + ti + ")) {")
28662+ val = compile_expr(@nd_expression[nid])
28663+ emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28664+ emit(" }")
28665+ emit(" }")
28666+ return
28667+ end
28668+ end
28669+
28670+ # `a[i] ||= val` -- read once, write only if current is falsy.
28671+ # Same C-truthy vs Ruby-truthy gap as the LocalVariable/Global/
28672+ # Class compound forms (numeric 0 is C-falsy; documented in
28673+ # test/global_var_or_write.rb). For string-valued slots (NULL
28674+ # falsy, anything else truthy) the C and Ruby semantics agree.
28675+ def compile_index_or_assign(nid)
28676+ recv = @nd_receiver[nid]
28677+ args_id = @nd_arguments[nid]
28678+ arg_ids = args_id >= 0 ? get_args(args_id) : []
28679+ return if arg_ids.length < 1
28680+ rt = infer_type(recv)
28681+ rc = compile_expr_gc_rooted(recv)
28682+ idx = compile_expr(arg_ids[0])
28683+ # Note: defer compile_expr(@nd_expression[nid]) to inside each
28684+ # if-branch so RHS side effects only fire when the get is falsy
28685+ # (Ruby `||=` short-circuit semantics).
28686+
28687+ if rt == "float_array" || rt == "int_array"
28688+ pfx = array_c_prefix(rt)
28689+ tt = new_temp
28690+ ti = new_temp
28691+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28692+ emit(" if (!sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")
28693+ val = compile_expr(@nd_expression[nid])
28694+ emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")
28695+ emit(" }")
28696+ emit(" }")
28697+ return
28698+ end
28699+ if rt == "str_int_hash"
28700+ tt = new_temp
28701+ ti = new_temp
28702+ idx_s = compile_expr_as_string(arg_ids[0])
28703+ emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")
28704+ emit(" if (!sp_StrIntHash_get(" + tt + ", " + ti + ")) {")
28705+ val = compile_expr(@nd_expression[nid])
28706+ emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28707+ emit(" }")
28708+ emit(" }")
28709+ return
28710+ end
28711+ if rt == "int_str_hash"
28712+ tt = new_temp
28713+ ti = new_temp
28714+ emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
28715+ emit(" if (!sp_IntStrHash_get(" + tt + ", " + ti + ")) {")
28716+ val = compile_expr(@nd_expression[nid])
28717+ emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")
28718+ emit(" }")
28719+ emit(" }")
28720+ return
28721+ end
28722+ if rt == "sym_int_hash"
28723+ tt = new_temp
28724+ ti = new_temp
28725+ emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")
28726+ emit(" if (!sp_SymIntHash_get(" + tt + ", " + ti + ")) {")
28727+ val = compile_expr(@nd_expression[nid])
28728+ emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")
28729+ emit(" }")
28730+ emit(" }")
28731+ return
28732+ end
28733+ end
28734+
2855328735 # Return a C expression that evaluates to the inspected form of `val`
2855428736 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
2855528737 # contract. Returns "" when `at` has no inspect implementation yet, so