Skip to content

Commit 41d02b6

Browse files
committed
py: Improve freeing of emitters in mp_compile.
There can be multiple emitters allocated during compile (eg byte code and native).
1 parent ceb8783 commit 41d02b6

7 files changed

Lines changed: 43 additions & 20 deletions

File tree

py/compile.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,25 +3154,46 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
31543154
}
31553155
}
31563156

3157-
bool had_error = comp->had_error;
3158-
if (comp->emit_method_table->free != NULL) {
3159-
comp->emit_method_table->free(comp->emit);
3157+
// free the emitters
3158+
#if !MICROPY_EMIT_CPYTHON
3159+
if (emit_bc != NULL) {
3160+
emit_bc_free(emit_bc);
31603161
}
3161-
m_del_obj(compiler_t, comp);
3162+
#if MICROPY_EMIT_NATIVE
3163+
if (emit_native != NULL) {
3164+
#if MICROPY_EMIT_X64
3165+
emit_native_x64_free(emit_native);
3166+
#elif MICROPY_EMIT_THUMB
3167+
emit_native_thumb_free(emit_native);
3168+
#endif
3169+
}
3170+
#endif
3171+
#if MICROPY_EMIT_INLINE_THUMB
3172+
if (emit_inline_thumb != NULL) {
3173+
emit_inline_thumb_free(emit_inline_thumb);
3174+
}
3175+
#endif
3176+
#endif // !MICROPY_EMIT_CPYTHON
3177+
3178+
// free the scopes
31623179
uint unique_code_id = module_scope->unique_code_id;
31633180
for (scope_t *s = module_scope; s;) {
31643181
scope_t *next = s->next;
31653182
scope_free(s);
31663183
s = next;
31673184
}
31683185

3186+
// free the compiler
3187+
bool had_error = comp->had_error;
3188+
m_del_obj(compiler_t, comp);
3189+
31693190
if (had_error) {
31703191
// TODO return a proper error message
31713192
return mp_const_none;
31723193
} else {
31733194
#if MICROPY_EMIT_CPYTHON
31743195
// can't create code, so just return true
3175-
(void)unique_code_id; // to suppress warning that module_scope is unused
3196+
(void)unique_code_id; // to suppress warning that unique_code_id is unused
31763197
return mp_const_true;
31773198
#else
31783199
// return function that executes the outer module

py/emit.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ typedef enum {
1717
typedef struct _emit_t emit_t;
1818

1919
typedef struct _emit_method_table_t {
20-
void (*free)(emit_t *emit);
21-
2220
void (*set_native_types)(emit_t *emit, bool do_native_types);
2321
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
2422
void (*end_pass)(emit_t *emit);
@@ -120,12 +118,16 @@ extern const emit_method_table_t emit_native_x64_method_table;
120118
extern const emit_method_table_t emit_native_thumb_method_table;
121119

122120
emit_t *emit_pass1_new(qstr qstr___class__);
123-
void emit_pass1_free(emit_t *emit);
124121
emit_t *emit_cpython_new(uint max_num_labels);
125122
emit_t *emit_bc_new(uint max_num_labels);
126123
emit_t *emit_native_x64_new(uint max_num_labels);
127124
emit_t *emit_native_thumb_new(uint max_num_labels);
128125

126+
void emit_pass1_free(emit_t *emit);
127+
void emit_bc_free(emit_t *emit);
128+
void emit_native_x64_free(emit_t *emit);
129+
void emit_native_thumb_free(emit_t *emit);
130+
129131
typedef struct _emit_inline_asm_t emit_inline_asm_t;
130132

131133
typedef struct _emit_inline_asm_method_table_t {
@@ -139,3 +141,5 @@ typedef struct _emit_inline_asm_method_table_t {
139141
extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
140142

141143
emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels);
144+
void emit_inline_thumb_free(emit_inline_asm_t *emit);
145+

py/emitbc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ emit_t *emit_bc_new(uint max_num_labels) {
4343
return emit;
4444
}
4545

46-
static void emit_bc_free(emit_t *emit) {
46+
void emit_bc_free(emit_t *emit) {
4747
m_del(uint, emit->label_offsets, emit->max_num_labels);
4848
m_del_obj(emit_t, emit);
4949
}
@@ -756,8 +756,6 @@ static void emit_bc_yield_from(emit_t *emit) {
756756
}
757757

758758
const emit_method_table_t emit_bc_method_table = {
759-
emit_bc_free,
760-
761759
emit_bc_set_native_types,
762760
emit_bc_start_pass,
763761
emit_bc_end_pass,

py/emitcpy.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,6 @@ static void emit_cpy_yield_from(emit_t *emit) {
796796
}
797797

798798
const emit_method_table_t emit_cpython_method_table = {
799-
NULL,
800-
801799
emit_cpy_set_native_types,
802800
emit_cpy_start_pass,
803801
emit_cpy_end_pass,

py/emitinlinethumb.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@
2020
struct _emit_inline_asm_t {
2121
int pass;
2222
scope_t *scope;
23-
int max_num_labels;
23+
uint max_num_labels;
2424
qstr *label_lookup;
2525
asm_thumb_t *as;
2626
};
2727

2828
emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels) {
29-
emit_inline_asm_t *emit = m_new(emit_inline_asm_t, 1);
29+
emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t);
3030
emit->max_num_labels = max_num_labels;
3131
emit->label_lookup = m_new(qstr, max_num_labels);
3232
memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr));
3333
emit->as = asm_thumb_new(max_num_labels);
3434
return emit;
3535
}
3636

37+
void emit_inline_thumb_free(emit_inline_asm_t *emit) {
38+
m_del(qstr, emit->label_lookup, emit->max_num_labels);
39+
asm_thumb_free(emit->as, false);
40+
m_del_obj(emit_inline_asm_t, emit);
41+
}
42+
3743
static void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope) {
3844
emit->pass = pass;
3945
emit->scope = scope;

py/emitnative.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ emit_t *EXPORT_FUN(new)(uint max_num_labels) {
146146
return emit;
147147
}
148148

149-
static void emit_native_free(emit_t *emit) {
149+
void EXPORT_FUN(free)(emit_t *emit) {
150150
#if N_X64
151151
asm_x64_free(emit->as, false);
152152
#elif N_THUMB
@@ -1235,8 +1235,6 @@ static void emit_native_yield_from(emit_t *emit) {
12351235
}
12361236

12371237
const emit_method_table_t EXPORT_FUN(method_table) = {
1238-
emit_native_free,
1239-
12401238
emit_native_set_viper_types,
12411239
emit_native_start_pass,
12421240
emit_native_end_pass,

py/emitpass1.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ static void emit_pass1_delete_id(emit_t *emit, qstr qstr) {
9797
}
9898

9999
const emit_method_table_t emit_pass1_method_table = {
100-
emit_pass1_free,
101-
102100
(void*)emit_pass1_dummy,
103101
emit_pass1_start_pass,
104102
emit_pass1_end_pass,

0 commit comments

Comments
 (0)