Commit 1c819212
1c819212d772Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
refactor(analyze,codegen): consolidate Math. name set into helpers
The recognized Math module function set lived hard-coded in two
places (analyze's infer_math_and_misc_type return-type gate and
codegen's compile_constant_recv_expr emit dispatch), with no
mechanical link between them — a name added to one side and
forgotten on the other would let a Math.cbrt(x) analyze type
the call as float but compile to an unresolved-call fallthrough,
or vice versa.
Extract to single-purpose helpers:
spinel_analyze.rb:math_fn_returns_float? — 1 if mname is a
Math fn whose return type is float
spinel_codegen.rb:math_fn_one_arg? / math_fn_two_arg? — 1
if mname is a 1-arg or 2-arg Math fn lowered to a libm
call of the same name
The codegen dispatch collapses to mname + "(" + compile_arg0 + ")" and `mname + "(" + compile_expr(a0) + ", " + compile_expr(a1)
- ")"` once the predicates fire — the 19-line if-chain reduces to two clauses.
Adding a new Math fn now requires updating the two helpers in both files; the test/math_fn_parity.rb regression ensures all 19 currently-recognised names round-trip CRuby's output.
No behaviour change. Tests 459 pass, bench 57/57, bootstrap 4-way fixpoint OK.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 1c819212d772
- Parent
- 1d2c2905b8d2
spinel_analyze.rbmodifiedView file
"" end # 1 if `mname` is a Math module function whose return type is # float (sqrt / sin / cos / tan / asin / acos / atan / sinh / # cosh / tanh / asinh / acosh / atanh / log / log2 / log10 / # exp / atan2 / hypot). Single point of truth so the codegen # dispatch in compile_constant_recv_expr can stay aligned; # adding a new Math fn means updating this list and the # parallel dispatch table in spinel_codegen.rb. def math_fn_returns_float?(mname) if mname == "sqrt" || mname == "cos" || mname == "sin" || mname == "tan" return 1 end if mname == "acos" || mname == "asin" || mname == "atan" return 1 end if mname == "sinh" || mname == "cosh" || mname == "tanh" return 1 end if mname == "asinh" || mname == "acosh" || mname == "atanh" return 1 end if mname == "log" || mname == "log2" || mname == "log10" || mname == "exp" return 1 end if mname == "atan2" || mname == "hypot" return 1 end 0 end def infer_math_and_misc_type(nid, mname, recv) # backtick if mname == "`" # `tan` / etc. on a non-Math receiver got typed as float # regardless — e.g. a `def log; @log; end` accessor returning a # str_array got misinferred as float and downstream # `router.log[i]` read as a float-index. Gate the whole math # block on recv shape and let infer_recv_method_type handle the # non-Math case. # `router.log[i]` read as a float-index. The list is kept in # sync with the parallel dispatch in # `compile_constant_recv_expr`'s `if rcname == "Math"` block; # additions need both sides updated. if recv < 0 || (@nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "Math") if mname == "sqrt" return "float" end if mname == "cos" return "float" end if mname == "sin" return "float" end if mname == "tan" return "float" end if mname == "acos" || mname == "asin" || mname == "atan" return "float" end # Hyperbolic + inverse hyperbolic — same C99 libm wrappers as the # circular ones above. Issue: returning "float" here lets call sites # that lift `Math.tanh(x)` into a non-Float context (e.g. into an # IntArray slot via `arr[i] = Math.tanh(x)`) get caught at type- # check time instead of silently emitting `0`. if mname == "sinh" || mname == "cosh" || mname == "tanh" return "float" end if mname == "asinh" || mname == "acosh" || mname == "atanh" return "float" end if mname == "log" return "float" end if mname == "log2" return "float" end if mname == "log10" return "float" end if mname == "exp" return "float" end if mname == "atan2" return "float" end if mname == "hypot" if math_fn_returns_float?(mname) == 1 return "float" end endspinel_codegen.rbmodifiedView file
Loading diff…
test/math_fn_parity.rbaddedView file
Loading diff…
test/math_fn_parity.rb.expectedaddedView file
Loading diff…