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 end
785785 if t == "ConstantPathNode"
786786 if @nd_receiver[nid] >= 0
787- cpname = @nd_name[@nd_receiver[nid]] + "_" + @nd_name[nid]
787+ rname = @nd_name[@nd_receiver[nid]]
788+ nname = @nd_name[nid]
789+ cpname = rname + "_" + nname
788790 ci = find_const_idx(cpname)
789791 if ci >= 0
790792 return @const_types[ci]
791793 end
794+ if rname == "Float"
795+ return "float"
796+ end
792797 end
793798 return "int"
794799 end
@@ -899,6 +904,9 @@
899904 if lt == "string"
900905 return "string"
901906 end
907+ if lt == "poly"
908+ return "poly"
909+ end
902910 if lt == "float"
903911 return "float"
904912 end
@@ -923,6 +931,9 @@
923931 if lt == "string"
924932 return "string"
925933 end
934+ if lt == "poly"
935+ return "poly"
936+ end
926937 end
927938 return "int"
928939 end
@@ -3126,6 +3137,22 @@
31263137 if mname == "upcase"
31273138 @needs_string_helpers = 1
31283139 end
3140+ if mname == "<<"
3141+ if @nd_receiver[nid] >= 0
3142+ rt = infer_type(@nd_receiver[nid])
3143+ if rt == "string"
3144+ @needs_string_helpers = 1
3145+ end
3146+ end
3147+ end
3148+ if mname == "+"
3149+ if @nd_receiver[nid] >= 0
3150+ rt = infer_type(@nd_receiver[nid])
3151+ if rt == "string"
3152+ @needs_string_helpers = 1
3153+ end
3154+ end
3155+ end
31293156 if mname == "downcase"
31303157 @needs_string_helpers = 1
31313158 end
@@ -3422,7 +3449,14 @@
34223449 ptypes[k] = at
34233450 else
34243451 if at == "int"
3425- # arg is int, param already has a type - keep it
3452+ # Check if arg is a literal int (genuine int value)
3453+ if k < arg_ids.length
3454+ if @nd_type[arg_ids[k]] == "IntegerNode"
3455+ ptypes[k] = "poly"
3456+ @needs_rb_value = 1
3457+ end
3458+ end
3459+ # otherwise arg is int variable, param already has a type - keep it
34263460 else
34273461 # Genuinely different types - mark poly
34283462 ptypes[k] = "poly"
@@ -3593,6 +3627,22 @@
35933627 emit_string_helpers
35943628 end
35953629 if @needs_rb_value == 1
3630+ if @needs_string_helpers == 0
3631+ @needs_string_helpers = 1
3632+ if @needs_gc == 0
3633+ @needs_gc = 1
3634+ emit_gc_runtime
3635+ end
3636+ if @needs_int_array == 0
3637+ @needs_int_array = 1
3638+ emit_int_array_runtime
3639+ end
3640+ if @needs_str_array == 0
3641+ @needs_str_array = 1
3642+ emit_str_array_runtime
3643+ end
3644+ emit_string_helpers
3645+ end
35963646 emit_rb_value_runtime
35973647 end
35983648 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 end
37863840
@@ -5007,11 +5061,27 @@
50075061 end
50085062 if t == "ConstantPathNode"
50095063 if @nd_receiver[nid] >= 0
5010- cpname = @nd_name[@nd_receiver[nid]] + "_" + @nd_name[nid]
5064+ rname = @nd_name[@nd_receiver[nid]]
5065+ nname = @nd_name[nid]
5066+ cpname = rname + "_" + nname
50115067 ci = find_const_idx(cpname)
50125068 if ci >= 0
50135069 return "cst_" + cpname
50145070 end
5071+ # Built-in constants
5072+ if rname == "Float"
5073+ if nname == "INFINITY"
5074+ return "(1.0/0.0)"
5075+ end
5076+ if nname == "NAN"
5077+ return "(0.0/0.0)"
5078+ end
5079+ end
5080+ if rname == "Integer"
5081+ if nname == "MAX"
5082+ return "INT64_MAX"
5083+ end
5084+ end
50155085 return cpname
50165086 end
50175087 return @nd_name[nid]
@@ -5353,13 +5423,23 @@
53535423 @needs_string_helpers = 1
53545424 return "sp_str_concat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
53555425 end
5426+ if lt == "poly"
5427+ @needs_rb_value = 1
5428+ @needs_string_helpers = 1
5429+ return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"
5430+ end
53565431 return "(" + compile_expr(recv) + " + " + compile_arg0(nid) + ")"
53575432 end
53585433 if mname == "-"
5434+ lt = infer_type(recv)
53595435 args_id = @nd_arguments[nid]
53605436 if args_id < 0
53615437 return "(-" + compile_expr(recv) + ")"
53625438 end
5439+ if lt == "poly"
5440+ @needs_rb_value = 1
5441+ return "sp_poly_sub(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(args_id)[0]) + ")"
5442+ end
53635443 return "(" + compile_expr(recv) + " - " + compile_arg0(nid) + ")"
53645444 end
53655445 if mname == "*"
@@ -5368,8 +5448,9 @@
53685448 @needs_string_helpers = 1
53695449 return "sp_str_repeat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
53705450 end
5371- if lt == "float"
5372- return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"
5451+ if lt == "poly"
5452+ @needs_rb_value = 1
5453+ return "sp_poly_mul(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
53735454 end
53745455 return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"
53755456 end
@@ -5391,6 +5472,11 @@
53915472 return "(" + compile_expr(recv) + " < " + compile_arg0(nid) + ")"
53925473 end
53935474 if mname == ">"
5475+ lt = infer_type(recv)
5476+ if lt == "poly"
5477+ @needs_rb_value = 1
5478+ return "sp_poly_gt(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
5479+ end
53945480 return "(" + compile_expr(recv) + " > " + compile_arg0(nid) + ")"
53955481 end
53965482 if mname == "<="
@@ -5896,6 +5982,9 @@
58965982 if mname == "nil?"
58975983 return "sp_poly_nil_p(" + rc + ")"
58985984 end
5985+ if mname == "to_s"
5986+ return "sp_poly_to_s(" + rc + ")"
5987+ end
58995988 # For object method calls, dispatch based on cls_id
59005989 # Generate: switch on v.tag and cls_id
59015990 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 val
6060+ end
59696061 if at == "int"
59706062 return "sp_box_int(" + val + ")"
59716063 end
@@ -6059,19 +6151,22 @@
60596151 if kw_found == 0
60606152 if k < ptypes.length
60616153 if ptypes[k] == "int_array"
6062- # This is a splat/rest param - collect remaining positional args into array
6063- @needs_int_array = 1
6064- @needs_gc = 1
6065- tmp = new_temp
6066- emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
6067- pi = 0
6068- while pi < positional_ids.length
6069- emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")
6070- pi = pi + 1
6154+ # Only splat if there are more positional args than remaining params
6155+ # (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 = 1
6158+ @needs_gc = 1
6159+ tmp = new_temp
6160+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
6161+ pi = 0
6162+ while pi < positional_ids.length
6163+ emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")
6164+ pi = pi + 1
6165+ end
6166+ result = result + tmp
6167+ k = k + 1
6168+ next
60716169 end
6072- result = result + tmp
6073- k = k + 1
6074- next
60756170 end
60766171 end
60776172 if k < positional_ids.length