py/emitglue: Remove union in mp_raw_code_t to combine bytecode & native.

This commit is contained in:
Damien George 2019-02-19 14:15:39 +11:00
parent 3986820912
commit 636ed0ff8d
4 changed files with 39 additions and 47 deletions

View File

@ -67,12 +67,12 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
rc->kind = MP_CODE_BYTECODE;
rc->scope_flags = scope_flags;
rc->data.u_byte.bytecode = code;
rc->data.u_byte.const_table = const_table;
rc->fun_data = code;
rc->const_table = const_table;
#if MICROPY_PERSISTENT_CODE_SAVE
rc->data.u_byte.bc_len = len;
rc->data.u_byte.n_obj = n_obj;
rc->data.u_byte.n_raw_code = n_raw_code;
rc->fun_data_len = len;
rc->n_obj = n_obj;
rc->n_raw_code = n_raw_code;
#endif
#ifdef DEBUG_PRINT
@ -94,9 +94,9 @@ void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void
rc->kind = kind;
rc->scope_flags = scope_flags;
rc->n_pos_args = n_pos_args;
rc->data.u_native.fun_data = fun_data;
rc->data.u_native.const_table = const_table;
rc->data.u_native.type_sig = type_sig;
rc->fun_data = fun_data;
rc->const_table = const_table;
rc->type_sig = type_sig;
#ifdef DEBUG_PRINT
DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, n_pos_args, (uint)scope_flags);
@ -135,7 +135,7 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_ar
#if MICROPY_EMIT_NATIVE
case MP_CODE_NATIVE_PY:
case MP_CODE_NATIVE_VIPER:
fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->data.u_native.fun_data, rc->data.u_native.const_table);
fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->fun_data, rc->const_table);
// Check for a generator function, and if so change the type of the object
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
((mp_obj_base_t*)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
@ -144,13 +144,13 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_ar
#endif
#if MICROPY_EMIT_INLINE_ASM
case MP_CODE_NATIVE_ASM:
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->fun_data, rc->type_sig);
break;
#endif
default:
// rc->kind should always be set and BYTECODE is the only remaining case
assert(rc->kind == MP_CODE_BYTECODE);
fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->data.u_byte.bytecode, rc->data.u_byte.const_table);
fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->fun_data, rc->const_table);
// check for generator functions and if so change the type of the object
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
((mp_obj_base_t*)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;

View File

@ -52,22 +52,16 @@ typedef struct _mp_raw_code_t {
mp_uint_t kind : 3; // of type mp_raw_code_kind_t
mp_uint_t scope_flags : 7;
mp_uint_t n_pos_args : 11;
union {
struct {
const byte *bytecode;
const mp_uint_t *const_table;
#if MICROPY_PERSISTENT_CODE_SAVE
mp_uint_t bc_len;
uint16_t n_obj;
uint16_t n_raw_code;
#endif
} u_byte;
struct {
void *fun_data;
const mp_uint_t *const_table;
mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
} u_native;
} data;
const void *fun_data;
const mp_uint_t *const_table;
#if MICROPY_PERSISTENT_CODE_SAVE
size_t fun_data_len;
uint16_t n_obj;
uint16_t n_raw_code;
#endif
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
#endif
} mp_raw_code_t;
mp_raw_code_t *mp_emit_glue_new_raw_code(void);

View File

@ -449,16 +449,16 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q
}
// extract prelude
const byte *ip = rc->data.u_byte.bytecode;
const byte *ip = rc->fun_data;
const byte *ip2;
bytecode_prelude_t prelude;
extract_prelude(&ip, &ip2, &prelude);
// save prelude
size_t prelude_len = ip - rc->data.u_byte.bytecode;
const byte *ip_top = rc->data.u_byte.bytecode + rc->data.u_byte.bc_len;
mp_print_uint(print, rc->data.u_byte.bc_len);
mp_print_bytes(print, rc->data.u_byte.bytecode, prelude_len);
size_t prelude_len = ip - rc->fun_data;
const byte *ip_top = rc->fun_data + rc->fun_data_len;
mp_print_uint(print, rc->fun_data_len);
mp_print_bytes(print, rc->fun_data, prelude_len);
// save bytecode
save_bytecode(print, qstr_window, ip, ip_top);
@ -468,17 +468,17 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q
save_qstr(print, qstr_window, ip2[2] | (ip2[3] << 8)); // source_file
// save constant table
mp_print_uint(print, rc->data.u_byte.n_obj);
mp_print_uint(print, rc->data.u_byte.n_raw_code);
const mp_uint_t *const_table = rc->data.u_byte.const_table;
mp_print_uint(print, rc->n_obj);
mp_print_uint(print, rc->n_raw_code);
const mp_uint_t *const_table = rc->const_table;
for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
mp_obj_t o = (mp_obj_t)*const_table++;
save_qstr(print, qstr_window, MP_OBJ_QSTR_VALUE(o));
}
for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) {
for (uint i = 0; i < rc->n_obj; ++i) {
save_obj(print, (mp_obj_t)*const_table++);
}
for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) {
for (uint i = 0; i < rc->n_raw_code; ++i) {
save_raw_code(print, (mp_raw_code_t*)(uintptr_t)*const_table++, qstr_window);
}
}

View File

@ -391,18 +391,16 @@ class RawCode:
print(' .kind = MP_CODE_BYTECODE,')
print(' .scope_flags = 0x%02x,' % self.prelude[2])
print(' .n_pos_args = %u,' % self.prelude[3])
print(' .data.u_byte = {')
print(' .bytecode = bytecode_data_%s,' % self.escaped_name)
print(' .fun_data = bytecode_data_%s,' % self.escaped_name)
if const_table_len:
print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
print(' .const_table = (mp_uint_t*)const_table_data_%s,' % self.escaped_name)
else:
print(' .const_table = NULL,')
print(' #if MICROPY_PERSISTENT_CODE_SAVE')
print(' .bc_len = %u,' % len(self.bytecode))
print(' .n_obj = %u,' % len(self.objs))
print(' .n_raw_code = %u,' % len(self.raw_codes))
print(' #endif')
print(' },')
print(' .const_table = NULL,')
print(' #if MICROPY_PERSISTENT_CODE_SAVE')
print(' .fun_data_len = %u,' % len(self.bytecode))
print(' .n_obj = %u,' % len(self.objs))
print(' .n_raw_code = %u,' % len(self.raw_codes))
print(' #endif')
print('};')
class BytecodeBuffer: