clean up bytecode

This commit is contained in:
2026-02-13 09:03:00 -06:00
parent 0acaabd5fa
commit 3795533554
14 changed files with 23087 additions and 29051 deletions

View File

@@ -1262,100 +1262,6 @@ JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code,
/* === New mcode-derived opcodes === */
/* Typed integer arithmetic — inline with overflow to float */
case MACH_ADD_INT: {
int32_t ia = JS_VALUE_GET_INT(frame->slots[b]);
int32_t ib = JS_VALUE_GET_INT(frame->slots[c]);
int64_t r = (int64_t)ia + (int64_t)ib;
frame->slots[a] = (r >= INT32_MIN && r <= INT32_MAX)
? JS_NewInt32(ctx, (int32_t)r) : JS_NewFloat64(ctx, (double)r);
break;
}
case MACH_SUB_INT: {
int32_t ia = JS_VALUE_GET_INT(frame->slots[b]);
int32_t ib = JS_VALUE_GET_INT(frame->slots[c]);
int64_t r = (int64_t)ia - (int64_t)ib;
frame->slots[a] = (r >= INT32_MIN && r <= INT32_MAX)
? JS_NewInt32(ctx, (int32_t)r) : JS_NewFloat64(ctx, (double)r);
break;
}
case MACH_MUL_INT: {
int32_t ia = JS_VALUE_GET_INT(frame->slots[b]);
int32_t ib = JS_VALUE_GET_INT(frame->slots[c]);
int64_t r = (int64_t)ia * (int64_t)ib;
frame->slots[a] = (r >= INT32_MIN && r <= INT32_MAX)
? JS_NewInt32(ctx, (int32_t)r) : JS_NewFloat64(ctx, (double)r);
break;
}
case MACH_DIV_INT: {
int32_t ia = JS_VALUE_GET_INT(frame->slots[b]);
int32_t ib = JS_VALUE_GET_INT(frame->slots[c]);
if (ib == 0) { frame->slots[a] = JS_NULL; break; }
if (ia % ib == 0) frame->slots[a] = JS_NewInt32(ctx, ia / ib);
else frame->slots[a] = JS_NewFloat64(ctx, (double)ia / (double)ib);
break;
}
case MACH_MOD_INT: {
int32_t ia = JS_VALUE_GET_INT(frame->slots[b]);
int32_t ib = JS_VALUE_GET_INT(frame->slots[c]);
if (ib == 0) { frame->slots[a] = JS_NULL; break; }
frame->slots[a] = JS_NewInt32(ctx, ia % ib);
break;
}
case MACH_NEG_INT: {
int32_t i = JS_VALUE_GET_INT(frame->slots[b]);
if (i == INT32_MIN)
frame->slots[a] = JS_NewFloat64(ctx, -(double)i);
else
frame->slots[a] = JS_NewInt32(ctx, -i);
break;
}
/* Typed float arithmetic */
case MACH_ADD_FLOAT: {
double da, db;
JS_ToFloat64(ctx, &da, frame->slots[b]);
JS_ToFloat64(ctx, &db, frame->slots[c]);
frame->slots[a] = JS_NewFloat64(ctx, da + db);
break;
}
case MACH_SUB_FLOAT: {
double da, db;
JS_ToFloat64(ctx, &da, frame->slots[b]);
JS_ToFloat64(ctx, &db, frame->slots[c]);
frame->slots[a] = JS_NewFloat64(ctx, da - db);
break;
}
case MACH_MUL_FLOAT: {
double da, db;
JS_ToFloat64(ctx, &da, frame->slots[b]);
JS_ToFloat64(ctx, &db, frame->slots[c]);
frame->slots[a] = JS_NewFloat64(ctx, da * db);
break;
}
case MACH_DIV_FLOAT: {
double da, db;
JS_ToFloat64(ctx, &da, frame->slots[b]);
JS_ToFloat64(ctx, &db, frame->slots[c]);
if (db == 0.0) { frame->slots[a] = JS_NULL; break; }
frame->slots[a] = JS_NewFloat64(ctx, da / db);
break;
}
case MACH_MOD_FLOAT: {
double da, db;
JS_ToFloat64(ctx, &da, frame->slots[b]);
JS_ToFloat64(ctx, &db, frame->slots[c]);
if (db == 0.0) { frame->slots[a] = JS_NULL; break; }
frame->slots[a] = JS_NewFloat64(ctx, fmod(da, db));
break;
}
case MACH_NEG_FLOAT: {
double d;
JS_ToFloat64(ctx, &d, frame->slots[b]);
frame->slots[a] = JS_NewFloat64(ctx, -d);
break;
}
/* Text concatenation */
case MACH_CONCAT: {
JSValue res = JS_ConcatString(ctx, frame->slots[b], frame->slots[c]);
@@ -2375,20 +2281,6 @@ static MachCode *mcode_lower_func(cJSON *fobj, const char *filename) {
else if (strcmp(op, "false") == 0) { EM(MACH_ABC(MACH_LOADFALSE, A1, 0, 0)); }
else if (strcmp(op, "null") == 0) { EM(MACH_ABC(MACH_LOADNULL, A1, 0, 0)); }
else if (strcmp(op, "move") == 0) { AB2(MACH_MOVE); }
/* Typed integer arithmetic */
else if (strcmp(op, "add_int") == 0) { ABC3(MACH_ADD_INT); }
else if (strcmp(op, "sub_int") == 0) { ABC3(MACH_SUB_INT); }
else if (strcmp(op, "mul_int") == 0) { ABC3(MACH_MUL_INT); }
else if (strcmp(op, "div_int") == 0) { ABC3(MACH_DIV_INT); }
else if (strcmp(op, "mod_int") == 0) { ABC3(MACH_MOD_INT); }
else if (strcmp(op, "neg_int") == 0) { AB2(MACH_NEG_INT); }
/* Typed float arithmetic */
else if (strcmp(op, "add_float") == 0) { ABC3(MACH_ADD_FLOAT); }
else if (strcmp(op, "sub_float") == 0) { ABC3(MACH_SUB_FLOAT); }
else if (strcmp(op, "mul_float") == 0) { ABC3(MACH_MUL_FLOAT); }
else if (strcmp(op, "div_float") == 0) { ABC3(MACH_DIV_FLOAT); }
else if (strcmp(op, "mod_float") == 0) { ABC3(MACH_MOD_FLOAT); }
else if (strcmp(op, "neg_float") == 0) { AB2(MACH_NEG_FLOAT); }
/* Text */
else if (strcmp(op, "concat") == 0) { ABC3(MACH_CONCAT); }
/* Generic arithmetic */

View File

@@ -556,22 +556,6 @@ typedef enum MachOpcode {
/* === New mcode-derived opcodes (1:1 mapping to mcode IR) === */
/* Typed integer arithmetic (ABC) */
MACH_ADD_INT, /* R(A) = R(B) + R(C) — int, overflow → float */
MACH_SUB_INT, /* R(A) = R(B) - R(C) — int */
MACH_MUL_INT, /* R(A) = R(B) * R(C) — int */
MACH_DIV_INT, /* R(A) = R(B) / R(C) — int */
MACH_MOD_INT, /* R(A) = R(B) % R(C) — int */
MACH_NEG_INT, /* R(A) = -R(B) — int (AB) */
/* Typed float arithmetic (ABC) */
MACH_ADD_FLOAT, /* R(A) = R(B) + R(C) — float */
MACH_SUB_FLOAT, /* R(A) = R(B) - R(C) — float */
MACH_MUL_FLOAT, /* R(A) = R(B) * R(C) — float */
MACH_DIV_FLOAT, /* R(A) = R(B) / R(C) — float */
MACH_MOD_FLOAT, /* R(A) = R(B) % R(C) — float */
MACH_NEG_FLOAT, /* R(A) = -R(B) — float (AB) */
/* Text */
MACH_CONCAT, /* R(A) = R(B) ++ R(C) — string concatenation */
@@ -725,18 +709,6 @@ static const char *mach_opcode_names[MACH_OP_COUNT] = {
[MACH_NEQ_TOL] = "neq_tol",
[MACH_NOP] = "nop",
/* Mcode-derived */
[MACH_ADD_INT] = "add_int",
[MACH_SUB_INT] = "sub_int",
[MACH_MUL_INT] = "mul_int",
[MACH_DIV_INT] = "div_int",
[MACH_MOD_INT] = "mod_int",
[MACH_NEG_INT] = "neg_int",
[MACH_ADD_FLOAT] = "add_float",
[MACH_SUB_FLOAT] = "sub_float",
[MACH_MUL_FLOAT] = "mul_float",
[MACH_DIV_FLOAT] = "div_float",
[MACH_MOD_FLOAT] = "mod_float",
[MACH_NEG_FLOAT] = "neg_float",
[MACH_CONCAT] = "concat",
[MACH_EQ_INT] = "eq_int",
[MACH_NE_INT] = "ne_int",