Commit 8472d3b9

8472d3b9acd7Merged

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

Description

Merge pull request #346 from mame/pr/poly-recv-each

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

Details

Commit
8472d3b9acd7
Parent
9b611afa73a1
Merge parent
3619b9deff58

spinel_codegen.rbmodifiedView file

@@ -1533,6 +1533,122 @@
15331533 [@nd_name[expr], nt]
15341534 end
15351535
1536+ # Try to evaluate a predicate expression at compile time. Returns
1537+ # "TRUE" / "FALSE" when the result is known statically; "" when it
1538+ # depends on runtime state. Currently handles `<typed>.is_a?(Klass)` /
1539+ # `kind_of?(Klass)` / `instance_of?(Klass)` where the receiver's
1540+ # static type clearly does or does not match the queried class —
1541+ # this lets compile_if_expr / infer_type(IfNode) skip the dead arm
1542+ # so the typed-friendly arm doesn't get widened to poly via unify.
1543+ def static_predicate_value(pred_id)
1544+ if pred_id < 0
1545+ return ""
1546+ end
1547+ pt = @nd_type[pred_id]
1548+ if pt == "ParenthesesNode"
1549+ pb = @nd_body[pred_id]
1550+ if pb >= 0
1551+ ps = get_stmts(pb)
1552+ if ps.length == 1
1553+ return static_predicate_value(ps[0])
1554+ end
1555+ end
1556+ return ""
1557+ end
1558+ if pt != "CallNode"
1559+ return ""
1560+ end
1561+ pname = @nd_name[pred_id]
1562+ if pname != "is_a?" && pname != "kind_of?" && pname != "instance_of?"
1563+ return ""
1564+ end
1565+ expr = @nd_receiver[pred_id]
1566+ if expr < 0
1567+ return ""
1568+ end
1569+ args = @nd_arguments[pred_id]
1570+ if args < 0
1571+ return ""
1572+ end
1573+ arg_ids = get_args(args)
1574+ if arg_ids.length < 1
1575+ return ""
1576+ end
1577+ arg0 = arg_ids[0]
1578+ klass = ""
1579+ if @nd_type[arg0] == "ConstantReadNode"
1580+ klass = @nd_name[arg0]
1581+ elsif @nd_type[arg0] == "ConstantPathNode"
1582+ klass = resolve_const_ref_name(arg0)
1583+ end
1584+ if klass == ""
1585+ return ""
1586+ end
1587+ rt = infer_type(expr)
1588+ # Skip when the recv is itself poly — cls_id varies at runtime.
1589+ if rt == "poly"
1590+ return ""
1591+ end
1592+ # User-class instance: walk the parent chain.
1593+ if is_obj_type(rt) == 1
1594+ cname = rt[4, rt.length - 4]
1595+ if is_class_or_ancestor(cname, klass) == 1
1596+ return "TRUE"
1597+ end
1598+ return "FALSE"
1599+ end
1600+ # Built-in primitive type → answer directly.
1601+ if rt == "int"
1602+ if klass == "Integer" || klass == "Numeric" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1603+ return "TRUE"
1604+ end
1605+ return "FALSE"
1606+ end
1607+ if rt == "string"
1608+ if klass == "String" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1609+ return "TRUE"
1610+ end
1611+ return "FALSE"
1612+ end
1613+ if rt == "float"
1614+ if klass == "Float" || klass == "Numeric" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1615+ return "TRUE"
1616+ end
1617+ return "FALSE"
1618+ end
1619+ if rt == "symbol"
1620+ if klass == "Symbol" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1621+ return "TRUE"
1622+ end
1623+ return "FALSE"
1624+ end
1625+ if rt == "bool"
1626+ if klass == "TrueClass" || klass == "FalseClass" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1627+ return "TRUE"
1628+ end
1629+ return "FALSE"
1630+ end
1631+ if rt == "nil"
1632+ if klass == "NilClass" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"
1633+ return "TRUE"
1634+ end
1635+ return "FALSE"
1636+ end
1637+ if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" || rt == "ptr_array" || rt == "poly_array"
1638+ if klass == "Array" || klass == "Object" || klass == "Kernel" || klass == "BasicObject" || klass == "Enumerable"
1639+ return "TRUE"
1640+ end
1641+ return "FALSE"
1642+ end
1643+ if rt == "range"
1644+ if klass == "Range" || klass == "Object" || klass == "Kernel" || klass == "BasicObject" || klass == "Enumerable"
1645+ return "TRUE"
1646+ end
1647+ return "FALSE"
1648+ end
1649+ ""
1650+ end
1651+
15361652 def set_var_type(name, vtype)
15371653 i = @scope_names.length - 1
15381654 while i >= 0
@@ -27726,15 +27842,19 @@
2772627842
2772727843 def compile_if_expr(nid)
2772827844 cond = compile_cond_expr(@nd_predicate[nid])
27845+ unified_t = infer_type(nid)
2772927846 then_val = "0"
27847+ then_t = "nil"
2773027848 body = @nd_body[nid]
2773127849 if body >= 0
2773227850 stmts = get_stmts(body)
2773327851 if stmts.length > 0
27852+ then_t = infer_type(stmts.last)
2773427853 then_val = compile_expr(stmts.last)
2773527854 end
2773627855 end
2773727856 else_val = "0"
27857+ else_t = "nil"
2773827858 sub = @nd_subsequent[nid]
2773927859 if sub >= 0
2774027860 if @nd_type[sub] == "ElseNode"
@@ -27742,13 +27862,31 @@
2774227862 if eb >= 0
2774327863 es = get_stmts(eb)
2774427864 if es.length > 0
27865+ else_t = infer_type(es.last)
2774527866 else_val = compile_expr(es.last)
2774627867 end
2774727868 end
2774827869 else
27870+ else_t = infer_type(sub)
2774927871 else_val = compile_if_expr(sub)
2775027872 end
2775127873 end
27874+ # When the if-expression's unified type is `poly` but the
27875+ # branches have concrete types, the raw C ternary
27876+ # `(cond ? sp_IntArray * : sp_PtrArray *)` mixes pointer types
27877+ # and the C compiler rejects the assignment to sp_RbVal.
27878+ # Box each branch into sp_RbVal so the ternary's two arms
27879+ # share a type. Keeps the static infer_type(IfNode) → poly
27880+ # contract that downstream dispatch (e.g. .each on poly recv)
27881+ # relies on.
27882+ if unified_t == "poly"
27883+ if then_t != "poly"
27884+ then_val = box_value_to_poly(then_t, then_val)
27885+ end
27886+ if else_t != "poly"
27887+ else_val = box_value_to_poly(else_t, else_val)
27888+ end
27889+ end
2775227890 "(" + cond + " ? " + then_val + " : " + else_val + ")"
2775327891 end
2775427892
@@ -32315,6 +32453,14 @@
3231532453 idx = compile_expr_as_string(arg_ids[0])
3231632454 else
3231732455 idx = compile_expr(arg_ids[0])
32456+ # Auto-unbox poly index for typed-array recv. The block-param
32457+ # iteration over a poly recv (compile_each_block's poly arm)
32458+ # delivers `lv_<bp>` as sp_RbVal, but a downstream
32459+ # `@arr[bp] = v` against a typed array expects an mrb_int.
32460+ idx_t = infer_type(arg_ids[0])
32461+ 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")
32462+ idx = "(" + idx + ").v.i"
32463+ end
3231832464 end
3231932465 end
3232032466 if arg_ids.length >= 2
@@ -33650,6 +33796,53 @@
3365033796 @indent = @indent - 1
3365133797 emit(" }")
3365233798 end
33799+ if rt == "poly"
33800+ # Poly receiver — dispatch on cls_id at runtime to pick the right
33801+ # iteration shape. Common shape: a `<typed>.is_a?(C) ? wrap : <typed>`
33802+ # ternary unifies to poly even when the live arm is a known
33803+ # concrete array. Without this branch, compile_each_block falls
33804+ # off the end and the iteration body is dropped silently.
33805+ # Block param is delivered as poly (sp_RbVal) regardless of the
33806+ # underlying array's element type, since poly is the widest fit.
33807+ poly_tmp = new_temp
33808+ idx_tmp = new_temp
33809+ val_tmp = new_temp
33810+ emit(" sp_RbVal " + poly_tmp + " = " + rc + ";")
33811+ emit(" if (" + poly_tmp + ".tag == SP_TAG_OBJ) {")
33812+ emit(" mrb_int " + idx_tmp + "_len = 0;")
33813+ 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;")
33814+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + idx_tmp + "_len = sp_IntArray_length((sp_IntArray *)" + poly_tmp + ".v.p);")
33815+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_FLT_ARRAY) " + idx_tmp + "_len = sp_FloatArray_length((sp_FloatArray *)" + poly_tmp + ".v.p);")
33816+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_STR_ARRAY) " + idx_tmp + "_len = sp_StrArray_length((sp_StrArray *)" + poly_tmp + ".v.p);")
33817+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_SYM_ARRAY) " + idx_tmp + "_len = sp_IntArray_length((sp_IntArray *)" + poly_tmp + ".v.p);")
33818+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_PTR_ARRAY) " + idx_tmp + "_len = sp_PtrArray_length((sp_PtrArray *)" + poly_tmp + ".v.p);")
33819+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + idx_tmp + "_len = sp_PolyArray_length((sp_PolyArray *)" + poly_tmp + ".v.p);")
33820+ emit(" for (mrb_int " + idx_tmp + " = 0; " + idx_tmp + " < " + idx_tmp + "_len; " + idx_tmp + "++) {")
33821+ emit(" sp_RbVal " + val_tmp + " = sp_box_nil();")
33822+ emit(" if (" + poly_tmp + ".cls_id == SP_BUILTIN_RANGE) " + val_tmp + " = sp_box_int(((sp_Range *)" + poly_tmp + ".v.p)->first + " + idx_tmp + ");")
33823+ 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 + "));")
33824+ 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 + "));")
33825+ 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 + "));")
33826+ 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 + "));")
33827+ 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);")
33828+ emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + val_tmp + " = sp_PolyArray_get((sp_PolyArray *)" + poly_tmp + ".v.p, " + idx_tmp + ");")
33829+ if has_bp == 1
33830+ emit(" sp_RbVal lv_" + bp1 + " = " + val_tmp + ";")
33831+ end
33832+ @indent = @indent + 2
33833+ push_scope
33834+ if has_bp == 1
33835+ declare_var(bp1, "poly")
33836+ end
33837+ redo_label = push_redo_label
33838+ emit_redo_label(redo_label)
33839+ compile_stmts_body(@nd_body[@nd_block[nid]])
33840+ pop_redo_label
33841+ pop_scope
33842+ @indent = @indent - 2
33843+ emit(" }")
33844+ emit(" }")
33845+ end
3365333846 @in_loop = old
3365433847 end
3365533848