better jump labels

This commit is contained in:
2026-02-05 10:28:13 -06:00
parent 8b7af0c22a
commit 1cc3005b68

View File

@@ -643,9 +643,10 @@ typedef struct JSCodeRegister {
uint32_t func_count;
struct JSCodeRegister **functions;
/* Labels resolved to instruction indices (for jump targets) */
/* Labels resolved to instruction indices (for debugging) */
uint32_t label_count;
uint32_t *label_offsets; /* label_idx -> instr_idx */
char **label_names; /* label_idx -> label name string */
/* Debug info */
JSValue name; /* function name (stone text) */
@@ -31497,6 +31498,17 @@ static JSCodeRegister *js_link_mach_unit(JSContext *ctx, cJSON *unit, JSValue en
}
}
/* Copy label info for debugging before freeing the table */
if (labels.label_count > 0) {
code->label_count = labels.label_count;
code->label_offsets = js_mallocz_rt(labels.label_count * sizeof(uint32_t));
code->label_names = js_mallocz_rt(labels.label_count * sizeof(char *));
for (int i = 0; i < labels.label_count; i++) {
code->label_offsets[i] = labels.labels[i].instr_idx;
code->label_names[i] = js_strdup_rt(labels.labels[i].name);
}
}
mach_label_table_free(&labels);
mach_cpool_free(&cpool);
@@ -31509,6 +31521,10 @@ static void js_free_code_register(JSCodeRegister *code) {
js_free_rt(code->instructions);
js_free_rt(code->cpool);
js_free_rt(code->label_offsets);
for (uint32_t i = 0; i < code->label_count; i++) {
js_free_rt(code->label_names[i]);
}
js_free_rt(code->label_names);
for (uint32_t i = 0; i < code->func_count; i++) {
js_free_code_register(code->functions[i]);
}
@@ -32307,6 +32323,16 @@ static void dump_cpool_value(JSContext *ctx, JSValue val) {
}
}
/* Find label name for an instruction index, or NULL if none */
static const char *find_label_for_instr(JSCodeRegister *code, uint32_t instr_idx) {
for (uint32_t i = 0; i < code->label_count; i++) {
if (code->label_offsets[i] == instr_idx) {
return code->label_names[i];
}
}
return NULL;
}
/* Internal helper to dump JSCodeRegister */
static void dump_register_code(JSContext *ctx, JSCodeRegister *code, int indent) {
char pad[64];
@@ -32341,6 +32367,12 @@ static void dump_register_code(JSContext *ctx, JSCodeRegister *code, int indent)
/* Instructions */
printf("%s\n%sInstructions (%d):\n", pad, pad, code->instr_count);
for (uint32_t i = 0; i < code->instr_count; i++) {
/* Print any labels pointing to this instruction */
const char *label = find_label_for_instr(code, i);
if (label) {
printf("%s%s:\n", pad, label);
}
MachInstr *instr = &code->instructions[i];
uint8_t op = instr->opcode;
const char *op_name = (op < MACH_OP_COUNT) ? mach_opcode_names[op] : "???";
@@ -32483,15 +32515,21 @@ static void dump_register_code(JSContext *ctx, JSCodeRegister *code, int indent)
/* label (resolved to instruction index) */
case MACH_OP_JUMP:
case MACH_OP_TRY_BEGIN:
case MACH_OP_TRY_BEGIN: {
const char *target_label = find_label_for_instr(code, instr->a);
printf("@%d", instr->a);
if (target_label) printf(" ; -> %s", target_label);
}
break;
/* cond, label (resolved to instruction index) */
case MACH_OP_JUMP_TRUE:
case MACH_OP_JUMP_FALSE:
case MACH_OP_JUMP_NOT_NULL:
case MACH_OP_JUMP_NOT_NULL: {
const char *target_label = find_label_for_instr(code, instr->b);
printf("r%d, @%d", instr->a, instr->b);
if (target_label) printf(" ; -> %s", target_label);
}
break;
/* frame_reg, func_reg, argc */