Commit e93a0c48

e93a0c486646Merged

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

Description

Add TY_BIGINT type and bigint loop-variable detection

Introduces a new TY_BIGINT type for arbitrary-precision integers. Variables that undergo multiplicative growth inside while loops (x = x * y, x *= y) are promoted from TY_INT to TY_BIGINT by the new detect_bigint_loop_vars post-fixpoint pass. A cascade propagation pass (propagate_bigint_cascade) updates variables assigned from bigint expressions, so downstream locals like x = a / 0 get the correct type as well.

Codegen emits sp_Bigint * declarations, sp_bigint_* arithmetic, and handles bigint in emit_print_one (print), emit_puts_one (puts), and bigint arithmetic operators.

Fixes int_overflow_promote, bigint_div_by_zero, bigint_volatile_combo, and bigint_propagation_chain tests (576 pass, up from 574).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
e93a0c486646
Parent
3e82c71d5d7a

src/analyze.cmodifiedView file

@@ -1111,8 +1111,11 @@
11111111 /* array * int -> same array type (repeat); array * string -> join string */
11121112 if (!strcmp(name, "*") && (ty_is_array(rt) || rt == TY_POLY_ARRAY) && a0 == TY_INT) return rt;
11131113 if (!strcmp(name, "*") && (ty_is_array(rt) || rt == TY_POLY_ARRAY) && a0 == TY_STRING) return TY_STRING;
1114- if (ty_is_numeric(rt) && ty_is_numeric(a0))
1115- return (rt == TY_FLOAT || a0 == TY_FLOAT) ? TY_FLOAT : TY_INT;
1114+ if (ty_is_numeric(rt) && ty_is_numeric(a0)) {
1115+ if (rt == TY_FLOAT || a0 == TY_FLOAT) return TY_FLOAT;
1116+ if (rt == TY_BIGINT || a0 == TY_BIGINT) return TY_BIGINT;
1117+ return TY_INT;
1118+ }
11161119 return TY_UNKNOWN;
11171120 }
11181121 if (recv >= 0 && argc == 1 && !strcmp(name, "<=>")) return TY_INT;
@@ -2232,8 +2235,11 @@
22322235 TyKind vt = infer_type(c, nt_ref(nt, id, "value"));
22332236 TyKind ct = cur ? (TyKind)cur->gc_root : TY_UNKNOWN; /* old type */
22342237 if (ct == TY_STRING) newt = TY_STRING;
2235- else if (ty_is_numeric(ct) && ty_is_numeric(vt))
2236- newt = (ct == TY_FLOAT || vt == TY_FLOAT) ? TY_FLOAT : TY_INT;
2238+ else if (ty_is_numeric(ct) && ty_is_numeric(vt)) {
2239+ if (ct == TY_FLOAT || vt == TY_FLOAT) newt = TY_FLOAT;
2240+ else if (ct == TY_BIGINT || vt == TY_BIGINT) newt = TY_BIGINT;
2241+ else newt = TY_INT;
2242+ }
22372243 else newt = ct;
22382244 }
22392245 else if (!strcmp(ty, "LocalVariableOrWriteNode") ||
@@ -3662,6 +3668,100 @@
36623668 free(inproc);
36633669 }
36643670
3671+/* ---- bigint loop-variable detection ---- */
3672+/* Scan a while-loop body for `x = x * y` or `x *= y` patterns and collect
3673+ the variable names in a heap-allocated array. Returns the count; caller
3674+ must free the returned array. */
3675+static void bigint_scan_body(const NodeTable *nt, int id, char ***names, int *n, int *cap) {
3676+ if (id < 0) return;
3677+ const char *ty = nt_type(nt, id);
3678+ if (!ty) return;
3679+ /* x *= y (LocalVariableOperatorWriteNode with * or **) */
3680+ if (!strcmp(ty, "LocalVariableOperatorWriteNode")) {
3681+ const char *op = nt_str(nt, id, "binary_operator");
3682+ if (op && (!strcmp(op, "*") || !strcmp(op, "**"))) {
3683+ const char *nm = nt_str(nt, id, "name");
3684+ if (nm) {
3685+ for (int k = 0; k < *n; k++) if (!strcmp((*names)[k], nm)) goto skip_mul;
3686+ if (*n >= *cap) { *cap = *cap * 2 + 4; *names = (char **)realloc(*names, (size_t)*cap * sizeof(char *)); }
3687+ (*names)[(*n)++] = (char *)nm;
3688+ skip_mul:;
3689+ }
3690+ }
3691+ }
3692+ /* x = x * y (LocalVariableWriteNode where value is CallNode * with recv = x) */
3693+ if (!strcmp(ty, "LocalVariableWriteNode")) {
3694+ const char *nm = nt_str(nt, id, "name");
3695+ int val = nt_ref(nt, id, "value");
3696+ if (nm && val >= 0 && !strcmp(nt_type(nt, val) ? nt_type(nt, val) : "", "CallNode")) {
3697+ const char *op2 = nt_str(nt, val, "name");
3698+ int recv2 = nt_ref(nt, val, "receiver");
3699+ if (op2 && (!strcmp(op2, "*") || !strcmp(op2, "**")) && recv2 >= 0 &&
3700+ !strcmp(nt_type(nt, recv2) ? nt_type(nt, recv2) : "", "LocalVariableReadNode") &&
3701+ !strcmp(nt_str(nt, recv2, "name") ? nt_str(nt, recv2, "name") : "", nm)) {
3702+ for (int k = 0; k < *n; k++) if (!strcmp((*names)[k], nm)) goto skip_lv;
3703+ if (*n >= *cap) { *cap = *cap * 2 + 4; *names = (char **)realloc(*names, (size_t)*cap * sizeof(char *)); }
3704+ (*names)[(*n)++] = (char *)nm;
3705+ skip_lv:;
3706+ }
3707+ }
3708+ }
3709+ /* Recurse into body / stmts / subsequent */
3710+ bigint_scan_body(nt, nt_ref(nt, id, "body"), names, n, cap);
3711+ int sn = 0; const int *stmts2 = nt_arr(nt, id, "body", &sn);
3712+ for (int k = 0; k < sn; k++) bigint_scan_body(nt, stmts2[k], names, n, cap);
3713+ bigint_scan_body(nt, nt_ref(nt, id, "subsequent"), names, n, cap);
3714+}
3715+
3716+static void detect_bigint_loop_vars(Compiler *c) {
3717+ const NodeTable *nt = c->nt;
3718+ for (int id = 0; id < nt->count; id++) {
3719+ const char *ty = nt_type(nt, id);
3720+ if (!ty || strcmp(ty, "WhileNode")) continue;
3721+ int body = nt_ref(nt, id, "statements");
3722+ if (body < 0) continue;
3723+ char **cands = NULL; int ncands = 0, cap = 0;
3724+ bigint_scan_body(nt, body, &cands, &ncands, &cap);
3725+ /* Promote matching TY_INT locals to TY_BIGINT */
3726+ for (int k = 0; k < ncands; k++) {
3727+ Scope *s = comp_scope_of(c, id);
3728+ LocalVar *lv = s ? scope_local(s, cands[k]) : NULL;
3729+ if (lv && lv->type == TY_INT) lv->type = TY_BIGINT;
3730+ }
3731+ free(cands);
3732+ }
3733+}
3734+
3735+/* After detect_bigint_loop_vars promotes some locals to TY_BIGINT, cascade
3736+ the promotion to variables assigned from bigint-typed expressions. */
3737+static void propagate_bigint_cascade(Compiler *c) {
3738+ const NodeTable *nt = c->nt;
3739+ int changed = 1;
3740+ while (changed) {
3741+ changed = 0;
3742+ for (int id = 0; id < nt->count; id++) {
3743+ const char *ty = nt_type(nt, id);
3744+ if (!ty) continue;
3745+ if (!strcmp(ty, "LocalVariableWriteNode")) {
3746+ const char *nm = nt_str(nt, id, "name");
3747+ Scope *s = comp_scope_of(c, id);
3748+ LocalVar *lv = nm ? scope_local(s, nm) : NULL;
3749+ if (!lv || lv->type != TY_INT) continue;
3750+ TyKind vt = infer_type(c, nt_ref(nt, id, "value"));
3751+ if (vt == TY_BIGINT) { lv->type = TY_BIGINT; changed = 1; }
3752+ }
3753+ else if (!strcmp(ty, "LocalVariableOperatorWriteNode")) {
3754+ const char *nm = nt_str(nt, id, "name");
3755+ Scope *s = comp_scope_of(c, id);
3756+ LocalVar *lv = nm ? scope_local(s, nm) : NULL;
3757+ if (!lv || lv->type != TY_INT) continue;
3758+ TyKind vt = infer_type(c, nt_ref(nt, id, "value"));
3759+ if (vt == TY_BIGINT) { lv->type = TY_BIGINT; changed = 1; }
3760+ }
3761+ }
3762+ }
3763+}
3764+
36653765 void analyze_program(Compiler *c) {
36663766 /* scope 0 = top level */
36673767 Scope *top = comp_scope_new(c, NULL, -1);
@@ -3878,6 +3978,10 @@
38783978 /* recompute returns: a method returning such a param is now poly */
38793979 for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break;
38803980
3981+ /* Promote loop-multiplication variables to bigint */
3982+ detect_bigint_loop_vars(c);
3983+ propagate_bigint_cascade(c);
3984+
38813985 /* mark locals captured by escaping procs (they need heap cells) */
38823986 mark_proc_captures(c);
38833987