Commit 9c05e832

9c05e832a7c0Merged

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

Description

Merge pull request #462 from ryanseys/rs-if-expr-multi-stmt

fix(codegen): emit side-effect stmts in consumed if-expr branches

Details

Commit
9c05e832a7c0
Parent
91d110ac4c57
Merge parent
7c5c27e035d5

spinel_codegen.rbmodifiedView file

@@ -20307,12 +20307,90 @@
2030720307 "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")"
2030820308 end
2030920309
20310+ def if_branch_has_multi_stmt(body)
20311+ if body < 0
20312+ return 0
20313+ end
20314+ if get_stmts(body).length > 1
20315+ return 1
20316+ end
20317+ 0
20318+ end
20319+
20320+ # Assign the value of an if-arm body to a temp, executing any
20321+ # leading side-effect statements before the assignment. Body
20322+ # convention: stmts[0..n-2] run for their side-effects;
20323+ # stmts[n-1] supplies the arm's value. Empty/missing body → "0".
20324+ def compile_if_arm_to_tmp(body, tmp, unified_t)
20325+ if body < 0
20326+ emit(" " + tmp + " = 0;")
20327+ return
20328+ end
20329+ stmts = get_stmts(body)
20330+ if stmts.length == 0
20331+ emit(" " + tmp + " = 0;")
20332+ return
20333+ end
20334+ i = 0
20335+ while i < stmts.length - 1
20336+ compile_stmt(stmts[i])
20337+ i = i + 1
20338+ end
20339+ last_id = stmts.last
20340+ val = compile_expr(last_id)
20341+ if unified_t == "poly" && infer_type(last_id) != "poly"
20342+ val = box_value_to_poly(infer_type(last_id), val)
20343+ end
20344+ emit(" " + tmp + " = " + val + ";")
20345+ end
20346+
2031020347 def compile_if_expr(nid)
2031120348 cond = compile_cond_expr(@nd_predicate[nid])
2031220349 unified_t = infer_type(nid)
20350+ body = @nd_body[nid]
20351+ sub = @nd_subsequent[nid]
20352+
20353+ # Side-effect-aware path: when any branch holds more than one
20354+ # statement, the leading stmts must execute conditionally. The
20355+ # plain ternary form drops them. Emit a real if-statement that
20356+ # assigns to a temp and return the temp as the expression value.
20357+ # Elsif chains recurse — the inner call returns either a temp or
20358+ # a ternary string, both valid expressions.
20359+ has_then_multi = if_branch_has_multi_stmt(body)
20360+ has_else_multi = 0
20361+ if sub >= 0 && @nd_type[sub] == "ElseNode"
20362+ has_else_multi = if_branch_has_multi_stmt(@nd_body[sub])
20363+ end
20364+ if has_then_multi == 1 || has_else_multi == 1
20365+ tmp = new_temp
20366+ emit(" " + c_type(unified_t) + " " + tmp + ";")
20367+ emit(" if (" + cond + ") {")
20368+ @indent = @indent + 1
20369+ compile_if_arm_to_tmp(body, tmp, unified_t)
20370+ @indent = @indent - 1
20371+ emit(" } else {")
20372+ @indent = @indent + 1
20373+ if sub >= 0 && @nd_type[sub] == "ElseNode"
20374+ compile_if_arm_to_tmp(@nd_body[sub], tmp, unified_t)
20375+ elsif sub >= 0
20376+ sub_val = compile_if_expr(sub)
20377+ sub_t = infer_type(sub)
20378+ if unified_t == "poly" && sub_t != "poly"
20379+ sub_val = box_value_to_poly(sub_t, sub_val)
20380+ end
20381+ emit(" " + tmp + " = " + sub_val + ";")
20382+ else
20383+ emit(" " + tmp + " = 0;")
20384+ end
20385+ @indent = @indent - 1
20386+ emit(" }")
20387+ return tmp
20388+ end
20389+
20390+ # Fast path: every branch is at most one statement, so the
20391+ # whole if-expression collapses to a C ternary.
2031320392 then_val = "0"
2031420393 then_t = "nil"
20315- body = @nd_body[nid]
2031620394 if body >= 0
2031720395 stmts = get_stmts(body)
2031820396 if stmts.length > 0
@@ -20322,7 +20400,6 @@
2032220400 end
2032320401 else_val = "0"
2032420402 else_t = "nil"
20325- sub = @nd_subsequent[nid]
2032620403 if sub >= 0
2032720404 if @nd_type[sub] == "ElseNode"
2032820405 eb = @nd_body[sub]