Commit 119d3440

119d3440390fMerged

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

Description

Value-type objects (Stage 2): arg-carrying constructors

Lift the Stage 1 no-arg-constructor restriction: emit_value_obj_new now forwards constructor arguments to the in-place initialize call (sp_X_initialize(&_t, args...)), so a scalar-ivar immutable leaf with an arg-taking initialize (e.g. Point.new(x, y)) is stack-allocated too.

Because the stack temp lives in the construction frame, a value-type instance must not escape it. Added exclusions for the new escape routes a constructor's reach exposes: a class that is a method's return type, that is built inside a poly-returning method (boxed at the poly return), or that is a block's result (collected into a boxed array via map/collect). All box or dangle a stack pointer, so those classes stay heap-allocated.

bm_object_new_no_escape 28x -> 1x vs legacy. make test 845, make bench 57, optcarrot checksum 59662, legacy self-host fixpoint all green. (object_new_init still heap: its class has a string ivar and is returned -- needs string-ivar value types + by-value return, later stages.)

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

Details

Commit
119d3440390f
Parent
15a8e52f90c8

src/analyze.cmodifiedView file

@@ -1497,11 +1497,6 @@
14971497 for (int j = 0; j < c->nclasses; j++)
14981498 if (c->classes[j].parent == i) { has_sub = 1; break; }
14991499 if (has_sub) continue;
1500- /* Stage 1: only no-arg constructors. Then every `X.new` is 0-arg and can be
1501- stack-allocated uniformly, so the (unrooted) instance pointer is always a
1502- stack address — never a mix of stack and heap that would break GC rooting. */
1503- int initm = comp_method_in_chain(c, i, "initialize", NULL);
1504- if (initm >= 0 && c->scopes[initm].nparams > 0) continue;
15051500 ci->is_value_type = 1; /* tentative; disqualified below */
15061501 }
15071502 for (int id = 0; id < c->nt->count; id++) {
@@ -1559,5 +1554,35 @@
15591554 }
15601555 }
15611556 }
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+ }
1565+ /* An instance built inside a poly-returning method is liable to be boxed at
1566+ the (poly) return -- sp_box_obj would carry a stack pointer. And an
1567+ instance that is a block's result is collected (map/collect -> a boxed
1568+ array). Both box a value, so exclude such classes. */
1569+ for (int id = 0; id < c->nt->count; id++) {
1570+ TyKind t = comp_ntype(c, id);
1571+ if (!ty_is_object(t)) continue;
1572+ int q = ty_object_class(t);
1573+ if (q < 0 || q >= c->nclasses || !c->classes[q].is_value_type) continue;
1574+ Scope *s = comp_scope_of(c, id);
1575+ if (s && s->ret == TY_POLY) c->classes[q].is_value_type = 0;
1576+ }
1577+ for (int id = 0; id < c->nt->count; id++) {
1578+ const char *ty = nt_type(c->nt, id);
1579+ if (!ty || strcmp(ty, "BlockNode")) continue;
1580+ int body = nt_ref(c->nt, id, "body");
1581+ if (body < 0) continue;
1582+ int n = 0; const int *st = nt_arr(c->nt, body, "body", &n);
1583+ if (n <= 0) continue;
1584+ TyKind lt = comp_ntype(c, st[n - 1]);
1585+ if (ty_is_object(lt)) { int q = ty_object_class(lt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1586+ }
15621587 }
15631588