Commit 758d6bba

758d6bba0454Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

feat(codegen): poly arg into bigint helper + bigint into float context

Two more promote-mode coercions:

  • compile_bigint_arg: poly-typed arg (a method whose return slot ended up "poly" due to heterogeneous body — Mixer / Noise / Pulse sample in optcarrot) is unboxed via .v.i then re-boxed into sp_Bigint *. Previously the catch-all sp_bigint_new_int(<poly_val>) cast tripped C compile.
  • compile_arg0_as_float and the Complex.polar FFI arm: bigint args now unbox via sp_bigint_to_int(...) before the (mrb_float) cast. C rejects struct-to-double casts; this path was the last optcarrot C compile error.

Optcarrot --int-overflow=promote: C compile now succeeds (0 errors). Binary runs but segfaults at runtime -- next phase covers that.

Default raise: 631/0/0. Optcarrot wrap checksum 59662 unchanged.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
758d6bba0454
Parent
5ff1b0dd76ff

spinel_codegen.rbmodifiedView file

@@ -12963,9 +12963,18 @@
1296312963 arg_ids = get_args(args_id)
1296412964 if arg_ids.length > 0
1296512965 ce = compile_expr(arg_ids[0])
12966- if infer_type(arg_ids[0]) == "poly"
12966+ at_a0f = infer_type(arg_ids[0])
12967+ if at_a0f == "poly"
1296712968 return "sp_poly_to_f(" + ce + ")"
1296812969 end
12970+ # `--int-overflow=promote` widens int to sp_Bigint *; a float
12971+ # context (FFI :float arg, Math.sin operand, etc.) needs the
12972+ # mrb_float scalar. Unbox via sp_bigint_to_int and let the
12973+ # implicit int -> double conversion finish the cast.
12974+ if at_a0f == "bigint" || expr_emit_is_bigint(arg_ids[0]) == 1
12975+ @needs_bigint = 1
12976+ return "(mrb_float)sp_bigint_to_int((sp_Bigint *)" + ce + ")"
12977+ end
1296912978 return ce
1297012979 end
1297112980 end
@@ -15441,6 +15450,14 @@
1544115450 # latent issue triggered by bigint inside begin/rescue.
1544215451 return "(sp_Bigint *)" + val
1544315452 end
15453+ if at == "poly"
15454+ # poly-typed source whose payload is an int (SP_TAG_INT). Unbox
15455+ # via .v.i then re-box into sp_Bigint *. Common in optcarrot
15456+ # where Mixer/Noise/Pulse `sample` methods end up with a "poly"
15457+ # return slot because their bodies have heterogeneous tails.
15458+ @needs_rb_value = 1
15459+ return "sp_bigint_new_int((" + val + ").v.i)"
15460+ end
1544415461 return "sp_bigint_new_int(" + val + ")"
1544515462 end
1544615463 end
@@ -20300,13 +20317,21 @@
2030020317 if aargs_cp.length >= 2
2030120318 a0_e = compile_expr(aargs_cp[0])
2030220319 a1_e = compile_expr(aargs_cp[1])
20303- if infer_type(aargs_cp[0]) == "poly"
20320+ a0_t_cp = infer_type(aargs_cp[0])
20321+ if a0_t_cp == "poly"
2030420322 a0_e = "sp_poly_to_f(" + a0_e + ")"
20323+ elsif a0_t_cp == "bigint" || expr_emit_is_bigint(aargs_cp[0]) == 1
20324+ @needs_bigint = 1
20325+ a0_e = "(mrb_float)sp_bigint_to_int((sp_Bigint *)" + a0_e + ")"
2030520326 else
2030620327 a0_e = "(mrb_float)(" + a0_e + ")"
2030720328 end
20308- if infer_type(aargs_cp[1]) == "poly"
20329+ a1_t_cp = infer_type(aargs_cp[1])
20330+ if a1_t_cp == "poly"
2030920331 a1_e = "sp_poly_to_f(" + a1_e + ")"
20332+ elsif a1_t_cp == "bigint" || expr_emit_is_bigint(aargs_cp[1]) == 1
20333+ @needs_bigint = 1
20334+ a1_e = "(mrb_float)sp_bigint_to_int((sp_Bigint *)" + a1_e + ")"
2031020335 else
2031120336 a1_e = "(mrb_float)(" + a1_e + ")"
2031220337 end