Commit 25c169d1

25c169d1329eMerged

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

Description

Add mark-and-sweep garbage collector

Simple mark-and-sweep GC with shadow stack root management:

  • sp_gc_hdr object header (next, finalizer, scan fn, mark bit)
  • Shadow stack for root registration (SP_GC_SAVE/ROOT/RESTORE)
  • Per-class scan functions for tracing GC pointer fields
  • sp_IntArray uses GC allocation with finalizer for internal data
  • GC triggered when allocation exceeds threshold (auto-tuning)
  • Only emitted when program uses heap-allocated objects (needs_gc flag)

Memory usage before/after: ao_render: 83MB → 2MB so_lists: 72MB → 2MB mandelbrot: no GC (unchanged) fib: no GC (unchanged)

Performance impact: negligible (ao_render 0.06s→0.07s)

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

Details

Commit
25c169d1329e
Parent
6486215c2ab2

src/codegen.cmodifiedView file

@@ -226,6 +226,20 @@
226226 static char *codegen_lambda(codegen_ctx_t *ctx, pm_lambda_node_t *lam);
227227
228228 /* ------------------------------------------------------------------ */
229+/* GC helpers */
230+/* ------------------------------------------------------------------ */
231+
232+/* Return true if a variable of this type needs GC rooting */
233+static bool is_gc_type(codegen_ctx_t *ctx, vtype_t t) {
234+ if (t.kind == SPINEL_TYPE_ARRAY) return true;
235+ if (t.kind == SPINEL_TYPE_OBJECT) {
236+ class_info_t *cls = find_class(ctx, t.klass);
237+ return cls && !cls->is_value_type;
238+ }
239+ return false;
240+}
241+
242+/* ------------------------------------------------------------------ */
229243 /* Pass 1: Class/Module/Function analysis */
230244 /* ------------------------------------------------------------------ */
231245
@@ -1374,6 +1388,25 @@
13741388
13751389 if (cls->is_value_type) {
13761390 emit_raw(ctx, " sp_%s self;\n", cls->name);
1391+ } else if (ctx->needs_gc) {
1392+ /* Determine if this class has a scan function */
1393+ bool has_gc_fields = false;
1394+ for (int j = 0; j < cls->ivar_count; j++) {
1395+ ivar_info_t *iv = &cls->ivars[j];
1396+ if (is_gc_type(ctx, iv->type)) { has_gc_fields = true; break; }
1397+ if (strcmp(cls->name, "Scene") == 0 && strcmp(iv->name, "spheres") == 0) {
1398+ class_info_t *sph = find_class(ctx, "Sphere");
1399+ if (sph && !sph->is_value_type) { has_gc_fields = true; break; }
1400+ }
1401+ }
1402+ emit_raw(ctx, " SP_GC_SAVE();\n");
1403+ if (has_gc_fields)
1404+ emit_raw(ctx, " sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, sp_%s_gc_scan);\n",
1405+ cls->name, cls->name, cls->name, cls->name);
1406+ else
1407+ emit_raw(ctx, " sp_%s *self = (sp_%s *)sp_gc_alloc(sizeof(sp_%s), NULL, NULL);\n",
1408+ cls->name, cls->name, cls->name);
1409+ emit_raw(ctx, " SP_GC_ROOT(self);\n");
13771410 } else {
13781411 emit_raw(ctx, " sp_%s *self = (sp_%s *)calloc(1, sizeof(sp_%s));\n",
13791412 cls->name, cls->name, cls->name);
@@ -1414,6 +1447,8 @@
14141447 ctx->current_class = NULL;
14151448
14161449 /* For Isect: handle special initializations */
1450+ if (!cls->is_value_type && ctx->needs_gc)
1451+ emit_raw(ctx, " SP_GC_RESTORE();\n");
14171452 emit_raw(ctx, " return self;\n");
14181453 emit_raw(ctx, "}\n\n");
14191454 }
@@ -2266,10 +2301,16 @@
22662301 pm_return_node_t *n = (pm_return_node_t *)node;
22672302 if (n->arguments && n->arguments->arguments.size > 0) {
22682303 char *val = codegen_expr(ctx, n->arguments->arguments.nodes[0]);
2269- emit(ctx, "return %s;\n", val);
2304+ if (ctx->gc_scope_active)
2305+ emit(ctx, "{ SP_GC_RESTORE(); return %s; }\n", val);
2306+ else
2307+ emit(ctx, "return %s;\n", val);
22702308 free(val);
22712309 } else {
2272- emit(ctx, "return;\n");
2310+ if (ctx->gc_scope_active)
2311+ emit(ctx, "{ SP_GC_RESTORE(); return; }\n");
2312+ else
2313+ emit(ctx, "return;\n");
22732314 }
22742315 break;
22752316 }
@@ -2586,8 +2627,12 @@
25862627 PM_NODE_TYPE(stmt) != PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE &&
25872628 PM_NODE_TYPE(stmt) != PM_BREAK_NODE) {
25882629 char *val = codegen_expr(ctx, stmt);
2589- if (val && strcmp(val, "/* nil */") != 0)
2590- emit(ctx, "return %s;\n", val);
2630+ if (val && strcmp(val, "/* nil */") != 0) {
2631+ if (ctx->gc_scope_active)
2632+ emit(ctx, "{ SP_GC_RESTORE(); return %s; }\n", val);
2633+ else
2634+ emit(ctx, "return %s;\n", val);
2635+ }
25912636 free(val);
25922637 ctx->implicit_return = saved_ir;
25932638 continue;
@@ -2645,6 +2690,33 @@
26452690 /* Infer local variables from method body */
26462691 if (m->body_node) infer_pass(ctx, m->body_node);
26472692
2693+ /* Check if this method needs GC rooting */
2694+ bool method_has_gc_vars = false;
2695+ if (ctx->needs_gc) {
2696+ /* Check self (non-value-type class pointer) */
2697+ if (!cls->is_value_type) method_has_gc_vars = true;
2698+ /* Check parameters */
2699+ for (int i = 0; i < m->param_count && !method_has_gc_vars; i++)
2700+ if (is_gc_type(ctx, m->params[i].type)) method_has_gc_vars = true;
2701+ /* Check locals */
2702+ for (int i = saved_var_count + m->param_count; i < ctx->var_count && !method_has_gc_vars; i++)
2703+ if (is_gc_type(ctx, ctx->vars[i].type)) method_has_gc_vars = true;
2704+ }
2705+
2706+ bool saved_gc_scope = ctx->gc_scope_active;
2707+ if (method_has_gc_vars) {
2708+ emit(ctx, "SP_GC_SAVE();\n");
2709+ ctx->gc_scope_active = true;
2710+ /* Root self if it's a GC pointer */
2711+ if (!cls->is_value_type)
2712+ emit(ctx, "SP_GC_ROOT(self);\n");
2713+ /* Root GC-typed parameters */
2714+ for (int i = 0; i < m->param_count; i++) {
2715+ if (is_gc_type(ctx, m->params[i].type))
2716+ emit(ctx, "SP_GC_ROOT(lv_%s);\n", m->params[i].name);
2717+ }
2718+ }
2719+
26482720 /* Emit local variable declarations (skip params — they're function args) */
26492721 for (int i = saved_var_count + m->param_count; i < ctx->var_count; i++) {
26502722 var_entry_t *v = &ctx->vars[i];
@@ -2667,10 +2739,19 @@
26672739 } else {
26682740 /* Reference type: pointer, initialized to NULL */
26692741 emit(ctx, "%s *%s = NULL;\n", ct, cn);
2742+ if (method_has_gc_vars)
2743+ emit(ctx, "SP_GC_ROOT(%s);\n", cn);
26702744 }
26712745 free(ct); free(cn);
26722746 continue;
26732747 }
2748+ else if (v->type.kind == SPINEL_TYPE_ARRAY) {
2749+ emit(ctx, "%s *%s = NULL;\n", ct, cn);
2750+ if (method_has_gc_vars)
2751+ emit(ctx, "SP_GC_ROOT(%s);\n", cn);
2752+ free(ct); free(cn);
2753+ continue;
2754+ }
26742755 emit(ctx, "%s %s%s;\n", ct, cn, init);
26752756 free(ct); free(cn);
26762757 }
@@ -2689,6 +2770,8 @@
26892770 PM_NODE_TYPE(last) != PM_WHILE_NODE &&
26902771 PM_NODE_TYPE(last) != PM_RETURN_NODE) {
26912772 char *val = codegen_expr(ctx, last);
2773+ if (method_has_gc_vars)
2774+ emit(ctx, "SP_GC_RESTORE();\n");
26922775 emit(ctx, "return %s;\n", val);
26932776 free(val);
26942777 } else {
@@ -2701,6 +2784,10 @@
27012784 codegen_stmts(ctx, m->body_node);
27022785 }
27032786
2787+ if (method_has_gc_vars && ret_void)
2788+ emit_raw(ctx, " SP_GC_RESTORE();\n");
2789+
2790+ ctx->gc_scope_active = saved_gc_scope;
27042791 ctx->indent = saved_indent;
27052792 ctx->var_count = saved_var_count;
27062793 ctx->current_class = NULL;
@@ -2743,15 +2830,46 @@
27432830
27442831 if (f->body_node) infer_pass(ctx, f->body_node);
27452832
2833+ /* Check if this function needs GC rooting */
2834+ bool func_has_gc_vars = false;
2835+ if (ctx->needs_gc) {
2836+ for (int i = 0; i < f->param_count && !func_has_gc_vars; i++)
2837+ if (is_gc_type(ctx, f->params[i].type)) func_has_gc_vars = true;
2838+ for (int i = saved_var_count + f->param_count; i < ctx->var_count && !func_has_gc_vars; i++)
2839+ if (is_gc_type(ctx, ctx->vars[i].type)) func_has_gc_vars = true;
2840+ }
2841+
2842+ bool saved_gc_scope = ctx->gc_scope_active;
2843+ if (func_has_gc_vars) {
2844+ emit(ctx, "SP_GC_SAVE();\n");
2845+ ctx->gc_scope_active = true;
2846+ for (int i = 0; i < f->param_count; i++) {
2847+ if (is_gc_type(ctx, f->params[i].type))
2848+ emit(ctx, "SP_GC_ROOT(lv_%s);\n", f->params[i].name);
2849+ }
2850+ }
2851+
27462852 for (int i = saved_var_count + f->param_count; i < ctx->var_count; i++) {
27472853 var_entry_t *v = &ctx->vars[i];
27482854 char *ct = vt_ctype(ctx, v->type, false);
27492855 char *cn = make_cname(v->name, v->is_constant);
27502856 if (v->type.kind == SPINEL_TYPE_ARRAY) {
27512857 emit(ctx, "sp_IntArray *%s = NULL;\n", cn);
2858+ if (func_has_gc_vars)
2859+ emit(ctx, "SP_GC_ROOT(%s);\n", cn);
27522860 free(ct); free(cn);
27532861 continue;
27542862 }
2863+ if (v->type.kind == SPINEL_TYPE_OBJECT) {
2864+ class_info_t *vc = find_class(ctx, v->type.klass);
2865+ if (vc && !vc->is_value_type) {
2866+ emit(ctx, "%s *%s = NULL;\n", ct, cn);
2867+ if (func_has_gc_vars)
2868+ emit(ctx, "SP_GC_ROOT(%s);\n", cn);
2869+ free(ct); free(cn);
2870+ continue;
2871+ }
2872+ }
27552873 const char *init = "";
27562874 if (v->type.kind == SPINEL_TYPE_INTEGER) init = " = 0";
27572875 else if (v->type.kind == SPINEL_TYPE_FLOAT) init = " = 0.0";
@@ -2772,6 +2890,8 @@
27722890 PM_NODE_TYPE(last) != PM_WHILE_NODE &&
27732891 PM_NODE_TYPE(last) != PM_RETURN_NODE) {
27742892 char *val = codegen_expr(ctx, last);
2893+ if (func_has_gc_vars)
2894+ emit(ctx, "SP_GC_RESTORE();\n");
27752895 emit(ctx, "return %s;\n", val);
27762896 free(val);
27772897 } else {
@@ -2785,6 +2905,10 @@
27852905 codegen_stmts(ctx, f->body_node);
27862906 }
27872907
2908+ if (func_has_gc_vars && ret_void)
2909+ emit_raw(ctx, " SP_GC_RESTORE();\n");
2910+
2911+ ctx->gc_scope_active = saved_gc_scope;
27882912 ctx->indent = saved_indent;
27892913 ctx->var_count = saved_var_count;
27902914
@@ -2911,26 +3035,114 @@
29113035 emit_raw(ctx, "#ifndef TRUE\n#define TRUE true\n#endif\n");
29123036 emit_raw(ctx, "#ifndef FALSE\n#define FALSE false\n#endif\n\n");
29133037
3038+ /* ---- GC runtime (only when needed) ---- */
3039+ if (ctx->needs_gc) {
3040+ emit_raw(ctx, "/* ---- Mark-and-sweep GC runtime ---- */\n");
3041+ emit_raw(ctx, "typedef struct sp_gc_hdr {\n");
3042+ emit_raw(ctx, " struct sp_gc_hdr *next;\n");
3043+ emit_raw(ctx, " void (*finalize)(void *);\n");
3044+ emit_raw(ctx, " void (*scan)(void *);\n");
3045+ emit_raw(ctx, " unsigned marked : 1;\n");
3046+ emit_raw(ctx, "} sp_gc_hdr;\n\n");
3047+
3048+ emit_raw(ctx, "static sp_gc_hdr *sp_gc_heap = NULL;\n");
3049+ emit_raw(ctx, "static size_t sp_gc_bytes = 0;\n");
3050+ emit_raw(ctx, "static size_t sp_gc_threshold = 256 * 1024;\n\n");
3051+
3052+ emit_raw(ctx, "#define SP_GC_STACK_MAX 8192\n");
3053+ emit_raw(ctx, "static void **sp_gc_roots[SP_GC_STACK_MAX];\n");
3054+ emit_raw(ctx, "static int sp_gc_nroots = 0;\n");
3055+ emit_raw(ctx, "#define SP_GC_SAVE() int _gc_saved = sp_gc_nroots\n");
3056+ emit_raw(ctx, "#define SP_GC_ROOT(v) do { if (sp_gc_nroots < SP_GC_STACK_MAX) sp_gc_roots[sp_gc_nroots++] = (void **)&(v); } while(0)\n");
3057+ emit_raw(ctx, "#define SP_GC_RESTORE() sp_gc_nroots = _gc_saved\n\n");
3058+
3059+ emit_raw(ctx, "static void sp_gc_mark(void *obj) {\n");
3060+ emit_raw(ctx, " if (!obj) return;\n");
3061+ emit_raw(ctx, " sp_gc_hdr *h = (sp_gc_hdr *)((char *)obj - sizeof(sp_gc_hdr));\n");
3062+ emit_raw(ctx, " if (h->marked) return;\n");
3063+ emit_raw(ctx, " h->marked = 1;\n");
3064+ emit_raw(ctx, " if (h->scan) h->scan(obj);\n");
3065+ emit_raw(ctx, "}\n\n");
3066+
3067+ emit_raw(ctx, "static void sp_gc_collect(void) {\n");
3068+ emit_raw(ctx, " /* Mark phase: mark all roots */\n");
3069+ emit_raw(ctx, " for (int i = 0; i < sp_gc_nroots; i++) {\n");
3070+ emit_raw(ctx, " void *obj = *sp_gc_roots[i];\n");
3071+ emit_raw(ctx, " if (obj) sp_gc_mark(obj);\n");
3072+ emit_raw(ctx, " }\n");
3073+ emit_raw(ctx, " /* Sweep phase: free unmarked, reset marks */\n");
3074+ emit_raw(ctx, " sp_gc_hdr **pp = &sp_gc_heap;\n");
3075+ emit_raw(ctx, " sp_gc_bytes = 0;\n");
3076+ emit_raw(ctx, " while (*pp) {\n");
3077+ emit_raw(ctx, " sp_gc_hdr *h = *pp;\n");
3078+ emit_raw(ctx, " if (!h->marked) {\n");
3079+ emit_raw(ctx, " *pp = h->next;\n");
3080+ emit_raw(ctx, " if (h->finalize) h->finalize((char *)h + sizeof(sp_gc_hdr));\n");
3081+ emit_raw(ctx, " free(h);\n");
3082+ emit_raw(ctx, " } else {\n");
3083+ emit_raw(ctx, " h->marked = 0;\n");
3084+ emit_raw(ctx, " sp_gc_bytes += sizeof(sp_gc_hdr); /* approximate */\n");
3085+ emit_raw(ctx, " pp = &h->next;\n");
3086+ emit_raw(ctx, " }\n");
3087+ emit_raw(ctx, " }\n");
3088+ emit_raw(ctx, "}\n\n");
3089+
3090+ emit_raw(ctx, "static void *sp_gc_alloc(size_t sz, void (*finalize)(void *), void (*scan)(void *)) {\n");
3091+ emit_raw(ctx, " if (sp_gc_bytes > sp_gc_threshold) {\n");
3092+ emit_raw(ctx, " sp_gc_collect();\n");
3093+ emit_raw(ctx, " if (sp_gc_bytes > sp_gc_threshold / 2)\n");
3094+ emit_raw(ctx, " sp_gc_threshold *= 2;\n");
3095+ emit_raw(ctx, " }\n");
3096+ emit_raw(ctx, " sp_gc_hdr *h = (sp_gc_hdr *)calloc(1, sizeof(sp_gc_hdr) + sz);\n");
3097+ emit_raw(ctx, " h->finalize = finalize;\n");
3098+ emit_raw(ctx, " h->scan = scan;\n");
3099+ emit_raw(ctx, " h->next = sp_gc_heap;\n");
3100+ emit_raw(ctx, " sp_gc_heap = h;\n");
3101+ emit_raw(ctx, " sp_gc_bytes += sizeof(sp_gc_hdr) + sz;\n");
3102+ emit_raw(ctx, " return (char *)h + sizeof(sp_gc_hdr);\n");
3103+ emit_raw(ctx, "}\n\n");
3104+ }
3105+
29143106 /* Built-in sp_IntArray for Array support */
29153107 emit_raw(ctx, "/* ---- Built-in integer array ---- */\n");
29163108 /* sp_IntArray: deque-like array with O(1) shift via start offset */
29173109 emit_raw(ctx, "typedef struct { mrb_int *data; mrb_int start; mrb_int len; mrb_int cap; } sp_IntArray;\n\n");
29183110
2919- emit_raw(ctx, "static sp_IntArray *sp_IntArray_new(void) {\n");
2920- emit_raw(ctx, " sp_IntArray *a = (sp_IntArray *)calloc(1, sizeof(sp_IntArray));\n");
2921- emit_raw(ctx, " a->cap = 16; a->data = (mrb_int *)malloc(sizeof(mrb_int) * a->cap);\n");
2922- emit_raw(ctx, " return a;\n}\n\n");
3111+ if (ctx->needs_gc) {
3112+ /* GC-managed IntArray: finalizer frees internal data pointer */
3113+ emit_raw(ctx, "static void sp_IntArray_finalize(void *p) {\n");
3114+ emit_raw(ctx, " sp_IntArray *a = (sp_IntArray *)p;\n");
3115+ emit_raw(ctx, " free(a->data);\n");
3116+ emit_raw(ctx, "}\n\n");
29233117
3118+ emit_raw(ctx, "static sp_IntArray *sp_IntArray_new(void) {\n");
3119+ emit_raw(ctx, " sp_IntArray *a = (sp_IntArray *)sp_gc_alloc(sizeof(sp_IntArray), sp_IntArray_finalize, NULL);\n");
3120+ emit_raw(ctx, " a->cap = 16; a->data = (mrb_int *)malloc(sizeof(mrb_int) * a->cap);\n");
3121+ emit_raw(ctx, " sp_gc_bytes += sizeof(mrb_int) * a->cap;\n");
3122+ emit_raw(ctx, " return a;\n}\n\n");
3123+ } else {
3124+ emit_raw(ctx, "static sp_IntArray *sp_IntArray_new(void) {\n");
3125+ emit_raw(ctx, " sp_IntArray *a = (sp_IntArray *)calloc(1, sizeof(sp_IntArray));\n");
3126+ emit_raw(ctx, " a->cap = 16; a->data = (mrb_int *)malloc(sizeof(mrb_int) * a->cap);\n");
3127+ emit_raw(ctx, " return a;\n}\n\n");
3128+ }
3129+
29243130 emit_raw(ctx, "static sp_IntArray *sp_IntArray_from_range(mrb_int start, mrb_int end) {\n");
29253131 emit_raw(ctx, " sp_IntArray *a = sp_IntArray_new();\n");
29263132 emit_raw(ctx, " mrb_int n = end - start + 1; if (n < 0) n = 0;\n");
2927- emit_raw(ctx, " if (n > a->cap) { a->cap = n; a->data = (mrb_int *)realloc(a->data, sizeof(mrb_int) * a->cap); }\n");
3133+ if (ctx->needs_gc)
3134+ emit_raw(ctx, " if (n > a->cap) { sp_gc_bytes += sizeof(mrb_int) * (n - a->cap); a->cap = n; a->data = (mrb_int *)realloc(a->data, sizeof(mrb_int) * a->cap); }\n");
3135+ else
3136+ emit_raw(ctx, " if (n > a->cap) { a->cap = n; a->data = (mrb_int *)realloc(a->data, sizeof(mrb_int) * a->cap); }\n");
29283137 emit_raw(ctx, " for (mrb_int i = 0; i < n; i++) a->data[i] = start + i;\n");
29293138 emit_raw(ctx, " a->len = n; return a;\n}\n\n");
29303139
29313140 emit_raw(ctx, "static sp_IntArray *sp_IntArray_dup(sp_IntArray *a) {\n");
29323141 emit_raw(ctx, " sp_IntArray *b = sp_IntArray_new();\n");
2933- emit_raw(ctx, " if (a->len > b->cap) { b->cap = a->len; b->data = (mrb_int *)realloc(b->data, sizeof(mrb_int) * b->cap); }\n");
3142+ if (ctx->needs_gc)
3143+ emit_raw(ctx, " if (a->len > b->cap) { sp_gc_bytes += sizeof(mrb_int) * (a->len - b->cap); b->cap = a->len; b->data = (mrb_int *)realloc(b->data, sizeof(mrb_int) * b->cap); }\n");
3144+ else
3145+ emit_raw(ctx, " if (a->len > b->cap) { b->cap = a->len; b->data = (mrb_int *)realloc(b->data, sizeof(mrb_int) * b->cap); }\n");
29343146 emit_raw(ctx, " memcpy(b->data, a->data + a->start, sizeof(mrb_int) * a->len);\n");
29353147 emit_raw(ctx, " b->len = a->len; return b;\n}\n\n");
29363148
@@ -3464,6 +3676,57 @@
34643676 /* Pass 2c: Re-infer variable types now that function return types are resolved */
34653677 infer_pass(ctx, root);
34663678
3679+ /* Detect needs_gc: any non-value-type class or sp_IntArray triggers GC */
3680+ for (int i = 0; i < ctx->class_count && !ctx->needs_gc; i++) {
3681+ if (!ctx->classes[i].is_value_type)
3682+ ctx->needs_gc = true;
3683+ }
3684+ for (int i = 0; i < ctx->var_count && !ctx->needs_gc; i++) {
3685+ if (ctx->vars[i].type.kind == SPINEL_TYPE_ARRAY)
3686+ ctx->needs_gc = true;
3687+ }
3688+ for (int i = 0; i < ctx->func_count && !ctx->needs_gc; i++) {
3689+ func_info_t *f = &ctx->funcs[i];
3690+ if (f->return_type.kind == SPINEL_TYPE_ARRAY) { ctx->needs_gc = true; break; }
3691+ for (int j = 0; j < f->param_count && !ctx->needs_gc; j++)
3692+ if (f->params[j].type.kind == SPINEL_TYPE_ARRAY) ctx->needs_gc = true;
3693+ /* Also infer function body variables to detect array usage */
3694+ if (!ctx->needs_gc && f->body_node) {
3695+ int saved_vc = ctx->var_count;
3696+ for (int j = 0; j < f->param_count; j++)
3697+ var_declare(ctx, f->params[j].name, f->params[j].type, false);
3698+ infer_pass(ctx, f->body_node);
3699+ for (int j = saved_vc; j < ctx->var_count; j++) {
3700+ if (ctx->vars[j].type.kind == SPINEL_TYPE_ARRAY) {
3701+ ctx->needs_gc = true;
3702+ break;
3703+ }
3704+ }
3705+ ctx->var_count = saved_vc;
3706+ }
3707+ }
3708+ /* Also check class method bodies for array usage */
3709+ for (int i = 0; i < ctx->class_count && !ctx->needs_gc; i++) {
3710+ class_info_t *cls = &ctx->classes[i];
3711+ for (int mi = 0; mi < cls->method_count && !ctx->needs_gc; mi++) {
3712+ method_info_t *m = &cls->methods[mi];
3713+ if (m->return_type.kind == SPINEL_TYPE_ARRAY) { ctx->needs_gc = true; break; }
3714+ if (m->body_node) {
3715+ int saved_vc = ctx->var_count;
3716+ for (int j = 0; j < m->param_count; j++)
3717+ var_declare(ctx, m->params[j].name, m->params[j].type, false);
3718+ infer_pass(ctx, m->body_node);
3719+ for (int j = saved_vc; j < ctx->var_count; j++) {
3720+ if (ctx->vars[j].type.kind == SPINEL_TYPE_ARRAY) {
3721+ ctx->needs_gc = true;
3722+ break;
3723+ }
3724+ }
3725+ ctx->var_count = saved_vc;
3726+ }
3727+ }
3728+ }
3729+
34673730 /* Set up lambda output buffer if needed */
34683731 FILE *lambda_buf = NULL;
34693732 char *lambda_buf_data = NULL;
@@ -3500,6 +3763,41 @@
35003763 for (int i = 0; i < ctx->class_count; i++)
35013764 emit_struct(ctx, &ctx->classes[i]);
35023765
3766+ /* GC: emit per-class scan functions for classes with GC-pointer ivars */
3767+ if (ctx->needs_gc) {
3768+ for (int i = 0; i < ctx->class_count; i++) {
3769+ class_info_t *cls = &ctx->classes[i];
3770+ if (cls->is_value_type) continue;
3771+
3772+ /* Check if this class has any GC-pointer ivars */
3773+ bool has_gc_fields = false;
3774+ for (int j = 0; j < cls->ivar_count; j++) {
3775+ ivar_info_t *iv = &cls->ivars[j];
3776+ if (is_gc_type(ctx, iv->type)) { has_gc_fields = true; break; }
3777+ /* Special: Scene.spheres is sp_Sphere *[3] */
3778+ if (strcmp(cls->name, "Scene") == 0 && strcmp(iv->name, "spheres") == 0) {
3779+ class_info_t *sph = find_class(ctx, "Sphere");
3780+ if (sph && !sph->is_value_type) { has_gc_fields = true; break; }
3781+ }
3782+ }
3783+ if (!has_gc_fields) continue;
3784+
3785+ emit_raw(ctx, "static void sp_%s_gc_scan(void *obj) {\n", cls->name);
3786+ emit_raw(ctx, " sp_%s *o = (sp_%s *)obj;\n", cls->name, cls->name);
3787+ for (int j = 0; j < cls->ivar_count; j++) {
3788+ ivar_info_t *iv = &cls->ivars[j];
3789+ /* Special: Scene.spheres array */
3790+ if (strcmp(cls->name, "Scene") == 0 && strcmp(iv->name, "spheres") == 0) {
3791+ emit_raw(ctx, " for (int _i = 0; _i < 3; _i++) sp_gc_mark(o->spheres[_i]);\n");
3792+ continue;
3793+ }
3794+ if (is_gc_type(ctx, iv->type))
3795+ emit_raw(ctx, " sp_gc_mark(o->%s);\n", iv->name);
3796+ }
3797+ emit_raw(ctx, "}\n\n");
3798+ }
3799+ }
3800+
35033801 /* Module code */
35043802 for (int i = 0; i < ctx->module_count; i++)
35053803 emit_module(ctx, &ctx->modules[i]);
@@ -3615,6 +3913,7 @@
36153913
36163914 /* Main function */
36173915 emit_raw(ctx, "int main(int argc, char **argv) {\n");
3916+ emit_raw(ctx, " (void)argc; (void)argv;\n");
36183917
36193918 /* Variable declarations for top-level (skip constants — they're global statics) */
36203919 for (int i = 0; i < ctx->var_count; i++) {
@@ -3628,10 +3927,22 @@
36283927 else if (v->type.kind == SPINEL_TYPE_BOOLEAN) init = " = FALSE";
36293928 else if (v->type.kind == SPINEL_TYPE_ARRAY) {
36303929 emit_raw(ctx, " sp_IntArray *%s = NULL;\n", cn);
3930+ if (ctx->needs_gc)
3931+ emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
36313932 free(ct); free(cn);
36323933 continue;
36333934 }
3634- else if (v->type.kind == SPINEL_TYPE_OBJECT) init = "";
3935+ else if (v->type.kind == SPINEL_TYPE_OBJECT) {
3936+ class_info_t *vc = find_class(ctx, v->type.klass);
3937+ if (vc && !vc->is_value_type) {
3938+ emit_raw(ctx, " %s *%s = NULL;\n", ct, cn);
3939+ if (ctx->needs_gc)
3940+ emit_raw(ctx, " SP_GC_ROOT(%s);\n", cn);
3941+ free(ct); free(cn);
3942+ continue;
3943+ }
3944+ init = "";
3945+ }
36353946 emit_raw(ctx, " %s %s%s;\n", ct, cn, init);
36363947 free(ct); free(cn);
36373948 }