Commit c8aa00eb

c8aa00eb07f1Merged

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

Description

fix(codegen): auto-unbox poly arg in arithmetic / bit ops with primitive recv

compile_operator_expr already routes through sp_poly_* helpers when the receiver is poly-typed, but the opposite shape — primitive receiver with a poly-typed argument — fell through to the raw C emit ((recv + arg), (recv << arg), etc.) and gcc rejected mrb_int + sp_RbVal. Replace compile_arg0 with compile_arg0_as_int at each fall-through arithmetic / bit operator (+, -, *, /, %, **, <<, >>, &, |, ^); the helper already unboxes a poly arg via .v.i, mirroring PR #347's LV-write to_i semantics.

Companion fix at the LV-write site: keep the slot's tracked type at vt after PR #347 unboxes a poly RHS into it. Without this, set_var_type(lname, rhs_t) widened the slot to poly and a subsequent poly-RHS write into the same slot saw the slot as poly, skipped the unbox, and emitted lv = <sp_RbVal>; against the still-mrb_int C decl.

Details

Commit
c8aa00eb07f1
Parent
01f36bc8ec7a

spinel_codegen.rbmodifiedView file

@@ -22696,7 +22696,7 @@
2269622696 end
2269722697 end
2269822698 if lt == "int"
22699- return "((mrb_int)pow((double)" + compile_expr(recv) + ", (double)" + compile_arg0(nid) + "))"
22699+ return "((mrb_int)pow((double)" + compile_expr(recv) + ", (double)" + compile_arg0_as_int(nid) + "))"
2270022700 end
2270122701 return "pow(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
2270222702 end
@@ -22767,7 +22767,7 @@
2276722767 emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + arg + ", " + itmp + "));")
2276822768 return tmp
2276922769 end
22770- return "(" + compile_expr(recv) + " + " + compile_arg0(nid) + ")"
22770+ return "(" + compile_expr(recv) + " + " + compile_arg0_as_int(nid) + ")"
2277122771 end
2277222772 if mname == "-"
2277322773 lt = infer_type(recv)
@@ -22796,7 +22796,7 @@
2279622796 if r != ""
2279722797 return r
2279822798 end
22799- return "(" + compile_expr(recv) + " - " + compile_arg0(nid) + ")"
22799+ return "(" + compile_expr(recv) + " - " + compile_arg0_as_int(nid) + ")"
2280022800 end
2280122801 if mname == "*"
2280222802 lt = infer_type(recv)
@@ -22823,7 +22823,7 @@
2282322823 emit(" { mrb_int _mi; mrb_int _sl = sp_" + pfx + "_length(" + src + "); for (_mi = 0; _mi < " + cnt + "; _mi++) { mrb_int _mj; for (_mj = 0; _mj < _sl; _mj++) sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + src + ", _mj)); } }")
2282422824 return tmp
2282522825 end
22826- return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"
22826+ return "(" + compile_expr(recv) + " * " + compile_arg0_as_int(nid) + ")"
2282722827 end
2282822828 if mname == "/"
2282922829 lt = infer_type(recv)
@@ -22845,7 +22845,7 @@
2284522845 end
2284622846 end
2284722847 end
22848- return "sp_idiv(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
22848+ return "sp_idiv(" + compile_expr(recv) + ", " + compile_arg0_as_int(nid) + ")"
2284922849 end
2285022850 if mname == "%"
2285122851 lt = infer_type(recv)
@@ -22925,7 +22925,7 @@
2292522925 return "sp_sprintf(" + fmt_c + ", (long long)" + compile_expr(arg0) + ")"
2292622926 end
2292722927 end
22928- return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
22928+ return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0_as_int(nid) + ")"
2292922929 end
2293022930 if mname == "<"
2293122931 lt = infer_type(recv)
@@ -23103,14 +23103,14 @@
2310323103 @needs_rb_value = 1
2310423104 return "sp_poly_shl(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
2310523105 end
23106- return "(" + compile_expr(recv) + " << " + compile_arg0(nid) + ")"
23106+ return "(" + compile_expr(recv) + " << " + compile_arg0_as_int(nid) + ")"
2310723107 end
2310823108 if mname == ">>"
2310923109 if infer_type(recv) == "poly"
2311023110 @needs_rb_value = 1
2311123111 return "sp_poly_shr(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
2311223112 end
23113- return "(" + compile_expr(recv) + " >> " + compile_arg0(nid) + ")"
23113+ return "(" + compile_expr(recv) + " >> " + compile_arg0_as_int(nid) + ")"
2311423114 end
2311523115 if mname == "&"
2311623116 lt = infer_type(recv)
@@ -23122,7 +23122,7 @@
2312223122 if r != ""
2312323123 return r
2312423124 end
23125- return "(" + compile_expr(recv) + " & " + compile_arg0(nid) + ")"
23125+ return "(" + compile_expr(recv) + " & " + compile_arg0_as_int(nid) + ")"
2312623126 end
2312723127 if mname == "|"
2312823128 lt = infer_type(recv)
@@ -23134,14 +23134,14 @@
2313423134 if r != ""
2313523135 return r
2313623136 end
23137- return "(" + compile_expr(recv) + " | " + compile_arg0(nid) + ")"
23137+ return "(" + compile_expr(recv) + " | " + compile_arg0_as_int(nid) + ")"
2313823138 end
2313923139 if mname == "^"
2314023140 if infer_type(recv) == "poly"
2314123141 @needs_rb_value = 1
2314223142 return "sp_poly_bxor(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
2314323143 end
23144- return "(" + compile_expr(recv) + " ^ " + compile_arg0(nid) + ")"
23144+ return "(" + compile_expr(recv) + " ^ " + compile_arg0_as_int(nid) + ")"
2314523145 end
2314623146 if mname == "~"
2314723147 return "(~" + compile_expr(recv) + ")"
@@ -29005,9 +29005,11 @@
2900529005 # (matches `to_i` semantics for poly).
2900629006 if rhs_t == "poly" && (vt == "int" || vt == "bool")
2900729007 val = "(" + val + ").v.i"
29008+ rhs_t = vt
2900829009 end
2900929010 if rhs_t == "poly" && vt == "float"
2901029011 val = "(" + val + ").v.f"
29012+ rhs_t = vt
2901129013 end
2901229014 emit(" " + vref + " = " + val + ";")
2901329015 end