Commit 6322b075

6322b075e9abMerged

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

Description

codegen: fix Array#sum with init arg, empty array, and PolyPolyHash ||= key boxing

  • Array#sum(init) coerces init type: float-array+int-init casts to mrb_float; float-array+poly-init uses sp_poly_to_f; int-array+poly-init uses sp_poly_to_i; same coercion applied in emit_sum_block_expr
  • PolyArray#sum(init) coerces poly init via sp_poly_to_i
  • [].sum / [].sum(init): empty ArrayNode (TY_UNKNOWN receiver) returns init or 0; boxes result when call type is TY_POLY
  • PolyPolyHash h[k] ||= v: emit_hash_key now boxes typed keys to sp_RbVal when key type is TY_POLY; forward-declares emit_boxed before the helper
  • Array.new(n) single-arg: creates PolyArray of n nils

Fixes bundle_array_e (sum init variants) and hash_int_and_or_assign tests.

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

Details

Commit
6322b075e9ab
Parent
ac496a6e30a5

src/codegen.cmodifiedView file

@@ -438,13 +438,21 @@
438438 static int needs_root(TyKind t);
439439 static int method_is_void(Scope *s);
440440 static void emit_index_op_write(Compiler *c, int id, Buf *b, int indent);
441+static void emit_index_and_or_write(Compiler *c, int id, Buf *b, int indent, int is_or);
442+static void emit_boxed(Compiler *c, int node, Buf *b);
441443 static void emit_hash_key(Compiler *c, int key, TyKind kt, Buf *b) {
442- if (comp_ntype(c, key) == TY_POLY) {
444+ TyKind actual = comp_ntype(c, key);
445+ if (actual == TY_POLY && kt != TY_POLY) {
443446 buf_puts(b, "(");
444447 emit_expr(c, key, b);
445448 buf_puts(b, kt == TY_STRING ? ").v.s" : ").v.i"); /* int/sym share v.i */
446449 return;
447450 }
451+ if (kt == TY_POLY && actual != TY_POLY) {
452+ /* PolyPolyHash key: box the typed value into sp_RbVal */
453+ emit_boxed(c, key, b);
454+ return;
455+ }
448456 emit_expr(c, key, b);
449457 }
450458 static void emit_super(Compiler *c, int id, Buf *b);
@@ -862,7 +870,24 @@
862870 buf_printf(b, "({ sp_%sArray *_t%d = ", k, ta); emit_expr(c, recv, b);
863871 buf_printf(b, "; mrb_int _t%d = sp_%sArray_length(_t%d); ", tn, k, ta);
864872 emit_ctype(c, acct, b); buf_printf(b, " _t%d = ", tacc);
865- if (argc == 1) emit_expr(c, argv[0], b); else buf_puts(b, acct == TY_FLOAT ? "0.0" : "0");
873+ if (argc == 1) {
874+ TyKind init_t = comp_ntype(c, argv[0]);
875+ if (acct == TY_FLOAT && init_t == TY_INT) {
876+ buf_puts(b, "(mrb_float)("); emit_expr(c, argv[0], b); buf_puts(b, ")");
877+ }
878+ else if (acct == TY_FLOAT && init_t == TY_POLY) {
879+ buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[0], b); buf_puts(b, ")");
880+ }
881+ else if (acct == TY_INT && init_t == TY_POLY) {
882+ buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[0], b); buf_puts(b, ")");
883+ }
884+ else {
885+ emit_expr(c, argv[0], b);
886+ }
887+ }
888+ else {
889+ buf_puts(b, acct == TY_FLOAT ? "0.0" : "0");
890+ }
866891 buf_printf(b, "; for (mrb_int _t%d = 0; _t%d < _t%d; _t%d++) { ", ti, ti, tn, ti);
867892 if (p0) buf_printf(b, "lv_%s = sp_%sArray_get(_t%d, _t%d); ", p0, k, ta, ti);
868893 buf_printf(b, "_t%d = ", tacc);
@@ -2909,6 +2934,21 @@
29092934 if (cn && !strcmp(cn, "Array") && argc == 0 && nt_ref(nt, id, "block") < 0) {
29102935 buf_puts(b, "sp_PolyArray_new()"); return;
29112936 }
2937+ if (cn && !strcmp(cn, "Array") && argc == 1 && nt_ref(nt, id, "block") < 0) {
2938+ /* Array.new(n) -> PolyArray of n nils */
2939+ int tn = ++g_tmp, tr = ++g_tmp, ti = ++g_tmp;
2940+ Buf nb; memset(&nb, 0, sizeof nb); emit_expr(c, argv[0], &nb);
2941+ emit_indent(g_pre, g_indent);
2942+ buf_printf(g_pre, "mrb_int _t%d = ", tn); buf_puts(g_pre, nb.p ? nb.p : "0"); buf_puts(g_pre, ";\n");
2943+ emit_indent(g_pre, g_indent);
2944+ buf_printf(g_pre, "sp_PolyArray *_t%d = sp_PolyArray_new();\n", tr);
2945+ emit_indent(g_pre, g_indent); buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", tr);
2946+ emit_indent(g_pre, g_indent);
2947+ buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d; _t%d++) sp_PolyArray_push(_t%d, sp_box_nil());\n",
2948+ ti, ti, tn, ti, tr);
2949+ free(nb.p);
2950+ buf_printf(b, "_t%d", tr); return;
2951+ }
29122952 if (cn && !strcmp(cn, "Array") && nt_ref(nt, id, "block") >= 0) {
29132953 /* Array.new(n) { |i| body } / Array.new(0) { body } */
29142954 int blk = nt_ref(nt, id, "block");
@@ -5002,6 +5042,23 @@
50025042 }
50035043
50045044 /* array value methods */
5045+ /* empty array literal [] has TY_UNKNOWN; sum returns init or 0 */
5046+ if (recv >= 0 && rt == TY_UNKNOWN && !strcmp(name, "sum") &&
5047+ nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ArrayNode")) {
5048+ int en = 0; nt_arr(nt, recv, "elements", &en);
5049+ if (en == 0) {
5050+ TyKind call_t = comp_ntype(c, id);
5051+ if (argc == 1) {
5052+ if (call_t == TY_POLY) emit_boxed(c, argv[0], b);
5053+ else emit_expr(c, argv[0], b);
5054+ }
5055+ else {
5056+ if (call_t == TY_POLY) buf_puts(b, "sp_box_int(0)");
5057+ else buf_puts(b, "0");
5058+ }
5059+ return;
5060+ }
5061+ }
50055062 if (recv >= 0 && ty_is_array(rt)) {
50065063 if (!strcmp(name, "pack") && argc == 1 && (rt == TY_INT_ARRAY || rt == TY_POLY_ARRAY)) {
50075064 buf_printf(b, "sp_%sArray_pack(", rt == TY_POLY_ARRAY ? "Poly" : "Int");
@@ -5056,6 +5113,18 @@
50565113 return;
50575114 }
50585115 }
5116+ if (rt == TY_POLY_ARRAY && !strcmp(name, "sum") && argc == 0 && nt_ref(nt, id, "block") < 0) {
5117+ buf_puts(b, "sp_PolyArray_sum_int("); emit_expr(c, recv, b); buf_puts(b, ")");
5118+ return;
5119+ }
5120+ if (rt == TY_POLY_ARRAY && !strcmp(name, "sum") && argc == 1 && nt_ref(nt, id, "block") < 0) {
5121+ TyKind init_t = comp_ntype(c, argv[0]);
5122+ buf_puts(b, "(");
5123+ if (init_t == TY_POLY) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
5124+ else { emit_expr(c, argv[0], b); }
5125+ buf_puts(b, " + sp_PolyArray_sum_int("); emit_expr(c, recv, b); buf_puts(b, "))");
5126+ return;
5127+ }
50595128 if (rt == TY_POLY_ARRAY && (!strcmp(name, "shift") || !strcmp(name, "pop")) && argc == 0) {
50605129 buf_printf(b, "sp_PolyArray_%s(", name); emit_expr(c, recv, b); buf_puts(b, ")");
50615130 return;
@@ -5413,6 +5482,24 @@
54135482 buf_printf(b, "sp_%sArray_sum(", k); emit_expr(c, recv, b); buf_puts(b, ", 0)");
54145483 return;
54155484 }
5485+ if (!strcmp(name, "sum") && argc == 1 && nt_ref(nt, id, "block") < 0) {
5486+ TyKind init_t = comp_ntype(c, argv[0]);
5487+ buf_printf(b, "sp_%sArray_sum(", k); emit_expr(c, recv, b); buf_puts(b, ", ");
5488+ if (rt == TY_FLOAT_ARRAY && init_t == TY_INT) {
5489+ buf_puts(b, "(mrb_float)("); emit_expr(c, argv[0], b); buf_puts(b, ")");
5490+ }
5491+ else if (rt == TY_FLOAT_ARRAY && init_t == TY_POLY) {
5492+ buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[0], b); buf_puts(b, ")");
5493+ }
5494+ else if (rt == TY_INT_ARRAY && init_t == TY_POLY) {
5495+ buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[0], b); buf_puts(b, ")");
5496+ }
5497+ else {
5498+ emit_expr(c, argv[0], b);
5499+ }
5500+ buf_puts(b, ")");
5501+ return;
5502+ }
54165503 if (!strcmp(name, "join") && rt == TY_STR_ARRAY && argc == 1) {
54175504 buf_puts(b, "sp_StrArray_join("); emit_expr(c, recv, b); buf_puts(b, ", ");
54185505 emit_expr(c, argv[0], b); buf_puts(b, ")");
@@ -6475,6 +6562,80 @@
64756562 unsupported(c, id, "index operator assignment");
64766563 }
64776564
6565+/* h[k] &&= v / h[k] ||= v / a[i] &&= v / a[i] ||= v.
6566+ IndexAndWriteNode / IndexOrWriteNode. Receiver and key evaluated once. */
6567+static void emit_index_and_or_write(Compiler *c, int id, Buf *b, int indent, int is_or) {
6568+ const NodeTable *nt = c->nt;
6569+ int recv = nt_ref(nt, id, "receiver");
6570+ int args = nt_ref(nt, id, "arguments");
6571+ int v = nt_ref(nt, id, "value");
6572+ int argc = 0;
6573+ const int *argv = NULL;
6574+ if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc);
6575+ if (argc != 1) { unsupported(c, id, is_or ? "index-or-write" : "index-and-write"); return; }
6576+ TyKind rt = comp_ntype(c, recv);
6577+ int ta = ++g_tmp, tb = ++g_tmp;
6578+
6579+ if (ty_is_hash(rt)) {
6580+ const char *hn = ty_hash_cname(rt);
6581+ if (!hn) { unsupported(c, id, "index and/or write (unknown hash)"); return; }
6582+ TyKind kt = ty_hash_key(rt);
6583+ TyKind vt = ty_hash_val(rt);
6584+ emit_indent(b, indent);
6585+ buf_printf(b, "{ %s _t%d = ", c_type_name(rt), ta); emit_expr(c, recv, b);
6586+ buf_printf(b, "; %s _t%d = ", c_type_name(kt), tb); emit_hash_key(c, argv[0], kt, b);
6587+ buf_puts(b, "; ");
6588+ if (vt == TY_POLY) {
6589+ buf_printf(b, "if (%ssp_poly_truthy(sp_%sHash_get(_t%d, _t%d))) sp_%sHash_set(_t%d, _t%d, ",
6590+ is_or ? "!" : "", hn, ta, tb, hn, ta, tb);
6591+ emit_boxed(c, v, b);
6592+ buf_puts(b, ")");
6593+ }
6594+ else {
6595+ buf_printf(b, "if (%ssp_%sHash_has_key(_t%d, _t%d)) sp_%sHash_set(_t%d, _t%d, ",
6596+ is_or ? "!" : "", hn, ta, tb, hn, ta, tb);
6597+ emit_expr(c, v, b);
6598+ buf_puts(b, ")");
6599+ }
6600+ buf_puts(b, "; }\n");
6601+ return;
6602+ }
6603+
6604+ if (ty_is_array(rt)) {
6605+ const char *k = array_kind(rt);
6606+ if (!k) { unsupported(c, id, "index and/or write (array kind)"); return; }
6607+ emit_indent(b, indent);
6608+ buf_printf(b, "{ %s _t%d = ", c_type_name(rt), ta); emit_expr(c, recv, b);
6609+ buf_printf(b, "; mrb_int _t%d = ", tb); emit_expr(c, argv[0], b);
6610+ buf_puts(b, "; ");
6611+ if (rt == TY_INT_ARRAY) {
6612+ buf_printf(b, "if (%ssp_IntArray_get(_t%d, _t%d) != SP_INT_NIL) sp_IntArray_set(_t%d, _t%d, ",
6613+ is_or ? "!" : "", ta, tb, ta, tb);
6614+ emit_expr(c, v, b);
6615+ buf_puts(b, ")");
6616+ }
6617+ else if (rt == TY_STR_ARRAY) {
6618+ buf_printf(b, "if (%ssp_StrArray_get(_t%d, _t%d)) sp_StrArray_set(_t%d, _t%d, ",
6619+ is_or ? "!" : "", ta, tb, ta, tb);
6620+ emit_expr(c, v, b);
6621+ buf_puts(b, ")");
6622+ }
6623+ else if (rt == TY_POLY_ARRAY) {
6624+ buf_printf(b, "if (%ssp_poly_truthy(sp_PolyArray_get(_t%d, _t%d))) sp_PolyArray_set(_t%d, _t%d, ",
6625+ is_or ? "!" : "", ta, tb, ta, tb);
6626+ emit_boxed(c, v, b);
6627+ buf_puts(b, ")");
6628+ }
6629+ else {
6630+ unsupported(c, id, "index and/or write (array type)"); return;
6631+ }
6632+ buf_puts(b, "; }\n");
6633+ return;
6634+ }
6635+
6636+ unsupported(c, id, is_or ? "index-or-write" : "index-and-write");
6637+}
6638+
64786639 static int scope_has_return(Compiler *c, int scope_idx) {
64796640 for (int id = 0; id < c->nt->count; id++) {
64806641 const char *ty = nt_type(c->nt, id);
@@ -9968,6 +10129,8 @@
996810129 return;
996910130 }
997010131 if (!strcmp(ty, "IndexOperatorWriteNode")) { emit_index_op_write(c, id, b, indent); return; }
10132+ if (!strcmp(ty, "IndexAndWriteNode")) { emit_index_and_or_write(c, id, b, indent, 0); return; }
10133+ if (!strcmp(ty, "IndexOrWriteNode")) { emit_index_and_or_write(c, id, b, indent, 1); return; }
997110134 if (!strcmp(ty, "IfNode")) { emit_if(c, id, b, indent, 0, 0); return; }
997210135 if (!strcmp(ty, "UnlessNode")) { emit_if(c, id, b, indent, 1, 0); return; }
997310136 if (!strcmp(ty, "WhileNode")) { emit_while(c, id, b, indent, 0); return; }