Commit ac474542
ac474542cb8dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified
Description
Add arithmetic operations on sp_RbValue (POLY) variables
When binary operators (+, -, , /, >, <, >=, <=, ==, !=) are used on POLY operands, generate sp_poly_ runtime dispatch functions that handle IntInt, FloatFloat, String+String, and StringInt at runtime.
- Emit sp_poly_add/sub/mul/div (return sp_RbValue) and sp_poly_gt/lt/ge/le/eq/neq (return mrb_bool) when needs_poly is true
- Type inference: POLY binop → POLY result; POLY comparison → BOOLEAN
- Codegen: box non-POLY operands before calling sp_poly_
- to_s on POLY receiver generates sp_poly_to_s(val)
- Widen int+float param types to POLY (not FLOAT) to preserve type info
- Sanitize Ruby ? and ! in function names for valid C identifiers
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ac474542cb8d
- Parent
- 48014e9ed8dd
src/codegen.cmodifiedView file
@@ -40,6 +40,21 @@
4040
return buf;4141
}4242
43+
/* Sanitize a Ruby identifier for use in C code: ? → _p, ! → _b, = → _eq */44+
static char *c_safe_name(const char *name) {45+
size_t len = strlen(name);46+
char *buf = malloc(len * 3 + 1);47+
size_t j = 0;48+
for (size_t i = 0; i < len; i++) {49+
if (name[i] == '?') { buf[j++] = '_'; buf[j++] = 'p'; }50+
else if (name[i] == '!') { buf[j++] = '_'; buf[j++] = 'b'; }51+
else if (name[i] == '=') { buf[j++] = '_'; buf[j++] = 'e'; buf[j++] = 'q'; }52+
else buf[j++] = name[i];53+
}54+
buf[j] = '\0';55+
return buf;56+
}57+
4358
/* ------------------------------------------------------------------ */4459
/* Constant pool helpers */4560
/* ------------------------------------------------------------------ */@@ -218,8 +233,11 @@
218233
}219234
220235
static func_info_t *find_func(codegen_ctx_t *ctx, const char *name) {221-
for (int i = 0; i < ctx->func_count; i++)222-
if (strcmp(ctx->funcs[i].name, name) == 0) return &ctx->funcs[i];236+
char *safe = c_safe_name(name);237+
for (int i = 0; i < ctx->func_count; i++) {238+
if (strcmp(ctx->funcs[i].name, safe) == 0) { free(safe); return &ctx->funcs[i]; }239+
}240+
free(safe);223241
return NULL;224242
}225243
@@ -818,8 +836,9 @@
818836
func_info_t *f = &ctx->funcs[ctx->func_count++];819837
memset(f, 0, sizeof(*f));820838
char *name = cstr(ctx, def->name);821-
snprintf(f->name, sizeof(f->name), "%s", name);822-
free(name);839+
char *safe = c_safe_name(name);840+
snprintf(f->name, sizeof(f->name), "%s", safe);841+
free(safe); free(name);823842
f->body_node = def->body ? (pm_node_t *)def->body : NULL;824843
f->params_node = def->parameters ? (pm_node_t *)def->parameters : NULL;825844
f->return_type = vt_prim(SPINEL_TYPE_VALUE);@@ -1190,6 +1209,17 @@
11901209
free(method);11911210
return vt_prim(SPINEL_TYPE_BOOLEAN);11921211
}1212+
/* POLY binary ops: comparisons → BOOLEAN, arithmetic → POLY */1213+
if (lt.kind == SPINEL_TYPE_POLY || rt.kind == SPINEL_TYPE_POLY) {1214+
if (strcmp(method, ">") == 0 || strcmp(method, "<") == 0 ||1215+
strcmp(method, ">=") == 0 || strcmp(method, "<=") == 0 ||1216+
strcmp(method, "==") == 0 || strcmp(method, "!=") == 0) {1217+
free(method);1218+
return vt_prim(SPINEL_TYPE_BOOLEAN);1219+
}1220+
free(method);1221+
return vt_prim(SPINEL_TYPE_POLY);1222+
}11931223
if (vt_is_numeric(lt) || vt_is_numeric(rt) ||11941224
lt.kind == SPINEL_TYPE_BOOLEAN || rt.kind == SPINEL_TYPE_BOOLEAN) {11951225
result = binop_result(lt, rt, method);@@ -2334,9 +2364,7 @@
23342364
at.kind != target->params[pi].type.kind &&23352365
target->params[pi].type.kind != SPINEL_TYPE_POLY) {23362366
/* Widen: called with incompatible types → POLY */2337-
if (vt_is_numeric(target->params[pi].type) && vt_is_numeric(at))2338-
target->params[pi].type = vt_prim(SPINEL_TYPE_FLOAT);2339-
else if (vt_is_poly_eligible(target->params[pi].type) && vt_is_poly_eligible(at))2367+
if (vt_is_poly_eligible(target->params[pi].type) && vt_is_poly_eligible(at))23402368
target->params[pi].type = vt_prim(SPINEL_TYPE_POLY);23412369
/* else: leave as-is (complex types like ARRAY shouldn't become POLY) */23422370
}@@ -2373,9 +2401,7 @@
23732401
} else if (at.kind != SPINEL_TYPE_VALUE &&23742402
at.kind != target2->params[pi].type.kind &&23752403
target2->params[pi].type.kind != SPINEL_TYPE_POLY) {2376-
if (vt_is_numeric(target2->params[pi].type) && vt_is_numeric(at))2377-
target2->params[pi].type = vt_prim(SPINEL_TYPE_FLOAT);2378-
else if (vt_is_poly_eligible(target2->params[pi].type) && vt_is_poly_eligible(at))2404+
if (vt_is_poly_eligible(target2->params[pi].type) && vt_is_poly_eligible(at))23792405
target2->params[pi].type = vt_prim(SPINEL_TYPE_POLY);23802406
}23812407
}@@ -2476,9 +2502,7 @@
24762502
} else if (at.kind != SPINEL_TYPE_VALUE &&24772503
at.kind != target->params[pi].type.kind &&24782504
target->params[pi].type.kind != SPINEL_TYPE_POLY) {2479-
if (vt_is_numeric(target->params[pi].type) && vt_is_numeric(at))2480-
target->params[pi].type = vt_prim(SPINEL_TYPE_FLOAT);2481-
else if (vt_is_poly_eligible(target->params[pi].type) && vt_is_poly_eligible(at))2505+
if (vt_is_poly_eligible(target->params[pi].type) && vt_is_poly_eligible(at))24822506
target->params[pi].type = vt_prim(SPINEL_TYPE_POLY);24832507
}24842508
}@@ -3560,6 +3584,33 @@
35603584
free(left); free(right); free(method);35613585
return r;35623586
}3587+
3588+
/* POLY binary operators → sp_poly_<op> dispatch */3589+
if (c_op && (lt.kind == SPINEL_TYPE_POLY || rt.kind == SPINEL_TYPE_POLY)) {3590+
const char *poly_fn = NULL;3591+
if (strcmp(method, "+") == 0) poly_fn = "sp_poly_add";3592+
else if (strcmp(method, "-") == 0) poly_fn = "sp_poly_sub";3593+
else if (strcmp(method, "*") == 0) poly_fn = "sp_poly_mul";3594+
else if (strcmp(method, "/") == 0) poly_fn = "sp_poly_div";3595+
else if (strcmp(method, ">") == 0) poly_fn = "sp_poly_gt";3596+
else if (strcmp(method, "<") == 0) poly_fn = "sp_poly_lt";3597+
else if (strcmp(method, ">=") == 0) poly_fn = "sp_poly_ge";3598+
else if (strcmp(method, "<=") == 0) poly_fn = "sp_poly_le";3599+
else if (strcmp(method, "==") == 0) poly_fn = "sp_poly_eq";3600+
else if (strcmp(method, "!=") == 0) poly_fn = "sp_poly_neq";3601+
if (poly_fn) {3602+
char *left = codegen_expr(ctx, call->receiver);3603+
char *right = codegen_expr(ctx, call->arguments->arguments.nodes[0]);3604+
/* Box non-POLY operands */3605+
char *bl = lt.kind == SPINEL_TYPE_POLY ? xstrdup(left)3606+
: poly_box_expr_vt(ctx, lt, left);3607+
char *br = rt.kind == SPINEL_TYPE_POLY ? xstrdup(right)3608+
: poly_box_expr_vt(ctx, rt, right);3609+
char *r = sfmt("%s(%s, %s)", poly_fn, bl, br);3610+
free(left); free(right); free(bl); free(br); free(method);3611+
return r;3612+
}3613+
}35633614
}35643615
35653616
/* ARGV.length → sp_argv.len */@@ -4631,6 +4682,12 @@
46314682
free(recv); free(method);46324683
return r;46334684
}4685+
if (strcmp(method, "to_s") == 0 && recv_t.kind == SPINEL_TYPE_POLY) {4686+
char *recv = codegen_expr(ctx, call->receiver);4687+
char *r = sfmt("sp_poly_to_s(%s)", recv);4688+
free(recv); free(method);4689+
return r;4690+
}46344691
if (strcmp(method, "to_i") == 0 && recv_t.kind == SPINEL_TYPE_FLOAT) {46354692
char *recv = codegen_expr(ctx, call->receiver);46364693
char *r = sfmt("((mrb_int)%s)", recv);@@ -4803,7 +4860,7 @@
48034860
fname[copy_len] = '\0';48044861
ctx->needs_proc = true;48054862
free(method);4806-
return sfmt("sp_Proc_new((sp_block_fn)sp_%s, NULL)", fname);4863+
{ char *sf = c_safe_name(fname); char *r = sfmt("sp_Proc_new((sp_block_fn)sp_%s, NULL)", sf); free(sf); return r; }48074864
}48084865
48094866
/* rand(n) → rand() % n */@@ -4891,7 +4948,7 @@
48914948
free(param_args[i]);48924949
}48934950
}4894-
char *r = sfmt("sp_%s(%s)", method, args);4951+
char *r = sfmt("sp_%s(%s)", fn->name, args);48954952
free(args); free(method);48964953
return r;48974954
}@@ -4932,7 +4989,7 @@
49324989
free(args); args = na2;49334990
}49344991
}4935-
char *r = sfmt("sp_%s(%s)", method, args);4992+
char *r = sfmt("sp_%s(%s)", fn->name, args);49364993
free(args); free(method);49374994
return r;49384995
}@@ -5105,7 +5162,7 @@
51055162
free(args); args = na;51065163
}51075164
}5108-
char *r = sfmt("sp_%s(%s)", method, args);5165+
char *r = sfmt("sp_%s(%s)", fn->name, args);51095166
free(args); free(method);51105167
return r;51115168
}@@ -6801,7 +6858,7 @@
68016858
args = na;68026859
}68036860
emit(ctx, "sp_%s(%s%s(sp_block_fn)_blk_%d, &_env_%d);\n",6804-
method, args, argc > 0 ? ", " : "", blk_id, blk_id);6861+
fn->name, args, argc > 0 ? ", " : "", blk_id, blk_id);68056862
free(args); free(bpname); free(method);68066863
break;68076864
}@@ -7968,6 +8025,109 @@
79688025
emit_raw(ctx, " case SP_T_NIL: return \"\";\n");79698026
emit_raw(ctx, " default: return \"(object)\";\n");79708027
emit_raw(ctx, " }\n}\n\n");8028+
8029+
/* ---- Polymorphic arithmetic/comparison helpers ---- */8030+
emit_raw(ctx, "static void sp_raise(const char *msg) { fprintf(stderr, \"%%s\\n\", msg); exit(1); }\n\n");8031+
8032+
emit_raw(ctx, "static sp_RbValue sp_poly_add(sp_RbValue a, sp_RbValue b) {\n");8033+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return sp_box_int(a.i + b.i);\n");8034+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8035+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8036+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8037+
emit_raw(ctx, " return sp_box_float(fa + fb);\n");8038+
emit_raw(ctx, " }\n");8039+
emit_raw(ctx, " if (a.tag == SP_T_STRING && b.tag == SP_T_STRING)\n");8040+
emit_raw(ctx, " return sp_box_str(sp_str_concat(a.s, b.s));\n");8041+
emit_raw(ctx, " sp_raise(\"TypeError: + not defined\"); return sp_box_nil();\n");8042+
emit_raw(ctx, "}\n\n");8043+
8044+
emit_raw(ctx, "static sp_RbValue sp_poly_sub(sp_RbValue a, sp_RbValue b) {\n");8045+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return sp_box_int(a.i - b.i);\n");8046+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8047+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8048+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8049+
emit_raw(ctx, " return sp_box_float(fa - fb);\n");8050+
emit_raw(ctx, " }\n");8051+
emit_raw(ctx, " sp_raise(\"TypeError: - not defined\"); return sp_box_nil();\n");8052+
emit_raw(ctx, "}\n\n");8053+
8054+
emit_raw(ctx, "static sp_RbValue sp_poly_mul(sp_RbValue a, sp_RbValue b) {\n");8055+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return sp_box_int(a.i * b.i);\n");8056+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8057+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8058+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8059+
emit_raw(ctx, " return sp_box_float(fa * fb);\n");8060+
emit_raw(ctx, " }\n");8061+
emit_raw(ctx, " if (a.tag == SP_T_STRING && b.tag == SP_T_INT)\n");8062+
emit_raw(ctx, " return sp_box_str(sp_str_repeat(a.s, b.i));\n");8063+
emit_raw(ctx, " sp_raise(\"TypeError: * not defined\"); return sp_box_nil();\n");8064+
emit_raw(ctx, "}\n\n");8065+
8066+
emit_raw(ctx, "static sp_RbValue sp_poly_div(sp_RbValue a, sp_RbValue b) {\n");8067+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return sp_box_int(a.i / b.i);\n");8068+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8069+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8070+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8071+
emit_raw(ctx, " return sp_box_float(fa / fb);\n");8072+
emit_raw(ctx, " }\n");8073+
emit_raw(ctx, " sp_raise(\"TypeError: / not defined\"); return sp_box_nil();\n");8074+
emit_raw(ctx, "}\n\n");8075+
8076+
emit_raw(ctx, "static mrb_bool sp_poly_gt(sp_RbValue a, sp_RbValue b) {\n");8077+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return a.i > b.i;\n");8078+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8079+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8080+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8081+
emit_raw(ctx, " return fa > fb;\n");8082+
emit_raw(ctx, " }\n");8083+
emit_raw(ctx, " sp_raise(\"TypeError: > not defined\"); return 0;\n");8084+
emit_raw(ctx, "}\n\n");8085+
8086+
emit_raw(ctx, "static mrb_bool sp_poly_lt(sp_RbValue a, sp_RbValue b) {\n");8087+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return a.i < b.i;\n");8088+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8089+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8090+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8091+
emit_raw(ctx, " return fa < fb;\n");8092+
emit_raw(ctx, " }\n");8093+
emit_raw(ctx, " sp_raise(\"TypeError: < not defined\"); return 0;\n");8094+
emit_raw(ctx, "}\n\n");8095+
8096+
emit_raw(ctx, "static mrb_bool sp_poly_ge(sp_RbValue a, sp_RbValue b) {\n");8097+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return a.i >= b.i;\n");8098+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8099+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8100+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8101+
emit_raw(ctx, " return fa >= fb;\n");8102+
emit_raw(ctx, " }\n");8103+
emit_raw(ctx, " sp_raise(\"TypeError: >= not defined\"); return 0;\n");8104+
emit_raw(ctx, "}\n\n");8105+
8106+
emit_raw(ctx, "static mrb_bool sp_poly_le(sp_RbValue a, sp_RbValue b) {\n");8107+
emit_raw(ctx, " if (a.tag == SP_T_INT && b.tag == SP_T_INT) return a.i <= b.i;\n");8108+
emit_raw(ctx, " if (a.tag == SP_T_FLOAT || b.tag == SP_T_FLOAT) {\n");8109+
emit_raw(ctx, " double fa = a.tag == SP_T_FLOAT ? a.f : (double)a.i;\n");8110+
emit_raw(ctx, " double fb = b.tag == SP_T_FLOAT ? b.f : (double)b.i;\n");8111+
emit_raw(ctx, " return fa <= fb;\n");8112+
emit_raw(ctx, " }\n");8113+
emit_raw(ctx, " sp_raise(\"TypeError: <= not defined\"); return 0;\n");8114+
emit_raw(ctx, "}\n\n");8115+
8116+
emit_raw(ctx, "static mrb_bool sp_poly_eq(sp_RbValue a, sp_RbValue b) {\n");8117+
emit_raw(ctx, " if (a.tag != b.tag) return 0;\n");8118+
emit_raw(ctx, " switch (a.tag) {\n");8119+
emit_raw(ctx, " case SP_T_INT: return a.i == b.i;\n");8120+
emit_raw(ctx, " case SP_T_FLOAT: return a.f == b.f;\n");8121+
emit_raw(ctx, " case SP_T_STRING: return strcmp(a.s, b.s) == 0;\n");8122+
emit_raw(ctx, " case SP_T_BOOL: return a.i == b.i;\n");8123+
emit_raw(ctx, " case SP_T_NIL: return 1;\n");8124+
emit_raw(ctx, " default: return a.p == b.p;\n");8125+
emit_raw(ctx, " }\n");8126+
emit_raw(ctx, "}\n\n");8127+
8128+
emit_raw(ctx, "static mrb_bool sp_poly_neq(sp_RbValue a, sp_RbValue b) {\n");8129+
emit_raw(ctx, " return !sp_poly_eq(a, b);\n");8130+
emit_raw(ctx, "}\n\n");79718131
}79728132
79738133
/* ---- Regexp runtime (only when needed) ---- */@@ -9764,8 +9924,10 @@
97649924
size_t len = pm_string_length(&sym->unescaped);97659925
char fname[64];97669926
snprintf(fname, sizeof(fname), "%.*s", (int)len, src);9927+
char *safe_fname = c_safe_name(fname);97679928
emit_raw(ctx, "static mrb_int _meth_adapt_%s(void *_e, mrb_int _a) { (void)_e; return sp_%s(_a); }\n",9768-
fname, fname);9929+
safe_fname, safe_fname);9930+
free(safe_fname);97699931
}97709932
if (c->receiver && msp < 255) mstack[msp++] = c->receiver;97719933
if (c->arguments) {