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 : 3574 : STATIC void mp_emit_common_init(mp_emit_common_t *emit, qstr source_file) {
206 : : #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
207 : 3574 : mp_map_init(&emit->qstr_map, 1);
208 : :
209 : : // add the source file as the first entry in the qstr table
210 : 3574 : 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 : 3574 : elem->value = MP_OBJ_NEW_SMALL_INT(0);
212 : : #endif
213 : 3574 : mp_obj_list_init(&emit->const_obj_list, 0);
214 : 3574 : }
215 : :
216 : 35677 : STATIC void mp_emit_common_start_pass(mp_emit_common_t *emit, pass_kind_t pass) {
217 : 35677 : emit->pass = pass;
218 [ + + ]: 35677 : if (pass == MP_PASS_CODE_SIZE) {
219 [ + + ]: 8760 : if (emit->ct_cur_child == 0) {
220 : 6829 : emit->children = NULL;
221 : : } else {
222 : 1931 : emit->children = m_new0(mp_raw_code_t *, emit->ct_cur_child);
223 : : }
224 : : }
225 : 35677 : emit->ct_cur_child = 0;
226 : 35677 : }
227 : :
228 : 3236 : 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 : 3236 : size_t qstr_map_used = emit->qstr_map.used;
231 : 3236 : mp_module_context_alloc_tables(context, qstr_map_used, emit->const_obj_list.len);
232 [ + + ]: 42236 : for (size_t i = 0; i < emit->qstr_map.alloc; ++i) {
233 [ + + ]: 39000 : if (mp_map_slot_is_filled(&emit->qstr_map, i)) {
234 : 35000 : size_t idx = MP_OBJ_SMALL_INT_VALUE(emit->qstr_map.table[i].value);
235 : 35000 : qstr qst = MP_OBJ_QSTR_VALUE(emit->qstr_map.table[i].key);
236 : 35000 : 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 [ + + ]: 9127 : for (size_t i = 0; i < emit->const_obj_list.len; ++i) {
245 : 5891 : context->constants.obj_table[i] = emit->const_obj_list.items[i];
246 : : }
247 : 3236 : }
248 : :
249 : : /******************************************************************************/
250 : :
251 : 572 : 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 [ + + + + : 572 : if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
+ + ]
254 : 316 : comp->compile_error_line = ((mp_parse_node_struct_t *)pn)->source_line;
255 : : }
256 : 572 : }
257 : :
258 : 244 : 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 [ + + ]: 244 : if (comp->compile_error == MP_OBJ_NULL) {
261 : 232 : comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
262 : 232 : compile_error_set_line(comp, pn);
263 : : }
264 : 244 : }
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 : 281938 : STATIC uint comp_next_label(compiler_t *comp) {
272 : 281938 : return comp->next_label++;
273 : : }
274 : :
275 : : #if MICROPY_EMIT_NATIVE
276 : 164576 : STATIC void reserve_labels_for_native(compiler_t *comp, int n) {
277 [ + + ]: 164576 : if (comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
278 : 26745 : comp->next_label += n;
279 : : }
280 : 164576 : }
281 : : #else
282 : : #define reserve_labels_for_native(comp, n)
283 : : #endif
284 : :
285 : 88983 : STATIC void compile_increase_except_level(compiler_t *comp, uint label, int kind) {
286 : 88983 : EMIT_ARG(setup_block, label, kind);
287 : 88983 : comp->cur_except_level += 1;
288 [ + + ]: 88983 : if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
289 : 2256 : comp->scope_cur->exc_stack_size = comp->cur_except_level;
290 : : }
291 : 88983 : }
292 : :
293 : 88983 : STATIC void compile_decrease_except_level(compiler_t *comp) {
294 [ - + ]: 88983 : assert(comp->cur_except_level > 0);
295 : 88983 : comp->cur_except_level -= 1;
296 : 88983 : EMIT(end_finally);
297 : 88983 : reserve_labels_for_native(comp, 1);
298 : 88983 : }
299 : :
300 : 9182 : STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
301 : 9182 : scope_t *scope = scope_new(kind, pn, emit_options);
302 : 9182 : scope->parent = comp->scope_cur;
303 : 9182 : scope->next = NULL;
304 [ + + ]: 9182 : if (comp->scope_head == NULL) {
305 : 3574 : comp->scope_head = scope;
306 : : } else {
307 : : scope_t *s = comp->scope_head;
308 [ + + ]: 46664 : while (s->next != NULL) {
309 : : s = s->next;
310 : : }
311 : 5608 : s->next = scope;
312 : : }
313 : 9182 : return scope;
314 : : }
315 : :
316 : : typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
317 : :
318 : 27376 : 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 [ + + + + : 27376 : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
+ + ]
320 : 8603 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
321 : 8603 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
322 [ + + ]: 28963 : for (int i = 0; i < num_nodes; i++) {
323 : 20360 : f(comp, pns->nodes[i]);
324 : : }
325 : : } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
326 : 12905 : f(comp, pn);
327 : : }
328 : 27372 : }
329 : :
330 : 74468 : STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
331 : 74468 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
332 [ + + ]: 287350 : for (int i = 0; i < num_nodes; i++) {
333 : 212906 : compile_node(comp, pns->nodes[i]);
334 [ + + ]: 212906 : if (comp->compile_error != MP_OBJ_NULL) {
335 : : // add line info for the error in case it didn't have a line number
336 : 24 : compile_error_set_line(comp, pns->nodes[i]);
337 : 24 : return;
338 : : }
339 : : }
340 : : }
341 : :
342 : 378256 : STATIC void compile_load_id(compiler_t *comp, qstr qst) {
343 [ + + ]: 378256 : if (comp->pass == MP_PASS_SCOPE) {
344 : 71848 : mp_emit_common_get_id_for_load(comp->scope_cur, qst);
345 : : } else {
346 : : #if NEED_METHOD_TABLE
347 : 306408 : 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 : 378256 : }
353 : :
354 : 111016 : STATIC void compile_store_id(compiler_t *comp, qstr qst) {
355 [ + + ]: 111016 : if (comp->pass == MP_PASS_SCOPE) {
356 : 20284 : mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
357 : : } else {
358 : : #if NEED_METHOD_TABLE
359 : 90732 : 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 : 111016 : }
365 : :
366 : 1594 : STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
367 [ + + ]: 1594 : if (comp->pass == MP_PASS_SCOPE) {
368 : 387 : mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
369 : : } else {
370 : : #if NEED_METHOD_TABLE
371 : 1207 : 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 : 1594 : }
377 : :
378 : 3589 : STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
379 : : // a simple tuple expression
380 : 3589 : size_t num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
381 [ + + ]: 13847 : for (size_t i = 0; i < num_nodes; i++) {
382 : 10258 : compile_node(comp, pns->nodes[i]);
383 : : }
384 : 3589 : EMIT_ARG(build, num_nodes, MP_EMIT_BUILD_TUPLE);
385 : 3589 : }
386 : :
387 : 17319 : STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
388 [ + + ]: 17319 : if (mp_parse_node_is_const_false(pn)) {
389 [ + + ]: 102 : if (jump_if == false) {
390 : 8 : EMIT_ARG(jump, label);
391 : : }
392 : 102 : return;
393 [ + + ]: 17217 : } 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 [ + - + + ]: 16267 : } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
399 : 14414 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
400 : 14414 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
401 [ + + ]: 14414 : 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 [ + + ]: 3174 : for (int i = 0; i < n; i++) {
413 : 2116 : c_if_cond(comp, pns->nodes[i], jump_if, label);
414 : : }
415 : : }
416 : 1418 : return;
417 [ + + ]: 14074 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
418 [ + + ]: 1078 : if (jump_if == false) {
419 : 1042 : goto and_or_logic2;
420 : : } else {
421 : 36 : goto and_or_logic1;
422 : : }
423 [ + + ]: 12996 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
424 : 1664 : c_if_cond(comp, pns->nodes[0], !jump_if, label);
425 : 1664 : return;
426 [ - + ]: 11332 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
427 : : // cond is something in parenthesis
428 [ # # ]: 0 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
429 : : // empty tuple, acts as false for the condition
430 [ # # ]: 0 : if (jump_if == false) {
431 : 0 : EMIT_ARG(jump, label);
432 : : }
433 : : } else {
434 [ # # # # ]: 0 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
435 : : // non-empty tuple, acts as true for the condition
436 [ # # ]: 0 : if (jump_if == true) {
437 : 0 : EMIT_ARG(jump, label);
438 : : }
439 : : }
440 : 0 : return;
441 : : }
442 : : }
443 : :
444 : : // nothing special, fall back to default compiling for node and jump
445 : 13185 : compile_node(comp, pn);
446 : 13185 : EMIT_ARG(pop_jump_if, jump_if, label);
447 : : }
448 : :
449 : : typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
450 : : STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
451 : :
452 : 9128 : STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
453 [ + + ]: 9128 : if (assign_kind != ASSIGN_AUG_STORE) {
454 : 8968 : compile_node(comp, pns->nodes[0]);
455 : : }
456 : :
457 [ + - + - ]: 9128 : if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
458 : 9128 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
459 [ + + ]: 9128 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
460 : 1247 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
461 [ + - ]: 1247 : if (assign_kind != ASSIGN_AUG_STORE) {
462 [ + + ]: 2526 : for (int i = 0; i < n - 1; i++) {
463 : 1279 : compile_node(comp, pns1->nodes[i]);
464 : : }
465 : : }
466 [ + - - + ]: 1247 : assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
467 : 1247 : pns1 = (mp_parse_node_struct_t *)pns1->nodes[n - 1];
468 : : }
469 [ + + ]: 9128 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
470 [ + + ]: 3665 : if (assign_kind == ASSIGN_AUG_STORE) {
471 : 44 : EMIT(rot_three);
472 : 44 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
473 : : } else {
474 : 3621 : compile_node(comp, pns1->nodes[0]);
475 [ + + ]: 3621 : if (assign_kind == ASSIGN_AUG_LOAD) {
476 : 44 : EMIT(dup_top_two);
477 : 44 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
478 : : } else {
479 : 3577 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
480 : : }
481 : : }
482 : 3665 : return;
483 [ + + ]: 5463 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
484 [ - + ]: 5459 : assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
485 [ + + ]: 5459 : if (assign_kind == ASSIGN_AUG_LOAD) {
486 : 116 : EMIT(dup_top);
487 : 116 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]), MP_EMIT_ATTR_LOAD);
488 : : } else {
489 [ + + ]: 5343 : if (assign_kind == ASSIGN_AUG_STORE) {
490 : 116 : EMIT(rot_two);
491 : : }
492 : 5343 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]), MP_EMIT_ATTR_STORE);
493 : : }
494 : 5459 : return;
495 : : }
496 : : }
497 : :
498 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("can't assign to expression"));
499 : : }
500 : :
501 : 1278 : STATIC void c_assign_tuple(compiler_t *comp, uint num_tail, mp_parse_node_t *nodes_tail) {
502 : : // look for star expression
503 : 1278 : uint have_star_index = -1;
504 [ + + ]: 4282 : for (uint i = 0; i < num_tail; i++) {
505 [ + - + + : 3008 : if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
+ + ]
506 [ + + ]: 280 : if (have_star_index == (uint)-1) {
507 : 276 : EMIT_ARG(unpack_ex, i, num_tail - i - 1);
508 : 276 : have_star_index = i;
509 : : } else {
510 : 4 : compile_syntax_error(comp, nodes_tail[i], MP_ERROR_TEXT("multiple *x in assignment"));
511 : 4 : return;
512 : : }
513 : : }
514 : : }
515 [ + + ]: 1274 : if (have_star_index == (uint)-1) {
516 : 1002 : EMIT_ARG(unpack_sequence, num_tail);
517 : : }
518 [ + + ]: 4274 : for (uint i = 0; i < num_tail; i++) {
519 [ + + ]: 3000 : if (i == have_star_index) {
520 : 272 : c_assign(comp, ((mp_parse_node_struct_t *)nodes_tail[i])->nodes[0], ASSIGN_STORE);
521 : : } else {
522 : 2728 : c_assign(comp, nodes_tail[i], ASSIGN_STORE);
523 : : }
524 : : }
525 : : }
526 : :
527 : : // assigns top of stack to pn
528 : 84142 : STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
529 [ - + ]: 84142 : assert(!MP_PARSE_NODE_IS_NULL(pn));
530 [ + + ]: 84142 : if (MP_PARSE_NODE_IS_LEAF(pn)) {
531 [ + + ]: 73692 : if (MP_PARSE_NODE_IS_ID(pn)) {
532 : 73680 : qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
533 [ + + ]: 73680 : switch (assign_kind) {
534 : 72289 : case ASSIGN_STORE:
535 : : case ASSIGN_AUG_STORE:
536 : 72289 : compile_store_id(comp, arg);
537 : 72289 : break;
538 : 1391 : case ASSIGN_AUG_LOAD:
539 : : default:
540 : 1391 : compile_load_id(comp, arg);
541 : 1391 : break;
542 : : }
543 : : } else {
544 : 12 : goto cannot_assign;
545 : : }
546 : : } else {
547 : : // pn must be a struct
548 : 10450 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
549 [ + + + + : 10450 : switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
+ ]
550 : 9128 : case PN_atom_expr_normal:
551 : : // lhs is an index or attribute
552 : 9128 : c_assign_atom_expr(comp, pns, assign_kind);
553 : 9128 : break;
554 : :
555 : 1182 : case PN_testlist_star_expr:
556 : : case PN_exprlist:
557 : : // lhs is a tuple
558 [ + + ]: 1182 : if (assign_kind != ASSIGN_STORE) {
559 : 8 : goto cannot_assign;
560 : : }
561 : 1174 : c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
562 : 1174 : break;
563 : :
564 : 72 : case PN_atom_paren:
565 : : // lhs is something in parenthesis
566 [ + + ]: 72 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
567 : : // empty tuple
568 : 4 : goto cannot_assign;
569 : : } else {
570 [ + - - + ]: 68 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
571 [ + + ]: 68 : if (assign_kind != ASSIGN_STORE) {
572 : 8 : goto cannot_assign;
573 : : }
574 : 60 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
575 : 60 : goto testlist_comp;
576 : : }
577 : 56 : break;
578 : :
579 : 56 : case PN_atom_bracket:
580 : : // lhs is something in brackets
581 [ + + ]: 56 : if (assign_kind != ASSIGN_STORE) {
582 : 8 : goto cannot_assign;
583 : : }
584 [ + + ]: 48 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
585 : : // empty list, assignment allowed
586 : 8 : c_assign_tuple(comp, 0, NULL);
587 [ + + + + ]: 40 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
588 : 24 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
589 : 24 : goto testlist_comp;
590 : : } else {
591 : : // brackets around 1 item
592 : 16 : c_assign_tuple(comp, 1, pns->nodes);
593 : : }
594 : : break;
595 : :
596 : 12 : default:
597 : 12 : goto cannot_assign;
598 : : }
599 : 10326 : return;
600 : :
601 : 84 : testlist_comp:
602 : : // lhs is a sequence
603 [ + + + - : 84 : if (MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns)) {
+ + + + ]
604 : 4 : goto cannot_assign;
605 : : }
606 : 80 : c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
607 : 80 : return;
608 : : }
609 : : return;
610 : :
611 : 56 : cannot_assign:
612 : 56 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("can't assign to expression"));
613 : : }
614 : :
615 : : // stuff for lambda and comprehensions and generators:
616 : : // if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
617 : : // if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
618 : : // if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
619 : 22211 : STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
620 [ - + ]: 22211 : assert(n_pos_defaults >= 0);
621 [ - + ]: 22211 : assert(n_kw_defaults >= 0);
622 : :
623 : : // set flags
624 [ + + ]: 22211 : if (n_kw_defaults > 0) {
625 : 100 : this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
626 : : }
627 : 22211 : this_scope->num_def_pos_args = n_pos_defaults;
628 : :
629 : : #if MICROPY_EMIT_NATIVE
630 : : // When creating a function/closure it will take a reference to the current globals
631 : 22211 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_REFGLOBALS | MP_SCOPE_FLAG_HASCONSTS;
632 : : #endif
633 : :
634 : : // make closed over variables, if any
635 : : // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
636 : 22211 : int nfree = 0;
637 [ + + ]: 22211 : if (comp->scope_cur->kind != SCOPE_MODULE) {
638 [ + + ]: 102707 : for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
639 : 93195 : id_info_t *id = &comp->scope_cur->id_info[i];
640 [ + + ]: 93195 : if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
641 [ + + ]: 4293 : for (int j = 0; j < this_scope->id_info_len; j++) {
642 : 3438 : id_info_t *id2 = &this_scope->id_info[j];
643 [ + + + + ]: 3438 : if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
644 : : // in MicroPython we load closures using LOAD_FAST
645 : 606 : EMIT_LOAD_FAST(id->qst, id->local_num);
646 : 606 : nfree += 1;
647 : : }
648 : : }
649 : : }
650 : : }
651 : : }
652 : :
653 : : // make the function/closure
654 [ + + ]: 9512 : if (nfree == 0) {
655 : 21791 : EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
656 : : } else {
657 : 420 : EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
658 : : }
659 : 22211 : }
660 : :
661 : 22109 : STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
662 : : // For efficiency of the code below we extract the parse-node kind here
663 : 22109 : int pn_kind;
664 [ + + ]: 22109 : if (MP_PARSE_NODE_IS_ID(pn)) {
665 : : pn_kind = -1;
666 : : } else {
667 [ + - - + ]: 3380 : assert(MP_PARSE_NODE_IS_STRUCT(pn));
668 : 3380 : pn_kind = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn);
669 : : }
670 : :
671 [ + + ]: 22109 : if (pn_kind == PN_typedargslist_star || pn_kind == PN_varargslist_star) {
672 : 457 : comp->have_star = true;
673 : : /* don't need to distinguish bare from named star
674 : : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
675 : : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
676 : : // bare star
677 : : } else {
678 : : // named star
679 : : }
680 : : */
681 : :
682 [ + + ]: 21652 : } else if (pn_kind == PN_typedargslist_dbl_star || pn_kind == PN_varargslist_dbl_star) {
683 : : // named double star
684 : : // TODO do we need to do anything with this?
685 : :
686 : : } else {
687 : 21499 : mp_parse_node_t pn_id;
688 : 21499 : mp_parse_node_t pn_equal;
689 [ + + ]: 21499 : if (pn_kind == -1) {
690 : : // this parameter is just an id
691 : :
692 : : pn_id = pn;
693 : : pn_equal = MP_PARSE_NODE_NULL;
694 : :
695 [ + + ]: 2770 : } else if (pn_kind == PN_typedargslist_name) {
696 : : // this parameter has a colon and/or equal specifier
697 : :
698 : 2746 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
699 : 2746 : pn_id = pns->nodes[0];
700 : : // pn_colon = pns->nodes[1]; // unused
701 : 2746 : pn_equal = pns->nodes[2];
702 : :
703 : : } else {
704 [ - + ]: 24 : assert(pn_kind == PN_varargslist_name); // should be
705 : : // this parameter has an equal specifier
706 : :
707 : 24 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
708 : 24 : pn_id = pns->nodes[0];
709 : 24 : pn_equal = pns->nodes[1];
710 : : }
711 : :
712 [ + + ]: 2770 : if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
713 : : // this parameter does not have a default value
714 : :
715 : : // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
716 [ + + + + ]: 20057 : if (!comp->have_star && comp->num_default_params != 0) {
717 : 4 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("non-default argument follows default argument"));
718 : 4 : return;
719 : : }
720 : :
721 : : } else {
722 : : // this parameter has a default value
723 : : // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
724 : :
725 [ + + ]: 1442 : if (comp->have_star) {
726 : 116 : comp->num_dict_params += 1;
727 : : // in MicroPython we put the default dict parameters into a dictionary using the bytecode
728 [ + + ]: 116 : if (comp->num_dict_params == 1) {
729 : : // in MicroPython we put the default positional parameters into a tuple using the bytecode
730 : : // we need to do this here before we start building the map for the default keywords
731 [ + + ]: 100 : if (comp->num_default_params > 0) {
732 : 8 : EMIT_ARG(build, comp->num_default_params, MP_EMIT_BUILD_TUPLE);
733 : : } else {
734 : 92 : EMIT(load_null); // sentinel indicating empty default positional args
735 : : }
736 : : // first default dict param, so make the map
737 : 100 : EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
738 : : }
739 : :
740 : : // compile value then key, then store it to the dict
741 : 116 : compile_node(comp, pn_equal);
742 : 116 : EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
743 : 116 : EMIT(store_map);
744 : : } else {
745 : 1326 : comp->num_default_params += 1;
746 : 1326 : compile_node(comp, pn_equal);
747 : : }
748 : : }
749 : : }
750 : : }
751 : :
752 : 17787 : STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) {
753 : : // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
754 : : // expression for default arguments, which may contain a lambda. The lambda will
755 : : // call here in a nested way, so we must save and restore the relevant state.
756 : 17787 : bool orig_have_star = comp->have_star;
757 : 17787 : uint16_t orig_num_dict_params = comp->num_dict_params;
758 : 17787 : uint16_t orig_num_default_params = comp->num_default_params;
759 : :
760 : : // compile default parameters
761 : 17787 : comp->have_star = false;
762 : 17787 : comp->num_dict_params = 0;
763 : 17787 : comp->num_default_params = 0;
764 : 17787 : apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
765 : :
766 [ + + ]: 17787 : if (comp->compile_error != MP_OBJ_NULL) {
767 : : return;
768 : : }
769 : :
770 : : // in MicroPython we put the default positional parameters into a tuple using the bytecode
771 : : // the default keywords args may have already made the tuple; if not, do it now
772 [ + + + + ]: 17775 : if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
773 : 1026 : EMIT_ARG(build, comp->num_default_params, MP_EMIT_BUILD_TUPLE);
774 : 1026 : EMIT(load_null); // sentinel indicating empty default keyword args
775 : : }
776 : :
777 : : // make the function
778 : 17775 : close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
779 : :
780 : : // restore state
781 : 17775 : comp->have_star = orig_have_star;
782 : 17775 : comp->num_dict_params = orig_num_dict_params;
783 : 17775 : comp->num_default_params = orig_num_default_params;
784 : : }
785 : :
786 : : // leaves function object on stack
787 : : // returns function name
788 : 17212 : STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
789 [ + + ]: 17212 : if (comp->pass == MP_PASS_SCOPE) {
790 : : // create a new scope for this function
791 : 4340 : scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
792 : : // store the function scope so the compiling function can use it at each pass
793 : 4340 : pns->nodes[4] = (mp_parse_node_t)s;
794 : : }
795 : :
796 : : // get the scope for this function
797 : 17212 : scope_t *fscope = (scope_t *)pns->nodes[4];
798 : :
799 : : // compile the function definition
800 : 17212 : compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
801 : :
802 : : // return its name (the 'f' in "def f(...):")
803 : 17212 : return fscope->simple_name;
804 : : }
805 : :
806 : : // leaves class object on stack
807 : : // returns class name
808 : 3692 : STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
809 [ + + ]: 3692 : if (comp->pass == MP_PASS_SCOPE) {
810 : : // create a new scope for this class
811 : 922 : scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
812 : : // store the class scope so the compiling function can use it at each pass
813 : 922 : pns->nodes[3] = (mp_parse_node_t)s;
814 : : }
815 : :
816 : 3692 : EMIT(load_build_class);
817 : :
818 : : // scope for this class
819 : 3692 : scope_t *cscope = (scope_t *)pns->nodes[3];
820 : :
821 : : // compile the class
822 : 3692 : close_over_variables_etc(comp, cscope, 0, 0);
823 : :
824 : : // get its name
825 : 3692 : EMIT_ARG(load_const_str, cscope->simple_name);
826 : :
827 : : // nodes[1] has parent classes, if any
828 : : // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
829 : 3692 : mp_parse_node_t parents = pns->nodes[1];
830 [ + + + + : 3692 : if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
+ + ]
831 : 48 : parents = MP_PARSE_NODE_NULL;
832 : : }
833 : 3692 : compile_trailer_paren_helper(comp, parents, false, 2);
834 : :
835 : : // return its name (the 'C' in class C(...):")
836 : 3692 : return cscope->simple_name;
837 : : }
838 : :
839 : : // returns true if it was a built-in decorator (even if the built-in had an error)
840 : 1696 : STATIC bool compile_built_in_decorator(compiler_t *comp, size_t name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
841 [ + + ]: 1696 : if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
842 : : return false;
843 : : }
844 : :
845 [ + + ]: 1488 : if (name_len != 2) {
846 : 4 : compile_syntax_error(comp, name_nodes[0], MP_ERROR_TEXT("invalid micropython decorator"));
847 : 4 : return true;
848 : : }
849 : :
850 : 1484 : qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
851 [ + + ]: 1484 : if (attr == MP_QSTR_bytecode) {
852 : 16 : *emit_options = MP_EMIT_OPT_BYTECODE;
853 : : #if MICROPY_EMIT_NATIVE
854 [ + + ]: 1468 : } else if (attr == MP_QSTR_native) {
855 : 252 : *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
856 [ + + ]: 1216 : } else if (attr == MP_QSTR_viper) {
857 : 1212 : *emit_options = MP_EMIT_OPT_VIPER;
858 : : #endif
859 : : #if MICROPY_EMIT_INLINE_ASM
860 : : #if MICROPY_DYNAMIC_COMPILER
861 : : } else if (attr == MP_QSTR_asm_thumb) {
862 : : *emit_options = MP_EMIT_OPT_ASM;
863 : : } else if (attr == MP_QSTR_asm_xtensa) {
864 : : *emit_options = MP_EMIT_OPT_ASM;
865 : : #else
866 : : } else if (attr == ASM_DECORATOR_QSTR) {
867 : : *emit_options = MP_EMIT_OPT_ASM;
868 : : #endif
869 : : #endif
870 : : } else {
871 : 4 : compile_syntax_error(comp, name_nodes[1], MP_ERROR_TEXT("invalid micropython decorator"));
872 : : }
873 : :
874 : : #if MICROPY_EMIT_NATIVE && MICROPY_DYNAMIC_COMPILER
875 : : if (*emit_options == MP_EMIT_OPT_NATIVE_PYTHON || *emit_options == MP_EMIT_OPT_VIPER) {
876 : : if (emit_native_table[mp_dynamic_compiler.native_arch] == NULL) {
877 : : compile_syntax_error(comp, name_nodes[1], MP_ERROR_TEXT("invalid arch"));
878 : : }
879 : : } else if (*emit_options == MP_EMIT_OPT_ASM) {
880 : : if (emit_asm_table[mp_dynamic_compiler.native_arch] == NULL) {
881 : : compile_syntax_error(comp, name_nodes[1], MP_ERROR_TEXT("invalid arch"));
882 : : }
883 : : }
884 : : #endif
885 : :
886 : : return true;
887 : : }
888 : :
889 : 1696 : STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
890 : : // get the list of decorators
891 : 1696 : mp_parse_node_t *nodes;
892 : 1696 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
893 : :
894 : : // inherit emit options for this function/class definition
895 : 1696 : uint emit_options = comp->scope_cur->emit_options;
896 : :
897 : : // compile each decorator
898 : 1696 : size_t num_built_in_decorators = 0;
899 [ + + ]: 3392 : for (size_t i = 0; i < n; i++) {
900 [ + - + - : 1696 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
- + ]
901 : 1696 : mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t *)nodes[i];
902 : :
903 : : // nodes[0] contains the decorator function, which is a dotted name
904 : 1696 : mp_parse_node_t *name_nodes;
905 : 1696 : size_t name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
906 : :
907 : : // check for built-in decorators
908 [ + + ]: 1696 : if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
909 : : // this was a built-in
910 : 1488 : num_built_in_decorators += 1;
911 : :
912 : : } else {
913 : : // not a built-in, compile normally
914 : :
915 : : // compile the decorator function
916 : 208 : compile_node(comp, name_nodes[0]);
917 [ + + ]: 224 : for (size_t j = 1; j < name_len; j++) {
918 [ - + ]: 16 : assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
919 : 16 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]), MP_EMIT_ATTR_LOAD);
920 : : }
921 : :
922 : : // nodes[1] contains arguments to the decorator function, if any
923 [ + + ]: 208 : if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
924 : : // call the decorator function with the arguments in nodes[1]
925 : 8 : compile_node(comp, pns_decorator->nodes[1]);
926 : : }
927 : : }
928 : : }
929 : :
930 : : // compile the body (funcdef, async funcdef or classdef) and get its name
931 : 1696 : mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t *)pns->nodes[1];
932 : 1696 : qstr body_name = 0;
933 [ + + ]: 1696 : if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
934 : 1680 : body_name = compile_funcdef_helper(comp, pns_body, emit_options);
935 : : #if MICROPY_PY_ASYNC_AWAIT
936 [ + + ]: 16 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
937 [ + - - + ]: 8 : assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
938 : 8 : mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns_body->nodes[0];
939 : 8 : body_name = compile_funcdef_helper(comp, pns0, emit_options);
940 : 8 : scope_t *fscope = (scope_t *)pns0->nodes[4];
941 : 8 : fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
942 : : #endif
943 : : } else {
944 [ - + ]: 8 : assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
945 : 8 : body_name = compile_classdef_helper(comp, pns_body, emit_options);
946 : : }
947 : :
948 : : // call each decorator
949 [ + + ]: 1904 : for (size_t i = 0; i < n - num_built_in_decorators; i++) {
950 : 208 : EMIT_ARG(call_function, 1, 0, 0);
951 : : }
952 : :
953 : : // store func/class object into name
954 : 1696 : compile_store_id(comp, body_name);
955 : 1696 : }
956 : :
957 : 15524 : STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
958 : 15524 : qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
959 : : // store function object into function name
960 : 15524 : compile_store_id(comp, fname);
961 : 15524 : }
962 : :
963 : 770 : STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
964 [ + + ]: 770 : if (MP_PARSE_NODE_IS_ID(pn)) {
965 : 156 : compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
966 [ + - + - : 614 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
+ + ]
967 : 562 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
968 : :
969 : 562 : compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
970 : :
971 [ + - + - ]: 562 : if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
972 : 562 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
973 [ + + ]: 562 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
974 : 264 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
975 [ + + ]: 528 : for (int i = 0; i < n - 1; i++) {
976 : 264 : compile_node(comp, pns1->nodes[i]);
977 : : }
978 [ + - - + ]: 264 : assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
979 : 264 : pns1 = (mp_parse_node_struct_t *)pns1->nodes[n - 1];
980 : : }
981 [ + + ]: 562 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
982 : 442 : compile_node(comp, pns1->nodes[0]);
983 : 442 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_DELETE);
984 [ + + ]: 120 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
985 [ - + ]: 116 : assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
986 : 116 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]), MP_EMIT_ATTR_DELETE);
987 : : } else {
988 : 4 : goto cannot_delete;
989 : : }
990 : : } else {
991 : 0 : goto cannot_delete;
992 : : }
993 : :
994 [ + - + + ]: 52 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
995 : 48 : pn = ((mp_parse_node_struct_t *)pn)->nodes[0];
996 [ + + ]: 48 : if (MP_PARSE_NODE_IS_NULL(pn)) {
997 : 4 : goto cannot_delete;
998 : : } else {
999 [ + - - + ]: 44 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
1000 : 44 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
1001 [ + + + - : 44 : if (MP_PARSE_NODE_TESTLIST_COMP_HAS_COMP_FOR(pns)) {
+ + + + ]
1002 : 4 : goto cannot_delete;
1003 : : }
1004 [ + + ]: 120 : for (size_t i = 0; i < MP_PARSE_NODE_STRUCT_NUM_NODES(pns); ++i) {
1005 : 80 : c_del_stmt(comp, pns->nodes[i]);
1006 : : }
1007 : : }
1008 : : } else {
1009 : : // some arbitrary statement that we can't delete (eg del 1)
1010 : 4 : goto cannot_delete;
1011 : : }
1012 : :
1013 : : return;
1014 : :
1015 : 16 : cannot_delete:
1016 : 16 : compile_syntax_error(comp, (mp_parse_node_t)pn, MP_ERROR_TEXT("can't delete expression"));
1017 : : }
1018 : :
1019 : 690 : STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1020 : 690 : apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1021 : 690 : }
1022 : :
1023 : 966 : STATIC void compile_break_cont_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1024 : 966 : uint16_t label;
1025 [ + + ]: 966 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_break_stmt) {
1026 : 812 : label = comp->break_label;
1027 : : } else {
1028 : 154 : label = comp->continue_label;
1029 : : }
1030 [ + + ]: 966 : if (label == INVALID_LABEL) {
1031 : 8 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'break'/'continue' outside loop"));
1032 : : }
1033 [ - + ]: 966 : assert(comp->cur_except_level >= comp->break_continue_except_level);
1034 : 966 : EMIT_ARG(unwind_jump, label, comp->cur_except_level - comp->break_continue_except_level);
1035 : 966 : }
1036 : :
1037 : 7824 : STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1038 : : #if MICROPY_CPYTHON_COMPAT
1039 [ + + ]: 7824 : if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1040 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("'return' outside function"));
1041 : 4 : return;
1042 : : }
1043 : : #endif
1044 [ + + ]: 7820 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1045 : : // no argument to 'return', so return None
1046 : 446 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1047 : 7374 : } else if (MICROPY_COMP_RETURN_IF_EXPR
1048 [ + + + + ]: 7590 : && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
1049 : : // special case when returning an if-expression; to match CPython optimisation
1050 : 216 : mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t *)pns->nodes[0];
1051 : 216 : mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t *)pns_test_if_expr->nodes[1];
1052 : :
1053 : 216 : uint l_fail = comp_next_label(comp);
1054 : 216 : c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1055 : 216 : compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1056 : 216 : EMIT(return_value);
1057 : 216 : EMIT_ARG(label_assign, l_fail);
1058 : 216 : compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1059 : : } else {
1060 : 7158 : compile_node(comp, pns->nodes[0]);
1061 : : }
1062 : 7820 : EMIT(return_value);
1063 : : }
1064 : :
1065 : 1148 : STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1066 : 1148 : compile_node(comp, pns->nodes[0]);
1067 : 1140 : EMIT(pop_top);
1068 : 1140 : }
1069 : :
1070 : 5933 : STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1071 [ + + ]: 5933 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1072 : : // raise
1073 : 54 : EMIT_ARG(raise_varargs, 0);
1074 [ + + + + ]: 5879 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
1075 : : // raise x from y
1076 : 4 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
1077 : 4 : compile_node(comp, pns->nodes[0]);
1078 : 4 : compile_node(comp, pns->nodes[1]);
1079 : 4 : EMIT_ARG(raise_varargs, 2);
1080 : : } else {
1081 : : // raise x
1082 : 5875 : compile_node(comp, pns->nodes[0]);
1083 : 5875 : EMIT_ARG(raise_varargs, 1);
1084 : : }
1085 : 5933 : }
1086 : :
1087 : : // q_base holds the base of the name
1088 : : // eg a -> q_base=a
1089 : : // a.b.c -> q_base=a
1090 : 6795 : STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
1091 : 6795 : bool is_as = false;
1092 [ + + + + : 6795 : if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
+ + ]
1093 : 1246 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
1094 : : // a name of the form x as y; unwrap it
1095 : 1246 : *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1096 : 1246 : pn = pns->nodes[0];
1097 : 1246 : is_as = true;
1098 : : }
1099 [ - + ]: 1402 : if (MP_PARSE_NODE_IS_NULL(pn)) {
1100 : : // empty name (eg, from . import x)
1101 : 108 : *q_base = MP_QSTR_;
1102 : 108 : EMIT_ARG(import, MP_QSTR_, MP_EMIT_IMPORT_NAME); // import the empty string
1103 [ + + ]: 6687 : } else if (MP_PARSE_NODE_IS_ID(pn)) {
1104 : : // just a simple name
1105 : 6523 : qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
1106 [ + + ]: 6523 : if (!is_as) {
1107 : 5285 : *q_base = q_full;
1108 : : }
1109 : 6523 : EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
1110 : : } else {
1111 [ + - + - : 164 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
- + ]
1112 : 164 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
1113 : : {
1114 : : // a name of the form a.b.c
1115 [ + + ]: 164 : if (!is_as) {
1116 : 156 : *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1117 : : }
1118 : 164 : size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
1119 [ - + ]: 164 : if (n == 0) {
1120 : : // There must be at least one node in this PN_dotted_name.
1121 : : // Let the compiler know this so it doesn't warn, and can generate better code.
1122 : 0 : MP_UNREACHABLE;
1123 : : }
1124 : 164 : size_t len = n - 1;
1125 [ + + ]: 516 : for (size_t i = 0; i < n; i++) {
1126 : 352 : len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1127 : : }
1128 : 164 : char *q_ptr = mp_local_alloc(len);
1129 : 164 : char *str_dest = q_ptr;
1130 [ + + ]: 516 : for (size_t i = 0; i < n; i++) {
1131 [ + + ]: 352 : if (i > 0) {
1132 : 188 : *str_dest++ = '.';
1133 : : }
1134 : 352 : size_t str_src_len;
1135 : 352 : const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
1136 : 352 : memcpy(str_dest, str_src, str_src_len);
1137 : 352 : str_dest += str_src_len;
1138 : : }
1139 : 164 : qstr q_full = qstr_from_strn(q_ptr, len);
1140 : 160 : mp_local_free(q_ptr);
1141 : 160 : EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
1142 [ + + ]: 160 : if (is_as) {
1143 [ + + ]: 16 : for (size_t i = 1; i < n; i++) {
1144 : 8 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), MP_EMIT_ATTR_LOAD);
1145 : : }
1146 : : }
1147 : : }
1148 : : }
1149 : 6791 : }
1150 : :
1151 : 4894 : STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
1152 : 4894 : EMIT_ARG(load_const_small_int, 0); // level 0 import
1153 : 4894 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1154 : 4894 : qstr q_base;
1155 : 4894 : do_import_name(comp, pn, &q_base);
1156 : 4890 : compile_store_id(comp, q_base);
1157 : 4890 : }
1158 : :
1159 : 4422 : STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
1160 : 4422 : apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1161 : 4418 : }
1162 : :
1163 : 1905 : STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1164 : 1905 : mp_parse_node_t pn_import_source = pns->nodes[0];
1165 : :
1166 : : // extract the preceding .'s (if any) for a relative import, to compute the import level
1167 : 1905 : uint import_level = 0;
1168 : 3810 : do {
1169 : 1905 : mp_parse_node_t pn_rel;
1170 [ + + + - : 1905 : 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)) {
+ + + + ]
1171 : : // This covers relative imports with dots only like "from .. import"
1172 : 108 : pn_rel = pn_import_source;
1173 : 108 : pn_import_source = MP_PARSE_NODE_NULL;
1174 [ + + + + ]: 1797 : } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1175 : : // This covers relative imports starting with dot(s) like "from .foo import"
1176 : 464 : mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t *)pn_import_source;
1177 : 464 : pn_rel = pns_2b->nodes[0];
1178 : 464 : pn_import_source = pns_2b->nodes[1];
1179 [ - + ]: 464 : assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1180 : : } else {
1181 : : // Not a relative import
1182 : : break;
1183 : : }
1184 : :
1185 : : // get the list of . and/or ...'s
1186 : 572 : mp_parse_node_t *nodes;
1187 : 572 : size_t n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1188 : :
1189 : : // count the total number of .'s
1190 [ + + ]: 1168 : for (size_t i = 0; i < n; i++) {
1191 [ + + ]: 596 : if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1192 : 572 : import_level++;
1193 : : } else {
1194 : : // should be an MP_TOKEN_ELLIPSIS
1195 : 24 : import_level += 3;
1196 : : }
1197 : : }
1198 : : } while (0);
1199 : :
1200 [ + + ]: 1905 : if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
1201 : : #if MICROPY_CPYTHON_COMPAT
1202 [ + + ]: 286 : if (comp->scope_cur->kind != SCOPE_MODULE) {
1203 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("import * not at module level"));
1204 : 4 : return;
1205 : : }
1206 : : #endif
1207 : :
1208 : 282 : EMIT_ARG(load_const_small_int, import_level);
1209 : :
1210 : : // build the "fromlist" tuple
1211 : 282 : EMIT_ARG(load_const_str, MP_QSTR__star_);
1212 : 282 : EMIT_ARG(build, 1, MP_EMIT_BUILD_TUPLE);
1213 : :
1214 : : // do the import
1215 : 282 : qstr dummy_q;
1216 : 282 : do_import_name(comp, pn_import_source, &dummy_q);
1217 : 282 : EMIT_ARG(import, MP_QSTRnull, MP_EMIT_IMPORT_STAR);
1218 : :
1219 : : } else {
1220 : 1619 : EMIT_ARG(load_const_small_int, import_level);
1221 : :
1222 : : // build the "fromlist" tuple
1223 : 1619 : mp_parse_node_t *pn_nodes;
1224 : 1619 : size_t n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1225 [ + + ]: 4152 : for (size_t i = 0; i < n; i++) {
1226 [ + - + - : 2533 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
- + ]
1227 : 2533 : mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pn_nodes[i];
1228 : 2533 : qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1229 : 2533 : EMIT_ARG(load_const_str, id2);
1230 : : }
1231 : 1619 : EMIT_ARG(build, n, MP_EMIT_BUILD_TUPLE);
1232 : :
1233 : : // do the import
1234 : 1619 : qstr dummy_q;
1235 : 1619 : do_import_name(comp, pn_import_source, &dummy_q);
1236 [ + + ]: 4152 : for (size_t i = 0; i < n; i++) {
1237 [ + - + - : 2533 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
- + ]
1238 : 2533 : mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pn_nodes[i];
1239 : 2533 : qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1240 : 2533 : EMIT_ARG(import, id2, MP_EMIT_IMPORT_FROM);
1241 [ + + ]: 2533 : if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
1242 : 2317 : compile_store_id(comp, id2);
1243 : : } else {
1244 : 216 : compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
1245 : : }
1246 : : }
1247 : 1619 : EMIT(pop_top);
1248 : : }
1249 : : }
1250 : :
1251 : 334 : STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, id_info_t *id_info) {
1252 [ + + ]: 334 : if (id_info->kind != ID_INFO_KIND_UNDECIDED && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1253 : 12 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("identifier redefined as global"));
1254 : 12 : return;
1255 : : }
1256 : 322 : id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1257 : :
1258 : : // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1259 : 322 : id_info = scope_find_global(comp->scope_cur, id_info->qst);
1260 [ + + ]: 322 : if (id_info != NULL) {
1261 : 148 : id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1262 : : }
1263 : : }
1264 : :
1265 : 61 : STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, id_info_t *id_info) {
1266 [ + + ]: 61 : if (id_info->kind == ID_INFO_KIND_UNDECIDED) {
1267 : 49 : id_info->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
1268 : 49 : scope_check_to_close_over(comp->scope_cur, id_info);
1269 [ + + ]: 49 : if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1270 : 8 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("no binding for nonlocal found"));
1271 : : }
1272 [ + + ]: 12 : } else if (id_info->kind != ID_INFO_KIND_FREE) {
1273 : 8 : compile_syntax_error(comp, pn, MP_ERROR_TEXT("identifier redefined as nonlocal"));
1274 : : }
1275 : 61 : }
1276 : :
1277 : 395 : STATIC void compile_declare_global_or_nonlocal(compiler_t *comp, mp_parse_node_t pn, id_info_t *id_info, bool is_global) {
1278 [ + + ]: 395 : if (is_global) {
1279 : 334 : compile_declare_global(comp, pn, id_info);
1280 : : } else {
1281 : 61 : compile_declare_nonlocal(comp, pn, id_info);
1282 : : }
1283 : 395 : }
1284 : :
1285 : 1212 : STATIC void compile_global_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1286 [ + + ]: 1212 : if (comp->pass == MP_PASS_SCOPE) {
1287 : 311 : bool is_global = MP_PARSE_NODE_STRUCT_KIND(pns) == PN_global_stmt;
1288 : :
1289 [ + + + + ]: 311 : if (!is_global && comp->scope_cur->kind == SCOPE_MODULE) {
1290 : 4 : compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("can't declare nonlocal in outer code"));
1291 : 4 : return;
1292 : : }
1293 : :
1294 : 307 : mp_parse_node_t *nodes;
1295 : 307 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
1296 [ + + ]: 672 : for (size_t i = 0; i < n; i++) {
1297 : 365 : qstr qst = MP_PARSE_NODE_LEAF_ARG(nodes[i]);
1298 : 365 : id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, ID_INFO_KIND_UNDECIDED);
1299 : 365 : compile_declare_global_or_nonlocal(comp, (mp_parse_node_t)pns, id_info, is_global);
1300 : : }
1301 : : }
1302 : : }
1303 : :
1304 : 2084 : STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1305 : : // with optimisations enabled we don't compile assertions
1306 [ + + ]: 2084 : if (MP_STATE_VM(mp_optimise_value) != 0) {
1307 : : return;
1308 : : }
1309 : :
1310 : 2060 : uint l_end = comp_next_label(comp);
1311 : 2060 : c_if_cond(comp, pns->nodes[0], true, l_end);
1312 : 2060 : EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
1313 [ + + ]: 2060 : if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1314 : : // assertion message
1315 : 64 : compile_node(comp, pns->nodes[1]);
1316 : 64 : EMIT_ARG(call_function, 1, 0, 0);
1317 : : }
1318 : 2060 : EMIT_ARG(raise_varargs, 1);
1319 : 2060 : EMIT_ARG(label_assign, l_end);
1320 : : }
1321 : :
1322 : 7521 : STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1323 : 7521 : uint l_end = comp_next_label(comp);
1324 : :
1325 : : // optimisation: don't emit anything when "if False"
1326 [ + + ]: 7521 : if (!mp_parse_node_is_const_false(pns->nodes[0])) {
1327 : 7409 : uint l_fail = comp_next_label(comp);
1328 : 7409 : c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1329 : :
1330 : 7409 : compile_node(comp, pns->nodes[1]); // if block
1331 : :
1332 : : // optimisation: skip everything else when "if True"
1333 [ + + ]: 7409 : if (mp_parse_node_is_const_true(pns->nodes[0])) {
1334 : 184 : goto done;
1335 : : }
1336 : :
1337 : : // optimisation: don't jump over non-existent elif/else blocks
1338 [ + + + + ]: 7225 : if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) {
1339 : : // jump over elif/else blocks
1340 : 2579 : EMIT_ARG(jump, l_end);
1341 : : }
1342 : :
1343 : 7225 : EMIT_ARG(label_assign, l_fail);
1344 : : }
1345 : :
1346 : : // compile elif blocks (if any)
1347 : 7337 : mp_parse_node_t *pn_elif;
1348 : 7337 : size_t n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1349 [ + + ]: 8520 : for (size_t i = 0; i < n_elif; i++) {
1350 [ + - + - : 1191 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
- + ]
1351 : 1191 : mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t *)pn_elif[i];
1352 : :
1353 : : // optimisation: don't emit anything when "if False"
1354 [ + + ]: 1191 : if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
1355 : 1183 : uint l_fail = comp_next_label(comp);
1356 : 1183 : c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1357 : :
1358 : 1183 : compile_node(comp, pns_elif->nodes[1]); // elif block
1359 : :
1360 : : // optimisation: skip everything else when "elif True"
1361 [ + + ]: 1183 : if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
1362 : 8 : goto done;
1363 : : }
1364 : :
1365 : 1175 : EMIT_ARG(jump, l_end);
1366 : 1175 : EMIT_ARG(label_assign, l_fail);
1367 : : }
1368 : : }
1369 : :
1370 : : // compile else block
1371 : 7329 : compile_node(comp, pns->nodes[3]); // can be null
1372 : :
1373 : 7521 : done:
1374 : 7521 : EMIT_ARG(label_assign, l_end);
1375 : 7521 : }
1376 : :
1377 : : #define START_BREAK_CONTINUE_BLOCK \
1378 : : uint16_t old_break_label = comp->break_label; \
1379 : : uint16_t old_continue_label = comp->continue_label; \
1380 : : uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
1381 : : uint break_label = comp_next_label(comp); \
1382 : : uint continue_label = comp_next_label(comp); \
1383 : : comp->break_label = break_label; \
1384 : : comp->continue_label = continue_label; \
1385 : : comp->break_continue_except_level = comp->cur_except_level;
1386 : :
1387 : : #define END_BREAK_CONTINUE_BLOCK \
1388 : : comp->break_label = old_break_label; \
1389 : : comp->continue_label = old_continue_label; \
1390 : : comp->break_continue_except_level = old_break_continue_except_level;
1391 : :
1392 : 1608 : STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1393 : 1608 : START_BREAK_CONTINUE_BLOCK
1394 : :
1395 [ + + ]: 1608 : if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1396 : 1568 : uint top_label = comp_next_label(comp);
1397 [ + + ]: 1568 : if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1398 : 826 : EMIT_ARG(jump, continue_label);
1399 : : }
1400 : 1568 : EMIT_ARG(label_assign, top_label);
1401 : 1568 : compile_node(comp, pns->nodes[1]); // body
1402 : 1568 : EMIT_ARG(label_assign, continue_label);
1403 : 1568 : c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1404 : : }
1405 : :
1406 : : // break/continue apply to outer loop (if any) in the else block
1407 : 1608 : END_BREAK_CONTINUE_BLOCK
1408 : :
1409 : 1608 : compile_node(comp, pns->nodes[2]); // else
1410 : :
1411 : 1608 : EMIT_ARG(label_assign, break_label);
1412 : 1608 : }
1413 : :
1414 : : // This function compiles an optimised for-loop of the form:
1415 : : // for <var> in range(<start>, <end>, <step>):
1416 : : // <body>
1417 : : // else:
1418 : : // <else>
1419 : : // <var> must be an identifier and <step> must be a small-int.
1420 : : //
1421 : : // Semantics of for-loop require:
1422 : : // - final failing value should not be stored in the loop variable
1423 : : // - if the loop never runs, the loop variable should never be assigned
1424 : : // - assignments to <var>, <end> or <step> in the body do not alter the loop
1425 : : // (<step> is a constant for us, so no need to worry about it changing)
1426 : : //
1427 : : // If <end> is a small-int, then the stack during the for-loop contains just
1428 : : // the current value of <var>. Otherwise, the stack contains <end> then the
1429 : : // current value of <var>.
1430 : 2143 : 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) {
1431 : 2143 : START_BREAK_CONTINUE_BLOCK
1432 : :
1433 : 2143 : uint top_label = comp_next_label(comp);
1434 : 2143 : uint entry_label = comp_next_label(comp);
1435 : :
1436 : : // put the end value on the stack if it's not a small-int constant
1437 : 2143 : bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1438 [ + + ]: 2143 : if (end_on_stack) {
1439 : 1053 : compile_node(comp, pn_end);
1440 : : }
1441 : :
1442 : : // compile: start
1443 : 2143 : compile_node(comp, pn_start);
1444 : :
1445 : 2143 : EMIT_ARG(jump, entry_label);
1446 : 2143 : EMIT_ARG(label_assign, top_label);
1447 : :
1448 : : // duplicate next value and store it to var
1449 : 2143 : EMIT(dup_top);
1450 : 2143 : c_assign(comp, pn_var, ASSIGN_STORE);
1451 : :
1452 : : // compile body
1453 : 2143 : compile_node(comp, pn_body);
1454 : :
1455 : 2143 : EMIT_ARG(label_assign, continue_label);
1456 : :
1457 : : // compile: var + step
1458 : 2143 : compile_node(comp, pn_step);
1459 : 2143 : EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
1460 : :
1461 : 2143 : EMIT_ARG(label_assign, entry_label);
1462 : :
1463 : : // compile: if var <cond> end: goto top
1464 [ + + ]: 2143 : if (end_on_stack) {
1465 : 1053 : EMIT(dup_top_two);
1466 : 1053 : EMIT(rot_two);
1467 : : } else {
1468 : 1090 : EMIT(dup_top);
1469 : 1090 : compile_node(comp, pn_end);
1470 : : }
1471 [ - + ]: 2143 : assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1472 [ + + ]: 2143 : if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
1473 : 2126 : EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
1474 : : } else {
1475 : 17 : EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
1476 : : }
1477 : 2143 : EMIT_ARG(pop_jump_if, true, top_label);
1478 : :
1479 : : // break/continue apply to outer loop (if any) in the else block
1480 : 2143 : END_BREAK_CONTINUE_BLOCK
1481 : :
1482 : : // Compile the else block. We must pop the iterator variables before
1483 : : // executing the else code because it may contain break/continue statements.
1484 : 2143 : uint end_label = 0;
1485 [ + + ]: 2143 : if (!MP_PARSE_NODE_IS_NULL(pn_else)) {
1486 : : // discard final value of "var", and possible "end" value
1487 : 32 : EMIT(pop_top);
1488 [ + + ]: 32 : if (end_on_stack) {
1489 : 8 : EMIT(pop_top);
1490 : : }
1491 : 32 : compile_node(comp, pn_else);
1492 : 32 : end_label = comp_next_label(comp);
1493 : 32 : EMIT_ARG(jump, end_label);
1494 : 32 : EMIT_ARG(adjust_stack_size, 1 + end_on_stack);
1495 : : }
1496 : :
1497 : 2143 : EMIT_ARG(label_assign, break_label);
1498 : :
1499 : : // discard final value of var that failed the loop condition
1500 : 2143 : EMIT(pop_top);
1501 : :
1502 : : // discard <end> value if it's on the stack
1503 [ + + ]: 2143 : if (end_on_stack) {
1504 : 1053 : EMIT(pop_top);
1505 : : }
1506 : :
1507 [ + + ]: 2143 : if (!MP_PARSE_NODE_IS_NULL(pn_else)) {
1508 : 32 : EMIT_ARG(label_assign, end_label);
1509 : : }
1510 : 2143 : }
1511 : :
1512 : 4949 : STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1513 : : // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1514 : : // this is actually slower, but uses no heap memory
1515 : : // for viper it will be much, much faster
1516 [ + + + - : 4949 : 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)) {
+ + + + ]
1517 : 2715 : mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t *)pns->nodes[1];
1518 [ + - ]: 2715 : if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1519 [ + + ]: 2715 : && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1520 [ + - ]: 2267 : && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pns_it->nodes[1]) == PN_trailer_paren) {
1521 : 2267 : mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t *)pns_it->nodes[1])->nodes[0];
1522 : 2267 : mp_parse_node_t *args;
1523 : 2267 : size_t n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
1524 : 2267 : mp_parse_node_t pn_range_start;
1525 : 2267 : mp_parse_node_t pn_range_end;
1526 : 2267 : mp_parse_node_t pn_range_step;
1527 : 2267 : bool optimize = false;
1528 [ + - ]: 2267 : if (1 <= n_args && n_args <= 3) {
1529 : 2267 : optimize = true;
1530 [ + + ]: 2267 : if (n_args == 1) {
1531 : 1742 : pn_range_start = mp_parse_node_new_small_int(0);
1532 : 1742 : pn_range_end = args[0];
1533 : 1742 : pn_range_step = mp_parse_node_new_small_int(1);
1534 [ + + ]: 525 : } else if (n_args == 2) {
1535 : 430 : pn_range_start = args[0];
1536 : 430 : pn_range_end = args[1];
1537 : 430 : pn_range_step = mp_parse_node_new_small_int(1);
1538 : : } else {
1539 : 95 : pn_range_start = args[0];
1540 : 95 : pn_range_end = args[1];
1541 : 95 : pn_range_step = args[2];
1542 : : // the step must be a non-zero constant integer to do the optimisation
1543 [ + + ]: 95 : if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)
1544 [ + + ]: 52 : || MP_PARSE_NODE_LEAF_SMALL_INT(pn_range_step) == 0) {
1545 : 52 : optimize = false;
1546 : : }
1547 : : }
1548 : : // arguments must be able to be compiled as standard expressions
1549 [ + + + + ]: 2267 : if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1550 : 17 : int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn_range_start);
1551 [ + + ]: 17 : if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1552 : 9 : optimize = false;
1553 : : }
1554 : : }
1555 [ + + + + ]: 2267 : if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1556 : 545 : int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pn_range_end);
1557 [ + + ]: 545 : if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1558 : 63 : optimize = false;
1559 : : }
1560 : : }
1561 : : }
1562 [ + + ]: 2267 : if (optimize) {
1563 : 2143 : compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1564 : 2143 : return;
1565 : : }
1566 : : }
1567 : : }
1568 : :
1569 : 2806 : START_BREAK_CONTINUE_BLOCK
1570 : 2806 : comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1571 : :
1572 : 2806 : uint pop_label = comp_next_label(comp);
1573 : :
1574 : 2806 : compile_node(comp, pns->nodes[1]); // iterator
1575 : 2806 : EMIT_ARG(get_iter, true);
1576 : 2806 : EMIT_ARG(label_assign, continue_label);
1577 : 2806 : EMIT_ARG(for_iter, pop_label);
1578 : 2806 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1579 : 2806 : compile_node(comp, pns->nodes[2]); // body
1580 : 2806 : EMIT_ARG(jump, continue_label);
1581 : 2806 : EMIT_ARG(label_assign, pop_label);
1582 : 2806 : EMIT(for_iter_end);
1583 : :
1584 : : // break/continue apply to outer loop (if any) in the else block
1585 : 2806 : END_BREAK_CONTINUE_BLOCK
1586 : :
1587 : 2806 : compile_node(comp, pns->nodes[3]); // else (may be empty)
1588 : :
1589 : 2806 : EMIT_ARG(label_assign, break_label);
1590 : : }
1591 : :
1592 : 49662 : 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) {
1593 : : // setup code
1594 : 49662 : uint l1 = comp_next_label(comp);
1595 : 49662 : uint success_label = comp_next_label(comp);
1596 : :
1597 : 49662 : compile_increase_except_level(comp, l1, MP_EMIT_SETUP_BLOCK_EXCEPT);
1598 : :
1599 : 49662 : compile_node(comp, pn_body); // body
1600 : 49662 : EMIT_ARG(pop_except_jump, success_label, false); // jump over exception handler
1601 : :
1602 : 49662 : EMIT_ARG(label_assign, l1); // start of exception handler
1603 : 49662 : EMIT(start_except_handler);
1604 : :
1605 : : // at this point the top of the stack contains the exception instance that was raised
1606 : :
1607 : 49662 : uint l2 = comp_next_label(comp);
1608 : :
1609 [ + + ]: 99407 : for (int i = 0; i < n_except; i++) {
1610 [ + - + - : 49749 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
- + ]
1611 : 49749 : mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t *)pn_excepts[i];
1612 : :
1613 : 49749 : qstr qstr_exception_local = 0;
1614 : 49749 : uint end_finally_label = comp_next_label(comp);
1615 : : #if MICROPY_PY_SYS_SETTRACE
1616 : : EMIT_ARG(set_source_line, pns_except->source_line);
1617 : : #endif
1618 : :
1619 [ + + ]: 49749 : if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
1620 : : // this is a catch all exception handler
1621 [ + + ]: 1516 : if (i + 1 != n_except) {
1622 : 4 : compile_syntax_error(comp, pn_excepts[i], MP_ERROR_TEXT("default 'except' must be last"));
1623 : 4 : compile_decrease_except_level(comp);
1624 : 4 : return;
1625 : : }
1626 : : } else {
1627 : : // this exception handler requires a match to a certain type of exception
1628 : 48233 : mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1629 [ + + ]: 48233 : if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1630 : 1945 : mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pns_exception_expr;
1631 [ + + ]: 1945 : if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
1632 : : // handler binds the exception to a local
1633 : 1438 : pns_exception_expr = pns3->nodes[0];
1634 : 1438 : qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
1635 : : }
1636 : : }
1637 : 48233 : EMIT(dup_top);
1638 : 48233 : compile_node(comp, pns_exception_expr);
1639 : 48233 : EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1640 : 48233 : EMIT_ARG(pop_jump_if, false, end_finally_label);
1641 : : }
1642 : :
1643 : : // either discard or store the exception instance
1644 [ + + ]: 48233 : if (qstr_exception_local == 0) {
1645 : 48307 : EMIT(pop_top);
1646 : : } else {
1647 : 1438 : compile_store_id(comp, qstr_exception_local);
1648 : : }
1649 : :
1650 : : // If the exception is bound to a variable <e> then the <body> of the
1651 : : // exception handler is wrapped in a try-finally so that the name <e> can
1652 : : // be deleted (per Python semantics) even if the <body> has an exception.
1653 : : // In such a case the generated code for the exception handler is:
1654 : : // try:
1655 : : // <body>
1656 : : // finally:
1657 : : // <e> = None
1658 : : // del <e>
1659 : 49745 : uint l3 = 0;
1660 [ + + ]: 49745 : if (qstr_exception_local != 0) {
1661 : 1438 : l3 = comp_next_label(comp);
1662 : 1438 : compile_increase_except_level(comp, l3, MP_EMIT_SETUP_BLOCK_FINALLY);
1663 : : }
1664 : 49745 : compile_node(comp, pns_except->nodes[1]); // the <body>
1665 [ + + ]: 49745 : if (qstr_exception_local != 0) {
1666 : 1438 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1667 : 1438 : EMIT_ARG(label_assign, l3);
1668 : 1438 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1669 : 1438 : compile_store_id(comp, qstr_exception_local);
1670 : 1438 : compile_delete_id(comp, qstr_exception_local);
1671 : 1438 : compile_decrease_except_level(comp);
1672 : : }
1673 : :
1674 : 49745 : EMIT_ARG(pop_except_jump, l2, true);
1675 : 49745 : EMIT_ARG(label_assign, end_finally_label);
1676 : 49745 : EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
1677 : : }
1678 : :
1679 : 49658 : compile_decrease_except_level(comp);
1680 : 49658 : EMIT(end_except_handler);
1681 : :
1682 : 49658 : EMIT_ARG(label_assign, success_label);
1683 : 49658 : compile_node(comp, pn_else); // else block, can be null
1684 : 49658 : EMIT_ARG(label_assign, l2);
1685 : : }
1686 : :
1687 : 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) {
1688 : 983 : uint l_finally_block = comp_next_label(comp);
1689 : :
1690 : 983 : compile_increase_except_level(comp, l_finally_block, MP_EMIT_SETUP_BLOCK_FINALLY);
1691 : :
1692 [ + + ]: 983 : if (n_except == 0) {
1693 [ - + ]: 891 : assert(MP_PARSE_NODE_IS_NULL(pn_else));
1694 : 891 : EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
1695 : 891 : compile_node(comp, pn_body);
1696 : 891 : EMIT_ARG(adjust_stack_size, -3);
1697 : : } else {
1698 : 92 : compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1699 : : }
1700 : 983 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1701 : 983 : EMIT_ARG(label_assign, l_finally_block);
1702 : 983 : compile_node(comp, pn_finally);
1703 : :
1704 : 983 : compile_decrease_except_level(comp);
1705 : 983 : }
1706 : :
1707 : 50553 : STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1708 [ + - - + ]: 50553 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1709 : : {
1710 : 50553 : mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)pns->nodes[1];
1711 [ + + ]: 50553 : if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
1712 : : // just try-finally
1713 : 891 : compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1714 [ + + ]: 49662 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
1715 : : // try-except and possibly else and/or finally
1716 : 464 : mp_parse_node_t *pn_excepts;
1717 : 464 : size_t n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
1718 [ + + ]: 464 : if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
1719 : : // no finally
1720 : 372 : compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1721 : : } else {
1722 : : // have finally
1723 : 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]);
1724 : : }
1725 : : } else {
1726 : : // just try-except
1727 : 49198 : mp_parse_node_t *pn_excepts;
1728 : 49198 : size_t n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
1729 : 49198 : compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
1730 : : }
1731 : : }
1732 : 50553 : }
1733 : :
1734 : 73656 : STATIC void compile_with_stmt_helper(compiler_t *comp, size_t n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1735 [ + + ]: 73656 : if (n == 0) {
1736 : : // no more pre-bits, compile the body of the with
1737 : 36828 : compile_node(comp, body);
1738 : : } else {
1739 : 36828 : uint l_end = comp_next_label(comp);
1740 [ + - + + : 73442 : if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
+ + ]
1741 : : // this pre-bit is of the form "a as b"
1742 : 36614 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)nodes[0];
1743 : 36614 : compile_node(comp, pns->nodes[0]);
1744 : 36614 : compile_increase_except_level(comp, l_end, MP_EMIT_SETUP_BLOCK_WITH);
1745 : 36614 : c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1746 : : } else {
1747 : : // this pre-bit is just an expression
1748 : 214 : compile_node(comp, nodes[0]);
1749 : 214 : compile_increase_except_level(comp, l_end, MP_EMIT_SETUP_BLOCK_WITH);
1750 : 214 : EMIT(pop_top);
1751 : : }
1752 : : // compile additional pre-bits and the body
1753 : 36828 : compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1754 : : // finish this with block
1755 : 36828 : EMIT_ARG(with_cleanup, l_end);
1756 : 36828 : reserve_labels_for_native(comp, 3); // used by native's with_cleanup
1757 : 36828 : compile_decrease_except_level(comp);
1758 : : }
1759 : 73656 : }
1760 : :
1761 : 36828 : STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1762 : : // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1763 : 36828 : mp_parse_node_t *nodes;
1764 : 36828 : size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1765 [ - + ]: 36828 : assert(n > 0);
1766 : :
1767 : : // compile in a nested fashion
1768 : 36828 : compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1769 : 36828 : }
1770 : :
1771 : 1840 : STATIC void compile_yield_from(compiler_t *comp) {
1772 : 1840 : EMIT_ARG(get_iter, false);
1773 : 1840 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1774 : 1840 : EMIT_ARG(yield, MP_EMIT_YIELD_FROM);
1775 : 1836 : reserve_labels_for_native(comp, 3);
1776 : 1836 : }
1777 : :
1778 : : #if MICROPY_PY_ASYNC_AWAIT
1779 : 72 : STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1780 : 72 : EMIT_ARG(load_method, method, false);
1781 : 72 : EMIT_ARG(call_method, 0, 0, 0);
1782 : 72 : compile_yield_from(comp);
1783 : 72 : }
1784 : :
1785 : 16 : STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1786 : : // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1787 : :
1788 : 16 : qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1789 : 16 : uint while_else_label = comp_next_label(comp);
1790 : 16 : uint try_exception_label = comp_next_label(comp);
1791 : 16 : uint try_else_label = comp_next_label(comp);
1792 : 16 : uint try_finally_label = comp_next_label(comp);
1793 : :
1794 : 16 : compile_node(comp, pns->nodes[1]); // iterator
1795 : 16 : EMIT_ARG(load_method, MP_QSTR___aiter__, false);
1796 : 16 : EMIT_ARG(call_method, 0, 0, 0);
1797 : 16 : compile_store_id(comp, context);
1798 : :
1799 : 16 : START_BREAK_CONTINUE_BLOCK
1800 : :
1801 : 16 : EMIT_ARG(label_assign, continue_label);
1802 : :
1803 : 16 : compile_increase_except_level(comp, try_exception_label, MP_EMIT_SETUP_BLOCK_EXCEPT);
1804 : :
1805 : 16 : compile_load_id(comp, context);
1806 : 16 : compile_await_object_method(comp, MP_QSTR___anext__);
1807 : 16 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1808 : 16 : EMIT_ARG(pop_except_jump, try_else_label, false);
1809 : :
1810 : 16 : EMIT_ARG(label_assign, try_exception_label);
1811 : 16 : EMIT(start_except_handler);
1812 : 16 : EMIT(dup_top);
1813 : 16 : EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1814 : 16 : EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1815 : 16 : EMIT_ARG(pop_jump_if, false, try_finally_label);
1816 : 16 : EMIT(pop_top); // pop exception instance
1817 : 16 : EMIT_ARG(pop_except_jump, while_else_label, true);
1818 : :
1819 : 16 : EMIT_ARG(label_assign, try_finally_label);
1820 : 16 : EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
1821 : 16 : compile_decrease_except_level(comp);
1822 : 16 : EMIT(end_except_handler);
1823 : :
1824 : 16 : EMIT_ARG(label_assign, try_else_label);
1825 : 16 : compile_node(comp, pns->nodes[2]); // body
1826 : :
1827 : 16 : EMIT_ARG(jump, continue_label);
1828 : : // break/continue apply to outer loop (if any) in the else block
1829 : 16 : END_BREAK_CONTINUE_BLOCK
1830 : :
1831 : 16 : EMIT_ARG(label_assign, while_else_label);
1832 : 16 : compile_node(comp, pns->nodes[3]); // else
1833 : :
1834 : 16 : EMIT_ARG(label_assign, break_label);
1835 : 16 : }
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 : 1184 : STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1960 [ + - - + ]: 1184 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1961 : 1184 : mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns->nodes[0];
1962 [ + + ]: 1184 : if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1963 : : // async def
1964 : 1104 : compile_funcdef(comp, pns0);
1965 : 1104 : scope_t *fscope = (scope_t *)pns0->nodes[4];
1966 : 1104 : fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1967 : : } else {
1968 : : // async for/with; first verify the scope is a generator
1969 : 80 : int scope_flags = comp->scope_cur->scope_flags;
1970 [ + + ]: 80 : 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 [ + + ]: 72 : if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1977 : : // async for
1978 : 16 : 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 : 164106 : STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1989 : 164106 : mp_parse_node_t pn_rhs = pns->nodes[1];
1990 [ + + ]: 164106 : if (MP_PARSE_NODE_IS_NULL(pn_rhs)) {
1991 [ + + + + ]: 127202 : 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 [ + + + - ]: 126746 : if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2001 [ + - + + : 126746 : || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
+ - ]
2002 : : // do nothing with a lonely constant
2003 : : } else {
2004 : 126746 : compile_node(comp, pns->nodes[0]); // just an expression
2005 : 126738 : EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2006 : : }
2007 : : }
2008 [ + + ]: 36904 : } else if (MP_PARSE_NODE_IS_STRUCT(pn_rhs)) {
2009 : 22939 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pn_rhs;
2010 : 22939 : int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
2011 [ + + ]: 22939 : 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 [ + + ]: 22919 : } else if (kind == PN_expr_stmt_augassign) {
2028 : 1563 : c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2029 : 1563 : compile_node(comp, pns1->nodes[1]); // rhs
2030 [ - + ]: 1563 : assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
2031 : 1563 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
2032 : 1563 : mp_binary_op_t op = MP_BINARY_OP_INPLACE_OR + (tok - MP_TOKEN_DEL_PIPE_EQUAL);
2033 : 1563 : EMIT_ARG(binary_op, op);
2034 : 1563 : c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2035 [ + + ]: 21356 : } 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 : 21228 : plain_assign:
2051 : : #if MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
2052 [ + - + + : 35201 : 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 : 35133 : compile_node(comp, pn_rhs); // rhs
2096 : 35133 : c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2097 : : }
2098 : : } else {
2099 : 13965 : goto plain_assign;
2100 : : }
2101 : : }
2102 : :
2103 : 319 : STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2104 [ + - + - : 319 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
- + ]
2105 : 319 : mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t *)pns->nodes[1];
2106 : :
2107 : 319 : uint l_fail = comp_next_label(comp);
2108 : 319 : uint l_end = comp_next_label(comp);
2109 : 319 : c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2110 : 319 : compile_node(comp, pns->nodes[0]); // success value
2111 : 319 : EMIT_ARG(jump, l_end);
2112 : 319 : EMIT_ARG(label_assign, l_fail);
2113 : 319 : EMIT_ARG(adjust_stack_size, -1); // adjust stack size
2114 : 319 : compile_node(comp, pns_test_if_else->nodes[1]); // failure value
2115 : 319 : EMIT_ARG(label_assign, l_end);
2116 : 319 : }
2117 : :
2118 : 575 : STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
2119 [ + + ]: 575 : if (comp->pass == MP_PASS_SCOPE) {
2120 : : // create a new scope for this lambda
2121 : 149 : 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 : 149 : pns->nodes[2] = (mp_parse_node_t)s;
2124 : : }
2125 : :
2126 : : // get the scope for this lambda
2127 : 575 : scope_t *this_scope = (scope_t *)pns->nodes[2];
2128 : :
2129 : : // compile the lambda definition
2130 : 575 : compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
2131 : 575 : }
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 : 469 : STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2172 : 469 : bool cond = MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test;
2173 : 469 : uint l_end = comp_next_label(comp);
2174 : 469 : int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2175 [ + + ]: 1407 : for (int i = 0; i < n; i += 1) {
2176 : 938 : compile_node(comp, pns->nodes[i]);
2177 [ + + ]: 938 : if (i + 1 < n) {
2178 : 469 : EMIT_ARG(jump_if_or_pop, cond, l_end);
2179 : : }
2180 : : }
2181 : 469 : EMIT_ARG(label_assign, l_end);
2182 : 469 : }
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 : 15593 : STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
2190 : 15593 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2191 : 15593 : compile_node(comp, pns->nodes[0]);
2192 : 15593 : bool multi = (num_nodes > 3);
2193 : 15593 : uint l_fail = 0;
2194 [ + + ]: 15593 : if (multi) {
2195 : 238 : l_fail = comp_next_label(comp);
2196 : : }
2197 [ + + ]: 31432 : for (int i = 1; i + 1 < num_nodes; i += 2) {
2198 : 15839 : compile_node(comp, pns->nodes[i + 1]);
2199 [ + + ]: 15839 : if (i + 2 < num_nodes) {
2200 : 246 : EMIT(dup_top);
2201 : 246 : EMIT(rot_three);
2202 : : }
2203 [ + + ]: 15839 : if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
2204 : 10763 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]);
2205 : 10763 : mp_binary_op_t op;
2206 [ + + ]: 10763 : if (tok == MP_TOKEN_KW_IN) {
2207 : : op = MP_BINARY_OP_IN;
2208 : : } else {
2209 : 9962 : op = MP_BINARY_OP_LESS + (tok - MP_TOKEN_OP_LESS);
2210 : : }
2211 : 10763 : EMIT_ARG(binary_op, op);
2212 : : } else {
2213 [ + - - + ]: 5076 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
2214 : 5076 : mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)pns->nodes[i];
2215 : 5076 : int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
2216 [ + + ]: 5076 : if (kind == PN_comp_op_not_in) {
2217 : 376 : EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
2218 : : } else {
2219 [ - + ]: 4700 : assert(kind == PN_comp_op_is); // should be
2220 [ + + ]: 4700 : if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
2221 : 3256 : EMIT_ARG(binary_op, MP_BINARY_OP_IS);
2222 : : } else {
2223 : 1444 : EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
2224 : : }
2225 : : }
2226 : : }
2227 [ + + ]: 15839 : if (i + 2 < num_nodes) {
2228 : 246 : EMIT_ARG(jump_if_or_pop, false, l_fail);
2229 : : }
2230 : : }
2231 [ + + ]: 15593 : if (multi) {
2232 : 238 : uint l_end = comp_next_label(comp);
2233 : 238 : EMIT_ARG(jump, l_end);
2234 : 238 : EMIT_ARG(label_assign, l_fail);
2235 : 238 : EMIT_ARG(adjust_stack_size, 1);
2236 : 238 : EMIT(rot_two);
2237 : 238 : EMIT(pop_top);
2238 : 238 : EMIT_ARG(label_assign, l_end);
2239 : : }
2240 : 15593 : }
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 : 1701 : STATIC void compile_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns) {
2247 : 1701 : MP_STATIC_ASSERT(MP_BINARY_OP_OR + PN_xor_expr - PN_expr == MP_BINARY_OP_XOR);
2248 : 1701 : MP_STATIC_ASSERT(MP_BINARY_OP_OR + PN_and_expr - PN_expr == MP_BINARY_OP_AND);
2249 : 1701 : mp_binary_op_t binary_op = MP_BINARY_OP_OR + MP_PARSE_NODE_STRUCT_KIND(pns) - PN_expr;
2250 : 1701 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2251 : 1701 : compile_node(comp, pns->nodes[0]);
2252 [ + + ]: 3466 : for (int i = 1; i < num_nodes; ++i) {
2253 : 1765 : compile_node(comp, pns->nodes[i]);
2254 : 1765 : EMIT_ARG(binary_op, binary_op);
2255 : : }
2256 : 1701 : }
2257 : :
2258 : 10915 : STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2259 : 10915 : int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2260 : 10915 : compile_node(comp, pns->nodes[0]);
2261 [ + + ]: 22832 : for (int i = 1; i + 1 < num_nodes; i += 2) {
2262 : 11917 : compile_node(comp, pns->nodes[i + 1]);
2263 : 11917 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]);
2264 : 11917 : mp_binary_op_t op = MP_BINARY_OP_LSHIFT + (tok - MP_TOKEN_OP_DBL_LESS);
2265 : 11917 : EMIT_ARG(binary_op, op);
2266 : : }
2267 : 10915 : }
2268 : :
2269 : 3235 : STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
2270 : 3235 : compile_node(comp, pns->nodes[1]);
2271 : 3235 : mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2272 : 3235 : mp_unary_op_t op;
2273 [ + + ]: 3235 : if (tok == MP_TOKEN_OP_TILDE) {
2274 : : op = MP_UNARY_OP_INVERT;
2275 : : } else {
2276 [ - + ]: 2657 : assert(tok == MP_TOKEN_OP_PLUS || tok == MP_TOKEN_OP_MINUS);
2277 : : op = MP_UNARY_OP_POSITIVE + (tok - MP_TOKEN_OP_PLUS);
2278 : : }
2279 : 3235 : EMIT_ARG(unary_op, op);
2280 : 3235 : }
2281 : :
2282 : 239444 : STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
2283 : : // compile the subject of the expression
2284 : 239444 : compile_node(comp, pns->nodes[0]);
2285 : :
2286 : : // compile_atom_expr_await may call us with a NULL node
2287 [ + + ]: 239444 : 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 : 239116 : size_t num_trail = 1;
2293 : 239116 : mp_parse_node_struct_t **pns_trail = (mp_parse_node_struct_t **)&pns->nodes[1];
2294 [ + + ]: 239116 : if (MP_PARSE_NODE_STRUCT_KIND(pns_trail[0]) == PN_atom_expr_trailers) {
2295 : 113192 : num_trail = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_trail[0]);
2296 : 113192 : pns_trail = (mp_parse_node_struct_t **)&pns_trail[0]->nodes[0];
2297 : : }
2298 : :
2299 : : // the current index into the array of trailers
2300 : 239116 : size_t i = 0;
2301 : :
2302 : : // handle special super() call
2303 [ + + ]: 239116 : if (comp->scope_cur->kind == SCOPE_FUNCTION
2304 [ + + ]: 50103 : && MP_PARSE_NODE_IS_ID(pns->nodes[0])
2305 [ + + ]: 49653 : && 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 [ + + ]: 238978 : } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
2348 [ + + ]: 232849 : && 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 [ + + ]: 631587 : for (; i < num_trail; i++) {
2362 [ + + ]: 392481 : if (i + 1 < num_trail
2363 [ + + ]: 190053 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[i]) == PN_trailer_period
2364 [ + + ]: 78932 : && MP_PARSE_NODE_STRUCT_KIND(pns_trail[i + 1]) == PN_trailer_paren) {
2365 : : // optimisation for method calls a.f(...), following PyPy
2366 : 36936 : mp_parse_node_struct_t *pns_period = pns_trail[i];
2367 : 36936 : mp_parse_node_struct_t *pns_paren = pns_trail[i + 1];
2368 : 36936 : EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0]), false);
2369 : 36936 : compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
2370 : 36936 : i += 1;
2371 : : } else {
2372 : : // node is one of: trailer_paren, trailer_bracket, trailer_period
2373 : 355545 : 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 : 367375 : 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 : 367375 : mp_parse_node_t *args;
2388 : 367375 : 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 : 367375 : int n_positional = n_positional_extra;
2395 : 367375 : uint n_keyword = 0;
2396 : 367375 : uint star_flags = 0;
2397 : 367375 : mp_uint_t star_args = 0;
2398 [ + + ]: 553067 : for (size_t i = 0; i < n_args; i++) {
2399 [ - + + + ]: 185730 : if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2400 : 71693 : mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t *)args[i];
2401 [ + + ]: 71693 : if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2402 [ + + ]: 1007 : 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 [ + + ]: 999 : 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 : 981 : star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2418 : 981 : star_args |= (mp_uint_t)1 << i;
2419 : 981 : compile_node(comp, pns_arg->nodes[0]);
2420 : 981 : n_positional++;
2421 [ + + ]: 70686 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2422 : 376 : star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2423 : : // double-star args are stored as kw arg with key of None
2424 : 376 : EMIT(load_null);
2425 : 376 : compile_node(comp, pns_arg->nodes[0]);
2426 : 376 : n_keyword++;
2427 [ + + ]: 70310 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2428 : : #if MICROPY_PY_ASSIGN_EXPR
2429 [ + - + + : 2665 : 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 [ + - + + : 2649 : if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
+ + ]
2435 [ + + ]: 2564 : 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 : 2560 : EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
2440 : 2560 : compile_node(comp, pns_arg->nodes[1]);
2441 : 2560 : n_keyword++;
2442 : : } else {
2443 : 85 : compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2444 : 85 : n_positional++;
2445 : : }
2446 : : } else {
2447 : 67645 : goto normal_argument;
2448 : : }
2449 : : } else {
2450 : 114037 : normal_argument:
2451 [ + + ]: 181682 : 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 [ + + ]: 181678 : 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 : 181674 : compile_node(comp, args[i]);
2460 : 181674 : n_positional++;
2461 : : }
2462 : : }
2463 : :
2464 [ + + ]: 367337 : if (star_flags != 0) {
2465 : : // one extra object that contains the star_args map
2466 : 1153 : EMIT_ARG(load_const_small_int, star_args);
2467 : : }
2468 : :
2469 : : // emit the function/method call
2470 [ + + ]: 367337 : if (is_method_call) {
2471 : 37052 : EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
2472 : : } else {
2473 : 330285 : 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 : 744 : STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2479 [ - + ]: 744 : assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2480 [ + - + - : 744 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
- + ]
2481 : 744 : mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t *)pns->nodes[1];
2482 : :
2483 [ + + ]: 744 : if (comp->pass == MP_PASS_SCOPE) {
2484 : : // create a new scope for this comprehension
2485 : 197 : 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 : 197 : pns_comp_for->nodes[3] = (mp_parse_node_t)s;
2488 : : }
2489 : :
2490 : : // get the scope for this comprehension
2491 : 744 : scope_t *this_scope = (scope_t *)pns_comp_for->nodes[3];
2492 : :
2493 : : // compile the comprehension
2494 : 744 : close_over_variables_etc(comp, this_scope, 0, 0);
2495 : :
2496 : 744 : compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2497 [ + + ]: 744 : if (kind == SCOPE_GEN_EXPR) {
2498 : 240 : EMIT_ARG(get_iter, false);
2499 : : }
2500 : 744 : EMIT_ARG(call_function, 1, 0, 0);
2501 : 744 : }
2502 : :
2503 : 4411 : STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2504 [ + + ]: 4411 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2505 : : // an empty tuple
2506 : 891 : EMIT_ARG(build, 0, MP_EMIT_BUILD_TUPLE);
2507 : : } else {
2508 [ + - - + ]: 3520 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
2509 : 3520 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
2510 [ + + + - : 3520 : 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 : 3365 : compile_generic_tuple(comp, pns);
2516 : : }
2517 : : }
2518 : 4411 : }
2519 : :
2520 : 5931 : STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2521 [ + + ]: 5931 : if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2522 : : // empty list
2523 : 1184 : 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 : 424 : compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2529 : : } else {
2530 : : // list with N items
2531 : 3074 : compile_generic_all_nodes(comp, pns2);
2532 : 3074 : 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 : 5931 : }
2540 : :
2541 : 3337 : STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool create_map) {
2542 : 3337 : mp_parse_node_t pn = pns->nodes[0];
2543 [ + + ]: 3337 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2544 : : // empty dict
2545 [ + - ]: 777 : if (create_map) {
2546 : 777 : EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
2547 : : }
2548 [ + + ]: 2560 : } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2549 : 2298 : pns = (mp_parse_node_struct_t *)pn;
2550 [ + + ]: 2298 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
2551 : : // dict with one element
2552 [ + - ]: 953 : if (create_map) {
2553 : 953 : EMIT_ARG(build, 1, MP_EMIT_BUILD_MAP);
2554 : : }
2555 : 953 : compile_node(comp, pn);
2556 : 953 : EMIT(store_map);
2557 [ + + ]: 1345 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2558 [ + - - + ]: 1337 : assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2559 : 1337 : mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
2560 [ + + ]: 1337 : if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
2561 : : // dict/set with multiple elements
2562 : :
2563 : : // get tail elements (2nd, 3rd, ...)
2564 : 1257 : mp_parse_node_t *nodes;
2565 : 1257 : 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 : 1257 : bool is_dict;
2569 [ + - + + : 1257 : 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 : 324 : compile_node(comp, pns->nodes[0]); // 1st value of set
2580 : 324 : is_dict = false;
2581 : : }
2582 : :
2583 : : // process rest of elements
2584 [ + + ]: 5150 : for (size_t i = 0; i < n; i++) {
2585 : 3901 : mp_parse_node_t pn_i = nodes[i];
2586 [ + - + + : 3901 : bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
+ + ]
2587 : 3901 : compile_node(comp, pn_i);
2588 [ + + ]: 3901 : if (is_dict) {
2589 [ + + ]: 3305 : 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 : 3301 : EMIT(store_map);
2598 : : } else {
2599 [ + + ]: 596 : 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 [ + + ]: 1249 : if (!is_dict) {
2613 : 320 : 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 : 3329 : STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2644 : 3329 : compile_atom_brace_helper(comp, pns, true);
2645 : 3329 : }
2646 : :
2647 : 326627 : STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2648 : 326627 : compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
2649 : 326619 : }
2650 : :
2651 : 9719 : 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 : 9719 : compile_node(comp, pns->nodes[0]); // the index
2654 : 9719 : EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
2655 : 9719 : }
2656 : :
2657 : 20750 : 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 : 20750 : EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), MP_EMIT_ATTR_LOAD); // attribute to get
2660 : 20750 : }
2661 : :
2662 : : #if MICROPY_PY_BUILTINS_SLICE
2663 : 2338 : STATIC void compile_subscript(compiler_t *comp, mp_parse_node_struct_t *pns) {
2664 [ + + ]: 2338 : 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 : 882 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2671 : : }
2672 : :
2673 [ - + ]: 2338 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2674 : 2338 : mp_parse_node_t pn = pns->nodes[0];
2675 [ + + ]: 2338 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2676 : : // [?:]
2677 : 473 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2678 : 473 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2679 [ + + ]: 1865 : } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2680 : 615 : pns = (mp_parse_node_struct_t *)pn;
2681 [ + + ]: 615 : if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2682 : 168 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2683 : 168 : pn = pns->nodes[0];
2684 [ + + ]: 168 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2685 : : // [?::]
2686 : 24 : 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 : 1250 : compile_node(comp, pn);
2713 : 1250 : EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
2714 : : }
2715 : 2338 : }
2716 : : #endif // MICROPY_PY_BUILTINS_SLICE
2717 : :
2718 : 5263 : 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 : 5263 : compile_node(comp, pns->nodes[1]); // value
2721 : 5263 : compile_node(comp, pns->nodes[0]); // key
2722 : 5263 : }
2723 : :
2724 : 3684 : STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
2725 : 3684 : qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
2726 : : // store class object into class name
2727 : 3684 : compile_store_id(comp, cname);
2728 : 3684 : }
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 : 1392 : STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2752 [ + + ]: 1392 : 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 : 1388 : compile_atom_expr_normal(comp, pns);
2757 : 1388 : compile_yield_from(comp);
2758 : : }
2759 : : #endif
2760 : :
2761 : 28961 : STATIC mp_obj_t get_const_object(mp_parse_node_struct_t *pns) {
2762 : 28961 : return mp_parse_node_extract_const_object(pns);
2763 : : }
2764 : :
2765 : 28961 : STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2766 : 28961 : EMIT_ARG(load_const_obj, get_const_object(pns));
2767 : 28961 : }
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 : 1653540 : STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2783 [ + + ]: 1653540 : if (MP_PARSE_NODE_IS_NULL(pn)) {
2784 : : // pass
2785 [ + + ]: 1594714 : } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2786 : 87362 : mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2787 : 87362 : EMIT_ARG(load_const_small_int, arg);
2788 [ + + ]: 1507352 : } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
2789 : 441897 : uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
2790 [ + + + ]: 441897 : switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
2791 : 371697 : case MP_PARSE_NODE_ID:
2792 : 371697 : compile_load_id(comp, arg);
2793 : 371697 : break;
2794 : 49607 : case MP_PARSE_NODE_STRING:
2795 : 49607 : EMIT_ARG(load_const_str, arg);
2796 : 49607 : break;
2797 : 20593 : case MP_PARSE_NODE_TOKEN:
2798 : : default:
2799 [ + + ]: 20593 : 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 : 14438 : EMIT_ARG(load_const_tok, arg);
2805 : : }
2806 : : break;
2807 : : }
2808 : : } else {
2809 : 1065455 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
2810 : 1065455 : EMIT_ARG(set_source_line, pns->source_line);
2811 [ - + ]: 1065455 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) <= PN_const_object);
2812 : 1065455 : compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2813 : 1065455 : f(comp, pns);
2814 : : }
2815 : 1653496 : }
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 : 5572 : 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 : 5572 : (void)pn_dbl_star;
2838 : :
2839 : : // check that **kw is last
2840 [ + + ]: 5572 : 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 : 5564 : qstr param_name = MP_QSTRnull;
2846 : 5564 : uint param_flag = ID_FLAG_IS_PARAM;
2847 : 5564 : mp_parse_node_struct_t *pns = NULL;
2848 [ + + ]: 5564 : if (MP_PARSE_NODE_IS_ID(pn)) {
2849 : 4692 : param_name = MP_PARSE_NODE_LEAF_ARG(pn);
2850 [ + + ]: 4692 : 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 : 4664 : comp->scope_cur->num_pos_args += 1;
2856 : : }
2857 : : } else {
2858 [ + - - + ]: 872 : assert(MP_PARSE_NODE_IS_STRUCT(pn));
2859 : 872 : pns = (mp_parse_node_struct_t *)pn;
2860 [ + + ]: 872 : 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 [ + + ]: 175 : } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2871 [ + + ]: 131 : 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 : 123 : comp->have_star = true;
2877 : 123 : param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
2878 [ + + ]: 123 : 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 [ + + ]: 99 : } 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 [ + - - + ]: 79 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
2890 : : // named star with possible annotation
2891 : 79 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
2892 : 79 : pns = (mp_parse_node_struct_t *)pns->nodes[0];
2893 : 79 : param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2894 : : }
2895 : : } else {
2896 : : // double star with possible annotation
2897 [ - + ]: 44 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
2898 : 44 : param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2899 : 44 : param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
2900 : 44 : comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
2901 : : }
2902 : : }
2903 : :
2904 [ + + ]: 5556 : if (param_name != MP_QSTRnull) {
2905 : 5532 : id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, ID_INFO_KIND_UNDECIDED);
2906 [ + + ]: 5532 : 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 : 5528 : id_info->kind = ID_INFO_KIND_LOCAL;
2911 : 5528 : id_info->flags = param_flag;
2912 : :
2913 : : #if MICROPY_EMIT_NATIVE
2914 [ + + + + ]: 5528 : 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 : 5423 : STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
2924 : 5423 : compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
2925 : 5423 : }
2926 : :
2927 : 149 : STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
2928 : 149 : compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2929 : 149 : }
2930 : :
2931 : 748 : 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 : 748 : uint l_top = comp_next_label(comp);
2933 : 748 : uint l_end = comp_next_label(comp);
2934 : 748 : EMIT_ARG(label_assign, l_top);
2935 : 748 : EMIT_ARG(for_iter, l_end);
2936 : 748 : c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2937 : 748 : mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2938 : :
2939 : 804 : tail_recursion:
2940 [ + + ]: 804 : if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
2941 : : // no more nested if/for; compile inner expression
2942 : 740 : compile_node(comp, pn_inner_expr);
2943 [ + + ]: 740 : if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
2944 : 236 : EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
2945 : 236 : reserve_labels_for_native(comp, 1);
2946 : 236 : EMIT(pop_top);
2947 : : } else {
2948 : 504 : 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 : 748 : EMIT_ARG(jump, l_top);
2966 : 748 : EMIT_ARG(label_assign, l_end);
2967 : 748 : EMIT(for_iter_end);
2968 : 748 : }
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 : 35677 : STATIC bool compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
3016 : 35677 : comp->pass = pass;
3017 : 35677 : comp->scope_cur = scope;
3018 : 35677 : comp->next_label = 0;
3019 : 35677 : mp_emit_common_start_pass(&comp->emit_common, pass);
3020 : 35677 : EMIT_ARG(start_pass, pass, scope);
3021 : 35677 : reserve_labels_for_native(comp, 6); // used by native's start_pass
3022 : :
3023 [ + + ]: 35677 : if (comp->pass == MP_PASS_SCOPE) {
3024 : : // reset maximum stack sizes in scope
3025 : : // they will be computed in this first pass
3026 : 9170 : scope->stack_size = 0;
3027 : 9170 : scope->exc_stack_size = 0;
3028 : : }
3029 : :
3030 : : // compile
3031 [ + - + + : 36911 : 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 [ + + ]: 34443 : } else if (scope->kind == SCOPE_MODULE) {
3037 : 12438 : if (!comp->is_repl) {
3038 : : check_for_doc_string(comp, scope->pn);
3039 : : }
3040 : 12438 : compile_node(comp, scope->pn);
3041 : 12434 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3042 : 12434 : EMIT(return_value);
3043 [ + + ]: 22005 : } else if (scope->kind == SCOPE_FUNCTION) {
3044 [ + - - + ]: 17011 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3045 : 17011 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3046 [ - + ]: 17011 : 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 [ + + ]: 17011 : if (comp->pass == MP_PASS_SCOPE) {
3051 : 4328 : comp->have_star = false;
3052 : 4328 : apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
3053 : :
3054 : : #if MICROPY_EMIT_NATIVE
3055 [ + + ]: 4328 : 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 : 17011 : compile_node(comp, pns->nodes[3]); // 3 is function body
3063 : 16995 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3064 : 16995 : EMIT(return_value);
3065 [ + + ]: 4994 : } else if (scope->kind == SCOPE_LAMBDA) {
3066 [ + - - + ]: 572 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3067 : 572 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3068 [ - + ]: 572 : assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
3069 : :
3070 : : // Set the source line number for the start of the lambda
3071 : 572 : 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 [ + + ]: 572 : if (comp->pass == MP_PASS_SCOPE) {
3076 : 149 : comp->have_star = false;
3077 : 149 : apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3078 : : }
3079 : :
3080 : 572 : 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 [ + + ]: 572 : 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 : 572 : EMIT(return_value);
3088 [ + + ]: 4422 : } else if (SCOPE_IS_COMP_LIKE(scope->kind)) {
3089 : : // a bit of a hack at the moment
3090 : :
3091 [ + - - + ]: 740 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3092 : 740 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3093 [ - + ]: 740 : assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3094 [ + - + - : 740 : assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
- + ]
3095 : 740 : 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 : 740 : qstr qstr_arg = MP_QSTR_;
3102 [ + + ]: 740 : if (comp->pass == MP_PASS_SCOPE) {
3103 : 197 : scope_find_or_add_id(comp->scope_cur, qstr_arg, ID_INFO_KIND_LOCAL);
3104 : 197 : scope->num_pos_args = 1;
3105 : : }
3106 : :
3107 : : // Set the source line number for the start of the comprehension
3108 : 740 : EMIT_ARG(set_source_line, pns->source_line);
3109 : :
3110 [ + + ]: 740 : if (scope->kind == SCOPE_LIST_COMP) {
3111 : 424 : EMIT_ARG(build, 0, MP_EMIT_BUILD_LIST);
3112 [ + + ]: 316 : } else if (scope->kind == SCOPE_DICT_COMP) {
3113 : 72 : EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
3114 : : #if MICROPY_PY_BUILTINS_SET
3115 [ + + ]: 244 : } 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 [ + + ]: 740 : if (scope->kind == SCOPE_GEN_EXPR) {
3123 : 236 : MP_STATIC_ASSERT(MP_OBJ_ITER_BUF_NSLOTS == 4);
3124 : 236 : EMIT(load_null);
3125 : 236 : compile_load_id(comp, qstr_arg);
3126 : 236 : EMIT(load_null);
3127 : 236 : EMIT(load_null);
3128 : : } else {
3129 : 504 : compile_load_id(comp, qstr_arg);
3130 : 504 : EMIT_ARG(get_iter, true);
3131 : : }
3132 : :
3133 : 740 : compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
3134 : :
3135 [ + + ]: 740 : if (scope->kind == SCOPE_GEN_EXPR) {
3136 : 236 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3137 : : }
3138 : 740 : EMIT(return_value);
3139 : : } else {
3140 [ - + ]: 3682 : assert(scope->kind == SCOPE_CLASS);
3141 [ + - - + ]: 3682 : assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3142 : 3682 : mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;
3143 [ - + ]: 3682 : assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
3144 : :
3145 [ + + ]: 3682 : if (comp->pass == MP_PASS_SCOPE) {
3146 : 922 : 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 : 3682 : compile_load_id(comp, MP_QSTR___name__);
3153 : 3682 : compile_store_id(comp, MP_QSTR___module__);
3154 : 3682 : EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
3155 : 3682 : compile_store_id(comp, MP_QSTR___qualname__);
3156 : :
3157 : 3682 : check_for_doc_string(comp, pns->nodes[2]);
3158 : 3682 : compile_node(comp, pns->nodes[2]); // 2 is class body
3159 : :
3160 : 3682 : id_info_t *id = scope_find(scope, MP_QSTR___class__);
3161 [ - + ]: 3682 : assert(id != NULL);
3162 [ + + ]: 3682 : if (id->kind == ID_INFO_KIND_LOCAL) {
3163 : 3598 : EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3164 : : } else {
3165 : 84 : EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
3166 : : }
3167 : 3682 : EMIT(return_value);
3168 : : }
3169 : :
3170 : 35657 : bool pass_complete = EMIT(end_pass);
3171 : :
3172 : : // make sure we match all the exception levels
3173 [ - + ]: 35655 : assert(comp->cur_except_level == 0);
3174 : :
3175 : 35655 : 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 : 8856 : STATIC void scope_compute_things(scope_t *scope) {
3356 : : // in MicroPython we put the *x parameter after all other parameters (except **y)
3357 [ + + ]: 8856 : if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3358 : 83 : id_info_t *id_param = NULL;
3359 [ + - ]: 315 : for (int i = scope->id_info_len - 1; i >= 0; i--) {
3360 : 315 : id_info_t *id = &scope->id_info[i];
3361 [ + + ]: 315 : if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3362 [ + + ]: 83 : 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 [ + + + + ]: 232 : } 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 : 8856 : scope->num_locals = 0;
3378 [ + + ]: 45102 : for (int i = 0; i < scope->id_info_len; i++) {
3379 : 36246 : id_info_t *id = &scope->id_info[i];
3380 [ + + + + ]: 36246 : 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 : 920 : continue;
3383 : : }
3384 [ + + + + ]: 35326 : if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3385 : 6432 : id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3386 : : }
3387 : : #if MICROPY_EMIT_NATIVE
3388 [ + + ]: 35326 : if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
3389 : : // This function makes a reference to a global variable
3390 [ + + ]: 6843 : 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 : 6805 : 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 [ + + + + ]: 35326 : if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
3400 : 8536 : id->local_num = scope->num_locals++;
3401 : : }
3402 : : }
3403 : :
3404 : : // compute the index of cell vars
3405 [ + + ]: 45102 : for (int i = 0; i < scope->id_info_len; i++) {
3406 : 36246 : 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 [ + + + + ]: 36246 : 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 [ + + ]: 8856 : if (scope->parent != NULL) {
3419 : : int num_free = 0;
3420 [ + + ]: 68916 : for (int i = 0; i < scope->parent->id_info_len; i++) {
3421 : 63394 : id_info_t *id = &scope->parent->id_info[i];
3422 [ + + ]: 63394 : if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3423 [ + + ]: 1429 : for (int j = 0; j < scope->id_info_len; j++) {
3424 : 1146 : id_info_t *id2 = &scope->id_info[j];
3425 [ + + + + ]: 1146 : if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
3426 [ - + ]: 202 : assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
3427 : : // in MicroPython the frees come first, before the params
3428 : 202 : id2->local_num = num_free;
3429 : 202 : num_free += 1;
3430 : : }
3431 : : }
3432 : : }
3433 : : }
3434 : : // in MicroPython shift all other locals after the free locals
3435 [ + + ]: 5522 : if (num_free > 0) {
3436 [ + + ]: 653 : for (int i = 0; i < scope->id_info_len; i++) {
3437 : 513 : id_info_t *id = &scope->id_info[i];
3438 [ + + - + ]: 513 : if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
3439 : 311 : id->local_num += num_free;
3440 : : }
3441 : : }
3442 : 140 : scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function
3443 : 140 : scope->num_locals += num_free;
3444 : : }
3445 : : }
3446 : 8856 : }
3447 : :
3448 : : #if !MICROPY_PERSISTENT_CODE_SAVE
3449 : : STATIC
3450 : : #endif
3451 : 3574 : 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 : 3574 : compiler_t comp_state = {0};
3454 : 3574 : compiler_t *comp = &comp_state;
3455 : :
3456 : 3574 : comp->is_repl = is_repl;
3457 : 3574 : comp->break_label = INVALID_LABEL;
3458 : 3574 : comp->continue_label = INVALID_LABEL;
3459 : 3574 : mp_emit_common_init(&comp->emit_common, source_file);
3460 : :
3461 : : // create the module scope
3462 : : #if MICROPY_EMIT_NATIVE
3463 : 3574 : const uint emit_opt = MP_STATE_VM(default_emit_opt);
3464 : : #else
3465 : : const uint emit_opt = MP_EMIT_OPT_NONE;
3466 : : #endif
3467 : 3574 : 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 : 3574 : emit_t *emit_bc = emit_bc_new(&comp->emit_common);
3471 : :
3472 : : // compile MP_PASS_SCOPE
3473 : 3574 : comp->emit = emit_bc;
3474 : : #if MICROPY_EMIT_NATIVE
3475 : 3574 : comp->emit_method_table = &emit_bc_method_table;
3476 : : #endif
3477 : 3574 : uint max_num_labels = 0;
3478 [ + + + + ]: 12740 : 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 : 9170 : compile_scope(comp, s, MP_PASS_SCOPE);
3486 : :
3487 : : // Check if any implicitly declared variables should be closed over
3488 [ + + ]: 45681 : for (size_t i = 0; i < s->id_info_len; ++i) {
3489 : 36511 : id_info_t *id = &s->id_info[i];
3490 [ + + ]: 36511 : if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3491 : 14961 : scope_check_to_close_over(s, id);
3492 : : }
3493 : : }
3494 : : }
3495 : :
3496 : : // update maximim number of labels needed
3497 [ + + ]: 9166 : if (comp->next_label > max_num_labels) {
3498 : 2917 : max_num_labels = comp->next_label;
3499 : : }
3500 : : }
3501 : :
3502 : : // compute some things related to scope and identifiers
3503 [ + + + + ]: 12426 : for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
3504 : 8856 : scope_compute_things(s);
3505 : : }
3506 : :
3507 : : // set max number of labels now that it's calculated
3508 : 3570 : 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 : 3570 : emit_t *emit_native = NULL;
3513 : : #endif
3514 [ + + + + ]: 12408 : 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 [ + + ]: 8856 : switch (s->emit_options) {
3545 : :
3546 : : #if MICROPY_EMIT_NATIVE
3547 : 3998 : case MP_EMIT_OPT_NATIVE_PYTHON:
3548 : : case MP_EMIT_OPT_VIPER:
3549 [ + + ]: 3998 : if (emit_native == NULL) {
3550 : 1369 : emit_native = NATIVE_EMITTER(new)(&comp->emit_common, &comp->compile_error, &comp->next_label, max_num_labels);
3551 : : }
3552 : 3998 : comp->emit_method_table = NATIVE_EMITTER_TABLE;
3553 : 3998 : comp->emit = emit_native;
3554 : 3998 : break;
3555 : : #endif // MICROPY_EMIT_NATIVE
3556 : :
3557 : 4858 : default:
3558 : 4858 : comp->emit = emit_bc;
3559 : : #if MICROPY_EMIT_NATIVE
3560 : 4858 : comp->emit_method_table = &emit_bc_method_table;
3561 : : #endif
3562 : 4858 : break;
3563 : : }
3564 : :
3565 : : // need a pass to compute stack size
3566 : 8856 : compile_scope(comp, s, MP_PASS_STACK_SIZE);
3567 : :
3568 : : // second last pass: compute code size
3569 [ + + ]: 8840 : if (comp->compile_error == MP_OBJ_NULL) {
3570 : 8760 : 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 [ + + ]: 8840 : if (comp->compile_error == MP_OBJ_NULL) {
3576 [ + + ]: 8889 : while (!compile_scope(comp, s, MP_PASS_EMIT)) {
3577 : 8891 : }
3578 : : }
3579 : : }
3580 : : }
3581 : :
3582 [ + + ]: 3552 : 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 : 316 : compile_error_set_line(comp, comp->scope_cur->pn);
3586 : : // add a traceback to the exception using relevant source info
3587 : 316 : mp_obj_exception_add_traceback(comp->compile_error, source_file,
3588 : 316 : comp->compile_error_line, comp->scope_cur->simple_name);
3589 : : }
3590 : :
3591 : : // construct the global qstr/const table for this module
3592 : 3552 : 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 [ + + ]: 3552 : if (comp->compile_error == MP_OBJ_NULL) {
3609 : 3236 : 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 [ + + ]: 3236 : 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 : 3552 : emit_bc_free(emit_bc);
3627 : : #if MICROPY_EMIT_NATIVE
3628 [ + + ]: 3552 : if (emit_native != NULL) {
3629 : 1353 : 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 : 3552 : mp_parse_tree_clear(parse_tree);
3640 : :
3641 : : // free the scopes
3642 [ + + ]: 12696 : for (scope_t *s = module_scope; s;) {
3643 : 9144 : scope_t *next = s->next;
3644 : 9144 : scope_free(s);
3645 : 9144 : s = next;
3646 : : }
3647 : :
3648 [ + + ]: 3552 : if (comp->compile_error != MP_OBJ_NULL) {
3649 : 316 : nlr_raise(comp->compile_error);
3650 : : }
3651 : 3236 : }
3652 : :
3653 : 3574 : mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) {
3654 : 3574 : mp_compiled_module_t cm;
3655 : 3574 : cm.context = m_new_obj(mp_module_context_t);
3656 : 3574 : cm.context->module.globals = mp_globals_get();
3657 : 3574 : mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm);
3658 : : // return function that executes the outer module
3659 : 3236 : return mp_make_function_from_raw_code(cm.rc, cm.context, NULL);
3660 : : }
3661 : :
3662 : : #endif // MICROPY_ENABLE_COMPILER
|