blob: 6c9b6abfe6fbe8cf035c5139a55782fbdd8b82d8 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
Damien George9d5e5c02015-09-24 13:15:57 +01006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
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
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
31#include <assert.h>
32
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/scope.h"
34#include "py/emit.h"
35#include "py/compile.h"
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010037
Damien Georgedd5353a2015-12-18 12:35:44 +000038#if MICROPY_ENABLE_COMPILER
39
Damien429d7192013-10-04 19:53:11 +010040// TODO need to mangle __attr names
41
42typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000043#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000044#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010045#undef DEF_RULE
46 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010047 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000048 PN_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000049 PN_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien George65dc9602015-08-14 12:24:11 +010052#define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE
Damien George41125902015-03-26 16:44:14 +000053
54#if NEED_METHOD_TABLE
55
56// we need a method table to do the lookup for the emitter functions
Damien Georgeb9791222014-01-23 00:34:21 +000057#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
58#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
Damien George542bd6b2015-03-26 14:42:40 +000059#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
60#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
Damien429d7192013-10-04 19:53:11 +010061
Damien George41125902015-03-26 16:44:14 +000062#else
63
64// if we only have the bytecode emitter enabled then we can do a direct call to the functions
65#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
66#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
67#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
68#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
69
70#endif
71
Damien George080a78b2016-12-07 11:17:17 +110072#if MICROPY_EMIT_NATIVE
73// define a macro to access external native emitter
74#if MICROPY_EMIT_X64
75#define NATIVE_EMITTER(f) emit_native_x64_##f
76#elif MICROPY_EMIT_X86
77#define NATIVE_EMITTER(f) emit_native_x86_##f
78#elif MICROPY_EMIT_THUMB
79#define NATIVE_EMITTER(f) emit_native_thumb_##f
80#elif MICROPY_EMIT_ARM
81#define NATIVE_EMITTER(f) emit_native_arm_##f
Damien George8e5aced2016-12-09 16:39:39 +110082#elif MICROPY_EMIT_XTENSA
83#define NATIVE_EMITTER(f) emit_native_xtensa_##f
Damien George080a78b2016-12-07 11:17:17 +110084#else
85#error "unknown native emitter"
86#endif
87#endif
88
Damien Georgead297a12016-12-09 13:17:49 +110089#if MICROPY_EMIT_INLINE_ASM
90// define macros for inline assembler
91#if MICROPY_EMIT_INLINE_THUMB
92#define ASM_DECORATOR_QSTR MP_QSTR_asm_thumb
93#define ASM_EMITTER(f) emit_inline_thumb_##f
Damien Georgef76b1bf2016-12-09 17:03:33 +110094#elif MICROPY_EMIT_INLINE_XTENSA
95#define ASM_DECORATOR_QSTR MP_QSTR_asm_xtensa
96#define ASM_EMITTER(f) emit_inline_xtensa_##f
Damien Georgead297a12016-12-09 13:17:49 +110097#else
98#error "unknown asm emitter"
99#endif
100#endif
101
Damien George0496de22015-10-03 17:07:54 +0100102#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
103#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
104
Damien Georgea91ac202014-10-05 19:01:34 +0100105// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +0100106typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +0000107 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +0100108
Damien George78035b92014-04-09 12:27:39 +0100109 uint8_t is_repl;
110 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +0100111 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +0100112 uint8_t have_star;
113
114 // try to keep compiler clean from nlr
Damien Georgecfc4c332015-07-29 22:16:01 +0000115 mp_obj_t compile_error; // set to an exception object if there's an error
Damien George831137b2015-12-17 13:13:18 +0000116 size_t compile_error_line; // set to best guess of line of error
Damien429d7192013-10-04 19:53:11 +0100117
Damien George6f355fd2014-04-10 14:11:31 +0100118 uint next_label;
Damienb05d7072013-10-05 13:37:10 +0100119
Damien George69b89d22014-04-11 13:38:30 +0000120 uint16_t num_dict_params;
121 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +0100122
Damien Georgea91ac202014-10-05 19:01:34 +0100123 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
124 uint16_t continue_label;
125 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +0000126 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +0100127
Damien429d7192013-10-04 19:53:11 +0100128 scope_t *scope_head;
129 scope_t *scope_cur;
130
Damien6cdd3af2013-10-05 18:08:26 +0100131 emit_t *emit; // current emitter
Damien George41125902015-03-26 16:44:14 +0000132 #if NEED_METHOD_TABLE
Damien6cdd3af2013-10-05 18:08:26 +0100133 const emit_method_table_t *emit_method_table; // current emit method table
Damien George41125902015-03-26 16:44:14 +0000134 #endif
Damien826005c2013-10-05 23:17:28 +0100135
Damien Georgead297a12016-12-09 13:17:49 +1100136 #if MICROPY_EMIT_INLINE_ASM
Damien826005c2013-10-05 23:17:28 +0100137 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
138 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien Georgea77ffe62015-03-14 12:59:31 +0000139 #endif
Damien429d7192013-10-04 19:53:11 +0100140} compiler_t;
141
Damien Georgecfc4c332015-07-29 22:16:01 +0000142STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) {
143 // if the line of the error is unknown then try to update it from the pn
144 if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien George831137b2015-12-17 13:13:18 +0000145 comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100146 }
Damien George84d59c22015-07-27 22:20:00 +0100147}
148
149STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgecfc4c332015-07-29 22:16:01 +0000150 // only register the error if there has been no other error
151 if (comp->compile_error == MP_OBJ_NULL) {
152 comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
153 compile_error_set_line(comp, pn);
154 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000155}
156
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200157STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George6be0b0a2014-08-15 14:30:52 +0100158STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000159STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100160
Damien George6f355fd2014-04-10 14:11:31 +0100161STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100162 return comp->next_label++;
163}
164
Damien George8dcc0c72014-03-27 10:55:21 +0000165STATIC void compile_increase_except_level(compiler_t *comp) {
166 comp->cur_except_level += 1;
167 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
168 comp->scope_cur->exc_stack_size = comp->cur_except_level;
169 }
170}
171
172STATIC void compile_decrease_except_level(compiler_t *comp) {
173 assert(comp->cur_except_level > 0);
174 comp->cur_except_level -= 1;
175}
176
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200177STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100178 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100179 scope->parent = comp->scope_cur;
180 scope->next = NULL;
181 if (comp->scope_head == NULL) {
182 comp->scope_head = scope;
183 } else {
184 scope_t *s = comp->scope_head;
185 while (s->next != NULL) {
186 s = s->next;
187 }
188 s->next = scope;
189 }
190 return scope;
191}
192
Damien George91bc32d2015-04-09 15:31:53 +0000193typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn);
194
195STATIC 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) {
Damien George963a5a32015-01-16 17:47:07 +0000196 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000197 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
198 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100199 for (int i = 0; i < num_nodes; i++) {
200 f(comp, pns->nodes[i]);
201 }
Damiend99b0522013-12-21 18:17:45 +0000202 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100203 f(comp, pn);
204 }
205}
206
Damien George969a6b32014-12-10 22:07:04 +0000207STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000208 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100209 for (int i = 0; i < num_nodes; i++) {
210 compile_node(comp, pns->nodes[i]);
Damien Georgecfc4c332015-07-29 22:16:01 +0000211 if (comp->compile_error != MP_OBJ_NULL) {
212 // add line info for the error in case it didn't have a line number
213 compile_error_set_line(comp, pns->nodes[i]);
214 return;
215 }
Damien429d7192013-10-04 19:53:11 +0100216 }
217}
218
Damien George542bd6b2015-03-26 14:42:40 +0000219STATIC void compile_load_id(compiler_t *comp, qstr qst) {
220 if (comp->pass == MP_PASS_SCOPE) {
221 mp_emit_common_get_id_for_load(comp->scope_cur, qst);
222 } else {
Damien George41125902015-03-26 16:44:14 +0000223 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000224 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000225 #else
226 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
227 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000228 }
229}
230
231STATIC void compile_store_id(compiler_t *comp, qstr qst) {
232 if (comp->pass == MP_PASS_SCOPE) {
233 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
234 } else {
Damien George41125902015-03-26 16:44:14 +0000235 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000236 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000237 #else
238 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
239 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000240 }
241}
242
243STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
244 if (comp->pass == MP_PASS_SCOPE) {
245 mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
246 } else {
Damien George41125902015-03-26 16:44:14 +0000247 #if NEED_METHOD_TABLE
Damien George542bd6b2015-03-26 14:42:40 +0000248 mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
Damien George41125902015-03-26 16:44:14 +0000249 #else
250 mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
251 #endif
Damien George542bd6b2015-03-26 14:42:40 +0000252 }
253}
254
Damien George2c0842b2014-04-27 16:46:51 +0100255STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3a205172013-10-12 15:01:56 +0100256 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000257 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100258 compile_node(comp, pn);
259 total += 1;
260 }
261 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000262 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100263 for (int i = 0; i < n; i++) {
264 compile_node(comp, pns_list->nodes[i]);
265 }
266 total += n;
267 }
Damien Georgeb9791222014-01-23 00:34:21 +0000268 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100269}
Damien429d7192013-10-04 19:53:11 +0100270
Damien George969a6b32014-12-10 22:07:04 +0000271STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100272 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000273 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100274}
275
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200276STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100277 if (mp_parse_node_is_const_false(pn)) {
Damien3a205172013-10-12 15:01:56 +0100278 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000279 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100280 }
281 return;
Damien Georgeb0cbfb02016-11-13 15:32:05 +1100282 } else if (mp_parse_node_is_const_true(pn)) {
Damien3a205172013-10-12 15:01:56 +0100283 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000284 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100285 }
286 return;
Damiend99b0522013-12-21 18:17:45 +0000287 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
288 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
289 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
290 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100291 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000292 and_or_logic1:;
Damien George6f355fd2014-04-10 14:11:31 +0100293 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100294 for (int i = 0; i < n - 1; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000295 c_if_cond(comp, pns->nodes[i], !jump_if, label2);
Damien3a205172013-10-12 15:01:56 +0100296 }
Damien George0b2fd912015-02-28 14:37:54 +0000297 c_if_cond(comp, pns->nodes[n - 1], jump_if, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000298 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100299 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000300 and_or_logic2:
Damien3a205172013-10-12 15:01:56 +0100301 for (int i = 0; i < n; i++) {
Damien George0b2fd912015-02-28 14:37:54 +0000302 c_if_cond(comp, pns->nodes[i], jump_if, label);
Damien3a205172013-10-12 15:01:56 +0100303 }
304 }
305 return;
Damiend99b0522013-12-21 18:17:45 +0000306 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100307 if (jump_if == false) {
Damien George0b2fd912015-02-28 14:37:54 +0000308 goto and_or_logic2;
Damien3a205172013-10-12 15:01:56 +0100309 } else {
Damien George0b2fd912015-02-28 14:37:54 +0000310 goto and_or_logic1;
Damien3a205172013-10-12 15:01:56 +0100311 }
Damiend99b0522013-12-21 18:17:45 +0000312 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100313 c_if_cond(comp, pns->nodes[0], !jump_if, label);
314 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100315 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
316 // cond is something in parenthesis
317 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
318 // empty tuple, acts as false for the condition
319 if (jump_if == false) {
320 EMIT_ARG(jump, label);
321 }
Damien George93b37262016-01-07 13:07:52 +0000322 } else {
323 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100324 // non-empty tuple, acts as true for the condition
325 if (jump_if == true) {
326 EMIT_ARG(jump, label);
327 }
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100328 }
329 return;
Damien3a205172013-10-12 15:01:56 +0100330 }
331 }
332
333 // nothing special, fall back to default compiling for node and jump
334 compile_node(comp, pn);
Damien George63f38322015-02-28 15:04:06 +0000335 EMIT_ARG(pop_jump_if, jump_if, label);
Damien429d7192013-10-04 19:53:11 +0100336}
337
338typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100339STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100340
pohmelie81ebba72016-01-27 23:23:11 +0300341STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100342 if (assign_kind != ASSIGN_AUG_STORE) {
343 compile_node(comp, pns->nodes[0]);
344 }
345
Damiend99b0522013-12-21 18:17:45 +0000346 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
347 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300348 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000349 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100350 if (assign_kind != ASSIGN_AUG_STORE) {
351 for (int i = 0; i < n - 1; i++) {
352 compile_node(comp, pns1->nodes[i]);
353 }
354 }
Damiend99b0522013-12-21 18:17:45 +0000355 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
356 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100357 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000358 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100359 if (assign_kind == ASSIGN_AUG_STORE) {
360 EMIT(rot_three);
361 EMIT(store_subscr);
362 } else {
363 compile_node(comp, pns1->nodes[0]);
364 if (assign_kind == ASSIGN_AUG_LOAD) {
365 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100366 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100367 } else {
368 EMIT(store_subscr);
369 }
370 }
Damiend99b0522013-12-21 18:17:45 +0000371 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
372 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100373 if (assign_kind == ASSIGN_AUG_LOAD) {
374 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000375 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100376 } else {
377 if (assign_kind == ASSIGN_AUG_STORE) {
378 EMIT(rot_two);
379 }
Damien Georgeb9791222014-01-23 00:34:21 +0000380 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100381 }
382 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100383 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100384 }
385 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100386 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100387 }
388
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100389 return;
390
391cannot_assign:
392 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100393}
394
Damien George0288cf02014-04-11 11:53:00 +0000395// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
Damien George6be0b0a2014-08-15 14:30:52 +0100396STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
Damien George0288cf02014-04-11 11:53:00 +0000397 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
398
399 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000400 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000401 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
402 EMIT_ARG(unpack_ex, 0, num_tail);
403 have_star_index = 0;
404 }
Damien George963a5a32015-01-16 17:47:07 +0000405 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000406 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000407 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000408 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
409 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100410 } else {
Damien George9d181f62014-04-27 16:55:27 +0100411 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100412 return;
413 }
414 }
415 }
Damien George963a5a32015-01-16 17:47:07 +0000416 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000417 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100418 }
Damien George0288cf02014-04-11 11:53:00 +0000419 if (num_head != 0) {
420 if (0 == have_star_index) {
421 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100422 } else {
Damien George0288cf02014-04-11 11:53:00 +0000423 c_assign(comp, node_head, ASSIGN_STORE);
424 }
425 }
Damien George963a5a32015-01-16 17:47:07 +0000426 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000427 if (num_head + i == have_star_index) {
428 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
429 } else {
430 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100431 }
432 }
433}
434
435// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100436STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien Georgeaedf5832015-03-25 22:06:47 +0000437 assert(!MP_PARSE_NODE_IS_NULL(pn));
438 if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000439 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000440 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100441 switch (assign_kind) {
442 case ASSIGN_STORE:
443 case ASSIGN_AUG_STORE:
Damien George542bd6b2015-03-26 14:42:40 +0000444 compile_store_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100445 break;
446 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000447 default:
Damien George542bd6b2015-03-26 14:42:40 +0000448 compile_load_id(comp, arg);
Damien429d7192013-10-04 19:53:11 +0100449 break;
450 }
451 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100452 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100453 return;
454 }
455 } else {
Damien Georgeaedf5832015-03-25 22:06:47 +0000456 // pn must be a struct
Damiend99b0522013-12-21 18:17:45 +0000457 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
458 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
pohmelie81ebba72016-01-27 23:23:11 +0300459 case PN_atom_expr_normal:
Damien429d7192013-10-04 19:53:11 +0100460 // lhs is an index or attribute
pohmelie81ebba72016-01-27 23:23:11 +0300461 c_assign_atom_expr(comp, pns, assign_kind);
Damien429d7192013-10-04 19:53:11 +0100462 break;
463
464 case PN_testlist_star_expr:
465 case PN_exprlist:
466 // lhs is a tuple
467 if (assign_kind != ASSIGN_STORE) {
468 goto bad_aug;
469 }
Damien George0288cf02014-04-11 11:53:00 +0000470 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100471 break;
472
473 case PN_atom_paren:
474 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000475 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100476 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100477 goto cannot_assign;
Damien George93b37262016-01-07 13:07:52 +0000478 } else {
479 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damien George44f65c02015-03-25 23:06:48 +0000480 if (assign_kind != ASSIGN_STORE) {
481 goto bad_aug;
482 }
Damiend99b0522013-12-21 18:17:45 +0000483 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100484 goto testlist_comp;
Damien429d7192013-10-04 19:53:11 +0100485 }
486 break;
487
488 case PN_atom_bracket:
489 // lhs is something in brackets
490 if (assign_kind != ASSIGN_STORE) {
491 goto bad_aug;
492 }
Damiend99b0522013-12-21 18:17:45 +0000493 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100494 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000495 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000496 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
497 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100498 goto testlist_comp;
499 } else {
500 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000501 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100502 }
503 break;
504
505 default:
Damien George9d181f62014-04-27 16:55:27 +0100506 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100507 }
508 return;
509
510 testlist_comp:
511 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000512 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
513 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
514 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100515 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000516 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000517 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000518 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100519 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000520 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
521 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damien George216a7112016-09-30 14:48:06 +1000522 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien George9d181f62014-04-27 16:55:27 +0100523 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100524 } else {
525 // sequence with 2 items
526 goto sequence_with_2_items;
527 }
528 } else {
529 // sequence with 2 items
530 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000531 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100532 }
533 return;
534 }
535 return;
536
Damien George9d181f62014-04-27 16:55:27 +0100537 cannot_assign:
538 compile_syntax_error(comp, pn, "can't assign to expression");
539 return;
540
Damien429d7192013-10-04 19:53:11 +0100541 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100542 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100543}
544
Damien George65dc9602015-08-14 12:24:11 +0100545// stuff for lambda and comprehensions and generators:
Damien Georgee337f1e2014-03-31 15:18:37 +0100546// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
547// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
548// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100549STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
Damien George30565092014-03-31 11:30:17 +0100550 assert(n_pos_defaults >= 0);
551 assert(n_kw_defaults >= 0);
552
Damien George3a3db4d2015-10-22 23:45:37 +0100553 // set flags
554 if (n_kw_defaults > 0) {
555 this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS;
556 }
557 this_scope->num_def_pos_args = n_pos_defaults;
558
Damien429d7192013-10-04 19:53:11 +0100559 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000560 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100561 int nfree = 0;
562 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000563 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
564 id_info_t *id = &comp->scope_cur->id_info[i];
565 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
566 for (int j = 0; j < this_scope->id_info_len; j++) {
567 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100568 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000569 // in Micro Python we load closures using LOAD_FAST
Damien George542bd6b2015-03-26 14:42:40 +0000570 EMIT_LOAD_FAST(id->qst, id->local_num);
Damien318aec62013-12-10 18:28:17 +0000571 nfree += 1;
572 }
573 }
Damien429d7192013-10-04 19:53:11 +0100574 }
575 }
576 }
Damien429d7192013-10-04 19:53:11 +0100577
578 // make the function/closure
579 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100580 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100581 } else {
Damien George3558f622014-04-20 17:50:40 +0100582 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100583 }
584}
585
Damien George2c838942015-11-17 14:00:14 +0000586STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
587 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
588 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100589 comp->have_star = true;
590 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +0000591 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
592 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100593 // bare star
Damien George8b19db02014-04-11 23:25:34 +0100594 } else {
595 // named star
Damien429d7192013-10-04 19:53:11 +0100596 }
Damien George8b19db02014-04-11 23:25:34 +0100597 */
Damien Georgef41fdd02014-03-03 23:19:11 +0000598
Damien George2c838942015-11-17 14:00:14 +0000599 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
600 || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +0100601 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +0000602 // TODO do we need to do anything with this?
603
604 } else {
605 mp_parse_node_t pn_id;
606 mp_parse_node_t pn_colon;
607 mp_parse_node_t pn_equal;
608 if (MP_PARSE_NODE_IS_ID(pn)) {
609 // this parameter is just an id
610
611 pn_id = pn;
612 pn_colon = MP_PARSE_NODE_NULL;
613 pn_equal = MP_PARSE_NODE_NULL;
614
Damien George2c838942015-11-17 14:00:14 +0000615 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000616 // this parameter has a colon and/or equal specifier
617
618 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
619 pn_id = pns->nodes[0];
620 pn_colon = pns->nodes[1];
621 pn_equal = pns->nodes[2];
Damien George2c838942015-11-17 14:00:14 +0000622
623 } else {
624 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
625 // this parameter has an equal specifier
626
627 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
628 pn_id = pns->nodes[0];
629 pn_equal = pns->nodes[1];
Damien Georgef41fdd02014-03-03 23:19:11 +0000630 }
631
632 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
633 // this parameter does not have a default value
634
635 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +0100636 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100637 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +0000638 return;
639 }
640
641 } else {
642 // this parameter has a default value
643 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
644
Damien George8b19db02014-04-11 23:25:34 +0100645 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +0000646 comp->num_dict_params += 1;
Damien George69b89d22014-04-11 13:38:30 +0000647 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
648 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +0100649 // in Micro Python we put the default positional parameters into a tuple using the bytecode
650 // we need to do this here before we start building the map for the default keywords
651 if (comp->num_default_params > 0) {
652 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +0100653 } else {
654 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +0100655 }
Damien George69b89d22014-04-11 13:38:30 +0000656 // first default dict param, so make the map
657 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +0000658 }
Damien Georgef0778a72014-06-07 22:01:00 +0100659
660 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +0000661 compile_node(comp, pn_equal);
Damien George59fba2d2015-06-25 14:42:13 +0000662 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id));
Damien George69b89d22014-04-11 13:38:30 +0000663 EMIT(store_map);
Damien Georgef41fdd02014-03-03 23:19:11 +0000664 } else {
Damien George69b89d22014-04-11 13:38:30 +0000665 comp->num_default_params += 1;
666 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +0000667 }
668 }
669
670 // TODO pn_colon not implemented
671 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100672 }
673}
674
Damien George2c838942015-11-17 14:00:14 +0000675STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) {
Damien George29e9db02015-12-12 13:42:51 +0000676 // When we call compile_funcdef_lambdef_param below it can compile an arbitrary
677 // expression for default arguments, which may contain a lambda. The lambda will
678 // call here in a nested way, so we must save and restore the relevant state.
679 bool orig_have_star = comp->have_star;
680 uint16_t orig_num_dict_params = comp->num_dict_params;
681 uint16_t orig_num_default_params = comp->num_default_params;
682
Damien George2c838942015-11-17 14:00:14 +0000683 // compile default parameters
684 comp->have_star = false;
685 comp->num_dict_params = 0;
686 comp->num_default_params = 0;
687 apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param);
688
689 if (comp->compile_error != MP_OBJ_NULL) {
690 return;
691 }
692
693 // in Micro Python we put the default positional parameters into a tuple using the bytecode
694 // the default keywords args may have already made the tuple; if not, do it now
695 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
696 EMIT_ARG(build_tuple, comp->num_default_params);
697 EMIT(load_null); // sentinel indicating empty default keyword args
698 }
699
700 // make the function
701 close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params);
Damien George29e9db02015-12-12 13:42:51 +0000702
703 // restore state
704 comp->have_star = orig_have_star;
705 comp->num_dict_params = orig_num_dict_params;
706 comp->num_default_params = orig_num_default_params;
Damien George2c838942015-11-17 14:00:14 +0000707}
708
Damien429d7192013-10-04 19:53:11 +0100709// leaves function object on stack
710// returns function name
Damien George969a6b32014-12-10 22:07:04 +0000711STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100712 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100713 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000714 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100715 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000716 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100717 }
718
Damien429d7192013-10-04 19:53:11 +0100719 // get the scope for this function
720 scope_t *fscope = (scope_t*)pns->nodes[4];
721
Damien George2c838942015-11-17 14:00:14 +0000722 // compile the function definition
723 compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist);
Damien429d7192013-10-04 19:53:11 +0100724
Damien429d7192013-10-04 19:53:11 +0100725 // return its name (the 'f' in "def f(...):")
726 return fscope->simple_name;
727}
728
729// leaves class object on stack
730// returns class name
Damien George969a6b32014-12-10 22:07:04 +0000731STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +0100732 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +0100733 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000734 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100735 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000736 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100737 }
738
739 EMIT(load_build_class);
740
741 // scope for this class
742 scope_t *cscope = (scope_t*)pns->nodes[3];
743
744 // compile the class
745 close_over_variables_etc(comp, cscope, 0, 0);
746
747 // get its name
Damien George59fba2d2015-06-25 14:42:13 +0000748 EMIT_ARG(load_const_str, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100749
750 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100751 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
752 mp_parse_node_t parents = pns->nodes[1];
753 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
754 parents = MP_PARSE_NODE_NULL;
755 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000756 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100757 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100758
759 // return its name (the 'C' in class C(...):")
760 return cscope->simple_name;
761}
762
Damien6cdd3af2013-10-05 18:08:26 +0100763// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200764STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000765 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100766 return false;
767 }
768
769 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100770 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100771 return true;
772 }
773
Damiend99b0522013-12-21 18:17:45 +0000774 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +0100775 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +0100776 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +0100777#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000778 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +0100779 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000780 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +0100781 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100782#endif
Damien Georgead297a12016-12-09 13:17:49 +1100783 #if MICROPY_EMIT_INLINE_ASM
784 } else if (attr == ASM_DECORATOR_QSTR) {
785 *emit_options = MP_EMIT_OPT_ASM;
786 #endif
Damien6cdd3af2013-10-05 18:08:26 +0100787 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100788 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100789 }
790
791 return true;
792}
793
Damien George969a6b32014-12-10 22:07:04 +0000794STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100795 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000796 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000797 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
Damien429d7192013-10-04 19:53:11 +0100798
Damien6cdd3af2013-10-05 18:08:26 +0100799 // inherit emit options for this function/class definition
800 uint emit_options = comp->scope_cur->emit_options;
801
802 // compile each decorator
803 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100804 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000805 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
806 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100807
808 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000809 mp_parse_node_t *name_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +0000810 int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
Damien6cdd3af2013-10-05 18:08:26 +0100811
812 // check for built-in decorators
813 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
814 // this was a built-in
815 num_built_in_decorators += 1;
816
817 } else {
818 // not a built-in, compile normally
819
820 // compile the decorator function
821 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +0000822 for (int j = 1; j < name_len; j++) {
823 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
824 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +0100825 }
826
827 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000828 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100829 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000830 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100831 compile_node(comp, pns_decorator->nodes[1]);
832 }
Damien429d7192013-10-04 19:53:11 +0100833 }
834 }
835
pohmelie81ebba72016-01-27 23:23:11 +0300836 // compile the body (funcdef, async funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000837 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100838 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000839 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100840 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
pohmelie81ebba72016-01-27 23:23:11 +0300841 #if MICROPY_PY_ASYNC_AWAIT
842 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) {
843 assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0]));
844 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
845 body_name = compile_funcdef_helper(comp, pns0, emit_options);
846 scope_t *fscope = (scope_t*)pns0->nodes[4];
847 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
848 #endif
Damien429d7192013-10-04 19:53:11 +0100849 } else {
Damien George0bb97132015-02-27 14:25:47 +0000850 assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
851 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100852 }
853
854 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100855 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +0100856 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +0100857 }
858
859 // store func/class object into name
Damien George542bd6b2015-03-26 14:42:40 +0000860 compile_store_id(comp, body_name);
Damien429d7192013-10-04 19:53:11 +0100861}
862
Damien George969a6b32014-12-10 22:07:04 +0000863STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100864 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100865 // store function object into function name
Damien George542bd6b2015-03-26 14:42:40 +0000866 compile_store_id(comp, fname);
Damien429d7192013-10-04 19:53:11 +0100867}
868
Damien George6be0b0a2014-08-15 14:30:52 +0100869STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000870 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +0000871 compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
pohmelie81ebba72016-01-27 23:23:11 +0300872 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) {
Damiend99b0522013-12-21 18:17:45 +0000873 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100874
pohmelie81ebba72016-01-27 23:23:11 +0300875 compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node
Damien429d7192013-10-04 19:53:11 +0100876
Damiend99b0522013-12-21 18:17:45 +0000877 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
878 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +0300879 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) {
Damiend99b0522013-12-21 18:17:45 +0000880 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100881 for (int i = 0; i < n - 1; i++) {
882 compile_node(comp, pns1->nodes[i]);
883 }
Damiend99b0522013-12-21 18:17:45 +0000884 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
885 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100886 }
Damien Georgeaedf5832015-03-25 22:06:47 +0000887 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100888 compile_node(comp, pns1->nodes[0]);
889 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +0000890 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
891 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +0000892 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100893 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100894 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100895 }
896 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100897 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100898 }
899
Damiend99b0522013-12-21 18:17:45 +0000900 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
901 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien George93b37262016-01-07 13:07:52 +0000902 if (MP_PARSE_NODE_IS_NULL(pn)) {
903 goto cannot_delete;
904 } else {
905 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +0000906 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100907 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
908
Damiend99b0522013-12-21 18:17:45 +0000909 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
910 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
911 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100912 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000913 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100914 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000915 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100916 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +0000917 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100918 c_del_stmt(comp, pns->nodes[0]);
919 for (int i = 0; i < n; i++) {
920 c_del_stmt(comp, pns1->nodes[i]);
921 }
Damien George216a7112016-09-30 14:48:06 +1000922 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100923 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100924 } else {
925 // sequence with 2 items
926 goto sequence_with_2_items;
927 }
928 } else {
929 // sequence with 2 items
930 sequence_with_2_items:
931 c_del_stmt(comp, pns->nodes[0]);
932 c_del_stmt(comp, pns->nodes[1]);
933 }
Damien429d7192013-10-04 19:53:11 +0100934 }
935 } else {
Damien George93b37262016-01-07 13:07:52 +0000936 // some arbitrary statment that we can't delete (eg del 1)
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100937 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +0100938 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100939
940 return;
941
942cannot_delete:
943 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +0100944}
945
Damien George969a6b32014-12-10 22:07:04 +0000946STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100947 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
948}
949
Damien George969a6b32014-12-10 22:07:04 +0000950STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100951 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100952 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +0100953 }
Damien George090c9232014-10-17 14:08:49 +0000954 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000955 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100956}
957
Damien George969a6b32014-12-10 22:07:04 +0000958STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100959 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100960 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +0100961 }
Damien George090c9232014-10-17 14:08:49 +0000962 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +0000963 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +0100964}
965
Damien George969a6b32014-12-10 22:07:04 +0000966STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +0100967 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100968 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +0100969 return;
970 }
Damiend99b0522013-12-21 18:17:45 +0000971 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +0100972 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +0000973 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +0000974 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +0100975 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +0000976 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
977 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100978
Damien George6f355fd2014-04-10 14:11:31 +0100979 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100980 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
981 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
982 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +0000983 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +0100984 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
985 } else {
986 compile_node(comp, pns->nodes[0]);
987 }
988 EMIT(return_value);
989}
990
Damien George969a6b32014-12-10 22:07:04 +0000991STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100992 compile_node(comp, pns->nodes[0]);
993 EMIT(pop_top);
994}
995
Damien George969a6b32014-12-10 22:07:04 +0000996STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000997 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100998 // raise
Damien Georgeb9791222014-01-23 00:34:21 +0000999 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001000 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001001 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001002 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001003 compile_node(comp, pns->nodes[0]);
1004 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001005 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001006 } else {
1007 // raise x
1008 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001009 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001010 }
1011}
1012
Damien George635543c2014-04-10 12:56:52 +01001013// q_base holds the base of the name
1014// eg a -> q_base=a
1015// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001016STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001017 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001018 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1019 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001020 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001021 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001022 pn = pns->nodes[0];
1023 is_as = true;
1024 }
Damien George635543c2014-04-10 12:56:52 +01001025 if (MP_PARSE_NODE_IS_NULL(pn)) {
1026 // empty name (eg, from . import x)
1027 *q_base = MP_QSTR_;
1028 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1029 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001030 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001031 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001032 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001033 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001034 }
Damien George635543c2014-04-10 12:56:52 +01001035 EMIT_ARG(import_name, q_full);
Damien George0bb97132015-02-27 14:25:47 +00001036 } else {
1037 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
Damiend99b0522013-12-21 18:17:45 +00001038 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George0bb97132015-02-27 14:25:47 +00001039 {
Damien429d7192013-10-04 19:53:11 +01001040 // a name of the form a.b.c
1041 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001042 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001043 }
Damiend99b0522013-12-21 18:17:45 +00001044 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001045 int len = n - 1;
1046 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001047 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001048 }
Damien George55baff42014-01-21 21:40:13 +00001049 byte *q_ptr;
1050 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001051 for (int i = 0; i < n; i++) {
1052 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001053 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001054 }
Damien Georgec3f64d92015-11-27 12:23:18 +00001055 size_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001056 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001057 memcpy(str_dest, str_src, str_src_len);
1058 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001059 }
Damien George635543c2014-04-10 12:56:52 +01001060 qstr q_full = qstr_build_end(q_ptr);
1061 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001062 if (is_as) {
1063 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001064 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001065 }
1066 }
Damien429d7192013-10-04 19:53:11 +01001067 }
Damien429d7192013-10-04 19:53:11 +01001068 }
1069}
1070
Damien George6be0b0a2014-08-15 14:30:52 +01001071STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001072 EMIT_ARG(load_const_small_int, 0); // level 0 import
1073 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1074 qstr q_base;
1075 do_import_name(comp, pn, &q_base);
Damien George542bd6b2015-03-26 14:42:40 +00001076 compile_store_id(comp, q_base);
Damien429d7192013-10-04 19:53:11 +01001077}
1078
Damien George969a6b32014-12-10 22:07:04 +00001079STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001080 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1081}
1082
Damien George969a6b32014-12-10 22:07:04 +00001083STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001084 mp_parse_node_t pn_import_source = pns->nodes[0];
1085
1086 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1087 uint import_level = 0;
1088 do {
1089 mp_parse_node_t pn_rel;
1090 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)) {
1091 // This covers relative imports with dots only like "from .. import"
1092 pn_rel = pn_import_source;
1093 pn_import_source = MP_PARSE_NODE_NULL;
1094 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1095 // This covers relative imports starting with dot(s) like "from .foo import"
1096 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1097 pn_rel = pns_2b->nodes[0];
1098 pn_import_source = pns_2b->nodes[1];
1099 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1100 } else {
1101 // Not a relative import
1102 break;
1103 }
1104
1105 // get the list of . and/or ...'s
1106 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001107 int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
Damien George635543c2014-04-10 12:56:52 +01001108
1109 // count the total number of .'s
1110 for (int i = 0; i < n; i++) {
1111 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1112 import_level++;
1113 } else {
1114 // should be an MP_TOKEN_ELLIPSIS
1115 import_level += 3;
1116 }
1117 }
1118 } while (0);
1119
Damiend99b0522013-12-21 18:17:45 +00001120 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001121 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001122
1123 // build the "fromlist" tuple
Damien George59fba2d2015-06-25 14:42:13 +00001124 EMIT_ARG(load_const_str, MP_QSTR__star_);
Damien Georgeb9791222014-01-23 00:34:21 +00001125 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001126
1127 // do the import
Damien George635543c2014-04-10 12:56:52 +01001128 qstr dummy_q;
1129 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001130 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001131
Damien429d7192013-10-04 19:53:11 +01001132 } else {
Damien George635543c2014-04-10 12:56:52 +01001133 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001134
1135 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001136 mp_parse_node_t *pn_nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001137 int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001138 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001139 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1140 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1141 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien George59fba2d2015-06-25 14:42:13 +00001142 EMIT_ARG(load_const_str, id2);
Damiendb4c3612013-12-10 17:27:24 +00001143 }
Damien Georgeb9791222014-01-23 00:34:21 +00001144 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001145
1146 // do the import
Damien George635543c2014-04-10 12:56:52 +01001147 qstr dummy_q;
1148 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001149 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001150 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1151 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1152 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001153 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001154 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien George542bd6b2015-03-26 14:42:40 +00001155 compile_store_id(comp, id2);
Damien429d7192013-10-04 19:53:11 +01001156 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001157 compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001158 }
1159 }
1160 EMIT(pop_top);
1161 }
1162}
1163
Damien George584ba672014-12-21 17:26:45 +00001164STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1165 bool added;
1166 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George558a0162015-09-07 16:55:02 +01001167 if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) {
1168 compile_syntax_error(comp, pn, "identifier redefined as global");
Damien George584ba672014-12-21 17:26:45 +00001169 return;
1170 }
1171 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1172
1173 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1174 id_info = scope_find_global(comp->scope_cur, qst);
1175 if (id_info != NULL) {
1176 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1177 }
1178}
1179
Damien George969a6b32014-12-10 22:07:04 +00001180STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001181 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001182 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001183 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001184 for (int i = 0; i < n; i++) {
1185 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001186 }
1187 }
1188}
1189
Damien George584ba672014-12-21 17:26:45 +00001190STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1191 bool added;
1192 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
Damien George0d105172016-09-30 13:53:00 +10001193 if (added) {
1194 scope_find_local_and_close_over(comp->scope_cur, id_info, qst);
1195 if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
1196 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1197 }
1198 } else if (id_info->kind != ID_INFO_KIND_FREE) {
Damien George558a0162015-09-07 16:55:02 +01001199 compile_syntax_error(comp, pn, "identifier redefined as nonlocal");
Damien George584ba672014-12-21 17:26:45 +00001200 }
Damien George584ba672014-12-21 17:26:45 +00001201}
1202
Damien George969a6b32014-12-10 22:07:04 +00001203STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001204 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001205 if (comp->scope_cur->kind == SCOPE_MODULE) {
1206 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1207 return;
1208 }
1209 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001210 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
Damien George584ba672014-12-21 17:26:45 +00001211 for (int i = 0; i < n; i++) {
1212 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001213 }
1214 }
1215}
1216
Damien George969a6b32014-12-10 22:07:04 +00001217STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George24df30c2016-08-26 22:28:22 +10001218 // with optimisations enabled we don't compile assertions
1219 if (MP_STATE_VM(mp_optimise_value) != 0) {
1220 return;
1221 }
1222
Damien George6f355fd2014-04-10 14:11:31 +01001223 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001224 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien George542bd6b2015-03-26 14:42:40 +00001225 EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001226 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001227 // assertion message
1228 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001229 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001230 }
Damien Georgeb9791222014-01-23 00:34:21 +00001231 EMIT_ARG(raise_varargs, 1);
1232 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001233}
1234
Damien George969a6b32014-12-10 22:07:04 +00001235STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001236 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001237
Damien George65dc9602015-08-14 12:24:11 +01001238 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001239 if (!mp_parse_node_is_const_false(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001240 uint l_fail = comp_next_label(comp);
1241 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001242
Damien George391db862014-10-17 17:57:33 +00001243 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001244
Damien George65dc9602015-08-14 12:24:11 +01001245 // optimisation: skip everything else when "if True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001246 if (mp_parse_node_is_const_true(pns->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001247 goto done;
1248 }
1249
1250 if (
Damien George65dc9602015-08-14 12:24:11 +01001251 // optimisation: don't jump over non-existent elif/else blocks
1252 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
Damien George391db862014-10-17 17:57:33 +00001253 // optimisation: don't jump if last instruction was return
1254 && !EMIT(last_emit_was_return_value)
1255 ) {
1256 // jump over elif/else blocks
1257 EMIT_ARG(jump, l_end);
1258 }
1259
1260 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001261 }
1262
Damien George235f9b32014-10-17 17:30:16 +00001263 // compile elif blocks (if any)
1264 mp_parse_node_t *pn_elif;
Damien Georgedfe944c2015-02-13 02:29:46 +00001265 int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
Damien George235f9b32014-10-17 17:30:16 +00001266 for (int i = 0; i < n_elif; i++) {
1267 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1268 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001269
Damien George65dc9602015-08-14 12:24:11 +01001270 // optimisation: don't emit anything when "if False"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001271 if (!mp_parse_node_is_const_false(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001272 uint l_fail = comp_next_label(comp);
1273 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001274
Damien George391db862014-10-17 17:57:33 +00001275 compile_node(comp, pns_elif->nodes[1]); // elif block
1276
Damien George65dc9602015-08-14 12:24:11 +01001277 // optimisation: skip everything else when "elif True"
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001278 if (mp_parse_node_is_const_true(pns_elif->nodes[0])) {
Damien George391db862014-10-17 17:57:33 +00001279 goto done;
1280 }
1281
1282 // optimisation: don't jump if last instruction was return
1283 if (!EMIT(last_emit_was_return_value)) {
1284 EMIT_ARG(jump, l_end);
1285 }
1286 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001287 }
1288 }
1289
1290 // compile else block
1291 compile_node(comp, pns->nodes[3]); // can be null
1292
Damien George391db862014-10-17 17:57:33 +00001293done:
Damien Georgeb9791222014-01-23 00:34:21 +00001294 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001295}
1296
Damien Georgecbddb272014-02-01 20:08:18 +00001297#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001298 uint16_t old_break_label = comp->break_label; \
1299 uint16_t old_continue_label = comp->continue_label; \
1300 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001301 uint break_label = comp_next_label(comp); \
1302 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001303 comp->break_label = break_label; \
1304 comp->continue_label = continue_label; \
1305 comp->break_continue_except_level = comp->cur_except_level;
1306
1307#define END_BREAK_CONTINUE_BLOCK \
1308 comp->break_label = old_break_label; \
1309 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001310 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001311
Damien George969a6b32014-12-10 22:07:04 +00001312STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001313 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001314
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001315 if (!mp_parse_node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
Damien George391db862014-10-17 17:57:33 +00001316 uint top_label = comp_next_label(comp);
Damien Georgeb0cbfb02016-11-13 15:32:05 +11001317 if (!mp_parse_node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
Damien George391db862014-10-17 17:57:33 +00001318 EMIT_ARG(jump, continue_label);
1319 }
1320 EMIT_ARG(label_assign, top_label);
1321 compile_node(comp, pns->nodes[1]); // body
1322 EMIT_ARG(label_assign, continue_label);
1323 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1324 }
Damience89a212013-10-15 22:25:17 +01001325
1326 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001327 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001328
1329 compile_node(comp, pns->nodes[2]); // else
1330
Damien Georgeb9791222014-01-23 00:34:21 +00001331 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001332}
1333
Damien Georgee181c0d2014-12-12 17:19:56 +00001334// This function compiles an optimised for-loop of the form:
1335// for <var> in range(<start>, <end>, <step>):
1336// <body>
1337// else:
1338// <else>
1339// <var> must be an identifier and <step> must be a small-int.
1340//
1341// Semantics of for-loop require:
1342// - final failing value should not be stored in the loop variable
1343// - if the loop never runs, the loop variable should never be assigned
1344// - assignments to <var>, <end> or <step> in the body do not alter the loop
1345// (<step> is a constant for us, so no need to worry about it changing)
1346//
1347// If <end> is a small-int, then the stack during the for-loop contains just
1348// the current value of <var>. Otherwise, the stack contains <end> then the
1349// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001350STATIC 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) {
Damien Georgecbddb272014-02-01 20:08:18 +00001351 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001352
Damien George6f355fd2014-04-10 14:11:31 +01001353 uint top_label = comp_next_label(comp);
1354 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001355
Damien Georgee181c0d2014-12-12 17:19:56 +00001356 // put the end value on the stack if it's not a small-int constant
1357 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1358 if (end_on_stack) {
1359 compile_node(comp, pn_end);
1360 }
1361
1362 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001363 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001364
Damien Georgeb9791222014-01-23 00:34:21 +00001365 EMIT_ARG(jump, entry_label);
1366 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001367
Damien Georgec33ce602014-12-11 17:35:23 +00001368 // duplicate next value and store it to var
1369 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001370 c_assign(comp, pn_var, ASSIGN_STORE);
1371
Damienf3822fc2013-11-09 20:12:03 +00001372 // compile body
1373 compile_node(comp, pn_body);
1374
Damien Georgeb9791222014-01-23 00:34:21 +00001375 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001376
Damien Georgee181c0d2014-12-12 17:19:56 +00001377 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001378 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001379 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001380
Damien Georgeb9791222014-01-23 00:34:21 +00001381 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001382
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001383 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001384 if (end_on_stack) {
1385 EMIT(dup_top_two);
1386 EMIT(rot_two);
1387 } else {
1388 EMIT(dup_top);
1389 compile_node(comp, pn_end);
1390 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001391 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1392 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001393 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001394 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001395 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001396 }
Damien George63f38322015-02-28 15:04:06 +00001397 EMIT_ARG(pop_jump_if, true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001398
1399 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001400 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001401
1402 compile_node(comp, pn_else);
1403
Damien Georgeb9791222014-01-23 00:34:21 +00001404 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001405
1406 // discard final value of var that failed the loop condition
1407 EMIT(pop_top);
1408
1409 // discard <end> value if it's on the stack
1410 if (end_on_stack) {
1411 EMIT(pop_top);
1412 }
Damienf72fd0e2013-11-06 20:20:49 +00001413}
1414
Damien George969a6b32014-12-10 22:07:04 +00001415STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001416 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1417 // this is actually slower, but uses no heap memory
1418 // for viper it will be much, much faster
pohmelie81ebba72016-01-27 23:23:11 +03001419 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)) {
Damiend99b0522013-12-21 18:17:45 +00001420 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
pohmelie81ebba72016-01-27 23:23:11 +03001421 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001422 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
Damien Georgeeacbd7a2016-04-13 15:21:47 +01001423 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) {
Damiend99b0522013-12-21 18:17:45 +00001424 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1425 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00001426 int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001427 mp_parse_node_t pn_range_start;
1428 mp_parse_node_t pn_range_end;
1429 mp_parse_node_t pn_range_step;
1430 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001431 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001432 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001433 if (n_args == 1) {
Damien Georgeed9c93f2016-11-13 15:35:11 +11001434 pn_range_start = mp_parse_node_new_small_int(0);
Damienf72fd0e2013-11-06 20:20:49 +00001435 pn_range_end = args[0];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001436 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001437 } else if (n_args == 2) {
1438 pn_range_start = args[0];
1439 pn_range_end = args[1];
Damien Georgeed9c93f2016-11-13 15:35:11 +11001440 pn_range_step = mp_parse_node_new_small_int(1);
Damienf72fd0e2013-11-06 20:20:49 +00001441 } else {
1442 pn_range_start = args[0];
1443 pn_range_end = args[1];
1444 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001445 // We need to know sign of step. This is possible only if it's constant
1446 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1447 optimize = false;
1448 }
Damienf72fd0e2013-11-06 20:20:49 +00001449 }
Damien George33ac0fd2015-12-08 21:05:14 +00001450 // arguments must be able to be compiled as standard expressions
1451 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) {
1452 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start);
1453 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1454 optimize = false;
1455 }
1456 }
1457 if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) {
1458 int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end);
1459 if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) {
1460 optimize = false;
1461 }
1462 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001463 }
1464 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001465 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1466 return;
1467 }
1468 }
1469 }
Damienf72fd0e2013-11-06 20:20:49 +00001470
Damien Georgecbddb272014-02-01 20:08:18 +00001471 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001472 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001473
Damien George6f355fd2014-04-10 14:11:31 +01001474 uint pop_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001475
Damien429d7192013-10-04 19:53:11 +01001476 compile_node(comp, pns->nodes[1]); // iterator
1477 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001478 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001479 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001480 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1481 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001482 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001483 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001484 }
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001486 EMIT(for_iter_end);
1487
1488 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001489 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001490
Damien429d7192013-10-04 19:53:11 +01001491 compile_node(comp, pns->nodes[3]); // else (not tested)
1492
Damien Georgeb9791222014-01-23 00:34:21 +00001493 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001494}
1495
Damien George6be0b0a2014-08-15 14:30:52 +01001496STATIC 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) {
Damien429d7192013-10-04 19:53:11 +01001497 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001498 uint l1 = comp_next_label(comp);
1499 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001500
Damien Georgeb9791222014-01-23 00:34:21 +00001501 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001502 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001503
Damien429d7192013-10-04 19:53:11 +01001504 compile_node(comp, pn_body); // body
1505 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001506 EMIT_ARG(jump, success_label); // jump over exception handler
1507
1508 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001509 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001510
Damien Georgef0406852016-09-27 12:37:21 +10001511 // at this point the top of the stack contains the exception instance that was raised
1512
Damien George6f355fd2014-04-10 14:11:31 +01001513 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001514
1515 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001516 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1517 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001518
1519 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001520 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001521
Damiend99b0522013-12-21 18:17:45 +00001522 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001523 // this is a catch all exception handler
1524 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001525 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001526 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001527 return;
1528 }
1529 } else {
1530 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001531 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1532 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1533 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1534 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001535 // handler binds the exception to a local
1536 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001537 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001538 }
1539 }
1540 EMIT(dup_top);
1541 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001542 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien George63f38322015-02-28 15:04:06 +00001543 EMIT_ARG(pop_jump_if, false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001544 }
1545
Damien Georgef0406852016-09-27 12:37:21 +10001546 // either discard or store the exception instance
Damien429d7192013-10-04 19:53:11 +01001547 if (qstr_exception_local == 0) {
1548 EMIT(pop_top);
1549 } else {
Damien George542bd6b2015-03-26 14:42:40 +00001550 compile_store_id(comp, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001551 }
1552
Damien George6f355fd2014-04-10 14:11:31 +01001553 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001554 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001555 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001557 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001558 }
1559 compile_node(comp, pns_except->nodes[1]);
1560 if (qstr_exception_local != 0) {
1561 EMIT(pop_block);
1562 }
1563 EMIT(pop_except);
1564 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001565 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1566 EMIT_ARG(label_assign, l3);
1567 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien George542bd6b2015-03-26 14:42:40 +00001568 compile_store_id(comp, qstr_exception_local);
1569 compile_delete_id(comp, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001570
Damien George8dcc0c72014-03-27 10:55:21 +00001571 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001572 EMIT(end_finally);
1573 }
Damien Georgeb9791222014-01-23 00:34:21 +00001574 EMIT_ARG(jump, l2);
1575 EMIT_ARG(label_assign, end_finally_label);
Damien Georgef0406852016-09-27 12:37:21 +10001576 EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance
Damien429d7192013-10-04 19:53:11 +01001577 }
1578
Damien George8dcc0c72014-03-27 10:55:21 +00001579 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001580 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01001581 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00001582
Damien Georgeb9791222014-01-23 00:34:21 +00001583 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001584 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001585 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01001586}
1587
Damien George6be0b0a2014-08-15 14:30:52 +01001588STATIC 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) {
Damien George6f355fd2014-04-10 14:11:31 +01001589 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001590
Damien Georgeb9791222014-01-23 00:34:21 +00001591 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001592 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001593
Damien429d7192013-10-04 19:53:11 +01001594 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001595 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00001596 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01001597 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00001598 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01001599 } else {
1600 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1601 }
1602 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001603 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1604 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001605 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001606
Damien George8dcc0c72014-03-27 10:55:21 +00001607 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001608 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01001609}
1610
Damien George969a6b32014-12-10 22:07:04 +00001611STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0bb97132015-02-27 14:25:47 +00001612 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
1613 {
Damiend99b0522013-12-21 18:17:45 +00001614 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1615 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001616 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001617 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1618 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001619 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001620 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001621 int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001622 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001623 // no finally
1624 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1625 } else {
1626 // have finally
Damiend99b0522013-12-21 18:17:45 +00001627 compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001628 }
1629 } else {
1630 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001631 mp_parse_node_t *pn_excepts;
Damien Georgedfe944c2015-02-13 02:29:46 +00001632 int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001633 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001634 }
Damien429d7192013-10-04 19:53:11 +01001635 }
1636}
1637
Damien George6be0b0a2014-08-15 14:30:52 +01001638STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
Damien429d7192013-10-04 19:53:11 +01001639 if (n == 0) {
1640 // no more pre-bits, compile the body of the with
1641 compile_node(comp, body);
1642 } else {
Damien George6f355fd2014-04-10 14:11:31 +01001643 uint l_end = comp_next_label(comp);
Damien George2c915e12016-04-07 08:53:24 +01001644 if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) {
1645 // we need to allocate an extra label for the native emitter
1646 // it will use l_end+1 as an auxiliary label
1647 comp_next_label(comp);
1648 }
Damiend99b0522013-12-21 18:17:45 +00001649 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001650 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001651 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001652 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001654 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1655 } else {
1656 // this pre-bit is just an expression
1657 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001658 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001659 EMIT(pop_top);
1660 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001661 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001662 // compile additional pre-bits and the body
1663 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1664 // finish this with block
Damien Georgece8b4e82016-04-07 08:50:38 +01001665 EMIT_ARG(with_cleanup, l_end);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001666 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001667 EMIT(end_finally);
1668 }
1669}
1670
Damien George969a6b32014-12-10 22:07:04 +00001671STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001672 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001673 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00001674 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
Damien429d7192013-10-04 19:53:11 +01001675 assert(n > 0);
1676
1677 // compile in a nested fashion
1678 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1679}
1680
pohmelie81ebba72016-01-27 23:23:11 +03001681STATIC void compile_yield_from(compiler_t *comp) {
1682 EMIT(get_iter);
1683 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1684 EMIT(yield_from);
1685}
1686
1687#if MICROPY_PY_ASYNC_AWAIT
1688STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1689 EMIT_ARG(load_method, method);
1690 EMIT_ARG(call_method, 0, 0, 0);
1691 compile_yield_from(comp);
1692}
1693
1694STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1695 // comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1696
1697 qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
1698 uint while_else_label = comp_next_label(comp);
1699 uint try_exception_label = comp_next_label(comp);
1700 uint try_else_label = comp_next_label(comp);
1701 uint try_finally_label = comp_next_label(comp);
1702
1703 compile_node(comp, pns->nodes[1]); // iterator
1704 compile_await_object_method(comp, MP_QSTR___aiter__);
1705 compile_store_id(comp, context);
1706
1707 START_BREAK_CONTINUE_BLOCK
1708
1709 EMIT_ARG(label_assign, continue_label);
1710
1711 EMIT_ARG(setup_except, try_exception_label);
1712 compile_increase_except_level(comp);
1713
1714 compile_load_id(comp, context);
1715 compile_await_object_method(comp, MP_QSTR___anext__);
1716 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1717 EMIT(pop_block);
1718 EMIT_ARG(jump, try_else_label);
1719
1720 EMIT_ARG(label_assign, try_exception_label);
1721 EMIT(start_except_handler);
1722 EMIT(dup_top);
1723 EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration);
1724 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1725 EMIT_ARG(pop_jump_if, false, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001726 EMIT(pop_top); // pop exception instance
pohmelie81ebba72016-01-27 23:23:11 +03001727 EMIT(pop_except);
1728 EMIT_ARG(jump, while_else_label);
1729
1730 EMIT_ARG(label_assign, try_finally_label);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001731 EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack
pohmelie81ebba72016-01-27 23:23:11 +03001732 compile_decrease_except_level(comp);
1733 EMIT(end_finally);
1734 EMIT(end_except_handler);
1735
1736 EMIT_ARG(label_assign, try_else_label);
1737 compile_node(comp, pns->nodes[2]); // body
1738
1739 EMIT_ARG(jump, continue_label);
1740 // break/continue apply to outer loop (if any) in the else block
1741 END_BREAK_CONTINUE_BLOCK
1742
1743 EMIT_ARG(label_assign, while_else_label);
1744 compile_node(comp, pns->nodes[3]); // else
1745
1746 EMIT_ARG(label_assign, break_label);
1747}
1748
1749STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1750 if (n == 0) {
1751 // no more pre-bits, compile the body of the with
1752 compile_node(comp, body);
1753 } else {
1754 uint try_exception_label = comp_next_label(comp);
1755 uint no_reraise_label = comp_next_label(comp);
1756 uint try_else_label = comp_next_label(comp);
1757 uint end_label = comp_next_label(comp);
1758 qstr context;
1759
1760 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1761 // this pre-bit is of the form "a as b"
1762 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
1763 compile_node(comp, pns->nodes[0]);
1764 context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
1765 compile_store_id(comp, context);
1766 compile_load_id(comp, context);
1767 compile_await_object_method(comp, MP_QSTR___aenter__);
1768 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1769 } else {
1770 // this pre-bit is just an expression
1771 compile_node(comp, nodes[0]);
1772 context = MP_PARSE_NODE_LEAF_ARG(nodes[0]);
1773 compile_store_id(comp, context);
1774 compile_load_id(comp, context);
1775 compile_await_object_method(comp, MP_QSTR___aenter__);
1776 EMIT(pop_top);
1777 }
1778
1779 compile_load_id(comp, context);
1780 EMIT_ARG(load_method, MP_QSTR___aexit__);
1781
1782 EMIT_ARG(setup_except, try_exception_label);
1783 compile_increase_except_level(comp);
1784 // compile additional pre-bits and the body
1785 compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);
1786 // finish this with block
1787 EMIT(pop_block);
1788 EMIT_ARG(jump, try_else_label); // jump over exception handler
1789
1790 EMIT_ARG(label_assign, try_exception_label); // start of exception handler
1791 EMIT(start_except_handler);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001792
1793 // at this point the stack contains: ..., __aexit__, self, exc
1794 EMIT(dup_top);
1795 #if MICROPY_CPYTHON_COMPAT
1796 EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc)
1797 #else
1798 compile_load_id(comp, MP_QSTR_type);
pohmelie81ebba72016-01-27 23:23:11 +03001799 EMIT(rot_two);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001800 EMIT_ARG(call_function, 1, 0, 0); // get type(exc)
1801 #endif
1802 EMIT(rot_two);
1803 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value
1804 // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
pohmelie81ebba72016-01-27 23:23:11 +03001805 EMIT_ARG(call_method, 3, 0, 0);
Damien Georgeb32c01b2016-09-28 11:52:13 +10001806
pohmelie81ebba72016-01-27 23:23:11 +03001807 compile_yield_from(comp);
1808 EMIT_ARG(pop_jump_if, true, no_reraise_label);
1809 EMIT_ARG(raise_varargs, 0);
1810
1811 EMIT_ARG(label_assign, no_reraise_label);
1812 EMIT(pop_except);
1813 EMIT_ARG(jump, end_label);
1814
Damien Georgeb32c01b2016-09-28 11:52:13 +10001815 EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc
pohmelie81ebba72016-01-27 23:23:11 +03001816 compile_decrease_except_level(comp);
1817 EMIT(end_finally);
1818 EMIT(end_except_handler);
1819
1820 EMIT_ARG(label_assign, try_else_label); // start of try-else handler
1821 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1822 EMIT(dup_top);
1823 EMIT(dup_top);
1824 EMIT_ARG(call_method, 3, 0, 0);
1825 compile_yield_from(comp);
1826 EMIT(pop_top);
1827
1828 EMIT_ARG(label_assign, end_label);
1829
1830 }
1831}
1832
1833STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1834 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1835 mp_parse_node_t *nodes;
1836 int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1837 assert(n > 0);
1838
1839 // compile in a nested fashion
1840 compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1841}
1842
1843STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1844 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0]));
1845 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
1846 if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) {
1847 // async def
1848 compile_funcdef(comp, pns0);
1849 scope_t *fscope = (scope_t*)pns0->nodes[4];
1850 fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
1851 } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) {
1852 // async for
1853 compile_async_for_stmt(comp, pns0);
1854 } else {
1855 // async with
1856 assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt);
1857 compile_async_with_stmt(comp, pns0);
1858 }
1859}
1860#endif
1861
Damien George969a6b32014-12-10 22:07:04 +00001862STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001863 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001864 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1865 // for REPL, evaluate then print the expression
Damien George542bd6b2015-03-26 14:42:40 +00001866 compile_load_id(comp, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001867 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01001868 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01001869 EMIT(pop_top);
1870
Damien429d7192013-10-04 19:53:11 +01001871 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001872 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01001873 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00001874 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
Damien George7d414a12015-02-08 01:57:40 +00001875 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
1876 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
Damien5ac1b2e2013-10-18 19:58:12 +01001877 // do nothing with a lonely constant
1878 } else {
1879 compile_node(comp, pns->nodes[0]); // just an expression
1880 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1881 }
Damien429d7192013-10-04 19:53:11 +01001882 }
Damien Georgeb948de32015-10-08 14:26:01 +01001883 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
Damiend99b0522013-12-21 18:17:45 +00001884 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1885 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001886 if (kind == PN_expr_stmt_augassign) {
1887 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1888 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001889 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001890 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001891 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001892 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1893 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1894 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1895 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1896 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1897 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1898 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1899 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1900 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1901 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1902 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00001903 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01001904 }
Damien George7e5fb242014-02-01 22:18:47 +00001905 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001906 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1907 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001908 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
Damien Georgeb948de32015-10-08 14:26:01 +01001909 compile_node(comp, pns1->nodes[rhs]); // rhs
Damien429d7192013-10-04 19:53:11 +01001910 // following CPython, we store left-most first
1911 if (rhs > 0) {
1912 EMIT(dup_top);
1913 }
1914 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1915 for (int i = 0; i < rhs; i++) {
1916 if (i + 1 < rhs) {
1917 EMIT(dup_top);
1918 }
Damien Georgeb948de32015-10-08 14:26:01 +01001919 c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001920 }
Damien George0bb97132015-02-27 14:25:47 +00001921 } else {
Damien Georgeb948de32015-10-08 14:26:01 +01001922 plain_assign:
Damien George42e0c592015-03-14 13:11:35 +00001923 if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001924 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001925 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001926 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2
Damiend99b0522013-12-21 18:17:45 +00001927 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien George65dc9602015-08-14 12:24:11 +01001928 // optimisation for a, b = c, d
Damien Georgeb948de32015-10-08 14:26:01 +01001929 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001930 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001931 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1932 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
1933 // can't optimise when it's a star expression on the lhs
1934 goto no_optimisation;
1935 }
Damien429d7192013-10-04 19:53:11 +01001936 compile_node(comp, pns10->nodes[0]); // rhs
1937 compile_node(comp, pns10->nodes[1]); // rhs
1938 EMIT(rot_two);
1939 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1940 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damien George42e0c592015-03-14 13:11:35 +00001941 } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN
Damien Georgeb948de32015-10-08 14:26:01 +01001942 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00001943 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
Damien Georgeb948de32015-10-08 14:26:01 +01001944 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3
Damiend99b0522013-12-21 18:17:45 +00001945 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien George65dc9602015-08-14 12:24:11 +01001946 // optimisation for a, b, c = d, e, f
Damien Georgeb948de32015-10-08 14:26:01 +01001947 mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1];
Damien George4dea9222015-04-09 15:29:54 +00001948 mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01001949 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
1950 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
1951 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
1952 // can't optimise when it's a star expression on the lhs
1953 goto no_optimisation;
1954 }
Damien429d7192013-10-04 19:53:11 +01001955 compile_node(comp, pns10->nodes[0]); // rhs
1956 compile_node(comp, pns10->nodes[1]); // rhs
1957 compile_node(comp, pns10->nodes[2]); // rhs
1958 EMIT(rot_three);
1959 EMIT(rot_two);
1960 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1961 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1962 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1963 } else {
Damien George495d7812014-04-08 17:51:47 +01001964 no_optimisation:
Damien Georgeb948de32015-10-08 14:26:01 +01001965 compile_node(comp, pns->nodes[1]); // rhs
Damien429d7192013-10-04 19:53:11 +01001966 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1967 }
Damien429d7192013-10-04 19:53:11 +01001968 }
Damien Georgeb948de32015-10-08 14:26:01 +01001969 } else {
1970 goto plain_assign;
Damien429d7192013-10-04 19:53:11 +01001971 }
1972}
1973
Damien George6be0b0a2014-08-15 14:30:52 +01001974STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001975 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001976 compile_node(comp, pns->nodes[0]);
1977 for (int i = 1; i < num_nodes; i += 1) {
1978 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001980 }
1981}
1982
Damien George969a6b32014-12-10 22:07:04 +00001983STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001984 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1985 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001986
Damien George6f355fd2014-04-10 14:11:31 +01001987 uint l_fail = comp_next_label(comp);
1988 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001989 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1990 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001991 EMIT_ARG(jump, l_end);
1992 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00001993 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01001994 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001995 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001996}
1997
Damien George969a6b32014-12-10 22:07:04 +00001998STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001999 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002000 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002001 scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002002 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002003 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002004 }
2005
2006 // get the scope for this lambda
2007 scope_t *this_scope = (scope_t*)pns->nodes[2];
2008
Damien George2c838942015-11-17 14:00:14 +00002009 // compile the lambda definition
2010 compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist);
Damien429d7192013-10-04 19:53:11 +01002011}
2012
Damien George7711afb2015-02-28 15:10:18 +00002013STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) {
Damien George6f355fd2014-04-10 14:11:31 +01002014 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002015 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002016 for (int i = 0; i < n; i += 1) {
2017 compile_node(comp, pns->nodes[i]);
2018 if (i + 1 < n) {
Damien George7711afb2015-02-28 15:10:18 +00002019 EMIT_ARG(jump_if_or_pop, cond, l_end);
Damien429d7192013-10-04 19:53:11 +01002020 }
2021 }
Damien Georgeb9791222014-01-23 00:34:21 +00002022 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002023}
2024
Damien George7711afb2015-02-28 15:10:18 +00002025STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2026 compile_or_and_test(comp, pns, true);
2027}
2028
Damien George969a6b32014-12-10 22:07:04 +00002029STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George7711afb2015-02-28 15:10:18 +00002030 compile_or_and_test(comp, pns, false);
Damien429d7192013-10-04 19:53:11 +01002031}
2032
Damien George969a6b32014-12-10 22:07:04 +00002033STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002034 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002035 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002036}
2037
Damien George969a6b32014-12-10 22:07:04 +00002038STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002039 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002040 compile_node(comp, pns->nodes[0]);
2041 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002042 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002043 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002044 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002045 }
2046 for (int i = 1; i + 1 < num_nodes; i += 2) {
2047 compile_node(comp, pns->nodes[i + 1]);
2048 if (i + 2 < num_nodes) {
2049 EMIT(dup_top);
2050 EMIT(rot_three);
2051 }
Damien George7e5fb242014-02-01 22:18:47 +00002052 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002053 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002054 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002055 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2056 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2057 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2058 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2059 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2060 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002061 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002062 }
2063 EMIT_ARG(binary_op, op);
Damien George0bb97132015-02-27 14:25:47 +00002064 } else {
2065 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
Damiend99b0522013-12-21 18:17:45 +00002066 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2067 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002068 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002069 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien George0bb97132015-02-27 14:25:47 +00002070 } else {
2071 assert(kind == PN_comp_op_is); // should be
Damiend99b0522013-12-21 18:17:45 +00002072 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002073 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002074 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002075 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002076 }
Damien429d7192013-10-04 19:53:11 +01002077 }
Damien429d7192013-10-04 19:53:11 +01002078 }
2079 if (i + 2 < num_nodes) {
Damien George63f38322015-02-28 15:04:06 +00002080 EMIT_ARG(jump_if_or_pop, false, l_fail);
Damien429d7192013-10-04 19:53:11 +01002081 }
2082 }
2083 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002084 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002085 EMIT_ARG(jump, l_end);
2086 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002087 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002088 EMIT(rot_two);
2089 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002090 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002091 }
2092}
2093
Damien George969a6b32014-12-10 22:07:04 +00002094STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002095 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002096}
2097
Damien George969a6b32014-12-10 22:07:04 +00002098STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002099 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002100}
2101
Damien George969a6b32014-12-10 22:07:04 +00002102STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002103 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002104}
2105
Damien George969a6b32014-12-10 22:07:04 +00002106STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002107 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002108}
2109
Damien George969a6b32014-12-10 22:07:04 +00002110STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002111 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002112 compile_node(comp, pns->nodes[0]);
2113 for (int i = 1; i + 1 < num_nodes; i += 2) {
2114 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002115 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002116 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damien429d7192013-10-04 19:53:11 +01002117 } else {
Damien George0bb97132015-02-27 14:25:47 +00002118 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2119 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002120 }
2121 }
2122}
2123
Damien George969a6b32014-12-10 22:07:04 +00002124STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002125 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002126 compile_node(comp, pns->nodes[0]);
2127 for (int i = 1; i + 1 < num_nodes; i += 2) {
2128 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002129 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002130 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damien429d7192013-10-04 19:53:11 +01002131 } else {
Damien George0bb97132015-02-27 14:25:47 +00002132 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2133 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002134 }
2135 }
2136}
2137
Damien George969a6b32014-12-10 22:07:04 +00002138STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002139 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002140 compile_node(comp, pns->nodes[0]);
2141 for (int i = 1; i + 1 < num_nodes; i += 2) {
2142 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002143 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002144 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002146 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002148 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damien429d7192013-10-04 19:53:11 +01002149 } else {
Damien George0bb97132015-02-27 14:25:47 +00002150 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2151 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002152 }
2153 }
2154}
2155
Damien George969a6b32014-12-10 22:07:04 +00002156STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002157 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002158 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002159 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002160 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002161 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damien429d7192013-10-04 19:53:11 +01002162 } else {
Damien George0bb97132015-02-27 14:25:47 +00002163 assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2164 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002165 }
2166}
2167
pohmelie81ebba72016-01-27 23:23:11 +03002168STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002169 // this is to handle special super() call
2170 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2171
2172 compile_generic_all_nodes(comp, pns);
pohmelie81ebba72016-01-27 23:23:11 +03002173}
Damien George3acaa282016-03-16 13:04:51 +00002174
pohmelie81ebba72016-01-27 23:23:11 +03002175STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2176 compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power
2177 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien George35e2a4e2014-02-05 00:51:47 +00002178}
2179
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002180STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) {
Damien429d7192013-10-04 19:53:11 +01002181 // function to call is on top of stack
2182
Damien George35e2a4e2014-02-05 00:51:47 +00002183 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002184 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George542bd6b2015-03-26 14:42:40 +00002185 compile_load_id(comp, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002186 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002187 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002188 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002189 // first argument found; load it and call super
Damien George542bd6b2015-03-26 14:42:40 +00002190 EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002191 EMIT_ARG(call_function, 2, 0, 0);
2192 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002193 }
2194 }
Damien George01039b52014-12-21 17:44:27 +00002195 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002196 return;
2197 }
Damien George35e2a4e2014-02-05 00:51:47 +00002198
Damien George2c0842b2014-04-27 16:46:51 +01002199 // get the list of arguments
2200 mp_parse_node_t *args;
Damien Georgedfe944c2015-02-13 02:29:46 +00002201 int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002202
Damien George2c0842b2014-04-27 16:46:51 +01002203 // compile the arguments
2204 // Rather than calling compile_node on the list, we go through the list of args
2205 // explicitly here so that we can count the number of arguments and give sensible
2206 // error messages.
2207 int n_positional = n_positional_extra;
2208 uint n_keyword = 0;
2209 uint star_flags = 0;
Delio Brignolie6978a42015-09-17 12:07:06 +02002210 mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
Damien George2c0842b2014-04-27 16:46:51 +01002211 for (int i = 0; i < n_args; i++) {
2212 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2213 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2214 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2215 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2216 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2217 return;
2218 }
2219 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002220 star_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002221 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2222 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2223 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2224 return;
2225 }
2226 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
Delio Brignolie6978a42015-09-17 12:07:06 +02002227 dblstar_args_node = pns_arg;
Damien George2c0842b2014-04-27 16:46:51 +01002228 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
Damien Georgeb948de32015-10-08 14:26:01 +01002229 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) {
Damien George2c0842b2014-04-27 16:46:51 +01002230 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2231 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2232 return;
2233 }
Damien George59fba2d2015-06-25 14:42:13 +00002234 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]));
Damien Georgeb948de32015-10-08 14:26:01 +01002235 compile_node(comp, pns_arg->nodes[1]);
Damien George2c0842b2014-04-27 16:46:51 +01002236 n_keyword += 1;
2237 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002238 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2239 n_positional++;
2240 }
2241 } else {
2242 goto normal_argument;
2243 }
2244 } else {
2245 normal_argument:
2246 if (n_keyword > 0) {
2247 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2248 return;
2249 }
2250 compile_node(comp, args[i]);
2251 n_positional++;
2252 }
Damien429d7192013-10-04 19:53:11 +01002253 }
2254
Delio Brignolie6978a42015-09-17 12:07:06 +02002255 // compile the star/double-star arguments if we had them
Damien Georgefbcaf0e2015-09-23 11:47:01 +01002256 // if we had one but not the other then we load "null" as a place holder
2257 if (star_flags != 0) {
2258 if (star_args_node == NULL) {
2259 EMIT(load_null);
2260 } else {
2261 compile_node(comp, star_args_node->nodes[0]);
2262 }
2263 if (dblstar_args_node == NULL) {
2264 EMIT(load_null);
2265 } else {
2266 compile_node(comp, dblstar_args_node->nodes[0]);
2267 }
Delio Brignolie6978a42015-09-17 12:07:06 +02002268 }
2269
Damien George2c0842b2014-04-27 16:46:51 +01002270 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002271 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002272 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002273 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002274 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002275 }
Damien429d7192013-10-04 19:53:11 +01002276}
2277
pohmelie81ebba72016-01-27 23:23:11 +03002278STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002279 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002280 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002281 if (i + 1 < num_nodes && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) {
Damien429d7192013-10-04 19:53:11 +01002282 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002283 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2284 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002285 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002286 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002287 i += 1;
2288 } else {
2289 compile_node(comp, pns->nodes[i]);
2290 }
Damien George35e2a4e2014-02-05 00:51:47 +00002291 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002292 }
2293}
2294
Damien George969a6b32014-12-10 22:07:04 +00002295STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002296 // a list of strings
Damien63321742013-12-10 17:41:49 +00002297
2298 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002299 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien George831137b2015-12-17 13:13:18 +00002300 size_t n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002301 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002302 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002303 int pn_kind;
2304 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2305 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2306 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2307 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2308 } else {
2309 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2310 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002311 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2312 pn_kind = MP_PARSE_NODE_STRING;
2313 } else {
2314 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2315 pn_kind = MP_PARSE_NODE_BYTES;
2316 }
Damien George831137b2015-12-17 13:13:18 +00002317 n_bytes += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002318 }
Damien63321742013-12-10 17:41:49 +00002319 if (i == 0) {
2320 string_kind = pn_kind;
2321 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002322 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002323 return;
2324 }
Damien429d7192013-10-04 19:53:11 +01002325 }
Damien63321742013-12-10 17:41:49 +00002326
Damien George65ef6b72015-01-14 21:17:27 +00002327 // if we are not in the last pass, just load a dummy object
2328 if (comp->pass != MP_PASS_EMIT) {
2329 EMIT_ARG(load_const_obj, mp_const_none);
2330 return;
2331 }
2332
Damien63321742013-12-10 17:41:49 +00002333 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002334 vstr_t vstr;
2335 vstr_init_len(&vstr, n_bytes);
2336 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002337 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002338 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien Georgec3f64d92015-11-27 12:23:18 +00002339 size_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002340 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2341 memcpy(s_dest, s, s_len);
2342 s_dest += s_len;
2343 } else {
2344 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George831137b2015-12-17 13:13:18 +00002345 memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]);
2346 s_dest += pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002347 }
Damien63321742013-12-10 17:41:49 +00002348 }
Damien George65ef6b72015-01-14 21:17:27 +00002349
2350 // load the object
Damien George05005f62015-01-21 22:48:37 +00002351 EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
Damien429d7192013-10-04 19:53:11 +01002352}
2353
2354// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002355STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002356 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2357 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2358 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002359
Damien George36db6bc2014-05-07 17:24:22 +01002360 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002361 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002362 scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002363 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002364 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002365 }
2366
2367 // get the scope for this comprehension
2368 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2369
2370 // compile the comprehension
2371 close_over_variables_etc(comp, this_scope, 0, 0);
2372
2373 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2374 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002375 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002376}
2377
Damien George969a6b32014-12-10 22:07:04 +00002378STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002379 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002380 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002381 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
Damien George93b37262016-01-07 13:07:52 +00002382 } else {
2383 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
Damiend99b0522013-12-21 18:17:45 +00002384 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2385 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2386 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2387 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2388 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002389 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002390 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002391 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002392 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002393 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002394 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002395 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002396 // generator expression
2397 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2398 } else {
2399 // tuple with 2 items
2400 goto tuple_with_2_items;
2401 }
2402 } else {
2403 // tuple with 2 items
2404 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002405 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002406 }
Damien429d7192013-10-04 19:53:11 +01002407 }
2408}
2409
Damien George969a6b32014-12-10 22:07:04 +00002410STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002411 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002412 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002413 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002414 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2415 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2416 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2417 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2418 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002419 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002420 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002421 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002422 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002423 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002424 // list of many items
2425 compile_node(comp, pns2->nodes[0]);
2426 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002427 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002428 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002429 // list comprehension
2430 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2431 } else {
2432 // list with 2 items
2433 goto list_with_2_items;
2434 }
2435 } else {
2436 // list with 2 items
2437 list_with_2_items:
2438 compile_node(comp, pns2->nodes[0]);
2439 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002440 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002441 }
2442 } else {
2443 // list with 1 item
2444 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002445 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002446 }
2447}
2448
Damien George969a6b32014-12-10 22:07:04 +00002449STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002450 mp_parse_node_t pn = pns->nodes[0];
2451 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002452 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002453 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002454 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2455 pns = (mp_parse_node_struct_t*)pn;
2456 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002457 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002458 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002459 compile_node(comp, pn);
2460 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002461 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2462 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2463 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2464 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002465 // dict/set with multiple elements
2466
2467 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002468 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00002469 int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
Damien429d7192013-10-04 19:53:11 +01002470
2471 // first element sets whether it's a dict or set
2472 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002473 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002474 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002475 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002476 compile_node(comp, pns->nodes[0]);
2477 EMIT(store_map);
2478 is_dict = true;
2479 } else {
2480 // a set
2481 compile_node(comp, pns->nodes[0]); // 1st value of set
2482 is_dict = false;
2483 }
2484
2485 // process rest of elements
2486 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002487 mp_parse_node_t pn_i = nodes[i];
2488 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2489 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002490 if (is_dict) {
2491 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002492 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002493 return;
2494 }
2495 EMIT(store_map);
2496 } else {
2497 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002498 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002499 return;
2500 }
2501 }
2502 }
2503
Damien Georgee37dcaa2014-12-27 17:07:16 +00002504 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002505 // if it's a set, build it
2506 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002508 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002509 #endif
Damien George0bb97132015-02-27 14:25:47 +00002510 } else {
2511 assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
Damien429d7192013-10-04 19:53:11 +01002512 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002513 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002514 // a dictionary comprehension
2515 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2516 } else {
2517 // a set comprehension
2518 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2519 }
Damien429d7192013-10-04 19:53:11 +01002520 }
2521 } else {
2522 // set with one element
2523 goto set_with_one_element;
2524 }
2525 } else {
2526 // set with one element
2527 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002528 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002529 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002530 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002531 #else
2532 assert(0);
2533 #endif
Damien429d7192013-10-04 19:53:11 +01002534 }
2535}
2536
Damien George969a6b32014-12-10 22:07:04 +00002537STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002538 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002539}
2540
Damien George969a6b32014-12-10 22:07:04 +00002541STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002542 // object who's index we want is on top of stack
2543 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002544 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002545}
2546
Damien George969a6b32014-12-10 22:07:04 +00002547STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002548 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002549 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002550}
2551
Damien George83204f32014-12-27 17:20:41 +00002552#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002553STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002554 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2555 mp_parse_node_t pn = pns->nodes[0];
2556 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002557 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002558 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2559 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002560 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2561 pns = (mp_parse_node_struct_t*)pn;
2562 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002563 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002564 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002565 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002566 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002567 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002568 } else {
2569 // [?::x]
2570 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002572 }
Damiend99b0522013-12-21 18:17:45 +00002573 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002574 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002575 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2576 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2577 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2578 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002579 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002580 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002581 } else {
2582 // [?:x:x]
2583 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002584 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002585 }
2586 } else {
2587 // [?:x]
2588 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002589 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002590 }
2591 } else {
2592 // [?:x]
2593 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002594 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002595 }
2596}
2597
Damien George969a6b32014-12-10 22:07:04 +00002598STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002599 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002600 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2601 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002602}
2603
Damien George969a6b32014-12-10 22:07:04 +00002604STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002605 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002606 compile_subscript_3_helper(comp, pns);
2607}
Damien George83204f32014-12-27 17:20:41 +00002608#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002609
Damien George969a6b32014-12-10 22:07:04 +00002610STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002611 // if this is called then we are compiling a dict key:value pair
2612 compile_node(comp, pns->nodes[1]); // value
2613 compile_node(comp, pns->nodes[0]); // key
2614}
2615
Damien George969a6b32014-12-10 22:07:04 +00002616STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002617 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002618 // store class object into class name
Damien George542bd6b2015-03-26 14:42:40 +00002619 compile_store_id(comp, cname);
Damien429d7192013-10-04 19:53:11 +01002620}
2621
Damien George969a6b32014-12-10 22:07:04 +00002622STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002623 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002624 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002625 return;
2626 }
Damiend99b0522013-12-21 18:17:45 +00002627 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002628 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002629 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002630 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2631 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002632 compile_node(comp, pns->nodes[0]);
pohmelie81ebba72016-01-27 23:23:11 +03002633 compile_yield_from(comp);
Damien429d7192013-10-04 19:53:11 +01002634 } else {
2635 compile_node(comp, pns->nodes[0]);
2636 EMIT(yield_value);
2637 }
2638}
2639
pohmelie81ebba72016-01-27 23:23:11 +03002640#if MICROPY_PY_ASYNC_AWAIT
2641STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) {
2642 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
2643 compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function");
2644 return;
2645 }
2646 compile_atom_expr_normal(comp, pns);
2647 compile_yield_from(comp);
2648}
2649#endif
2650
Damien George65ef6b72015-01-14 21:17:27 +00002651STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2652 // only create and load the actual str object on the last pass
2653 if (comp->pass != MP_PASS_EMIT) {
2654 EMIT_ARG(load_const_obj, mp_const_none);
2655 } else {
Damien George831137b2015-12-17 13:13:18 +00002656 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], pns->nodes[1], false));
Damien George65ef6b72015-01-14 21:17:27 +00002657 }
2658}
2659
2660STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2661 // only create and load the actual bytes object on the last pass
2662 if (comp->pass != MP_PASS_EMIT) {
2663 EMIT_ARG(load_const_obj, mp_const_none);
2664 } else {
Damien George831137b2015-12-17 13:13:18 +00002665 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1]));
Damien George65ef6b72015-01-14 21:17:27 +00002666 }
2667}
2668
Damien George7d414a12015-02-08 01:57:40 +00002669STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002670 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2671 // nodes are 32-bit pointers, but need to extract 64-bit object
2672 EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2673 #else
Damien George7d414a12015-02-08 01:57:40 +00002674 EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +00002675 #endif
Damien George7d414a12015-02-08 01:57:40 +00002676}
2677
Damiend99b0522013-12-21 18:17:45 +00002678typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien George3ff16ff2016-05-20 12:38:15 +01002679STATIC const compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002680#define nc NULL
2681#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002682#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002683#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002684#undef nc
2685#undef c
2686#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002687 NULL,
2688 compile_string,
2689 compile_bytes,
Damien George7d414a12015-02-08 01:57:40 +00002690 compile_const_object,
Damien429d7192013-10-04 19:53:11 +01002691};
2692
Damien George6be0b0a2014-08-15 14:30:52 +01002693STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002694 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002695 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002696 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002697 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Damien Georgeea235202016-02-11 22:30:53 +00002698 #if MICROPY_DYNAMIC_COMPILER
2699 mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1));
2700 if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) {
2701 // integer fits in target runtime's small-int
2702 EMIT_ARG(load_const_small_int, arg);
2703 } else {
2704 // integer doesn't fit, so create a multi-precision int object
2705 // (but only create the actual object on the last pass)
2706 if (comp->pass != MP_PASS_EMIT) {
2707 EMIT_ARG(load_const_obj, mp_const_none);
2708 } else {
2709 EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg));
2710 }
2711 }
2712 #else
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002713 EMIT_ARG(load_const_small_int, arg);
Damien Georgeea235202016-02-11 22:30:53 +00002714 #endif
Damiend99b0522013-12-21 18:17:45 +00002715 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George831137b2015-12-17 13:13:18 +00002716 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002717 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien George542bd6b2015-03-26 14:42:40 +00002718 case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
Damien George59fba2d2015-06-25 14:42:13 +00002719 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break;
2720 case MP_PARSE_NODE_BYTES:
2721 // only create and load the actual bytes object on the last pass
2722 if (comp->pass != MP_PASS_EMIT) {
2723 EMIT_ARG(load_const_obj, mp_const_none);
2724 } else {
Damien Georgec3f64d92015-11-27 12:23:18 +00002725 size_t len;
Damien George59fba2d2015-06-25 14:42:13 +00002726 const byte *data = qstr_data(arg, &len);
2727 EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len));
2728 }
2729 break;
Damien Georged2d64f02015-01-14 21:32:42 +00002730 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002731 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002732 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002733 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002734 // do nothing
2735 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002737 }
2738 break;
Damien429d7192013-10-04 19:53:11 +01002739 }
2740 } else {
Damiend99b0522013-12-21 18:17:45 +00002741 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George41125902015-03-26 16:44:14 +00002742 EMIT_ARG(set_source_line, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002743 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien Georgedeaa57a2016-10-12 10:20:48 +11002744 assert(f != NULL);
2745 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01002746 }
2747}
2748
Damien George2ac4af62014-08-15 16:45:41 +01002749STATIC 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) {
Damien George9a569122015-11-23 16:50:42 +00002750 // check that **kw is last
2751 if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
2752 compile_syntax_error(comp, pn, "invalid syntax");
2753 return;
2754 }
2755
Damien George2827d622014-04-27 15:50:52 +01002756 qstr param_name = MP_QSTR_NULL;
2757 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002758 if (MP_PARSE_NODE_IS_ID(pn)) {
2759 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002760 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002761 // comes after a star, so counts as a keyword-only parameter
2762 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002763 } else {
Damien George2827d622014-04-27 15:50:52 +01002764 // comes before a star, so counts as a positional parameter
2765 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002766 }
Damienb14de212013-10-06 00:28:28 +01002767 } else {
Damiend99b0522013-12-21 18:17:45 +00002768 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2769 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2770 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2771 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002772 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002773 // comes after a star, so counts as a keyword-only parameter
2774 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002775 } else {
Damien George2827d622014-04-27 15:50:52 +01002776 // comes before a star, so counts as a positional parameter
2777 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002778 }
Damiend99b0522013-12-21 18:17:45 +00002779 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George9a569122015-11-23 16:50:42 +00002780 if (comp->have_star) {
2781 // more than one star
2782 compile_syntax_error(comp, pn, "invalid syntax");
2783 return;
2784 }
Damien George8b19db02014-04-11 23:25:34 +01002785 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002786 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002787 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002788 // bare star
2789 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002790 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002791 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002792 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002793 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002794 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George0bb97132015-02-27 14:25:47 +00002795 } else {
2796 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
Damien George8b19db02014-04-11 23:25:34 +01002797 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002798 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002799 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2800 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002801 }
Damien George0bb97132015-02-27 14:25:47 +00002802 } else {
2803 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
Damiend99b0522013-12-21 18:17:45 +00002804 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01002805 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00002806 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002807 }
Damien429d7192013-10-04 19:53:11 +01002808 }
2809
Damien George2827d622014-04-27 15:50:52 +01002810 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01002811 bool added;
2812 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2813 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01002814 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01002815 return;
2816 }
Damien429d7192013-10-04 19:53:11 +01002817 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01002818 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01002819 }
2820}
2821
Damien George2827d622014-04-27 15:50:52 +01002822STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002823 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01002824}
2825
Damien George2827d622014-04-27 15:50:52 +01002826STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01002827 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
2828}
2829
Damien Georgeea5b59b2015-09-04 16:44:14 +01002830#if MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01002831STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
2832 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
2833 // no annotation
2834 return;
2835 }
2836
2837 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2838 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
2839 // named parameter with possible annotation
2840 // fallthrough
2841 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
2842 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2843 // named star with possible annotation
2844 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2845 // fallthrough
2846 } else {
2847 // no annotation
2848 return;
2849 }
Damien Georgee49153f2016-10-11 12:29:54 +11002850 } else {
2851 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star);
Damien George2ac4af62014-08-15 16:45:41 +01002852 // double star with possible annotation
2853 // fallthrough
Damien George2ac4af62014-08-15 16:45:41 +01002854 }
2855
2856 mp_parse_node_t pn_annotation = pns->nodes[1];
2857
2858 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien George2ac4af62014-08-15 16:45:41 +01002859 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2860 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
2861 assert(id_info != NULL);
2862
Damien Georgeea5b59b2015-09-04 16:44:14 +01002863 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2864 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2865 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2866 } else {
2867 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01002868 }
Damien George2ac4af62014-08-15 16:45:41 +01002869 }
Damien429d7192013-10-04 19:53:11 +01002870}
Damien Georgeea5b59b2015-09-04 16:44:14 +01002871#endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01002872
Damien Georgea8312432015-12-18 01:37:55 +00002873STATIC 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) {
2874 uint l_top = comp_next_label(comp);
2875 uint l_end = comp_next_label(comp);
2876 EMIT_ARG(label_assign, l_top);
2877 EMIT_ARG(for_iter, l_end);
2878 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2879 mp_parse_node_t pn_iter = pns_comp_for->nodes[2];
2880
Damien429d7192013-10-04 19:53:11 +01002881 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002882 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002883 // no more nested if/for; compile inner expression
2884 compile_node(comp, pn_inner_expr);
Damien Georgea5624bf2016-09-18 23:59:47 +10002885 if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
Damien429d7192013-10-04 19:53:11 +01002886 EMIT(yield_value);
2887 EMIT(pop_top);
Damien Georgea5624bf2016-09-18 23:59:47 +10002888 } else {
2889 EMIT_ARG(store_comp, comp->scope_cur->kind, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002890 }
Damiend99b0522013-12-21 18:17:45 +00002891 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002892 // if condition
Damiend99b0522013-12-21 18:17:45 +00002893 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002894 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2895 pn_iter = pns_comp_if->nodes[1];
2896 goto tail_recursion;
Damien George0bb97132015-02-27 14:25:47 +00002897 } else {
2898 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
Damien429d7192013-10-04 19:53:11 +01002899 // for loop
Damiend99b0522013-12-21 18:17:45 +00002900 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002901 compile_node(comp, pns_comp_for2->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002902 EMIT(get_iter);
Damien Georgea8312432015-12-18 01:37:55 +00002903 compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1);
Damien429d7192013-10-04 19:53:11 +01002904 }
Damien Georgea8312432015-12-18 01:37:55 +00002905
2906 EMIT_ARG(jump, l_top);
2907 EMIT_ARG(label_assign, l_end);
2908 EMIT(for_iter_end);
Damien429d7192013-10-04 19:53:11 +01002909}
2910
Damien George1463c1f2014-04-25 23:52:57 +01002911STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien George65dc9602015-08-14 12:24:11 +01002912#if MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01002913 // see http://www.python.org/dev/peps/pep-0257/
2914
2915 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002916 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002917 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002918 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002919 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002920 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2921 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002922 for (int i = 0; i < num_nodes; i++) {
2923 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002924 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)) {
Damiene388f102013-12-12 15:24:38 +00002925 // not a newline, so this is the first statement; finish search
2926 break;
2927 }
2928 }
2929 // 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
Damiend99b0522013-12-21 18:17:45 +00002930 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002931 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002932 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002933 } else {
2934 return;
2935 }
2936
2937 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002938 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damien George4dea9222015-04-09 15:29:54 +00002939 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01002940 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
2941 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
2942 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
2943 // compile the doc string
2944 compile_node(comp, pns->nodes[0]);
2945 // store the doc string
Damien George542bd6b2015-03-26 14:42:40 +00002946 compile_store_id(comp, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002947 }
2948 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002949#else
2950 (void)comp;
2951 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01002952#endif
Damien429d7192013-10-04 19:53:11 +01002953}
2954
Damien George1463c1f2014-04-25 23:52:57 +01002955STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01002956 comp->pass = pass;
2957 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002958 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002959 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002960
Damien George36db6bc2014-05-07 17:24:22 +01002961 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00002962 // reset maximum stack sizes in scope
2963 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002964 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002965 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002966 }
2967
Damien429d7192013-10-04 19:53:11 +01002968 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002969 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2970 assert(scope->kind == SCOPE_MODULE);
2971 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2972 compile_node(comp, pns->nodes[0]); // compile the expression
2973 EMIT(return_value);
2974 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002975 if (!comp->is_repl) {
2976 check_for_doc_string(comp, scope->pn);
2977 }
Damien429d7192013-10-04 19:53:11 +01002978 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002979 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002980 EMIT(return_value);
2981 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002982 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2983 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2984 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002985
2986 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002987 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01002988 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01002989 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01002990 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien Georgeea5b59b2015-09-04 16:44:14 +01002991 }
2992 #if MICROPY_EMIT_NATIVE
2993 else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
Damien George2ac4af62014-08-15 16:45:41 +01002994 // compile annotations; only needed on latter compiler passes
Damien Georgeea5b59b2015-09-04 16:44:14 +01002995 // only needed for viper emitter
Damien429d7192013-10-04 19:53:11 +01002996
Damien George2ac4af62014-08-15 16:45:41 +01002997 // argument annotations
2998 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
2999
3000 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003001 mp_parse_node_t pn_annotation = pns->nodes[2];
3002 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien Georgeea5b59b2015-09-04 16:44:14 +01003003 // nodes[2] can be null or a test-expr
3004 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3005 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3006 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3007 } else {
3008 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003009 }
3010 }
Damien George2ac4af62014-08-15 16:45:41 +01003011 }
Damien Georgeea5b59b2015-09-04 16:44:14 +01003012 #endif // MICROPY_EMIT_NATIVE
Damien429d7192013-10-04 19:53:11 +01003013
3014 compile_node(comp, pns->nodes[3]); // 3 is function body
3015 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003016 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003017 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003018 EMIT(return_value);
3019 }
3020 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003021 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3022 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3023 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003024
3025 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003026 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003027 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003028 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003029 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3030 }
3031
3032 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003033
3034 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3035 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3036 EMIT(pop_top);
3037 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3038 }
Damien429d7192013-10-04 19:53:11 +01003039 EMIT(return_value);
3040 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3041 // a bit of a hack at the moment
3042
Damiend99b0522013-12-21 18:17:45 +00003043 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3044 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3045 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3046 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3047 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003048
Damien George708c0732014-04-27 19:23:46 +01003049 // We need a unique name for the comprehension argument (the iterator).
3050 // CPython uses .0, but we should be able to use anything that won't
3051 // clash with a user defined variable. Best to use an existing qstr,
3052 // so we use the blank qstr.
Damien George708c0732014-04-27 19:23:46 +01003053 qstr qstr_arg = MP_QSTR_;
Damien George36db6bc2014-05-07 17:24:22 +01003054 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003055 bool added;
3056 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3057 assert(added);
3058 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003059 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003060 }
3061
3062 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003063 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003064 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003065 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003066 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003067 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003068 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003069 #endif
Damien429d7192013-10-04 19:53:11 +01003070 }
3071
Damien George542bd6b2015-03-26 14:42:40 +00003072 compile_load_id(comp, qstr_arg);
Damien Georgea8312432015-12-18 01:37:55 +00003073 compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
Damien429d7192013-10-04 19:53:11 +01003074
3075 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003076 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003077 }
3078 EMIT(return_value);
3079 } else {
3080 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003081 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3082 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3083 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003084
Damien George36db6bc2014-05-07 17:24:22 +01003085 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003086 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003087 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003088 assert(added);
3089 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003090 }
3091
Damien George542bd6b2015-03-26 14:42:40 +00003092 compile_load_id(comp, MP_QSTR___name__);
3093 compile_store_id(comp, MP_QSTR___module__);
Damien George59fba2d2015-06-25 14:42:13 +00003094 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien George542bd6b2015-03-26 14:42:40 +00003095 compile_store_id(comp, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003096
3097 check_for_doc_string(comp, pns->nodes[2]);
3098 compile_node(comp, pns->nodes[2]); // 2 is class body
3099
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003100 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003101 assert(id != NULL);
3102 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003103 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003104 } else {
Damien George542bd6b2015-03-26 14:42:40 +00003105 EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
Damien429d7192013-10-04 19:53:11 +01003106 }
3107 EMIT(return_value);
3108 }
3109
Damien415eb6f2013-10-05 12:19:06 +01003110 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003111
3112 // make sure we match all the exception levels
3113 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003114}
3115
Damien Georgead297a12016-12-09 13:17:49 +11003116#if MICROPY_EMIT_INLINE_ASM
Damien George36db6bc2014-05-07 17:24:22 +01003117// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003118STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003119 comp->pass = pass;
3120 comp->scope_cur = scope;
3121 comp->next_label = 1;
3122
3123 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003124 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003125 return;
3126 }
3127
Damien George36db6bc2014-05-07 17:24:22 +01003128 if (comp->pass > MP_PASS_SCOPE) {
Damien George8dfbd2d2015-02-13 01:00:51 +00003129 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error);
Damiena2f2f7d2013-10-06 00:14:13 +01003130 }
3131
Damien826005c2013-10-05 23:17:28 +01003132 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003133 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3134 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3135 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003136
Damiend99b0522013-12-21 18:17:45 +00003137 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003138
Damiena2f2f7d2013-10-06 00:14:13 +01003139 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003140 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003141 mp_parse_node_t *pn_params;
Damien Georgedfe944c2015-02-13 02:29:46 +00003142 int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003143 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damien George8dfbd2d2015-02-13 01:00:51 +00003144 if (comp->compile_error != MP_OBJ_NULL) {
3145 goto inline_asm_error;
3146 }
Damiena2f2f7d2013-10-06 00:14:13 +01003147 }
3148
Damien George8f54c082016-01-15 15:20:43 +00003149 // pns->nodes[2] is function return annotation
3150 mp_uint_t type_sig = MP_NATIVE_TYPE_INT;
3151 mp_parse_node_t pn_annotation = pns->nodes[2];
3152 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3153 // nodes[2] can be null or a test-expr
3154 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3155 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3156 switch (ret_type) {
3157 case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break;
3158 case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break;
3159 case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break;
3160 case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break;
3161 default: compile_syntax_error(comp, pn_annotation, "unknown type"); return;
3162 }
3163 } else {
3164 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3165 }
3166 }
Damien826005c2013-10-05 23:17:28 +01003167
Damiend99b0522013-12-21 18:17:45 +00003168 mp_parse_node_t pn_body = pns->nodes[3]; // body
3169 mp_parse_node_t *nodes;
Damien Georgedfe944c2015-02-13 02:29:46 +00003170 int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
Damien826005c2013-10-05 23:17:28 +01003171
Damien826005c2013-10-05 23:17:28 +01003172 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003173 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3174 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003175 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3176 // no instructions
3177 continue;
Damien George3d7bf5d2015-02-16 17:46:28 +00003178 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) {
Damien Georgea26dc502014-04-12 17:54:52 +01003179 // not an instruction; error
Damien George3d7bf5d2015-02-16 17:46:28 +00003180 not_an_instruction:
Damien George3665d0b2015-03-03 17:11:18 +00003181 compile_syntax_error(comp, nodes[i], "expecting an assembler instruction");
Damien Georgea26dc502014-04-12 17:54:52 +01003182 return;
3183 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003184
3185 // check structure of parse node
Damiend99b0522013-12-21 18:17:45 +00003186 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
Damien George3d7bf5d2015-02-16 17:46:28 +00003187 if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) {
3188 goto not_an_instruction;
3189 }
Damiend99b0522013-12-21 18:17:45 +00003190 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
pohmelie81ebba72016-01-27 23:23:11 +03003191 if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) {
Damien George3d7bf5d2015-02-16 17:46:28 +00003192 goto not_an_instruction;
3193 }
3194 if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) {
3195 goto not_an_instruction;
3196 }
3197 if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) {
3198 goto not_an_instruction;
3199 }
Damien George3d7bf5d2015-02-16 17:46:28 +00003200
3201 // parse node looks like an instruction
3202 // get instruction name and args
Damiend99b0522013-12-21 18:17:45 +00003203 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3204 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3205 mp_parse_node_t *pn_arg;
Damien Georgedfe944c2015-02-13 02:29:46 +00003206 int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
Damien826005c2013-10-05 23:17:28 +01003207
3208 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003209 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003210 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003211 compile_syntax_error(comp, nodes[i], "'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003212 return;
3213 }
Damien George6f355fd2014-04-10 14:11:31 +01003214 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003215 if (pass > MP_PASS_SCOPE) {
Damien George9c5cabb2015-03-03 17:08:02 +00003216 if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) {
3217 compile_syntax_error(comp, nodes[i], "label redefined");
3218 return;
3219 }
Damien826005c2013-10-05 23:17:28 +01003220 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003221 } else if (op == MP_QSTR_align) {
3222 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003223 compile_syntax_error(comp, nodes[i], "'align' requires 1 argument");
Damien Georgee5f8a772014-04-21 13:33:15 +01003224 return;
3225 }
Damien George36db6bc2014-05-07 17:24:22 +01003226 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003227 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3228 }
3229 } else if (op == MP_QSTR_data) {
3230 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
Damien George3665d0b2015-03-03 17:11:18 +00003231 compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003232 return;
3233 }
Damien George36db6bc2014-05-07 17:24:22 +01003234 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003235 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003236 for (uint j = 1; j < n_args; j++) {
3237 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien George3665d0b2015-03-03 17:11:18 +00003238 compile_syntax_error(comp, nodes[i], "'data' requires integer arguments");
Damien Georgee5f8a772014-04-21 13:33:15 +01003239 return;
3240 }
Damien George50912e72015-01-20 11:55:10 +00003241 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003242 }
3243 }
Damien826005c2013-10-05 23:17:28 +01003244 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003245 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003246 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003247 }
3248 }
Damien George8dfbd2d2015-02-13 01:00:51 +00003249
3250 if (comp->compile_error != MP_OBJ_NULL) {
3251 pns = pns2; // this is the parse node that had the error
3252 goto inline_asm_error;
3253 }
Damien826005c2013-10-05 23:17:28 +01003254 }
3255
Damien George36db6bc2014-05-07 17:24:22 +01003256 if (comp->pass > MP_PASS_SCOPE) {
Damien George8f54c082016-01-15 15:20:43 +00003257 EMIT_INLINE_ASM_ARG(end_pass, type_sig);
Damien George8dfbd2d2015-02-13 01:00:51 +00003258 }
3259
3260 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgecfc4c332015-07-29 22:16:01 +00003261 // inline assembler had an error; set line for its exception
Damien George8dfbd2d2015-02-13 01:00:51 +00003262 inline_asm_error:
Damien Georgecfc4c332015-07-29 22:16:01 +00003263 comp->compile_error_line = pns->source_line;
Damienb05d7072013-10-05 13:37:10 +01003264 }
Damien429d7192013-10-04 19:53:11 +01003265}
Damien George094d4502014-04-02 17:31:27 +01003266#endif
Damien429d7192013-10-04 19:53:11 +01003267
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003268STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003269 // in Micro Python we put the *x parameter after all other parameters (except **y)
3270 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3271 id_info_t *id_param = NULL;
3272 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3273 id_info_t *id = &scope->id_info[i];
3274 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3275 if (id_param != NULL) {
3276 // swap star param with last param
3277 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3278 }
3279 break;
3280 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3281 id_param = id;
3282 }
3283 }
3284 }
Damien George2827d622014-04-27 15:50:52 +01003285
Damien429d7192013-10-04 19:53:11 +01003286 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003287 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003288 scope->num_locals = 0;
3289 for (int i = 0; i < scope->id_info_len; i++) {
3290 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003291 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003292 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3293 continue;
3294 }
Damien George3dea8c92016-09-30 12:34:05 +10003295 if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +01003296 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3297 }
Damien George2827d622014-04-27 15:50:52 +01003298 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003299 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003300 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003301 }
3302 }
3303
Damien George65dc9602015-08-14 12:24:11 +01003304 // compute the index of cell vars
Damien9ecbcff2013-12-11 00:41:43 +00003305 for (int i = 0; i < scope->id_info_len; i++) {
3306 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003307 // in Micro Python the cells come right after the fast locals
3308 // parameters are not counted here, since they remain at the start
3309 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003310 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003311 id->local_num = scope->num_locals;
3312 scope->num_locals += 1;
3313 }
Damien9ecbcff2013-12-11 00:41:43 +00003314 }
Damien9ecbcff2013-12-11 00:41:43 +00003315
Damien George65dc9602015-08-14 12:24:11 +01003316 // compute the index of free vars
Damien9ecbcff2013-12-11 00:41:43 +00003317 // make sure they are in the order of the parent scope
3318 if (scope->parent != NULL) {
3319 int num_free = 0;
3320 for (int i = 0; i < scope->parent->id_info_len; i++) {
3321 id_info_t *id = &scope->parent->id_info[i];
3322 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3323 for (int j = 0; j < scope->id_info_len; j++) {
3324 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003325 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003326 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003327 // in Micro Python the frees come first, before the params
3328 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003329 num_free += 1;
3330 }
3331 }
3332 }
Damien429d7192013-10-04 19:53:11 +01003333 }
Damien George6baf76e2013-12-30 22:32:17 +00003334 // in Micro Python shift all other locals after the free locals
3335 if (num_free > 0) {
3336 for (int i = 0; i < scope->id_info_len; i++) {
3337 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003338 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003339 id->local_num += num_free;
3340 }
3341 }
Damien George2827d622014-04-27 15:50:52 +01003342 scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function
Damien George6baf76e2013-12-30 22:32:17 +00003343 scope->num_locals += num_free;
3344 }
Damien429d7192013-10-04 19:53:11 +01003345 }
Damien429d7192013-10-04 19:53:11 +01003346}
3347
Damien Georged4dba882015-11-13 13:38:28 +00003348#if !MICROPY_PERSISTENT_CODE_SAVE
3349STATIC
3350#endif
3351mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
Damien George9d5e5c02015-09-24 13:15:57 +01003352 // put compiler state on the stack, it's relatively small
3353 compiler_t comp_state = {0};
3354 compiler_t *comp = &comp_state;
3355
Damien Georgecbd2f742014-01-19 11:48:48 +00003356 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003357 comp->is_repl = is_repl;
Damien429d7192013-10-04 19:53:11 +01003358
Damien George62a3a282015-03-01 12:04:05 +00003359 // create the module scope
Damien George58e0f4a2015-09-23 10:50:43 +01003360 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);
Damien George62a3a282015-03-01 12:04:05 +00003361
Damien Georgea210c772015-03-26 15:49:53 +00003362 // create standard emitter; it's used at least for MP_PASS_SCOPE
Damien Georgea210c772015-03-26 15:49:53 +00003363 emit_t *emit_bc = emit_bc_new();
Damien Georgea210c772015-03-26 15:49:53 +00003364
Damien826005c2013-10-05 23:17:28 +01003365 // compile pass 1
Damien Georgea210c772015-03-26 15:49:53 +00003366 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003367 #if MICROPY_EMIT_NATIVE
Damien Georgea210c772015-03-26 15:49:53 +00003368 comp->emit_method_table = &emit_bc_method_table;
3369 #endif
Damien826005c2013-10-05 23:17:28 +01003370 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003371 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003372 if (false) {
Damien Georgead297a12016-12-09 13:17:49 +11003373 #if MICROPY_EMIT_INLINE_ASM
3374 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
Damien George36db6bc2014-05-07 17:24:22 +01003375 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damien Georgead297a12016-12-09 13:17:49 +11003376 #endif
Damien826005c2013-10-05 23:17:28 +01003377 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003378 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003379 }
3380
3381 // update maximim number of labels needed
3382 if (comp->next_label > max_num_labels) {
3383 max_num_labels = comp->next_label;
3384 }
Damien429d7192013-10-04 19:53:11 +01003385 }
3386
Damien826005c2013-10-05 23:17:28 +01003387 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003388 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003389 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003390 }
3391
Damien Georgea210c772015-03-26 15:49:53 +00003392 // set max number of labels now that it's calculated
Damien Georgea210c772015-03-26 15:49:53 +00003393 emit_bc_set_max_num_labels(emit_bc, max_num_labels);
Damien Georgea210c772015-03-26 15:49:53 +00003394
Damien826005c2013-10-05 23:17:28 +01003395 // compile pass 2 and 3
Damien Georgee67ed5d2014-01-04 13:55:24 +00003396#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003397 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003398#endif
Damien Georgea91ac202014-10-05 19:01:34 +01003399 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003400 if (false) {
3401 // dummy
3402
Damien Georgead297a12016-12-09 13:17:49 +11003403 #if MICROPY_EMIT_INLINE_ASM
3404 } else if (s->emit_options == MP_EMIT_OPT_ASM) {
3405 // inline assembly
3406 if (comp->emit_inline_asm == NULL) {
3407 comp->emit_inline_asm = ASM_EMITTER(new)(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003408 }
3409 comp->emit = NULL;
Damien Georgead297a12016-12-09 13:17:49 +11003410 comp->emit_inline_asm_method_table = &ASM_EMITTER(method_table);
Damien George36db6bc2014-05-07 17:24:22 +01003411 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003412 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003413 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003414 }
Damien Georgead297a12016-12-09 13:17:49 +11003415 #endif
Damienc025ebb2013-10-12 14:30:21 +01003416
Damien826005c2013-10-05 23:17:28 +01003417 } else {
Damienc025ebb2013-10-12 14:30:21 +01003418
3419 // choose the emit type
3420
Damien826005c2013-10-05 23:17:28 +01003421 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003422
3423#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003424 case MP_EMIT_OPT_NATIVE_PYTHON:
3425 case MP_EMIT_OPT_VIPER:
Damiendc833822013-10-06 01:01:01 +01003426 if (emit_native == NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003427 emit_native = NATIVE_EMITTER(new)(&comp->compile_error, max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003428 }
Damien George080a78b2016-12-07 11:17:17 +11003429 comp->emit_method_table = &NATIVE_EMITTER(method_table);
Damienc025ebb2013-10-12 14:30:21 +01003430 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003431 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003432 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003433#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003434
Damien826005c2013-10-05 23:17:28 +01003435 default:
Damien826005c2013-10-05 23:17:28 +01003436 comp->emit = emit_bc;
Damien George41125902015-03-26 16:44:14 +00003437 #if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003438 comp->emit_method_table = &emit_bc_method_table;
Damien George41125902015-03-26 16:44:14 +00003439 #endif
Damien826005c2013-10-05 23:17:28 +01003440 break;
3441 }
Damienc025ebb2013-10-12 14:30:21 +01003442
Damien George1e1779e2015-01-14 00:20:28 +00003443 // need a pass to compute stack size
3444 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3445
Damien George36db6bc2014-05-07 17:24:22 +01003446 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003447 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003448 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3449 }
3450
3451 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003452 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003453 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003454 }
Damien6cdd3af2013-10-05 18:08:26 +01003455 }
Damien429d7192013-10-04 19:53:11 +01003456 }
3457
Damien Georgecfc4c332015-07-29 22:16:01 +00003458 if (comp->compile_error != MP_OBJ_NULL) {
3459 // if there is no line number for the error then use the line
3460 // number for the start of this scope
3461 compile_error_set_line(comp, comp->scope_cur->pn);
3462 // add a traceback to the exception using relevant source info
3463 mp_obj_exception_add_traceback(comp->compile_error, comp->source_file,
3464 comp->compile_error_line, comp->scope_cur->simple_name);
3465 }
3466
Damien George41d02b62014-01-24 22:42:28 +00003467 // free the emitters
Damien Georgea210c772015-03-26 15:49:53 +00003468
Damien Georgea210c772015-03-26 15:49:53 +00003469 emit_bc_free(emit_bc);
Damien George41d02b62014-01-24 22:42:28 +00003470#if MICROPY_EMIT_NATIVE
3471 if (emit_native != NULL) {
Damien George080a78b2016-12-07 11:17:17 +11003472 NATIVE_EMITTER(free)(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003473 }
3474#endif
Damien Georgead297a12016-12-09 13:17:49 +11003475 #if MICROPY_EMIT_INLINE_ASM
3476 if (comp->emit_inline_asm != NULL) {
3477 ASM_EMITTER(free)(comp->emit_inline_asm);
Damien George41d02b62014-01-24 22:42:28 +00003478 }
Damien Georgead297a12016-12-09 13:17:49 +11003479 #endif
Damien George41d02b62014-01-24 22:42:28 +00003480
Damien George52b5d762014-09-23 15:31:56 +00003481 // free the parse tree
Damien George58e0f4a2015-09-23 10:50:43 +01003482 mp_parse_tree_clear(parse_tree);
Damien George52b5d762014-09-23 15:31:56 +00003483
Damien George41d02b62014-01-24 22:42:28 +00003484 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003485 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003486 for (scope_t *s = module_scope; s;) {
3487 scope_t *next = s->next;
3488 scope_free(s);
3489 s = next;
3490 }
Damien5ac1b2e2013-10-18 19:58:12 +01003491
Damien George9d5e5c02015-09-24 13:15:57 +01003492 if (comp->compile_error != MP_OBJ_NULL) {
3493 nlr_raise(comp->compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003494 } else {
Damien Georged4dba882015-11-13 13:38:28 +00003495 return outer_raw_code;
Damien George1fb03172014-01-03 14:22:03 +00003496 }
Damien429d7192013-10-04 19:53:11 +01003497}
Damien Georged4dba882015-11-13 13:38:28 +00003498
3499mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) {
3500 mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl);
3501 // return function that executes the outer module
3502 return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
3503}
Damien Georgedd5353a2015-12-18 12:35:44 +00003504
3505#endif // MICROPY_ENABLE_COMPILER