Commit 80fa8bde

80fa8bdebba5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

feat(codegen): multi-arg lambda support up to arity 4 (#400)

Fixes #400.

->(a, b) { a + b } previously emitted broken code: only the first required param was registered as formal; 2nd+ were treated as free variables to capture, the call site sp_lam_call(f, a) dropped extra args, and the body fn signature was 1-arg.

Three coordinated changes:

  1. lib/sp_runtime.h: add sp_lam_call2 / sp_lam_call3 / sp_lam_call4 (and matching sp_fn{2,3,4}_t typedefs) that re-cast the stored 1-arg fn ptr to the right arity at the call site.

  2. spinel_codegen.rb scan_lambda_free_vars + compile_lambda_expr: collect every required param (was reqs[0] only). The lambda fn signature gets one sp_Val *_argN per param plus aliasing lv_<name> decls inside the body.

  3. compile_lambda_call_expr dispatches to sp_lam_callN based on arg count; arity 1 still uses the original sp_lam_call.

compile_lambda_body_expr also learns * / - / / / % / <>= arms so multi-arg arithmetic bodies (a * b * c, a > b) lower correctly. Without these, those CallNodes fell through to the catch-all &sp_lam_nil_val and the lambda returned nil for any non-add expression.

The fn ptr stored in sp_Val.u.proc.fn keeps its 1-arg sp_fn_t type; the multi-arg path casts on both sides (sp_lam_proc receive + sp_lam_callN dispatch). Arities beyond 4 keep the legacy 1-arg path (caller is on its own).

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

Details

Commit
80fa8bdebba5
Parent
c58498694011

spinel_codegen.rbmodifiedView file

@@ -11319,9 +11319,27 @@
1131911319 args_id = @nd_arguments[nid]
1132011320 if args_id >= 0
1132111321 aargs = get_args(args_id)
11322- if aargs.length > 0
11322+ # Issue #400: multi-arg lambda dispatch. Pick `sp_lam_callN`
11323+ # based on the number of args; up to arity-4 covered. Beyond
11324+ # that, fall back to single-arg (caller is on its own).
11325+ if aargs.length == 1
1132311326 ac = wrap_as_sp_val(aargs.first)
1132411327 call_expr = "sp_lam_call(" + rc + ", " + ac + ")"
11328+ elsif aargs.length == 2
11329+ a0 = wrap_as_sp_val(aargs[0])
11330+ a1 = wrap_as_sp_val(aargs[1])
11331+ call_expr = "sp_lam_call2(" + rc + ", " + a0 + ", " + a1 + ")"
11332+ elsif aargs.length == 3
11333+ a0 = wrap_as_sp_val(aargs[0])
11334+ a1 = wrap_as_sp_val(aargs[1])
11335+ a2 = wrap_as_sp_val(aargs[2])
11336+ call_expr = "sp_lam_call3(" + rc + ", " + a0 + ", " + a1 + ", " + a2 + ")"
11337+ elsif aargs.length == 4
11338+ a0 = wrap_as_sp_val(aargs[0])
11339+ a1 = wrap_as_sp_val(aargs[1])
11340+ a2 = wrap_as_sp_val(aargs[2])
11341+ a3 = wrap_as_sp_val(aargs[3])
11342+ call_expr = "sp_lam_call4(" + rc + ", " + a0 + ", " + a1 + ", " + a2 + ", " + a3 + ")"
1132511343 end
1132611344 end
1132711345 if call_expr == ""
@@ -22016,18 +22034,17 @@
2201622034 end
2201722035 if t == "LambdaNode"
2201822036 # For nested lambdas, find their free vars and add them to OUR free vars
22019- # (they need to be captured transitively)
22020- inner_pname = ""
22037+ # (they need to be captured transitively). Collect ALL formal params,
22038+ # not just the first one (issue #400).
22039+ inner_params = "".split(",")
2202122040 inner_params_id = @nd_parameters[nid]
2202222041 if inner_params_id >= 0
2202322042 reqs = parse_id_list(@nd_requireds[inner_params_id])
22024- if reqs.length > 0
22025- inner_pname = @nd_name[reqs[0]]
22043+ ip = 0
22044+ while ip < reqs.length
22045+ inner_params.push(@nd_name[reqs[ip]])
22046+ ip = ip + 1
2202622047 end
22027- end
22028- inner_params = "".split(",")
22029- if inner_pname != ""
22030- inner_params.push(inner_pname)
2203122048 end
2203222049 inner_body = @nd_body[nid]
2203322050 if inner_body >= 0
@@ -22400,6 +22417,43 @@
2240022417 return "sp_lam_int(sp_lam_to_int(" + rc + ") + sp_lam_to_int(" + ac + "))"
2240122418 end
2240222419 end
22420+ # Issue #400: more numeric ops on sp_Val so multi-arg lambda
22421+ # bodies like `->(a, b, c) { a * b * c }` lower correctly.
22422+ if mname == "-"
22423+ if recv >= 0
22424+ rc = compile_lambda_body_expr(recv, params, captures)
22425+ ac = compile_lambda_body_expr(get_args(@nd_arguments[nid])[0], params, captures)
22426+ return "sp_lam_int(sp_lam_to_int(" + rc + ") - sp_lam_to_int(" + ac + "))"
22427+ end
22428+ end
22429+ if mname == "*"
22430+ if recv >= 0
22431+ rc = compile_lambda_body_expr(recv, params, captures)
22432+ ac = compile_lambda_body_expr(get_args(@nd_arguments[nid])[0], params, captures)
22433+ return "sp_lam_int(sp_lam_to_int(" + rc + ") * sp_lam_to_int(" + ac + "))"
22434+ end
22435+ end
22436+ if mname == "/"
22437+ if recv >= 0
22438+ rc = compile_lambda_body_expr(recv, params, captures)
22439+ ac = compile_lambda_body_expr(get_args(@nd_arguments[nid])[0], params, captures)
22440+ return "sp_lam_int(sp_idiv(sp_lam_to_int(" + rc + "), sp_lam_to_int(" + ac + ")))"
22441+ end
22442+ end
22443+ if mname == "%"
22444+ if recv >= 0
22445+ rc = compile_lambda_body_expr(recv, params, captures)
22446+ ac = compile_lambda_body_expr(get_args(@nd_arguments[nid])[0], params, captures)
22447+ return "sp_lam_int(sp_imod(sp_lam_to_int(" + rc + "), sp_lam_to_int(" + ac + ")))"
22448+ end
22449+ end
22450+ if mname == "<" || mname == ">" || mname == "<=" || mname == ">=" || mname == "==" || mname == "!="
22451+ if recv >= 0
22452+ rc = compile_lambda_body_expr(recv, params, captures)
22453+ ac = compile_lambda_body_expr(get_args(@nd_arguments[nid])[0], params, captures)
22454+ return "sp_lam_bool(sp_lam_to_int(" + rc + ") " + mname + " sp_lam_to_int(" + ac + "))"
22455+ end
22456+ end
2240322457 return "&sp_lam_nil_val"
2240422458 end
2240522459 if t == "IfNode"
@@ -22445,18 +22499,24 @@
2244522499
2244622500 def compile_lambda_expr(nid)
2244722501 @needs_lambda = 1
22448- # Get the parameter name
22449- pname = ""
22502+ # Collect ALL formal params, not just the first (issue #400). The
22503+ # legacy single-`pname` path still drives the typed-caps emit
22504+ # block below (it only handles single-arg lambdas with typed
22505+ # captures); multi-arg flows through the sp_Val* path which now
22506+ # honours `param_arr.length`.
22507+ param_arr = "".split(",")
2245022508 params_id = @nd_parameters[nid]
2245122509 if params_id >= 0
2245222510 reqs = parse_id_list(@nd_requireds[params_id])
22453- if reqs.length > 0
22454- pname = @nd_name[reqs[0]]
22511+ pi = 0
22512+ while pi < reqs.length
22513+ param_arr.push(@nd_name[reqs[pi]])
22514+ pi = pi + 1
2245522515 end
2245622516 end
22457- param_arr = "".split(",")
22458- if pname != ""
22459- param_arr.push(pname)
22517+ pname = ""
22518+ if param_arr.length > 0
22519+ pname = param_arr[0]
2246022520 end
2246122521
2246222522 body = @nd_body[nid]
@@ -22647,21 +22707,37 @@
2264722707 @lambda_captures = save_captures
2264822708 @lambda_capture_cell_types = save_cell_types
2264922709
22710+ # Issue #400: emit signature with one sp_Val* per param so
22711+ # multi-arg `->(a, b) { a + b }` works. The runtime
22712+ # sp_lam_call2 / _3 / _4 cast the fn ptr to the matching arity.
22713+ sig_args = "sp_Val *self"
22714+ decls = ""
22715+ pi_s = 0
22716+ while pi_s < param_arr.length
22717+ sig_args = sig_args + ", sp_Val *_arg" + pi_s.to_s
22718+ decls = decls + " sp_Val *lv_" + param_arr[pi_s] + " = _arg" + pi_s.to_s + ";\n"
22719+ pi_s = pi_s + 1
22720+ end
22721+ if param_arr.length == 0
22722+ sig_args = sig_args + ", sp_Val *arg"
22723+ end
2265022724 if body_stmts != ""
22651- @lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"
22652- if pname != ""
22653- @lambda_funcs <<" sp_Val *lv_" + pname + " = arg;\n"
22654- end
22725+ @lambda_funcs <<"static sp_Val *" + fname + "(" + sig_args + ") {\n"
22726+ @lambda_funcs <<decls
2265522727 @lambda_funcs <<" (void)self;\n"
22728+ if param_arr.length == 0
22729+ @lambda_funcs <<" (void)arg;\n"
22730+ end
2265622731 @lambda_funcs <<body_stmts + 10.chr
2265722732 @lambda_funcs <<" return " + bexpr + ";\n"
2265822733 @lambda_funcs <<"}\n\n"
2265922734 else
22660- @lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"
22661- if pname != ""
22662- @lambda_funcs <<" sp_Val *lv_" + pname + " = arg;\n"
22735+ @lambda_funcs <<"static sp_Val *" + fname + "(" + sig_args + ") {\n"
22736+ @lambda_funcs <<decls
22737+ @lambda_funcs <<" (void)self;\n"
22738+ if param_arr.length == 0
22739+ @lambda_funcs <<" (void)arg;\n"
2266322740 end
22664- @lambda_funcs <<" (void)self;\n"
2266522741 @lambda_funcs <<" return " + bexpr + ";\n"
2266622742 @lambda_funcs <<"}\n\n"
2266722743 end
@@ -22694,7 +22770,9 @@
2269422770 k = k + 1
2269522771 end
2269622772 tmp = new_temp
22697- emit(" sp_Val *" + tmp + " = sp_lam_proc(" + fname + ", " + free_vars.length.to_s + ");")
22773+ # Cast multi-arg fn ptr to sp_fn_t for storage; sp_lam_callN
22774+ # casts back to the matching arity at the call site (Issue #400).
22775+ emit(" sp_Val *" + tmp + " = sp_lam_proc((sp_fn_t)(uintptr_t)" + fname + ", " + free_vars.length.to_s + ");")
2269822776 k = 0
2269922777 while k < free_vars.length
2270022778 fv = free_vars[k]
@@ -22735,7 +22813,7 @@
2273522813 end
2273622814 return tmp
2273722815 else
22738- return "sp_lam_proc(" + fname + ", 0)"
22816+ return "sp_lam_proc((sp_fn_t)(uintptr_t)" + fname + ", 0)"
2273922817 end
2274022818 end
2274122819