Skip to content

Commit d9dc6ff

Browse files
committed
py: Allocate memory for assembled code at start of PASS_EMIT.
Previously was allocating at end of PASS_COMPUTE, and this pass was being run twice, so memory was being allocated twice.
1 parent bc47c28 commit d9dc6ff

File tree

4 files changed

+31
-54
lines changed

4 files changed

+31
-54
lines changed

py/asmarm.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ void asm_arm_free(asm_arm_t *as, bool free_code) {
7070
}
7171

7272
void asm_arm_start_pass(asm_arm_t *as, uint pass) {
73-
as->pass = pass;
74-
as->code_offset = 0;
7573
if (pass == ASM_ARM_PASS_COMPUTE) {
7674
memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
75+
} else if (pass == ASM_ARM_PASS_EMIT) {
76+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
77+
if (as->code_base == NULL) {
78+
assert(0);
79+
}
7780
}
81+
as->pass = pass;
82+
as->code_offset = 0;
7883
}
7984

8085
void asm_arm_end_pass(asm_arm_t *as) {
81-
if (as->pass == ASM_ARM_PASS_COMPUTE) {
82-
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
83-
if(as->code_base == NULL) {
84-
assert(0);
85-
}
86-
} else if(as->pass == ASM_ARM_PASS_EMIT) {
86+
if (as->pass == ASM_ARM_PASS_EMIT) {
8787
#ifdef __arm__
8888
// flush I- and D-cache
8989
asm volatile(

py/asmthumb.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,21 @@ void asm_thumb_free(asm_thumb_t *as, bool free_code) {
7373
}
7474

7575
void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
76-
as->pass = pass;
77-
as->code_offset = 0;
7876
if (pass == ASM_THUMB_PASS_COMPUTE) {
7977
memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
80-
}
81-
}
82-
83-
void asm_thumb_end_pass(asm_thumb_t *as) {
84-
if (as->pass == ASM_THUMB_PASS_COMPUTE) {
85-
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
86-
if(as->code_base == NULL) {
78+
} else if (pass == ASM_THUMB_PASS_EMIT) {
79+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
80+
if (as->code_base == NULL) {
8781
assert(0);
8882
}
8983
//printf("code_size: %u\n", as->code_size);
9084
}
85+
as->pass = pass;
86+
as->code_offset = 0;
87+
}
9188

92-
/*
93-
// check labels are resolved
94-
if (as->label != NULL)
95-
{
96-
int i;
97-
for (i = 0; i < as->label->len; ++i)
98-
if (g_array_index(as->label, Label, i).unresolved != NULL)
99-
return false;
100-
}
101-
*/
89+
void asm_thumb_end_pass(asm_thumb_t *as) {
90+
// could check labels are resolved...
10291
}
10392

10493
// all functions must go through this one to emit bytes

py/asmx64.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,33 +143,22 @@ void asm_x64_free(asm_x64_t *as, bool free_code) {
143143
}
144144

145145
void asm_x64_start_pass(asm_x64_t *as, uint pass) {
146-
as->pass = pass;
147-
as->code_offset = 0;
148146
if (pass == ASM_X64_PASS_COMPUTE) {
149147
// reset all labels
150148
memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
151-
}
152-
}
153-
154-
void asm_x64_end_pass(asm_x64_t *as) {
155-
if (as->pass == ASM_X64_PASS_COMPUTE) {
156-
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
157-
if(as->code_base == NULL) {
149+
} if (pass == ASM_X64_PASS_EMIT) {
150+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
151+
if (as->code_base == NULL) {
158152
assert(0);
159153
}
160154
//printf("code_size: %u\n", as->code_size);
161155
}
156+
as->pass = pass;
157+
as->code_offset = 0;
158+
}
162159

163-
/*
164-
// check labels are resolved
165-
if (as->label != NULL)
166-
{
167-
int i;
168-
for (i = 0; i < as->label->len; ++i)
169-
if (g_array_index(as->label, Label, i).unresolved != NULL)
170-
return false;
171-
}
172-
*/
160+
void asm_x64_end_pass(asm_x64_t *as) {
161+
// could check labels are resolved...
173162
}
174163

175164
// all functions must go through this one to emit bytes

py/asmx86.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,20 @@ void asm_x86_free(asm_x86_t *as, bool free_code) {
131131
}
132132

133133
void asm_x86_start_pass(asm_x86_t *as, mp_uint_t pass) {
134-
as->pass = pass;
135-
as->code_offset = 0;
136134
if (pass == ASM_X86_PASS_COMPUTE) {
137135
// reset all labels
138136
memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
137+
} else if (pass == ASM_X86_PASS_EMIT) {
138+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
139+
if (as->code_base == NULL) {
140+
assert(0);
141+
}
139142
}
143+
as->pass = pass;
144+
as->code_offset = 0;
140145
}
141146

142147
void asm_x86_end_pass(asm_x86_t *as) {
143-
if (as->pass == ASM_X86_PASS_COMPUTE) {
144-
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
145-
if(as->code_base == NULL) {
146-
assert(0);
147-
}
148-
}
149148
}
150149

151150
// all functions must go through this one to emit bytes

0 commit comments

Comments
 (0)