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
end15351535
1536+
# Try to evaluate a predicate expression at compile time. Returns1537+
# "TRUE" / "FALSE" when the result is known statically; "" when it1538+
# depends on runtime state. Currently handles `<typed>.is_a?(Klass)` /1539+
# `kind_of?(Klass)` / `instance_of?(Klass)` where the receiver's1540+
# static type clearly does or does not match the queried class —1541+
# this lets compile_if_expr / infer_type(IfNode) skip the dead arm1542+
# so the typed-friendly arm doesn't get widened to poly via unify.1543+
def static_predicate_value(pred_id)1544+
if pred_id < 01545+
return ""1546+
end1547+
pt = @nd_type[pred_id]1548+
if pt == "ParenthesesNode"1549+
pb = @nd_body[pred_id]1550+
if pb >= 01551+
ps = get_stmts(pb)1552+
if ps.length == 11553+
return static_predicate_value(ps[0])1554+
end1555+
end1556+
return ""1557+
end1558+
if pt != "CallNode"1559+
return ""1560+
end1561+
pname = @nd_name[pred_id]1562+
if pname != "is_a?" && pname != "kind_of?" && pname != "instance_of?"1563+
return ""1564+
end1565+
expr = @nd_receiver[pred_id]1566+
if expr < 01567+
return ""1568+
end1569+
args = @nd_arguments[pred_id]1570+
if args < 01571+
return ""1572+
end1573+
arg_ids = get_args(args)1574+
if arg_ids.length < 11575+
return ""1576+
end1577+
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+
end1584+
if klass == ""1585+
return ""1586+
end1587+
rt = infer_type(expr)1588+
# Skip when the recv is itself poly — cls_id varies at runtime.1589+
if rt == "poly"1590+
return ""1591+
end1592+
# User-class instance: walk the parent chain.1593+
if is_obj_type(rt) == 11594+
cname = rt[4, rt.length - 4]1595+
if is_class_or_ancestor(cname, klass) == 11596+
return "TRUE"1597+
end1598+
return "FALSE"1599+
end1600+
# 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+
end1605+
return "FALSE"1606+
end1607+
if rt == "string"1608+
if klass == "String" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"1609+
return "TRUE"1610+
end1611+
return "FALSE"1612+
end1613+
if rt == "float"1614+
if klass == "Float" || klass == "Numeric" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"1615+
return "TRUE"1616+
end1617+
return "FALSE"1618+
end1619+
if rt == "symbol"1620+
if klass == "Symbol" || klass == "Comparable" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"1621+
return "TRUE"1622+
end1623+
return "FALSE"1624+
end1625+
if rt == "bool"1626+
if klass == "TrueClass" || klass == "FalseClass" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"1627+
return "TRUE"1628+
end1629+
return "FALSE"1630+
end1631+
if rt == "nil"1632+
if klass == "NilClass" || klass == "Object" || klass == "Kernel" || klass == "BasicObject"1633+
return "TRUE"1634+
end1635+
return "FALSE"1636+
end1637+
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+
end1641+
return "FALSE"1642+
end1643+
if rt == "range"1644+
if klass == "Range" || klass == "Object" || klass == "Kernel" || klass == "BasicObject" || klass == "Enumerable"1645+
return "TRUE"1646+
end1647+
return "FALSE"1648+
end1649+
""1650+
end1651+
15361652
def set_var_type(name, vtype)15371653
i = @scope_names.length - 115381654
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 >= 02773227850
stmts = get_stmts(body)2773327851
if stmts.length > 027852+
then_t = infer_type(stmts.last)2773427853
then_val = compile_expr(stmts.last)2773527854
end2773627855
end2773727856
else_val = "0"27857+
else_t = "nil"2773827858
sub = @nd_subsequent[nid]2773927859
if sub >= 02774027860
if @nd_type[sub] == "ElseNode"@@ -27742,13 +27862,31 @@
2774227862
if eb >= 02774327863
es = get_stmts(eb)2774427864
if es.length > 027865+
else_t = infer_type(es.last)2774527866
else_val = compile_expr(es.last)2774627867
end2774727868
end2774827869
else27870+
else_t = infer_type(sub)2774927871
else_val = compile_if_expr(sub)2775027872
end2775127873
end27874+
# When the if-expression's unified type is `poly` but the27875+
# branches have concrete types, the raw C ternary27876+
# `(cond ? sp_IntArray * : sp_PtrArray *)` mixes pointer types27877+
# and the C compiler rejects the assignment to sp_RbVal.27878+
# Box each branch into sp_RbVal so the ternary's two arms27879+
# share a type. Keeps the static infer_type(IfNode) → poly27880+
# 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+
end27886+
if else_t != "poly"27887+
else_val = box_value_to_poly(else_t, else_val)27888+
end27889+
end2775227890
"(" + cond + " ? " + then_val + " : " + else_val + ")"2775327891
end2775427892
@@ -32315,6 +32453,14 @@
3231532453
idx = compile_expr_as_string(arg_ids[0])3231632454
else3231732455
idx = compile_expr(arg_ids[0])32456+
# Auto-unbox poly index for typed-array recv. The block-param32457+
# iteration over a poly recv (compile_each_block's poly arm)32458+
# delivers `lv_<bp>` as sp_RbVal, but a downstream32459+
# `@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+
end3231832464
end3231932465
end3232032466
if arg_ids.length >= 2@@ -33650,6 +33796,53 @@
3365033796
@indent = @indent - 13365133797
emit(" }")3365233798
end33799+
if rt == "poly"33800+
# Poly receiver — dispatch on cls_id at runtime to pick the right33801+
# iteration shape. Common shape: a `<typed>.is_a?(C) ? wrap : <typed>`33802+
# ternary unifies to poly even when the live arm is a known33803+
# concrete array. Without this branch, compile_each_block falls33804+
# off the end and the iteration body is dropped silently.33805+
# Block param is delivered as poly (sp_RbVal) regardless of the33806+
# underlying array's element type, since poly is the widest fit.33807+
poly_tmp = new_temp33808+
idx_tmp = new_temp33809+
val_tmp = new_temp33810+
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 == 133830+
emit(" sp_RbVal lv_" + bp1 + " = " + val_tmp + ";")33831+
end33832+
@indent = @indent + 233833+
push_scope33834+
if has_bp == 133835+
declare_var(bp1, "poly")33836+
end33837+
redo_label = push_redo_label33838+
emit_redo_label(redo_label)33839+
compile_stmts_body(@nd_body[@nd_block[nid]])33840+
pop_redo_label33841+
pop_scope33842+
@indent = @indent - 233843+
emit(" }")33844+
emit(" }")33845+
end3365333846
@in_loop = old3365433847
end3365533848
test/poly_recv_each.rbaddedView file
Loading diff…
test/poly_recv_each.rb.expectedaddedView file
Loading diff…