Commit b3836955

b3836955a63fMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): support "fmt" % value (single-value String#% form)

"fmt" % v on a string receiver fell through to sp_imod, which rejected the format string as an int:

/tmp/_t.c:12:42: error: passing argument 1 of 'sp_imod' makes integer from pointer without a cast [-Wint-conversion] 12 | printf("%lld\n", (long long)sp_imod((&("\xff" "%d")[1]), 42));

Detect a string LHS, emit sp_sprintf with the appropriate cast ((double) for %f, (long long) for integer conversions, const char * for %s), and rewrite literal %d/%i to %lld at compile time so mrb_int args match. The rewrite is byte-by-byte so the bootstrap gen1.c (which doesn't link libspinel_rt.a) doesn't need the regex engine.

The companion type-inference change makes infer_call_type return "string" for string % value so callers (e.g. puts) treat the result as a string, not as an int.

Adds test/string_format_single.rb.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b3836955a63f
Parent
0c3c5d918fc5

spinel_codegen.rbmodifiedView file

@@ -1771,17 +1771,23 @@
17711771 return "int"
17721772 end
17731773 if mname == "%"
1774- # String#% returns "string" only when the RHS is a str_array
1775- # (sprintf-style format). Other RHS types fall through to sp_imod
1776- # in compile_operator_expr, which yields int.
1774+ # String#% returns "string" when the LHS is a string (and the RHS
1775+ # is a str_array or a single primitive value). Otherwise the
1776+ # operator is integer modulo.
17771777 if recv >= 0
17781778 rt = infer_type(recv)
17791779 if rt == "string" || rt == "mutable_str"
17801780 args_id = @nd_arguments[nid]
17811781 if args_id >= 0
17821782 aargs = get_args(args_id)
1783- if aargs.length > 0 && infer_type(aargs[0]) == "str_array"
1784- return "string"
1783+ if aargs.length > 0
1784+ at = infer_type(aargs[0])
1785+ if at == "str_array"
1786+ return "string"
1787+ end
1788+ if rt == "string"
1789+ return "string"
1790+ end
17851791 end
17861792 end
17871793 end
@@ -13901,6 +13907,62 @@
1390113907 end
1390213908 end
1390313909 end
13910+ if lt == "string"
13911+ # Ruby's `"fmt" % val` — single-value form. Arg is cast to match
13912+ # the conversion: (double) for %f, (long long) for integer
13913+ # conversions. Ruby's `%d` is mapped to C's `%lld` when the
13914+ # format is a string literal — otherwise pass through.
13915+ arg0 = get_args(@nd_arguments[nid])[0]
13916+ at = infer_type(arg0)
13917+ fmt_c = compile_expr(recv)
13918+ # Literal-format optimization: rewrite %d → %lld at compile time
13919+ # (done byte-by-byte to avoid pulling in the regex engine — the
13920+ # self-hosted bootstrap step links without libspinel_rt.a).
13921+ if @nd_type[recv] == "StringNode"
13922+ lit = @nd_unescaped[recv]
13923+ if lit == ""
13924+ lit = @nd_content[recv]
13925+ end
13926+ rewritten = ""
13927+ fi = 0
13928+ while fi < lit.length
13929+ if lit[fi] == "%"
13930+ # Copy "%" + any flags/width + final spec.
13931+ rewritten = rewritten + "%"
13932+ fi = fi + 1
13933+ while fi < lit.length
13934+ c = lit[fi]
13935+ if c == "-" || c == "+" || c == " " || c == "#" || c == "0" || (c >= "1" && c <= "9") || c == "."
13936+ rewritten = rewritten + c
13937+ fi = fi + 1
13938+ else
13939+ break
13940+ end
13941+ end
13942+ if fi < lit.length
13943+ c = lit[fi]
13944+ if c == "d" || c == "i"
13945+ rewritten = rewritten + "lld"
13946+ else
13947+ rewritten = rewritten + c
13948+ end
13949+ fi = fi + 1
13950+ end
13951+ else
13952+ rewritten = rewritten + lit[fi]
13953+ fi = fi + 1
13954+ end
13955+ end
13956+ fmt_c = c_string_literal(rewritten)
13957+ end
13958+ if at == "float"
13959+ return "sp_sprintf(" + fmt_c + ", (double)" + compile_expr(arg0) + ")"
13960+ elsif at == "string"
13961+ return "sp_sprintf(" + fmt_c + ", " + compile_expr(arg0) + ")"
13962+ else
13963+ return "sp_sprintf(" + fmt_c + ", (long long)" + compile_expr(arg0) + ")"
13964+ end
13965+ end
1390413966 return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
1390513967 end
1390613968 if mname == "<"