Commit bfc8ed7b
bfc8ed7bf537Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · ~8 modified
Description
Support poly receivers and template methods needed by optcarrot
Several inference and codegen gaps surfaced compiling optcarrot's APU/PPU.
None change existing behavior (make test 845, make bench 57 still green);
each adds a path that previously hit unsupported:
- sp_poly_replace / sp_poly_pack: Array#replace / #pack on a nullable-array (poly) receiver, dispatched on the runtime tag.
- unary
~(bitwise complement) on int/poly: was unhandled in both infer and codegen. - poly numeric predicates even?/odd?/zero?/positive?/negative?: coerce to int.
- poly local bitwise op-assign (
x >>= v,x &= v, ...): the int result is re-boxed into the poly slot. - IndexOrWriteNode / IndexAndWriteNode (
h[k] ||= v,h[k] &&= v) now drive empty-{}hash promotion likeh[k] = valready did. <<on a numeric-assigned ivar is a shift, not Array#push: don't promote such a slot to an array (which would unify with int writes to poly).- uniq/uniq! block params get the element (or poly) type, plus a post-fixpoint fallback typing any still-unknown block param as poly.
- Template-method dispatch: an implicit-self call to a method defined only in subclasses now resolves virtually on self's runtime class. emit_dispatch forces the cls_id switch when there is no base impl and emits a typed placeholder default arm instead of calling a nonexistent sp__.
- unsupported() prints the receiver node type and inferred types to speed diagnosis.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- bfc8ed7bf537
- Parent
- 5de531fb630c
lib/sp_runtime.hmodifiedView file
@@ -3418,6 +3418,31 @@
34183418
}34193419
}34203420
}3421+
/* Array#replace(other): replace recv's contents with other's, returning recv.3422+
recv keeps the same boxed pointer (the underlying array is mutated in place),3423+
so a nullable-array slot typed poly stays valid. A nil/non-array recv is a3424+
no-op (the call sites that reach a nil poly are dead-guarded in Ruby). */3425+
static sp_RbVal sp_poly_replace(sp_RbVal recv, sp_RbVal src) {3426+
if (recv.tag != SP_TAG_OBJ || src.tag != SP_TAG_OBJ) return recv;3427+
if (recv.cls_id == SP_BUILTIN_INT_ARRAY && src.cls_id == SP_BUILTIN_INT_ARRAY)3428+
sp_IntArray_replace((sp_IntArray *)recv.v.p, (sp_IntArray *)src.v.p);3429+
else if (recv.cls_id == SP_BUILTIN_FLT_ARRAY && src.cls_id == SP_BUILTIN_FLT_ARRAY)3430+
sp_FloatArray_replace((sp_FloatArray *)recv.v.p, (sp_FloatArray *)src.v.p);3431+
else if (recv.cls_id == SP_BUILTIN_STR_ARRAY && src.cls_id == SP_BUILTIN_STR_ARRAY)3432+
sp_StrArray_replace((sp_StrArray *)recv.v.p, (sp_StrArray *)src.v.p);3433+
else if (recv.cls_id == SP_BUILTIN_POLY_ARRAY) {3434+
sp_PolyArray *d = (sp_PolyArray *)recv.v.p;3435+
d->len = 0;3436+
switch (src.cls_id) {3437+
case SP_BUILTIN_INT_ARRAY: { sp_IntArray *s = (sp_IntArray *)src.v.p; for (mrb_int i = 0; i < s->len; i++) sp_PolyArray_push(d, sp_box_int(s->data[s->start + i])); break; }3438+
case SP_BUILTIN_FLT_ARRAY: { sp_FloatArray *s = (sp_FloatArray *)src.v.p; for (mrb_int i = 0; i < s->len; i++) sp_PolyArray_push(d, sp_box_float(s->data[i])); break; }3439+
case SP_BUILTIN_STR_ARRAY: { sp_StrArray *s = (sp_StrArray *)src.v.p; for (mrb_int i = 0; i < s->len; i++) sp_PolyArray_push(d, sp_box_str(s->data[i])); break; }3440+
case SP_BUILTIN_POLY_ARRAY: { sp_PolyArray *s = (sp_PolyArray *)src.v.p; for (mrb_int i = 0; i < s->len; i++) sp_PolyArray_push(d, s->data[i]); break; }3441+
default: break;3442+
}3443+
}3444+
return recv;3445+
}34213446
static sp_PolyArray *sp_PolyArray_slice_bang(sp_PolyArray *a, mrb_int from, mrb_int n) {34223447
if (!a) return sp_PolyArray_new();34233448
if (a->frozen) { sp_raise_frozen_array(); return sp_PolyArray_new(); }@@ -5424,6 +5449,16 @@
54245449
const char *sp_PolyArray_pack(sp_PolyArray *arr, const char *fmt);54255450
sp_PolyArray *sp_str_unpack(const char *str, const char *fmt);54265451
5452+
/* Array#pack on a poly (nullable-array) receiver: dispatch on the runtime tag.5453+
A nil/non-array recv packs to the empty string. */5454+
static inline const char *sp_poly_pack(sp_RbVal recv, const char *fmt) {5455+
if (recv.tag == SP_TAG_OBJ && recv.cls_id == SP_BUILTIN_INT_ARRAY)5456+
return sp_IntArray_pack((sp_IntArray *)recv.v.p, fmt);5457+
if (recv.tag == SP_TAG_OBJ && recv.cls_id == SP_BUILTIN_POLY_ARRAY)5458+
return sp_PolyArray_pack((sp_PolyArray *)recv.v.p, fmt);5459+
return "";5460+
}5461+
54275462
/* ---- StringScanner (linked from sp_strscan.o) ----54285463
GC-allocated; the source / matched fields are marked by54295464
sp_StringScanner_scan_gc through the shim layer. */src/analyze.cmodifiedView file
Loading diff…
src/analyze_infer.cmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_fold.cmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…