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
TyKind rt = comp_ntype(c, recv); if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; } } /* `method(:foo)` with no receiver captures `self`; a bound Method needs a stable heap pointer, so the enclosing class can't be a value type. */ if (nm && !strcmp(nm, "method") && recv < 0) { Scope *s = comp_scope_of(c, id); if (s && s->class_id >= 0 && s->class_id < c->nclasses) c->classes[s->class_id].is_value_type = 0; } /* instance_eval/exec lifts the block body into a method on the receiver that needs a by-pointer self; exclude such receivers. */ if (nm && (!strcmp(nm, "instance_eval") || !strcmp(nm, "instance_exec")) && recv >= 0) { TyKind rt = comp_ntype(c, recv); if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; } } int args = nt_ref(c->nt, id, "arguments"); int an = 0; const int *av = args >= 0 ? nt_arr(c->nt, args, "arguments", &an) : NULL; for (int k = 0; k < an; k++) { } } } /* 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 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; /* poly scalar return boxes; a tuple return (POLY_ARRAY, e.g. `return a, b, c`) boxes each element. Either way a value instance here would be boxed. */ if (s && (s->ret == TY_POLY || s->ret == TY_POLY_ARRAY)) c->classes[q].is_value_type = 0; } for (int id = 0; id < c->nt->count; id++) { const char *ty = nt_type(c->nt, id);src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_expr.cmodifiedView file
Loading diff…
src/codegen_fold.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…