Skip to content

Commit 054848a

Browse files
committed
Compiler computes labels and max_num_labels.
1 parent b05d707 commit 054848a

7 files changed

Lines changed: 61 additions & 93 deletions

File tree

py/asmx64.c

Lines changed: 59 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ struct _asm_x64_t {
8181
byte *code_base;
8282
byte dummy_data[8];
8383

84-
int next_label;
85-
int max_num_labels;
84+
uint max_num_labels;
8685
int *label_offsets;
8786
};
8887

@@ -98,15 +97,16 @@ void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
9897
return ptr;
9998
}
10099

101-
asm_x64_t* asm_x64_new() {
100+
asm_x64_t* asm_x64_new(uint max_num_labels) {
102101
asm_x64_t* as;
103102

104103
as = m_new(asm_x64_t, 1);
105104
as->pass = 0;
106105
as->code_offset = 0;
107106
as->code_size = 0;
108107
as->code_base = NULL;
109-
as->label_offsets = NULL;
108+
as->max_num_labels = max_num_labels;
109+
as->label_offsets = m_new(int, max_num_labels);
110110

111111
return as;
112112
}
@@ -134,29 +134,19 @@ void asm_x64_free(asm_x64_t* as, bool free_code) {
134134
void asm_x64_start_pass(asm_x64_t *as, int pass) {
135135
as->pass = pass;
136136
as->code_offset = 0;
137-
as->next_label = 1;
138-
if (pass == ASM_X64_PASS_1) {
139-
as->max_num_labels = 0;
140-
} else {
141-
if (pass == ASM_X64_PASS_2) {
142-
memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
143-
}
137+
if (pass == ASM_X64_PASS_2) {
138+
// reset all labels
139+
memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
144140
}
145141
}
146142

147143
void asm_x64_end_pass(asm_x64_t *as) {
148-
if (as->pass == ASM_X64_PASS_1) {
149-
// calculate number of labels need
150-
if (as->next_label > as->max_num_labels) {
151-
as->max_num_labels = as->next_label;
152-
}
153-
as->label_offsets = m_new(int, as->max_num_labels);
154-
} else if (as->pass == ASM_X64_PASS_2) {
144+
if (as->pass == ASM_X64_PASS_2) {
155145
// calculate size of code in bytes
156146
as->code_size = as->code_offset;
157-
as->code_base = m_new(byte, as->code_size);
158-
//uint actual_alloc;
159-
//as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
147+
//as->code_base = m_new(byte, as->code_size); need to allocale executable memory
148+
uint actual_alloc;
149+
as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
160150
printf("code_size: %u\n", as->code_size);
161151
}
162152

@@ -458,70 +448,65 @@ void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8) {
458448
asm_x64_write_byte_3(as, OPCODE_SETCC_RM8_A, OPCODE_SETCC_RM8_B | jcc_type, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r8));
459449
}
460450

461-
int asm_x64_label_new(asm_x64_t* as) {
462-
return as->next_label++;
463-
}
464-
465451
void asm_x64_label_assign(asm_x64_t* as, int label) {
466-
if (as->pass > ASM_X64_PASS_1) {
467-
assert(label < as->max_num_labels);
468-
if (as->pass == ASM_X64_PASS_2) {
469-
// assign label offset
470-
assert(as->label_offsets[label] == -1);
471-
as->label_offsets[label] = as->code_offset;
472-
} else if (as->pass == ASM_X64_PASS_3) {
473-
// ensure label offset has not changed from PASS_2 to PASS_3
474-
//printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
475-
assert(as->label_offsets[label] == as->code_offset);
476-
}
452+
assert(label < as->max_num_labels);
453+
if (as->pass == ASM_X64_PASS_2) {
454+
// assign label offset
455+
assert(as->label_offsets[label] == -1);
456+
as->label_offsets[label] = as->code_offset;
457+
} else if (as->pass == ASM_X64_PASS_3) {
458+
// ensure label offset has not changed from PASS_2 to PASS_3
459+
//printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
460+
assert(as->label_offsets[label] == as->code_offset);
477461
}
478462
}
479463

480-
void asm_x64_jmp_label(asm_x64_t* as, int label) {
481-
if (as->pass > ASM_X64_PASS_1) {
482-
int dest = as->label_offsets[label];
483-
int rel = dest - as->code_offset;
484-
if (dest >= 0 && rel < 0) {
485-
// is a backwards jump, so we know the size of the jump on the first pass
486-
// calculate rel assuming 8 bit relative jump
487-
rel -= 2;
488-
if (SIGNED_FIT8(rel)) {
489-
asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
490-
} else {
491-
rel += 2;
492-
goto large_jump;
493-
}
464+
static int get_label_dest(asm_x64_t *as, int label) {
465+
assert(label < as->max_num_labels);
466+
return as->label_offsets[label];
467+
}
468+
469+
void asm_x64_jmp_label(asm_x64_t *as, int label) {
470+
int dest = get_label_dest(as, label);
471+
int rel = dest - as->code_offset;
472+
if (dest >= 0 && rel < 0) {
473+
// is a backwards jump, so we know the size of the jump on the first pass
474+
// calculate rel assuming 8 bit relative jump
475+
rel -= 2;
476+
if (SIGNED_FIT8(rel)) {
477+
asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
494478
} else {
495-
// is a forwards jump, so need to assume it's large
496-
large_jump:
497-
rel -= 5;
498-
asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
499-
asm_x64_write_word32(as, rel);
479+
rel += 2;
480+
goto large_jump;
500481
}
482+
} else {
483+
// is a forwards jump, so need to assume it's large
484+
large_jump:
485+
rel -= 5;
486+
asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
487+
asm_x64_write_word32(as, rel);
501488
}
502489
}
503490

504-
void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label) {
505-
if (as->pass > ASM_X64_PASS_1) {
506-
int dest = as->label_offsets[label];
507-
int rel = dest - as->code_offset;
508-
if (dest >= 0 && rel < 0) {
509-
// is a backwards jump, so we know the size of the jump on the first pass
510-
// calculate rel assuming 8 bit relative jump
511-
rel -= 2;
512-
if (SIGNED_FIT8(rel)) {
513-
asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
514-
} else {
515-
rel += 2;
516-
goto large_jump;
517-
}
491+
void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
492+
int dest = get_label_dest(as, label);
493+
int rel = dest - as->code_offset;
494+
if (dest >= 0 && rel < 0) {
495+
// is a backwards jump, so we know the size of the jump on the first pass
496+
// calculate rel assuming 8 bit relative jump
497+
rel -= 2;
498+
if (SIGNED_FIT8(rel)) {
499+
asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
518500
} else {
519-
// is a forwards jump, so need to assume it's large
520-
large_jump:
521-
rel -= 6;
522-
asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
523-
asm_x64_write_word32(as, rel);
501+
rel += 2;
502+
goto large_jump;
524503
}
504+
} else {
505+
// is a forwards jump, so need to assume it's large
506+
large_jump:
507+
rel -= 6;
508+
asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
509+
asm_x64_write_word32(as, rel);
525510
}
526511
}
527512

py/asmx64.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void asm_x64_cmp_disp_with_r32(asm_x64_t* as, int src_r32_a, int src_disp_a, int
5959
void asm_x64_cmp_i32_with_r32(asm_x64_t* as, int src_i32, int src_r32);
6060
void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b);
6161
void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8);
62-
int asm_x64_label_new(asm_x64_t* as);
6362
void asm_x64_label_assign(asm_x64_t* as, int label);
6463
void asm_x64_jmp_label(asm_x64_t* as, int label);
6564
void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label);

py/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,8 +2523,8 @@ void py_compile(py_parse_node_t pn) {
25232523
}
25242524

25252525
//emit_cpython_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2526-
emit_bc_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2527-
//emit_new_x64(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2526+
//emit_bc_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2527+
emit_x64_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
25282528

25292529
for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
25302530
compile_scope(comp, s, PASS_2);

py/emit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ typedef struct _emit_method_table_t {
2424
int (*get_stack_size)(emit_t *emit);
2525
void (*set_stack_size)(emit_t *emit, int size);
2626

27-
int (*label_new)(emit_t *emit);
2827
void (*label_assign)(emit_t *emit, int l);
2928
void (*import_name)(emit_t *emit, qstr qstr);
3029
void (*import_from)(emit_t *emit, qstr qstr);

py/emitbc.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ static void emit_pre(emit_t *emit, int stack_size_delta) {
158158
emit->last_emit_was_return_value = false;
159159
}
160160

161-
static int emit_bc_label_new(emit_t *emit) {
162-
return emit->next_label++;
163-
}
164-
165161
static void emit_bc_label_assign(emit_t *emit, int l) {
166162
emit_pre(emit, 0);
167163
assert(l < emit->max_num_labels);
@@ -669,7 +665,6 @@ static const emit_method_table_t emit_bc_method_table = {
669665
emit_bc_get_stack_size,
670666
emit_bc_set_stack_size,
671667

672-
emit_bc_label_new,
673668
emit_bc_label_assign,
674669
emit_bc_import_name,
675670
emit_bc_import_from,

py/emitcpy.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
7979
emit->byte_code_offset += byte_code_size;
8080
}
8181

82-
static int emit_cpy_label_new(emit_t *emit) {
83-
return emit->next_label++;
84-
}
85-
8682
static void emit_cpy_label_assign(emit_t *emit, int l) {
8783
emit_pre(emit, 0, 0);
8884
assert(l < emit->max_num_labels);
@@ -822,7 +818,6 @@ static const emit_method_table_t emit_cpy_method_table = {
822818
emit_cpy_get_stack_size,
823819
emit_cpy_set_stack_size,
824820

825-
emit_cpy_label_new,
826821
emit_cpy_label_assign,
827822
emit_cpy_import_name,
828823
emit_cpy_import_from,

py/emitx64.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ static void emit_call_with_i64_arg(emit_t *emit, void *fun, int64_t arg_val, int
249249
asm_x64_call_ind(emit->as, fun, REG_RAX);
250250
}
251251

252-
static int emit_x64_label_new(emit_t *emit) {
253-
return asm_x64_label_new(emit->as);
254-
}
255-
256252
static void emit_x64_label_assign(emit_t *emit, int l) {
257253
asm_x64_label_assign(emit->as, l);
258254
}
@@ -675,7 +671,6 @@ static const emit_method_table_t emit_x64_method_table = {
675671
emit_x64_get_stack_size,
676672
emit_x64_set_stack_size,
677673

678-
emit_x64_label_new,
679674
emit_x64_label_assign,
680675
emit_x64_import_name,
681676
emit_x64_import_from,

0 commit comments

Comments
 (0)