@@ -107,6 +107,22 @@ static void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
107107 c [1 ] = b2 ;
108108}
109109
110+ static void emit_write_byte_code_uint (emit_t * emit , uint num ) {
111+ if (num <= 127 ) { // fits in 0x7f
112+ // fit argument in single byte
113+ byte * c = emit_get_cur_to_write_byte_code (emit , 1 );
114+ c [0 ] = num ;
115+ } else if (num <= 16383 ) { // fits in 0x3fff
116+ // fit argument in two bytes
117+ byte * c = emit_get_cur_to_write_byte_code (emit , 2 );
118+ c [0 ] = (num >> 8 ) | 0x80 ;
119+ c [1 ] = num ;
120+ } else {
121+ // larger numbers not implemented/supported
122+ assert (0 );
123+ }
124+ }
125+
110126// integers (for small ints) are stored as 24 bits, in excess
111127static void emit_write_byte_code_byte_int (emit_t * emit , byte b1 , machine_int_t num ) {
112128 num += 0x800000 ;
@@ -118,26 +134,21 @@ static void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t n
118134 c [3 ] = num >> 16 ;
119135}
120136
121- static void emit_write_byte_code_byte_uint (emit_t * emit , byte b1 , uint num ) {
122- if (num <= 127 ) { // fits in 0x7f
123- // fit argument in single byte
124- byte * c = emit_get_cur_to_write_byte_code (emit , 2 );
125- c [0 ] = b1 ;
126- c [1 ] = num ;
127- } else if (num <= 16383 ) { // fits in 0x3fff
128- // fit argument in two bytes
129- byte * c = emit_get_cur_to_write_byte_code (emit , 3 );
130- c [0 ] = b1 ;
131- c [1 ] = (num >> 8 ) | 0x80 ;
132- c [2 ] = num ;
133- } else {
134- // larger numbers not implemented/supported
135- assert (0 );
136- }
137+ static void emit_write_byte_code_byte_uint (emit_t * emit , byte b , uint num ) {
138+ emit_write_byte_code_byte (emit , b );
139+ emit_write_byte_code_uint (emit , num );
137140}
138141
139- static void emit_write_byte_code_byte_qstr (emit_t * emit , byte b1 , qstr qstr ) {
140- emit_write_byte_code_byte_uint (emit , b1 , qstr );
142+ /* currently unused
143+ static void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
144+ emit_write_byte_code_byte(emit, b);
145+ emit_write_byte_code_byte_uint(emit, num1);
146+ emit_write_byte_code_byte_uint(emit, num2);
147+ }
148+ */
149+
150+ static void emit_write_byte_code_byte_qstr (emit_t * emit , byte b , qstr qstr ) {
151+ emit_write_byte_code_byte_uint (emit , b , qstr );
141152}
142153
143154// unsigned labels are relative to ip following this instruction, stored as 16 bits
@@ -665,13 +676,13 @@ static void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
665676
666677static void emit_bc_make_function (emit_t * emit , scope_t * scope , int n_dict_params , int n_default_params ) {
667678 assert (n_dict_params == 0 );
668- if (n_default_params != 0 ) {
679+ if (n_default_params == 0 ) {
680+ emit_pre (emit , 1 );
681+ emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_FUNCTION , scope -> unique_code_id );
682+ } else {
669683 emit_bc_build_tuple (emit , n_default_params );
670684 emit_pre (emit , 0 );
671685 emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_FUNCTION_DEFARGS , scope -> unique_code_id );
672- } else {
673- emit_pre (emit , 1 );
674- emit_write_byte_code_byte_uint (emit , MP_BC_MAKE_FUNCTION , scope -> unique_code_id );
675686 }
676687}
677688
0 commit comments