Commit bd123579

bd123579b145Merged

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

Description

Migrate objects from value structs to heap references

Objects are now heap-allocated (sp_ *) with reference semantics, matching Ruby (and the legacy backend's pool objects), instead of value structs copied on every assignment/parameter pass.

  • emit_ctype(object) -> sp_ *; declare_local / default_value / needs_root treat objects as GC-rooted heap pointers (init NULL).
  • sp__new heap-allocates via sp_gc_alloc(sizeof, NULL, scan), GC-roots self, sets cls_id, calls initialize (self is already a pointer), returns the pointer.
  • Per-class GC scan function (emit_class_scan) marks heap ivars (sp_mark_string / sp_gc_mark); classes with only scalar ivars get a NULL scan.
  • Call sites: attr reader/writer use (recv)->iv_; method dispatch passes the receiver pointer directly (no &recv / value copy); SelfNode -> the self pointer; inline self-binding and puts/p-via-to_s pass the pointer.

Fixes forward_call_param_type_inference (mutating an object passed as a method parameter now persists to the caller). Inheritance, virtual dispatch, and chaining (Counter) still match CRuby. make test: 39 (up from 37; only int_overflow_promote remains failing).

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

Details

Commit
bd123579b145
Parent
68efb64e4f89

src/codegen.cmodifiedView file

@@ -115,14 +115,14 @@
115115 case TY_INT_ARRAY:
116116 case TY_FLOAT_ARRAY:
117117 case TY_STR_ARRAY: return "NULL";
118- default: return ty_is_hash(t) ? "NULL" : "0";
118+ default: return (ty_is_hash(t) || ty_is_object(t)) ? "NULL" : "0";
119119 }
120120 }
121121 /* Append the C type name for `t` to `b` (objects need the class name). */
122122 static void emit_ctype(Compiler *c, TyKind t, Buf *b) {
123123 if (ty_is_object(t)) {
124124 int cid = ty_object_class(t);
125- buf_printf(b, "sp_%s", c->classes[cid].name);
125+ buf_printf(b, "sp_%s *", c->classes[cid].name); /* objects are heap pointers (reference semantics) */
126126 }
127127 else {
128128 const char *n = c_type_name(t);
@@ -598,19 +598,19 @@
598598 int cid = ty_object_class(rt);
599599 /* attr reader -> field access (recv).iv_x */
600600 if (comp_reader_in_chain(c, cid, name, NULL)) {
601- buf_puts(b, "("); emit_expr(c, recv, b); buf_printf(b, ").iv_%s", name);
601+ buf_puts(b, "("); emit_expr(c, recv, b); buf_printf(b, ")->iv_%s", name);
602602 return;
603603 }
604604 int mi = comp_method_in_chain(c, cid, name, NULL);
605605 if (mi >= 0) {
606- /* receiver pointer: &lvalue, or a temp for an rvalue receiver */
606+ /* receiver is a pointer; reuse it directly if it's a simple lvalue,
607+ else stash in a temp (the virtual-dispatch switch references it
608+ multiple times) */
607609 char selfptr[64];
608610 const char *rty = nt_type(nt, recv);
609611 if (rty && (!strcmp(rty, "LocalVariableReadNode") || !strcmp(rty, "InstanceVariableReadNode") || !strcmp(rty, "SelfNode"))) {
610612 Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
611- char tmp[64];
612- snprintf(tmp, sizeof tmp, "&%s", rb.p ? rb.p : "");
613- snprintf(selfptr, sizeof selfptr, "%s", tmp);
613+ snprintf(selfptr, sizeof selfptr, "%s", rb.p ? rb.p : "");
614614 free(rb.p);
615615 }
616616 else {
@@ -620,7 +620,7 @@
620620 buf_printf(g_pre, " _t%d = ", t);
621621 Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
622622 buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p);
623- snprintf(selfptr, sizeof selfptr, "&_t%d", t);
623+ snprintf(selfptr, sizeof selfptr, "_t%d", t);
624624 }
625625 emit_dispatch(c, cid, name, selfptr, nt_ref(nt, id, "arguments"), b);
626626 return;
@@ -1087,20 +1087,12 @@
10871087
10881088 if (as_expr) buf_puts(b, "({\n");
10891089 else { emit_indent(b, indent); buf_puts(b, "{\n"); }
1090- /* instance method: bind self to the receiver */
1090+ /* instance method: bind self to the receiver (a pointer) */
10911091 if (recv >= 0) {
10921092 int st = ++g_tmp;
1093- const char *rty = nt_type(nt, recv);
10941093 emit_indent(b, indent + 1);
1095- if (rty && (!strcmp(rty, "LocalVariableReadNode") || !strcmp(rty, "InstanceVariableReadNode") || !strcmp(rty, "SelfNode"))) {
1096- buf_printf(b, "sp_%s *_t%d = &", c->classes[recv_class].name, st);
1097- emit_expr(c, recv, b);
1098- }
1099- else {
1100- buf_printf(b, "sp_%s _t%da = ", c->classes[recv_class].name, st);
1101- emit_expr(c, recv, b);
1102- buf_printf(b, "; sp_%s *_t%d = &_t%da", c->classes[recv_class].name, st, st);
1103- }
1094+ buf_printf(b, "sp_%s *_t%d = ", c->classes[recv_class].name, st);
1095+ emit_expr(c, recv, b);
11041096 buf_puts(b, ";\n");
11051097 snprintf(selfbuf, sizeof selfbuf, "_t%d", st);
11061098 g_self = selfbuf;
@@ -1432,7 +1424,7 @@
14321424 buf_puts(b, "})");
14331425 return;
14341426 }
1435- if (!strcmp(ty, "SelfNode")) { buf_printf(b, "(*%s)", g_self); return; }
1427+ if (!strcmp(ty, "SelfNode")) { buf_puts(b, g_self); return; } /* self is the object reference (pointer) */
14361428 if (!strcmp(ty, "InstanceVariableReadNode")) {
14371429 const char *nm = nt_str(nt, id, "name"); /* "@x" */
14381430 buf_printf(b, "%s->iv_%s", g_self, nm + 1);
@@ -1571,7 +1563,7 @@
15711563 buf_printf(b, "sp_%s_to_s(", c->classes[cid].name);
15721564 const char *rty = nt_type(c->nt, arg);
15731565 if (rty && (!strcmp(rty, "LocalVariableReadNode") || !strcmp(rty, "InstanceVariableReadNode") || !strcmp(rty, "SelfNode"))) {
1574- buf_puts(b, "&"); emit_expr(c, arg, b);
1566+ emit_expr(c, arg, b);
15751567 }
15761568 else {
15771569 int tt = ++g_tmp;
@@ -1579,7 +1571,7 @@
15791571 buf_printf(g_pre, " _t%d = ", tt);
15801572 Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, arg, &rb);
15811573 buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p);
1582- buf_printf(b, "&_t%d", tt);
1574+ buf_printf(b, "_t%d", tt);
15831575 }
15841576 buf_puts(b, ")); if (_ps) fputs(_ps, stdout); if (!_ps || !*_ps || _ps[strlen(_ps)-1] != '\\n') putchar('\\n'); }\n");
15851577 }
@@ -1968,7 +1960,7 @@
19681960 int an = 0; const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
19691961 if (an >= 1) {
19701962 emit_indent(b, indent);
1971- buf_puts(b, "("); emit_expr(c, recv, b); buf_printf(b, ").iv_%s = ", base);
1963+ buf_puts(b, "("); emit_expr(c, recv, b); buf_printf(b, ")->iv_%s = ", base);
19721964 emit_expr(c, argv[0], b); buf_puts(b, ";\n");
19731965 return;
19741966 }
@@ -2163,7 +2155,7 @@
21632155 /* ---- declarations ---- */
21642156
21652157 /* Heap-managed types need a GC root for their local slot. */
2166-static int needs_root(TyKind t) { return t == TY_STRING || ty_is_array(t) || ty_is_hash(t); }
2158+static int needs_root(TyKind t) { return t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t); }
21672159
21682160 static void declare_local(Compiler *c, Buf *b, LocalVar *lv) {
21692161 switch (lv->type) {
@@ -2194,8 +2186,9 @@
21942186 }
21952187 if (ty_is_object(lv->type)) {
21962188 buf_puts(b, " ");
2197- emit_ctype(c, lv->type, b);
2198- buf_printf(b, " lv_%s = {0};\n", lv->name);
2189+ emit_ctype(c, lv->type, b); /* "sp_<Class> *" */
2190+ buf_printf(b, " lv_%s = NULL;\n", lv->name);
2191+ buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name);
21992192 break;
22002193 }
22012194 fprintf(stderr, "spinelc: local '%s' has unsupported type %s\n",
@@ -2273,7 +2266,7 @@
22732266 else {
22742267 emit_stmts_tail(c, s->body, b, 1);
22752268 buf_puts(b, " return ");
2276- if (ty_is_object(s->ret)) { buf_puts(b, "("); emit_ctype(c, s->ret, b); buf_puts(b, "){0};\n"); /* unreachable default */ }
2269+ if (ty_is_object(s->ret)) { buf_puts(b, "NULL;\n"); /* unreachable default (object pointer) */ }
22772270 else buf_printf(b, "%s;\n", default_value(s->ret));
22782271 }
22792272 buf_puts(b, "}\n");
@@ -2295,11 +2288,36 @@
22952288 buf_puts(b, "};\n");
22962289 }
22972290
2291+/* A class needs a GC scan iff any ivar holds a heap reference. */
2292+static int class_needs_scan(ClassInfo *ci) {
2293+ for (int i = 0; i < ci->nivars; i++) {
2294+ TyKind t = ci->ivar_types[i];
2295+ if (t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t)) return 1;
2296+ }
2297+ return 0;
2298+}
2299+
2300+/* Emit the GC scan function (marks heap ivars) for a class that needs one. */
2301+static void emit_class_scan(Compiler *c, ClassInfo *ci, Buf *b) {
2302+ (void)c;
2303+ if (!class_needs_scan(ci)) return;
2304+ buf_printf(b, "static void sp_%s_scan(void *p) {\n", ci->name);
2305+ buf_printf(b, " sp_%s *o = (sp_%s *)p;\n", ci->name, ci->name);
2306+ for (int i = 0; i < ci->nivars; i++) {
2307+ TyKind t = ci->ivar_types[i];
2308+ const char *iv = ci->ivars[i] + 1;
2309+ if (t == TY_STRING) buf_printf(b, " sp_mark_string(o->iv_%s);\n", iv);
2310+ else if (ty_is_array(t) || ty_is_hash(t) || ty_is_object(t))
2311+ buf_printf(b, " if (o->iv_%s) sp_gc_mark(o->iv_%s);\n", iv, iv);
2312+ }
2313+ buf_puts(b, "}\n");
2314+}
2315+
22982316 static void emit_class_new(Compiler *c, ClassInfo *ci, Buf *b) {
22992317 int cid = comp_class_index(c, ci->name);
23002318 int initcls = cid;
23012319 int init = comp_method_in_chain(c, cid, "initialize", &initcls);
2302- buf_printf(b, "static sp_%s sp_%s_new(", ci->name, ci->name);
2320+ buf_printf(b, "static sp_%s *sp_%s_new(", ci->name, ci->name);
23032321 if (init >= 0 && c->scopes[init].nparams > 0) {
23042322 Scope *s = &c->scopes[init];
23052323 for (int i = 0; i < s->nparams; i++) {
@@ -2312,13 +2330,16 @@
23122330 else {
23132331 buf_puts(b, "void");
23142332 }
2315- buf_printf(b, ") {\n sp_%s self = {0};\n", ci->name);
2316- buf_printf(b, " self.cls_id = %d;\n", cid);
2333+ buf_printf(b, ") {\n sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, %s%s%s);\n",
2334+ ci->name, ci->name, ci->name,
2335+ class_needs_scan(ci) ? "sp_" : "", class_needs_scan(ci) ? ci->name : "NULL",
2336+ class_needs_scan(ci) ? "_scan" : "");
2337+ buf_printf(b, " SP_GC_ROOT(self);\n");
2338+ buf_printf(b, " self->cls_id = %d;\n", cid);
23172339 if (init >= 0) {
2318- /* an inherited initialize takes the parent's self type */
23192340 buf_printf(b, " sp_%s_initialize(", c->classes[initcls].name);
23202341 if (initcls != cid) buf_printf(b, "(sp_%s *)", c->classes[initcls].name);
2321- buf_puts(b, "&self");
2342+ buf_puts(b, "self");
23222343 Scope *s = &c->scopes[init];
23232344 for (int i = 0; i < s->nparams; i++) buf_printf(b, ", lv_%s", s->pnames[i]);
23242345 buf_puts(b, ");\n");
@@ -2372,15 +2393,16 @@
23722393 }
23732394 buf_puts(&b, "static const char *sp_class_to_s(sp_Class c){(void)c;return \"\";}\n\n\n");
23742395
2375- /* class structs */
2396+ /* class structs + GC scan functions */
23762397 for (int i = 0; i < c->nclasses; i++) emit_class_struct(c, &c->classes[i], &b);
2398+ for (int i = 0; i < c->nclasses; i++) emit_class_scan(c, &c->classes[i], &b);
23772399 if (c->nclasses > 0) buf_puts(&b, "\n");
23782400
23792401 /* method prototypes (scope 0 is top-level) */
23802402 for (int s = 1; s < c->nscopes; s++) { if (c->scopes[s].yields) continue; emit_method_signature(c, &c->scopes[s], &b); buf_puts(&b, ";\n"); }
23812403 /* constructor prototypes + definitions (after method protos: new calls initialize) */
23822404 for (int i = 0; i < c->nclasses; i++) {
2383- buf_printf(&b, "static sp_%s sp_%s_new();\n", c->classes[i].name, c->classes[i].name);
2405+ buf_printf(&b, "static sp_%s *sp_%s_new();\n", c->classes[i].name, c->classes[i].name);
23842406 }
23852407 if (c->nscopes > 1 || c->nclasses > 0) buf_puts(&b, "\n");
23862408 for (int i = 0; i < c->nclasses; i++) emit_class_new(c, &c->classes[i], &b);