37 #if MICROPY_ENABLE_COMPILER 39 #define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7) 40 #define DUMMY_DATA_SIZE (BYTES_FOR_INT) 46 byte dummy_data[DUMMY_DATA_SIZE];
61 size_t code_info_offset;
62 size_t code_info_size;
63 size_t bytecode_offset;
67 #if MICROPY_PERSISTENT_CODE 81 emit->max_num_labels = max_num_labels;
90 typedef byte *(*emit_allocator_t)(
emit_t *emit,
int nbytes);
94 byte buf[BYTES_FOR_INT];
95 byte *p = buf +
sizeof(buf);
101 byte *
c = allocator(emit, buf +
sizeof(buf) - p);
102 while (p != buf +
sizeof(buf) - 1) {
109 STATIC byte *emit_get_cur_to_write_code_info(
emit_t *emit,
int num_bytes_to_write) {
112 emit->code_info_offset += num_bytes_to_write;
113 return emit->dummy_data;
115 assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
116 byte *
c = emit->code_base + emit->code_info_offset;
117 emit->code_info_offset += num_bytes_to_write;
123 *emit_get_cur_to_write_code_info(emit, 1) = val;
127 emit_write_uint(emit, emit_get_cur_to_write_code_info, val);
131 #if MICROPY_PERSISTENT_CODE 133 byte *
c = emit_get_cur_to_write_code_info(emit, 2);
137 emit_write_uint(emit, emit_get_cur_to_write_code_info, qst);
141 #if MICROPY_ENABLE_SOURCE_LINE 143 assert(bytes_to_skip > 0 || lines_to_skip > 0);
145 while (bytes_to_skip > 0 || lines_to_skip > 0) {
147 if (lines_to_skip <= 6 || bytes_to_skip > 0xf) {
149 b =
MIN(bytes_to_skip, 0x1f);
150 if (b < bytes_to_skip) {
154 l =
MIN(lines_to_skip, 0x3);
156 *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
159 b =
MIN(bytes_to_skip, 0xf);
160 l =
MIN(lines_to_skip, 0x7ff);
161 byte *ci = emit_get_cur_to_write_code_info(emit, 2);
162 ci[0] = 0x80 | b | ((l >> 4) & 0x70);
172 STATIC byte *emit_get_cur_to_write_bytecode(
emit_t *emit,
int num_bytes_to_write) {
175 emit->bytecode_offset += num_bytes_to_write;
176 return emit->dummy_data;
178 assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
179 byte *
c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
180 emit->bytecode_offset += num_bytes_to_write;
186 byte *
c = emit_get_cur_to_write_bytecode(emit, 1);
191 byte *
c = emit_get_cur_to_write_bytecode(emit, 2);
198 emit_write_bytecode_byte(emit, b1);
201 byte buf[BYTES_FOR_INT];
202 byte *p = buf +
sizeof(buf);
207 }
while (num != 0 && num != -1);
210 if (num == -1 && (*p & 0x40) == 0) {
212 }
else if (num == 0 && (*p & 0x40) != 0) {
216 byte *
c = emit_get_cur_to_write_bytecode(emit, buf +
sizeof(buf) - p);
217 while (p != buf +
sizeof(buf) - 1) {
224 emit_write_bytecode_byte(emit, b);
225 emit_write_uint(emit, emit_get_cur_to_write_bytecode, val);
228 #if MICROPY_PERSISTENT_CODE 231 emit->const_table[n] =
c;
233 emit_write_bytecode_byte_uint(emit, b, n);
238 #if MICROPY_PERSISTENT_CODE 240 byte *
c = emit_get_cur_to_write_bytecode(emit, 3);
245 emit_write_bytecode_byte_uint(emit, b, qst);
250 #if MICROPY_PERSISTENT_CODE 251 emit_write_bytecode_byte_const(emit, b,
252 emit->scope->num_pos_args + emit->scope->num_kwonly_args
256 emit_write_bytecode_byte(emit, b);
266 #if MICROPY_PERSISTENT_CODE 267 emit_write_bytecode_byte_const(emit, b,
268 emit->scope->num_pos_args + emit->scope->num_kwonly_args
272 emit_write_bytecode_byte(emit, b);
273 emit->bytecode_offset = (
size_t)
MP_ALIGN(emit->bytecode_offset,
sizeof(
void*));
274 void **
c = (
void**)emit_get_cur_to_write_bytecode(emit,
sizeof(
void*));
287 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
289 byte *
c = emit_get_cur_to_write_bytecode(emit, 3);
291 c[1] = bytecode_offset;
292 c[2] = bytecode_offset >> 8;
301 bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
303 byte *
c = emit_get_cur_to_write_bytecode(emit, 3);
305 c[1] = bytecode_offset;
306 c[2] = bytecode_offset >> 8;
311 emit->stack_size = 0;
312 emit->last_emit_was_return_value =
false;
314 emit->last_source_line_offset = 0;
315 emit->last_source_line = 1;
317 memset(emit->label_offsets, -1, emit->max_num_labels *
sizeof(
mp_uint_t));
319 emit->bytecode_offset = 0;
320 emit->code_info_offset = 0;
331 emit_write_code_info_uint(emit, n_state);
337 emit_write_code_info_byte(emit, emit->scope->scope_flags);
338 emit_write_code_info_byte(emit, emit->scope->num_pos_args);
339 emit_write_code_info_byte(emit, emit->scope->num_kwonly_args);
340 emit_write_code_info_byte(emit, emit->scope->num_def_pos_args);
346 emit_write_code_info_uint(emit, emit->code_info_size - emit->code_info_offset);
348 emit_get_cur_to_write_code_info(emit, 2);
352 emit_write_code_info_qstr(emit, scope->
simple_name);
353 emit_write_code_info_qstr(emit, scope->
source_file);
360 emit_write_bytecode_byte(emit, id->
local_num);
363 emit_write_bytecode_byte(emit, 255);
365 #if MICROPY_PERSISTENT_CODE 366 emit->ct_cur_obj = 0;
367 emit->ct_cur_raw_code = 0;
387 qstr qst = MP_QSTR__star_;
406 assert(emit->stack_size == 0);
408 emit_write_code_info_byte(emit, 0);
410 #if MICROPY_PERSISTENT_CODE 412 emit->ct_num_obj = emit->ct_cur_obj;
416 #if !MICROPY_PERSISTENT_CODE 422 emit->code_info_size = emit->code_info_offset;
423 emit->bytecode_size = emit->bytecode_offset;
424 emit->code_base =
m_new0(
byte, emit->code_info_size + emit->bytecode_size);
426 #if MICROPY_PERSISTENT_CODE 428 emit->scope->num_pos_args + emit->scope->num_kwonly_args
429 + emit->ct_cur_obj + emit->ct_cur_raw_code);
432 emit->scope->num_pos_args + emit->scope->num_kwonly_args);
437 emit->code_info_size + emit->bytecode_size,
440 emit->ct_cur_obj, emit->ct_cur_raw_code,
442 emit->scope->scope_flags);
447 return emit->last_emit_was_return_value;
455 emit->stack_size += delta;
456 if (emit->stack_size > emit->scope->stack_size) {
457 emit->scope->stack_size = emit->stack_size;
459 emit->last_emit_was_return_value =
false;
462 static inline void emit_bc_pre(
emit_t *emit,
mp_int_t stack_size_delta) {
468 #if MICROPY_ENABLE_SOURCE_LINE 473 if (source_line > emit->last_source_line) {
474 mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
475 mp_uint_t lines_to_skip = source_line - emit->last_source_line;
476 emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
477 emit->last_source_line_offset = emit->bytecode_offset;
478 emit->last_source_line = source_line;
487 emit_bc_pre(emit, 0);
491 assert(l < emit->max_num_labels);
495 emit->label_offsets[l] = emit->bytecode_offset;
499 assert(emit->label_offsets[l] == emit->bytecode_offset);
504 emit_bc_pre(emit, -1);
509 emit_bc_pre(emit, 1);
514 emit_bc_pre(emit, -1);
519 emit_bc_pre(emit, 1);
532 emit_bc_pre(emit, 1);
533 if (-16 <= arg && arg <= 47) {
541 emit_bc_pre(emit, 1);
546 emit_bc_pre(emit, 1);
551 emit_bc_pre(emit, 1);
557 emit_bc_pre(emit, 1);
558 if (local_num <= 15) {
567 emit_bc_pre(emit, 1);
573 emit_bc_pre(emit, 1);
576 emit_write_bytecode_byte(emit, 0);
582 emit_bc_pre(emit, 1);
585 emit_write_bytecode_byte(emit, 0);
590 emit_bc_pre(emit, 0);
593 emit_write_bytecode_byte(emit, 0);
598 emit_bc_pre(emit, 1 - 2 * is_super);
603 emit_bc_pre(emit, 1);
608 emit_bc_pre(emit, -1);
614 emit_bc_pre(emit, -1);
615 if (local_num <= 15) {
624 emit_bc_pre(emit, -1);
629 emit_bc_pre(emit, -1);
634 emit_bc_pre(emit, -1);
639 emit_bc_pre(emit, -2);
642 emit_write_bytecode_byte(emit, 0);
647 emit_bc_pre(emit, -3);
662 emit_bc_pre(emit, 0);
667 emit_bc_pre(emit, 0);
684 emit_bc_pre(emit, 1);
689 emit_bc_pre(emit, 2);
694 emit_bc_pre(emit, -1);
699 emit_bc_pre(emit, 0);
704 emit_bc_pre(emit, 0);
709 emit_bc_pre(emit, 0);
710 emit_write_bytecode_byte_signed_label(emit,
MP_BC_JUMP, label);
714 emit_bc_pre(emit, -1);
723 emit_bc_pre(emit, -1);
732 if (except_depth == 0) {
733 emit_bc_pre(emit, 0);
752 emit_bc_pre(emit, 2);
760 emit_bc_pre(emit, 2);
762 emit_bc_pre(emit, -4);
766 emit_bc_pre(emit, 0);
771 emit_bc_pre(emit, 0);
776 emit_bc_pre(emit, -1);
786 emit_bc_pre(emit, 1);
787 emit_write_bytecode_byte_unsigned_label(emit,
MP_BC_FOR_ITER, label);
795 emit_bc_pre(emit, 0);
800 emit_bc_pre(emit, 0);
805 emit_bc_pre(emit, 0);
818 emit_bc_pre(emit, -1);
821 emit_bc_pre(emit, 0);
827 emit_bc_pre(emit, 1 - n_args);
832 emit_bc_pre(emit, 1 - n_args);
837 emit_bc_pre(emit, 1);
842 emit_bc_pre(emit, -2);
846 #if MICROPY_PY_BUILTINS_SET 848 emit_bc_pre(emit, 1 - n_args);
853 #if MICROPY_PY_BUILTINS_SLICE 855 emit_bc_pre(emit, 1 - n_args);
873 emit_bc_pre(emit, -1 - n);
875 emit_write_bytecode_byte_uint(emit,
MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
879 emit_bc_pre(emit, -1 + n_args);
884 emit_bc_pre(emit, -1 + n_left + n_right + 1);
885 emit_write_bytecode_byte_uint(emit,
MP_BC_UNPACK_EX, n_left | (n_right << 8));
889 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
890 emit_bc_pre(emit, 1);
893 emit_bc_pre(emit, -1);
899 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
900 emit_bc_pre(emit, -n_closed_over + 1);
902 emit_write_bytecode_byte(emit, n_closed_over);
904 assert(n_closed_over <= 255);
905 emit_bc_pre(emit, -2 - (
mp_int_t)n_closed_over + 1);
907 emit_write_bytecode_byte(emit, n_closed_over);
913 emit_bc_pre(emit, stack_adj - (
mp_int_t)n_positional - 2 * (
mp_int_t)n_keyword - 2);
914 emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional);
916 emit_bc_pre(emit, stack_adj - (
mp_int_t)n_positional - 2 * (
mp_int_t)n_keyword);
917 emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional);
922 emit_bc_call_function_method_helper(emit, 0,
MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
926 emit_bc_call_function_method_helper(emit, -1,
MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
930 emit_bc_pre(emit, -1);
931 emit->last_emit_was_return_value =
true;
937 emit_bc_pre(emit, -n_args);
942 emit_bc_pre(emit, 0);
948 emit_bc_pre(emit, -1);
961 #if MICROPY_EMIT_NATIVE 1032 #if MICROPY_PY_BUILTINS_SET 1033 mp_emit_bc_build_set,
1035 #if MICROPY_PY_BUILTINS_SLICE 1036 mp_emit_bc_build_slice,
1076 #endif //MICROPY_ENABLE_COMPILER
#define MP_BC_LOAD_CONST_FALSE
#define MP_ALIGN(ptr, alignment)
void mp_emit_bc_delete_global(emit_t *emit, qstr qst)
#define MP_BC_BUILD_TUPLE
void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults)
void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num)
void * memset(void *b, int c, size_t len)
void mp_emit_bc_pop_top(emit_t *emit)
void mp_emit_bc_delete_name(emit_t *emit, qstr qst)
#define MP_BC_RAISE_VARARGS
#define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC
void mp_emit_bc_rot_two(emit_t *emit)
#define MP_BC_GET_ITER_STACK
void mp_emit_bc_dup_top_two(emit_t *emit)
#define MP_BC_UNARY_OP_MULTI
void mp_emit_bc_load_const_str(emit_t *emit, qstr qst)
void mp_emit_bc_for_iter_end(emit_t *emit)
#define MP_BC_IMPORT_FROM
void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right)
#define MP_BC_LOAD_SUPER_METHOD
void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label)
#define MP_BC_BINARY_OP_MULTI
void mp_emit_bc_store_attr(emit_t *emit, qstr qst)
#define m_del(type, ptr, num)
#define MP_BC_LOAD_CONST_SMALL_INT_MULTI
void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args)
void mp_emit_bc_load_global(emit_t *emit, qstr qst)
#define MP_BC_DUP_TOP_TWO
#define MP_BC_LOAD_CONST_NONE
void mp_emit_bc_pop_block(emit_t *emit)
void mp_emit_bc_store_global(emit_t *emit, qstr qst)
#define MP_BC_LOAD_BUILD_CLASS
#define MP_OBJ_FROM_PTR(p)
void emit_bc_free(emit_t *emit)
void mp_emit_bc_end_pass(emit_t *emit)
void mp_emit_bc_import_name(emit_t *emit, qstr qst)
#define MP_BC_IMPORT_NAME
#define MP_OBJ_NEW_QSTR(qst)
void mp_emit_bc_yield_value(emit_t *emit)
#define MP_BC_MAKE_FUNCTION_DEFARGS
#define MP_BC_MAKE_CLOSURE_DEFARGS
void mp_emit_bc_load_build_class(emit_t *emit)
#define MP_BC_LOAD_CONST_TRUE
#define MP_BC_DELETE_DEREF
#define MP_SCOPE_FLAG_GENERATOR
bool mp_emit_bc_last_emit_was_return_value(emit_t *emit)
#define MP_BC_CALL_FUNCTION
void mp_emit_bc_import_star(emit_t *emit)
#define MP_BC_POP_JUMP_IF_FALSE
#define MP_BC_BUILD_SLICE
#define MP_BC_DELETE_NAME
#define MP_EMIT_BREAK_FROM_FOR
enum _mp_token_kind_t mp_token_kind_t
#define MP_BC_JUMP_IF_TRUE_OR_POP
#define MP_BC_LOAD_GLOBAL
#define MP_BC_MAKE_CLOSURE
void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth)
emit_t * emit_bc_new(void)
void mp_emit_bc_load_attr(emit_t *emit, qstr qst)
#define MICROPY_PY_BUILTINS_SET
#define MP_BC_DELETE_GLOBAL
#define MP_BC_UNPACK_SEQUENCE
#define MP_BC_STORE_GLOBAL
#define m_del_obj(type, ptr)
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst)
void mp_emit_bc_end_except_handler(emit_t *emit)
#define MP_OBJ_ITER_BUF_NSLOTS
#define MP_BC_DELETE_FAST
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops
#define MP_BC_LOAD_SUBSCR
void mp_emit_bc_load_name(emit_t *emit, qstr qst)
void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label)
void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op)
void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags)
void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj)
#define MP_BC_UNWIND_JUMP
#define MP_BC_LOAD_CONST_OBJ
#define MP_BC_CALL_METHOD
void mp_emit_bc_load_null(emit_t *emit)
void mp_emit_bc_load_subscr(emit_t *emit)
#define MP_BC_JUMP_IF_FALSE_OR_POP
void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num)
void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label)
void mp_emit_bc_pop_except(emit_t *emit)
#define MP_BC_WITH_CLEANUP
void mp_emit_bc_jump(emit_t *emit, mp_uint_t label)
void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op)
#define MP_BC_SETUP_EXCEPT
#define MP_BC_LOAD_METHOD
#define m_new0(type, num)
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops
#define MP_BC_LOAD_CONST_SMALL_INT
void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args)
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t line)
#define MP_BC_LOAD_FAST_MULTI
const struct _mp_obj_singleton_t mp_const_ellipsis_obj
void mp_emit_bc_start_except_handler(emit_t *emit)
void mp_emit_bc_delete_subscr(emit_t *emit)
void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags)
#define MP_BC_STORE_FAST_MULTI
void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg)
void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label)
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops
void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label)
#define MP_BC_MAKE_FUNCTION
void mp_emit_bc_store_subscr(emit_t *emit)
void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args)
#define MP_BC_END_FINALLY
void mp_emit_bc_rot_three(emit_t *emit)
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta)
#define MP_BC_STORE_FAST_N
void mp_emit_bc_store_name(emit_t *emit, qstr qst)
void mp_emit_bc_return_value(emit_t *emit)
void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super)
const emit_method_table_t emit_bc_method_table
void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults)
#define MP_BC_STORE_SUBSCR
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l)
void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num)
void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num)
#define MP_BC_SETUP_FINALLY
void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label)
void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num)
void mp_emit_bc_store_map(emit_t *emit)
void mp_emit_bc_yield_from(emit_t *emit)
void mp_emit_bc_import_from(emit_t *emit, qstr qst)
void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label)
void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args)
#define MP_BC_LOAD_CONST_STRING
#define MP_BC_RETURN_VALUE
void emit_bc_set_max_num_labels(emit_t *emit, mp_uint_t max_num_labels)
void mp_emit_bc_get_iter(emit_t *emit, bool use_stack)
#define MP_BC_POP_JUMP_IF_TRUE
#define MP_BC_STORE_DEREF
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t len, const mp_uint_t *const_table, mp_uint_t scope_flags)
void mp_emit_bc_dup_top(emit_t *emit)
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok)
#define MP_BC_IMPORT_STAR
#define MP_BC_YIELD_VALUE
void mp_emit_bc_end_finally(emit_t *emit)
void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args)
void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num)
#define MICROPY_PERSISTENT_CODE_SAVE
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope)
#define MP_BC_LOAD_FAST_N
void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t list_stack_index)