Skip to content

Commit ceb8783

Browse files
committed
Merge pull request micropython#217 from pfalcon/free-emitter
Add support for freeing code emitter objects at the end of compilation.
2 parents 4461970 + f46d87a commit ceb8783

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

py/compile.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,6 +3155,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
31553155
}
31563156

31573157
bool had_error = comp->had_error;
3158+
if (comp->emit_method_table->free != NULL) {
3159+
comp->emit_method_table->free(comp->emit);
3160+
}
31583161
m_del_obj(compiler_t, comp);
31593162
uint unique_code_id = module_scope->unique_code_id;
31603163
for (scope_t *s = module_scope; s;) {

py/emit.h

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

1919
typedef struct _emit_method_table_t {
20+
void (*free)(emit_t *emit);
21+
2022
void (*set_native_types)(emit_t *emit, bool do_native_types);
2123
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
2224
void (*end_pass)(emit_t *emit);

py/emitbc.c

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

46+
static void emit_bc_free(emit_t *emit) {
47+
m_del(uint, emit->label_offsets, emit->max_num_labels);
48+
m_del_obj(emit_t, emit);
49+
}
50+
4651
// all functions must go through this one to emit code info
4752
static byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
4853
//printf("emit %d\n", num_bytes_to_write);
@@ -751,6 +756,8 @@ static void emit_bc_yield_from(emit_t *emit) {
751756
}
752757

753758
const emit_method_table_t emit_bc_method_table = {
759+
emit_bc_free,
760+
754761
emit_bc_set_native_types,
755762
emit_bc_start_pass,
756763
emit_bc_end_pass,

py/emitcpy.c

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

798798
const emit_method_table_t emit_cpython_method_table = {
799+
NULL,
800+
799801
emit_cpy_set_native_types,
800802
emit_cpy_start_pass,
801803
emit_cpy_end_pass,

py/emitnative.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ emit_t *EXPORT_FUN(new)(uint max_num_labels) {
146146
return emit;
147147
}
148148

149+
static void emit_native_free(emit_t *emit) {
150+
#if N_X64
151+
asm_x64_free(emit->as, false);
152+
#elif N_THUMB
153+
asm_thumb_free(emit->as, false);
154+
#endif
155+
m_del_obj(emit_t, emit);
156+
}
157+
149158
static void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) {
150159
emit->do_viper_types = do_viper_types;
151160
}
@@ -1226,6 +1235,8 @@ static void emit_native_yield_from(emit_t *emit) {
12261235
}
12271236

12281237
const emit_method_table_t EXPORT_FUN(method_table) = {
1238+
emit_native_free,
1239+
12291240
emit_native_set_viper_types,
12301241
emit_native_start_pass,
12311242
emit_native_end_pass,

py/emitpass1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ 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+
100102
(void*)emit_pass1_dummy,
101103
emit_pass1_start_pass,
102104
emit_pass1_end_pass,

0 commit comments

Comments
 (0)