Commit b258716b

b258716b233aMerged

Dmitry Bochkarev <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat: propagate block param types from call sites to proc bodies

Add infer_proc_blk_param_types pass in analyzer that walks block.call(x) patterns on &block parameters and records arg types. Serialize via IR BP/CBP records. Codegen uses types to declare proc params with real types instead of hardcoded 'int', enabling method dispatch on proc params.

Fix: handle StatementsNode input in get_body_stmts, fall back to write-site RHS type when scope unavailable in collect_blk_call_arg_types, emit typed declarations with (uintptr_t) cast for pointer-typed block params in proc_fn_args_unpack.

Details

Commit
b258716b233a
Parent
b54d68dc38b1

spinel_analyze.rbmodifiedView file

@@ -1007,6 +1007,9 @@
10071007 end
10081008
10091009 def get_body_stmts(nid)
1010+ if @nd_type[nid] == "StatementsNode"
1011+ return get_stmts(nid)
1012+ end
10101013 body = @nd_body[nid]
10111014 if body < 0
10121015 return []
@@ -11605,7 +11608,134 @@
1160511608 # `def []=(_, value); case ... when :a then @a = value;
1160611609 # when :b then @b = value; end` shapes where the param flows
1160711610 # into ivars of differing types — the param then needs to widen
11608- # to poly so each ivar-write arm can unbox/cast to its slot type.
11611+ # to poly so each ivar-write arm can unbox/cast to its slot type.
11612+
11613+ # Find the RHS type of the most recent write to `vname` in body
11614+ # rooted at `nid`. Returns "" if not found.
11615+ def find_var_write_rhs_type(nid, vname)
11616+ if nid < 0
11617+ return ""
11618+ end
11619+ if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == vname
11620+ rhs = @nd_expression[nid]
11621+ if rhs >= 0
11622+ rt = infer_type(rhs)
11623+ if rt != "" && rt != "int" && rt != "void"
11624+ return rt
11625+ end
11626+ end
11627+ end
11628+ stmts = get_body_stmts(nid)
11629+ k = 0
11630+ while k < stmts.length
11631+ rt = find_var_write_rhs_type(stmts[k], vname)
11632+ if rt != ""
11633+ return rt
11634+ end
11635+ k = k + 1
11636+ end
11637+ return ""
11638+ end
11639+
11640+ def infer_proc_blk_param_types
11641+ @meth_blk_param_types = "".split(",")
11642+ mi = 0
11643+ while mi < @meth_param_names.length
11644+ types = @meth_param_types[mi].split(",")
11645+ if types.length > 0 && types.last == "proc"
11646+ bpname = @meth_param_names[mi].split(",").last
11647+ acc = "".split(",")
11648+ collect_blk_call_arg_types(@meth_body_ids[mi], bpname, acc, @meth_body_ids[mi])
11649+ @meth_blk_param_types.push(acc.join("|"))
11650+ else
11651+ @meth_blk_param_types.push("")
11652+ end
11653+ mi = mi + 1
11654+ end
11655+ @cls_cmeth_blk_param_types = "".split(",")
11656+ ci = 0
11657+ while ci < @cls_names.length
11658+ if ci < @cls_cmeth_names.length
11659+ cmnames = @cls_cmeth_names[ci].split(";")
11660+ cm_bodies = @cls_cmeth_bodies[ci].split(";")
11661+ cm_ptypes = @cls_cmeth_ptypes[ci].split("|")
11662+ cmidx = 0
11663+ while cmidx < cmnames.length
11664+ if cmidx < cm_ptypes.length
11665+ cpts = cm_ptypes[cmidx].split(",")
11666+ if cpts.length > 0 && cpts.last == "proc"
11667+ cpnames = cls_cmeth_pnames_get(ci, cmidx)
11668+ bpname = cpnames[cpnames.length - 1]
11669+ acc = "".split(",")
11670+ body_id = cm_bodies[cmidx].to_i
11671+ collect_blk_call_arg_types(body_id, bpname, acc, body_id)
11672+ @cls_cmeth_blk_param_types.push(acc.join("|"))
11673+ else
11674+ @cls_cmeth_blk_param_types.push("")
11675+ end
11676+ else
11677+ @cls_cmeth_blk_param_types.push("")
11678+ end
11679+ cmidx = cmidx + 1
11680+ end
11681+ end
11682+ ci = ci + 1
11683+ end
11684+ end
11685+
11686+ def collect_blk_call_arg_types(nid, bpname, acc, body_id = -1)
11687+ if nid < 0
11688+ return
11689+ end
11690+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "call"
11691+ recv = @nd_receiver[nid]
11692+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode" && @nd_name[recv] == bpname
11693+ args_id = @nd_arguments[nid]
11694+ if args_id >= 0
11695+ arg_ids = get_args(args_id)
11696+ pi = 0
11697+ while pi < arg_ids.length
11698+ t = infer_type(arg_ids[pi])
11699+ if t == "" || t == "int"
11700+ arg_node = arg_ids[pi]
11701+ if arg_node >= 0 && @nd_type[arg_node] == "LocalVariableReadNode"
11702+ lvname = @nd_name[arg_node]
11703+ lvt = find_var_type(lvname)
11704+ if lvt != "" && lvt != "int"
11705+ t = lvt
11706+ end
11707+ if t == "int" && body_id >= 0
11708+ wtt = find_var_write_rhs_type(body_id, lvname)
11709+ if wtt != ""
11710+ t = wtt
11711+ end
11712+ end
11713+ end
11714+ end
11715+ while acc.length <= pi
11716+ acc.push("")
11717+ end
11718+ if acc[pi] == ""
11719+ acc[pi] = t
11720+ elsif acc[pi] != t
11721+ acc[pi] = "poly"
11722+ @needs_rb_value = 1 if acc[pi] == "poly"
11723+ end
11724+ pi = pi + 1
11725+ end
11726+ end
11727+ return
11728+ end
11729+ end
11730+ stmts = get_body_stmts(nid)
11731+ k = 0
11732+ while k < stmts.length
11733+ collect_blk_call_arg_types(stmts[k], bpname, acc, body_id)
11734+ k = k + 1
11735+ end
11736+ end
11737+
11738+ # Used by widen_param_types_from_body_writes to detect
1160911739 def collect_param_ivar_write_slot_types(nid, pname, ci, ivt_set)
1161011740 if nid < 0
1161111741 return
@@ -13818,7 +13948,7 @@
1381813948 # so the slot becomes poly).
1381913949 widen_param_types_from_body_writes
1382013950
13821- # Top-level methods
13951+ # Top-level methods
1382213952 i = 0
1382313953 while i < @meth_names.length
1382413954 push_scope
@@ -16170,6 +16300,7 @@
1617016300 @analysis_frozen = 1
1617116301 precompute_all_scope_decls
1617216302 annotate_all_node_types
16303+ infer_proc_blk_param_types
1617316304 if ENV["SP_POLY_REPORT"] == "1"
1617416305 emit_poly_report
1617516306 end
@@ -16473,6 +16604,7 @@
1647316604 end
1647416605 $stderr.puts "-----------------"
1647516606 end
16607+ end
1647616608
1647716609
1647816610 # ============================================================
@@ -21155,6 +21287,8 @@
2115521287 # Non-array string ivars (computed in analyze, consumed by emit)
2115621288 buf = buf + "STR @cls_cmeth_live " + ir_escape(@cls_cmeth_live) + "\n"
2115721289 buf = buf + "STR @cls_meth_live " + ir_escape(@cls_meth_live) + "\n"
21290+ buf = buf + "STR @meth_blk_param_types " + ir_escape(@meth_blk_param_types.join("|")) + "\n"
21291+ buf = buf + "STR @cls_cmeth_blk_param_types " + ir_escape(@cls_cmeth_blk_param_types.join("|")) + "\n"
2115821292
2115921293 # Per-AST-node records (T / NM / NB / SN / ST) get accumulated
2116021294 # into a StrArray and joined once. Building them with `buf + ...`