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

@@ -4867,6 +4867,35 @@
48674867 ""
48684868 end
48694869
4870+ # 1 if `mname` is a Math module function whose return type is
4871+ # float (sqrt / sin / cos / tan / asin / acos / atan / sinh /
4872+ # cosh / tanh / asinh / acosh / atanh / log / log2 / log10 /
4873+ # exp / atan2 / hypot). Single point of truth so the codegen
4874+ # dispatch in compile_constant_recv_expr can stay aligned;
4875+ # adding a new Math fn means updating this list and the
4876+ # parallel dispatch table in spinel_codegen.rb.
4877+ def math_fn_returns_float?(mname)
4878+ if mname == "sqrt" || mname == "cos" || mname == "sin" || mname == "tan"
4879+ return 1
4880+ end
4881+ if mname == "acos" || mname == "asin" || mname == "atan"
4882+ return 1
4883+ end
4884+ if mname == "sinh" || mname == "cosh" || mname == "tanh"
4885+ return 1
4886+ end
4887+ if mname == "asinh" || mname == "acosh" || mname == "atanh"
4888+ return 1
4889+ end
4890+ if mname == "log" || mname == "log2" || mname == "log10" || mname == "exp"
4891+ return 1
4892+ end
4893+ if mname == "atan2" || mname == "hypot"
4894+ return 1
4895+ end
4896+ 0
4897+ end
4898+
48704899 def infer_math_and_misc_type(nid, mname, recv)
48714900 # backtick
48724901 if mname == "`"
@@ -4879,52 +4908,12 @@
48794908 # `tan` / etc. on a non-Math receiver got typed as float
48804909 # regardless — e.g. a `def log; @log; end` accessor returning a
48814910 # str_array got misinferred as float and downstream
4882- # `router.log[i]` read as a float-index. Gate the whole math
4883- # block on recv shape and let infer_recv_method_type handle the
4884- # non-Math case.
4911+ # `router.log[i]` read as a float-index. The list is kept in
4912+ # sync with the parallel dispatch in
4913+ # `compile_constant_recv_expr`'s `if rcname == "Math"` block;
4914+ # additions need both sides updated.
48854915 if recv < 0 || (@nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "Math")
4886- if mname == "sqrt"
4887- return "float"
4888- end
4889- if mname == "cos"
4890- return "float"
4891- end
4892- if mname == "sin"
4893- return "float"
4894- end
4895- if mname == "tan"
4896- return "float"
4897- end
4898- if mname == "acos" || mname == "asin" || mname == "atan"
4899- return "float"
4900- end
4901- # Hyperbolic + inverse hyperbolic — same C99 libm wrappers as the
4902- # circular ones above. Issue: returning "float" here lets call sites
4903- # that lift `Math.tanh(x)` into a non-Float context (e.g. into an
4904- # IntArray slot via `arr[i] = Math.tanh(x)`) get caught at type-
4905- # check time instead of silently emitting `0`.
4906- if mname == "sinh" || mname == "cosh" || mname == "tanh"
4907- return "float"
4908- end
4909- if mname == "asinh" || mname == "acosh" || mname == "atanh"
4910- return "float"
4911- end
4912- if mname == "log"
4913- return "float"
4914- end
4915- if mname == "log2"
4916- return "float"
4917- end
4918- if mname == "log10"
4919- return "float"
4920- end
4921- if mname == "exp"
4922- return "float"
4923- end
4924- if mname == "atan2"
4925- return "float"
4926- end
4927- if mname == "hypot"
4916+ if math_fn_returns_float?(mname) == 1
49284917 return "float"
49294918 end
49304919 end