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 return
2984629846 end
2984729847
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 reads
29850+ # @nd_else_clause; the if path also threads elsif chains
29851+ # (subsequent IfNode) through a wrapping `else { ... }` to keep
29852+ # 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 == 1
29856+ cond = "!(" + cond + ")"
29857+ end
29858+ emit(" if (" + cond + ") {")
2985129859 @indent = @indent + 1
2985229860 body = @nd_body[nid]
2985329861 if body >= 0
@@ -29858,11 +29866,23 @@
2985829866 end
2985929867 end
2986029868 @indent = @indent - 1
29861- ec = @nd_else_clause[nid]
29862- if ec >= 0
29869+ if is_unless == 1
29870+ sub = @nd_else_clause[nid]
29871+ else
29872+ sub = @nd_subsequent[nid]
29873+ end
29874+ if sub >= 0
29875+ if is_unless == 0 && @nd_type[sub] != "ElseNode"
29876+ emit(" } else {")
29877+ @indent = @indent + 1
29878+ compile_cond_return(sub, rt, 0)
29879+ @indent = @indent - 1
29880+ emit(" }")
29881+ return
29882+ end
2986329883 emit(" } else {")
2986429884 @indent = @indent + 1
29865- eb = @nd_body[ec]
29885+ eb = @nd_body[sub]
2986629886 if eb >= 0
2986729887 compile_body_return(eb, rt)
2986829888 else
@@ -29881,51 +29901,11 @@
2988129901 end
2988229902
2988329903 def compile_if_return(nid, rt)
29884- cond = compile_cond_expr(@nd_predicate[nid])
29885- emit(" if (" + cond + ") {")
29886- @indent = @indent + 1
29887- body = @nd_body[nid]
29888- if body >= 0
29889- compile_body_return(body, rt)
29890- else
29891- if rt != "void"
29892- emit(" return " + c_return_default(rt) + ";")
29893- end
29894- end
29895- @indent = @indent - 1
29896- sub = @nd_subsequent[nid]
29897- if sub >= 0
29898- if @nd_type[sub] == "ElseNode"
29899- emit(" } else {")
29900- @indent = @indent + 1
29901- eb = @nd_body[sub]
29902- if eb >= 0
29903- compile_body_return(eb, rt)
29904- else
29905- if rt != "void"
29906- emit(" return " + c_return_default(rt) + ";")
29907- end
29908- end
29909- @indent = @indent - 1
29910- else
29911- # Wrap elsif in braces. See compile_if_stmt for the same
29912- # rationale: predicates that emit prologue C statements
29913- # (poly is_a? etc.) would leak declarations out of a bare
29914- # else because the bare else's body is one statement.
29915- emit(" } else {")
29916- @indent = @indent + 1
29917- compile_if_return(sub, rt)
29918- @indent = @indent - 1
29919- emit(" }")
29920- return
29921- end
29922- else
29923- if rt != "void"
29924- emit(" } else {")
29925- emit(" return " + c_return_default(rt) + ";")
29926- end
29927- end
29928- emit(" }")
29904+ compile_cond_return(nid, rt, 0)
29905+ end
29906+
29907+ def compile_unless_return(nid, rt)
29908+ compile_cond_return(nid, rt, 1)
2992929909 end
2993029910
2993129911 def compile_case_match_return(nid, rt)