Commit 3bb483bd

3bb483bdafdcMerged

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

Description

Add poly (sp_RbVal) Tier 1: box/store/print mixed values and poly arrays

TY_POLY -> sp_RbVal, TY_POLY_ARRAY -> sp_PolyArray*. Poly locals declared with SP_GC_ROOT_RBVAL. emit_boxed wraps a non-poly value via sp_box_* (idempotent: already-poly passes through). Mixed array literals build an sp_PolyArray of boxed elements; assignment to a poly slot boxes the RHS; puts/p handle poly and poly_array via sp_poly_puts / sp_poly_inspect / sp_PolyArray_inspect.

Also fix a tail-position raise emitting return sp_raise(...) (void value) once a method's return type widened to poly -- raise diverges, so emit it as a plain statement.

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

Details

Commit
3bb483bdafdc
Parent
0a798c07cf74

src/codegen.cmodifiedView file

@@ -111,6 +111,8 @@
111111 case TY_STR_STR_HASH: return "sp_StrStrHash *";
112112 case TY_INT_INT_HASH: return "sp_IntIntHash *";
113113 case TY_INT_STR_HASH: return "sp_IntStrHash *";
114+ case TY_POLY: return "sp_RbVal";
115+ case TY_POLY_ARRAY: return "sp_PolyArray *";
114116 default: return NULL;
115117 }
116118 }
@@ -118,6 +120,7 @@
118120 return t == TY_INT || t == TY_FLOAT || t == TY_BOOL || t == TY_STRING ||
119121 t == TY_SYMBOL || t == TY_RANGE || t == TY_EXCEPTION ||
120122 t == TY_INT_ARRAY || t == TY_FLOAT_ARRAY || t == TY_STR_ARRAY ||
123+ t == TY_POLY || t == TY_POLY_ARRAY ||
121124 ty_is_hash(t) || ty_is_object(t);
122125 }
123126 static const char *default_value(TyKind t) {
@@ -131,7 +134,9 @@
131134 case TY_EXCEPTION: return "NULL";
132135 case TY_INT_ARRAY:
133136 case TY_FLOAT_ARRAY:
134- case TY_STR_ARRAY: return "NULL";
137+ case TY_STR_ARRAY:
138+ case TY_POLY_ARRAY: return "NULL";
139+ case TY_POLY: return "sp_box_nil()";
135140 default: return (ty_is_hash(t) || ty_is_object(t)) ? "NULL" : "0";
136141 }
137142 }
@@ -193,6 +198,7 @@
193198 static void emit_index_op_write(Compiler *c, int id, Buf *b, int indent);
194199 static void emit_super(Compiler *c, int id, Buf *b);
195200 static void emit_args_filled(Compiler *c, int callee_idx, int argsNode, const char *lead, Buf *out);
201+static void emit_boxed(Compiler *c, int node, Buf *b);
196202
197203 /* Strip ParenthesesNode wrappers to reach the inner expression. */
198204 static int unwrap_parens(Compiler *c, int id) {
@@ -1581,7 +1587,26 @@
15811587 emitted via the target's type in emit_assign. If we reach here for
15821588 an empty literal, fall back to an int array. */
15831589 const char *k = array_kind(at);
1584- if (n == 0 && !k) { buf_puts(b, "sp_IntArray_new()"); return; }
1590+ if (n == 0 && !k && at != TY_POLY_ARRAY) { buf_puts(b, "sp_IntArray_new()"); return; }
1591+ /* poly (mixed-element) array: build an sp_PolyArray of boxed elements */
1592+ if (at == TY_POLY_ARRAY) {
1593+ int t = ++g_tmp;
1594+ emit_indent(g_pre, g_indent);
1595+ buf_printf(g_pre, "sp_PolyArray *_t%d = sp_PolyArray_new();\n", t);
1596+ emit_indent(g_pre, g_indent);
1597+ buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", t);
1598+ for (int j = 0; j < n; j++) {
1599+ Buf el; memset(&el, 0, sizeof el);
1600+ emit_boxed(c, els[j], &el);
1601+ emit_indent(g_pre, g_indent);
1602+ buf_printf(g_pre, "sp_PolyArray_push(_t%d, ", t);
1603+ buf_puts(g_pre, el.p ? el.p : "");
1604+ buf_puts(g_pre, ");\n");
1605+ free(el.p);
1606+ }
1607+ buf_printf(b, "_t%d", t);
1608+ return;
1609+ }
15851610 if (!k) unsupported(c, id, "array literal (element type)");
15861611 int t = ++g_tmp;
15871612 emit_indent(g_pre, g_indent);
@@ -1710,6 +1735,18 @@
17101735 buf_printf(b, "{ const char *_ps = sp_StrArray_get(%s, _t%d); if (_ps) fputs(_ps, stdout); if (!_ps || !*_ps || _ps[strlen(_ps)-1] != '\\n') putchar('\\n'); }\n", a, ti);
17111736 free(ab.p);
17121737 }
1738+ else if (t == TY_POLY) {
1739+ buf_puts(b, "sp_poly_puts("); emit_expr(c, arg, b); buf_puts(b, ");\n");
1740+ }
1741+ else if (t == TY_POLY_ARRAY) {
1742+ int ta = ++g_tmp, ti = ++g_tmp;
1743+ Buf ab; memset(&ab, 0, sizeof ab); emit_expr(c, arg, &ab);
1744+ buf_printf(b, "{ sp_PolyArray *_t%d = %s;\n", ta, ab.p ? ab.p : "");
1745+ emit_indent(b, indent);
1746+ buf_printf(b, "for (mrb_int _t%d = 0; _t%d < sp_PolyArray_length(_t%d); _t%d++) sp_poly_puts(sp_PolyArray_get(_t%d, _t%d)); }\n",
1747+ ti, ti, ta, ti, ta, ti);
1748+ free(ab.p);
1749+ }
17131750 else if (ty_is_object(t) && comp_method_in_class(c, ty_object_class(t), "to_s") >= 0) {
17141751 int cid = ty_object_class(t);
17151752 buf_puts(b, "{ const char *_ps = (const char *)(");
@@ -1784,6 +1821,14 @@
17841821 emit_expr(c, arg, b);
17851822 buf_puts(b, "), stdout); putchar('\\n');\n");
17861823 }
1824+ else if (t == TY_POLY_ARRAY) {
1825+ buf_puts(b, "fputs(sp_PolyArray_inspect("); emit_expr(c, arg, b);
1826+ buf_puts(b, "), stdout); putchar('\\n');\n");
1827+ }
1828+ else if (t == TY_POLY) {
1829+ buf_puts(b, "fputs(sp_poly_inspect("); emit_expr(c, arg, b);
1830+ buf_puts(b, "), stdout); putchar('\\n');\n");
1831+ }
17871832 else {
17881833 unsupported(c, arg, "p argument");
17891834 }
@@ -1858,6 +1903,9 @@
18581903 buf_printf(b, "sp_%sHash_new()", hcn);
18591904 }
18601905 }
1906+ else if (lv && lv->type == TY_POLY) {
1907+ emit_boxed(c, v, b); /* poly slot: box the (non-poly) RHS */
1908+ }
18611909 else {
18621910 emit_expr(c, v, b);
18631911 }
@@ -2399,6 +2447,12 @@
23992447 if (!strcmp(ty, "IfNode")) { emit_if(c, id, b, indent, 0, 1); return; }
24002448 if (!strcmp(ty, "UnlessNode")) { emit_if(c, id, b, indent, 1, 1); return; }
24012449 if (!strcmp(ty, "ReturnNode")) { emit_return(c, id, b, indent); return; }
2450+ /* `raise` diverges -- no value to return; emit as a plain statement. */
2451+ if (!strcmp(ty, "CallNode") && nt_ref(nt, id, "receiver") < 0 &&
2452+ nt_str(nt, id, "name") && !strcmp(nt_str(nt, id, "name"), "raise")) {
2453+ emit_indent(b, indent); emit_expr(c, id, b); buf_puts(b, ";\n");
2454+ return;
2455+ }
24022456 if (!strcmp(ty, "BeginNode")) {
24032457 /* begin/rescue value -> a temp, assigned in both branches, then tail */
24042458 TyKind rt = comp_ntype(c, id);
@@ -2465,8 +2519,39 @@
24652519 /* ---- declarations ---- */
24662520
24672521 /* Heap-managed types need a GC root for their local slot. */
2468-static int needs_root(TyKind t) { return t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t) || t == TY_EXCEPTION; }
2522+static int needs_root(TyKind t) { return t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t) || t == TY_EXCEPTION || t == TY_POLY; }
24692523
2524+/* Emit `node` boxed into an sp_RbVal. Idempotent: an already-poly value is
2525+ passed through unboxed (double-boxing is a classic silent-corruption bug). */
2526+static void emit_boxed(Compiler *c, int node, Buf *b) {
2527+ TyKind t = comp_ntype(c, node);
2528+ if (t == TY_POLY) { emit_expr(c, node, b); return; }
2529+ if (ty_is_object(t)) {
2530+ buf_printf(b, "sp_box_obj(");
2531+ emit_expr(c, node, b);
2532+ buf_printf(b, ", %d)", ty_object_class(t));
2533+ return;
2534+ }
2535+ const char *fn = NULL;
2536+ switch (t) {
2537+ case TY_INT: fn = "sp_box_int"; break;
2538+ case TY_FLOAT: fn = "sp_box_float"; break;
2539+ case TY_STRING: fn = "sp_box_str"; break;
2540+ case TY_BOOL: fn = "sp_box_bool"; break;
2541+ case TY_SYMBOL: fn = "sp_box_sym"; break;
2542+ case TY_INT_ARRAY: fn = "sp_box_int_array"; break;
2543+ case TY_FLOAT_ARRAY: fn = "sp_box_float_array"; break;
2544+ case TY_STR_ARRAY: fn = "sp_box_str_array"; break;
2545+ case TY_POLY_ARRAY: fn = "sp_box_poly_array"; break;
2546+ case TY_NIL: buf_puts(b, "sp_box_nil()"); return;
2547+ default: break;
2548+ }
2549+ if (!fn) { unsupported(c, node, "boxing value into poly"); return; }
2550+ buf_printf(b, "%s(", fn);
2551+ emit_expr(c, node, b);
2552+ buf_puts(b, ")");
2553+}
2554+
24702555 /* `vol` makes the local volatile (required for locals live across a setjmp
24712556 in a begin/rescue). Pointers need the volatile on the pointer itself
24722557 (T * volatile), value types take a leading qualifier. */
@@ -2482,6 +2567,7 @@
24822567 case TY_SYMBOL: buf_puts(&cty, "sp_sym"); init = "((sp_sym)-1)"; break;
24832568 case TY_RANGE: buf_puts(&cty, "sp_Range"); init = "{0}"; break;
24842569 case TY_STRING: buf_puts(&cty, "const char *"); init = "(&(\"\\xff\")[1])"; ptr = 1; break;
2570+ case TY_POLY: buf_puts(&cty, "sp_RbVal"); init = "sp_box_nil()"; break;
24852571 default:
24862572 if (is_scalar_ret(t) && t != TY_UNKNOWN) { emit_ctype(c, t, &cty); init = "NULL"; ptr = 1; }
24872573 else {
@@ -2494,7 +2580,8 @@
24942580 buf_puts(b, cty.p ? cty.p : "");
24952581 if (vol && ptr) buf_puts(b, "volatile "); /* cty ends with "* "; -> "* volatile " */
24962582 buf_printf(b, " lv_%s = %s;\n", lv->name, init);
2497- if (root) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name);
2583+ if (t == TY_POLY) buf_printf(b, " SP_GC_ROOT_RBVAL(lv_%s);\n", lv->name);
2584+ else if (root) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name);
24982585 free(cty.p);
24992586 }
25002587