Commit 24a87506

24a87506af5dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

codegen: Integer#magnitude/modulo/remainder/size/gcdlcm

Five Integer instance methods that fell through to the unresolved- call fallback (emitting 0). All are direct lowerings to existing helpers:

magnitude - alias for abs modulo - sp_imod (same as %) remainder - truncated-division remainder, differs from % for mixed-sign operands (-7.remainder(3) == -1) size - bytes per mrb_int = 8 (constant on 64-bit) gcdlcm - [gcd, lcm] tuple via the same path divmod uses

Refs #860 #873 #875 #876 #894

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

Details

Commit
24a87506af5d
Parent
da7ca3b74f65

spinel_analyze.rbmodifiedView file

@@ -3769,7 +3769,10 @@
37693769 if mname == "allbits?" || mname == "anybits?" || mname == "nobits?"
37703770 return 1
37713771 end
3772- if mname == "gcd" || mname == "lcm" || mname == "ceildiv" || mname == "div" || mname == "clamp"
3772+ if mname == "gcd" || mname == "lcm" || mname == "gcdlcm" || mname == "ceildiv" || mname == "div" || mname == "clamp"
3773+ return 1
3774+ end
3775+ if mname == "magnitude" || mname == "modulo" || mname == "remainder" || mname == "size"
37733776 return 1
37743777 end
37753778 if mname == "itself" || mname == "then" || mname == "yield_self"
@@ -4392,7 +4395,16 @@
43924395 if mname == "nil?"
43934396 return "bool"
43944397 end
4395- if mname == "abs"
4398+ if mname == "abs" || mname == "magnitude"
4399+ if recv >= 0
4400+ lt = infer_type(recv)
4401+ if lt == "float"
4402+ return "float"
4403+ end
4404+ end
4405+ return "int"
4406+ end
4407+ if mname == "modulo" || mname == "remainder"
43964408 if recv >= 0
43974409 lt = infer_type(recv)
43984410 if lt == "float"
@@ -4401,6 +4413,20 @@
44014413 end
44024414 return "int"
44034415 end
4416+ # Integer#size: bytes used to represent the int. Spinel
4417+ # mrb_int is always 64-bit, so it's a constant 8. Issue #860.
4418+ if mname == "size" && recv >= 0
4419+ rt_sz = infer_type(recv)
4420+ if rt_sz == "int"
4421+ return "int"
4422+ end
4423+ end
4424+ # Integer#gcdlcm returns [gcd, lcm] as a tuple value. Issue #894.
4425+ if mname == "gcdlcm"
4426+ tt_gl_inf = "tuple:int,int"
4427+ register_tuple_type(tt_gl_inf)
4428+ return "tuple:int,int"
4429+ end
44044430 if mname == "**" || mname == "pow"
44054431 if recv >= 0
44064432 lt = infer_type(recv)