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
end10081008
10091009
def get_body_stmts(nid)1010+
if @nd_type[nid] == "StatementsNode"1011+
return get_stmts(nid)1012+
end10101013
body = @nd_body[nid]10111014
if body < 010121015
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 flows1160711610
# into ivars of differing types — the param then needs to widen11608-
# 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 body11614+
# rooted at `nid`. Returns "" if not found.11615+
def find_var_write_rhs_type(nid, vname)11616+
if nid < 011617+
return ""11618+
end11619+
if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == vname11620+
rhs = @nd_expression[nid]11621+
if rhs >= 011622+
rt = infer_type(rhs)11623+
if rt != "" && rt != "int" && rt != "void"11624+
return rt11625+
end11626+
end11627+
end11628+
stmts = get_body_stmts(nid)11629+
k = 011630+
while k < stmts.length11631+
rt = find_var_write_rhs_type(stmts[k], vname)11632+
if rt != ""11633+
return rt11634+
end11635+
k = k + 111636+
end11637+
return ""11638+
end11639+
11640+
def infer_proc_blk_param_types11641+
@meth_blk_param_types = "".split(",")11642+
mi = 011643+
while mi < @meth_param_names.length11644+
types = @meth_param_types[mi].split(",")11645+
if types.length > 0 && types.last == "proc"11646+
bpname = @meth_param_names[mi].split(",").last11647+
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+
else11651+
@meth_blk_param_types.push("")11652+
end11653+
mi = mi + 111654+
end11655+
@cls_cmeth_blk_param_types = "".split(",")11656+
ci = 011657+
while ci < @cls_names.length11658+
if ci < @cls_cmeth_names.length11659+
cmnames = @cls_cmeth_names[ci].split(";")11660+
cm_bodies = @cls_cmeth_bodies[ci].split(";")11661+
cm_ptypes = @cls_cmeth_ptypes[ci].split("|")11662+
cmidx = 011663+
while cmidx < cmnames.length11664+
if cmidx < cm_ptypes.length11665+
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_i11671+
collect_blk_call_arg_types(body_id, bpname, acc, body_id)11672+
@cls_cmeth_blk_param_types.push(acc.join("|"))11673+
else11674+
@cls_cmeth_blk_param_types.push("")11675+
end11676+
else11677+
@cls_cmeth_blk_param_types.push("")11678+
end11679+
cmidx = cmidx + 111680+
end11681+
end11682+
ci = ci + 111683+
end11684+
end11685+
11686+
def collect_blk_call_arg_types(nid, bpname, acc, body_id = -1)11687+
if nid < 011688+
return11689+
end11690+
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] == bpname11693+
args_id = @nd_arguments[nid]11694+
if args_id >= 011695+
arg_ids = get_args(args_id)11696+
pi = 011697+
while pi < arg_ids.length11698+
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 = lvt11706+
end11707+
if t == "int" && body_id >= 011708+
wtt = find_var_write_rhs_type(body_id, lvname)11709+
if wtt != ""11710+
t = wtt11711+
end11712+
end11713+
end11714+
end11715+
while acc.length <= pi11716+
acc.push("")11717+
end11718+
if acc[pi] == ""11719+
acc[pi] = t11720+
elsif acc[pi] != t11721+
acc[pi] = "poly"11722+
@needs_rb_value = 1 if acc[pi] == "poly"11723+
end11724+
pi = pi + 111725+
end11726+
end11727+
return11728+
end11729+
end11730+
stmts = get_body_stmts(nid)11731+
k = 011732+
while k < stmts.length11733+
collect_blk_call_arg_types(stmts[k], bpname, acc, body_id)11734+
k = k + 111735+
end11736+
end11737+
11738+
# Used by widen_param_types_from_body_writes to detect1160911739
def collect_param_ivar_write_slot_types(nid, pname, ci, ivt_set)1161011740
if nid < 01161111741
return@@ -13818,7 +13948,7 @@
1381813948
# so the slot becomes poly).1381913949
widen_param_types_from_body_writes1382013950
13821-
# Top-level methods13951+
# Top-level methods1382213952
i = 01382313953
while i < @meth_names.length1382413954
push_scope@@ -16170,6 +16300,7 @@
1617016300
@analysis_frozen = 11617116301
precompute_all_scope_decls1617216302
annotate_all_node_types16303+
infer_proc_blk_param_types1617316304
if ENV["SP_POLY_REPORT"] == "1"1617416305
emit_poly_report1617516306
end@@ -16473,6 +16604,7 @@
1647316604
end1647416605
$stderr.puts "-----------------"1647516606
end16607+
end1647616608
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 accumulated2116021294
# into a StrArray and joined once. Building them with `buf + ...`spinel_codegen.rbmodifiedView file
Loading diff…
test/proc_body_nested_block.rbaddedView file
Loading diff…
test/proc_body_nested_block.rb.expectedaddedView file
Loading diff…