@@ -56,6 +56,7 @@ struct _emit_t {
5656 mp_uint_t bytecode_offset ;
5757 mp_uint_t bytecode_size ;
5858 byte * code_base ; // stores both byte code and code info
59+ mp_uint_t * const_table ;
5960 // Accessed as mp_uint_t, so must be aligned as such
6061 byte dummy_data [DUMMY_DATA_SIZE ];
6162};
@@ -123,13 +124,6 @@ STATIC void emit_write_code_info_qstr(emit_t *emit, qstr qst) {
123124 emit_write_uint (emit , emit_get_cur_to_write_code_info , qst );
124125}
125126
126- STATIC void emit_write_code_info_prealigned_ptr (emit_t * emit , void * ptr ) {
127- mp_uint_t * c = (mp_uint_t * )emit_get_cur_to_write_code_info (emit , sizeof (mp_uint_t ));
128- // Verify thar c is already uint-aligned
129- assert (c == MP_ALIGN (c , sizeof (mp_uint_t )));
130- * c = (mp_uint_t )ptr ;
131- }
132-
133127#if MICROPY_ENABLE_SOURCE_LINE
134128STATIC void emit_write_code_info_bytes_lines (emit_t * emit , mp_uint_t bytes_to_skip , mp_uint_t lines_to_skip ) {
135129 assert (bytes_to_skip > 0 || lines_to_skip > 0 );
@@ -301,37 +295,6 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
301295 emit_write_code_info_byte (emit , emit -> scope -> num_kwonly_args );
302296 emit_write_code_info_byte (emit , emit -> scope -> num_def_pos_args );
303297
304- // Align code-info so that following pointers are aligned on a machine word.
305- emit_align_code_info_to_machine_word (emit );
306-
307- // Write argument names (needed to resolve positional args passed as
308- // keywords). We store them as full word-sized objects for efficient access
309- // in mp_setup_code_state this is the start of the prelude and is guaranteed
310- // to be aligned on a word boundary.
311- {
312- // For a given argument position (indexed by i) we need to find the
313- // corresponding id_info which is a parameter, as it has the correct
314- // qstr name to use as the argument name. Note that it's not a simple
315- // 1-1 mapping (ie i!=j in general) because of possible closed-over
316- // variables. In the case that the argument i has no corresponding
317- // parameter we use "*" as its name (since no argument can ever be named
318- // "*"). We could use a blank qstr but "*" is better for debugging.
319- // Note: there is some wasted RAM here for the case of storing a qstr
320- // for each closed-over variable, and maybe there is a better way to do
321- // it, but that would require changes to mp_setup_code_state.
322- for (int i = 0 ; i < scope -> num_pos_args + scope -> num_kwonly_args ; i ++ ) {
323- qstr qst = MP_QSTR__star_ ;
324- for (int j = 0 ; j < scope -> id_info_len ; ++ j ) {
325- id_info_t * id = & scope -> id_info [j ];
326- if ((id -> flags & ID_FLAG_IS_PARAM ) && id -> local_num == i ) {
327- qst = id -> qst ;
328- break ;
329- }
330- }
331- emit_write_code_info_prealigned_ptr (emit , MP_OBJ_NEW_QSTR (qst ));
332- }
333- }
334-
335298 // Write size of the rest of the code info. We don't know how big this
336299 // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
337300 // for it and hope that is enough! TODO assert this or something.
@@ -354,6 +317,35 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
354317 }
355318 }
356319 emit_write_bytecode_byte (emit , 255 ); // end of list sentinel
320+
321+ if (pass == MP_PASS_EMIT ) {
322+ // Write argument names (needed to resolve positional args passed as
323+ // keywords). We store them as full word-sized objects for efficient access
324+ // in mp_setup_code_state this is the start of the prelude and is guaranteed
325+ // to be aligned on a word boundary.
326+
327+ // For a given argument position (indexed by i) we need to find the
328+ // corresponding id_info which is a parameter, as it has the correct
329+ // qstr name to use as the argument name. Note that it's not a simple
330+ // 1-1 mapping (ie i!=j in general) because of possible closed-over
331+ // variables. In the case that the argument i has no corresponding
332+ // parameter we use "*" as its name (since no argument can ever be named
333+ // "*"). We could use a blank qstr but "*" is better for debugging.
334+ // Note: there is some wasted RAM here for the case of storing a qstr
335+ // for each closed-over variable, and maybe there is a better way to do
336+ // it, but that would require changes to mp_setup_code_state.
337+ for (int i = 0 ; i < scope -> num_pos_args + scope -> num_kwonly_args ; i ++ ) {
338+ qstr qst = MP_QSTR__star_ ;
339+ for (int j = 0 ; j < scope -> id_info_len ; ++ j ) {
340+ id_info_t * id = & scope -> id_info [j ];
341+ if ((id -> flags & ID_FLAG_IS_PARAM ) && id -> local_num == i ) {
342+ qst = id -> qst ;
343+ break ;
344+ }
345+ }
346+ emit -> const_table [i ] = (mp_uint_t )MP_OBJ_NEW_QSTR (qst );
347+ }
348+ }
357349}
358350
359351void mp_emit_bc_end_pass (emit_t * emit ) {
@@ -377,9 +369,12 @@ void mp_emit_bc_end_pass(emit_t *emit) {
377369 emit -> bytecode_size = emit -> bytecode_offset ;
378370 emit -> code_base = m_new0 (byte , emit -> code_info_size + emit -> bytecode_size );
379371
372+ emit -> const_table = m_new0 (mp_uint_t , emit -> scope -> num_pos_args + emit -> scope -> num_kwonly_args );
373+
380374 } else if (emit -> pass == MP_PASS_EMIT ) {
381375 mp_emit_glue_assign_bytecode (emit -> scope -> raw_code , emit -> code_base ,
382- emit -> code_info_size + emit -> bytecode_size , emit -> scope -> scope_flags );
376+ emit -> code_info_size + emit -> bytecode_size ,
377+ emit -> const_table , emit -> scope -> scope_flags );
383378 }
384379}
385380
0 commit comments