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
for (int j = 0; j < c->nclasses; j++) if (c->classes[j].parent == i) { has_sub = 1; break; } if (has_sub) continue; /* Stage 1: only no-arg constructors. Then every `X.new` is 0-arg and can be stack-allocated uniformly, so the (unrooted) instance pointer is always a stack address — never a mix of stack and heap that would break GC rooting. */ int initm = comp_method_in_chain(c, i, "initialize", NULL); if (initm >= 0 && c->scopes[initm].nparams > 0) continue; ci->is_value_type = 1; /* tentative; disqualified below */ } for (int id = 0; id < c->nt->count; id++) { } } } /* A value-type instance returned from a method would be a pointer into that method's frame -- it dangles once the method returns. Exclude any class that is some method's return type. (Returning by value would need the full value representation; that is a later stage.) */ for (int s = 0; s < c->nscopes; s++) { TyKind rt = c->scopes[s].ret; if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; } } /* An instance built inside a poly-returning method is liable to be boxed at the (poly) return -- sp_box_obj would carry a stack pointer. And an instance that is a block's result is collected (map/collect -> a boxed array). Both box a value, so exclude such classes. */ for (int id = 0; id < c->nt->count; id++) { TyKind t = comp_ntype(c, id); if (!ty_is_object(t)) continue; int q = ty_object_class(t); if (q < 0 || q >= c->nclasses || !c->classes[q].is_value_type) continue; Scope *s = comp_scope_of(c, id); if (s && s->ret == TY_POLY) c->classes[q].is_value_type = 0; } for (int id = 0; id < c->nt->count; id++) { const char *ty = nt_type(c->nt, id); if (!ty || strcmp(ty, "BlockNode")) continue; int body = nt_ref(c->nt, id, "body"); if (body < 0) continue; int n = 0; const int *st = nt_arr(c->nt, body, "body", &n); if (n <= 0) continue; TyKind lt = comp_ntype(c, st[n - 1]); if (ty_is_object(lt)) { int q = ty_object_class(lt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; } }}src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…