Commit 3619b9de

3619b9deff58Merged

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

Description

fix(codegen): poly receiver .each + auto-unbox poly index for typed array

Three coordinated changes:

  1. compile_each_block: dispatch on cls_id at runtime when the receiver type is poly. Previously fell off the end without emitting any iteration, so (addr.is_a?(Integer) ? [addr] : addr).each do |a| ... end (a common dispatch idiom) silently dropped the body. Block param is delivered as sp_RbVal. Handles SP_BUILTIN_RANGE / *_ARRAY / POLY_ARRAY cls_ids.

  2. compile_call_expr ([]= writer): auto-unbox a poly index when the recv is a typed array. The new poly-recv .each delivers lv_<bp> as sp_RbVal; downstream @arr[bp] = v against a typed array (poly_array / int_array / ...) needs an mrb_int. Without this, sp_PolyArray_set(arr, lv_a, ...) flunks the C compile (incompatible type for argument 2).

  3. compile_if_expr: when the unified type is poly but a branch has a concrete (non-poly) type, box the branch value to sp_RbVal. Otherwise the (cond ? <sp_PtrArray *> : <sp_IntArray *>) raw ternary mixes pointer types and the C compiler rejects.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
3619b9deff58
Parent
a0e14abe8ff5

spinel_codegen.rbmodifiedView file

@@ -1527,6 +1527,122 @@
15271527 [@nd_name[expr], nt]
15281528 end
15291529
1530+ # Try to evaluate a predicate expression at compile time. Returns
1531+ # "TRUE" / "FALSE" when the result is known statically; "" when it
1532+ # depends on runtime state. Currently handles `<typed>.is_a?(Klass)` /
1533+ # `kind_of?(Klass)` / `instance_of?(Klass)` where the receiver's
1534+ # static type clearly does or does not match the queried class —
1535+ # this lets compile_if_expr / infer_type(IfNode) skip the dead arm
1536+ # so the typed-friendly arm doesn't get widened to poly via unify.
1537+ def static_predicate_value(pred_id)
1538+ if pred_id < 0
1539+ return ""
1540+ end
1541+ pt = @nd_type[pred_id]
1542+ if pt == "ParenthesesNode"
1543+ pb = @nd_body[pred_id]
1544+ if pb >= 0
1545+ ps = get_stmts(pb)
1546+ if ps.length == 1
1547+ return static_predicate_value(ps[0])
1548+ end
1549+ end
1550+ return ""
1551+ end
1552+ if pt != "CallNode"
1553+ return ""
1554+ end
1555+ pname = @nd_name[pred_id]
1556+ if pname != "is_a?" && pname != "kind_of?" && pname != "instance_of?"
1557+ return ""
1558+ end
1559+ expr = @nd_receiver[pred_id]
1560+ if expr < 0
1561+ return ""
1562+ end
1563+ args = @nd_arguments[pred_id]
1564+ if args < 0
1565+ return ""
1566+ end
1567+ arg_ids = get_args(args)
1568+ if arg_ids.length < 1
1569+ return ""
1570+ end
1571+ arg0 = arg_ids[0]
1572+ klass = ""
1573+ if @nd_type[arg0] == "ConstantReadNode"
1574+ klass = @nd_name[arg0]
1575+ elsif @nd_type[arg0] == "ConstantPathNode"
1576+ klass = resolve_const_ref_name(arg0)
1577+ end
1578+ if klass == ""
1579+ return ""
1580+ end
1581+ rt = infer_type(expr)
1582+ # Skip when the recv is itself poly — cls_id varies at runtime.
1583+ if rt == "poly"
1584+ return ""
1585+ end
1586+ # User-class instance: walk the parent chain.
1587+ if is_obj_type(rt) == 1
1588+ cname = rt[4, rt.length - 4]
1589+ if is_class_or_ancestor(cname, klass) == 1
1590+ return "TRUE"
1591+ end
1592+ return "FALSE"
1593+ end
1594+ # Built-in primitive type → answer directly.
1595+ if rt == "int"
1596+ if klass == "Integer" || klass == "Numeric" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1597+ return "TRUE"
1598+ end
1599+ return "FALSE"
1600+ end
1601+ if rt == "string"
1602+ if klass == "String" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1603+ return "TRUE"
1604+ end
1605+ return "FALSE"
1606+ end
1607+ if rt == "float"
1608+ if klass == "Float" || klass == "Numeric" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1609+ return "TRUE"
1610+ end
1611+ return "FALSE"
1612+ end
1613+ if rt == "symbol"
1614+ if klass == "Symbol" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1615+ return "TRUE"
1616+ end
1617+ return "FALSE"
1618+ end
1619+ if rt == "bool"
1620+ if klass == "TrueClass" || klass == "FalseClass" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1621+ return "TRUE"
1622+ end
1623+ return "FALSE"
1624+ end
1625+ if rt == "nil"
1626+ if klass == "NilClass" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1627+ return "TRUE"
1628+ end
1629+ return "FALSE"
1630+ end
1631+ if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" || rt == "ptr_array" || rt == "poly_array"
1632+ if klass == "Array" || klass == "Object" || klass == "Kernel" || klass == "BasicObject" || klass == "Enumerable"
1633+ return "TRUE"
1634+ end
1635+ return "FALSE"
1636+ end
1637+ if rt == "range"
1638+ if klass == "Range" || klass == "Object" || klass == "Kernel" || klass == "BasicObject" || klass == "Enumerable"
1639+ return "TRUE"
1640+ end
1641+ return "FALSE"
1642+ end
1643+ ""
1644+ end
1645+
15301646 def set_var_type(name, vtype)
15311647 i = @scope_names.length - 1
15321648 while i >= 0
@@ -27440,15 +27556,19 @@
2744027556
2744127557 def compile_if_expr(nid)
2744227558 cond = compile_cond_expr(@nd_predicate[nid])
27559+ unified_t = infer_type(nid)
2744327560 then_val = "0"
27561+ then_t = "nil"
2744427562 body = @nd_body[nid]
2744527563 if body >= 0
2744627564 stmts = get_stmts(body)
2744727565 if stmts.length > 0
27566+ then_t = infer_type(stmts.last)
2744827567 then_val = compile_expr(stmts.last)
2744927568 end
2745027569 end
2745127570 else_val = "0"
27571+ else_t = "nil"
2745227572 sub = @nd_subsequent[nid]
2745327573 if sub >= 0
2745427574 if @nd_type[sub] == "ElseNode"
@@ -27456,13 +27576,31 @@
2745627576 if eb >= 0
2745727577 es = get_stmts(eb)
2745827578 if es.length > 0
27579+ else_t = infer_type(es.last)
2745927580 else_val = compile_expr(es.last)
2746027581 end
2746127582 end
2746227583 else
27584+ else_t = infer_type(sub)
2746327585 else_val = compile_if_expr(sub)
2746427586 end
2746527587 end
27588+ # When the if-expression's unified type is `poly` but the
27589+ # branches have concrete types, the raw C ternary
27590+ # `(cond ? sp_IntArray * : sp_PtrArray *)` mixes pointer types
27591+ # and the C compiler rejects the assignment to sp_RbVal.
27592+ # Box each branch into sp_RbVal so the ternary's two arms
27593+ # share a type. Keeps the static infer_type(IfNode) → poly
27594+ # contract that downstream dispatch (e.g. .each on poly recv)
27595+ # relies on.
27596+ if unified_t == "poly"
27597+ if then_t != "poly"
27598+ then_val = box_value_to_poly(then_t, then_val)
27599+ end
27600+ if else_t != "poly"
27601+ else_val = box_value_to_poly(else_t, else_val)
27602+ end
27603+ end
2746627604 "(" + cond + " ? " + then_val + " : " + else_val + ")"
2746727605 end
2746827606
@@ -31997,6 +32135,14 @@
3199732135 idx = compile_expr_as_string(arg_ids[0])
3199832136 else
3199932137 idx = compile_expr(arg_ids[0])
32138+ # Auto-unbox poly index for typed-array recv. The block-param
32139+ # iteration over a poly recv (compile_each_block's poly arm)
32140+ # delivers `lv_<bp>` as sp_RbVal, but a downstream
32141+ # `@arr[bp] = v` against a typed array expects an mrb_int.
32142+ idx_t = infer_type(arg_ids[0])
32143+ if idx_t == "poly" && (rt == "int_array" || rt == "float_array" || rt == "str_array" || rt == "sym_array" || is_ptr_array_type(rt) == 1 || rt == "poly_array")
32144+ idx = "(" + idx + ").v.i"
32145+ end
3200032146 end
3200132147 end
3200232148 if arg_ids.length >= 2
@@ -33290,6 +33436,53 @@
3329033436 @indent = @indent - 1
3329133437 emit(" }")
3329233438 end
33439+ if rt == "poly"
33440+ # Poly receiver — dispatch on cls_id at runtime to pick the right
33441+ # iteration shape. Common shape: a `<typed>.is_a?(C) ? wrap : <typed>`
33442+ # ternary unifies to poly even when the live arm is a known
33443+ # concrete array. Without this branch, compile_each_block falls
33444+ # off the end and the iteration body is dropped silently.
33445+ # Block param is delivered as poly (sp_RbVal) regardless of the
33446+ # underlying array's element type, since poly is the widest fit.
33447+ poly_tmp = new_temp
33448+ idx_tmp = new_temp
33449+ val_tmp = new_temp
33450+ emit(" sp_RbVal " + poly_tmp + " = " + rc + ";")
33451+ emit(" if (" + poly_tmp + ".tag == SP_TAG_OBJ) {")
33452+ emit(" mrb_int " + idx_tmp + "_len = 0;")
33453+ emit(" if (" + poly_tmp + ".cls_id == SP_BUILTIN_RANGE) " + idx_tmp + "_len = ((sp_Range *)" + poly_tmp + ".v.p)->last - ((sp_Range *)" + poly_tmp + ".v.p)->first + 1;")
33454+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + idx_tmp + "_len = sp_IntArray_length((sp_IntArray *)" + poly_tmp + ".v.p);")
33455+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_FLT_ARRAY) " + idx_tmp + "_len = sp_FloatArray_length((sp_FloatArray *)" + poly_tmp + ".v.p);")
33456+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_STR_ARRAY) " + idx_tmp + "_len = sp_StrArray_length((sp_StrArray *)" + poly_tmp + ".v.p);")
33457+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_SYM_ARRAY) " + idx_tmp + "_len = sp_IntArray_length((sp_IntArray *)" + poly_tmp + ".v.p);")
33458+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_PTR_ARRAY) " + idx_tmp + "_len = sp_PtrArray_length((sp_PtrArray *)" + poly_tmp + ".v.p);")
33459+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + idx_tmp + "_len = sp_PolyArray_length((sp_PolyArray *)" + poly_tmp + ".v.p);")
33460+ emit(" for (mrb_int " + idx_tmp + " = 0; " + idx_tmp + " < " + idx_tmp + "_len; " + idx_tmp + "++) {")
33461+ emit(" sp_RbVal " + val_tmp + " = sp_box_nil();")
33462+ emit(" if (" + poly_tmp + ".cls_id == SP_BUILTIN_RANGE) " + val_tmp + " = sp_box_int(((sp_Range *)" + poly_tmp + ".v.p)->first + " + idx_tmp + ");")
33463+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + val_tmp + " = sp_box_int(sp_IntArray_get((sp_IntArray *)" + poly_tmp + ".v.p, " + idx_tmp + "));")
33464+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_FLT_ARRAY) " + val_tmp + " = sp_box_float(sp_FloatArray_get((sp_FloatArray *)" + poly_tmp + ".v.p, " + idx_tmp + "));")
33465+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_STR_ARRAY) " + val_tmp + " = sp_box_str(sp_StrArray_get((sp_StrArray *)" + poly_tmp + ".v.p, " + idx_tmp + "));")
33466+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_SYM_ARRAY) " + val_tmp + " = sp_box_sym((sp_sym)sp_IntArray_get((sp_IntArray *)" + poly_tmp + ".v.p, " + idx_tmp + "));")
33467+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_PTR_ARRAY) " + val_tmp + " = sp_box_obj(sp_PtrArray_get((sp_PtrArray *)" + poly_tmp + ".v.p, " + idx_tmp + "), 0);")
33468+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + val_tmp + " = sp_PolyArray_get((sp_PolyArray *)" + poly_tmp + ".v.p, " + idx_tmp + ");")
33469+ if has_bp == 1
33470+ emit(" sp_RbVal lv_" + bp1 + " = " + val_tmp + ";")
33471+ end
33472+ @indent = @indent + 2
33473+ push_scope
33474+ if has_bp == 1
33475+ declare_var(bp1, "poly")
33476+ end
33477+ redo_label = push_redo_label
33478+ emit_redo_label(redo_label)
33479+ compile_stmts_body(@nd_body[@nd_block[nid]])
33480+ pop_redo_label
33481+ pop_scope
33482+ @indent = @indent - 2
33483+ emit(" }")
33484+ emit(" }")
33485+ end
3329333486 @in_loop = old
3329433487 end
3329533488