Commit 93bd2c07

93bd2c0718feMerged

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

Description

Value-type objects (Stage 4): full value representation

Replace the Stage 1/2 stack-temp-pointer hack with a true value representation: a value-type class is emitted as sp_X (by value) for locals, params, returns, and fields; sp_X_new returns the struct by value; reader methods take self by value (initialize keeps a pointer to populate fields). This supersedes the stack-temp model and, crucially, lets value-type instances be returned by value (a copy), so return is no longer an escape that forces heap allocation.

Mechanics: emit_ctype/new/_new-prototype emit sp_X; method self-param is by-value for readers; g_self_deref switches ivar access to . in value-self bodies; emit_dispatch passes a value receiver without the pointer cast; the attr-reader inline, .class, object truthiness, and local-decl init all handle the value form. Detection drops the return-escape exclusion (return-by-value is safe now) and adds exclusions for the contexts that still box or need a stable pointer: instance_eval/ exec receivers, bare method(:x) self-capture, and tuple (POLY_ARRAY) returns.

object_new 1x and object_new_no_escape 1x vs legacy (unchanged from Stage 1/2, now on the value model). make test 845, make bench 57, optcarrot checksum 59662. (object_new_init still needs string-ivar value types -- next.)

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

Details

Commit
93bd2c0718fe
Parent
119d3440390f

src/analyze.cmodifiedView file

@@ -1528,6 +1528,18 @@
15281528 TyKind rt = comp_ntype(c, recv);
15291529 if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
15301530 }
1531+ /* `method(:foo)` with no receiver captures `self`; a bound Method needs a
1532+ stable heap pointer, so the enclosing class can't be a value type. */
1533+ if (nm && !strcmp(nm, "method") && recv < 0) {
1534+ Scope *s = comp_scope_of(c, id);
1535+ if (s && s->class_id >= 0 && s->class_id < c->nclasses) c->classes[s->class_id].is_value_type = 0;
1536+ }
1537+ /* instance_eval/exec lifts the block body into a method on the receiver
1538+ that needs a by-pointer self; exclude such receivers. */
1539+ if (nm && (!strcmp(nm, "instance_eval") || !strcmp(nm, "instance_exec")) && recv >= 0) {
1540+ TyKind rt = comp_ntype(c, recv);
1541+ if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1542+ }
15311543 int args = nt_ref(c->nt, id, "arguments"); int an = 0;
15321544 const int *av = args >= 0 ? nt_arr(c->nt, args, "arguments", &an) : NULL;
15331545 for (int k = 0; k < an; k++) {
@@ -1554,14 +1566,6 @@
15541566 }
15551567 }
15561568 }
1557- /* A value-type instance returned from a method would be a pointer into that
1558- method's frame -- it dangles once the method returns. Exclude any class
1559- that is some method's return type. (Returning by value would need the
1560- full value representation; that is a later stage.) */
1561- for (int s = 0; s < c->nscopes; s++) {
1562- TyKind rt = c->scopes[s].ret;
1563- if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1564- }
15651569 /* An instance built inside a poly-returning method is liable to be boxed at
15661570 the (poly) return -- sp_box_obj would carry a stack pointer. And an
15671571 instance that is a block's result is collected (map/collect -> a boxed
@@ -1572,7 +1576,9 @@
15721576 int q = ty_object_class(t);
15731577 if (q < 0 || q >= c->nclasses || !c->classes[q].is_value_type) continue;
15741578 Scope *s = comp_scope_of(c, id);
1575- if (s && s->ret == TY_POLY) c->classes[q].is_value_type = 0;
1579+ /* poly scalar return boxes; a tuple return (POLY_ARRAY, e.g. `return a, b, c`)
1580+ boxes each element. Either way a value instance here would be boxed. */
1581+ if (s && (s->ret == TY_POLY || s->ret == TY_POLY_ARRAY)) c->classes[q].is_value_type = 0;
15761582 }
15771583 for (int id = 0; id < c->nt->count; id++) {
15781584 const char *ty = nt_type(c->nt, id);