Commit ad4b3b30

ad4b3b306386Merged

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

Description

Emit a C switch for value-position case <int>

The statement form of case already lowers an integer subject with integer-literal when clauses to a C switch (jump table). The expression/value form (x = case n when 0 then ... end) still walked an O(n) == if-chain. Mirror the fast path in emit_case_expr: when the predicate is TY_INT and every when condition is an IntegerNode, capture each branch value into the result temp through a switch.

Gated to TY_INT only -- the poly path must keep sp_poly_eq so that === stays correct (0 === "foo" is false, but sp_poly_to_i("foo") would spuriously match case 0). Duplicate when labels fall back to the if-chain, since a repeated C case label is a compile error.

Refs #282

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

Details

Commit
ad4b3b306386
Parent
3d8f02eed749

src/codegen_stmt.cmodifiedView file

@@ -1387,6 +1387,46 @@
13871387 t = ++g_tmp;
13881388 emit_ctype(c, pt, b); buf_printf(b, " _t%d = ", t); emit_expr(c, pred, b); buf_puts(b, "; ");
13891389 }
1390+
1391+ /* Fast path: `case <int> when <integer literals>` captures the branch value
1392+ into _cr through a C switch (jump table) instead of an O(n) `==` if-chain
1393+ -- the slab-AST `case @nd_type[id] when <int>` interpreter-dispatch shape
1394+ (#282). INT only: the poly path keeps sp_poly_eq (`===`), since
1395+ sp_poly_to_i on a non-numeric subject would mis-match `case 0`. */
1396+ if (pred >= 0 && pt == TY_INT && nw > 0) {
1397+ int all_int = 1, ndup = 0;
1398+ long long vals[512];
1399+ for (int w = 0; w < nw && all_int; w++) {
1400+ int wc = 0; const int *conds = nt_arr(nt, whens[w], "conditions", &wc);
1401+ if (wc == 0) { all_int = 0; break; }
1402+ for (int j = 0; j < wc; j++) {
1403+ const char *cty = nt_type(nt, conds[j]);
1404+ if (!cty || strcmp(cty, "IntegerNode")) { all_int = 0; break; }
1405+ long long v = (long long)nt_int(nt, conds[j], "value", 0);
1406+ for (int d = 0; d < ndup; d++) if (vals[d] == v) { all_int = 0; break; } /* dup label -> bail */
1407+ if (all_int && ndup < (int)(sizeof vals / sizeof vals[0])) vals[ndup++] = v;
1408+ }
1409+ }
1410+ if (all_int) {
1411+ buf_printf(b, "switch (_t%d) { ", t);
1412+ for (int w = 0; w < nw; w++) {
1413+ int wc = 0; const int *conds = nt_arr(nt, whens[w], "conditions", &wc);
1414+ for (int j = 0; j < wc; j++)
1415+ buf_printf(b, "case %lldLL: ", (long long)nt_int(nt, conds[j], "value", 0));
1416+ buf_puts(b, "{ ");
1417+ emit_case_branch_value(c, nt_ref(nt, whens[w], "statements"), rt, cr, b);
1418+ buf_puts(b, "break; } ");
1419+ }
1420+ if (else_c >= 0) {
1421+ buf_puts(b, "default: { ");
1422+ emit_case_branch_value(c, nt_ref(nt, else_c, "statements"), rt, cr, b);
1423+ buf_puts(b, "break; } ");
1424+ }
1425+ buf_printf(b, "} _cr%d; })", cr);
1426+ return;
1427+ }
1428+ }
1429+
13901430 for (int w = 0; w < nw; w++) {
13911431 int wn = whens[w];
13921432 int wc = 0;