9
9
#include "qstr.h"
10
10
#include "lexer.h"
11
11
#include "parse.h"
12
+ #include "obj.h"
13
+ #include "emitglue.h"
12
14
#include "scope.h"
13
15
#include "runtime0.h"
14
16
#include "emit.h"
15
- #include "emitglue.h"
16
17
#include "bc0.h"
17
18
18
19
struct _emit_t {
@@ -65,6 +66,10 @@ STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_writ
65
66
}
66
67
}
67
68
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
+
68
73
STATIC void emit_write_code_info_qstr (emit_t * emit , qstr qstr ) {
69
74
byte * c = emit_get_cur_to_write_code_info (emit , 4 );
70
75
// 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
98
103
}
99
104
}
100
105
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
+
101
110
STATIC void emit_write_byte_code_byte (emit_t * emit , byte b1 ) {
102
111
byte * c = emit_get_cur_to_write_byte_code (emit , 1 );
103
112
c [0 ] = b1 ;
@@ -158,6 +167,14 @@ STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
158
167
emit_write_byte_code_uint (emit , num );
159
168
}
160
169
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
+
161
178
/* currently unused
162
179
STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
163
180
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
178
195
} else {
179
196
byte_code_offset = emit -> label_offsets [label ] - emit -> byte_code_offset - 3 ;
180
197
}
181
- byte * c = emit_get_cur_to_write_byte_code (emit , 3 );
198
+ byte * c = emit_get_cur_to_write_byte_code (emit , 3 );
182
199
c [0 ] = b1 ;
183
200
c [1 ] = byte_code_offset ;
184
201
c [2 ] = byte_code_offset >> 8 ;
@@ -269,19 +286,20 @@ STATIC void emit_bc_end_pass(emit_t *emit) {
269
286
}
270
287
271
288
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
272
290
273
291
if (emit -> pass == PASS_2 ) {
274
292
// calculate size of code in bytes
275
293
emit -> code_info_size = emit -> code_info_offset ;
276
294
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 );
278
296
279
297
} else if (emit -> pass == PASS_3 ) {
280
298
qstr * arg_names = m_new (qstr , emit -> scope -> num_params );
281
299
for (int i = 0 ; i < emit -> scope -> num_params ; i ++ ) {
282
300
arg_names [i ] = emit -> scope -> id_info [i ].qstr ;
283
301
}
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 ,
285
303
emit -> code_info_size + emit -> byte_code_size ,
286
304
emit -> scope -> num_params , emit -> scope -> num_locals ,
287
305
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) {
733
751
STATIC void emit_bc_make_function (emit_t * emit , scope_t * scope , uint n_pos_defaults , uint n_kw_defaults ) {
734
752
if (n_pos_defaults == 0 && n_kw_defaults == 0 ) {
735
753
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 );
737
755
} else {
738
756
if (n_pos_defaults == 0 ) {
739
757
// 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
744
762
emit_bc_load_null (emit );
745
763
}
746
764
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 );
748
766
}
749
767
}
750
768
751
769
STATIC void emit_bc_make_closure (emit_t * emit , scope_t * scope , uint n_pos_defaults , uint n_kw_defaults ) {
752
770
if (n_pos_defaults == 0 && n_kw_defaults == 0 ) {
753
771
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 );
755
773
} else {
756
774
if (n_pos_defaults == 0 ) {
757
775
// 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
763
781
emit_bc_rot_two (emit );
764
782
}
765
783
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 );
767
785
}
768
786
}
769
787
0 commit comments