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 end
24212421 return "int"
24222422 end
2423+ if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"
2424+ # `(@x ||= expr)` / `(@x &&= expr)` evaluates to @x's slot type
2425+ # (union of the prior value and the rhs, but Spinel widens those
2426+ # via update_ivar_type already, so reading the slot type is the
2427+ # same answer).
2428+ if @current_class_idx >= 0
2429+ return cls_ivar_type(@current_class_idx, @nd_name[nid])
2430+ end
2431+ return "int"
2432+ end
24232433 if t == "InstanceVariableReadNode"
24242434 if @current_class_idx >= 0
24252435 return cls_ivar_type(@current_class_idx, @nd_name[nid])
@@ -8161,6 +8171,36 @@
81618171 add_ivar(ci, iname, "int")
81628172 end
81638173 end
8174+ # `@x ||= expr` / `@x &&= expr`: register the slot when first
8175+ # encountered. The rhs type seeds the ivar; without registration
8176+ # the struct comes out without the slot and any subsequent read
8177+ # 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) == 1
8182+ vtype = infer_ivar_init_type(expr_first)
8183+ if vtype != "int" && vtype != "nil"
8184+ update_ivar_type(ci, iname, vtype)
8185+ end
8186+ elsif ivar_exists(ci, iname) == 0
8187+ vtype = infer_ivar_init_type(expr_first)
8188+ # ||= reads @x first; pre-register a nil observation so the
8189+ # `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+ end
8194+ else
8195+ 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+ end
8201+ end
8202+ end
8203+ end
81648204 # Multi-write to ivars: `@a, @b = expr1, expr2` (or `[expr1, expr2]`).
81658205 # Without this branch, ivars assigned only via destructuring never get
81668206 # registered and the struct comes out missing them.
@@ -12320,6 +12360,23 @@
1232012360 end
1232112361 end
1232212362 end
12363+ # `@x ||= expr` / `@x &&= expr`: writer-scan the rhs the same as a
12364+ # plain `@x = expr` so the slot widens from its `nil` seed to the
12365+ # 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 >= 0
12369+ iname = @nd_name[nid]
12370+ expr_id = @nd_expression[nid]
12371+ if expr_id >= 0
12372+ 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+ end
12377+ end
12378+ end
12379+ end
1232312380 # Multi-write to ivars: `@a, @b = pulse_0, pulse_1`. Mirrors the
1232412381 # single-write branch above so each ivar's type is widened from
1232512382 # the corresponding RHS slot. Without this the multi-write left
@@ -20955,6 +21012,15 @@
2095521012 end
2095621013 return "(" + ivar_lhs(iname_w) + " = " + val + ")"
2095721014 end
21015+ if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"
21016+ # Expression form of `@x ||= …` / `@x &&= …`. Method bodies
21017+ # whose last statement is one of these reach `compile_expr`
21018+ # via the return-value path, so we need a value-producing
21019+ # form. Run the statement-style emit for the side effect, then
21020+ # surface `@x`'s post-assign value as the expression result.
21021+ compile_stmt(nid)
21022+ return ivar_lhs(@nd_name[nid])
21023+ end
2095821024 if t == "LocalVariableWriteNode"
2095921025 # `local = expr` used as an expression (e.g. inside a chained
2096021026 # `@a = local = expr` write, or `if (sprite = arr[i])` in a
@@ -30405,6 +30471,42 @@
3040530471 end
3040630472 return
3040730473 end
30474+ # `@x ||= expr`: `@x = expr` only when @x is currently nil/false.
30475+ # Lower to `if (!@x) { @x = expr; }`. For pointer-typed slots
30476+ # (sp_Foo *, const char *, ...) the C `!ptr` check is exact; for
30477+ # an int slot we treat 0 as falsy (Spinel's int slot has no
30478+ # separate nil/false state). The `&&= expr` form is the dual.
30479+ # Compile the rhs INSIDE the conditional block so any auxiliary
30480+ # emits land in the conditional body — matches Ruby's
30481+ # 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 >= 0
30489+ ivor_ivar_t = cls_ivar_type(@current_class_idx, ivor_iname)
30490+ end
30491+ 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+ else
30495+ ivor_cond = "!(" + ivor_lhs + ")"
30496+ end
30497+ if t == "InstanceVariableAndWriteNode"
30498+ ivor_cond = "!(" + ivor_cond + ")"
30499+ end
30500+ 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+ end
30506+ emit(" " + ivor_lhs + " = " + ivor_val + ";")
30507+ emit(" }")
30508+ return
30509+ end
3040830510 if t == "InstanceVariableOperatorWriteNode"
3040930511 op = @nd_binop[nid]
3041030512 val = compile_expr(@nd_expression[nid])