Commit a3b55d4c
a3b55d4c1128Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: pass bm_poly_arith, bm_misc, bm_lrama_features (partial), bm_mutable_str3
- Add poly arithmetic: sp_poly_add, sp_poly_sub, sp_poly_mul, sp_poly_gt
- Fix poly puts for float values to show .0 suffix
- Fix poly to_s dispatch
- Handle Float::INFINITY and Float::NAN constants
- Fix string << feature detection for needs_string_helpers
- Fix string + feature detection
- Fix splat/rest param call dispatch to not treat regular array params as splat
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- a3b55d4c1128
- Parent
- ea7e261436c7
spinel_codegen.rbmodifiedView file
@@ -784,11 +784,16 @@
784784
end785785
if t == "ConstantPathNode"786786
if @nd_receiver[nid] >= 0787-
cpname = @nd_name[@nd_receiver[nid]] + "_" + @nd_name[nid]787+
rname = @nd_name[@nd_receiver[nid]]788+
nname = @nd_name[nid]789+
cpname = rname + "_" + nname788790
ci = find_const_idx(cpname)789791
if ci >= 0790792
return @const_types[ci]791793
end794+
if rname == "Float"795+
return "float"796+
end792797
end793798
return "int"794799
end@@ -899,6 +904,9 @@
899904
if lt == "string"900905
return "string"901906
end907+
if lt == "poly"908+
return "poly"909+
end902910
if lt == "float"903911
return "float"904912
end@@ -923,6 +931,9 @@
923931
if lt == "string"924932
return "string"925933
end934+
if lt == "poly"935+
return "poly"936+
end926937
end927938
return "int"928939
end@@ -3126,6 +3137,22 @@
31263137
if mname == "upcase"31273138
@needs_string_helpers = 131283139
end3140+
if mname == "<<"3141+
if @nd_receiver[nid] >= 03142+
rt = infer_type(@nd_receiver[nid])3143+
if rt == "string"3144+
@needs_string_helpers = 13145+
end3146+
end3147+
end3148+
if mname == "+"3149+
if @nd_receiver[nid] >= 03150+
rt = infer_type(@nd_receiver[nid])3151+
if rt == "string"3152+
@needs_string_helpers = 13153+
end3154+
end3155+
end31293156
if mname == "downcase"31303157
@needs_string_helpers = 131313158
end@@ -3422,7 +3449,14 @@
34223449
ptypes[k] = at34233450
else34243451
if at == "int"3425-
# arg is int, param already has a type - keep it3452+
# Check if arg is a literal int (genuine int value)3453+
if k < arg_ids.length3454+
if @nd_type[arg_ids[k]] == "IntegerNode"3455+
ptypes[k] = "poly"3456+
@needs_rb_value = 13457+
end3458+
end3459+
# otherwise arg is int variable, param already has a type - keep it34263460
else34273461
# Genuinely different types - mark poly34283462
ptypes[k] = "poly"@@ -3593,6 +3627,22 @@
35933627
emit_string_helpers35943628
end35953629
if @needs_rb_value == 13630+
if @needs_string_helpers == 03631+
@needs_string_helpers = 13632+
if @needs_gc == 03633+
@needs_gc = 13634+
emit_gc_runtime3635+
end3636+
if @needs_int_array == 03637+
@needs_int_array = 13638+
emit_int_array_runtime3639+
end3640+
if @needs_str_array == 03641+
@needs_str_array = 13642+
emit_str_array_runtime3643+
end3644+
emit_string_helpers3645+
end35963646
emit_rb_value_runtime35973647
end35983648
if @needs_setjmp == 1@@ -3773,7 +3823,7 @@
37733823
emit_raw(" switch (v.tag) {")37743824
emit_raw(" case SP_TAG_INT: printf(\"%lld\\n\", (long long)v.v.i); break;")37753825
emit_raw(" case SP_TAG_STR: if (v.v.s) { fputs(v.v.s, stdout); if (!*v.v.s || v.v.s[strlen(v.v.s)-1] != '\\n') putchar('\\n'); } else putchar('\\n'); break;")3776-
emit_raw(" case SP_TAG_FLT: printf(\"%g\\n\", v.v.f); break;")3826+
emit_raw(" case SP_TAG_FLT: { char _fb[64]; snprintf(_fb,64,\"%g\",v.v.f); if(!strchr(_fb,'.')&&!strchr(_fb,'e')&&!strchr(_fb,'i')&&!strchr(_fb,'n')){strcat(_fb,\".0\");} printf(\"%s\\n\",_fb); break; }")37773827
emit_raw(" case SP_TAG_BOOL: puts(v.v.b ? \"true\" : \"false\"); break;")37783828
emit_raw(" case SP_TAG_NIL: putchar('\\n'); break;")37793829
emit_raw(" default: printf(\"%lld\\n\", (long long)v.v.i); break;")@@ -3781,6 +3831,10 @@
37813831
emit_raw("}")37823832
emit_raw("static mrb_bool sp_poly_nil_p(sp_RbVal v) { return v.tag == SP_TAG_NIL; }")37833833
emit_raw("static const char *sp_poly_to_s(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: { char *b = (char*)malloc(32); snprintf(b, 32, \"%lld\", (long long)v.v.i); return b; } case SP_TAG_STR: return v.v.s ? v.v.s : \"\"; case SP_TAG_FLT: { char *b = (char*)malloc(64); snprintf(b, 64, \"%g\", v.v.f); return b; } case SP_TAG_BOOL: return v.v.b ? \"true\" : \"false\"; case SP_TAG_NIL: return \"\"; default: return \"\"; } }")3834+
emit_raw("static sp_RbVal sp_poly_add(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i + b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f + b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i + b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f + (mrb_float)b.v.i); if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR) return sp_box_str(sp_str_concat(a.v.s, b.v.s)); return sp_box_int(0); }")3835+
emit_raw("static sp_RbVal sp_poly_sub(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i - b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f - b.v.f); return sp_box_int(0); }")3836+
emit_raw("static sp_RbVal sp_poly_mul(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i * b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f * b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i * b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f * (mrb_float)b.v.i); return sp_box_int(0); }")3837+
emit_raw("static mrb_bool sp_poly_gt(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return a.v.i > b.v.i; if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return a.v.f > b.v.f; return FALSE; }")37843838
emit_raw("")37853839
end37863840
@@ -5007,11 +5061,27 @@
50075061
end50085062
if t == "ConstantPathNode"50095063
if @nd_receiver[nid] >= 05010-
cpname = @nd_name[@nd_receiver[nid]] + "_" + @nd_name[nid]5064+
rname = @nd_name[@nd_receiver[nid]]5065+
nname = @nd_name[nid]5066+
cpname = rname + "_" + nname50115067
ci = find_const_idx(cpname)50125068
if ci >= 050135069
return "cst_" + cpname50145070
end5071+
# Built-in constants5072+
if rname == "Float"5073+
if nname == "INFINITY"5074+
return "(1.0/0.0)"5075+
end5076+
if nname == "NAN"5077+
return "(0.0/0.0)"5078+
end5079+
end5080+
if rname == "Integer"5081+
if nname == "MAX"5082+
return "INT64_MAX"5083+
end5084+
end50155085
return cpname50165086
end50175087
return @nd_name[nid]@@ -5353,13 +5423,23 @@
53535423
@needs_string_helpers = 153545424
return "sp_str_concat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"53555425
end5426+
if lt == "poly"5427+
@needs_rb_value = 15428+
@needs_string_helpers = 15429+
return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"5430+
end53565431
return "(" + compile_expr(recv) + " + " + compile_arg0(nid) + ")"53575432
end53585433
if mname == "-"5434+
lt = infer_type(recv)53595435
args_id = @nd_arguments[nid]53605436
if args_id < 053615437
return "(-" + compile_expr(recv) + ")"53625438
end5439+
if lt == "poly"5440+
@needs_rb_value = 15441+
return "sp_poly_sub(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(args_id)[0]) + ")"5442+
end53635443
return "(" + compile_expr(recv) + " - " + compile_arg0(nid) + ")"53645444
end53655445
if mname == "*"@@ -5368,8 +5448,9 @@
53685448
@needs_string_helpers = 153695449
return "sp_str_repeat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"53705450
end5371-
if lt == "float"5372-
return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"5451+
if lt == "poly"5452+
@needs_rb_value = 15453+
return "sp_poly_mul(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"53735454
end53745455
return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"53755456
end@@ -5391,6 +5472,11 @@
53915472
return "(" + compile_expr(recv) + " < " + compile_arg0(nid) + ")"53925473
end53935474
if mname == ">"5475+
lt = infer_type(recv)5476+
if lt == "poly"5477+
@needs_rb_value = 15478+
return "sp_poly_gt(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"5479+
end53945480
return "(" + compile_expr(recv) + " > " + compile_arg0(nid) + ")"53955481
end53965482
if mname == "<="@@ -5896,6 +5982,9 @@
58965982
if mname == "nil?"58975983
return "sp_poly_nil_p(" + rc + ")"58985984
end5985+
if mname == "to_s"5986+
return "sp_poly_to_s(" + rc + ")"5987+
end58995988
# For object method calls, dispatch based on cls_id59005989
# Generate: switch on v.tag and cls_id59015990
tmp = new_temp@@ -5966,6 +6055,9 @@
59666055
def box_expr_to_poly(nid)59676056
at = infer_type(nid)59686057
val = compile_expr(nid)6058+
if at == "poly"6059+
return val6060+
end59696061
if at == "int"59706062
return "sp_box_int(" + val + ")"59716063
end@@ -6059,19 +6151,22 @@
60596151
if kw_found == 060606152
if k < ptypes.length60616153
if ptypes[k] == "int_array"6062-
# This is a splat/rest param - collect remaining positional args into array6063-
@needs_int_array = 16064-
@needs_gc = 16065-
tmp = new_temp6066-
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")6067-
pi = 06068-
while pi < positional_ids.length6069-
emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")6070-
pi = pi + 16154+
# Only splat if there are more positional args than remaining params6155+
# (i.e., this is a rest/splat parameter, not a regular array param)6156+
if positional_ids.length > (pnames.length - k)6157+
@needs_int_array = 16158+
@needs_gc = 16159+
tmp = new_temp6160+
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")6161+
pi = 06162+
while pi < positional_ids.length6163+
emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")6164+
pi = pi + 16165+
end6166+
result = result + tmp6167+
k = k + 16168+
next60716169
end6072-
result = result + tmp6073-
k = k + 16074-
next60756170
end60766171
end60776172
if k < positional_ids.length