Commit de98a258

de98a258cf3fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #335 from mame/pr/box-unbox-poly-boundaries

fix(codegen): consistent box/unbox at poly type boundaries

Details

Commit
de98a258cf3f
Parent
b2de8cb93610
Merge parent
62df9eb9c0ea

spinel_codegen.rbmodifiedView file

@@ -2034,6 +2034,19 @@
20342034 end
20352035 return "int"
20362036 end
2037+ if t == "LocalVariableWriteNode"
2038+ # `var = expr` used as an expression — the value of the
2039+ # expression is the assigned slot's value (after any boxing
2040+ # done by the LocalVariableWriteNode emit path). Reporting the
2041+ # slot type lets compile_cond_expr know to wrap with
2042+ # sp_poly_truthy when the slot is poly (e.g.
2043+ # `if (sprite = arr[i])` where sprite is a sp_RbVal local).
2044+ vt = find_var_type(@nd_name[nid])
2045+ if vt != ""
2046+ return vt
2047+ end
2048+ return infer_type(@nd_expression[nid])
2049+ end
20372050 if t == "GlobalVariableReadNode"
20382051 # `alias $copy $orig` -- a $copy read must look up $orig's
20392052 # registered type so the C codegen sees the correct format
@@ -19423,13 +19436,22 @@
1942319436 end
1942419437 if t == "LocalVariableWriteNode"
1942519438 # `local = expr` used as an expression (e.g. inside a chained
19426- # `@a = local = expr` write). Without this branch compile_expr
19427- # falls through and returns the default "0", which silently
19428- # zeroed the outer write — every `@_p_nz = ___a__ = __data__`
19429- # in optcarrots OptimizedCodeBuilder CPU output became
19430- # `iv__p_nz = 0`. Lower as a parenthesized C-style assignment.
19439+ # `@a = local = expr` write, or `if (sprite = arr[i])` in a
19440+ # conditional). Without this branch compile_expr falls through
19441+ # and returns the default "0", which silently zeroed the outer
19442+ # write — every `@_p_nz = ___a__ = __data__` in optcarrots
19443+ # OptimizedCodeBuilder CPU output became `iv__p_nz = 0`.
19444+ # Lower as a parenthesized C-style assignment, boxing scalar
19445+ # RHS into poly when the local slot is poly (mirrors compile_stmt
19446+ # InstanceVariableWriteNode logic).
1943119447 val = compile_expr(@nd_expression[nid])
19432- return "(" + fiber_var_ref(@nd_name[nid]) + " = " + val + ")"
19448+ lname2 = @nd_name[nid]
19449+ vt = find_var_type(lname2)
19450+ rhs_t = infer_type(@nd_expression[nid])
19451+ if vt == "poly" && rhs_t != "" && rhs_t != "poly"
19452+ val = box_value_to_poly(rhs_t, val)
19453+ end
19454+ return "(" + fiber_var_ref(lname2) + " = " + val + ")"
1943319455 end
1943419456 if t == "LocalVariableOperatorWriteNode"
1943519457 # `local OP= expr` used as an expression (e.g. ORA's
@@ -20028,6 +20050,27 @@
2002820050 "0"
2002920051 end
2003020052
20053+ # Like compile_arg0, but unboxes the result to mrb_int when the
20054+ # argument's static type is poly. Use at call sites that pass the
20055+ # arg directly to a C function expecting `mrb_int` (sp_IntArray_get,
20056+ # sp_IntArray_push integer-element variants, runtime helpers that
20057+ # take an int index, etc.). Without unboxing, gcc rejects passing
20058+ # `sp_RbVal` to a `mrb_int` parameter.
20059+ def compile_arg0_as_int(nid)
20060+ args_id = @nd_arguments[nid]
20061+ if args_id >= 0
20062+ arg_ids = get_args(args_id)
20063+ if arg_ids.length > 0
20064+ ce = compile_expr(arg_ids[0])
20065+ if infer_type(arg_ids[0]) == "poly"
20066+ return "(" + ce + ").v.i"
20067+ end
20068+ return ce
20069+ end
20070+ end
20071+ "0"
20072+ end
20073+
2003120074 # Like compile_arg0, but converts symbol-typed arg to const char *
2003220075 # (sp_sym_to_s wrap). Use for callsites that need a string key.
2003320076 def compile_str_arg0(nid)
@@ -23799,10 +23842,10 @@
2379923842 return "sp_IntArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2380023843 end
2380123844 end
23802- return "sp_IntArray_get(" + rc + ", " + compile_arg0(nid) + ")"
23845+ return "sp_IntArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2380323846 end
2380423847 if mname == "push"
23805- return "(sp_IntArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
23848+ return "(sp_IntArray_push(" + rc + ", " + compile_arg0_as_int(nid) + "), 0)"
2380623849 end
2380723850 if mname == "pop"
2380823851 return "sp_IntArray_pop(" + rc + ")"
@@ -24059,7 +24102,7 @@
2405924102 return "sp_FloatArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2406024103 end
2406124104 end
24062- return "sp_FloatArray_get(" + rc + ", " + compile_arg0(nid) + ")"
24105+ return "sp_FloatArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2406324106 end
2406424107 if mname == "push"
2406524108 return "(sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
@@ -24111,7 +24154,7 @@
2411124154 return "sp_PtrArray_length(" + rc + ")"
2411224155 end
2411324156 if mname == "[]"
24114- return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0(nid) + "))"
24157+ return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + "))"
2411524158 end
2411624159 if mname == "push"
2411724160 return "(sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
@@ -24178,7 +24221,7 @@
2417824221 return "sp_StrArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2417924222 end
2418024223 end
24181- return "sp_StrArray_get(" + rc + ", " + compile_arg0(nid) + ")"
24224+ return "sp_StrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2418224225 end
2418324226 if mname == "first"
2418424227 return "sp_StrArray_get(" + rc + ", 0)"
@@ -24254,7 +24297,7 @@
2425424297 return "sp_PolyArray_length(" + rc + ")"
2425524298 end
2425624299 if mname == "[]"
24257- return "sp_PolyArray_get(" + rc + ", " + compile_arg0(nid) + ")"
24300+ return "sp_PolyArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2425824301 end
2425924302 if mname == "push"
2426024303 arg_id = -1
@@ -27787,6 +27830,13 @@
2778727830 emit(" " + vref + " = NULL;")
2778827831 else
2778927832 val = compile_expr(@nd_expression[nid])
27833+ # Box scalar RHS into poly when the local slot is poly. Without
27834+ # this, `lv = sp_IntArray_get(...)` (int RHS) into a sp_RbVal
27835+ # slot fails C compile. Mirrors the InstanceVariableWriteNode
27836+ # path's box-when-slot-is-poly logic.
27837+ if vt == "poly" && rhs_t != "" && rhs_t != "poly"
27838+ val = box_value_to_poly(rhs_t, val)
27839+ end
2779027840 emit(" " + vref + " = " + val + ";")
2779127841 end
2779227842 if rhs_t != "nil" || is_nullable_type(vt) == 0
@@ -28544,11 +28594,15 @@
2854428594 while k < targets.length
2854528595 tid = targets[k]
2854628596 rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
28597+ # Route ivar / local writes through emit_multi_write_target so
28598+ # the box-when-slot-is-poly logic fires. Without boxing, an int
28599+ # RHS into a sp_RbVal slot fails C compile (`@x, @y = arr` where
28600+ # @y is poly because other writers store other types).
2854728601 if @nd_type[tid] == "LocalVariableTargetNode"
28548- emit(" " + fiber_var_ref(@nd_name[tid]) + " = " + rhs + ";")
28602+ emit_multi_write_target(tid, rhs, "int")
2854928603 end
2855028604 if @nd_type[tid] == "InstanceVariableTargetNode"
28551- emit(" " + self_arrow + sanitize_ivar(@nd_name[tid]) + " = " + rhs + ";")
28605+ emit_multi_write_target(tid, rhs, "int")
2855228606 end
2855328607 if @nd_type[tid] == "ConstantTargetNode"
2855428608 if find_const_idx(@nd_name[tid]) >= 0