decomposed mcode

This commit is contained in:
2026-02-10 07:12:27 -06:00
parent 3f7e34cd7a
commit 877250b1d8
3 changed files with 977 additions and 11 deletions

View File

@@ -486,6 +486,220 @@ JSValue mcode_exec(JSContext *ctx, JSMCode *code, JSValue this_obj,
frame->slots[dest] = JS_NewFloat64(ctx, -d);
}
}
/* ---- Compiler-internal type guards ---- */
else if (strcmp(op, "is_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_IsInt(frame->slots[(int)a2->valuedouble]) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "is_num") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_IsNumber(frame->slots[(int)a2->valuedouble]) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "is_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_IsText(frame->slots[(int)a2->valuedouble]) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "is_null") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_IsNull(frame->slots[(int)a2->valuedouble]) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "is_bool") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = (JS_VALUE_GET_TAG(frame->slots[(int)a2->valuedouble]) == JS_TAG_BOOL) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "is_identical") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = (frame->slots[(int)a2->valuedouble] == frame->slots[(int)a3->valuedouble]) ? JS_TRUE : JS_FALSE;
}
/* ---- Specialized arithmetic (int) ---- */
else if (strcmp(op, "add_int") == 0) {
int dest = (int)a1->valuedouble;
int64_t r = (int64_t)JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) + (int64_t)JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = (r >= INT32_MIN && r <= INT32_MAX) ? JS_NewInt32(ctx, (int32_t)r) : JS_NewFloat64(ctx, (double)r);
}
else if (strcmp(op, "sub_int") == 0) {
int dest = (int)a1->valuedouble;
int64_t r = (int64_t)JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) - (int64_t)JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = (r >= INT32_MIN && r <= INT32_MAX) ? JS_NewInt32(ctx, (int32_t)r) : JS_NewFloat64(ctx, (double)r);
}
else if (strcmp(op, "mul_int") == 0) {
int dest = (int)a1->valuedouble;
int64_t r = (int64_t)JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) * (int64_t)JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = (r >= INT32_MIN && r <= INT32_MAX) ? JS_NewInt32(ctx, (int32_t)r) : JS_NewFloat64(ctx, (double)r);
}
else if (strcmp(op, "div_int") == 0) {
int dest = (int)a1->valuedouble;
int32_t ia = JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]);
int32_t ib = JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]);
if (ib == 0) frame->slots[dest] = JS_NULL;
else if (ia % ib == 0) frame->slots[dest] = JS_NewInt32(ctx, ia / ib);
else frame->slots[dest] = JS_NewFloat64(ctx, (double)ia / (double)ib);
}
else if (strcmp(op, "mod_int") == 0) {
int dest = (int)a1->valuedouble;
int32_t ib = JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]);
if (ib == 0) frame->slots[dest] = JS_NULL;
else frame->slots[dest] = JS_NewInt32(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) % ib);
}
else if (strcmp(op, "neg_int") == 0) {
int dest = (int)a1->valuedouble;
int32_t i = JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]);
if (i == INT32_MIN) frame->slots[dest] = JS_NewFloat64(ctx, -(double)i);
else frame->slots[dest] = JS_NewInt32(ctx, -i);
}
/* ---- Specialized arithmetic (float) ---- */
else if (strcmp(op, "add_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewFloat64(ctx, a + b);
}
else if (strcmp(op, "sub_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewFloat64(ctx, a - b);
}
else if (strcmp(op, "mul_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewFloat64(ctx, a * b);
}
else if (strcmp(op, "div_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
if (b == 0.0) frame->slots[dest] = JS_NULL;
else frame->slots[dest] = JS_NewFloat64(ctx, a / b);
}
else if (strcmp(op, "mod_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
if (b == 0.0) frame->slots[dest] = JS_NULL;
else frame->slots[dest] = JS_NewFloat64(ctx, fmod(a, b));
}
else if (strcmp(op, "neg_float") == 0) {
int dest = (int)a1->valuedouble;
double d;
JS_ToFloat64(ctx, &d, frame->slots[(int)a2->valuedouble]);
frame->slots[dest] = JS_NewFloat64(ctx, -d);
}
/* ---- Specialized comparisons (int) ---- */
else if (strcmp(op, "eq_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) == JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]));
}
else if (strcmp(op, "ne_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) != JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]));
}
else if (strcmp(op, "lt_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) < JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]));
}
else if (strcmp(op, "le_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) <= JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]));
}
else if (strcmp(op, "gt_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) > JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]));
}
else if (strcmp(op, "ge_int") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, JS_VALUE_GET_INT(frame->slots[(int)a2->valuedouble]) >= JS_VALUE_GET_INT(frame->slots[(int)a3->valuedouble]));
}
/* ---- Specialized comparisons (float) ---- */
else if (strcmp(op, "eq_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewBool(ctx, a == b);
}
else if (strcmp(op, "ne_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewBool(ctx, a != b);
}
else if (strcmp(op, "lt_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewBool(ctx, a < b);
}
else if (strcmp(op, "le_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewBool(ctx, a <= b);
}
else if (strcmp(op, "gt_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewBool(ctx, a > b);
}
else if (strcmp(op, "ge_float") == 0) {
int dest = (int)a1->valuedouble;
double a, b;
JS_ToFloat64(ctx, &a, frame->slots[(int)a2->valuedouble]);
JS_ToFloat64(ctx, &b, frame->slots[(int)a3->valuedouble]);
frame->slots[dest] = JS_NewBool(ctx, a >= b);
}
/* ---- Specialized comparisons (text) ---- */
else if (strcmp(op, "eq_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, js_string_compare_value(ctx, frame->slots[(int)a2->valuedouble], frame->slots[(int)a3->valuedouble], TRUE) == 0);
}
else if (strcmp(op, "ne_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, js_string_compare_value(ctx, frame->slots[(int)a2->valuedouble], frame->slots[(int)a3->valuedouble], TRUE) != 0);
}
else if (strcmp(op, "lt_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, js_string_compare_value(ctx, frame->slots[(int)a2->valuedouble], frame->slots[(int)a3->valuedouble], FALSE) < 0);
}
else if (strcmp(op, "le_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, js_string_compare_value(ctx, frame->slots[(int)a2->valuedouble], frame->slots[(int)a3->valuedouble], FALSE) <= 0);
}
else if (strcmp(op, "gt_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, js_string_compare_value(ctx, frame->slots[(int)a2->valuedouble], frame->slots[(int)a3->valuedouble], FALSE) > 0);
}
else if (strcmp(op, "ge_text") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = JS_NewBool(ctx, js_string_compare_value(ctx, frame->slots[(int)a2->valuedouble], frame->slots[(int)a3->valuedouble], FALSE) >= 0);
}
/* ---- Specialized comparisons (bool) ---- */
else if (strcmp(op, "eq_bool") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = (frame->slots[(int)a2->valuedouble] == frame->slots[(int)a3->valuedouble]) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "ne_bool") == 0) {
int dest = (int)a1->valuedouble;
frame->slots[dest] = (frame->slots[(int)a2->valuedouble] != frame->slots[(int)a3->valuedouble]) ? JS_TRUE : JS_FALSE;
}
else if (strcmp(op, "abs") == 0) {
int dest = (int)a1->valuedouble;
JSValue v = frame->slots[(int)a2->valuedouble];