@@ -111,14 +111,25 @@ STATIC void emit_align_code_info_to_machine_word(emit_t *emit) {
111111 emit -> code_info_offset = (emit -> code_info_offset + sizeof (mp_uint_t ) - 1 ) & (~(sizeof (mp_uint_t ) - 1 ));
112112}
113113
114- STATIC void emit_write_code_info_uint (emit_t * emit , mp_uint_t val ) {
114+ STATIC void emit_write_code_info_byte (emit_t * emit , byte val ) {
115+ * emit_get_cur_to_write_code_info (emit , 1 ) = val ;
116+ }
117+
118+ STATIC void emit_write_code_info_uint (emit_t * emit , mp_uint_t val ) {
115119 emit_write_uint (emit , emit_get_cur_to_write_code_info , val );
116120}
117121
118122STATIC void emit_write_code_info_qstr (emit_t * emit , qstr qst ) {
119123 emit_write_uint (emit , emit_get_cur_to_write_code_info , qst );
120124}
121125
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+
122133#if MICROPY_ENABLE_SOURCE_LINE
123134STATIC void emit_write_code_info_bytes_lines (emit_t * emit , mp_uint_t bytes_to_skip , mp_uint_t lines_to_skip ) {
124135 assert (bytes_to_skip > 0 || lines_to_skip > 0 );
@@ -167,11 +178,7 @@ STATIC void emit_write_bytecode_byte(emit_t *emit, byte b1) {
167178 c [0 ] = b1 ;
168179}
169180
170- STATIC void emit_write_bytecode_uint (emit_t * emit , mp_uint_t val ) {
171- emit_write_uint (emit , emit_get_cur_to_write_bytecode , val );
172- }
173-
174- STATIC void emit_write_bytecode_byte_byte (emit_t * emit , byte b1 , byte b2 ) {
181+ STATIC void emit_write_bytecode_byte_byte (emit_t * emit , byte b1 , byte b2 ) {
175182 assert ((b2 & (~0xff )) == 0 );
176183 byte * c = emit_get_cur_to_write_bytecode (emit , 2 );
177184 c [0 ] = b1 ;
@@ -210,13 +217,6 @@ STATIC void emit_write_bytecode_byte_uint(emit_t *emit, byte b, mp_uint_t val) {
210217 emit_write_uint (emit , emit_get_cur_to_write_bytecode , val );
211218}
212219
213- STATIC void emit_write_bytecode_prealigned_ptr (emit_t * emit , void * ptr ) {
214- mp_uint_t * c = (mp_uint_t * )emit_get_cur_to_write_bytecode (emit , sizeof (mp_uint_t ));
215- // Verify thar c is already uint-aligned
216- assert (c == MP_ALIGN (c , sizeof (mp_uint_t )));
217- * c = (mp_uint_t )ptr ;
218- }
219-
220220// aligns the pointer so it is friendly to GC
221221STATIC void emit_write_bytecode_byte_ptr (emit_t * emit , byte b , void * ptr ) {
222222 emit_write_bytecode_byte (emit , b );
@@ -227,15 +227,7 @@ STATIC void emit_write_bytecode_byte_ptr(emit_t *emit, byte b, void *ptr) {
227227 * c = (mp_uint_t )ptr ;
228228}
229229
230- /* currently unused
231- STATIC void emit_write_bytecode_byte_uint_uint(emit_t *emit, byte b, mp_uint_t num1, mp_uint_t num2) {
232- emit_write_bytecode_byte(emit, b);
233- emit_write_bytecode_byte_uint(emit, num1);
234- emit_write_bytecode_byte_uint(emit, num2);
235- }
236- */
237-
238- STATIC void emit_write_bytecode_byte_qstr (emit_t * emit , byte b , qstr qst ) {
230+ STATIC void emit_write_bytecode_byte_qstr (emit_t * emit , byte b , qstr qst ) {
239231 emit_write_bytecode_byte_uint (emit , b , qst );
240232}
241233
@@ -289,19 +281,26 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
289281 emit -> bytecode_offset = 0 ;
290282 emit -> code_info_offset = 0 ;
291283
292- // Write code info size as compressed uint. If we are not in the final pass
293- // then space for this uint is reserved in emit_bc_end_pass.
294- if (pass == MP_PASS_EMIT ) {
295- emit_write_code_info_uint (emit , emit -> code_info_size );
284+ // Write local state size and exception stack size.
285+ {
286+ mp_uint_t n_state = scope -> num_locals + scope -> stack_size ;
287+ if (n_state == 0 ) {
288+ // Need at least 1 entry in the state, in the case an exception is
289+ // propagated through this function, the exception is returned in
290+ // the highest slot in the state (fastn[0], see vm.c).
291+ n_state = 1 ;
292+ }
293+ emit_write_code_info_uint (emit , n_state );
294+ emit_write_code_info_uint (emit , scope -> exc_stack_size );
296295 }
297296
298- // write the name and source file of this function
299- emit_write_code_info_qstr (emit , scope -> simple_name );
300- emit_write_code_info_qstr (emit , scope -> source_file );
297+ // Align code-info so that following pointers are aligned on a machine word.
298+ emit_align_code_info_to_machine_word (emit );
301299
302- // bytecode prelude: argument names (needed to resolve positional args passed as keywords)
303- // we store them as full word-sized objects for efficient access in mp_setup_code_state
304- // this is the start of the prelude and is guaranteed to be aligned on a word boundary
300+ // Write argument names (needed to resolve positional args passed as
301+ // keywords). We store them as full word-sized objects for efficient access
302+ // in mp_setup_code_state this is the start of the prelude and is guaranteed
303+ // to be aligned on a word boundary.
305304 {
306305 // For a given argument position (indexed by i) we need to find the
307306 // corresponding id_info which is a parameter, as it has the correct
@@ -322,23 +321,23 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
322321 break ;
323322 }
324323 }
325- emit_write_bytecode_prealigned_ptr (emit , MP_OBJ_NEW_QSTR (qst ));
324+ emit_write_code_info_prealigned_ptr (emit , MP_OBJ_NEW_QSTR (qst ));
326325 }
327326 }
328327
329- // bytecode prelude: local state size and exception stack size
330- {
331- mp_uint_t n_state = scope -> num_locals + scope -> stack_size ;
332- if (n_state == 0 ) {
333- // Need at least 1 entry in the state, in the case an exception is
334- // propagated through this function, the exception is returned in
335- // the highest slot in the state (fastn[0], see vm.c).
336- n_state = 1 ;
337- }
338- emit_write_bytecode_uint (emit , n_state );
339- emit_write_bytecode_uint (emit , scope -> exc_stack_size );
328+ // Write size of the rest of the code info. We don't know how big this
329+ // variable uint will be on the MP_PASS_CODE_SIZE pass so we reserve 2 bytes
330+ // for it and hope that is enough! TODO assert this or something.
331+ if (pass == MP_PASS_EMIT ) {
332+ emit_write_code_info_uint (emit , emit -> code_info_size - emit -> code_info_offset );
333+ } else {
334+ emit_get_cur_to_write_code_info (emit , 2 );
340335 }
341336
337+ // Write the name and source file of this function.
338+ emit_write_code_info_qstr (emit , scope -> simple_name );
339+ emit_write_code_info_qstr (emit , scope -> source_file );
340+
342341 // bytecode prelude: initialise closed over variables
343342 for (int i = 0 ; i < scope -> id_info_len ; i ++ ) {
344343 id_info_t * id = & scope -> id_info [i ];
@@ -360,25 +359,10 @@ void mp_emit_bc_end_pass(emit_t *emit) {
360359 mp_printf (& mp_plat_print , "ERROR: stack size not back to zero; got %d\n" , emit -> stack_size );
361360 }
362361
363- * emit_get_cur_to_write_code_info (emit , 1 ) = 0 ; // end of line number info
362+ emit_write_code_info_byte (emit , 0 ) ; // end of line number info
364363
365364 if (emit -> pass == MP_PASS_CODE_SIZE ) {
366- // Need to make sure we have enough room in the code-info block to write
367- // the size of the code-info block. Since the size is written as a
368- // compressed uint, we don't know its size until we write it! Thus, we
369- // take the biggest possible value it could be and write that here.
370- // Then there will be enough room to write the value, and any leftover
371- // space will be absorbed in the alignment at the end of the code-info
372- // block.
373- mp_uint_t max_code_info_size =
374- emit -> code_info_offset // current code-info size
375- + BYTES_FOR_INT // maximum space for compressed uint
376- + BYTES_PER_WORD - 1 ; // maximum space for alignment padding
377- emit_write_code_info_uint (emit , max_code_info_size );
378-
379- // Align code-info so that following bytecode is aligned on a machine word.
380- // We don't need to write anything here, it's just dead space between the
381- // code-info block and the bytecode block that follows it.
365+ // so bytecode is aligned
382366 emit_align_code_info_to_machine_word (emit );
383367
384368 // calculate size of total code-info + bytecode, in bytes
0 commit comments