Commit df8127a1 authored by Damien George's avatar Damien George
Browse files

py: Remove unique_codes from emitglue.c. Replace with pointers.

Attempt to address issue #386.  unique_code_id's have been removed and
replaced with a pointer to the "raw code" information.  This pointer is
stored in the actual byte code (aligned, so the GC can trace it), so
that raw code (ie byte code, native code and inline assembler) is kept
only for as long as it is needed.  In memory it's now like a tree: the
outer module's byte code points directly to its children's raw code.  So
when the outer code gets freed, if there are no remaining functions that
need the raw code, then the children's code gets freed as well.

This is pretty much like CPython does it, except that CPython stores
indexes in the byte code rather than machine pointers.  These indices
index the per-function constant table in order to find the relevant
code.
parent 68e7c514
...@@ -10,11 +10,11 @@ ...@@ -10,11 +10,11 @@
#include "qstr.h" #include "qstr.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h"
#include "emitglue.h"
#include "obj.h" #include "obj.h"
#include "emitglue.h"
#include "scope.h"
#include "emit.h"
#include "compile.h" #include "compile.h"
#include "runtime.h" #include "runtime.h"
#include "builtin.h" #include "builtin.h"
...@@ -283,7 +283,7 @@ STATIC void compile_decrease_except_level(compiler_t *comp) { ...@@ -283,7 +283,7 @@ STATIC void compile_decrease_except_level(compiler_t *comp) {
} }
STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) { STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options); scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
scope->parent = comp->scope_cur; scope->parent = comp->scope_cur;
scope->next = NULL; scope->next = NULL;
if (comp->scope_head == NULL) { if (comp->scope_head == NULL) {
...@@ -3454,7 +3454,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is ...@@ -3454,7 +3454,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
#endif // !MICROPY_EMIT_CPYTHON #endif // !MICROPY_EMIT_CPYTHON
// free the scopes // free the scopes
uint unique_code_id = module_scope->unique_code_id; mp_raw_code_t *outer_raw_code = module_scope->raw_code;
for (scope_t *s = module_scope; s;) { for (scope_t *s = module_scope; s;) {
scope_t *next = s->next; scope_t *next = s->next;
scope_free(s); scope_free(s);
...@@ -3471,12 +3471,11 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is ...@@ -3471,12 +3471,11 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
} else { } else {
#if MICROPY_EMIT_CPYTHON #if MICROPY_EMIT_CPYTHON
// can't create code, so just return true // can't create code, so just return true
(void)unique_code_id; // to suppress warning that unique_code_id is unused (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
return mp_const_true; return mp_const_true;
#else #else
// return function that executes the outer module // return function that executes the outer module
// we can free the unique_code slot because no-one has reference to this unique_code_id anymore return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
return mp_make_function_from_id_and_free(unique_code_id, MP_OBJ_NULL, MP_OBJ_NULL);
#endif #endif
} }
} }
...@@ -9,10 +9,11 @@ ...@@ -9,10 +9,11 @@
#include "qstr.h" #include "qstr.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "obj.h"
#include "emitglue.h"
#include "scope.h" #include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h" #include "emit.h"
#include "emitglue.h"
#include "bc0.h" #include "bc0.h"
struct _emit_t { struct _emit_t {
...@@ -65,6 +66,10 @@ STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_writ ...@@ -65,6 +66,10 @@ STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_writ
} }
} }
STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
}
STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) { STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
byte* c = emit_get_cur_to_write_code_info(emit, 4); byte* c = emit_get_cur_to_write_code_info(emit, 4);
// TODO variable length encoding for qstr // TODO variable length encoding for qstr
...@@ -98,6 +103,10 @@ STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_writ ...@@ -98,6 +103,10 @@ STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_writ
} }
} }
STATIC void emit_align_byte_code_to_machine_word(emit_t* emit) {
emit->byte_code_offset = (emit->byte_code_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
}
STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) { STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
byte* c = emit_get_cur_to_write_byte_code(emit, 1); byte* c = emit_get_cur_to_write_byte_code(emit, 1);
c[0] = b1; c[0] = b1;
...@@ -158,6 +167,14 @@ STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) { ...@@ -158,6 +167,14 @@ STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
emit_write_byte_code_uint(emit, num); emit_write_byte_code_uint(emit, num);
} }
// aligns the pointer so it is friendly to GC
STATIC void emit_write_byte_code_byte_ptr(emit_t* emit, byte b, void *ptr) {
emit_write_byte_code_byte(emit, b);
emit_align_byte_code_to_machine_word(emit);
machine_uint_t *c = (machine_uint_t*)emit_get_cur_to_write_byte_code(emit, sizeof(machine_uint_t));
*c = (machine_uint_t)ptr;
}
/* currently unused /* currently unused
STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) { STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
emit_write_byte_code_byte(emit, b); emit_write_byte_code_byte(emit, b);
...@@ -178,7 +195,7 @@ STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, uint ...@@ -178,7 +195,7 @@ STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, uint
} else { } else {
byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3; byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
} }
byte* c = emit_get_cur_to_write_byte_code(emit, 3); byte *c = emit_get_cur_to_write_byte_code(emit, 3);
c[0] = b1; c[0] = b1;
c[1] = byte_code_offset; c[1] = byte_code_offset;
c[2] = byte_code_offset >> 8; c[2] = byte_code_offset >> 8;
...@@ -269,19 +286,20 @@ STATIC void emit_bc_end_pass(emit_t *emit) { ...@@ -269,19 +286,20 @@ STATIC void emit_bc_end_pass(emit_t *emit) {
} }
emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info
emit_align_code_info_to_machine_word(emit); // align so that following byte_code is aligned
if (emit->pass == PASS_2) { if (emit->pass == PASS_2) {
// calculate size of code in bytes // calculate size of code in bytes
emit->code_info_size = emit->code_info_offset; emit->code_info_size = emit->code_info_offset;
emit->byte_code_size = emit->byte_code_offset; emit->byte_code_size = emit->byte_code_offset;
emit->code_base = m_new(byte, emit->code_info_size + emit->byte_code_size); emit->code_base = m_new0(byte, emit->code_info_size + emit->byte_code_size);
} else if (emit->pass == PASS_3) { } else if (emit->pass == PASS_3) {
qstr *arg_names = m_new(qstr, emit->scope->num_params); qstr *arg_names = m_new(qstr, emit->scope->num_params);
for (int i = 0; i < emit->scope->num_params; i++) { for (int i = 0; i < emit->scope->num_params; i++) {
arg_names[i] = emit->scope->id_info[i].qstr; arg_names[i] = emit->scope->id_info[i].qstr;
} }
mp_emit_glue_assign_byte_code(emit->scope->unique_code_id, emit->code_base, mp_emit_glue_assign_byte_code(emit->scope->raw_code, emit->code_base,
emit->code_info_size + emit->byte_code_size, emit->code_info_size + emit->byte_code_size,
emit->scope->num_params, emit->scope->num_locals, emit->scope->num_params, emit->scope->num_locals,
emit->scope->scope_flags, arg_names); emit->scope->scope_flags, arg_names);
...@@ -733,7 +751,7 @@ STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) { ...@@ -733,7 +751,7 @@ STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) { STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
if (n_pos_defaults == 0 && n_kw_defaults == 0) { if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_bc_pre(emit, 1); emit_bc_pre(emit, 1);
emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION, scope->unique_code_id); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
} else { } else {
if (n_pos_defaults == 0) { if (n_pos_defaults == 0) {
// load dummy entry for non-existent positional default tuple // load dummy entry for non-existent positional default tuple
...@@ -744,14 +762,14 @@ STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defau ...@@ -744,14 +762,14 @@ STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defau
emit_bc_load_null(emit); emit_bc_load_null(emit);
} }
emit_bc_pre(emit, -1); emit_bc_pre(emit, -1);
emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->unique_code_id); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
} }
} }
STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) { STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
if (n_pos_defaults == 0 && n_kw_defaults == 0) { if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_bc_pre(emit, 0); emit_bc_pre(emit, 0);
emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE, scope->unique_code_id); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
} else { } else {
if (n_pos_defaults == 0) { if (n_pos_defaults == 0) {
// load dummy entry for non-existent positional default tuple // load dummy entry for non-existent positional default tuple
...@@ -763,7 +781,7 @@ STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaul ...@@ -763,7 +781,7 @@ STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_pos_defaul
emit_bc_rot_two(emit); emit_bc_rot_two(emit);
} }
emit_bc_pre(emit, -2); emit_bc_pre(emit, -2);
emit_write_byte_code_byte_uint(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->unique_code_id); emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
} }
} }
......
...@@ -7,8 +7,10 @@ ...@@ -7,8 +7,10 @@
#include "qstr.h" #include "qstr.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "obj.h"
#include "emitglue.h"
#include "scope.h"
#include "emit.h" #include "emit.h"
#define EMIT(fun, ...) (emit_method_table->fun(emit, __VA_ARGS__)) #define EMIT(fun, ...) (emit_method_table->fun(emit, __VA_ARGS__))
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "qstr.h" #include "qstr.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "obj.h"
#include "emitglue.h"
#include "scope.h" #include "scope.h"
#include "runtime0.h" #include "runtime0.h"
#include "emit.h" #include "emit.h"
......
...@@ -23,46 +23,11 @@ ...@@ -23,46 +23,11 @@
#define DEBUG_OP_printf(...) (void)0 #define DEBUG_OP_printf(...) (void)0
#endif #endif
typedef enum {
MP_CODE_UNUSED,
MP_CODE_RESERVED,
MP_CODE_BYTE,
MP_CODE_NATIVE,
MP_CODE_INLINE_ASM,
} mp_code_kind_t;
typedef struct _mp_code_t {
mp_code_kind_t kind : 8;
uint scope_flags : 8;
uint n_args : 16;
union {
struct {
byte *code;
uint len;
} u_byte;
struct {
mp_fun_t fun;
} u_native;
struct {
void *fun;
} u_inline_asm;
};
qstr *arg_names;
} mp_code_t;
STATIC machine_uint_t unique_codes_alloc = 0;
STATIC machine_uint_t unique_codes_total = 0; // always >= unique_codes_alloc
STATIC mp_code_t *unique_codes = NULL;
#ifdef WRITE_CODE #ifdef WRITE_CODE
FILE *fp_write_code = NULL; FILE *fp_write_code = NULL;
#endif #endif
void mp_emit_glue_init(void) { void mp_emit_glue_init(void) {
unique_codes_alloc = 0;
unique_codes_total = 0;
unique_codes = NULL;
#ifdef WRITE_CODE #ifdef WRITE_CODE
fp_write_code = fopen("out-code", "wb"); fp_write_code = fopen("out-code", "wb");
#endif #endif
...@@ -74,50 +39,24 @@ void mp_emit_glue_deinit(void) { ...@@ -74,50 +39,24 @@ void mp_emit_glue_deinit(void) {
fclose(fp_write_code); fclose(fp_write_code);
} }
#endif #endif
m_del(mp_code_t, unique_codes, unique_codes_alloc);
}
uint mp_emit_glue_get_unique_code_id(void) {
// look for an existing unused slot
for (uint i = 0; i < unique_codes_alloc; i++) {
if (unique_codes[i].kind == MP_CODE_UNUSED) {
unique_codes[i].kind = MP_CODE_RESERVED;
return i;
}
}
// no existing slot
// return next available id, memory will be allocated later
return unique_codes_total++;
} }
STATIC void mp_emit_glue_alloc_unique_codes(void) { mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
if (unique_codes_total > unique_codes_alloc) { mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
DEBUG_printf("allocate more unique codes: " UINT_FMT " -> %u\n", unique_codes_alloc, unique_codes_total); rc->kind = MP_CODE_RESERVED;
// increase size of unique_codes table (all new entries are already reserved) return rc;
unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, unique_codes_total);
for (uint i = unique_codes_alloc; i < unique_codes_total; i++) {
unique_codes[i].kind = MP_CODE_RESERVED;
}
unique_codes_alloc = unique_codes_total;
}
} }
void mp_emit_glue_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, uint scope_flags, qstr *arg_names) { void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, int n_args, int n_locals, uint scope_flags, qstr *arg_names) {
mp_emit_glue_alloc_unique_codes(); rc->kind = MP_CODE_BYTE;
rc->scope_flags = scope_flags;
assert(unique_code_id < unique_codes_alloc && unique_codes[unique_code_id].kind == MP_CODE_RESERVED); rc->n_args = n_args;
unique_codes[unique_code_id].kind = MP_CODE_BYTE; rc->u_byte.code = code;
unique_codes[unique_code_id].scope_flags = scope_flags; rc->u_byte.len = len;
unique_codes[unique_code_id].n_args = n_args; rc->arg_names = arg_names;
unique_codes[unique_code_id].u_byte.code = code;
unique_codes[unique_code_id].u_byte.len = len;
unique_codes[unique_code_id].arg_names = arg_names;
//printf("byte code: %d bytes\n", len);
#ifdef DEBUG_PRINT #ifdef DEBUG_PRINT
DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d n_locals=%d\n", unique_code_id, code, len, n_args, n_locals); DEBUG_printf("assign byte code: code=%p len=%u n_args=%d n_locals=%d\n", code, len, n_args, n_locals);
for (int i = 0; i < 128 && i < len; i++) { for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) { if (i > 0 && i % 16 == 0) {
DEBUG_printf("\n"); DEBUG_printf("\n");
...@@ -131,19 +70,14 @@ void mp_emit_glue_assign_byte_code(uint unique_code_id, byte *code, uint len, in ...@@ -131,19 +70,14 @@ void mp_emit_glue_assign_byte_code(uint unique_code_id, byte *code, uint len, in
#endif #endif
} }
void mp_emit_glue_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) { void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
mp_emit_glue_alloc_unique_codes(); rc->kind = MP_CODE_NATIVE;
rc->scope_flags = 0;
assert(unique_code_id < unique_codes_alloc && unique_codes[unique_code_id].kind == MP_CODE_RESERVED); rc->n_args = n_args;
unique_codes[unique_code_id].kind = MP_CODE_NATIVE; rc->u_native.fun = fun;
unique_codes[unique_code_id].scope_flags = 0;
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].u_native.fun = fun;
//printf("native code: %d bytes\n", len);
#ifdef DEBUG_PRINT #ifdef DEBUG_PRINT
DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args); DEBUG_printf("assign native code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
for (int i = 0; i < 128 && i < len; i++) { for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) { if (i > 0 && i % 16 == 0) {
...@@ -162,17 +96,14 @@ void mp_emit_glue_assign_native_code(uint unique_code_id, void *fun, uint len, i ...@@ -162,17 +96,14 @@ void mp_emit_glue_assign_native_code(uint unique_code_id, void *fun, uint len, i
#endif #endif
} }
void mp_emit_glue_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) { void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
mp_emit_glue_alloc_unique_codes(); rc->kind = MP_CODE_INLINE_ASM;
rc->scope_flags = 0;
assert(unique_code_id < unique_codes_alloc && unique_codes[unique_code_id].kind == MP_CODE_RESERVED); rc->n_args = n_args;
unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM; rc->u_inline_asm.fun = fun;
unique_codes[unique_code_id].scope_flags = 0;
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].u_inline_asm.fun = fun;
#ifdef DEBUG_PRINT #ifdef DEBUG_PRINT
DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args); DEBUG_printf("assign inline asm code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
for (int i = 0; i < 128 && i < len; i++) { for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) { if (i > 0 && i % 16 == 0) {
...@@ -191,12 +122,9 @@ void mp_emit_glue_assign_inline_asm_code(uint unique_code_id, void *fun, uint le ...@@ -191,12 +122,9 @@ void mp_emit_glue_assign_inline_asm_code(uint unique_code_id, void *fun, uint le
#endif #endif
} }
mp_obj_t mp_make_function_from_id(uint unique_code_id, mp_obj_t def_args, mp_obj_t def_kw_args) { mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id); DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
if (unique_code_id >= unique_codes_total) { assert(rc != NULL);
// illegal code id
return mp_const_none;
}
// def_args must be MP_OBJ_NULL or a tuple // def_args must be MP_OBJ_NULL or a tuple
assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple)); assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
...@@ -204,49 +132,36 @@ mp_obj_t mp_make_function_from_id(uint unique_code_id, mp_obj_t def_args, mp_obj ...@@ -204,49 +132,36 @@ mp_obj_t mp_make_function_from_id(uint unique_code_id, mp_obj_t def_args, mp_obj
// TODO implement default kw args // TODO implement default kw args
assert(def_kw_args == MP_OBJ_NULL); assert(def_kw_args == MP_OBJ_NULL);
// make the function, depending on the code kind // make the function, depending on the raw code kind
mp_code_t *c = &unique_codes[unique_code_id];
mp_obj_t fun; mp_obj_t fun;
switch (c->kind) { switch (rc->kind) {
case MP_CODE_BYTE: case MP_CODE_BYTE:
fun = mp_obj_new_fun_bc(c->scope_flags, c->arg_names, c->n_args, def_args, c->u_byte.code); fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_args, def_args, rc->u_byte.code);
break; break;
case MP_CODE_NATIVE: case MP_CODE_NATIVE:
fun = mp_make_function_n(c->n_args, c->u_native.fun); fun = mp_make_function_n(rc->n_args, rc->u_native.fun);
break; break;
case MP_CODE_INLINE_ASM: case MP_CODE_INLINE_ASM:
fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun); fun = mp_obj_new_fun_asm(rc->n_args, rc->u_inline_asm.fun);
break; break;
default: default:
// code id was never assigned (this should not happen) // raw code was never set (this should not happen)
assert(0); assert(0);
return mp_const_none; return mp_const_none;
} }
// check for generator functions and if so wrap in generator object // check for generator functions and if so wrap in generator object
if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
fun = mp_obj_new_gen_wrap(fun); fun = mp_obj_new_gen_wrap(fun);
} }
return fun; return fun;
} }
mp_obj_t mp_make_function_from_id_and_free(uint unique_code_id, mp_obj_t def_args, mp_obj_t def_kw_args) { mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_obj_t closure_tuple, mp_obj_t def_args, mp_obj_t def_kw_args) {
mp_obj_t f = mp_make_function_from_id(unique_code_id, def_args, def_kw_args); DEBUG_OP_printf("make_closure_from_raw_code %p\n", rc);
// in some cases we can free the unique_code slot
// any dynamically allocated memory is now owned by the fun object
mp_code_t *c = &unique_codes[unique_code_id];
memset(c, 0, sizeof *c); // make sure all pointers are zeroed
c->kind = MP_CODE_UNUSED;
return f;
}
mp_obj_t mp_make_closure_from_id(uint unique_code_id, mp_obj_t closure_tuple, mp_obj_t def_args, mp_obj_t def_kw_args) {
DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
// make function object // make function object
mp_obj_t ffun = mp_make_function_from_id(unique_code_id, def_args, def_kw_args); mp_obj_t ffun = mp_make_function_from_raw_code(rc, def_args, def_kw_args);
// wrap function in closure object // wrap function in closure object
return mp_obj_new_closure(ffun, closure_tuple); return mp_obj_new_closure(ffun, closure_tuple);
} }
// These variables and functions glue the code emitters to the runtime. // These variables and functions glue the code emitters to the runtime.
typedef enum {
MP_CODE_UNUSED,
MP_CODE_RESERVED,
MP_CODE_BYTE,
MP_CODE_NATIVE,
MP_CODE_INLINE_ASM,
} mp_raw_code_kind_t;
typedef struct _mp_code_t {
mp_raw_code_kind_t kind : 8;
uint scope_flags : 8;
uint n_args : 16;
union {
struct {
byte *code;
uint len;
} u_byte;
struct {
mp_fun_t fun;
} u_native;
struct {
void *fun;
} u_inline_asm;
};
qstr *arg_names;
} mp_raw_code_t;
void mp_emit_glue_init(void); void mp_emit_glue_init(void);
void mp_emit_glue_deinit(void); void mp_emit_glue_deinit(void);
uint mp_emit_glue_get_unique_code_id(void);
void mp_emit_glue_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, uint scope_flags, qstr *arg_names); mp_raw_code_t *mp_emit_glue_new_raw_code(void);
void mp_emit_glue_assign_native_code(uint unique_code_id, void *f, uint len, int n_args);
void mp_emit_glue_assign_inline_asm_code(uint unique_code_id, void *f, uint len, int n_args); void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, int n_args, int n_locals, uint scope_flags, qstr *arg_names);
void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *f, uint len, int n_args);
void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *f, uint len, int n_args);
mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args);
mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_obj_t closure_tuple, mp_obj_t def_args, mp_obj_t def_kw_args);
...@@ -9,10 +9,11 @@ ...@@ -9,10 +9,11 @@
#include "qstr.h" #include "qstr.h"
#include "lexer.h" #include "lexer.h"
#include "parse.h" #include "parse.h"
#include "obj.h"
#include "emitglue.h"
#include "scope.h" #include "scope.h"