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
end end if lt == "int" return "((mrb_int)pow((double)" + compile_expr(recv) + ", (double)" + compile_arg0(nid) + "))" return "((mrb_int)pow((double)" + compile_expr(recv) + ", (double)" + compile_arg0_as_int(nid) + "))" end return "pow(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")" end emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + arg + ", " + itmp + "));") return tmp end return "(" + compile_expr(recv) + " + " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " + " + compile_arg0_as_int(nid) + ")" end if mname == "-" lt = infer_type(recv) if r != "" return r end return "(" + compile_expr(recv) + " - " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " - " + compile_arg0_as_int(nid) + ")" end if mname == "*" lt = infer_type(recv) 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)); } }") return tmp end return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " * " + compile_arg0_as_int(nid) + ")" end if mname == "/" lt = infer_type(recv) end end end return "sp_idiv(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")" return "sp_idiv(" + compile_expr(recv) + ", " + compile_arg0_as_int(nid) + ")" end if mname == "%" lt = infer_type(recv) return "sp_sprintf(" + fmt_c + ", (long long)" + compile_expr(arg0) + ")" end end return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")" return "sp_imod(" + compile_expr(recv) + ", " + compile_arg0_as_int(nid) + ")" end if mname == "<" lt = infer_type(recv) @needs_rb_value = 1 return "sp_poly_shl(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")" end return "(" + compile_expr(recv) + " << " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " << " + compile_arg0_as_int(nid) + ")" end if mname == ">>" if infer_type(recv) == "poly" @needs_rb_value = 1 return "sp_poly_shr(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")" end return "(" + compile_expr(recv) + " >> " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " >> " + compile_arg0_as_int(nid) + ")" end if mname == "&" lt = infer_type(recv) if r != "" return r end return "(" + compile_expr(recv) + " & " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " & " + compile_arg0_as_int(nid) + ")" end if mname == "|" lt = infer_type(recv) if r != "" return r end return "(" + compile_expr(recv) + " | " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " | " + compile_arg0_as_int(nid) + ")" end if mname == "^" if infer_type(recv) == "poly" @needs_rb_value = 1 return "sp_poly_bxor(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")" end return "(" + compile_expr(recv) + " ^ " + compile_arg0(nid) + ")" return "(" + compile_expr(recv) + " ^ " + compile_arg0_as_int(nid) + ")" end if mname == "~" return "(~" + compile_expr(recv) + ")" # (matches `to_i` semantics for poly). if rhs_t == "poly" && (vt == "int" || vt == "bool") val = "(" + val + ").v.i" rhs_t = vt end if rhs_t == "poly" && vt == "float" val = "(" + val + ").v.f" rhs_t = vt end emit(" " + vref + " = " + val + ";") endtest/auto_unbox_keeps_int_slot.rbaddedView file
Loading diff…
test/auto_unbox_keeps_int_slot.rb.expectedaddedView file
Loading diff…
test/poly_int_arith_auto_unify.rbaddedView file
Loading diff…
test/poly_int_arith_auto_unify.rb.expectedaddedView file
Loading diff…
test/poly_int_bitops_auto_unify.rbaddedView file
Loading diff…
test/poly_int_bitops_auto_unify.rb.expectedaddedView file
Loading diff…