Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython project, http://micropython.org/
3 : : *
4 : : * The MIT License (MIT)
5 : : *
6 : : * Copyright (c) 2013-2020 Damien P. George
7 : : *
8 : : * Permission is hereby granted, free of charge, to any person obtaining a copy
9 : : * of this software and associated documentation files (the "Software"), to deal
10 : : * in the Software without restriction, including without limitation the rights
11 : : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 : : * copies of the Software, and to permit persons to whom the Software is
13 : : * furnished to do so, subject to the following conditions:
14 : : *
15 : : * The above copyright notice and this permission notice shall be included in
16 : : * all copies or substantial portions of the Software.
17 : : *
18 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 : : * THE SOFTWARE.
25 : : */
26 : :
27 : : #include <stdbool.h>
28 : : #include <stdint.h>
29 : : #include <stdio.h>
30 : : #include <string.h>
31 : : #include <assert.h>
32 : :
33 : : #include "py/scope.h"
34 : : #include "py/emit.h"
35 : : #include "py/compile.h"
36 : : #include "py/runtime.h"
37 : : #include "py/asmbase.h"
38 : : #include "py/nativeglue.h"
39 : : #include "py/persistentcode.h"
40 : : #include "py/smallint.h"
41 : :
42 : : #if MICROPY_ENABLE_COMPILER
43 : :
44 : : // TODO need to mangle __attr names
45 : :
46 : : #define INVALID_LABEL (0xffff)
47 : :
48 : : typedef enum {
49 : : // define rules with a compile function
50 : : #define DEF_RULE(rule, comp, kind, ...) PN_##rule,
51 : : #define DEF_RULE_NC(rule, kind, ...)
52 : : #include "py/grammar.h"
53 : : #undef DEF_RULE
54 : : #undef DEF_RULE_NC
55 : : PN_const_object, // special node for a constant, generic Python object
56 : : // define rules without a compile function
57 : : #define DEF_RULE(rule, comp, kind, ...)
58 : : #define DEF_RULE_NC(rule, kind, ...) PN_##rule,
59 : : #include "py/grammar.h"
60 : : #undef DEF_RULE
61 : : #undef DEF_RULE_NC
62 : : } pn_kind_t;
63 : :
64 : : // Whether a mp_parse_node_struct_t that has pns->kind == PN_testlist_comp
65 : : // corresponds to a list comprehension or generator.
66 : : #define MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns) \
67 : : (MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2 && \
68 : : MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for))
69 : :
70 : : #define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
71 : :
72 : : #if NEED_METHOD_TABLE
73 : :
74 : : // we need a method table to do the lookup for the emitter functions
75 : : #define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
76 : : #define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
77 : : #define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.local(comp->emit, qst, local_num, MP_EMIT_IDOP_LOCAL_FAST))
78 : : #define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL))
79 : :
80 : : #else
81 : :
82 : : // if we only have the bytecode emitter enabled then we can do a direct call to the functions
83 : : #define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
84 : : #define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
85 : : #define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_local(comp->emit, qst, local_num, MP_EMIT_IDOP_LOCAL_FAST))
86 : : #define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL))
87 : :
88 : : #endif
89 : :
90 : : #if MICROPY_EMIT_NATIVE && MICROPY_DYNAMIC_COMPILER
91 : :
92 : : #define NATIVE_EMITTER(f) emit_native_table[mp_dynamic_compiler.native_arch]->emit_##f
93 : : #define NATIVE_EMITTER_TABLE (emit_native_table[mp_dynamic_compiler.native_arch])
94 : :
95 : : STATIC const emit_method_table_t *emit_native_table[] = {
96 : : NULL,
97 : : &emit_native_x86_method_table,
98 : : &emit_native_x64_method_table,
99 : : &emit_native_arm_method_table,
100 : : &emit_native_thumb_method_table,
101 : : &emit_native_thumb_method_table,
102 : : &emit_native_thumb_method_table,
103 : : &emit_native_thumb_method_table,
104 : : &emit_native_thumb_method_table,
105 : : &emit_native_xtensa_method_table,
106 : : &emit_native_xtensawin_method_table,
107 : : };
108 : :
109 : : #elif MICROPY_EMIT_NATIVE
110 : : // define a macro to access external native emitter
111 : : #if MICROPY_EMIT_X64
112 : : #define NATIVE_EMITTER(f) emit_native_x64_##f
113 : : #elif MICROPY_EMIT_X86
114 : : #define NATIVE_EMITTER(f) emit_native_x86_##f
115 : : #elif MICROPY_EMIT_THUMB
116 : : #define NATIVE_EMITTER(f) emit_native_thumb_##f
117 : : #elif MICROPY_EMIT_ARM
118 : : #define NATIVE_EMITTER(f) emit_native_arm_##f
119 : : #elif MICROPY_EMIT_XTENSA
120 : : #define NATIVE_EMITTER(f) emit_native_xtensa_##f
121 : : #elif MICROPY_EMIT_XTENSAWIN
122 : : #define NATIVE_EMITTER(f) emit_native_xtensawin_##f
123 : : #else
124 : : #error "unknown native emitter"
125 : : #endif
126 : : #define NATIVE_EMITTER_TABLE (&NATIVE_EMITTER(method_table))
127 : : #endif
128 : :
129 : : #if MICROPY_EMIT_INLINE_ASM && MICROPY_DYNAMIC_COMPILER
130 : :
131 : : #define ASM_EMITTER(f) emit_asm_table[mp_dynamic_compiler.native_arch]->asm_##f
132 : : #define ASM_EMITTER_TABLE emit_asm_table[mp_dynamic_compiler.native_arch]
133 : :
134 : : STATIC const emit_inline_asm_method_table_t *emit_asm_table[] = {
135 : : NULL,
136 : : NULL,
137 : : NULL,
138 : : &emit_inline_thumb_method_table,
139 : : &emit_inline_thumb_method_table,
140 : : &emit_inline_thumb_method_table,
141 : : &emit_inline_thumb_method_table,
142 : : &emit_inline_thumb_method_table,
143 : : &emit_inline_thumb_method_table,
144 : : &emit_inline_xtensa_method_table,
145 : : NULL,
146 : : };
147 : :
148 : : #elif MICROPY_EMIT_INLINE_ASM
149 : : // define macros for inline assembler
150 : : #if MICROPY_EMIT_INLINE_THUMB
151 : : #define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb
152 : : #define ASM_EMITTER(f) emit_inline_thumb_##f
153 : : #elif MICROPY_EMIT_INLINE_XTENSA
154 : : #define ASM_DECORATOR_QSTR MP_QSTR_asm_xtensa
155 : : #define ASM_EMITTER(f) emit_inline_xtensa_##f
156 : : #else
157 : : #error "unknown asm emitter"
158 : : #endif
159 : : #define ASM_EMITTER_TABLE &ASM_EMITTER(method_table)
160 : : #endif
161 : :
162 : : #define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
163 : : #define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
164 : :
165 : : // elements in this struct are ordered to make it compact
166 : : typedef struct _compiler_t {
167 : : uint8_t is_repl;
168 : : uint8_t pass; // holds enum type pass_kind_t
169 : : uint8_t have_star;
170 : :
171 : : // try to keep compiler clean from nlr
172 : : mp_obj_t compile_error; // set to an exception object if there's an error
173 : : size_t compile_error_line; // set to best guess of line of error
174 : :
175 : : uint next_label;
176 : :
177 : : uint16_t num_dict_params;
178 : : uint16_t num_default_params;
179 : :
180 : : uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
181 : : uint16_t continue_label;
182 : : uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
183 : : uint16_t break_continue_except_level;
184 : :
185 : : scope_t *scope_head;
186 : : scope_t *scope_cur;
187 : :
188 : : emit_t *emit; // current emitter
189 : : #if NEED_METHOD_TABLE
190 : : const emit_method_table_t *emit_method_table; // current emit method table
191 : : #endif
192 : :
193 : : #if MICROPY_EMIT_INLINE_ASM
194 : : emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
195 : : const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
196 : : #endif
197 : :
198 : : mp_emit_common_t emit_common;
199 : : } compiler_t;
200 : :
201 : : /******************************************************************************/
202 : : // mp_emit_common_t helper functions
203 : : // These are defined here so they can be inlined, to reduce code size.
204 : :
205 : 3661 : STATIC void mp_emit_common_init(mp_emit_common_t *emit, qstr source_file) {
206 : : #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
207 : 3661 : mp_map_init(&emit->qstr_map, 1);
208 : :
209 : : // add the source file as the first entry in the qstr table
210 : 3661 : mp_map_elem_t *elem = mp_map_lookup(&emit->qstr_map, MP_OBJ_NEW_QSTR(source_file), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
211 : 3661 : elem->value = MP_OBJ_NEW_SMALL_INT(0);
212 : : #endif
213 : 3661 : mp_obj_list_init(&emit->const_obj_list, 0);
214 : 3661 : }
215 : :
216 : 36360 : STATIC void mp_emit_common_start_pass(mp_emit_common_t *emit, pass_kind_t pass) {
217 : 36360 : emit->pass = pass;
218 [ + + ]: 36360 : if (pass == MP_PASS_CODE_SIZE) {
219 [ + + ]: 8930 : if (emit->ct_cur_child == 0) {
220 : 6951 : emit->children = NULL;
221 : : } else {
222 : 1979 : emit->children = m_new0(mp_raw_code_t *, emit->ct_cur_child);
223 : : }
224 : : }
225 : 36360 : emit->ct_cur_child = 0;
226 : 36360 : }
227 : :
228 : 3318 : STATIC void mp_emit_common_populate_module_context(mp_emit_common_t *emit, qstr source_file, mp_module_context_t *context) {
229 : : #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
230 : 3318 : size_t qstr_map_used = emit->qstr_map.used;
231 : 3318 : mp_module_context_alloc_tables(context, qstr_map_used, emit->const_obj_list.len);
232 [ + + ]: 43053 : for (size_t i = 0; i < emit->qstr_map.alloc; ++i) {
233 [ + + ]: 39735 : if (mp_map_slot_is_filled(&emit->qstr_map, i)) {
234 : 35797 : size_t idx = MP_OBJ_SMALL_INT_VALUE(emit->qstr_map.table[i].value);
235 : 35797 : qstr qst = MP_OBJ_QSTR_VALUE(emit->qstr_map.table[i].key);
236 : 35797 : context->constants.qstr_table[idx] = qst;
237 : : }
238 : : }
239 : : #else
240 : : mp_module_context_alloc_tables(context, 0, emit->const_obj_list.len);
241 : : context->constants.source_file = source_file;
242 : : #endif
243 : :
244 [ + + ]: 9397 : for (size_t i = 0; i < emit->const_obj_list.len; ++i) {
245 : 6079 : context->constants.obj_table[i] = emit->const_obj_list.items[i];
246 : : }
247 : 3318 : }
248 : :
249 : : /******************************************************************************/
250 : :
251 : 587 : STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
252 : : // if the line of the error is unknown then try to update it from the pn
253 [ + + + + : 587 : if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
+ + ]
254 : 321 : comp->compile_error_line = ((mp_parse_node_struct_t *)pn)->source_line;
255 : : }
256 : 587 : }
257 : :
258 : 249 : STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, mp_rom_error_text_t msg) {
259 : : // only register the error if there has been no other error
260 [ + + ]: 249 : if (comp->compile_error == MP_OBJ_NULL) {
261 : 237 : comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
262 : 237 : compile_error_set_line(comp, pn);
263 : : }
264 : 249 : }
265 : :
266 : : STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
267 : : STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
268 : : STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool create_map);
269 : : STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
270 : :
271 : 279985 : STATIC uint comp_next_label(compiler_t *comp) {
272 : 279985 : return comp->next_label++;
273 : : }
274 : :
275 : : #if MICROPY_EMIT_NATIVE
276 : 164833 : STATIC void reserve_labels_for_native(compiler_t *comp, int n) {
277 [ + + ]: 164833 : if (comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
278 : 26902 : comp->next_label += n;
279 : : }
280 : 164833 : }
281 : : #else
282 : : #define reserve_labels_for_native(comp, n)
283 : : #endif
284 : :
285 : 88419 : STATIC void compile_increase_except_level(compiler_t *comp, uint label, int kind) {
286 : 88419 : EMIT_ARG(setup_block, label, kind);
287 : 88419 : comp->cur_except_level += 1;
288 [ + + ]: 88419 : if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
289 : 2088 : comp->scope_cur->exc_stack_size = comp->cur_except_level;
290 : : }
291 : 88419 : }
292 : :
293 : 88419 : STATIC void compile_decrease_except_level(compiler_t *comp) {
294 [ - + ]: 88419 : assert(comp->cur_except_level > 0);
295 : 88419 : comp->cur_except_level -= 1;
296 : 88419 : EMIT(end_finally);
297 : 88419 : reserve_labels_for_native(comp, 1);
298 : 88419 : }
299 : :
300 : 9362 : STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
301 : 9362 : scope_t *scope = scope_new(kind, pn, emit_options);
302 : 9362 : scope->parent = comp->scope_cur;
303 : 9362 : scope->next = NULL;
304 [ + + ]: 9362 : if (comp->scope_head == NULL) {
305 : 3661 : comp->scope_head = scope;
306 : : } else {
307 : : scope_t *s = comp->scope_head;
308 [ + + ]: 46891 : while (s->next != NULL) {
309 : : s = s->next;
310 : : }
311 : 5701 : s->next = scope;
312 : : }
313 : 9362 : return scope;
314 : : }
315 : :
316 : : typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
317 : :
318 : 27062 : STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, apply_list_fun_t f) {
319 [ + + + + : 27062 : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
+ + ]
320 : 8843 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
321 : 8843 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
322 [ + + ]: 29762 : for (int i = 0; i < num_nodes; i++) {
323 : 20919 : f(comp, pns->nodes[i]);
324 : : }
325 : : } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
326 : 12286 : f(comp, pn);
327 : : }
328 : 27058 : }
329 : :
330 : 75177 : STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
331 : 75177 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
332 [ + + ]: 293454 : for (int i = 0; i < num_nodes; i++) {
333 : 218306 : compile_node(comp, pns->nodes[i]);
334 [ + + ]: 218306 : if (comp->compile_error != MP_OBJ_NULL) {
335 : : // add line info for the error in case it didn't have a line number
336 : 29 : compile_error_set_line(comp, pns->nodes[i]);
337 : 29 : return;
338 : : }
339 : : }
340 : : }
341 : :
342 : 388480 : STATIC void compile_load_id(compiler_t *comp, qstr qst) {
343 [ + + ]: 388480 : if (comp->pass == MP_PASS_SCOPE) {
344 : 74408 : mp_emit_common_get_id_for_load(comp->scope_cur, qst);
345 : : } else {
346 : : #if NEED_METHOD_TABLE
347 : 314072 : mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
348 : : #else
349 : : mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
350 : : #endif
351 : : }
352 : 388479 : }
353 : :
354 : 112024 : STATIC void compile_store_id(compiler_t *comp, qstr qst) {
355 [ + + ]: 112024 : if (comp->pass == MP_PASS_SCOPE) {
356 : 20543 : mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
357 : : } else {
358 : : #if NEED_METHOD_TABLE
359 : 91481 : mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
360 : : #else
361 : : mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
362 : : #endif
363 : : }
364 : 112024 : }
365 : :
366 : 1705 : STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
367 [ + + ]: 1705 : if (comp->pass == MP_PASS_SCOPE) {
368 : 415 : mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
369 : : } else {
370 : : #if NEED_METHOD_TABLE
371 : 1290 : mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
372 : : #else
373 : : mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
374 : : #endif
375 : : }
376 : 1705 : }
377 : :
378 : 3752 : STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
379 : : // a simple tuple expression
380 : 3752 : size_t num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
381 [ + + ]: 14307 : for (size_t i = 0; i < num_nodes; i++) {
382 : 10555 : compile_node(comp, pns->nodes[i]);
383 : : }
384 : 3752 : EMIT_ARG(build, num_nodes, MP_EMIT_BUILD_TUPLE);
385 : 3752 : }
386 : :
387 : 17337 : STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
388 [ + + ]: 17337 : if (mp_parse_node_is_const_false(pn)) {
389 [ + + ]: 48 : if (jump_if == false) {
390 : 8 : EMIT_ARG(jump, label);
391 : : }
392 : 48 : return;
393 [ + + ]: 17289 : } else if (mp_parse_node_is_const_true(pn)) {
394 [ + + ]: 950 : if (jump_if == true) {
395 : 750 : EMIT_ARG(jump, label);
396 : : }
397 : 950 : return;
398 [ + - + + ]: 16339 : } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
399 : 14467 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
400 : 14467 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
401 [ + + ]: 14467 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
402 [ + + ]: 340 : if (jump_if == false) {
403 : 324 : and_or_logic1:;
404 : 360 : uint label2 = comp_next_label(comp);
405 [ + + ]: 728 : for (int i = 0; i < n - 1; i++) {
406 : 368 : c_if_cond(comp, pns->nodes[i], !jump_if, label2);
407 : : }
408 : 360 : c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
409 : 360 : EMIT_ARG(label_assign, label2);
410 : : } else {
411 : 16 : and_or_logic2:
412 [ + + ]: 3186 : for (int i = 0; i < n; i++) {
413 : 2124 : c_if_cond(comp, pns->nodes[i], jump_if, label);
414 : : }
415 : : }
416 : 1422 : return;
417 [ + + ]: 14127 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
418 [ + + ]: 1082 : if (jump_if == false) {
419 : 1046 : goto and_or_logic2;
420 : : } else {
421 : 36 : goto and_or_logic1;
422 : : }
423 [ + + ]: 13045 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
424 : 1697 : c_if_cond(comp, pns->nodes[0], !jump_if, label);
425 : 1697 : return;
426 : : }
427 : : }
428 : :
429 : : // nothing special, fall back to default compiling for node and jump
430 : 13220 : compile_node(comp, pn);
431 : 13220 : EMIT_ARG(pop_jump_if, jump_if, label);
432 : : }
433 : :
434 : : typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
435 : : STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
436 : :
437 : 9321 : STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
438 [ + + ]: 9321 : if (assign_kind != ASSIGN_AUG_STORE) {
439 : 9153 : compile_node(comp, pns->nodes[0]);
440 : : }
441 : :
442 [ + - + - ]: 9321 : if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
443 : 9321 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
444 [ + + ]: 9321 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
445 : 1247 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
446 [ + - ]: 1247 : if (assign_kind != ASSIGN_AUG_STORE) {
447 [ + + ]: 2526 : for (int i = 0; i < n - 1; i++) {
448 : 1279 : compile_node(comp, pns1->nodes[i]);
449 : : }
450 : : }
451 [ + - - + ]: 1247 : assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
452 : 1247 : pns1 = (mp_parse_node_struct_t *)pns1->nodes[n - 1];
453 : : }
454 [ + + ]: 9321 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
455 [ + + ]: 3682 : if (assign_kind == ASSIGN_AUG_STORE) {
456 : 44 : EMIT(rot_three);
457 : 44 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
458 : : } else {
459 : 3638 : compile_node(comp, pns1->nodes[0]);
460 [ + + ]: 3638 : if (assign_kind == ASSIGN_AUG_LOAD) {
461 : 44 : EMIT(dup_top_two);
462 : 44 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
463 : : } else {
464 : 3594 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
465 : : }
466 : : }
467 : 3682 : return;
468 [ + + ]: 5639 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
469 [ - + ]: 5635 : assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
470 [ + + ]: 5635 : if (assign_kind == ASSIGN_AUG_LOAD) {
471 : 124 : EMIT(dup_top);
472 : 124 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]), MP_EMIT_ATTR_LOAD);
473 : : } else {
474 [ + + ]: 5511 : if (assign_kind == ASSIGN_AUG_STORE) {
475 : 124 : EMIT(rot_two);
476 : : }
477 : 5511 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]), MP_EMIT_ATTR_STORE);
478 : : }
479 : 5635 : return;
480 : : }
481 : : }
482 : :
483 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("can't assign to expression"));
484 : : }
485 : :
486 : 1309 : STATIC void c_assign_tuple(compiler_t *comp, uint num_tail, mp_parse_node_t *nodes_tail) {
487 : : // look for star expression
488 : 1309 : uint have_star_index = -1;
489 [ + + ]: 4357 : for (uint i = 0; i < num_tail; i++) {
490 [ + - + + : 3052 : if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
+ + ]
491 [ + + ]: 280 : if (have_star_index == (uint)-1) {
492 : 276 : EMIT_ARG(unpack_ex, i, num_tail - i - 1);
493 : 276 : have_star_index = i;
494 : : } else {
495 : 4 : compile_syntax_error(comp, nodes_tail[i], MP_ERROR_TEXT("multiple *x in assignment"));
496 : 4 : return;
497 : : }
498 : : }
499 : : }
500 [ + + ]: 1305 : if (have_star_index == (uint)-1) {
501 : 1033 : EMIT_ARG(unpack_sequence, num_tail);
502 : : }
503 [ + + ]: 4349 : for (uint i = 0; i < num_tail; i++) {
504 [ + + ]: 3044 : if (i == have_star_index) {
505 : 272 : c_assign(comp, ((mp_parse_node_struct_t *)nodes_tail[i])->nodes[0], ASSIGN_STORE);
506 : : } else {
507 : 2772 : c_assign(comp, nodes_tail[i], ASSIGN_STORE);
508 : : }
509 : : }
510 : : }
511 : :
512 : : // assigns top of stack to pn
513 : 85449 : STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
514 [ - + ]: 85449 : assert(!MP_PARSE_NODE_IS_NULL(pn));
515 [ + + ]: 85449 : if (MP_PARSE_NODE_IS_LEAF(pn)) {
516 [ + + ]: 74775 : if (MP_PARSE_NODE_IS_ID(pn)) {
517 : 74763 : qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
518 [ + + ]: 74763 : switch (assign_kind) {
519 : 73309 : case ASSIGN_STORE:
520 : : case ASSIGN_AUG_STORE:
521 : 73309 : compile_store_id(comp, arg);
522 : 73309 : break;
523 : 1454 : case ASSIGN_AUG_LOAD:
524 : : default:
525 : 1454 : compile_load_id(comp, arg);
526 : 1454 : break;
527 : : }
528 : : } else {
529 : 12 : goto cannot_assign;
530 : : }
531 : : } else {
532 : : // pn must be a struct
533 : 10674 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
534 [ + + + + : 10674 : switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
+ ]
535 : 9321 : case PN_atom_expr_normal:
536 : : // lhs is an index or attribute
537 : 9321 : c_assign_atom_expr(comp, pns, assign_kind);
538 : 9321 : break;
539 : :
540 : 1213 : case PN_testlist_star_expr:
541 : : case PN_exprlist:
542 : : // lhs is a tuple
543 [ + + ]: 1213 : if (assign_kind != ASSIGN_STORE) {
544 : 8 : goto cannot_assign;
545 : : }
546 : 1205 : c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
547 : 1205 : break;
548 : :
549 : 72 : case PN_atom_paren:
550 : : // lhs is something in parenthesis
551 [ + + ]: 72 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
552 : : // empty tuple
553 : 4 : goto cannot_assign;
554 : : } else {
555 [ + - - + ]: 68 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
556 [ + + ]: 68 : if (assign_kind != ASSIGN_STORE) {
557 : 8 : goto cannot_assign;
558 : : }
559 : 60 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
560 : 60 : goto testlist_comp;
561 : : }
562 : 56 : break;
563 : :
564 : 56 : case PN_atom_bracket:
565 : : // lhs is something in brackets
566 [ + + ]: 56 : if (assign_kind != ASSIGN_STORE) {
567 : 8 : goto cannot_assign;
568 : : }
569 [ + + ]: 48 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
570 : : // empty list, assignment allowed
571 : 8 : c_assign_tuple(comp, 0, NULL);
572 [ + + + + ]: 40 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
573 : 24 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
574 : 24 : goto testlist_comp;
575 : : } else {
576 : : // brackets around 1 item
577 : 16 : c_assign_tuple(comp, 1, pns->nodes);
578 : : }
579 : : break;
580 : :
581 : 12 : default:
582 : 12 : goto cannot_assign;
583 : : }
584 : 10550 : return;
585 : :
586 : 84 : testlist_comp:
587 : : // lhs is a sequence
588 [ + + + - : 84 : if (MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns)) {
+ + + + ]
589 : 4 : goto cannot_assign;
590 : : }
591 : 80 : c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
592 : 80 : return;
593 : : }
594 : : return;
595 : :
596 : 56 : cannot_assign:
597 : 56 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("can't assign to expression"));
598 : : }
599 : :
600 : : // stuff for lambda and comprehensions and generators:
601 : : // if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
602 : : // if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
603 : : // if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
604 : 22560 : STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
605 [ - + ]: 22560 : assert(n_pos_defaults >= 0);
606 [ - + ]: 22560 : assert(n_kw_defaults >= 0);
607 : :
608 : : // set flags
609 [ + + ]: 22560 : if (n_kw_defaults > 0) {
610 : 100 : this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
611 : : }
612 : 22560 : this_scope->num_def_pos_args = n_pos_defaults;
613 : :
614 : : #if MICROPY_EMIT_NATIVE
615 : : // When creating a function/closure it will take a reference to the current globals
616 : 22560 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_REFGLOBALS | MP_SCOPE_FLAG_HASCONSTS;
617 : : #endif
618 : :
619 : : // make closed over variables, if any
620 : : // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
621 : 22560 : int nfree = 0;
622 [ + + ]: 22560 : if (comp->scope_cur->kind != SCOPE_MODULE) {
623 [ + + ]: 102067 : for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
624 : 92419 : id_info_t *id = &comp->scope_cur->id_info[i];
625 [ + + ]: 92419 : if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
626 [ + + ]: 4305 : for (int j = 0; j < this_scope->id_info_len; j++) {
627 : 3444 : id_info_t *id2 = &this_scope->id_info[j];
628 [ + + + + ]: 3444 : if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
629 : : // in MicroPython we load closures using LOAD_FAST
630 : 612 : EMIT_LOAD_FAST(id->qst, id->local_num);
631 : 612 : nfree += 1;
632 : : }
633 : : }
634 : : }
635 : : }
636 : : }
637 : :
638 : : // make the function/closure
639 [ + + ]: 9648 : if (nfree == 0) {
640 : 22134 : EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
641 : : } else {
642 : 426 : EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
643 : : }
644 : 22560 : }
645 : :
646 : 22428 : STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
647 : : // For efficiency of the code below we extract the parse-node kind here
648 : 22428 : int pn_kind;
649 [ + + ]: 22428 : if (MP_PARSE_NODE_IS_ID(pn)) {
650 : : pn_kind = -1;
651 : : } else {
652 [ + - - + ]: 3429 : assert(MP_PARSE_NODE_IS_STRUCT(pn));
653 : 3429 : pn_kind = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn);
654 : : }
655 : :
656 [ + + - + : 22428 : if (pn_kind == PN_typedargslist_star || pn_kind == PN_varargslist_star) {
+ + ]
657 : 498 : comp->have_star = true;
658 : : /* don't need to distinguish bare from named star
659 : : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
660 : : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
661 : : // bare star
662 : : } else {
663 : : // named star
664 : : }
665 : : */
666 : :
667 : : } else if (pn_kind == PN_typedargslist_dbl_star || pn_kind == PN_varargslist_dbl_star) {
668 : : // named double star
669 : : // TODO do we need to do anything with this?
670 : :
671 : : } else {
672 : : mp_parse_node_t pn_id;
673 : : mp_parse_node_t pn_equal;
674 : : if (pn_kind == -1) {
675 : : // this parameter is just an id
676 : :
677 : : pn_id = pn;
678 : : pn_equal = MP_PARSE_NODE_NULL;
679 : :
680 : : } else if (pn_kind == PN_typedargslist_name) {
681 : : // this parameter has a colon and/or equal specifier
682 : :
683 : 2746 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
684 : 2746 : pn_id = pns->nodes[0];
685 : : // pn_colon = pns->nodes[1]; // unused
686 : 2746 : pn_equal = pns->nodes[2];
687 : :
688 : : } else {
689 : 0 : assert(pn_kind == PN_varargslist_name); // should be
690 : : // this parameter has an equal specifier
691 : :
692 : 24 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
693 : 24 : pn_id = pns->nodes[0];
694 : 24 : pn_equal = pns->nodes[1];
695 : : }
696 : :
697 [ + + ]: 2770 : if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
698 : : // this parameter does not have a default value
699 : :
700 : : // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
701 [ + + + + ]: 20327 : if (!comp->have_star && comp->num_default_params != 0) {
702 : 4 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("non-default argument follows default argument"));
703 : 4 : return;
704 : : }
705 : :
706 : : } else {
707 : : // this parameter has a default value
708 : : // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
709 : :
710 [ + + ]: 1442 : if (comp->have_star) {
711 : 116 : comp->num_dict_params += 1;
712 : : // in MicroPython we put the default dict parameters into a dictionary using the bytecode
713 [ + + ]: 116 : if (comp->num_dict_params == 1) {
714 : : // in MicroPython we put the default positional parameters into a tuple using the bytecode
715 : : // we need to do this here before we start building the map for the default keywords
716 [ + + ]: 100 : if (comp->num_default_params > 0) {
717 : 8 : EMIT_ARG(build, comp->num_default_params, MP_EMIT_BUILD_TUPLE);
718 : : } else {
719 : 92 : EMIT(load_null); // sentinel indicating empty default positional args
720 : : }
721 : : // first default dict param, so make the map
722 : 100 : EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
723 : : }
724 : :
725 : : // compile value then key, then store it to the dict
726 : 116 : compile_node(comp, pn_equal);
727 : 116 : EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
728 : 116 : EMIT(store_map);
729 : : } else {
730 : 1326 : comp->num_default_params += 1;
731 : 1326 : compile_node(comp, pn_equal);
732 : : }
733 : : }
734 : : }
735 : : }
736 : :
737 : 18012 : STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) {
738 : : // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
739 : : // expression for default arguments, which may contain a lambda. The lambda will
740 : : // call here in a nested way, so we must save and restore the relevant state.
741 : 18012 : bool orig_have_star = comp->have_star;
742 : 18012 : uint16_t orig_num_dict_params = comp->num_dict_params;
743 : 18012 : uint16_t orig_num_default_params = comp->num_default_params;
744 : :
745 : : // compile default parameters
746 : 18012 : comp->have_star = false;
747 : 18012 : comp->num_dict_params = 0;
748 : 18012 : comp->num_default_params = 0;
749 : 18012 : apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
750 : :
751 [ + + ]: 18012 : if (comp->compile_error != MP_OBJ_NULL) {
752 : : return;
753 : : }
754 : :
755 : : // in MicroPython we put the default positional parameters into a tuple using the bytecode
756 : : // the default keywords args may have already made the tuple; if not, do it now
757 [ + + + + ]: 17995 : if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
758 : 1026 : EMIT_ARG(build, comp->num_default_params, MP_EMIT_BUILD_TUPLE);
759 : 1026 : EMIT(load_null); // sentinel indicating empty default keyword args
760 : : }
761 : :
762 : : // make the function
763 : 17995 : close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
764 : :
765 : : // restore state
766 : 17995 : comp->have_star = orig_have_star;
767 : 17995 : comp->num_dict_params = orig_num_dict_params;
768 : 17995 : comp->num_default_params = orig_num_default_params;
769 : : }
770 : :
771 : : // leaves function object on stack
772 : : // returns function name
773 : 17447 : STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
774 [ + + ]: 17447 : if (comp->pass == MP_PASS_SCOPE) {
775 : : // create a new scope for this function
776 : 4403 : scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
777 : : // store the function scope so the compiling function can use it at each pass
778 : 4403 : pns->nodes[4] = (mp_parse_node_t)s;
779 : : }
780 : :
781 : : // get the scope for this function
782 : 17447 : scope_t *fscope = (scope_t *)pns->nodes[4];
783 : :
784 : : // compile the function definition
785 : 17447 : compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
786 : :
787 : : // return its name (the 'f' in "def f(...):")
788 : 17447 : return fscope->simple_name;
789 : : }
790 : :
791 : : // leaves class object on stack
792 : : // returns class name
793 : 3765 : STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
794 [ + + ]: 3765 : if (comp->pass == MP_PASS_SCOPE) {
795 : : // create a new scope for this class
796 : 940 : scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
797 : : // store the class scope so the compiling function can use it at each pass
798 : 940 : pns->nodes[3] = (mp_parse_node_t)s;
799 : : }
800 : :
801 : 3765 : EMIT(load_build_class);
802 : :
803 : : // scope for this class
804 : 3765 : scope_t *cscope = (scope_t *)pns->nodes[3];
805 : :
806 : : // compile the class
807 : 3765 : close_over_variables_etc(comp, cscope, 0, 0);
808 : :
809 : : // get its name
810 : 3765 : EMIT_ARG(load_const_str, cscope->simple_name);
811 : :
812 : : // nodes[1] has parent classes, if any
813 : : // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
814 : 3765 : mp_parse_node_t parents = pns->nodes[1];
815 [ + + + + : 3765 : if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
+ + ]
816 : 48 : parents = MP_PARSE_NODE_NULL;
817 : : }
818 : 3765 : compile_trailer_paren_helper(comp, parents, false, 2);
819 : :
820 : : // return its name (the 'C' in class C(...):")
821 : 3765 : return cscope->simple_name;
822 : : }
823 : :
824 : : // returns true if it was a built-in decorator (even if the built-in had an error)
825 : 1701 : STATIC bool compile_built_in_decorator(compiler_t *comp, size_t name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
826 [ + + ]: 1701 : if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
827 : : return false;
828 : : }
829 : :
830 [ + + ]: 1493 : if (name_len != 2) {
831 : 4 : compile_syntax_error(comp, name_nodes[0], MP_ERROR_TEXT("invalid micropython decorator"));
832 : 4 : return true;
833 : : }
834 : :
835 : 1489 : qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
836 [ + + ]: 1489 : if (attr == MP_QSTR_bytecode) {
837 : 16 : *emit_options = MP_EMIT_OPT_BYTECODE;
838 : : #if MICROPY_EMIT_NATIVE
839 [ + + ]: 1473 : } else if (attr == MP_QSTR_native) {
840 : 252 : *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
841 [ + + ]: 1221 : } else if (attr == MP_QSTR_viper) {
842 : 1212 : *emit_options = MP_EMIT_OPT_VIPER;
843 : : #endif
844 : : #if MICROPY_EMIT_INLINE_ASM
845 : : #if MICROPY_DYNAMIC_COMPILER
846 : : } else if (attr == MP_QSTR_asm_thumb) {
847 : : *emit_options = MP_EMIT_OPT_ASM;
848 : : } else if (attr == MP_QSTR_asm_xtensa) {
849 : : *emit_options = MP_EMIT_OPT_ASM;
850 : : #else
851 : : } else if (attr == ASM_DECORATOR_QSTR) {
852 : : *emit_options = MP_EMIT_OPT_ASM;
853 : : #endif
854 : : #endif
855 : : } else {
856 : 9 : compile_syntax_error(comp, name_nodes[1], MP_ERROR_TEXT("invalid micropython decorator"));
857 : : }
858 : :
859 : : #if MICROPY_EMIT_NATIVE && MICROPY_DYNAMIC_COMPILER
860 : : if (*emit_options == MP_EMIT_OPT_NATIVE_PYTHON || *emit_options == MP_EMIT_OPT_VIPER) {
861 : : if (emit_native_table[mp_dynamic_compiler.native_arch] == NULL) {
862 : : compile_syntax_error(comp, name_nodes[1], MP_ERROR_TEXT("invalid arch"));
863 : : }
864 : : } else if (*emit_options == MP_EMIT_OPT_ASM) {
865 : : if (emit_asm_table[mp_dynamic_compiler.native_arch] == NULL) {
866 : : compile_syntax_error(comp, name_nodes[1], MP_ERROR_TEXT("invalid arch"));
867 : : }
868 : : }
869 : : #endif
870 : :
871 : : return true;
872 : : }
873 : :
874 : 1701 : STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
875 : : // get the list of decorators
876 : 1701 : mp_parse_node_t *nodes;
877 : 1701 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
878 : :
879 : : // inherit emit options for this function/class definition
880 : 1701 : uint emit_options = comp->scope_cur->emit_options;
881 : :
882 : : // compile each decorator
883 : 1701 : size_t num_built_in_decorators = 0;
884 [ + + ]: 3402 : for (size_t i = 0; i < n; i++) {
885 [ + - + - : 1701 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
- + ]
886 : 1701 : mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t *)nodes[i];
887 : :
888 : : // nodes[0] contains the decorator function, which is a dotted name
889 : 1701 : mp_parse_node_t *name_nodes;
890 : 1701 : size_t name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
891 : :
892 : : // check for built-in decorators
893 [ + + ]: 1701 : if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
894 : : // this was a built-in
895 : 1493 : num_built_in_decorators += 1;
896 : :
897 : : } else {
898 : : // not a built-in, compile normally
899 : :
900 : : // compile the decorator function
901 : 208 : compile_node(comp, name_nodes[0]);
902 [ + + ]: 224 : for (size_t j = 1; j < name_len; j++) {
903 [ - + ]: 16 : assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
904 : 16 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]), MP_EMIT_ATTR_LOAD);
905 : : }
906 : :
907 : : // nodes[1] contains arguments to the decorator function, if any
908 [ + + ]: 208 : if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
909 : : // call the decorator function with the arguments in nodes[1]
910 : 8 : compile_node(comp, pns_decorator->nodes[1]);
911 : : }
912 : : }
913 : : }
914 : :
915 : : // compile the body (funcdef, async funcdef or classdef) and get its name
916 : 1701 : mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t *)pns->nodes[1];
917 : 1701 : qstr body_name = 0;
918 [ + + ]: 1701 : if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
919 : 1685 : body_name = compile_funcdef_helper(comp, pns_body, emit_options);
920 : : #if MICROPY_PY_ASYNC_AWAIT
921 [ + + ]: 16 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
922 [ + - - + ]: 8 : assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
923 : 8 : mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns_body->nodes[0];
924 : 8 : body_name = compile_funcdef_helper(comp, pns0, emit_options);
925 : 8 : scope_t *fscope = (scope_t *)pns0->nodes[4];
926 : 8 : fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
927 : : #endif
928 : : } else {
929 [ - + ]: 8 : assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
930 : 8 : body_name = compile_classdef_helper(comp, pns_body, emit_options);
931 : : }
932 : :
933 : : // call each decorator
934 [ + + ]: 1909 : for (size_t i = 0; i < n - num_built_in_decorators; i++) {
935 : 208 : EMIT_ARG(call_function, 1, 0, 0);
936 : : }
937 : :
938 : : // store func/class object into name
939 : 1701 : compile_store_id(comp, body_name);
940 : 1701 : }
941 : :
942 : 15754 : STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
943 : 15754 : qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
944 : : // store function object into function name
945 : 15754 : compile_store_id(comp, fname);
946 : 15754 : }
947 : :
948 : 794 : STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
949 [ + + ]: 794 : if (MP_PARSE_NODE_IS_ID(pn)) {
950 : 164 : compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
951 [ + - + - : 630 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
+ + ]
952 : 578 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
953 : :
954 : 578 : compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
955 : :
956 [ + - + - ]: 578 : if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
957 : 578 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
958 [ + + ]: 578 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
959 : 272 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
960 [ + + ]: 544 : for (int i = 0; i < n - 1; i++) {
961 : 272 : compile_node(comp, pns1->nodes[i]);
962 : : }
963 [ + - - + ]: 272 : assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
964 : 272 : pns1 = (mp_parse_node_struct_t *)pns1->nodes[n - 1];
965 : : }
966 [ + + ]: 578 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
967 : 450 : compile_node(comp, pns1->nodes[0]);
968 : 450 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_DELETE);
969 [ + + ]: 128 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
970 [ - + ]: 124 : assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
971 : 124 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]), MP_EMIT_ATTR_DELETE);
972 : : } else {
973 : 4 : goto cannot_delete;
974 : : }
975 : : } else {
976 : 0 : goto cannot_delete;
977 : : }
978 : :
979 [ + - + + ]: 52 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
980 : 48 : pn = ((mp_parse_node_struct_t *)pn)->nodes[0];
981 [ + + ]: 48 : if (MP_PARSE_NODE_IS_NULL(pn)) {
982 : 4 : goto cannot_delete;
983 : : } else {
984 [ + - - + ]: 44 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
985 : 44 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
986 [ + + + - : 44 : if (MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns)) {
+ + + + ]
987 : 4 : goto cannot_delete;
988 : : }
989 [ + + ]: 120 : for (size_t i = 0; i < MP_PARSE_NODE_STRUCT_NUM_NODES(pns); ++i) {
990 : 80 : c_del_stmt(comp, pns->nodes[i]);
991 : : }
992 : : }
993 : : } else {
994 : : // some arbitrary statement that we can't delete (eg del 1)
995 : 4 : goto cannot_delete;
996 : : }
997 : :
998 : : return;
999 : :
1000 : 16 : cannot_delete:
1001 : 16 : compile_syntax_error(comp, (mp_parse_node_t)pn, MP_ERROR_TEXT("can't delete expression"));
1002 : : }
1003 : :
1004 : 714 : STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1005 : 714 : apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1006 : 714 : }
1007 : :
1008 : 966 : STATIC void compile_break_cont_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1009 : 966 : uint16_t label;
1010 [ + + ]: 966 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_break_stmt) {
1011 : 812 : label = comp->break_label;
1012 : : } else {
1013 : 154 : label = comp->continue_label;
1014 : : }
1015 [ + + ]: 966 : if (label == INVALID_LABEL) {
1016 : 8 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'break'/'continue' outside loop"));
1017 : : }
1018 [ - + ]: 966 : assert(comp->cur_except_level >= comp->break_continue_except_level);
1019 : 966 : EMIT_ARG(unwind_jump, label, comp->cur_except_level - comp->break_continue_except_level);
1020 : 966 : }
1021 : :
1022 : 8012 : STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1023 : : #if MICROPY_CPYTHON_COMPAT
1024 [ + + ]: 8012 : if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1025 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'return' outside function"));
1026 : 4 : return;
1027 : : }
1028 : : #endif
1029 [ + + ]: 8008 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1030 : : // no argument to 'return', so return None
1031 : 446 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1032 : 7562 : } else if (MICROPY_COMP_RETURN_IF_EXPR
1033 [ + + + + ]: 7778 : && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
1034 : : // special case when returning an if-expression; to match CPython optimisation
1035 : 216 : mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t *)pns->nodes[0];
1036 : 216 : mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t *)pns_test_if_expr->nodes[1];
1037 : :
1038 : 216 : uint l_fail = comp_next_label(comp);
1039 : 216 : c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1040 : 216 : compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1041 : 216 : EMIT(return_value);
1042 : 216 : EMIT_ARG(label_assign, l_fail);
1043 : 216 : compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1044 : : } else {
1045 : 7346 : compile_node(comp, pns->nodes[0]);
1046 : : }
1047 : 8008 : EMIT(return_value);
1048 : : }
1049 : :
1050 : 1148 : STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1051 : 1148 : compile_node(comp, pns->nodes[0]);
1052 : 1140 : EMIT(pop_top);
1053 : 1140 : }
1054 : :
1055 : 6181 : STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1056 [ + + ]: 6181 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1057 : : // raise
1058 : 54 : EMIT_ARG(raise_varargs, 0);
1059 [ + + + + ]: 6127 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
1060 : : // raise x from y
1061 : 8 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
1062 : 8 : compile_node(comp, pns->nodes[0]);
1063 : 8 : compile_node(comp, pns->nodes[1]);
1064 : 8 : EMIT_ARG(raise_varargs, 2);
1065 : : } else {
1066 : : // raise x
1067 : 6119 : compile_node(comp, pns->nodes[0]);
1068 : 6119 : EMIT_ARG(raise_varargs, 1);
1069 : : }
1070 : 6181 : }
1071 : :
1072 : : // q_base holds the base of the name
1073 : : // eg a -> q_base=a
1074 : : // a.b.c -> q_base=a
1075 : 6166 : STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
1076 : 6166 : bool is_as = false;
1077 [ + + + + : 6166 : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
+ + ]
1078 : 36 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
1079 : : // a name of the form x as y; unwrap it
1080 : 36 : *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1081 : 36 : pn = pns->nodes[0];
1082 : 36 : is_as = true;
1083 : : }
1084 [ - + ]: 280 : if (MP_PARSE_NODE_IS_NULL(pn)) {
1085 : : // empty name (eg, from . import x)
1086 : 116 : *q_base = MP_QSTR_;
1087 : 116 : EMIT_ARG(import, MP_QSTR_, MP_EMIT_IMPORT_NAME); // import the empty string
1088 [ + + ]: 6050 : } else if (MP_PARSE_NODE_IS_ID(pn)) {
1089 : : // just a simple name
1090 : 5798 : qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
1091 [ + + ]: 5798 : if (!is_as) {
1092 : 5770 : *q_base = q_full;
1093 : : }
1094 : 5798 : EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
1095 : : } else {
1096 [ + - + - : 252 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
- + ]
1097 : 252 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
1098 : : {
1099 : : // a name of the form a.b.c
1100 [ + + ]: 252 : if (!is_as) {
1101 : 244 : *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1102 : : }
1103 : 252 : size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
1104 [ - + ]: 252 : if (n == 0) {
1105 : : // There must be at least one node in this PN_dotted_name.
1106 : : // Let the compiler know this so it doesn't warn, and can generate better code.
1107 : 0 : MP_UNREACHABLE;
1108 : : }
1109 : 252 : size_t len = n - 1;
1110 [ + + ]: 780 : for (size_t i = 0; i < n; i++) {
1111 : 528 : len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1112 : : }
1113 : 252 : char *q_ptr = mp_local_alloc(len);
1114 : 252 : char *str_dest = q_ptr;
1115 [ + + ]: 780 : for (size_t i = 0; i < n; i++) {
1116 [ + + ]: 528 : if (i > 0) {
1117 : 276 : *str_dest++ = '.';
1118 : : }
1119 : 528 : size_t str_src_len;
1120 : 528 : const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
1121 : 528 : memcpy(str_dest, str_src, str_src_len);
1122 : 528 : str_dest += str_src_len;
1123 : : }
1124 : 252 : qstr q_full = qstr_from_strn(q_ptr, len);
1125 : 248 : mp_local_free(q_ptr);
1126 : 248 : EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
1127 [ + + ]: 248 : if (is_as) {
1128 [ + + ]: 16 : for (size_t i = 1; i < n; i++) {
1129 : 8 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), MP_EMIT_ATTR_LOAD);
1130 : : }
1131 : : }
1132 : : }
1133 : : }
1134 : 6162 : }
1135 : :
1136 : 4411 : STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
1137 : 4411 : EMIT_ARG(load_const_small_int, 0); // level 0 import
1138 : 4411 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1139 : 4411 : qstr q_base;
1140 : 4411 : do_import_name(comp, pn, &q_base);
1141 : 4407 : compile_store_id(comp, q_base);
1142 : 4407 : }
1143 : :
1144 : 3803 : STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
1145 : 3803 : apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1146 : 3799 : }
1147 : :
1148 : 1759 : STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1149 : 1759 : mp_parse_node_t pn_import_source = pns->nodes[0];
1150 : :
1151 : : // extract the preceding .'s (if any) for a relative import, to compute the import level
1152 : 1759 : uint import_level = 0;
1153 : 3518 : do {
1154 : 1759 : mp_parse_node_t pn_rel;
1155 [ + + + - : 1759 : if (MP_PARSE_NODE_IS_TOKEN(pn_import_source) || MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_one_or_more_period_or_ellipsis)) {
+ + + + ]
1156 : : // This covers relative imports with dots only like "from .. import"
1157 : 116 : pn_rel = pn_import_source;
1158 : 116 : pn_import_source = MP_PARSE_NODE_NULL;
1159 [ + + + + ]: 1643 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1160 : : // This covers relative imports starting with dot(s) like "from .foo import"
1161 : 472 : mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t *)pn_import_source;
1162 : 472 : pn_rel = pns_2b->nodes[0];
1163 : 472 : pn_import_source = pns_2b->nodes[1];
1164 [ - + ]: 472 : assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1165 : : } else {
1166 : : // Not a relative import
1167 : : break;
1168 : : }
1169 : :
1170 : : // get the list of . and/or ...'s
1171 : 588 : mp_parse_node_t *nodes;
1172 : 588 : size_t n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1173 : :
1174 : : // count the total number of .'s
1175 [ + + ]: 1200 : for (size_t i = 0; i < n; i++) {
1176 [ + + ]: 612 : if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1177 : 588 : import_level++;
1178 : : } else {
1179 : : // should be an MP_TOKEN_ELLIPSIS
1180 : 24 : import_level += 3;
1181 : : }
1182 : : }
1183 : : } while (0);
1184 : :
1185 [ + + ]: 1759 : if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
1186 : : #if MICROPY_CPYTHON_COMPAT
1187 [ + + ]: 302 : if (comp->scope_cur->kind != SCOPE_MODULE) {
1188 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("import * not at module level"));
1189 : 4 : return;
1190 : : }
1191 : : #endif
1192 : :
1193 : 298 : EMIT_ARG(load_const_small_int, import_level);
1194 : :
1195 : : // build the "fromlist" tuple
1196 : 298 : EMIT_ARG(load_const_str, MP_QSTR__star_);
1197 : 298 : EMIT_ARG(build, 1, MP_EMIT_BUILD_TUPLE);
1198 : :
1199 : : // do the import
1200 : 298 : qstr dummy_q;
1201 : 298 : do_import_name(comp, pn_import_source, &dummy_q);
1202 : 298 : EMIT_ARG(import, MP_QSTRnull, MP_EMIT_IMPORT_STAR);
1203 : :
1204 : : } else {
1205 : 1457 : EMIT_ARG(load_const_small_int, import_level);
1206 : :
1207 : : // build the "fromlist" tuple
1208 : 1457 : mp_parse_node_t *pn_nodes;
1209 : 1457 : size_t n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1210 [ + + ]: 3819 : for (size_t i = 0; i < n; i++) {
1211 [ + - + - : 2362 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
- + ]
1212 : 2362 : mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pn_nodes[i];
1213 : 2362 : qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1214 : 2362 : EMIT_ARG(load_const_str, id2);
1215 : : }
1216 : 1457 : EMIT_ARG(build, n, MP_EMIT_BUILD_TUPLE);
1217 : :
1218 : : // do the import
1219 : 1457 : qstr dummy_q;
1220 : 1457 : do_import_name(comp, pn_import_source, &dummy_q);
1221 [ + + ]: 3819 : for (size_t i = 0; i < n; i++) {
1222 [ + - + - : 2362 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
- + ]
1223 : 2362 : mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pn_nodes[i];
1224 : 2362 : qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1225 : 2362 : EMIT_ARG(import, id2, MP_EMIT_IMPORT_FROM);
1226 [ + + ]: 2362 : if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
1227 : 2138 : compile_store_id(comp, id2);
1228 : : } else {
1229 : 224 : compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
1230 : : }
1231 : : }
1232 : 1457 : EMIT(pop_top);
1233 : : }
1234 : : }
1235 : :
1236 : 335 : STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, id_info_t *id_info) {
1237 [ + + ]: 335 : if (id_info->kind != ID_INFO_KIND_UNDECIDED && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1238 : 12 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("identifier redefined as global"));
1239 : 12 : return;
1240 : : }
1241 : 323 : id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1242 : :
1243 : : // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1244 : 323 : id_info = scope_find_global(comp->scope_cur, id_info->qst);
1245 [ + + ]: 323 : if (id_info != NULL) {
1246 : 149 : id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1247 : : }
1248 : : }
1249 : :
1250 : 61 : STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, id_info_t *id_info) {
1251 [ + + ]: 61 : if (id_info->kind == ID_INFO_KIND_UNDECIDED) {
1252 : 49 : id_info->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
1253 : 49 : scope_check_to_close_over(comp->scope_cur, id_info);
1254 [ + + ]: 49 : if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1255 : 8 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("no binding for nonlocal found"));
1256 : : }
1257 [ + + ]: 12 : } else if (id_info->kind != ID_INFO_KIND_FREE) {
1258 : 8 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("identifier redefined as nonlocal"));
1259 : : }
1260 : 61 : }
1261 : :
1262 : 396 : STATIC void compile_declare_global_or_nonlocal(compiler_t *comp, mp_parse_node_t pn, id_info_t *id_info, bool is_global) {
1263 [ + + ]: 396 : if (is_global) {
1264 : 335 : compile_declare_global(comp, pn, id_info);
1265 : : } else {
1266 : 61 : compile_declare_nonlocal(comp, pn, id_info);
1267 : : }
1268 : 396 : }
1269 : :
1270 : 1216 : STATIC void compile_global_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1271 [ + + ]: 1216 : if (comp->pass == MP_PASS_SCOPE) {
1272 : 312 : bool is_global = MP_PARSE_NODE_STRUCT_KIND(pns) == PN_global_stmt;
1273 : :
1274 [ + + + + ]: 312 : if (!is_global && comp->scope_cur->kind == SCOPE_MODULE) {
1275 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("can't declare nonlocal in outer code"));
1276 : 4 : return;
1277 : : }
1278 : :
1279 : 308 : mp_parse_node_t *nodes;
1280 : 308 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
1281 [ + + ]: 674 : for (size_t i = 0; i < n; i++) {
1282 : 366 : qstr qst = MP_PARSE_NODE_LEAF_ARG(nodes[i]);
1283 : 366 : id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, ID_INFO_KIND_UNDECIDED);
1284 : 366 : compile_declare_global_or_nonlocal(comp, (mp_parse_node_t)pns, id_info, is_global);
1285 : : }
1286 : : }
1287 : : }
1288 : :
1289 : 1882 : STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1290 : : // with optimisations enabled we don't compile assertions
1291 [ + + ]: 1882 : if (MP_STATE_VM(mp_optimise_value) != 0) {
1292 : : return;
1293 : : }
1294 : :
1295 : 1858 : uint l_end = comp_next_label(comp);
1296 : 1858 : c_if_cond(comp, pns->nodes[0], true, l_end);
1297 : 1858 : EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
1298 [ + + ]: 1858 : if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1299 : : // assertion message
1300 : 64 : compile_node(comp, pns->nodes[1]);
1301 : 64 : EMIT_ARG(call_function, 1, 0, 0);
1302 : : }
1303 : 1858 : EMIT_ARG(raise_varargs, 1);
1304 : 1858 : EMIT_ARG(label_assign, l_end);
1305 : : }
1306 : :
1307 : 7685 : STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1308 : 7685 : uint l_end = comp_next_label(comp);
1309 : :
1310 : : // optimisation: don't emit anything when "if False"
1311 [ + + ]: 7685 : if (!mp_parse_node_is_const_false(pns->nodes[0])) {
1312 : 7573 : uint l_fail = comp_next_label(comp);
1313 : 7573 : c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1314 : :
1315 : 7573 : compile_node(comp, pns->nodes[1]); // if block
1316 : :
1317 : : // optimisation: skip everything else when "if True"
1318 [ + + ]: 7573 : if (mp_parse_node_is_const_true(pns->nodes[0])) {
1319 : 184 : goto done;
1320 : : }
1321 : :
1322 : : // optimisation: don't jump over non-existent elif/else blocks
1323 [ + + + + ]: 7389 : if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) {
1324 : : // jump over elif/else blocks
1325 : 2626 : EMIT_ARG(jump, l_end);
1326 : : }
1327 : :
1328 : 7389 : EMIT_ARG(label_assign, l_fail);
1329 : : }
1330 : :
1331 : : // compile elif blocks (if any)
1332 : 7501 : mp_parse_node_t *pn_elif;
1333 : 7501 : size_t n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1334 [ + + ]: 8692 : for (size_t i = 0; i < n_elif; i++) {
1335 [ + - + - : 1199 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
- + ]
1336 : 1199 : mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t *)pn_elif[i];
1337 : :
1338 : : // optimisation: don't emit anything when "if False"
1339 [ + + ]: 1199 : if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
1340 : 1191 : uint l_fail = comp_next_label(comp);
1341 : 1191 : c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1342 : :
1343 : 1191 : compile_node(comp, pns_elif->nodes[1]); // elif block
1344 : :
1345 : : // optimisation: skip everything else when "elif True"
1346 [ + + ]: 1191 : if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
1347 : 8 : goto done;
1348 : : }
1349 : :
1350 : 1183 : EMIT_ARG(jump, l_end);
1351 : 1183 : EMIT_ARG(label_assign, l_fail);
1352 : : }
1353 : : }
1354 : :
1355 : : // compile else block
1356 : 7493 : compile_node(comp, pns->nodes[3]); // can be null
1357 : :
1358 : 7685 : done:
1359 : 7685 : EMIT_ARG(label_assign, l_end);
1360 : 7685 : }
1361 : :
1362 : : #define START_BREAK_CONTINUE_BLOCK \
1363 : : uint16_t old_break_label = comp->break_label; \
1364 : : uint16_t old_continue_label = comp->continue_label; \
1365 : : uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
1366 : : uint break_label = comp_next_label(comp); \
1367 : : uint continue_label = comp_next_label(comp); \
1368 : : comp->break_label = break_label; \
1369 : : comp->continue_label = continue_label; \
1370 : : comp->break_continue_except_level = comp->cur_except_level;
1371 : :
1372 : : #define END_BREAK_CONTINUE_BLOCK \
1373 : : comp->break_label = old_break_label; \
1374 : : comp->continue_label = old_continue_label; \
1375 : : comp->break_continue_except_level = old_break_continue_except_level;
1376 : :
1377 : 1599 : STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1378 : 1599 : START_BREAK_CONTINUE_BLOCK
1379 : :
1380 [ + + ]: 1599 : if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1381 : 1559 : uint top_label = comp_next_label(comp);
1382 [ + + ]: 1559 : if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1383 : 817 : EMIT_ARG(jump, continue_label);
1384 : : }
1385 : 1559 : EMIT_ARG(label_assign, top_label);
1386 : 1559 : compile_node(comp, pns->nodes[1]); // body
1387 : 1559 : EMIT_ARG(label_assign, continue_label);
1388 : 1559 : c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1389 : : }
1390 : :
1391 : : // break/continue apply to outer loop (if any) in the else block
1392 : 1599 : END_BREAK_CONTINUE_BLOCK
1393 : :
1394 : 1599 : compile_node(comp, pns->nodes[2]); // else
1395 : :
1396 : 1599 : EMIT_ARG(label_assign, break_label);
1397 : 1599 : }
1398 : :
1399 : : // This function compiles an optimised for-loop of the form:
1400 : : // for <var> in range(<start>, <end>, <step>):
1401 : : // <body>
1402 : : // else:
1403 : : // <else>
1404 : : // <var> must be an identifier and <step> must be a small-int.
1405 : : //
1406 : : // Semantics of for-loop require:
1407 : : // - final failing value should not be stored in the loop variable
1408 : : // - if the loop never runs, the loop variable should never be assigned
1409 : : // - assignments to <var>, <end> or <step> in the body do not alter the loop
1410 : : // (<step> is a constant for us, so no need to worry about it changing)
1411 : : //
1412 : : // If <end> is a small-int, then the stack during the for-loop contains just
1413 : : // the current value of <var>. Otherwise, the stack contains <end> then the
1414 : : // current value of <var>.
1415 : 2203 : STATIC void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
1416 : 2203 : START_BREAK_CONTINUE_BLOCK
1417 : :
1418 : 2203 : uint top_label = comp_next_label(comp);
1419 : 2203 : uint entry_label = comp_next_label(comp);
1420 : :
1421 : : // put the end value on the stack if it's not a small-int constant
1422 : 2203 : bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1423 [ + + ]: 2203 : if (end_on_stack) {
1424 : 1071 : compile_node(comp, pn_end);
1425 : : }
1426 : :
1427 : : // compile: start
1428 : 2203 : compile_node(comp, pn_start);
1429 : :
1430 : 2203 : EMIT_ARG(jump, entry_label);
1431 : 2203 : EMIT_ARG(label_assign, top_label);
1432 : :
1433 : : // duplicate next value and store it to var
1434 : 2203 : EMIT(dup_top);
1435 : 2203 : c_assign(comp, pn_var, ASSIGN_STORE);
1436 : :
1437 : : // compile body
1438 : 2203 : compile_node(comp, pn_body);
1439 : :
1440 : 2203 : EMIT_ARG(label_assign, continue_label);
1441 : :
1442 : : // compile: var + step
1443 : 2203 : compile_node(comp, pn_step);
1444 : 2203 : EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
1445 : :
1446 : 2203 : EMIT_ARG(label_assign, entry_label);
1447 : :
1448 : : // compile: if var <cond> end: goto top
1449 [ + + ]: 2203 : if (end_on_stack) {
1450 : 1071 : EMIT(dup_top_two);
1451 : 1071 : EMIT(rot_two);
1452 : : } else {
1453 : 1132 : EMIT(dup_top);
1454 : 1132 : compile_node(comp, pn_end);
1455 : : }
1456 [ - + ]: 2203 : assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1457 [ + + ]: 2203 : if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
1458 : 2186 : EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
1459 : : } else {
1460 : 17 : EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
1461 : : }
1462 : 2203 : EMIT_ARG(pop_jump_if, true, top_label);
1463 : :
1464 : : // break/continue apply to outer loop (if any) in the else block
1465 : 2203 : END_BREAK_CONTINUE_BLOCK
1466 : :
1467 : : // Compile the else block. We must pop the iterator variables before
1468 : : // executing the else code because it may contain break/continue statements.
1469 : 2203 : uint end_label = 0;
1470 [ + + ]: 2203 : if (!MP_PARSE_NODE_IS_NULL(pn_else)) {
1471 : : // discard final value of "var", and possible "end" value
1472 : 32 : EMIT(pop_top);
1473 [ + + ]: 32 : if (end_on_stack) {
1474 : 8 : EMIT(pop_top);
1475 : : }
1476 : 32 : compile_node(comp, pn_else);
1477 : 32 : end_label = comp_next_label(comp);
1478 : 32 : EMIT_ARG(jump, end_label);
1479 : 32 : EMIT_ARG(adjust_stack_size, 1 + end_on_stack);
1480 : : }
1481 : :
1482 : 2203 : EMIT_ARG(label_assign, break_label);
1483 : :
1484 : : // discard final value of var that failed the loop condition
1485 : 2203 : EMIT(pop_top);
1486 : :
1487 : : // discard <end> value if it's on the stack
1488 [ + + ]: 2203 : if (end_on_stack) {
1489 : 1071 : EMIT(pop_top);
1490 : : }
1491 : :
1492 [ + + ]: 2203 : if (!MP_PARSE_NODE_IS_NULL(pn_else)) {
1493 : 32 : EMIT_ARG(label_assign, end_label);
1494 : : }
1495 : 2203 : }
1496 : :
1497 : 5078 : STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1498 : : // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1499 : : // this is actually slower, but uses no heap memory
1500 : : // for viper it will be much, much faster
1501 [ + + + - : 5078 : if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_atom_expr_normal)) {
+ + + + ]
1502 : 2783 : mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t *)pns->nodes[1];
1503 [ + - ]: 2783 : if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1504 [ + + ]: 2783 : && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1505 [ + - ]: 2327 : && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pns_it->nodes[1]) == PN_trailer_paren) {
1506 : 2327 : mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t *)pns_it->nodes[1])->nodes[0];
1507 : 2327 : mp_parse_node_t *args;
1508 : 2327 : size_t n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
1509 : 2327 : mp_parse_node_t pn_range_start;
1510 : 2327 : mp_parse_node_t pn_range_end;
1511 : 2327 : mp_parse_node_t pn_range_step;
1512 : 2327 : bool optimize = false;
1513 [ + - ]: 2327 : if (1 <= n_args && n_args <= 3) {
1514 : 2327 : optimize = true;
1515 [ + + ]: 2327 : if (n_args == 1) {
1516 : 1776 : pn_range_start = mp_parse_node_new_small_int(0);
1517 : 1776 : pn_range_end = args[0];
1518 : 1776 : pn_range_step = mp_parse_node_new_small_int(1);
1519 [ + + ]: 551 : } else if (n_args == 2) {
1520 : 456 : pn_range_start = args[0];
1521 : 456 : pn_range_end = args[1];
1522 : 456 : pn_range_step = mp_parse_node_new_small_int(1);
1523 : : } else {
1524 : 95 : pn_range_start = args[0];
1525 : 95 : pn_range_end = args[1];
1526 : 95 : pn_range_step = args[2];
1527 : : // the step must be a non-zero constant integer to do the optimisation
1528 [ + + ]: 95 : if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)
1529 [ + + ]: 52 : || MP_PARSE_NODE_LEAF_SMALL_INT(pn_range_step) == 0) {
1530 : 52 : optimize = false;
1531 : : }
1532 : : }
1533 : : // arguments must be able to be compiled as standard expressions
1534 [ + + + + ]: 2327 : if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1535 : 17 : int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn_range_start);
1536 [ + + ]: 17 : if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1537 : 9 : optimize = false;
1538 : : }
1539 : : }
1540 [ + + + + ]: 2327 : if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1541 : 545 : int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn_range_end);
1542 [ + + ]: 545 : if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1543 : 63 : optimize = false;
1544 : : }
1545 : : }
1546 : : }
1547 [ + + ]: 2327 : if (optimize) {
1548 : 2203 : compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1549 : 2203 : return;
1550 : : }
1551 : : }
1552 : : }
1553 : :
1554 : 2875 : START_BREAK_CONTINUE_BLOCK
1555 : 2875 : comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1556 : :
1557 : 2875 : uint pop_label = comp_next_label(comp);
1558 : :
1559 : 2875 : compile_node(comp, pns->nodes[1]); // iterator
1560 : 2875 : EMIT_ARG(get_iter, true);
1561 : 2875 : EMIT_ARG(label_assign, continue_label);
1562 : 2875 : EMIT_ARG(for_iter, pop_label);
1563 : 2875 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1564 : 2875 : compile_node(comp, pns->nodes[2]); // body
1565 : 2875 : EMIT_ARG(jump, continue_label);
1566 : 2875 : EMIT_ARG(label_assign, pop_label);
1567 : 2875 : EMIT(for_iter_end);
1568 : :
1569 : : // break/continue apply to outer loop (if any) in the else block
1570 : 2875 : END_BREAK_CONTINUE_BLOCK
1571 : :
1572 : 2875 : compile_node(comp, pns->nodes[3]); // else (may be empty)
1573 : :
1574 : 2875 : EMIT_ARG(label_assign, break_label);
1575 : : }
1576 : :
1577 : 48905 : STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) {
1578 : : // setup code
1579 : 48905 : uint l1 = comp_next_label(comp);
1580 : 48905 : uint success_label = comp_next_label(comp);
1581 : :
1582 : 48905 : compile_increase_except_level(comp, l1, MP_EMIT_SETUP_BLOCK_EXCEPT);
1583 : :
1584 : 48905 : compile_node(comp, pn_body); // body
1585 : 48905 : EMIT_ARG(pop_except_jump, success_label, false); // jump over exception handler
1586 : :
1587 : 48905 : EMIT_ARG(label_assign, l1); // start of exception handler
1588 : 48905 : EMIT(start_except_handler);
1589 : :
1590 : : // at this point the top of the stack contains the exception instance that was raised
1591 : :
1592 : 48905 : uint l2 = comp_next_label(comp);
1593 : :
1594 [ + + ]: 97932 : for (int i = 0; i < n_except; i++) {
1595 [ + - + - : 49031 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
- + ]
1596 : 49031 : mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t *)pn_excepts[i];
1597 : :
1598 : 49031 : qstr qstr_exception_local = 0;
1599 : 49031 : uint end_finally_label = comp_next_label(comp);
1600 : : #if MICROPY_PY_SYS_SETTRACE
1601 : : EMIT_ARG(set_source_line, pns_except->source_line);
1602 : : #endif
1603 : :
1604 [ + + ]: 49031 : if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
1605 : : // this is a catch all exception handler
1606 [ + + ]: 1393 : if (i + 1 != n_except) {
1607 : 4 : compile_syntax_error(comp, pn_excepts[i], MP_ERROR_TEXT("default 'except' must be last"));
1608 : 4 : compile_decrease_except_level(comp);
1609 : 4 : return;
1610 : : }
1611 : : } else {
1612 : : // this exception handler requires a match to a certain type of exception
1613 : 47638 : mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1614 [ + + ]: 47638 : if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1615 : 2096 : mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pns_exception_expr;
1616 [ + + ]: 2096 : if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
1617 : : // handler binds the exception to a local
1618 : 1541 : pns_exception_expr = pns3->nodes[0];
1619 : 1541 : qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
1620 : : }
1621 : : }
1622 : 47638 : EMIT(dup_top);
1623 : 47638 : compile_node(comp, pns_exception_expr);
1624 : 47638 : EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1625 : 47638 : EMIT_ARG(pop_jump_if, false, end_finally_label);
1626 : : }
1627 : :
1628 : : // either discard or store the exception instance
1629 [ + + ]: 47638 : if (qstr_exception_local == 0) {
1630 : 47486 : EMIT(pop_top);
1631 : : } else {
1632 : 1541 : compile_store_id(comp, qstr_exception_local);
1633 : : }
1634 : :
1635 : : // If the exception is bound to a variable <e> then the <body> of the
1636 : : // exception handler is wrapped in a try-finally so that the name <e> can
1637 : : // be deleted (per Python semantics) even if the <body> has an exception.
1638 : : // In such a case the generated code for the exception handler is:
1639 : : // try:
1640 : : // <body>
1641 : : // finally:
1642 : : // <e> = None
1643 : : // del <e>
1644 : 49027 : uint l3 = 0;
1645 [ + + ]: 49027 : if (qstr_exception_local != 0) {
1646 : 1541 : l3 = comp_next_label(comp);
1647 : 1541 : compile_increase_except_level(comp, l3, MP_EMIT_SETUP_BLOCK_FINALLY);
1648 : : }
1649 : 49027 : compile_node(comp, pns_except->nodes[1]); // the <body>
1650 [ + + ]: 49027 : if (qstr_exception_local != 0) {
1651 : 1541 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1652 : 1541 : EMIT_ARG(label_assign, l3);
1653 : 1541 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1654 : 1541 : compile_store_id(comp, qstr_exception_local);
1655 : 1541 : compile_delete_id(comp, qstr_exception_local);
1656 : 1541 : compile_decrease_except_level(comp);
1657 : : }
1658 : :
1659 : 49027 : EMIT_ARG(pop_except_jump, l2, true);
1660 : 49027 : EMIT_ARG(label_assign, end_finally_label);
1661 : 49027 : EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
1662 : : }
1663 : :
1664 : 48901 : compile_decrease_except_level(comp);
1665 : 48901 : EMIT(end_except_handler);
1666 : :
1667 : 48901 : EMIT_ARG(label_assign, success_label);
1668 : 48901 : compile_node(comp, pn_else); // else block, can be null
1669 : 48901 : EMIT_ARG(label_assign, l2);
1670 : : }
1671 : :
1672 : 983 : STATIC void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
1673 : 983 : uint l_finally_block = comp_next_label(comp);
1674 : :
1675 : 983 : compile_increase_except_level(comp, l_finally_block, MP_EMIT_SETUP_BLOCK_FINALLY);
1676 : :
1677 [ + + ]: 983 : if (n_except == 0) {
1678 [ - + ]: 891 : assert(MP_PARSE_NODE_IS_NULL(pn_else));
1679 : 891 : EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
1680 : 891 : compile_node(comp, pn_body);
1681 : 891 : EMIT_ARG(adjust_stack_size, -3);
1682 : : } else {
1683 : 92 : compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1684 : : }
1685 : 983 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1686 : 983 : EMIT_ARG(label_assign, l_finally_block);
1687 : 983 : compile_node(comp, pn_finally);
1688 : :
1689 : 983 : compile_decrease_except_level(comp);
1690 : 983 : }
1691 : :
1692 : 49796 : STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1693 [ + - - + ]: 49796 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1694 : : {
1695 : 49796 : mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)pns->nodes[1];
1696 [ + + ]: 49796 : if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
1697 : : // just try-finally
1698 : 891 : compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1699 [ + + ]: 48905 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
1700 : : // try-except and possibly else and/or finally
1701 : 464 : mp_parse_node_t *pn_excepts;
1702 : 464 : size_t n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
1703 [ + + ]: 464 : if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
1704 : : // no finally
1705 : 372 : compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1706 : : } else {
1707 : : // have finally
1708 : 92 : compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t *)pns2->nodes[2])->nodes[0]);
1709 : : }
1710 : : } else {
1711 : : // just try-except
1712 : 48441 : mp_parse_node_t *pn_excepts;
1713 : 48441 : size_t n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
1714 : 48441 : compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
1715 : : }
1716 : : }
1717 : 49796 : }
1718 : :
1719 : 73788 : STATIC void compile_with_stmt_helper(compiler_t *comp, size_t n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1720 [ + + ]: 73788 : if (n == 0) {
1721 : : // no more pre-bits, compile the body of the with
1722 : 36894 : compile_node(comp, body);
1723 : : } else {
1724 : 36894 : uint l_end = comp_next_label(comp);
1725 [ + - + + : 73574 : if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
+ + ]
1726 : : // this pre-bit is of the form "a as b"
1727 : 36680 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)nodes[0];
1728 : 36680 : compile_node(comp, pns->nodes[0]);
1729 : 36680 : compile_increase_except_level(comp, l_end, MP_EMIT_SETUP_BLOCK_WITH);
1730 : 36680 : c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1731 : : } else {
1732 : : // this pre-bit is just an expression
1733 : 214 : compile_node(comp, nodes[0]);
1734 : 214 : compile_increase_except_level(comp, l_end, MP_EMIT_SETUP_BLOCK_WITH);
1735 : 214 : EMIT(pop_top);
1736 : : }
1737 : : // compile additional pre-bits and the body
1738 : 36894 : compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1739 : : // finish this with block
1740 : 36894 : EMIT_ARG(with_cleanup, l_end);
1741 : 36894 : reserve_labels_for_native(comp, 3); // used by native's with_cleanup
1742 : 36894 : compile_decrease_except_level(comp);
1743 : : }
1744 : 73788 : }
1745 : :
1746 : 36894 : STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1747 : : // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1748 : 36894 : mp_parse_node_t *nodes;
1749 : 36894 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1750 [ - + ]: 36894 : assert(n > 0);
1751 : :
1752 : : // compile in a nested fashion
1753 : 36894 : compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1754 : 36894 : }
1755 : :
1756 : 1872 : STATIC void compile_yield_from(compiler_t *comp) {
1757 : 1872 : EMIT_ARG(get_iter, false);
1758 : 1872 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1759 : 1872 : EMIT_ARG(yield, MP_EMIT_YIELD_FROM);
1760 : 1868 : reserve_labels_for_native(comp, 3);
1761 : 1868 : }
1762 : :
1763 : : #if MICROPY_PY_ASYNC_AWAIT
1764 : 96 : STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1765 : 96 : EMIT_ARG(load_method, method, false);
1766 : 96 : EMIT_ARG(call_method, 0, 0, 0);
1767 : 96 : compile_yield_from(comp);
1768 : 96 : }
1769 : :
1770 : 40 : STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1771 : : // Allocate labels.
1772 : 40 : uint while_else_label = comp_next_label(comp);
1773 : 40 : uint try_exception_label = comp_next_label(comp);
1774 : 40 : uint try_else_label = comp_next_label(comp);
1775 : 40 : uint try_finally_label = comp_next_label(comp);
1776 : :
1777 : : // Stack: (...)
1778 : :
1779 : : // Compile the iterator expression and load and call its __aiter__ method.
1780 : 40 : compile_node(comp, pns->nodes[1]); // iterator
1781 : : // Stack: (..., iterator)
1782 : 40 : EMIT_ARG(load_method, MP_QSTR___aiter__, false);
1783 : : // Stack: (..., iterator, __aiter__)
1784 : 40 : EMIT_ARG(call_method, 0, 0, 0);
1785 : : // Stack: (..., iterable)
1786 : :
1787 : 40 : START_BREAK_CONTINUE_BLOCK
1788 : :
1789 : 40 : EMIT_ARG(label_assign, continue_label);
1790 : :
1791 : 40 : compile_increase_except_level(comp, try_exception_label, MP_EMIT_SETUP_BLOCK_EXCEPT);
1792 : :
1793 : 40 : EMIT(dup_top);
1794 : : // Stack: (..., iterable, iterable)
1795 : :
1796 : : // Compile: yield from iterable.__anext__()
1797 : 40 : compile_await_object_method(comp, MP_QSTR___anext__);
1798 : : // Stack: (..., iterable, yielded_value)
1799 : :
1800 : 40 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1801 : : // Stack: (..., iterable)
1802 : 40 : EMIT_ARG(pop_except_jump, try_else_label, false);
1803 : :
1804 : 40 : EMIT_ARG(label_assign, try_exception_label);
1805 : 40 : EMIT(start_except_handler);
1806 : 40 : EMIT(dup_top);
1807 : 40 : EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1808 : 40 : EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1809 : 40 : EMIT_ARG(pop_jump_if, false, try_finally_label);
1810 : 40 : EMIT(pop_top); // pop exception instance
1811 : 40 : EMIT_ARG(pop_except_jump, while_else_label, true);
1812 : :
1813 : 40 : EMIT_ARG(label_assign, try_finally_label);
1814 : 40 : EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
1815 : 40 : compile_decrease_except_level(comp);
1816 : 40 : EMIT(end_except_handler);
1817 : :
1818 : : // Stack: (..., iterable)
1819 : :
1820 : 40 : EMIT_ARG(label_assign, try_else_label);
1821 : 40 : compile_node(comp, pns->nodes[2]); // body
1822 : :
1823 : 40 : EMIT_ARG(jump, continue_label);
1824 : : // break/continue apply to outer loop (if any) in the else block
1825 : 40 : END_BREAK_CONTINUE_BLOCK
1826 : :
1827 : 40 : EMIT_ARG(label_assign, while_else_label);
1828 : 40 : compile_node(comp, pns->nodes[3]); // else
1829 : :
1830 : 40 : EMIT_ARG(label_assign, break_label);
1831 : : // Stack: (..., iterable)
1832 : :
1833 : 40 : EMIT(pop_top);
1834 : : // Stack: (...)
1835 : 40 : }
1836 : :
1837 : 112 : STATIC void compile_async_with_stmt_helper(compiler_t *comp, size_t n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1838 [ + + ]: 112 : if (n == 0) {
1839 : : // no more pre-bits, compile the body of the with
1840 : 56 : compile_node(comp, body);
1841 : : } else {
1842 : 56 : uint l_finally_block = comp_next_label(comp);
1843 : 56 : uint l_aexit_no_exc = comp_next_label(comp);
1844 : 56 : uint l_ret_unwind_jump = comp_next_label(comp);
1845 : 56 : uint l_end = comp_next_label(comp);
1846 : :
1847 [ + - + + : 60 : if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
+ + ]
1848 : : // this pre-bit is of the form "a as b"
1849 : 4 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)nodes[0];
1850 : 4 : compile_node(comp, pns->nodes[0]);
1851 : 4 : EMIT(dup_top);
1852 : 4 : compile_await_object_method(comp, MP_QSTR___aenter__);
1853 : 4 : c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1854 : : } else {
1855 : : // this pre-bit is just an expression
1856 : 52 : compile_node(comp, nodes[0]);
1857 : 52 : EMIT(dup_top);
1858 : 52 : compile_await_object_method(comp, MP_QSTR___aenter__);
1859 : 52 : EMIT(pop_top);
1860 : : }
1861 : :
1862 : : // To keep the Python stack size down, and because we can't access values on
1863 : : // this stack further down than 3 elements (via rot_three), we don't preload
1864 : : // __aexit__ (as per normal with) but rather wait until we need it below.
1865 : :
1866 : : // Start the try-finally statement
1867 : 56 : compile_increase_except_level(comp, l_finally_block, MP_EMIT_SETUP_BLOCK_FINALLY);
1868 : :
1869 : : // Compile any additional pre-bits of the "async with", and also the body
1870 : 56 : EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
1871 : 56 : compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1872 : 56 : EMIT_ARG(adjust_stack_size, -3);
1873 : :
1874 : : // We have now finished the "try" block and fall through to the "finally"
1875 : :
1876 : : // At this point, after the with body has executed, we have 3 cases:
1877 : : // 1. no exception, we just fall through to this point; stack: (..., ctx_mgr)
1878 : : // 2. exception propagating out, we get to the finally block; stack: (..., ctx_mgr, exc)
1879 : : // 3. return or unwind jump, we get to the finally block; stack: (..., ctx_mgr, X, INT)
1880 : :
1881 : : // Handle case 1: call __aexit__
1882 : : // Stack: (..., ctx_mgr)
1883 : 56 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // to tell end_finally there's no exception
1884 : 56 : EMIT(rot_two);
1885 : 56 : EMIT_ARG(jump, l_aexit_no_exc); // jump to code below to call __aexit__
1886 : :
1887 : : // Start of "finally" block
1888 : : // At this point we have case 2 or 3, we detect which one by the TOS being an exception or not
1889 : 56 : EMIT_ARG(label_assign, l_finally_block);
1890 : :
1891 : : // Detect if TOS an exception or not
1892 : 56 : EMIT(dup_top);
1893 : 56 : EMIT_LOAD_GLOBAL(MP_QSTR_BaseException);
1894 : 56 : EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1895 : 56 : EMIT_ARG(pop_jump_if, false, l_ret_unwind_jump); // if not an exception then we have case 3
1896 : :
1897 : : // Handle case 2: call __aexit__ and either swallow or re-raise the exception
1898 : : // Stack: (..., ctx_mgr, exc)
1899 : 56 : EMIT(dup_top);
1900 : 56 : EMIT(rot_three);
1901 : 56 : EMIT(rot_two);
1902 : 56 : EMIT_ARG(load_method, MP_QSTR___aexit__, false);
1903 : 56 : EMIT(rot_three);
1904 : 56 : EMIT(rot_three);
1905 : 56 : EMIT(dup_top);
1906 : : #if MICROPY_CPYTHON_COMPAT
1907 : 56 : EMIT_ARG(attr, MP_QSTR___class__, MP_EMIT_ATTR_LOAD); // get type(exc)
1908 : : #else
1909 : : compile_load_id(comp, MP_QSTR_type);
1910 : : EMIT(rot_two);
1911 : : EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1912 : : #endif
1913 : 56 : EMIT(rot_two);
1914 : 56 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1915 : : // Stack: (..., exc, __aexit__, ctx_mgr, type(exc), exc, None)
1916 : 56 : EMIT_ARG(call_method, 3, 0, 0);
1917 : 56 : compile_yield_from(comp);
1918 : 56 : EMIT_ARG(pop_jump_if, false, l_end);
1919 : 56 : EMIT(pop_top); // pop exception
1920 : 56 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // replace with None to swallow exception
1921 : 56 : EMIT_ARG(jump, l_end);
1922 : 56 : EMIT_ARG(adjust_stack_size, 2);
1923 : :
1924 : : // Handle case 3: call __aexit__
1925 : : // Stack: (..., ctx_mgr, X, INT)
1926 : 56 : EMIT_ARG(label_assign, l_ret_unwind_jump);
1927 : 56 : EMIT(rot_three);
1928 : 56 : EMIT(rot_three);
1929 : 56 : EMIT_ARG(label_assign, l_aexit_no_exc);
1930 : 56 : EMIT_ARG(load_method, MP_QSTR___aexit__, false);
1931 : 56 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1932 : 56 : EMIT(dup_top);
1933 : 56 : EMIT(dup_top);
1934 : 56 : EMIT_ARG(call_method, 3, 0, 0);
1935 : 56 : compile_yield_from(comp);
1936 : 56 : EMIT(pop_top);
1937 : 56 : EMIT_ARG(adjust_stack_size, -1);
1938 : :
1939 : : // End of "finally" block
1940 : : // Stack can have one of three configurations:
1941 : : // a. (..., None) - from either case 1, or case 2 with swallowed exception
1942 : : // b. (..., exc) - from case 2 with re-raised exception
1943 : : // c. (..., X, INT) - from case 3
1944 : 56 : EMIT_ARG(label_assign, l_end);
1945 : 56 : compile_decrease_except_level(comp);
1946 : : }
1947 : 112 : }
1948 : :
1949 : 56 : STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1950 : : // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1951 : 56 : mp_parse_node_t *nodes;
1952 : 56 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1953 [ - + ]: 56 : assert(n > 0);
1954 : :
1955 : : // compile in a nested fashion
1956 : 56 : compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1957 : 56 : }
1958 : :
1959 : 1232 : STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1960 [ + - - + ]: 1232 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1961 : 1232 : mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns->nodes[0];
1962 [ + + ]: 1232 : if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1963 : : // async def
1964 : 1128 : compile_funcdef(comp, pns0);
1965 : 1128 : scope_t *fscope = (scope_t *)pns0->nodes[4];
1966 : 1128 : fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1967 : : } else {
1968 : : // async for/with; first verify the scope is a generator
1969 : 104 : int scope_flags = comp->scope_cur->scope_flags;
1970 [ + + ]: 104 : if (!(scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
1971 : 8 : compile_syntax_error(comp, (mp_parse_node_t)pns0,
1972 : 8 : MP_ERROR_TEXT("async for/with outside async function"));
1973 : 8 : return;
1974 : : }
1975 : :
1976 [ + + ]: 96 : if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1977 : : // async for
1978 : 40 : compile_async_for_stmt(comp, pns0);
1979 : : } else {
1980 : : // async with
1981 [ - + ]: 56 : assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1982 : 56 : compile_async_with_stmt(comp, pns0);
1983 : : }
1984 : : }
1985 : : }
1986 : : #endif
1987 : :
1988 : 168690 : STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1989 : 168690 : mp_parse_node_t pn_rhs = pns->nodes[1];
1990 [ + + ]: 168690 : if (MP_PARSE_NODE_IS_NULL(pn_rhs)) {
1991 [ + + + + ]: 130869 : if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1992 : : // for REPL, evaluate then print the expression
1993 : 456 : compile_load_id(comp, MP_QSTR___repl_print__);
1994 : 456 : compile_node(comp, pns->nodes[0]);
1995 : 456 : EMIT_ARG(call_function, 1, 0, 0);
1996 : 456 : EMIT(pop_top);
1997 : :
1998 : : } else {
1999 : : // for non-REPL, evaluate then discard the expression
2000 [ + + + - ]: 130413 : if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2001 [ + - + + : 130413 : || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
+ - ]
2002 : : // do nothing with a lonely constant
2003 : : } else {
2004 : 130413 : compile_node(comp, pns->nodes[0]); // just an expression
2005 : 130404 : EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2006 : : }
2007 : : }
2008 [ + + ]: 37821 : } else if (MP_PARSE_NODE_IS_STRUCT(pn_rhs)) {
2009 : 23574 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pn_rhs;
2010 : 23574 : int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
2011 [ + + ]: 23574 : if (kind == PN_annassign) {
2012 : : // the annotation is in pns1->nodes[0] and is ignored
2013 [ + + ]: 20 : if (MP_PARSE_NODE_IS_NULL(pns1->nodes[1])) {
2014 : : // an annotation of the form "x: y"
2015 : : // inside a function this declares "x" as a local
2016 [ + + ]: 12 : if (comp->scope_cur->kind == SCOPE_FUNCTION) {
2017 [ + + ]: 8 : if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
2018 : 4 : qstr lhs = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2019 : 4 : scope_find_or_add_id(comp->scope_cur, lhs, ID_INFO_KIND_LOCAL);
2020 : : }
2021 : : }
2022 : : } else {
2023 : : // an assigned annotation of the form "x: y = z"
2024 : 8 : pn_rhs = pns1->nodes[1];
2025 : 8 : goto plain_assign;
2026 : : }
2027 [ + + ]: 23554 : } else if (kind == PN_expr_stmt_augassign) {
2028 : 1634 : c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2029 : 1634 : compile_node(comp, pns1->nodes[1]); // rhs
2030 [ - + ]: 1634 : assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
2031 : 1634 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
2032 : 1634 : mp_binary_op_t op = MP_BINARY_OP_INPLACE_OR + (tok - MP_TOKEN_DEL_PIPE_EQUAL);
2033 : 1634 : EMIT_ARG(binary_op, op);
2034 : 1634 : c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2035 [ + + ]: 21920 : } else if (kind == PN_expr_stmt_assign_list) {
2036 : 128 : int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2037 : 128 : compile_node(comp, pns1->nodes[rhs]); // rhs
2038 : : // following CPython, we store left-most first
2039 [ + - ]: 128 : if (rhs > 0) {
2040 : 128 : EMIT(dup_top);
2041 : : }
2042 : 128 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2043 [ + + ]: 400 : for (int i = 0; i < rhs; i++) {
2044 [ + + ]: 272 : if (i + 1 < rhs) {
2045 : 144 : EMIT(dup_top);
2046 : : }
2047 : 272 : c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
2048 : : }
2049 : : } else {
2050 : 21792 : plain_assign:
2051 : : #if MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2052 [ + - + + : 36047 : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_rhs, PN_testlist_star_expr)
+ + ]
2053 [ + - + - : 68 : && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)) {
+ - ]
2054 : 68 : mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns->nodes[0];
2055 : 68 : pns1 = (mp_parse_node_struct_t *)pn_rhs;
2056 : 68 : uint32_t n_pns0 = MP_PARSE_NODE_STRUCT_NUM_NODES(pns0);
2057 : : // Can only optimise a tuple-to-tuple assignment when all of the following hold:
2058 : : // - equal number of items in LHS and RHS tuples
2059 : : // - 2 or 3 items in the tuples
2060 : : // - there are no star expressions in the LHS tuple
2061 [ + - ]: 68 : if (n_pns0 == MP_PARSE_NODE_STRUCT_NUM_NODES(pns1)
2062 : 68 : && (n_pns0 == 2
2063 : : #if MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2064 [ + - ]: 68 : || n_pns0 == 3
2065 : : #endif
2066 : : )
2067 [ + - + + : 68 : && !MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
+ - ]
2068 [ + - + + : 68 : && !MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
+ - ]
2069 : : #if MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2070 [ + + + - : 68 : && (n_pns0 == 2 || !MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr))
- + - - ]
2071 : : #endif
2072 : : ) {
2073 : : // Optimisation for a, b = c, d or a, b, c = d, e, f
2074 : 68 : compile_node(comp, pns1->nodes[0]); // rhs
2075 : 68 : compile_node(comp, pns1->nodes[1]); // rhs
2076 : : #if MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2077 [ + + ]: 68 : if (n_pns0 == 3) {
2078 : 16 : compile_node(comp, pns1->nodes[2]); // rhs
2079 : 16 : EMIT(rot_three);
2080 : : }
2081 : : #endif
2082 : 68 : EMIT(rot_two);
2083 : 68 : c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2084 : 68 : c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2085 : : #if MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
2086 [ + + ]: 68 : if (n_pns0 == 3) {
2087 : 16 : c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2088 : : }
2089 : : #endif
2090 : 68 : return;
2091 : : }
2092 : : }
2093 : : #endif
2094 : :
2095 : 35979 : compile_node(comp, pn_rhs); // rhs
2096 : 35979 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2097 : : }
2098 : : } else {
2099 : 14247 : goto plain_assign;
2100 : : }
2101 : : }
2102 : :
2103 : 335 : STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2104 [ + - + - : 335 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
- + ]
2105 : 335 : mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t *)pns->nodes[1];
2106 : :
2107 : 335 : uint l_fail = comp_next_label(comp);
2108 : 335 : uint l_end = comp_next_label(comp);
2109 : 335 : c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2110 : 335 : compile_node(comp, pns->nodes[0]); // success value
2111 : 335 : EMIT_ARG(jump, l_end);
2112 : 335 : EMIT_ARG(label_assign, l_fail);
2113 : 335 : EMIT_ARG(adjust_stack_size, -1); // adjust stack size
2114 : 335 : compile_node(comp, pns_test_if_else->nodes[1]); // failure value
2115 : 335 : EMIT_ARG(label_assign, l_end);
2116 : 335 : }
2117 : :
2118 : 565 : STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
2119 [ + + ]: 565 : if (comp->pass == MP_PASS_SCOPE) {
2120 : : // create a new scope for this lambda
2121 : 147 : scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
2122 : : // store the lambda scope so the compiling function (this one) can use it at each pass
2123 : 147 : pns->nodes[2] = (mp_parse_node_t)s;
2124 : : }
2125 : :
2126 : : // get the scope for this lambda
2127 : 565 : scope_t *this_scope = (scope_t *)pns->nodes[2];
2128 : :
2129 : : // compile the lambda definition
2130 : 565 : compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
2131 : 565 : }
2132 : :
2133 : : #if MICROPY_PY_ASSIGN_EXPR
2134 : 144 : STATIC void compile_namedexpr_helper(compiler_t *comp, mp_parse_node_t pn_name, mp_parse_node_t pn_expr) {
2135 [ + + ]: 144 : if (!MP_PARSE_NODE_IS_ID(pn_name)) {
2136 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pn_name, MP_ERROR_TEXT("can't assign to expression"));
2137 : : }
2138 : 144 : compile_node(comp, pn_expr);
2139 : 144 : EMIT(dup_top);
2140 : :
2141 : 144 : qstr target = MP_PARSE_NODE_LEAF_ARG(pn_name);
2142 : :
2143 : : // When a variable is assigned via := in a comprehension then that variable is bound to
2144 : : // the parent scope. Any global or nonlocal declarations in the parent scope are honoured.
2145 : : // For details see: https://peps.python.org/pep-0572/#scope-of-the-target
2146 [ + + + + ]: 144 : if (comp->pass == MP_PASS_SCOPE && SCOPE_IS_COMP_LIKE(comp->scope_cur->kind)) {
2147 : 34 : id_info_t *id_info_parent = mp_emit_common_get_id_for_modification(comp->scope_cur->parent, target);
2148 [ + + ]: 34 : if (id_info_parent->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
2149 : 4 : scope_find_or_add_id(comp->scope_cur, target, ID_INFO_KIND_GLOBAL_EXPLICIT);
2150 : : } else {
2151 : 30 : id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, target, ID_INFO_KIND_UNDECIDED);
2152 : 30 : bool is_global = comp->scope_cur->parent->parent == NULL; // comprehension is defined in outer scope
2153 [ + + + + ]: 30 : if (!is_global && id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2154 : : // Variable was already referenced but now needs to be closed over, so reset the kind
2155 : : // such that scope_check_to_close_over() is called in compile_declare_nonlocal().
2156 : 2 : id_info->kind = ID_INFO_KIND_UNDECIDED;
2157 : : }
2158 : 30 : compile_declare_global_or_nonlocal(comp, pn_name, id_info, is_global);
2159 : : }
2160 : : }
2161 : :
2162 : : // Do the store to the target variable.
2163 : 144 : compile_store_id(comp, target);
2164 : 144 : }
2165 : :
2166 : 128 : STATIC void compile_namedexpr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2167 : 128 : compile_namedexpr_helper(comp, pns->nodes[0], pns->nodes[1]);
2168 : 128 : }
2169 : : #endif
2170 : :
2171 : 478 : STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2172 : 478 : bool cond = MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test;
2173 : 478 : uint l_end = comp_next_label(comp);
2174 : 478 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2175 [ + + ]: 1434 : for (int i = 0; i < n; i += 1) {
2176 : 956 : compile_node(comp, pns->nodes[i]);
2177 [ + + ]: 956 : if (i + 1 < n) {
2178 : 478 : EMIT_ARG(jump_if_or_pop, cond, l_end);
2179 : : }
2180 : : }
2181 : 478 : EMIT_ARG(label_assign, l_end);
2182 : 478 : }
2183 : :
2184 : 96 : STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
2185 : 96 : compile_node(comp, pns->nodes[0]);
2186 : 96 : EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
2187 : 96 : }
2188 : :
2189 : 15846 : STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
2190 : 15846 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2191 : 15846 : compile_node(comp, pns->nodes[0]);
2192 : 15846 : bool multi = (num_nodes > 3);
2193 : 15846 : uint l_fail = 0;
2194 [ + + ]: 15846 : if (multi) {
2195 : 246 : l_fail = comp_next_label(comp);
2196 : : }
2197 [ + + ]: 31946 : for (int i = 1; i + 1 < num_nodes; i += 2) {
2198 : 16100 : compile_node(comp, pns->nodes[i + 1]);
2199 [ + + ]: 16100 : if (i + 2 < num_nodes) {
2200 : 254 : EMIT(dup_top);
2201 : 254 : EMIT(rot_three);
2202 : : }
2203 [ + + ]: 16100 : if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
2204 : 11060 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]);
2205 : 11060 : mp_binary_op_t op;
2206 [ + + ]: 11060 : if (tok == MP_TOKEN_KW_IN) {
2207 : : op = MP_BINARY_OP_IN;
2208 : : } else {
2209 : 10171 : op = MP_BINARY_OP_LESS + (tok - MP_TOKEN_OP_LESS);
2210 : : }
2211 : 11060 : EMIT_ARG(binary_op, op);
2212 : : } else {
2213 [ + - - + ]: 5040 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
2214 : 5040 : mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)pns->nodes[i];
2215 : 5040 : int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
2216 [ + + ]: 5040 : if (kind == PN_comp_op_not_in) {
2217 : 376 : EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
2218 : : } else {
2219 [ - + ]: 4664 : assert(kind == PN_comp_op_is); // should be
2220 [ + + ]: 4664 : if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
2221 : 3256 : EMIT_ARG(binary_op, MP_BINARY_OP_IS);
2222 : : } else {
2223 : 1408 : EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
2224 : : }
2225 : : }
2226 : : }
2227 [ + + ]: 16100 : if (i + 2 < num_nodes) {
2228 : 254 : EMIT_ARG(jump_if_or_pop, false, l_fail);
2229 : : }
2230 : : }
2231 [ + + ]: 15846 : if (multi) {
2232 : 246 : uint l_end = comp_next_label(comp);
2233 : 246 : EMIT_ARG(jump, l_end);
2234 : 246 : EMIT_ARG(label_assign, l_fail);
2235 : 246 : EMIT_ARG(adjust_stack_size, 1);
2236 : 246 : EMIT(rot_two);
2237 : 246 : EMIT(pop_top);
2238 : 246 : EMIT_ARG(label_assign, l_end);
2239 : : }
2240 : 15846 : }
2241 : :
2242 : 4 : STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2243 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("*x must be assignment target"));
2244 : 4 : }
2245 : :
2246 : 1777 : STATIC void compile_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns) {
2247 : 1777 : MP_STATIC_ASSERT(MP_BINARY_OP_OR + PN_xor_expr - PN_expr == MP_BINARY_OP_XOR);
2248 : 1777 : MP_STATIC_ASSERT(MP_BINARY_OP_OR + PN_and_expr - PN_expr == MP_BINARY_OP_AND);
2249 : 1777 : mp_binary_op_t binary_op = MP_BINARY_OP_OR + MP_PARSE_NODE_STRUCT_KIND(pns) - PN_expr;
2250 : 1777 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2251 : 1777 : compile_node(comp, pns->nodes[0]);
2252 [ + + ]: 3636 : for (int i = 1; i < num_nodes; ++i) {
2253 : 1859 : compile_node(comp, pns->nodes[i]);
2254 : 1859 : EMIT_ARG(binary_op, binary_op);
2255 : : }
2256 : 1777 : }
2257 : :
2258 : 11125 : STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2259 : 11125 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2260 : 11125 : compile_node(comp, pns->nodes[0]);
2261 [ + + ]: 23319 : for (int i = 1; i + 1 < num_nodes; i += 2) {
2262 : 12194 : compile_node(comp, pns->nodes[i + 1]);
2263 : 12194 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]);
2264 : 12194 : mp_binary_op_t op = MP_BINARY_OP_LSHIFT + (tok - MP_TOKEN_OP_DBL_LESS);
2265 : 12194 : EMIT_ARG(binary_op, op);
2266 : : }
2267 : 11125 : }
2268 : :
2269 : 3241 : STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
2270 : 3241 : compile_node(comp, pns->nodes[1]);
2271 : 3241 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2272 : 3241 : mp_unary_op_t op;
2273 [ + + ]: 3241 : if (tok == MP_TOKEN_OP_TILDE) {
2274 : : op = MP_UNARY_OP_INVERT;
2275 : : } else {
2276 [ - + ]: 2672 : assert(tok == MP_TOKEN_OP_PLUS || tok == MP_TOKEN_OP_MINUS);
2277 : : op = MP_UNARY_OP_POSITIVE + (tok - MP_TOKEN_OP_PLUS);
2278 : : }
2279 : 3241 : EMIT_ARG(unary_op, op);
2280 : 3241 : }
2281 : :
2282 : 247723 : STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
2283 : : // compile the subject of the expression
2284 : 247723 : compile_node(comp, pns->nodes[0]);
2285 : :
2286 : : // compile_atom_expr_await may call us with a NULL node
2287 [ + + ]: 247722 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2288 : : return;
2289 : : }
2290 : :
2291 : : // get the array of trailers (known to be an array of PARSE_NODE_STRUCT)
2292 : 247394 : size_t num_trail = 1;
2293 : 247394 : mp_parse_node_struct_t **pns_trail = (mp_parse_node_struct_t **)&pns->nodes[1];
2294 [ + + ]: 247394 : if (MP_PARSE_NODE_STRUCT_KIND(pns_trail[0]) == PN_atom_expr_trailers) {
2295 : 115131 : num_trail = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_trail[0]);
2296 : 115131 : pns_trail = (mp_parse_node_struct_t **)&pns_trail[0]->nodes[0];
2297 : : }
2298 : :
2299 : : // the current index into the array of trailers
2300 : 247394 : size_t i = 0;
2301 : :
2302 : : // handle special super() call
2303 [ + + ]: 247394 : if (comp->scope_cur->kind == SCOPE_FUNCTION
2304 [ + + ]: 51227 : && MP_PARSE_NODE_IS_ID(pns->nodes[0])
2305 [ + + ]: 50769 : && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super
2306 [ + + ]: 154 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[0]) == PN_trailer_paren
2307 [ + + ]: 146 : && MP_PARSE_NODE_IS_NULL(pns_trail[0]->nodes[0])) {
2308 : : // at this point we have matched "super()" within a function
2309 : :
2310 : : // load the class for super to search for a parent
2311 : 138 : compile_load_id(comp, MP_QSTR___class__);
2312 : :
2313 : : // look for first argument to function (assumes it's "self")
2314 : 138 : bool found = false;
2315 : 138 : id_info_t *id = &comp->scope_cur->id_info[0];
2316 [ + + ]: 142 : for (size_t n = comp->scope_cur->id_info_len; n > 0; --n, ++id) {
2317 [ + + ]: 140 : if (id->flags & ID_FLAG_IS_PARAM) {
2318 : : // first argument found; load it
2319 : 136 : compile_load_id(comp, id->qst);
2320 : 136 : found = true;
2321 : 136 : break;
2322 : : }
2323 : : }
2324 : 138 : if (!found) {
2325 : 2 : compile_syntax_error(comp, (mp_parse_node_t)pns_trail[0],
2326 : 2 : MP_ERROR_TEXT("super() can't find self")); // really a TypeError
2327 : 2 : return;
2328 : : }
2329 : :
2330 [ + + ]: 136 : if (num_trail >= 3
2331 [ + - ]: 120 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[1]) == PN_trailer_period
2332 [ + - ]: 240 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[2]) == PN_trailer_paren) {
2333 : : // optimisation for method calls super().f(...), to eliminate heap allocation
2334 : 120 : mp_parse_node_struct_t *pns_period = pns_trail[1];
2335 : 120 : mp_parse_node_struct_t *pns_paren = pns_trail[2];
2336 : 120 : EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0]), true);
2337 : 120 : compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
2338 : 120 : i = 3;
2339 : : } else {
2340 : : // a super() call
2341 : 16 : EMIT_ARG(call_function, 2, 0, 0);
2342 : 16 : i = 1;
2343 : : }
2344 : :
2345 : : #if MICROPY_COMP_CONST_LITERAL && MICROPY_PY_COLLECTIONS_ORDEREDDICT
2346 : : // handle special OrderedDict constructor
2347 [ + + ]: 247256 : } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
2348 [ + + ]: 240999 : && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_OrderedDict
2349 [ + - ]: 56 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[0]) == PN_trailer_paren
2350 [ + + + - : 56 : && MP_PARSE_NODE_IS_STRUCT_KIND(pns_trail[0]->nodes[0], PN_atom_brace)) {
+ + ]
2351 : : // at this point we have matched "OrderedDict({...})"
2352 : :
2353 : 8 : EMIT_ARG(call_function, 0, 0, 0);
2354 : 8 : mp_parse_node_struct_t *pns_dict = (mp_parse_node_struct_t *)pns_trail[0]->nodes[0];
2355 : 8 : compile_atom_brace_helper(comp, pns_dict, false);
2356 : 8 : i = 1;
2357 : : #endif
2358 : : }
2359 : :
2360 : : // compile the remaining trailers
2361 [ + + ]: 648544 : for (; i < num_trail; i++) {
2362 [ + + ]: 401160 : if (i + 1 < num_trail
2363 [ + + ]: 192279 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[i]) == PN_trailer_period
2364 [ + + ]: 83018 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[i + 1]) == PN_trailer_paren) {
2365 : : // optimisation for method calls a.f(...), following PyPy
2366 : 38833 : mp_parse_node_struct_t *pns_period = pns_trail[i];
2367 : 38833 : mp_parse_node_struct_t *pns_paren = pns_trail[i + 1];
2368 : 38833 : EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0]), false);
2369 : 38833 : compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
2370 : 38833 : i += 1;
2371 : : } else {
2372 : : // node is one of: trailer_paren, trailer_bracket, trailer_period
2373 : 362327 : compile_node(comp, (mp_parse_node_t)pns_trail[i]);
2374 : : }
2375 : : }
2376 : : }
2377 : :
2378 : 830 : STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2379 : 830 : compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2380 : 830 : EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
2381 : 830 : }
2382 : :
2383 : 373523 : STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) {
2384 : : // function to call is on top of stack
2385 : :
2386 : : // get the list of arguments
2387 : 373523 : mp_parse_node_t *args;
2388 : 373523 : size_t n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
2389 : :
2390 : : // compile the arguments
2391 : : // Rather than calling compile_node on the list, we go through the list of args
2392 : : // explicitly here so that we can count the number of arguments and give sensible
2393 : : // error messages.
2394 : 373523 : int n_positional = n_positional_extra;
2395 : 373523 : uint n_keyword = 0;
2396 : 373523 : uint star_flags = 0;
2397 : 373523 : mp_uint_t star_args = 0;
2398 [ + + ]: 567452 : for (size_t i = 0; i < n_args; i++) {
2399 [ - + + + ]: 193972 : if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2400 : 75871 : mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t *)args[i];
2401 [ + + ]: 75871 : if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2402 [ + + ]: 1070 : if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2403 : 8 : compile_syntax_error(comp, (mp_parse_node_t)pns_arg, MP_ERROR_TEXT("* arg after **"));
2404 : 46 : return;
2405 : : }
2406 : : #if MICROPY_DYNAMIC_COMPILER
2407 : : if (i >= (size_t)mp_dynamic_compiler.small_int_bits - 1)
2408 : : #else
2409 [ + + ]: 1062 : if (i >= MP_SMALL_INT_BITS - 1)
2410 : : #endif
2411 : : {
2412 : : // If there are not enough bits in a small int to fit the flag, then we consider
2413 : : // it a syntax error. It should be unlikely to have this many args in practice.
2414 : 18 : compile_syntax_error(comp, (mp_parse_node_t)pns_arg, MP_ERROR_TEXT("too many args"));
2415 : 18 : return;
2416 : : }
2417 : 1044 : star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2418 : 1044 : star_args |= (mp_uint_t)1 << i;
2419 : 1044 : compile_node(comp, pns_arg->nodes[0]);
2420 : 1044 : n_positional++;
2421 [ + + ]: 74801 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2422 : 392 : star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2423 : : // double-star args are stored as kw arg with key of None
2424 : 392 : EMIT(load_null);
2425 : 392 : compile_node(comp, pns_arg->nodes[0]);
2426 : 392 : n_keyword++;
2427 [ + + ]: 74409 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2428 : : #if MICROPY_PY_ASSIGN_EXPR
2429 [ + - + + : 3161 : if (MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_argument_3)) {
+ + ]
2430 : 16 : compile_namedexpr_helper(comp, pns_arg->nodes[0], ((mp_parse_node_struct_t *)pns_arg->nodes[1])->nodes[0]);
2431 : 16 : n_positional++;
2432 : : } else
2433 : : #endif
2434 [ + - + + : 3145 : if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
+ + ]
2435 [ + + ]: 3020 : if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2436 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns_arg, MP_ERROR_TEXT("LHS of keyword arg must be an id"));
2437 : 4 : return;
2438 : : }
2439 : 3016 : EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
2440 : 3016 : compile_node(comp, pns_arg->nodes[1]);
2441 : 3016 : n_keyword++;
2442 : : } else {
2443 : 125 : compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2444 : 125 : n_positional++;
2445 : : }
2446 : : } else {
2447 : 71248 : goto normal_argument;
2448 : : }
2449 : : } else {
2450 : 118101 : normal_argument:
2451 [ + + ]: 189349 : if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2452 : 4 : compile_syntax_error(comp, args[i], MP_ERROR_TEXT("positional arg after **"));
2453 : 4 : return;
2454 : : }
2455 [ + + ]: 189345 : if (n_keyword > 0) {
2456 : 4 : compile_syntax_error(comp, args[i], MP_ERROR_TEXT("positional arg after keyword arg"));
2457 : 4 : return;
2458 : : }
2459 : 189341 : compile_node(comp, args[i]);
2460 : 189336 : n_positional++;
2461 : : }
2462 : : }
2463 : :
2464 [ + + ]: 373479 : if (star_flags != 0) {
2465 : : // one extra object that contains the star_args map
2466 : 1216 : EMIT_ARG(load_const_small_int, star_args);
2467 : : }
2468 : :
2469 : : // emit the function/method call
2470 [ + + ]: 373485 : if (is_method_call) {
2471 : 38949 : EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
2472 : : } else {
2473 : 334536 : EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
2474 : : }
2475 : : }
2476 : :
2477 : : // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
2478 : 800 : STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2479 [ - + ]: 800 : assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2480 [ + - + - : 800 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
- + ]
2481 : 800 : mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t *)pns->nodes[1];
2482 : :
2483 [ + + ]: 800 : if (comp->pass == MP_PASS_SCOPE) {
2484 : : // create a new scope for this comprehension
2485 : 211 : scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
2486 : : // store the comprehension scope so the compiling function (this one) can use it at each pass
2487 : 211 : pns_comp_for->nodes[3] = (mp_parse_node_t)s;
2488 : : }
2489 : :
2490 : : // get the scope for this comprehension
2491 : 800 : scope_t *this_scope = (scope_t *)pns_comp_for->nodes[3];
2492 : :
2493 : : // compile the comprehension
2494 : 800 : close_over_variables_etc(comp, this_scope, 0, 0);
2495 : :
2496 : 800 : compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2497 [ + + ]: 800 : if (kind == SCOPE_GEN_EXPR) {
2498 : 280 : EMIT_ARG(get_iter, false);
2499 : : }
2500 : 800 : EMIT_ARG(call_function, 1, 0, 0);
2501 : 800 : }
2502 : :
2503 : 4455 : STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2504 [ + + ]: 4455 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2505 : : // an empty tuple
2506 : 772 : EMIT_ARG(build, 0, MP_EMIT_BUILD_TUPLE);
2507 : : } else {
2508 [ + - - + ]: 3683 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
2509 : 3683 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
2510 [ + + + - : 3683 : if (MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns)) {
+ + + + ]
2511 : : // generator expression
2512 : 155 : compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2513 : : } else {
2514 : : // tuple with N items
2515 : 3528 : compile_generic_tuple(comp, pns);
2516 : : }
2517 : : }
2518 : 4455 : }
2519 : :
2520 : 5946 : STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2521 [ + + ]: 5946 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2522 : : // empty list
2523 : 1199 : EMIT_ARG(build, 0, MP_EMIT_BUILD_LIST);
2524 [ + + + + ]: 4747 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2525 : 3498 : mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)pns->nodes[0];
2526 [ + + + - : 3498 : if (MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns2)) {
+ + + + ]
2527 : : // list comprehension
2528 : 440 : compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2529 : : } else {
2530 : : // list with N items
2531 : 3058 : compile_generic_all_nodes(comp, pns2);
2532 : 3058 : EMIT_ARG(build, MP_PARSE_NODE_STRUCT_NUM_NODES(pns2), MP_EMIT_BUILD_LIST);
2533 : : }
2534 : : } else {
2535 : : // list with 1 item
2536 : 1249 : compile_node(comp, pns->nodes[0]);
2537 : 1249 : EMIT_ARG(build, 1, MP_EMIT_BUILD_LIST);
2538 : : }
2539 : 5946 : }
2540 : :
2541 : 3411 : STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool create_map) {
2542 : 3411 : mp_parse_node_t pn = pns->nodes[0];
2543 [ + + ]: 3411 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2544 : : // empty dict
2545 [ + - ]: 834 : if (create_map) {
2546 : 834 : EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
2547 : : }
2548 [ + + ]: 2577 : } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2549 : 2315 : pns = (mp_parse_node_struct_t *)pn;
2550 [ + + ]: 2315 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
2551 : : // dict with one element
2552 [ + - ]: 962 : if (create_map) {
2553 : 962 : EMIT_ARG(build, 1, MP_EMIT_BUILD_MAP);
2554 : : }
2555 : 962 : compile_node(comp, pn);
2556 : 962 : EMIT(store_map);
2557 [ + + ]: 1353 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2558 [ + - - + ]: 1345 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2559 : 1345 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
2560 [ + + ]: 1345 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
2561 : : // dict/set with multiple elements
2562 : :
2563 : : // get tail elements (2nd, 3rd, ...)
2564 : 1265 : mp_parse_node_t *nodes;
2565 : 1265 : size_t n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2566 : :
2567 : : // first element sets whether it's a dict or set
2568 : 1265 : bool is_dict;
2569 [ + - + + : 1265 : if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
+ - ]
2570 : : // a dictionary
2571 [ + + ]: 933 : if (create_map) {
2572 : 925 : EMIT_ARG(build, 1 + n, MP_EMIT_BUILD_MAP);
2573 : : }
2574 : 933 : compile_node(comp, pns->nodes[0]);
2575 : 933 : EMIT(store_map);
2576 : 933 : is_dict = true;
2577 : : } else {
2578 : : // a set
2579 : 332 : compile_node(comp, pns->nodes[0]); // 1st value of set
2580 : 332 : is_dict = false;
2581 : : }
2582 : :
2583 : : // process rest of elements
2584 [ + + ]: 5198 : for (size_t i = 0; i < n; i++) {
2585 : 3941 : mp_parse_node_t pn_i = nodes[i];
2586 [ + - + + : 3941 : bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
+ + ]
2587 : 3941 : compile_node(comp, pn_i);
2588 [ + + ]: 3941 : if (is_dict) {
2589 [ + + ]: 3337 : if (!is_key_value) {
2590 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
2591 : : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("invalid syntax"));
2592 : : #else
2593 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("expecting key:value for dict"));
2594 : : #endif
2595 : 12 : return;
2596 : : }
2597 : 3333 : EMIT(store_map);
2598 : : } else {
2599 [ + + ]: 604 : if (is_key_value) {
2600 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
2601 : : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("invalid syntax"));
2602 : : #else
2603 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("expecting just a value for set"));
2604 : : #endif
2605 : 4 : return;
2606 : : }
2607 : : }
2608 : : }
2609 : :
2610 : : #if MICROPY_PY_BUILTINS_SET
2611 : : // if it's a set, build it
2612 [ + + ]: 1257 : if (!is_dict) {
2613 : 328 : EMIT_ARG(build, 1 + n, MP_EMIT_BUILD_SET);
2614 : : }
2615 : : #endif
2616 : : } else {
2617 [ - + ]: 80 : assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
2618 : : // dict/set comprehension
2619 [ + - + + : 80 : if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
+ - ]
2620 : : // a dictionary comprehension
2621 : 72 : compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2622 : : } else {
2623 : : // a set comprehension
2624 : 8 : compile_comprehension(comp, pns, SCOPE_SET_COMP);
2625 : : }
2626 : : }
2627 : : } else {
2628 : : // set with one element
2629 : 8 : goto set_with_one_element;
2630 : : }
2631 : : } else {
2632 : : // set with one element
2633 : 262 : set_with_one_element:
2634 : : #if MICROPY_PY_BUILTINS_SET
2635 : 270 : compile_node(comp, pn);
2636 : 270 : EMIT_ARG(build, 1, MP_EMIT_BUILD_SET);
2637 : : #else
2638 : : assert(0);
2639 : : #endif
2640 : : }
2641 : : }
2642 : :
2643 : 3403 : STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2644 : 3403 : compile_atom_brace_helper(comp, pns, true);
2645 : 3403 : }
2646 : :
2647 : 330803 : STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2648 : 330803 : compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
2649 : 330797 : }
2650 : :
2651 : 9825 : STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2652 : : // object who's index we want is on top of stack
2653 : 9825 : compile_node(comp, pns->nodes[0]); // the index
2654 : 9825 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
2655 : 9825 : }
2656 : :
2657 : 23257 : STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
2658 : : // object who's attribute we want is on top of stack
2659 : 23257 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), MP_EMIT_ATTR_LOAD); // attribute to get
2660 : 23257 : }
2661 : :
2662 : : #if MICROPY_PY_BUILTINS_SLICE
2663 : 2402 : STATIC void compile_subscript(compiler_t *comp, mp_parse_node_struct_t *pns) {
2664 [ + + ]: 2402 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_2) {
2665 : 1456 : compile_node(comp, pns->nodes[0]); // start of slice
2666 [ + - - + ]: 1456 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2667 : 1456 : pns = (mp_parse_node_struct_t *)pns->nodes[1];
2668 : : } else {
2669 : : // pns is a PN_subscript_3, load None for start of slice
2670 : 946 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2671 : : }
2672 : :
2673 [ - + ]: 2402 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2674 : 2402 : mp_parse_node_t pn = pns->nodes[0];
2675 [ + + ]: 2402 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2676 : : // [?:]
2677 : 489 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2678 : 489 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2679 [ + + ]: 1913 : } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2680 : 623 : pns = (mp_parse_node_struct_t *)pn;
2681 [ + + ]: 623 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2682 : 176 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2683 : 176 : pn = pns->nodes[0];
2684 [ + + ]: 176 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2685 : : // [?::]
2686 : 32 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2687 : : } else {
2688 : : // [?::x]
2689 : 144 : compile_node(comp, pn);
2690 : 144 : EMIT_ARG(build, 3, MP_EMIT_BUILD_SLICE);
2691 : : }
2692 [ + + ]: 447 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
2693 : 342 : compile_node(comp, pns->nodes[0]);
2694 [ + - - + ]: 342 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2695 : 342 : pns = (mp_parse_node_struct_t *)pns->nodes[1];
2696 [ - + ]: 342 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2697 [ + + ]: 342 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2698 : : // [?:x:]
2699 : 24 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2700 : : } else {
2701 : : // [?:x:x]
2702 : 318 : compile_node(comp, pns->nodes[0]);
2703 : 318 : EMIT_ARG(build, 3, MP_EMIT_BUILD_SLICE);
2704 : : }
2705 : : } else {
2706 : : // [?:x]
2707 : 105 : compile_node(comp, pn);
2708 : 105 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2709 : : }
2710 : : } else {
2711 : : // [?:x]
2712 : 1290 : compile_node(comp, pn);
2713 : 1290 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2714 : : }
2715 : 2402 : }
2716 : : #endif // MICROPY_PY_BUILTINS_SLICE
2717 : :
2718 : 5304 : STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
2719 : : // if this is called then we are compiling a dict key:value pair
2720 : 5304 : compile_node(comp, pns->nodes[1]); // value
2721 : 5304 : compile_node(comp, pns->nodes[0]); // key
2722 : 5304 : }
2723 : :
2724 : 3757 : STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
2725 : 3757 : qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
2726 : : // store class object into class name
2727 : 3757 : compile_store_id(comp, cname);
2728 : 3757 : }
2729 : :
2730 : 1292 : STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2731 [ + + ]: 1292 : if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2732 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'yield' outside function"));
2733 : 4 : return;
2734 : : }
2735 [ + + ]: 1288 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2736 : 140 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2737 : 140 : EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
2738 : 136 : reserve_labels_for_native(comp, 1);
2739 [ + + + + ]: 1148 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2740 : 268 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
2741 : 268 : compile_node(comp, pns->nodes[0]);
2742 : 268 : compile_yield_from(comp);
2743 : : } else {
2744 : 880 : compile_node(comp, pns->nodes[0]);
2745 : 880 : EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
2746 : 880 : reserve_labels_for_native(comp, 1);
2747 : : }
2748 : : }
2749 : :
2750 : : #if MICROPY_PY_ASYNC_AWAIT
2751 : 1400 : STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2752 [ + + ]: 1400 : if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2753 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'await' outside function"));
2754 : 4 : return;
2755 : : }
2756 : 1396 : compile_atom_expr_normal(comp, pns);
2757 : 1396 : compile_yield_from(comp);
2758 : : }
2759 : : #endif
2760 : :
2761 : 29657 : STATIC mp_obj_t get_const_object(mp_parse_node_struct_t *pns) {
2762 : 29657 : return mp_parse_node_extract_const_object(pns);
2763 : : }
2764 : :
2765 : 29657 : STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2766 : 29657 : EMIT_ARG(load_const_obj, get_const_object(pns));
2767 : 29657 : }
2768 : :
2769 : : typedef void (*compile_function_t)(compiler_t *, mp_parse_node_struct_t *);
2770 : : STATIC const compile_function_t compile_function[] = {
2771 : : // only define rules with a compile function
2772 : : #define c(f) compile_##f
2773 : : #define DEF_RULE(rule, comp, kind, ...) comp,
2774 : : #define DEF_RULE_NC(rule, kind, ...)
2775 : : #include "py/grammar.h"
2776 : : #undef c
2777 : : #undef DEF_RULE
2778 : : #undef DEF_RULE_NC
2779 : : compile_const_object,
2780 : : };
2781 : :
2782 : 1688180 : STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2783 [ + + ]: 1688180 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2784 : : // pass
2785 [ + + ]: 1629917 : } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2786 : 89085 : mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2787 : 89085 : EMIT_ARG(load_const_small_int, arg);
2788 [ + + ]: 1540832 : } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
2789 : 454086 : uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
2790 [ + + + ]: 454086 : switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
2791 : 381746 : case MP_PARSE_NODE_ID:
2792 : 381746 : compile_load_id(comp, arg);
2793 : 381746 : break;
2794 : 51473 : case MP_PARSE_NODE_STRING:
2795 : 51473 : EMIT_ARG(load_const_str, arg);
2796 : 51473 : break;
2797 : 20867 : case MP_PARSE_NODE_TOKEN:
2798 : : default:
2799 [ + + ]: 20867 : if (arg == MP_TOKEN_NEWLINE) {
2800 : : // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
2801 : : // or when single_input lets through a NEWLINE (user enters a blank line)
2802 : : // do nothing
2803 : : } else {
2804 : 14526 : EMIT_ARG(load_const_tok, arg);
2805 : : }
2806 : : break;
2807 : : }
2808 : : } else {
2809 : 1086746 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
2810 : 1086746 : EMIT_ARG(set_source_line, pns->source_line);
2811 [ - + ]: 1086746 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) <= PN_const_object);
2812 : 1086746 : compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2813 : 1086746 : f(comp, pns);
2814 : : }
2815 : 1688130 : }
2816 : :
2817 : : #if MICROPY_EMIT_NATIVE
2818 : 650 : STATIC int compile_viper_type_annotation(compiler_t *comp, mp_parse_node_t pn_annotation) {
2819 : 650 : int native_type = MP_NATIVE_TYPE_OBJ;
2820 [ + + ]: 650 : if (MP_PARSE_NODE_IS_NULL(pn_annotation)) {
2821 : : // No annotation, type defaults to object
2822 [ + + ]: 406 : } else if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2823 : 398 : qstr type_name = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2824 : 398 : native_type = mp_native_type_from_qstr(type_name);
2825 [ + + ]: 398 : if (native_type < 0) {
2826 : 4 : comp->compile_error = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, MP_ERROR_TEXT("unknown type '%q'"), type_name);
2827 : 4 : native_type = 0;
2828 : : }
2829 : : } else {
2830 : 8 : compile_syntax_error(comp, pn_annotation, MP_ERROR_TEXT("annotation must be an identifier"));
2831 : : }
2832 : 650 : return native_type;
2833 : : }
2834 : : #endif
2835 : :
2836 : 5652 : STATIC void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star) {
2837 : 5652 : (void)pn_dbl_star;
2838 : :
2839 : : // check that **kw is last
2840 [ + + ]: 5652 : if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2841 : 8 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("invalid syntax"));
2842 : 8 : return;
2843 : : }
2844 : :
2845 : 5644 : qstr param_name = MP_QSTRnull;
2846 : 5644 : uint param_flag = ID_FLAG_IS_PARAM;
2847 : 5644 : mp_parse_node_struct_t *pns = NULL;
2848 [ + + ]: 5644 : if (MP_PARSE_NODE_IS_ID(pn)) {
2849 : 4760 : param_name = MP_PARSE_NODE_LEAF_ARG(pn);
2850 [ + + ]: 4760 : if (comp->have_star) {
2851 : : // comes after a star, so counts as a keyword-only parameter
2852 : 28 : comp->scope_cur->num_kwonly_args += 1;
2853 : : } else {
2854 : : // comes before a star, so counts as a positional parameter
2855 : 4732 : comp->scope_cur->num_pos_args += 1;
2856 : : }
2857 : : } else {
2858 [ + - - + ]: 884 : assert(MP_PARSE_NODE_IS_STRUCT(pn));
2859 : 884 : pns = (mp_parse_node_struct_t *)pn;
2860 [ + + ]: 884 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2861 : : // named parameter with possible annotation
2862 : 697 : param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2863 [ + + ]: 697 : if (comp->have_star) {
2864 : : // comes after a star, so counts as a keyword-only parameter
2865 : 29 : comp->scope_cur->num_kwonly_args += 1;
2866 : : } else {
2867 : : // comes before a star, so counts as a positional parameter
2868 : 668 : comp->scope_cur->num_pos_args += 1;
2869 : : }
2870 [ + + ]: 187 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2871 [ + + ]: 141 : if (comp->have_star) {
2872 : : // more than one star
2873 : 8 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("invalid syntax"));
2874 : 8 : return;
2875 : : }
2876 : 133 : comp->have_star = true;
2877 : 133 : param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
2878 [ + + ]: 133 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2879 : : // bare star
2880 : : // TODO see http://www.python.org/dev/peps/pep-3102/
2881 : : // assert(comp->scope_cur->num_dict_params == 0);
2882 : : pns = NULL;
2883 [ + + ]: 109 : } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
2884 : : // named star
2885 : 20 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
2886 : 20 : param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2887 : 20 : pns = NULL;
2888 : : } else {
2889 [ + - - + ]: 89 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
2890 : : // named star with possible annotation
2891 : 89 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
2892 : 89 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
2893 : 89 : param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2894 : : }
2895 : : } else {
2896 : : // double star with possible annotation
2897 [ - + ]: 46 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
2898 : 46 : param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2899 : 46 : param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
2900 : 46 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
2901 : : }
2902 : : }
2903 : :
2904 [ + + ]: 5636 : if (param_name != MP_QSTRnull) {
2905 : 5612 : id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, ID_INFO_KIND_UNDECIDED);
2906 [ + + ]: 5612 : if (id_info->kind != ID_INFO_KIND_UNDECIDED) {
2907 : 4 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("argument name reused"));
2908 : 4 : return;
2909 : : }
2910 : 5608 : id_info->kind = ID_INFO_KIND_LOCAL;
2911 : 5608 : id_info->flags = param_flag;
2912 : :
2913 : : #if MICROPY_EMIT_NATIVE
2914 [ + + + + ]: 5608 : if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER && pn_name == PN_typedargslist_name && pns != NULL) {
2915 : 338 : id_info->flags |= compile_viper_type_annotation(comp, pns->nodes[1]) << ID_FLAG_VIPER_TYPE_POS;
2916 : : }
2917 : : #else
2918 : : (void)pns;
2919 : : #endif
2920 : : }
2921 : : }
2922 : :
2923 : 5505 : STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
2924 : 5505 : compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
2925 : 5505 : }
2926 : :
2927 : 147 : STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
2928 : 147 : compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2929 : 147 : }
2930 : :
2931 : 804 : STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_struct_t *pns_comp_for, mp_parse_node_t pn_inner_expr, int for_depth) {
2932 : 804 : uint l_top = comp_next_label(comp);
2933 : 804 : uint l_end = comp_next_label(comp);
2934 : 804 : EMIT_ARG(label_assign, l_top);
2935 : 804 : EMIT_ARG(for_iter, l_end);
2936 : 804 : c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2937 : 804 : mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2938 : :
2939 : 860 : tail_recursion:
2940 [ + + ]: 860 : if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
2941 : : // no more nested if/for; compile inner expression
2942 : 796 : compile_node(comp, pn_inner_expr);
2943 [ + + ]: 796 : if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
2944 : 276 : EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
2945 : 276 : reserve_labels_for_native(comp, 1);
2946 : 276 : EMIT(pop_top);
2947 : : } else {
2948 : 520 : EMIT_ARG(store_comp, comp->scope_cur->kind, 4 * for_depth + 5);
2949 : : }
2950 [ + + ]: 64 : } else if (MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn_iter) == PN_comp_if) {
2951 : : // if condition
2952 : 56 : mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t *)pn_iter;
2953 : 56 : c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2954 : 56 : pn_iter = pns_comp_if->nodes[1];
2955 : 56 : goto tail_recursion;
2956 : : } else {
2957 [ - + ]: 8 : assert(MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn_iter) == PN_comp_for); // should be
2958 : : // for loop
2959 : 8 : mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t *)pn_iter;
2960 : 8 : compile_node(comp, pns_comp_for2->nodes[1]);
2961 : 8 : EMIT_ARG(get_iter, true);
2962 : 8 : compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
2963 : : }
2964 : :
2965 : 804 : EMIT_ARG(jump, l_top);
2966 : 804 : EMIT_ARG(label_assign, l_end);
2967 : 804 : EMIT(for_iter_end);
2968 : 804 : }
2969 : :
2970 : : STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
2971 : : #if MICROPY_ENABLE_DOC_STRING
2972 : : // see http://www.python.org/dev/peps/pep-0257/
2973 : :
2974 : : // look for the first statement
2975 : : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2976 : : // a statement; fall through
2977 : : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
2978 : : // file input; find the first non-newline node
2979 : : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
2980 : : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2981 : : for (int i = 0; i < num_nodes; i++) {
2982 : : pn = pns->nodes[i];
2983 : : if (!(MP_PARSE_NODE_IS_LEAF(pn) && MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN && MP_PARSE_NODE_LEAF_ARG(pn) == MP_TOKEN_NEWLINE)) {
2984 : : // not a newline, so this is the first statement; finish search
2985 : : break;
2986 : : }
2987 : : }
2988 : : // if we didn't find a non-newline then it's okay to fall through; pn will be a newline and so doc-string test below will fail gracefully
2989 : : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
2990 : : // a list of statements; get the first one
2991 : : pn = ((mp_parse_node_struct_t *)pn)->nodes[0];
2992 : : } else {
2993 : : return;
2994 : : }
2995 : :
2996 : : // check the first statement for a doc string
2997 : : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2998 : : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
2999 : : if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3000 : : && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3001 : : || (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)
3002 : : && mp_obj_is_str(get_const_object((mp_parse_node_struct_t *)pns->nodes[0])))) {
3003 : : // compile the doc string
3004 : : compile_node(comp, pns->nodes[0]);
3005 : : // store the doc string
3006 : : compile_store_id(comp, MP_QSTR___doc__);
3007 : : }
3008 : : }
3009 : : #else
3010 : : (void)comp;
3011 : : (void)pn;
3012 : : #endif
3013 : : }
3014 : :
3015 : 36360 : STATIC bool compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3016 : 36360 : comp->pass = pass;
3017 : 36360 : comp->scope_cur = scope;
3018 : 36360 : comp->next_label = 0;
3019 : 36360 : mp_emit_common_start_pass(&comp->emit_common, pass);
3020 : 36360 : EMIT_ARG(start_pass, pass, scope);
3021 : 36360 : reserve_labels_for_native(comp, 6); // used by native's start_pass
3022 : :
3023 [ + + ]: 36360 : if (comp->pass == MP_PASS_SCOPE) {
3024 : : // reset maximum stack sizes in scope
3025 : : // they will be computed in this first pass
3026 : 9345 : scope->stack_size = 0;
3027 : 9345 : scope->exc_stack_size = 0;
3028 : : }
3029 : :
3030 : : // compile
3031 [ + - + + : 37594 : if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
+ + ]
3032 [ - + ]: 1234 : assert(scope->kind == SCOPE_MODULE);
3033 : 1234 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3034 : 1234 : compile_node(comp, pns->nodes[0]); // compile the expression
3035 : 1234 : EMIT(return_value);
3036 [ + + + + : 35126 : } else if (scope->kind == SCOPE_MODULE) {
- + ]
3037 : 12769 : if (!comp->is_repl) {
3038 : 12769 : check_for_doc_string(comp, scope->pn);
3039 : : }
3040 : 12769 : compile_node(comp, scope->pn);
3041 : 12763 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3042 : 12764 : EMIT(return_value);
3043 : : } else if (scope->kind == SCOPE_FUNCTION) {
3044 [ + - - + ]: 17243 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3045 : 17243 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3046 [ - + ]: 17243 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
3047 : :
3048 : : // work out number of parameters, keywords and default parameters, and add them to the id_info array
3049 : : // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
3050 [ + + ]: 17243 : if (comp->pass == MP_PASS_SCOPE) {
3051 : 4386 : comp->have_star = false;
3052 : 4386 : apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3053 : :
3054 : : #if MICROPY_EMIT_NATIVE
3055 [ + + ]: 4386 : if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3056 : : // Compile return type; pns->nodes[2] is return/whole function annotation
3057 : 312 : scope->scope_flags |= compile_viper_type_annotation(comp, pns->nodes[2]) << MP_SCOPE_FLAG_VIPERRET_POS;
3058 : : }
3059 : : #endif // MICROPY_EMIT_NATIVE
3060 : : }
3061 : :
3062 : 17243 : compile_node(comp, pns->nodes[3]); // 3 is function body
3063 : 17227 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3064 : 17227 : EMIT(return_value);
3065 : : } else if (scope->kind == SCOPE_LAMBDA) {
3066 [ + - - + ]: 564 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3067 : 564 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3068 [ - + ]: 564 : assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
3069 : :
3070 : : // Set the source line number for the start of the lambda
3071 : 564 : EMIT_ARG(set_source_line, pns->source_line);
3072 : :
3073 : : // work out number of parameters, keywords and default parameters, and add them to the id_info array
3074 : : // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
3075 [ + + ]: 564 : if (comp->pass == MP_PASS_SCOPE) {
3076 : 147 : comp->have_star = false;
3077 : 147 : apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3078 : : }
3079 : :
3080 : 564 : compile_node(comp, pns->nodes[1]); // 1 is lambda body
3081 : :
3082 : : // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3083 [ + + ]: 564 : if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3084 : 8 : EMIT(pop_top);
3085 : 8 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3086 : : }
3087 : 564 : EMIT(return_value);
3088 : : } else if (SCOPE_IS_COMP_LIKE(scope->kind)) {
3089 : : // a bit of a hack at the moment
3090 : :
3091 [ + - - + ]: 796 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3092 : 796 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3093 [ - + ]: 796 : assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3094 [ + - + - : 796 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
- + ]
3095 : 796 : mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t *)pns->nodes[1];
3096 : :
3097 : : // We need a unique name for the comprehension argument (the iterator).
3098 : : // CPython uses .0, but we should be able to use anything that won't
3099 : : // clash with a user defined variable. Best to use an existing qstr,
3100 : : // so we use the blank qstr.
3101 : 796 : qstr qstr_arg = MP_QSTR_;
3102 [ + + ]: 796 : if (comp->pass == MP_PASS_SCOPE) {
3103 : 211 : scope_find_or_add_id(comp->scope_cur, qstr_arg, ID_INFO_KIND_LOCAL);
3104 : 211 : scope->num_pos_args = 1;
3105 : : }
3106 : :
3107 : : // Set the source line number for the start of the comprehension
3108 : 796 : EMIT_ARG(set_source_line, pns->source_line);
3109 : :
3110 [ + + ]: 796 : if (scope->kind == SCOPE_LIST_COMP) {
3111 : 440 : EMIT_ARG(build, 0, MP_EMIT_BUILD_LIST);
3112 [ + + ]: 356 : } else if (scope->kind == SCOPE_DICT_COMP) {
3113 : 72 : EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
3114 : : #if MICROPY_PY_BUILTINS_SET
3115 [ + + ]: 284 : } else if (scope->kind == SCOPE_SET_COMP) {
3116 : 8 : EMIT_ARG(build, 0, MP_EMIT_BUILD_SET);
3117 : : #endif
3118 : : }
3119 : :
3120 : : // There are 4 slots on the stack for the iterator, and the first one is
3121 : : // NULL to indicate that the second one points to the iterator object.
3122 [ + + ]: 796 : if (scope->kind == SCOPE_GEN_EXPR) {
3123 : 276 : MP_STATIC_ASSERT(MP_OBJ_ITER_BUF_NSLOTS == 4);
3124 : 276 : EMIT(load_null);
3125 : 276 : compile_load_id(comp, qstr_arg);
3126 : 276 : EMIT(load_null);
3127 : 276 : EMIT(load_null);
3128 : : } else {
3129 : 520 : compile_load_id(comp, qstr_arg);
3130 : 520 : EMIT_ARG(get_iter, true);
3131 : : }
3132 : :
3133 : 796 : compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
3134 : :
3135 [ + + ]: 796 : if (scope->kind == SCOPE_GEN_EXPR) {
3136 : 276 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3137 : : }
3138 : 796 : EMIT(return_value);
3139 : : } else {
3140 : 0 : assert(scope->kind == SCOPE_CLASS);
3141 [ + - - + ]: 3754 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3142 : 3754 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3143 [ - + ]: 3754 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
3144 : :
3145 [ + + ]: 3754 : if (comp->pass == MP_PASS_SCOPE) {
3146 : 940 : scope_find_or_add_id(scope, MP_QSTR___class__, ID_INFO_KIND_LOCAL);
3147 : : }
3148 : :
3149 : : #if MICROPY_PY_SYS_SETTRACE
3150 : : EMIT_ARG(set_source_line, pns->source_line);
3151 : : #endif
3152 : 3754 : compile_load_id(comp, MP_QSTR___name__);
3153 : 3754 : compile_store_id(comp, MP_QSTR___module__);
3154 : 3754 : EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3155 : 3754 : compile_store_id(comp, MP_QSTR___qualname__);
3156 : :
3157 : 3754 : check_for_doc_string(comp, pns->nodes[2]);
3158 : 3754 : compile_node(comp, pns->nodes[2]); // 2 is class body
3159 : :
3160 : 3754 : id_info_t *id = scope_find(scope, MP_QSTR___class__);
3161 [ - + ]: 3754 : assert(id != NULL);
3162 [ + + ]: 3754 : if (id->kind == ID_INFO_KIND_LOCAL) {
3163 : 3670 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3164 : : } else {
3165 : 84 : EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
3166 : : }
3167 : 3754 : EMIT(return_value);
3168 : : }
3169 : :
3170 : 36340 : bool pass_complete = EMIT(end_pass);
3171 : :
3172 : : // make sure we match all the exception levels
3173 [ - + ]: 36338 : assert(comp->cur_except_level == 0);
3174 : :
3175 : 36338 : return pass_complete;
3176 : : }
3177 : :
3178 : : #if MICROPY_EMIT_INLINE_ASM
3179 : : // requires 3 passes: SCOPE, CODE_SIZE, EMIT
3180 : : STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3181 : : comp->pass = pass;
3182 : : comp->scope_cur = scope;
3183 : : comp->next_label = 0;
3184 : :
3185 : : if (scope->kind != SCOPE_FUNCTION) {
3186 : : compile_syntax_error(comp, MP_PARSE_NODE_NULL, MP_ERROR_TEXT("inline assembler must be a function"));
3187 : : return;
3188 : : }
3189 : :
3190 : : if (comp->pass > MP_PASS_SCOPE) {
3191 : : EMIT_INLINE_ASM_ARG(start_pass, comp->pass, &comp->compile_error);
3192 : : }
3193 : :
3194 : : // get the function definition parse node
3195 : : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3196 : : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3197 : : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
3198 : :
3199 : : // qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
3200 : :
3201 : : // parameters are in pns->nodes[1]
3202 : : if (comp->pass == MP_PASS_CODE_SIZE) {
3203 : : mp_parse_node_t *pn_params;
3204 : : size_t n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
3205 : : scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
3206 : : if (comp->compile_error != MP_OBJ_NULL) {
3207 : : goto inline_asm_error;
3208 : : }
3209 : : }
3210 : :
3211 : : // pns->nodes[2] is function return annotation
3212 : : mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3213 : : mp_parse_node_t pn_annotation = pns->nodes[2];
3214 : : if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3215 : : // nodes[2] can be null or a test-expr
3216 : : if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3217 : : qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3218 : : switch (ret_type) {
3219 : : case MP_QSTR_object:
3220 : : type_sig = MP_NATIVE_TYPE_OBJ;
3221 : : break;
3222 : : case MP_QSTR_bool:
3223 : : type_sig = MP_NATIVE_TYPE_BOOL;
3224 : : break;
3225 : : case MP_QSTR_int:
3226 : : type_sig = MP_NATIVE_TYPE_INT;
3227 : : break;
3228 : : case MP_QSTR_uint:
3229 : : type_sig = MP_NATIVE_TYPE_UINT;
3230 : : break;
3231 : : default:
3232 : : compile_syntax_error(comp, pn_annotation, MP_ERROR_TEXT("unknown type"));
3233 : : return;
3234 : : }
3235 : : } else {
3236 : : compile_syntax_error(comp, pn_annotation, MP_ERROR_TEXT("return annotation must be an identifier"));
3237 : : }
3238 : : }
3239 : :
3240 : : mp_parse_node_t pn_body = pns->nodes[3]; // body
3241 : : mp_parse_node_t *nodes;
3242 : : size_t num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
3243 : :
3244 : : for (size_t i = 0; i < num; i++) {
3245 : : assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3246 : : mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)nodes[i];
3247 : : if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3248 : : // no instructions
3249 : : continue;
3250 : : } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
3251 : : // not an instruction; error
3252 : : not_an_instruction:
3253 : : compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("expecting an assembler instruction"));
3254 : : return;
3255 : : }
3256 : :
3257 : : // check structure of parse node
3258 : : assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3259 : : if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3260 : : goto not_an_instruction;
3261 : : }
3262 : : pns2 = (mp_parse_node_struct_t *)pns2->nodes[0];
3263 : : if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
3264 : : goto not_an_instruction;
3265 : : }
3266 : : if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3267 : : goto not_an_instruction;
3268 : : }
3269 : : if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3270 : : goto not_an_instruction;
3271 : : }
3272 : :
3273 : : // parse node looks like an instruction
3274 : : // get instruction name and args
3275 : : qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3276 : : pns2 = (mp_parse_node_struct_t *)pns2->nodes[1]; // PN_trailer_paren
3277 : : mp_parse_node_t *pn_arg;
3278 : : size_t n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
3279 : :
3280 : : // emit instructions
3281 : : if (op == MP_QSTR_label) {
3282 : : if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
3283 : : compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("'label' requires 1 argument"));
3284 : : return;
3285 : : }
3286 : : uint lab = comp_next_label(comp);
3287 : : if (pass > MP_PASS_SCOPE) {
3288 : : if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3289 : : compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("label redefined"));
3290 : : return;
3291 : : }
3292 : : }
3293 : : } else if (op == MP_QSTR_align) {
3294 : : if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3295 : : compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("'align' requires 1 argument"));
3296 : : return;
3297 : : }
3298 : : if (pass > MP_PASS_SCOPE) {
3299 : : mp_asm_base_align((mp_asm_base_t *)comp->emit_inline_asm,
3300 : : MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3301 : : }
3302 : : } else if (op == MP_QSTR_data) {
3303 : : if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3304 : : compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("'data' requires at least 2 arguments"));
3305 : : return;
3306 : : }
3307 : : if (pass > MP_PASS_SCOPE) {
3308 : : mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
3309 : : for (uint j = 1; j < n_args; j++) {
3310 : : mp_obj_t int_obj;
3311 : : if (!mp_parse_node_get_int_maybe(pn_arg[j], &int_obj)) {
3312 : : compile_syntax_error(comp, nodes[i], MP_ERROR_TEXT("'data' requires integer arguments"));
3313 : : return;
3314 : : }
3315 : : mp_asm_base_data((mp_asm_base_t *)comp->emit_inline_asm,
3316 : : bytesize, mp_obj_int_get_truncated(int_obj));
3317 : : }
3318 : : }
3319 : : } else {
3320 : : if (pass > MP_PASS_SCOPE) {
3321 : : EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
3322 : : }
3323 : : }
3324 : :
3325 : : if (comp->compile_error != MP_OBJ_NULL) {
3326 : : pns = pns2; // this is the parse node that had the error
3327 : : goto inline_asm_error;
3328 : : }
3329 : : }
3330 : :
3331 : : if (comp->pass > MP_PASS_SCOPE) {
3332 : : EMIT_INLINE_ASM_ARG(end_pass, type_sig);
3333 : :
3334 : : if (comp->pass == MP_PASS_EMIT) {
3335 : : void *f = mp_asm_base_get_code((mp_asm_base_t *)comp->emit_inline_asm);
3336 : : mp_emit_glue_assign_native(comp->scope_cur->raw_code, MP_CODE_NATIVE_ASM,
3337 : : f, mp_asm_base_get_code_size((mp_asm_base_t *)comp->emit_inline_asm),
3338 : : NULL,
3339 : : #if MICROPY_PERSISTENT_CODE_SAVE
3340 : : 0,
3341 : : 0,
3342 : : #endif
3343 : : 0, comp->scope_cur->num_pos_args, type_sig);
3344 : : }
3345 : : }
3346 : :
3347 : : if (comp->compile_error != MP_OBJ_NULL) {
3348 : : // inline assembler had an error; set line for its exception
3349 : : inline_asm_error:
3350 : : comp->compile_error_line = pns->source_line;
3351 : : }
3352 : : }
3353 : : #endif
3354 : :
3355 : 9026 : STATIC void scope_compute_things(scope_t *scope) {
3356 : : // in MicroPython we put the *x parameter after all other parameters (except **y)
3357 [ + + ]: 9026 : if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3358 : 93 : id_info_t *id_param = NULL;
3359 [ + - ]: 377 : for (int i = scope->id_info_len - 1; i >= 0; i--) {
3360 : 377 : id_info_t *id = &scope->id_info[i];
3361 [ + + ]: 377 : if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3362 [ + + ]: 93 : if (id_param != NULL) {
3363 : : // swap star param with last param
3364 : 15 : id_info_t temp = *id_param;
3365 : 15 : *id_param = *id;
3366 : 15 : *id = temp;
3367 : : }
3368 : : break;
3369 [ + + + + ]: 284 : } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3370 : 15 : id_param = id;
3371 : : }
3372 : : }
3373 : : }
3374 : :
3375 : : // in functions, turn implicit globals into explicit globals
3376 : : // compute the index of each local
3377 : 9026 : scope->num_locals = 0;
3378 [ + + ]: 46054 : for (int i = 0; i < scope->id_info_len; i++) {
3379 : 37028 : id_info_t *id = &scope->id_info[i];
3380 [ + + + + ]: 37028 : if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
3381 : : // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3382 : 938 : continue;
3383 : : }
3384 [ + + + + ]: 36090 : if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3385 : 6515 : id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3386 : : }
3387 : : #if MICROPY_EMIT_NATIVE
3388 [ + + ]: 36090 : if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
3389 : : // This function makes a reference to a global variable
3390 [ + + ]: 6928 : if (scope->emit_options == MP_EMIT_OPT_VIPER
3391 [ + + ]: 162 : && mp_native_type_from_qstr(id->qst) >= MP_NATIVE_TYPE_INT) {
3392 : : // A casting operator in viper mode, not a real global reference
3393 : : } else {
3394 : 6890 : scope->scope_flags |= MP_SCOPE_FLAG_REFGLOBALS;
3395 : : }
3396 : : }
3397 : : #endif
3398 : : // params always count for 1 local, even if they are a cell
3399 [ + + + + ]: 36090 : if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
3400 : 8663 : id->local_num = scope->num_locals++;
3401 : : }
3402 : : }
3403 : :
3404 : : // compute the index of cell vars
3405 [ + + ]: 46054 : for (int i = 0; i < scope->id_info_len; i++) {
3406 : 37028 : id_info_t *id = &scope->id_info[i];
3407 : : // in MicroPython the cells come right after the fast locals
3408 : : // parameters are not counted here, since they remain at the start
3409 : : // of the locals, even if they are cell vars
3410 [ + + + + ]: 37028 : if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
3411 : 138 : id->local_num = scope->num_locals;
3412 : 138 : scope->num_locals += 1;
3413 : : }
3414 : : }
3415 : :
3416 : : // compute the index of free vars
3417 : : // make sure they are in the order of the parent scope
3418 [ + + ]: 9026 : if (scope->parent != NULL) {
3419 : : int num_free = 0;
3420 [ + + ]: 69555 : for (int i = 0; i < scope->parent->id_info_len; i++) {
3421 : 63945 : id_info_t *id = &scope->parent->id_info[i];
3422 [ + + ]: 63945 : if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3423 [ + + ]: 1433 : for (int j = 0; j < scope->id_info_len; j++) {
3424 : 1148 : id_info_t *id2 = &scope->id_info[j];
3425 [ + + + + ]: 1148 : if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
3426 [ - + ]: 204 : assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
3427 : : // in MicroPython the frees come first, before the params
3428 : 204 : id2->local_num = num_free;
3429 : 204 : num_free += 1;
3430 : : }
3431 : : }
3432 : : }
3433 : : }
3434 : : // in MicroPython shift all other locals after the free locals
3435 [ + + ]: 5610 : if (num_free > 0) {
3436 [ + + ]: 657 : for (int i = 0; i < scope->id_info_len; i++) {
3437 : 515 : id_info_t *id = &scope->id_info[i];
3438 [ + + - + ]: 515 : if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
3439 : 311 : id->local_num += num_free;
3440 : : }
3441 : : }
3442 : 142 : scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function
3443 : 142 : scope->num_locals += num_free;
3444 : : }
3445 : : }
3446 : 9026 : }
3447 : :
3448 : : #if !MICROPY_PERSISTENT_CODE_SAVE
3449 : : STATIC
3450 : : #endif
3451 : 3661 : void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_compiled_module_t *cm) {
3452 : : // put compiler state on the stack, it's relatively small
3453 : 3661 : compiler_t comp_state = {0};
3454 : 3661 : compiler_t *comp = &comp_state;
3455 : :
3456 : 3661 : comp->is_repl = is_repl;
3457 : 3661 : comp->break_label = INVALID_LABEL;
3458 : 3661 : comp->continue_label = INVALID_LABEL;
3459 : 3661 : mp_emit_common_init(&comp->emit_common, source_file);
3460 : :
3461 : : // create the module scope
3462 : : #if MICROPY_EMIT_NATIVE
3463 : 3661 : const uint emit_opt = MP_STATE_VM(default_emit_opt);
3464 : : #else
3465 : : const uint emit_opt = MP_EMIT_OPT_NONE;
3466 : : #endif
3467 : 3661 : scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
3468 : :
3469 : : // create standard emitter; it's used at least for MP_PASS_SCOPE
3470 : 3661 : emit_t *emit_bc = emit_bc_new(&comp->emit_common);
3471 : :
3472 : : // compile MP_PASS_SCOPE
3473 : 3661 : comp->emit = emit_bc;
3474 : : #if MICROPY_EMIT_NATIVE
3475 : 3661 : comp->emit_method_table = &emit_bc_method_table;
3476 : : #endif
3477 : 3661 : uint max_num_labels = 0;
3478 [ + + + + ]: 13002 : for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
3479 : : #if MICROPY_EMIT_INLINE_ASM
3480 : : if (s->emit_options == MP_EMIT_OPT_ASM) {
3481 : : compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
3482 : : } else
3483 : : #endif
3484 : : {
3485 : 9345 : compile_scope(comp, s, MP_PASS_SCOPE);
3486 : :
3487 : : // Check if any implicitly declared variables should be closed over
3488 [ + + ]: 46643 : for (size_t i = 0; i < s->id_info_len; ++i) {
3489 : 37298 : id_info_t *id = &s->id_info[i];
3490 [ + + ]: 37298 : if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3491 : 15235 : scope_check_to_close_over(s, id);
3492 : : }
3493 : : }
3494 : : }
3495 : :
3496 : : // update maximum number of labels needed
3497 [ + + ]: 9341 : if (comp->next_label > max_num_labels) {
3498 : 2993 : max_num_labels = comp->next_label;
3499 : : }
3500 : : }
3501 : :
3502 : : // compute some things related to scope and identifiers
3503 [ + + + + ]: 12683 : for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
3504 : 9026 : scope_compute_things(s);
3505 : : }
3506 : :
3507 : : // set max number of labels now that it's calculated
3508 : 3657 : emit_bc_set_max_num_labels(emit_bc, max_num_labels);
3509 : :
3510 : : // compile MP_PASS_STACK_SIZE, MP_PASS_CODE_SIZE, MP_PASS_EMIT
3511 : : #if MICROPY_EMIT_NATIVE
3512 : 3657 : emit_t *emit_native = NULL;
3513 : : #endif
3514 [ + + + + ]: 12665 : for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
3515 : : #if MICROPY_EMIT_INLINE_ASM
3516 : : if (s->emit_options == MP_EMIT_OPT_ASM) {
3517 : : // inline assembly
3518 : : if (comp->emit_inline_asm == NULL) {
3519 : : comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels);
3520 : : }
3521 : : comp->emit = NULL;
3522 : : comp->emit_inline_asm_method_table = ASM_EMITTER_TABLE;
3523 : : compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
3524 : : #if MICROPY_EMIT_INLINE_XTENSA
3525 : : // Xtensa requires an extra pass to compute size of l32r const table
3526 : : // TODO this can be improved by calculating it during SCOPE pass
3527 : : // but that requires some other structural changes to the asm emitters
3528 : : #if MICROPY_DYNAMIC_COMPILER
3529 : : if (mp_dynamic_compiler.native_arch == MP_NATIVE_ARCH_XTENSA)
3530 : : #endif
3531 : : {
3532 : : compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
3533 : : }
3534 : : #endif
3535 : : if (comp->compile_error == MP_OBJ_NULL) {
3536 : : compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
3537 : : }
3538 : : } else
3539 : : #endif
3540 : : {
3541 : :
3542 : : // choose the emit type
3543 : :
3544 [ + + ]: 9026 : switch (s->emit_options) {
3545 : :
3546 : : #if MICROPY_EMIT_NATIVE
3547 : 4083 : case MP_EMIT_OPT_NATIVE_PYTHON:
3548 : : case MP_EMIT_OPT_VIPER:
3549 [ + + ]: 4083 : if (emit_native == NULL) {
3550 : 1410 : emit_native = NATIVE_EMITTER(new)(&comp->emit_common, &comp->compile_error, &comp->next_label, max_num_labels);
3551 : : }
3552 : 4083 : comp->emit_method_table = NATIVE_EMITTER_TABLE;
3553 : 4083 : comp->emit = emit_native;
3554 : 4083 : break;
3555 : : #endif // MICROPY_EMIT_NATIVE
3556 : :
3557 : 4943 : default:
3558 : 4943 : comp->emit = emit_bc;
3559 : : #if MICROPY_EMIT_NATIVE
3560 : 4943 : comp->emit_method_table = &emit_bc_method_table;
3561 : : #endif
3562 : 4943 : break;
3563 : : }
3564 : :
3565 : : // need a pass to compute stack size
3566 : 9026 : compile_scope(comp, s, MP_PASS_STACK_SIZE);
3567 : :
3568 : : // second last pass: compute code size
3569 [ + + ]: 9010 : if (comp->compile_error == MP_OBJ_NULL) {
3570 : 8930 : compile_scope(comp, s, MP_PASS_CODE_SIZE);
3571 : : }
3572 : :
3573 : : // final pass: emit code
3574 : : // the emitter can request multiple of these passes
3575 [ + + ]: 9010 : if (comp->compile_error == MP_OBJ_NULL) {
3576 [ + + ]: 9057 : while (!compile_scope(comp, s, MP_PASS_EMIT)) {
3577 : 9059 : }
3578 : : }
3579 : : }
3580 : : }
3581 : :
3582 [ + + ]: 3639 : if (comp->compile_error != MP_OBJ_NULL) {
3583 : : // if there is no line number for the error then use the line
3584 : : // number for the start of this scope
3585 : 321 : compile_error_set_line(comp, comp->scope_cur->pn);
3586 : : // add a traceback to the exception using relevant source info
3587 : 321 : mp_obj_exception_add_traceback(comp->compile_error, source_file,
3588 : 321 : comp->compile_error_line, comp->scope_cur->simple_name);
3589 : : }
3590 : :
3591 : : // construct the global qstr/const table for this module
3592 : 3639 : cm->rc = module_scope->raw_code;
3593 : : #if MICROPY_PERSISTENT_CODE_SAVE
3594 : : cm->has_native = false;
3595 : : #if MICROPY_EMIT_NATIVE
3596 : : if (emit_native != NULL) {
3597 : : cm->has_native = true;
3598 : : }
3599 : : #endif
3600 : : #if MICROPY_EMIT_INLINE_ASM
3601 : : if (comp->emit_inline_asm != NULL) {
3602 : : cm->has_native = true;
3603 : : }
3604 : : #endif
3605 : : cm->n_qstr = comp->emit_common.qstr_map.used;
3606 : : cm->n_obj = comp->emit_common.const_obj_list.len;
3607 : : #endif
3608 [ + + ]: 3639 : if (comp->compile_error == MP_OBJ_NULL) {
3609 : 3318 : mp_emit_common_populate_module_context(&comp->emit_common, source_file, cm->context);
3610 : :
3611 : : #if MICROPY_DEBUG_PRINTERS
3612 : : // now that the module context is valid, the raw codes can be printed
3613 [ + + ]: 3318 : if (mp_verbose_flag >= 2) {
3614 [ + + ]: 52 : for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
3615 : 42 : mp_raw_code_t *rc = s->raw_code;
3616 [ + - ]: 42 : if (rc->kind == MP_CODE_BYTECODE) {
3617 : 42 : mp_bytecode_print(&mp_plat_print, rc, &cm->context->constants);
3618 : : }
3619 : : }
3620 : : }
3621 : : #endif
3622 : : }
3623 : :
3624 : : // free the emitters
3625 : :
3626 : 3639 : emit_bc_free(emit_bc);
3627 : : #if MICROPY_EMIT_NATIVE
3628 [ + + ]: 3639 : if (emit_native != NULL) {
3629 : 1394 : NATIVE_EMITTER(free)(emit_native);
3630 : : }
3631 : : #endif
3632 : : #if MICROPY_EMIT_INLINE_ASM
3633 : : if (comp->emit_inline_asm != NULL) {
3634 : : ASM_EMITTER(free)(comp->emit_inline_asm);
3635 : : }
3636 : : #endif
3637 : :
3638 : : // free the parse tree
3639 : 3639 : mp_parse_tree_clear(parse_tree);
3640 : :
3641 : : // free the scopes
3642 [ + + ]: 12963 : for (scope_t *s = module_scope; s;) {
3643 : 9324 : scope_t *next = s->next;
3644 : 9324 : scope_free(s);
3645 : 9324 : s = next;
3646 : : }
3647 : :
3648 [ + + ]: 3639 : if (comp->compile_error != MP_OBJ_NULL) {
3649 : 321 : nlr_raise(comp->compile_error);
3650 : : }
3651 : 3318 : }
3652 : :
3653 : 3661 : mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) {
3654 : 3661 : mp_compiled_module_t cm;
3655 : 3661 : cm.context = m_new_obj(mp_module_context_t);
3656 : 3661 : cm.context->module.globals = mp_globals_get();
3657 : 3661 : mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm);
3658 : : // return function that executes the outer module
3659 : 3318 : return mp_make_function_from_raw_code(cm.rc, cm.context, NULL);
3660 : : }
3661 : :
3662 : : #endif // MICROPY_ENABLE_COMPILER
|