99#include "qstr.h"
1010#include "lexer.h"
1111#include "parse.h"
12+ #include "obj.h"
13+ #include "emitglue.h"
1214#include "scope.h"
1315#include "runtime0.h"
1416#include "emit.h"
15- #include "emitglue.h"
1617#include "bc0.h"
1718
1819struct _emit_t {
@@ -65,6 +66,10 @@ STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_writ
6566 }
6667}
6768
69+ STATIC void emit_align_code_info_to_machine_word (emit_t * emit ) {
70+ emit -> code_info_offset = (emit -> code_info_offset + sizeof (machine_uint_t ) - 1 ) & (~(sizeof (machine_uint_t ) - 1 ));
71+ }
72+
6873STATIC void emit_write_code_info_qstr (emit_t * emit , qstr qstr ) {
6974 byte * c = emit_get_cur_to_write_code_info (emit , 4 );
7075 // 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
98103 }
99104}
100105
106+ STATIC void emit_align_byte_code_to_machine_word (emit_t * emit ) {
107+ emit -> byte_code_offset = (emit -> byte_code_offset + sizeof (machine_uint_t ) - 1 ) & (~(sizeof (machine_uint_t ) - 1 ));
108+ }
109+
101110STATIC void emit_write_byte_code_byte (emit_t * emit , byte b1 ) {
102111 byte * c = emit_get_cur_to_write_byte_code (emit , 1 );
103112 c [0 ] = b1 ;
@@ -158,6 +167,14 @@ STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
158167 emit_write_byte_code_uint (emit , num );
159168}
160169
170+ // aligns the pointer so it is friendly to GC
171+ STATIC void emit_write_byte_code_byte_ptr (emit_t * emit , byte b , void * ptr ) {
172+ emit_write_byte_code_byte (emit , b );
173+ emit_align_byte_code_to_machine_word (emit );
174+ machine_uint_t * c = (machine_uint_t * )emit_get_cur_to_write_byte_code (emit , sizeof (machine_uint_t ));
175+ * c = (machine_uint_t )ptr ;
176+ }
177+
161178/* currently unused
162179STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
163180 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
178195 } else {
179196 byte_code_offset = emit -> label_offsets [label ] - emit -> byte_code_offset - 3 ;
180197 }
181- byte * c = emit_get_cur_to_write_byte_code (emit , 3 );
198+ byte * c = emit_get_cur_to_write_byte_code (emit , 3 );
182199 c [0 ] = b1 ;
183200 c [1 ] = byte_code_offset ;
184201 c [2 ] = byte_code_offset >> 8 ;
@@ -269,19 +286,20 @@ STATIC void emit_bc_end_pass(emit_t *emit) {
269286 }
270287
271288 emit_write_code_info_bytes_lines (emit , 0 , 0 ); // end of line number info
289+ emit_align_code_info_to_machine_word (emit ); // align so that following byte_code is aligned
272290
273291 if (emit -> pass == PASS_2 ) {
274292 // calculate size of code in bytes
275293 emit -> code_info_size = emit -> code_info_offset ;
276294 emit -> byte_code_size = emit -> byte_code_offset ;
277- emit -> code_base = m_new (byte , emit -> code_info_size + emit -> byte_code_size );
295+ emit -> code_base = m_new0 (byte , emit -> code_info_size + emit -> byte_code_size );
278296
279297 } else if (emit -> pass == PASS_3 ) {
280298 qstr * arg_names = m_new (qstr , emit -> scope -> num_params );
281299 for (int i = 0 ; i < emit -> scope -> num_params ; i ++ ) {
282300 arg_names [i ] = emit -> scope -> id_info [i ].qstr ;
283301 }
284- mp_emit_glue_assign_byte_code (emit -> scope -> unique_code_id , emit -> code_base ,
302+ mp_emit_glue_assign_byte_code (emit -> scope -> raw_code , emit -> code_base ,
285303 emit -> code_info_size + emit -> byte_code_size ,
286304 emit -> scope -> num_params , emit -> scope -> num_locals ,
287305 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) {
733751STATIC void emit_bc_make_function (emit_t * emit , scope_t * scope , uint n_pos_defaults , uint n_kw_defaults ) {
734752 if (n_pos_defaults == 0 && n_kw_defaults == 0 ) {
735753 emit_bc_pre (emit , 1 );
736- emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_FUNCTION , scope -> unique_code_id );
754+ emit_write_byte_code_byte_ptr (emit , MP_BC_MAKE_FUNCTION , scope -> raw_code );
737755 } else {
738756 if (n_pos_defaults == 0 ) {
739757 // 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
744762 emit_bc_load_null (emit );
745763 }
746764 emit_bc_pre (emit , -1 );
747- emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_FUNCTION_DEFARGS , scope -> unique_code_id );
765+ emit_write_byte_code_byte_ptr (emit , MP_BC_MAKE_FUNCTION_DEFARGS , scope -> raw_code );
748766 }
749767}
750768
751769STATIC void emit_bc_make_closure (emit_t * emit , scope_t * scope , uint n_pos_defaults , uint n_kw_defaults ) {
752770 if (n_pos_defaults == 0 && n_kw_defaults == 0 ) {
753771 emit_bc_pre (emit , 0 );
754- emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_CLOSURE , scope -> unique_code_id );
772+ emit_write_byte_code_byte_ptr (emit , MP_BC_MAKE_CLOSURE , scope -> raw_code );
755773 } else {
756774 if (n_pos_defaults == 0 ) {
757775 // 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
763781 emit_bc_rot_two (emit );
764782 }
765783 emit_bc_pre (emit , -2 );
766- emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_CLOSURE_DEFARGS , scope -> unique_code_id );
784+ emit_write_byte_code_byte_ptr (emit , MP_BC_MAKE_CLOSURE_DEFARGS , scope -> raw_code );
767785 }
768786}
769787
0 commit comments