Commit 62a3227c

62a3227c72a4Merged

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

Description

Add class-membership test for case/when against poly scrutinee

When the predicate is TY_POLY and a when-arm is a ConstantReadNode (Integer, String, Float, Symbol, etc.), emit a tag check (_tN.tag == SP_TAG_INT etc.) instead of sp_poly_eq + emit_boxed. Mirrors the same logic already used for poly.is_a?(Class).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
62a3227c72a4
Parent
179b3c990587

src/codegen.cmodifiedView file

@@ -9081,6 +9081,53 @@
90819081 }
90829082 }
90839083
9084+/* Emit `when ClassName` test against a poly (sp_RbVal) scrutinee temp.
9085+ Returns 1 if the class is known and the check was emitted, 0 otherwise. */
9086+static int emit_poly_class_when(Compiler *c, int cond_id, const char *tmp, Buf *b) {
9087+ const NodeTable *nt = c->nt;
9088+ const char *cty = nt_type(nt, cond_id);
9089+ if (!cty || strcmp(cty, "ConstantReadNode")) return 0;
9090+ const char *cn = nt_str(nt, cond_id, "name");
9091+ if (!cn) return 0;
9092+ if (!strcmp(cn, "Integer") || !strcmp(cn, "Fixnum"))
9093+ buf_printf(b, "%s.tag == SP_TAG_INT", tmp);
9094+ else if (!strcmp(cn, "String"))
9095+ buf_printf(b, "%s.tag == SP_TAG_STR", tmp);
9096+ else if (!strcmp(cn, "Float"))
9097+ buf_printf(b, "%s.tag == SP_TAG_FLT", tmp);
9098+ else if (!strcmp(cn, "Symbol"))
9099+ buf_printf(b, "%s.tag == SP_TAG_SYM", tmp);
9100+ else if (!strcmp(cn, "NilClass"))
9101+ buf_printf(b, "%s.tag == SP_TAG_NIL", tmp);
9102+ else if (!strcmp(cn, "TrueClass"))
9103+ buf_printf(b, "(%s.tag == SP_TAG_BOOL && %s.v.b)", tmp, tmp);
9104+ else if (!strcmp(cn, "FalseClass"))
9105+ buf_printf(b, "(%s.tag == SP_TAG_BOOL && !%s.v.b)", tmp, tmp);
9106+ else if (!strcmp(cn, "Numeric"))
9107+ buf_printf(b, "(%s.tag == SP_TAG_INT || %s.tag == SP_TAG_FLT)", tmp, tmp);
9108+ else if (!strcmp(cn, "Array"))
9109+ buf_printf(b, "(%s.tag == SP_TAG_OBJ && %s.cls_id <= -1 && %s.cls_id >= -12)", tmp, tmp, tmp);
9110+ else if (!strcmp(cn, "Hash"))
9111+ buf_printf(b, "(%s.tag == SP_TAG_OBJ && %s.cls_id <= -13 && %s.cls_id >= -20)", tmp, tmp, tmp);
9112+ else {
9113+ int cid = comp_class_index(c, cn);
9114+ if (cid >= 0) {
9115+ buf_printf(b, "(%s.tag == SP_TAG_OBJ && (", tmp);
9116+ int first = 1;
9117+ for (int k = 0; k < c->nclasses; k++) {
9118+ if (k == cid || is_descendant(c, k, cid)) {
9119+ buf_printf(b, "%s%s.cls_id == %d", first ? "" : " || ", tmp, k);
9120+ first = 0;
9121+ }
9122+ }
9123+ if (first) buf_puts(b, "0");
9124+ buf_puts(b, "))");
9125+ }
9126+ else return 0;
9127+ }
9128+ return 1;
9129+}
9130+
90849131 /* case/when -> an if / else-if chain. Statement form. */
90859132 static void emit_case(Compiler *c, int id, Buf *b, int indent) {
90869133 const NodeTable *nt = c->nt;
@@ -9174,7 +9221,9 @@
91749221 buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")");
91759222 }
91769223 else if (pt == TY_POLY) {
9177- buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")");
9224+ char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);
9225+ if (!emit_poly_class_when(c, conds[j], tmp, b))
9226+ { buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")"); }
91789227 }
91799228 else {
91809229 buf_printf(b, "(_t%d == ", t); emit_expr(c, conds[j], b); buf_puts(b, ")");
@@ -9254,7 +9303,11 @@
92549303 buf_puts(b, "0");
92559304 }
92569305 else if (pt == TY_STRING) { buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
9257- else if (pt == TY_POLY) { buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")"); }
9306+ else if (pt == TY_POLY) {
9307+ char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);
9308+ if (!emit_poly_class_when(c, conds[j], tmp, b))
9309+ { buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")"); }
9310+ }
92589311 else { buf_printf(b, "(_t%d == ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
92599312 }
92609313 else { buf_puts(b, "("); emit_expr(c, conds[j], b); buf_puts(b, ")"); }