Commit b7e3a90e
b7e3a90e8725Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #367 from mame/pr/ivar-or-and-write
fix(codegen): @x ||= expr and @x &&= expr
Details
- Commit
- b7e3a90e8725
- Parent
- 9153a62f417c
- Merge parent
- 4b4bb55455a0
spinel_codegen.rbmodifiedView file
@@ -2420,6 +2420,16 @@
24202420
end24212421
return "int"24222422
end2423+
if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"2424+
# `(@x ||= expr)` / `(@x &&= expr)` evaluates to @x's slot type2425+
# (union of the prior value and the rhs, but Spinel widens those2426+
# via update_ivar_type already, so reading the slot type is the2427+
# same answer).2428+
if @current_class_idx >= 02429+
return cls_ivar_type(@current_class_idx, @nd_name[nid])2430+
end2431+
return "int"2432+
end24232433
if t == "InstanceVariableReadNode"24242434
if @current_class_idx >= 024252435
return cls_ivar_type(@current_class_idx, @nd_name[nid])@@ -8161,6 +8171,36 @@
81618171
add_ivar(ci, iname, "int")81628172
end81638173
end8174+
# `@x ||= expr` / `@x &&= expr`: register the slot when first8175+
# encountered. The rhs type seeds the ivar; without registration8176+
# the struct comes out without the slot and any subsequent read8177+
# ground out at the int default.8178+
if @nd_type[nid] == "InstanceVariableOrWriteNode" || @nd_type[nid] == "InstanceVariableAndWriteNode"8179+
iname = @nd_name[nid]8180+
expr_first = @nd_expression[nid]8181+
if ivar_exists(ci, iname) == 0 && ivar_exists_in_ancestor(ci, iname) == 18182+
vtype = infer_ivar_init_type(expr_first)8183+
if vtype != "int" && vtype != "nil"8184+
update_ivar_type(ci, iname, vtype)8185+
end8186+
elsif ivar_exists(ci, iname) == 08187+
vtype = infer_ivar_init_type(expr_first)8188+
# ||= reads @x first; pre-register a nil observation so the8189+
# `nil + T → T?` rule fires when a later writer-scan widens.8190+
add_ivar(ci, iname, "nil", 0)8191+
if vtype != "int" && vtype != "nil"8192+
update_ivar_type(ci, iname, vtype)8193+
end8194+
else8195+
expr = @nd_expression[nid]8196+
if expr >= 0 && @nd_type[expr] != "NilNode"8197+
vtype = infer_ivar_init_type(expr)8198+
if vtype != "int" && vtype != "nil"8199+
update_ivar_type(ci, iname, vtype)8200+
end8201+
end8202+
end8203+
end81648204
# Multi-write to ivars: `@a, @b = expr1, expr2` (or `[expr1, expr2]`).81658205
# Without this branch, ivars assigned only via destructuring never get81668206
# registered and the struct comes out missing them.@@ -12320,6 +12360,23 @@
1232012360
end1232112361
end1232212362
end12363+
# `@x ||= expr` / `@x &&= expr`: writer-scan the rhs the same as a12364+
# plain `@x = expr` so the slot widens from its `nil` seed to the12365+
# rhs's actual type (`infer_type` resolves CallNode return types,12366+
# unlike `infer_ivar_init_type` used at registration time).12367+
if @nd_type[nid] == "InstanceVariableOrWriteNode" || @nd_type[nid] == "InstanceVariableAndWriteNode"12368+
if @current_class_idx >= 012369+
iname = @nd_name[nid]12370+
expr_id = @nd_expression[nid]12371+
if expr_id >= 012372+
at = infer_type(expr_id)12373+
record_ivar_observation(@current_class_idx, iname, at, expr_id)12374+
if at != "int" && at != "nil"12375+
update_ivar_type(@current_class_idx, iname, at)12376+
end12377+
end12378+
end12379+
end1232312380
# Multi-write to ivars: `@a, @b = pulse_0, pulse_1`. Mirrors the1232412381
# single-write branch above so each ivar's type is widened from1232512382
# the corresponding RHS slot. Without this the multi-write left@@ -20955,6 +21012,15 @@
2095521012
end2095621013
return "(" + ivar_lhs(iname_w) + " = " + val + ")"2095721014
end21015+
if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"21016+
# Expression form of `@x ||= …` / `@x &&= …`. Method bodies21017+
# whose last statement is one of these reach `compile_expr`21018+
# via the return-value path, so we need a value-producing21019+
# form. Run the statement-style emit for the side effect, then21020+
# surface `@x`'s post-assign value as the expression result.21021+
compile_stmt(nid)21022+
return ivar_lhs(@nd_name[nid])21023+
end2095821024
if t == "LocalVariableWriteNode"2095921025
# `local = expr` used as an expression (e.g. inside a chained2096021026
# `@a = local = expr` write, or `if (sprite = arr[i])` in a@@ -30405,6 +30471,42 @@
3040530471
end3040630472
return3040730473
end30474+
# `@x ||= expr`: `@x = expr` only when @x is currently nil/false.30475+
# Lower to `if (!@x) { @x = expr; }`. For pointer-typed slots30476+
# (sp_Foo *, const char *, ...) the C `!ptr` check is exact; for30477+
# an int slot we treat 0 as falsy (Spinel's int slot has no30478+
# separate nil/false state). The `&&= expr` form is the dual.30479+
# Compile the rhs INSIDE the conditional block so any auxiliary30480+
# emits land in the conditional body — matches Ruby's30481+
# short-circuit semantics where the rhs (and its side effects)30482+
# only run when the lhs is falsy.30483+
if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"30484+
ivor_iname = @nd_name[nid]30485+
ivor_lhs = ivar_lhs(ivor_iname)30486+
ivor_expr_id = @nd_expression[nid]30487+
ivor_ivar_t = ""30488+
if @current_class_idx >= 030489+
ivor_ivar_t = cls_ivar_type(@current_class_idx, ivor_iname)30490+
end30491+
ivor_cond = ""30492+
if base_type(ivor_ivar_t) == "poly"30493+
ivor_cond = "(" + ivor_lhs + ").tag == SP_TAG_NIL || ((" + ivor_lhs + ").tag == SP_TAG_BOOL && (" + ivor_lhs + ").v.i == 0)"30494+
else30495+
ivor_cond = "!(" + ivor_lhs + ")"30496+
end30497+
if t == "InstanceVariableAndWriteNode"30498+
ivor_cond = "!(" + ivor_cond + ")"30499+
end30500+
emit(" if (" + ivor_cond + ") {")30501+
ivor_val = compile_expr(ivor_expr_id)30502+
ivor_val_t = infer_type(ivor_expr_id)30503+
if base_type(ivor_ivar_t) == "poly" && ivor_val_t != "" && ivor_val_t != "poly"30504+
ivor_val = box_value_to_poly(ivor_val_t, ivor_val)30505+
end30506+
emit(" " + ivor_lhs + " = " + ivor_val + ";")30507+
emit(" }")30508+
return30509+
end3040830510
if t == "InstanceVariableOperatorWriteNode"3040930511
op = @nd_binop[nid]3041030512
val = compile_expr(@nd_expression[nid])test/ivar_and_write_basic.rbaddedView file
Loading diff…
test/ivar_and_write_basic.rb.expectedaddedView file
Loading diff…
test/ivar_or_write_basic.rbaddedView file
Loading diff…
test/ivar_or_write_basic.rb.expectedaddedView file
Loading diff…