@@ -122,23 +122,15 @@ STATIC void dump_args(const mp_obj_t *a, size_t sz) {
122
122
// contain the following valid entries:
123
123
// - code_state->fun_bc should contain a pointer to the function object
124
124
// - code_state->ip should contain a pointer to the beginning of the prelude
125
+ // - code_state->sp should be: &code_state->state[0] - 1
125
126
// - code_state->n_state should be the number of objects in the local state
126
- void mp_setup_code_state (mp_code_state_t * code_state , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
127
+ STATIC void mp_setup_code_state_helper (mp_code_state_t * code_state , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
127
128
// This function is pretty complicated. It's main aim is to be efficient in speed and RAM
128
129
// usage for the common case of positional only args.
129
130
130
131
// get the function object that we want to set up (could be bytecode or native code)
131
132
mp_obj_fun_bc_t * self = code_state -> fun_bc ;
132
133
133
- #if MICROPY_STACKLESS
134
- code_state -> prev = NULL ;
135
- #endif
136
-
137
- #if MICROPY_PY_SYS_SETTRACE
138
- code_state -> prev_state = NULL ;
139
- code_state -> frame = NULL ;
140
- #endif
141
-
142
134
// Get cached n_state (rather than decode it again)
143
135
size_t n_state = code_state -> n_state ;
144
136
@@ -149,16 +141,16 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
149
141
(void )n_state_unused ;
150
142
(void )n_exc_stack_unused ;
151
143
152
- code_state -> sp = & code_state -> state [ 0 ] - 1 ;
144
+ mp_obj_t * code_state_state = code_state -> sp + 1 ;
153
145
code_state -> exc_sp_idx = 0 ;
154
146
155
147
// zero out the local stack to begin with
156
- memset (code_state -> state , 0 , n_state * sizeof (* code_state -> state ));
148
+ memset (code_state_state , 0 , n_state * sizeof (* code_state -> state ));
157
149
158
150
const mp_obj_t * kwargs = args + n_args ;
159
151
160
152
// var_pos_kw_args points to the stack where the var-args tuple, and var-kw dict, should go (if they are needed)
161
- mp_obj_t * var_pos_kw_args = & code_state -> state [n_state - 1 - n_pos_args - n_kwonly_args ];
153
+ mp_obj_t * var_pos_kw_args = & code_state_state [n_state - 1 - n_pos_args - n_kwonly_args ];
162
154
163
155
// check positional arguments
164
156
@@ -181,7 +173,7 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
181
173
if (n_args >= (size_t )(n_pos_args - n_def_pos_args )) {
182
174
// given enough arguments, but may need to use some default arguments
183
175
for (size_t i = n_args ; i < n_pos_args ; i ++ ) {
184
- code_state -> state [n_state - 1 - i ] = self -> extra_args [i - (n_pos_args - n_def_pos_args )];
176
+ code_state_state [n_state - 1 - i ] = self -> extra_args [i - (n_pos_args - n_def_pos_args )];
185
177
}
186
178
} else {
187
179
fun_pos_args_mismatch (self , n_pos_args - n_def_pos_args , n_args );
@@ -191,14 +183,14 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
191
183
192
184
// copy positional args into state
193
185
for (size_t i = 0 ; i < n_args ; i ++ ) {
194
- code_state -> state [n_state - 1 - i ] = args [i ];
186
+ code_state_state [n_state - 1 - i ] = args [i ];
195
187
}
196
188
197
189
// check keyword arguments
198
190
199
191
if (n_kw != 0 || (scope_flags & MP_SCOPE_FLAG_DEFKWARGS ) != 0 ) {
200
192
DEBUG_printf ("Initial args: " );
201
- dump_args (code_state -> state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
193
+ dump_args (code_state_state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
202
194
203
195
mp_obj_t dict = MP_OBJ_NULL ;
204
196
if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS ) != 0 ) {
@@ -220,11 +212,11 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
220
212
arg_qstr = self -> context -> constants .qstr_table [arg_qstr ];
221
213
#endif
222
214
if (wanted_arg_name == MP_OBJ_NEW_QSTR (arg_qstr )) {
223
- if (code_state -> state [n_state - 1 - j ] != MP_OBJ_NULL ) {
215
+ if (code_state_state [n_state - 1 - j ] != MP_OBJ_NULL ) {
224
216
mp_raise_msg_varg (& mp_type_TypeError ,
225
217
MP_ERROR_TEXT ("function got multiple values for argument '%q'" ), MP_OBJ_QSTR_VALUE (wanted_arg_name ));
226
218
}
227
- code_state -> state [n_state - 1 - j ] = kwargs [2 * i + 1 ];
219
+ code_state_state [n_state - 1 - j ] = kwargs [2 * i + 1 ];
228
220
goto continue2 ;
229
221
}
230
222
}
@@ -242,10 +234,10 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
242
234
}
243
235
244
236
DEBUG_printf ("Args with kws flattened: " );
245
- dump_args (code_state -> state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
237
+ dump_args (code_state_state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
246
238
247
239
// fill in defaults for positional args
248
- mp_obj_t * d = & code_state -> state [n_state - n_pos_args ];
240
+ mp_obj_t * d = & code_state_state [n_state - n_pos_args ];
249
241
mp_obj_t * s = & self -> extra_args [n_def_pos_args - 1 ];
250
242
for (size_t i = n_def_pos_args ; i > 0 ; i -- , d ++ , s -- ) {
251
243
if (* d == MP_OBJ_NULL ) {
@@ -254,13 +246,13 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
254
246
}
255
247
256
248
DEBUG_printf ("Args after filling default positional: " );
257
- dump_args (code_state -> state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
249
+ dump_args (code_state_state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
258
250
259
251
// Check that all mandatory positional args are specified
260
- while (d < & code_state -> state [n_state ]) {
252
+ while (d < & code_state_state [n_state ]) {
261
253
if (* d ++ == MP_OBJ_NULL ) {
262
254
mp_raise_msg_varg (& mp_type_TypeError ,
263
- MP_ERROR_TEXT ("function missing required positional argument #%d" ), & code_state -> state [n_state ] - d );
255
+ MP_ERROR_TEXT ("function missing required positional argument #%d" ), & code_state_state [n_state ] - d );
264
256
}
265
257
}
266
258
@@ -275,13 +267,13 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
275
267
#if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
276
268
arg_qstr = self -> context -> constants .qstr_table [arg_qstr ];
277
269
#endif
278
- if (code_state -> state [n_state - 1 - n_pos_args - i ] == MP_OBJ_NULL ) {
270
+ if (code_state_state [n_state - 1 - n_pos_args - i ] == MP_OBJ_NULL ) {
279
271
mp_map_elem_t * elem = NULL ;
280
272
if ((scope_flags & MP_SCOPE_FLAG_DEFKWARGS ) != 0 ) {
281
273
elem = mp_map_lookup (& ((mp_obj_dict_t * )MP_OBJ_TO_PTR (self -> extra_args [n_def_pos_args ]))-> map , MP_OBJ_NEW_QSTR (arg_qstr ), MP_MAP_LOOKUP );
282
274
}
283
275
if (elem != NULL ) {
284
- code_state -> state [n_state - 1 - n_pos_args - i ] = elem -> value ;
276
+ code_state_state [n_state - 1 - n_pos_args - i ] = elem -> value ;
285
277
} else {
286
278
mp_raise_msg_varg (& mp_type_TypeError ,
287
279
MP_ERROR_TEXT ("function missing required keyword argument '%q'" ), arg_qstr );
@@ -305,18 +297,47 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
305
297
// bytecode prelude: initialise closed over variables
306
298
for (; n_cell ; -- n_cell ) {
307
299
size_t local_num = * ip ++ ;
308
- code_state -> state [n_state - 1 - local_num ] =
309
- mp_obj_new_cell (code_state -> state [n_state - 1 - local_num ]);
300
+ code_state_state [n_state - 1 - local_num ] =
301
+ mp_obj_new_cell (code_state_state [n_state - 1 - local_num ]);
310
302
}
311
303
312
304
// now that we skipped over the prelude, set the ip for the VM
313
305
code_state -> ip = ip ;
314
306
315
307
DEBUG_printf ("Calling: n_pos_args=%d, n_kwonly_args=%d\n" , n_pos_args , n_kwonly_args );
316
- dump_args (code_state -> state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
317
- dump_args (code_state -> state , n_state );
308
+ dump_args (code_state_state + n_state - n_pos_args - n_kwonly_args , n_pos_args + n_kwonly_args );
309
+ dump_args (code_state_state , n_state );
318
310
}
319
311
312
+ // On entry code_state should be allocated somewhere (stack/heap) and
313
+ // contain the following valid entries:
314
+ // - code_state->fun_bc should contain a pointer to the function object
315
+ // - code_state->n_state should be the number of objects in the local state
316
+ void mp_setup_code_state (mp_code_state_t * code_state , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
317
+ code_state -> ip = code_state -> fun_bc -> bytecode ;
318
+ code_state -> sp = & code_state -> state [0 ] - 1 ;
319
+ #if MICROPY_STACKLESS
320
+ code_state -> prev = NULL ;
321
+ #endif
322
+ #if MICROPY_PY_SYS_SETTRACE
323
+ code_state -> prev_state = NULL ;
324
+ code_state -> frame = NULL ;
325
+ #endif
326
+ mp_setup_code_state_helper (code_state , n_args , n_kw , args );
327
+ }
328
+
329
+ #if MICROPY_EMIT_NATIVE
330
+ // On entry code_state should be allocated somewhere (stack/heap) and
331
+ // contain the following valid entries:
332
+ // - code_state->fun_bc should contain a pointer to the function object
333
+ // - code_state->ip should contain a pointer to the beginning of the prelude
334
+ // - code_state->n_state should be the number of objects in the local state
335
+ void mp_setup_code_state_native (mp_code_state_native_t * code_state , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
336
+ code_state -> sp = & code_state -> state [0 ] - 1 ;
337
+ mp_setup_code_state_helper ((mp_code_state_t * )code_state , n_args , n_kw , args );
338
+ }
339
+ #endif
340
+
320
341
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
321
342
322
343
// The following table encodes the number of bytes that a specific opcode
0 commit comments