Commit c2ec3e02
c2ec3e02b08aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
refactor(codegen): unify if/unless return-emit lowering
compile_if_return (29942 pre-refactor) and compile_unless_return (29907 pre-refactor) shared ~80% of their bodies, differing only in (a) predicate polarity, (b) subsequent vs else-clause field, and (c) elsif chain support (IfNode only).
Collapse both into compile_cond_return(nid, rt, is_unless) and keep the original names as 1-line wrappers so the dispatch in compile_body_return stays untouched.
-20 lines net, no behavior change (bootstrap fixpoint OK, 388 tests green, optcarrot checksum 59662).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- c2ec3e02b08a
- Parent
- 26a33e95628c
spinel_codegen.rbmodifiedView file
@@ -29845,9 +29845,17 @@
2984529845
return2984629846
end2984729847
29848-
def compile_unless_return(nid, rt)29848+
# Shared lowering for if- and unless-modifier-as-last-expr.29849+
# `is_unless == 1` inverts the predicate and reads29850+
# @nd_else_clause; the if path also threads elsif chains29851+
# (subsequent IfNode) through a wrapping `else { ... }` to keep29852+
# poly-is_a? predicate prologues from leaking declarations.29853+
def compile_cond_return(nid, rt, is_unless)2984929854
cond = compile_cond_expr(@nd_predicate[nid])29850-
emit(" if (!(" + cond + ")) {")29855+
if is_unless == 129856+
cond = "!(" + cond + ")"29857+
end29858+
emit(" if (" + cond + ") {")2985129859
@indent = @indent + 12985229860
body = @nd_body[nid]2985329861
if body >= 0@@ -29858,11 +29866,23 @@
2985829866
end2985929867
end2986029868
@indent = @indent - 129861-
ec = @nd_else_clause[nid]29862-
if ec >= 029869+
if is_unless == 129870+
sub = @nd_else_clause[nid]29871+
else29872+
sub = @nd_subsequent[nid]29873+
end29874+
if sub >= 029875+
if is_unless == 0 && @nd_type[sub] != "ElseNode"29876+
emit(" } else {")29877+
@indent = @indent + 129878+
compile_cond_return(sub, rt, 0)29879+
@indent = @indent - 129880+
emit(" }")29881+
return29882+
end2986329883
emit(" } else {")2986429884
@indent = @indent + 129865-
eb = @nd_body[ec]29885+
eb = @nd_body[sub]2986629886
if eb >= 02986729887
compile_body_return(eb, rt)2986829888
else@@ -29881,51 +29901,11 @@
2988129901
end2988229902
2988329903
def compile_if_return(nid, rt)29884-
cond = compile_cond_expr(@nd_predicate[nid])29885-
emit(" if (" + cond + ") {")29886-
@indent = @indent + 129887-
body = @nd_body[nid]29888-
if body >= 029889-
compile_body_return(body, rt)29890-
else29891-
if rt != "void"29892-
emit(" return " + c_return_default(rt) + ";")29893-
end29894-
end29895-
@indent = @indent - 129896-
sub = @nd_subsequent[nid]29897-
if sub >= 029898-
if @nd_type[sub] == "ElseNode"29899-
emit(" } else {")29900-
@indent = @indent + 129901-
eb = @nd_body[sub]29902-
if eb >= 029903-
compile_body_return(eb, rt)29904-
else29905-
if rt != "void"29906-
emit(" return " + c_return_default(rt) + ";")29907-
end29908-
end29909-
@indent = @indent - 129910-
else29911-
# Wrap elsif in braces. See compile_if_stmt for the same29912-
# rationale: predicates that emit prologue C statements29913-
# (poly is_a? etc.) would leak declarations out of a bare29914-
# else because the bare else's body is one statement.29915-
emit(" } else {")29916-
@indent = @indent + 129917-
compile_if_return(sub, rt)29918-
@indent = @indent - 129919-
emit(" }")29920-
return29921-
end29922-
else29923-
if rt != "void"29924-
emit(" } else {")29925-
emit(" return " + c_return_default(rt) + ";")29926-
end29927-
end29928-
emit(" }")29904+
compile_cond_return(nid, rt, 0)29905+
end29906+
29907+
def compile_unless_return(nid, rt)29908+
compile_cond_return(nid, rt, 1)2992929909
end2993029910
2993129911
def compile_case_match_return(nid, rt)