blob: 66e2996c445f8e58bfeaa428e33f9071bc9793df [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 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
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>
Rachel Dowdall56402792014-03-22 20:19:24 +000032#include <math.h>
Damien429d7192013-10-04 19:53:11 +010033
Damiend99b0522013-12-21 18:17:45 +000034#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030035#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010037#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010038#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien George1fb03172014-01-03 14:22:03 +000040#include "obj.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010041#include "emitglue.h"
42#include "scope.h"
43#include "emit.h"
Damien George1fb03172014-01-03 14:22:03 +000044#include "compile.h"
45#include "runtime.h"
Damien Georgea26dc502014-04-12 17:54:52 +010046#include "builtin.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000047#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010048
49// TODO need to mangle __attr names
50
51typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000052#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010053#include "grammar.h"
54#undef DEF_RULE
55 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010056 PN_string, // special node for non-interned string
Damien429d7192013-10-04 19:53:11 +010057} pn_kind_t;
58
Damien Georgeb9791222014-01-23 00:34:21 +000059#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
60#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
61#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
62#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010063
Damien Georgea91ac202014-10-05 19:01:34 +010064// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010065typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000066 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010067
Damien George78035b92014-04-09 12:27:39 +010068 uint8_t is_repl;
69 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010070 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010071 uint8_t have_star;
72
73 // try to keep compiler clean from nlr
74 // this is set to an exception object if we have a compile error
75 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010076
Damien George6f355fd2014-04-10 14:11:31 +010077 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010078
Damien George69b89d22014-04-11 13:38:30 +000079 uint16_t num_dict_params;
80 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010081
Damien Georgea91ac202014-10-05 19:01:34 +010082 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
83 uint16_t continue_label;
84 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000085 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010086
Damien429d7192013-10-04 19:53:11 +010087 scope_t *scope_head;
88 scope_t *scope_cur;
89
Damien6cdd3af2013-10-05 18:08:26 +010090 emit_t *emit; // current emitter
91 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010092
93 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
94 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010095} compiler_t;
96
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010097STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010098 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
99 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100100 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +0100101 mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100102 } else {
Damien Georgea91ac202014-10-05 19:01:34 +0100103 // we don't have a line number, so just pass 0
104 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100105 }
Damien Georgea91ac202014-10-05 19:01:34 +0100106 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000107}
108
Damien George57e99eb2014-04-10 22:42:11 +0100109STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300110 #if MICROPY_PY_UCTYPES
111 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
112 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100113 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100114 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100115};
116
117STATIC const mp_map_t mp_constants_map = {
118 .all_keys_are_qstrs = 1,
119 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200120 .used = MP_ARRAY_SIZE(mp_constants_table),
121 .alloc = MP_ARRAY_SIZE(mp_constants_table),
Damien George57e99eb2014-04-10 22:42:11 +0100122 .table = (mp_map_elem_t*)mp_constants_table,
123};
124
Damien Georgeffae48d2014-05-08 15:58:39 +0000125// this function is essentially a simple preprocessor
126STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
127 if (0) {
128 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100129#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000130 } else if (MP_PARSE_NODE_IS_ID(pn)) {
131 // lookup identifier in table of dynamic constants
132 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
133 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
134 if (elem != NULL) {
135 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
136 }
137#endif
138 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000139 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100140
Damien Georgeffae48d2014-05-08 15:58:39 +0000141 // fold some parse nodes before folding their arguments
142 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100143#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000144 case PN_expr_stmt:
145 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
146 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
147 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
148 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
149 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
150 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
151 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
152 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
153 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
154 ) {
155 // code to assign dynamic constants: id = const(value)
156
157 // get the id
158 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
159
160 // get the value
161 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
162 pn_value = fold_constants(comp, pn_value, consts);
163 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
164 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
165 break;
166 }
Damien George40f3c022014-07-03 13:25:24 +0100167 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000168
169 // store the value in the table of dynamic constants
170 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
171 if (elem->value != MP_OBJ_NULL) {
172 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
173 break;
174 }
175 elem->value = MP_OBJ_NEW_SMALL_INT(value);
176
177 // replace const(value) with value
178 pns1->nodes[0] = pn_value;
179
180 // finished folding this assignment
181 return pn;
182 }
183 }
184 }
185 break;
186#endif
Damien George5042bce2014-05-25 22:06:06 +0100187 case PN_string:
188 return pn;
Damien429d7192013-10-04 19:53:11 +0100189 }
190
Damien Georgeffae48d2014-05-08 15:58:39 +0000191 // fold arguments
192 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
193 for (int i = 0; i < n; i++) {
194 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
195 }
196
197 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000198 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100199 case PN_atom_paren:
200 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
201 // (int)
202 pn = pns->nodes[0];
203 }
204 break;
205
206 case PN_expr:
207 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
208 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100209 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
210 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100211 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
212 }
213 break;
214
215 case PN_and_expr:
216 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
217 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100218 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
219 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100220 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
221 }
222 break;
223
Damien429d7192013-10-04 19:53:11 +0100224 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000225 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100226 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
227 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000228 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100229 // int << int
230 if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
231 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
232 }
Damiend99b0522013-12-21 18:17:45 +0000233 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100234 // int >> int
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200235 if (arg1 >= BITS_PER_WORD) {
236 // Shifting to big amounts is underfined behavior
237 // in C and is CPU-dependent; propagate sign bit.
238 arg1 = BITS_PER_WORD - 1;
239 }
Damiend99b0522013-12-21 18:17:45 +0000240 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100241 } else {
242 // shouldn't happen
243 assert(0);
244 }
245 }
246 break;
247
248 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100249 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000250 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100251 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
252 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000253 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100254 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000255 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000256 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100257 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000258 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100259 } else {
260 // shouldn't happen
261 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100262 }
Damien Georged1e355e2014-05-28 14:51:12 +0100263 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100264 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000265 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100266 }
267 }
268 break;
269
270 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000271 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien George40f3c022014-07-03 13:25:24 +0100272 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
273 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000274 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100275 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000276 if (!mp_small_int_mul_overflow(arg0, arg1)) {
277 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100278 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000279 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
280 }
281 }
Damiend99b0522013-12-21 18:17:45 +0000282 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100283 // int / int
284 // pass
Damiend99b0522013-12-21 18:17:45 +0000285 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100286 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000287 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000288 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300289 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100290 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000291 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300292 }
Damien429d7192013-10-04 19:53:11 +0100293 } else {
294 // shouldn't happen
295 assert(0);
296 }
297 }
298 break;
299
300 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000301 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100302 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000303 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100304 // +int
Damiend99b0522013-12-21 18:17:45 +0000305 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
306 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100307 // -int
Damiend99b0522013-12-21 18:17:45 +0000308 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
309 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100310 // ~int
Damiend99b0522013-12-21 18:17:45 +0000311 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100312 } else {
313 // shouldn't happen
314 assert(0);
315 }
316 }
317 break;
318
319 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100320 if (0) {
321#if MICROPY_EMIT_CPYTHON
322 } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgea26dc502014-04-12 17:54:52 +0100323 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100324 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000325 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
326 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200327 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100328 if (power >= 0) {
329 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200330 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100331 for (; power > 0; power--) {
332 ans *= base;
333 }
Damiend99b0522013-12-21 18:17:45 +0000334 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100335 }
336 }
Damien George57e99eb2014-04-10 22:42:11 +0100337#endif
338 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
339 // id.id
340 // look it up in constant table, see if it can be replaced with an integer
341 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
342 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
343 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
344 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
345 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
346 if (elem != NULL) {
347 mp_obj_t dest[2];
348 mp_load_method_maybe(elem->value, q_attr, dest);
349 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100350 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georged1e355e2014-05-28 14:51:12 +0100351 if (MP_SMALL_INT_FITS(val)) {
Damien George57e99eb2014-04-10 22:42:11 +0100352 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
353 }
354 }
355 }
Damien429d7192013-10-04 19:53:11 +0100356 }
357 break;
358 }
359 }
360
361 return pn;
362}
363
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200364STATIC 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 +0100365STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000366STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100367
Damien George6f355fd2014-04-10 14:11:31 +0100368STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100369 return comp->next_label++;
370}
371
Damien George8dcc0c72014-03-27 10:55:21 +0000372STATIC void compile_increase_except_level(compiler_t *comp) {
373 comp->cur_except_level += 1;
374 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
375 comp->scope_cur->exc_stack_size = comp->cur_except_level;
376 }
377}
378
379STATIC void compile_decrease_except_level(compiler_t *comp) {
380 assert(comp->cur_except_level > 0);
381 comp->cur_except_level -= 1;
382}
383
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200384STATIC 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 +0100385 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100386 scope->parent = comp->scope_cur;
387 scope->next = NULL;
388 if (comp->scope_head == NULL) {
389 comp->scope_head = scope;
390 } else {
391 scope_t *s = comp->scope_head;
392 while (s->next != NULL) {
393 s = s->next;
394 }
395 s->next = scope;
396 }
397 return scope;
398}
399
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200400STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000401 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
402 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
403 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100404 for (int i = 0; i < num_nodes; i++) {
405 f(comp, pns->nodes[i]);
406 }
Damiend99b0522013-12-21 18:17:45 +0000407 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100408 f(comp, pn);
409 }
410}
411
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200412STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000413 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100414 *nodes = NULL;
415 return 0;
Damiend99b0522013-12-21 18:17:45 +0000416 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100417 *nodes = pn;
418 return 1;
419 } else {
Damiend99b0522013-12-21 18:17:45 +0000420 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
421 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100422 *nodes = pn;
423 return 1;
424 } else {
425 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000426 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100427 }
428 }
429}
430
Damien George969a6b32014-12-10 22:07:04 +0000431STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000432 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100433 for (int i = 0; i < num_nodes; i++) {
434 compile_node(comp, pns->nodes[i]);
435 }
436}
437
Damien3ef4abb2013-10-12 16:53:13 +0100438#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200439STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100440 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
441 return true;
442 }
Damiend99b0522013-12-21 18:17:45 +0000443 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100444 return false;
445 }
Damiend99b0522013-12-21 18:17:45 +0000446 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100447 return false;
448 }
449 return true;
450}
451
Damien George5042bce2014-05-25 22:06:06 +0100452STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000453 bool has_single_quote = false;
454 bool has_double_quote = false;
455 for (int i = 0; i < len; i++) {
456 if (str[i] == '\'') {
457 has_single_quote = true;
458 } else if (str[i] == '"') {
459 has_double_quote = true;
460 }
461 }
462 if (bytes) {
463 vstr_printf(vstr, "b");
464 }
465 bool quote_single = false;
466 if (has_single_quote && !has_double_quote) {
467 vstr_printf(vstr, "\"");
468 } else {
469 quote_single = true;
470 vstr_printf(vstr, "'");
471 }
472 for (int i = 0; i < len; i++) {
473 if (str[i] == '\n') {
474 vstr_printf(vstr, "\\n");
475 } else if (str[i] == '\\') {
476 vstr_printf(vstr, "\\\\");
477 } else if (str[i] == '\'' && quote_single) {
478 vstr_printf(vstr, "\\'");
479 } else {
480 vstr_printf(vstr, "%c", str[i]);
481 }
482 }
483 if (has_single_quote && !has_double_quote) {
484 vstr_printf(vstr, "\"");
485 } else {
486 vstr_printf(vstr, "'");
487 }
488}
489
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200490STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George5042bce2014-05-25 22:06:06 +0100491 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
492 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George40f3c022014-07-03 13:25:24 +0100493 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false);
Damien George5042bce2014-05-25 22:06:06 +0100494 return;
495 }
496
Damiend99b0522013-12-21 18:17:45 +0000497 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200498 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
499 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
500 return;
501 }
502
Damien George42f3de92014-10-03 17:44:14 +0000503 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000504 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
505 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000506 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
507 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100508 case MP_PARSE_NODE_STRING:
509 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100510 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100511 const byte *str = qstr_data(arg, &len);
512 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
513 break;
514 }
Damiend99b0522013-12-21 18:17:45 +0000515 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100516 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000517 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
518 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
519 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100520 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100521 }
522 break;
523 default: assert(0);
524 }
525}
526
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200527STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100528 int n = 0;
529 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000530 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100531 }
532 int total = n;
533 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000534 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100535 total += 1;
Damien3a205172013-10-12 15:01:56 +0100536 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100537 is_const = false;
538 }
539 }
540 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100541 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100542 is_const = false;
543 break;
544 }
545 }
546 if (total > 0 && is_const) {
547 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000548 vstr_t *vstr = vstr_new();
549 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000550 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000551 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100552 need_comma = true;
553 }
554 for (int i = 0; i < n; i++) {
555 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000556 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100557 }
Damien02f89412013-12-12 15:13:36 +0000558 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100559 need_comma = true;
560 }
561 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000562 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100563 } else {
Damien02f89412013-12-12 15:13:36 +0000564 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100565 }
Damien Georgeb9791222014-01-23 00:34:21 +0000566 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000567 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100568 } else {
Damiend99b0522013-12-21 18:17:45 +0000569 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100570 compile_node(comp, pn);
571 }
572 for (int i = 0; i < n; i++) {
573 compile_node(comp, pns_list->nodes[i]);
574 }
Damien Georgeb9791222014-01-23 00:34:21 +0000575 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100576 }
577}
Damien3a205172013-10-12 15:01:56 +0100578#endif
579
580// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100581STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100582#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100583 cpython_c_tuple(comp, pn, pns_list);
584#else
585 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000586 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100587 compile_node(comp, pn);
588 total += 1;
589 }
590 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000591 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100592 for (int i = 0; i < n; i++) {
593 compile_node(comp, pns_list->nodes[i]);
594 }
595 total += n;
596 }
Damien Georgeb9791222014-01-23 00:34:21 +0000597 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100598#endif
599}
Damien429d7192013-10-04 19:53:11 +0100600
Damien George969a6b32014-12-10 22:07:04 +0000601STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100602 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000603 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100604}
605
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200606STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000607 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
608 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100609}
610
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200611STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000612 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
613 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100614}
615
Damien3ef4abb2013-10-12 16:53:13 +0100616#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100617// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200618STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100619 if (node_is_const_false(pn)) {
620 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000621 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100622 }
623 return;
624 } else if (node_is_const_true(pn)) {
625 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000626 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100627 }
628 return;
Damiend99b0522013-12-21 18:17:45 +0000629 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
630 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
631 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
632 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100633 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100634 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100635 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100636 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100637 }
Damien3a205172013-10-12 15:01:56 +0100638 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000639 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100640 } else {
641 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100642 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100643 }
644 }
645 return;
Damiend99b0522013-12-21 18:17:45 +0000646 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100647 if (jump_if == false) {
648 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100649 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100650 }
651 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100652 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100653 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100654 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100655 }
Damien3a205172013-10-12 15:01:56 +0100656 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000657 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100658 }
659 return;
Damiend99b0522013-12-21 18:17:45 +0000660 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100661 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100662 return;
663 }
664 }
665
666 // nothing special, fall back to default compiling for node and jump
667 compile_node(comp, pn);
668 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100670 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000671 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100672 }
673}
Damien3a205172013-10-12 15:01:56 +0100674#endif
Damien429d7192013-10-04 19:53:11 +0100675
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200676STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100677#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100678 cpython_c_if_cond(comp, pn, jump_if, label, false);
679#else
680 if (node_is_const_false(pn)) {
681 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000682 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100683 }
684 return;
685 } else if (node_is_const_true(pn)) {
686 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000687 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100688 }
689 return;
Damiend99b0522013-12-21 18:17:45 +0000690 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
691 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
692 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
693 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100694 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100695 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100696 for (int i = 0; i < n - 1; i++) {
697 c_if_cond(comp, pns->nodes[i], true, label2);
698 }
699 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000700 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100701 } else {
702 for (int i = 0; i < n; i++) {
703 c_if_cond(comp, pns->nodes[i], true, label);
704 }
705 }
706 return;
Damiend99b0522013-12-21 18:17:45 +0000707 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100708 if (jump_if == false) {
709 for (int i = 0; i < n; i++) {
710 c_if_cond(comp, pns->nodes[i], false, label);
711 }
712 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100713 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100714 for (int i = 0; i < n - 1; i++) {
715 c_if_cond(comp, pns->nodes[i], false, label2);
716 }
717 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000718 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100719 }
720 return;
Damiend99b0522013-12-21 18:17:45 +0000721 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100722 c_if_cond(comp, pns->nodes[0], !jump_if, label);
723 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100724 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
725 // cond is something in parenthesis
726 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
727 // empty tuple, acts as false for the condition
728 if (jump_if == false) {
729 EMIT_ARG(jump, label);
730 }
731 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
732 // non-empty tuple, acts as true for the condition
733 if (jump_if == true) {
734 EMIT_ARG(jump, label);
735 }
736 } else {
737 // parenthesis around 1 item, is just that item
738 c_if_cond(comp, pns->nodes[0], jump_if, label);
739 }
740 return;
Damien3a205172013-10-12 15:01:56 +0100741 }
742 }
743
744 // nothing special, fall back to default compiling for node and jump
745 compile_node(comp, pn);
746 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000747 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100748 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000749 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100750 }
751#endif
Damien429d7192013-10-04 19:53:11 +0100752}
753
754typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100755STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100756
Damien George6be0b0a2014-08-15 14:30:52 +0100757STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100758 if (assign_kind != ASSIGN_AUG_STORE) {
759 compile_node(comp, pns->nodes[0]);
760 }
761
Damiend99b0522013-12-21 18:17:45 +0000762 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
763 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
764 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
765 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100766 if (assign_kind != ASSIGN_AUG_STORE) {
767 for (int i = 0; i < n - 1; i++) {
768 compile_node(comp, pns1->nodes[i]);
769 }
770 }
Damiend99b0522013-12-21 18:17:45 +0000771 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
772 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100773 }
Damiend99b0522013-12-21 18:17:45 +0000774 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100775 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000776 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100777 if (assign_kind == ASSIGN_AUG_STORE) {
778 EMIT(rot_three);
779 EMIT(store_subscr);
780 } else {
781 compile_node(comp, pns1->nodes[0]);
782 if (assign_kind == ASSIGN_AUG_LOAD) {
783 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100784 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100785 } else {
786 EMIT(store_subscr);
787 }
788 }
Damiend99b0522013-12-21 18:17:45 +0000789 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
790 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100791 if (assign_kind == ASSIGN_AUG_LOAD) {
792 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000793 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100794 } else {
795 if (assign_kind == ASSIGN_AUG_STORE) {
796 EMIT(rot_two);
797 }
Damien Georgeb9791222014-01-23 00:34:21 +0000798 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100799 }
800 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100801 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100802 }
803 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100804 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100805 }
806
Damiend99b0522013-12-21 18:17:45 +0000807 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100808 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100809 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100810
811 return;
812
813cannot_assign:
814 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100815}
816
Damien George0288cf02014-04-11 11:53:00 +0000817// 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 +0100818STATIC 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 +0000819 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
820
821 // look for star expression
Damien429d7192013-10-04 19:53:11 +0100822 int have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000823 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
824 EMIT_ARG(unpack_ex, 0, num_tail);
825 have_star_index = 0;
826 }
827 for (int i = 0; i < num_tail; i++) {
828 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100829 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000830 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
831 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100832 } else {
Damien George9d181f62014-04-27 16:55:27 +0100833 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100834 return;
835 }
836 }
837 }
838 if (have_star_index < 0) {
Damien George0288cf02014-04-11 11:53:00 +0000839 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100840 }
Damien George0288cf02014-04-11 11:53:00 +0000841 if (num_head != 0) {
842 if (0 == have_star_index) {
843 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100844 } else {
Damien George0288cf02014-04-11 11:53:00 +0000845 c_assign(comp, node_head, ASSIGN_STORE);
846 }
847 }
848 for (int i = 0; i < num_tail; i++) {
849 if (num_head + i == have_star_index) {
850 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
851 } else {
852 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100853 }
854 }
855}
856
857// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100858STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100859 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000860 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100861 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000862 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
863 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000864 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100865 switch (assign_kind) {
866 case ASSIGN_STORE:
867 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000868 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100869 break;
870 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000871 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100872 break;
873 }
874 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100875 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100876 return;
877 }
878 } else {
Damiend99b0522013-12-21 18:17:45 +0000879 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
880 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100881 case PN_power:
882 // lhs is an index or attribute
883 c_assign_power(comp, pns, assign_kind);
884 break;
885
886 case PN_testlist_star_expr:
887 case PN_exprlist:
888 // lhs is a tuple
889 if (assign_kind != ASSIGN_STORE) {
890 goto bad_aug;
891 }
Damien George0288cf02014-04-11 11:53:00 +0000892 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100893 break;
894
895 case PN_atom_paren:
896 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000897 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100898 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100899 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000900 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
901 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100902 goto testlist_comp;
903 } else {
904 // parenthesis around 1 item, is just that item
905 pn = pns->nodes[0];
906 goto tail_recursion;
907 }
908 break;
909
910 case PN_atom_bracket:
911 // lhs is something in brackets
912 if (assign_kind != ASSIGN_STORE) {
913 goto bad_aug;
914 }
Damiend99b0522013-12-21 18:17:45 +0000915 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100916 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000917 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000918 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
919 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100920 goto testlist_comp;
921 } else {
922 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000923 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100924 }
925 break;
926
927 default:
Damien George9d181f62014-04-27 16:55:27 +0100928 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100929 }
930 return;
931
932 testlist_comp:
933 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000934 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
935 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
936 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100937 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000938 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000939 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000940 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100941 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000942 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
943 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000944 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100945 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100946 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100947 } else {
948 // sequence with 2 items
949 goto sequence_with_2_items;
950 }
951 } else {
952 // sequence with 2 items
953 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000954 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100955 }
956 return;
957 }
958 return;
959
Damien George9d181f62014-04-27 16:55:27 +0100960 cannot_assign:
961 compile_syntax_error(comp, pn, "can't assign to expression");
962 return;
963
Damien429d7192013-10-04 19:53:11 +0100964 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100965 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100966}
967
968// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100969// if we are not in CPython compatibility mode then:
970// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
971// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
972// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100973STATIC 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 +0100974 assert(n_pos_defaults >= 0);
975 assert(n_kw_defaults >= 0);
976
Damien429d7192013-10-04 19:53:11 +0100977 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000978 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100979 int nfree = 0;
980 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000981 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
982 id_info_t *id = &comp->scope_cur->id_info[i];
983 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
984 for (int j = 0; j < this_scope->id_info_len; j++) {
985 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100986 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000987#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100988 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000989#else
990 // in Micro Python we load closures using LOAD_FAST
Damien George7ff996c2014-09-08 23:05:16 +0100991 EMIT_ARG(load_fast, id->qst, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000992#endif
Damien318aec62013-12-10 18:28:17 +0000993 nfree += 1;
994 }
995 }
Damien429d7192013-10-04 19:53:11 +0100996 }
997 }
998 }
Damien429d7192013-10-04 19:53:11 +0100999
1000 // make the function/closure
1001 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +01001002 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001003 } else {
Damien George3558f622014-04-20 17:50:40 +01001004 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +01001005 }
1006}
1007
Damien George6be0b0a2014-08-15 14:30:52 +01001008STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001009 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001010 comp->have_star = true;
1011 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001012 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1013 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001014 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001015 } else {
1016 // named star
Damien429d7192013-10-04 19:53:11 +01001017 }
Damien George8b19db02014-04-11 23:25:34 +01001018 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001019
1020 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001021 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001022 // TODO do we need to do anything with this?
1023
1024 } else {
1025 mp_parse_node_t pn_id;
1026 mp_parse_node_t pn_colon;
1027 mp_parse_node_t pn_equal;
1028 if (MP_PARSE_NODE_IS_ID(pn)) {
1029 // this parameter is just an id
1030
1031 pn_id = pn;
1032 pn_colon = MP_PARSE_NODE_NULL;
1033 pn_equal = MP_PARSE_NODE_NULL;
1034
1035 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1036 // this parameter has a colon and/or equal specifier
1037
1038 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1039 pn_id = pns->nodes[0];
1040 pn_colon = pns->nodes[1];
1041 pn_equal = pns->nodes[2];
1042
1043 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001044 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001045 assert(0);
1046 return;
1047 }
1048
1049 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1050 // this parameter does not have a default value
1051
1052 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001053 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001054 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001055 return;
1056 }
1057
1058 } else {
1059 // this parameter has a default value
1060 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1061
Damien George8b19db02014-04-11 23:25:34 +01001062 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001063 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001064#if MICROPY_EMIT_CPYTHON
1065 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1066 compile_node(comp, pn_equal);
1067#else
Damien George69b89d22014-04-11 13:38:30 +00001068 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1069 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001070 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1071 // we need to do this here before we start building the map for the default keywords
1072 if (comp->num_default_params > 0) {
1073 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001074 } else {
1075 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001076 }
Damien George69b89d22014-04-11 13:38:30 +00001077 // first default dict param, so make the map
1078 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001079 }
Damien Georgef0778a72014-06-07 22:01:00 +01001080
1081 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001082 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001083 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001084 EMIT(store_map);
1085#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001086 } else {
Damien George69b89d22014-04-11 13:38:30 +00001087 comp->num_default_params += 1;
1088 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001089 }
1090 }
1091
1092 // TODO pn_colon not implemented
1093 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001094 }
1095}
1096
1097// leaves function object on stack
1098// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001099STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001100 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001101 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001102 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001103 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001104 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001105 }
1106
1107 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001108 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001109 uint old_num_dict_params = comp->num_dict_params;
1110 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001111
1112 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001113 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001114 comp->num_dict_params = 0;
1115 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001116 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001117
Damien Georgea91ac202014-10-05 19:01:34 +01001118 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001119 return MP_QSTR_NULL;
1120 }
1121
Damien Georgee337f1e2014-03-31 15:18:37 +01001122#if !MICROPY_EMIT_CPYTHON
1123 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001124 // the default keywords args may have already made the tuple; if not, do it now
1125 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001126 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001127 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001128 }
1129#endif
1130
Damien429d7192013-10-04 19:53:11 +01001131 // get the scope for this function
1132 scope_t *fscope = (scope_t*)pns->nodes[4];
1133
1134 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001135 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001136
1137 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001138 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001139 comp->num_dict_params = old_num_dict_params;
1140 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001141
1142 // return its name (the 'f' in "def f(...):")
1143 return fscope->simple_name;
1144}
1145
1146// leaves class object on stack
1147// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001148STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001149 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001150 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001151 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001152 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001153 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001154 }
1155
1156 EMIT(load_build_class);
1157
1158 // scope for this class
1159 scope_t *cscope = (scope_t*)pns->nodes[3];
1160
1161 // compile the class
1162 close_over_variables_etc(comp, cscope, 0, 0);
1163
1164 // get its name
Damien George968bf342014-04-27 19:12:05 +01001165 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001166
1167 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001168 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1169 mp_parse_node_t parents = pns->nodes[1];
1170 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1171 parents = MP_PARSE_NODE_NULL;
1172 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001173 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001174 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001175
1176 // return its name (the 'C' in class C(...):")
1177 return cscope->simple_name;
1178}
1179
Damien6cdd3af2013-10-05 18:08:26 +01001180// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001181STATIC 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 +00001182 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001183 return false;
1184 }
1185
1186 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001187 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001188 return true;
1189 }
1190
Damiend99b0522013-12-21 18:17:45 +00001191 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001192 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001193 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001194#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001195 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001196 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001197 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001198 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001199#endif
Damien3ef4abb2013-10-12 16:53:13 +01001200#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001201 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001202 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001203#endif
Damien6cdd3af2013-10-05 18:08:26 +01001204 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001205 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001206 }
1207
1208 return true;
1209}
1210
Damien George969a6b32014-12-10 22:07:04 +00001211STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001212 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001213 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001214 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1215
Damien6cdd3af2013-10-05 18:08:26 +01001216 // inherit emit options for this function/class definition
1217 uint emit_options = comp->scope_cur->emit_options;
1218
1219 // compile each decorator
1220 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001221 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001222 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1223 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001224
1225 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001226 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001227 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1228
1229 // check for built-in decorators
1230 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1231 // this was a built-in
1232 num_built_in_decorators += 1;
1233
1234 } else {
1235 // not a built-in, compile normally
1236
1237 // compile the decorator function
1238 compile_node(comp, name_nodes[0]);
1239 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001240 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001241 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001242 }
1243
1244 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001245 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001246 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001247 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001248 compile_node(comp, pns_decorator->nodes[1]);
1249 }
Damien429d7192013-10-04 19:53:11 +01001250 }
1251 }
1252
1253 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001254 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001255 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001256 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001257 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001258 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001259 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001260 } else {
1261 // shouldn't happen
1262 assert(0);
1263 }
1264
1265 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001266 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001267 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001268 }
1269
1270 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001271 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001272}
1273
Damien George969a6b32014-12-10 22:07:04 +00001274STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001275 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001276 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001277 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001278}
1279
Damien George6be0b0a2014-08-15 14:30:52 +01001280STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001281 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001282 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001283 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1284 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001285
1286 compile_node(comp, pns->nodes[0]); // base of the power node
1287
Damiend99b0522013-12-21 18:17:45 +00001288 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1289 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1290 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1291 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001292 for (int i = 0; i < n - 1; i++) {
1293 compile_node(comp, pns1->nodes[i]);
1294 }
Damiend99b0522013-12-21 18:17:45 +00001295 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1296 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001297 }
Damiend99b0522013-12-21 18:17:45 +00001298 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001299 // can't delete function calls
1300 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001301 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001302 compile_node(comp, pns1->nodes[0]);
1303 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001304 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1305 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001306 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001307 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001308 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001309 }
1310 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001311 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001312 }
1313
Damiend99b0522013-12-21 18:17:45 +00001314 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001315 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001316 }
Damiend99b0522013-12-21 18:17:45 +00001317 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1318 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1319 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1320 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001321 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1322
Damiend99b0522013-12-21 18:17:45 +00001323 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1324 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1325 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001326 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001327 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001328 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001329 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001330 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001331 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001332 c_del_stmt(comp, pns->nodes[0]);
1333 for (int i = 0; i < n; i++) {
1334 c_del_stmt(comp, pns1->nodes[i]);
1335 }
Damiend99b0522013-12-21 18:17:45 +00001336 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001337 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001338 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001339 } else {
1340 // sequence with 2 items
1341 goto sequence_with_2_items;
1342 }
1343 } else {
1344 // sequence with 2 items
1345 sequence_with_2_items:
1346 c_del_stmt(comp, pns->nodes[0]);
1347 c_del_stmt(comp, pns->nodes[1]);
1348 }
1349 } else {
1350 // tuple with 1 element
1351 c_del_stmt(comp, pn);
1352 }
1353 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001354 // TODO is there anything else to implement?
1355 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001356 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001357
1358 return;
1359
1360cannot_delete:
1361 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001362}
1363
Damien George969a6b32014-12-10 22:07:04 +00001364STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001365 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1366}
1367
Damien George969a6b32014-12-10 22:07:04 +00001368STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001369 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001370 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001371 }
Damien George090c9232014-10-17 14:08:49 +00001372 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001373 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001374}
1375
Damien George969a6b32014-12-10 22:07:04 +00001376STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001377 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001378 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001379 }
Damien George090c9232014-10-17 14:08:49 +00001380 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001381 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001382}
1383
Damien George969a6b32014-12-10 22:07:04 +00001384STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001385 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001386 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001387 return;
1388 }
Damiend99b0522013-12-21 18:17:45 +00001389 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001390 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001391 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001392 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001393 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001394 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1395 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 +01001396
Damien George6f355fd2014-04-10 14:11:31 +01001397 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001398 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1399 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1400 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001401 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001402 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1403 } else {
1404 compile_node(comp, pns->nodes[0]);
1405 }
1406 EMIT(return_value);
1407}
1408
Damien George969a6b32014-12-10 22:07:04 +00001409STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001410 compile_node(comp, pns->nodes[0]);
1411 EMIT(pop_top);
1412}
1413
Damien George969a6b32014-12-10 22:07:04 +00001414STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001415 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001416 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001417 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001418 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001419 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001420 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001421 compile_node(comp, pns->nodes[0]);
1422 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001423 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001424 } else {
1425 // raise x
1426 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001428 }
1429}
1430
Damien George635543c2014-04-10 12:56:52 +01001431// q_base holds the base of the name
1432// eg a -> q_base=a
1433// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001434STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001435 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001436 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1437 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001438 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001439 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001440 pn = pns->nodes[0];
1441 is_as = true;
1442 }
Damien George635543c2014-04-10 12:56:52 +01001443 if (MP_PARSE_NODE_IS_NULL(pn)) {
1444 // empty name (eg, from . import x)
1445 *q_base = MP_QSTR_;
1446 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1447 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001448 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001449 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001450 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001451 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001452 }
Damien George635543c2014-04-10 12:56:52 +01001453 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001454 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1455 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1456 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001457 // a name of the form a.b.c
1458 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001459 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001460 }
Damiend99b0522013-12-21 18:17:45 +00001461 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001462 int len = n - 1;
1463 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001464 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001465 }
Damien George55baff42014-01-21 21:40:13 +00001466 byte *q_ptr;
1467 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001468 for (int i = 0; i < n; i++) {
1469 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001470 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001471 }
Damien George39dc1452014-10-03 19:52:22 +01001472 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001473 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001474 memcpy(str_dest, str_src, str_src_len);
1475 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001476 }
Damien George635543c2014-04-10 12:56:52 +01001477 qstr q_full = qstr_build_end(q_ptr);
1478 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001479 if (is_as) {
1480 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001481 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001482 }
1483 }
1484 } else {
Damien George635543c2014-04-10 12:56:52 +01001485 // shouldn't happen
1486 assert(0);
Damien429d7192013-10-04 19:53:11 +01001487 }
1488 } else {
Damien George635543c2014-04-10 12:56:52 +01001489 // shouldn't happen
1490 assert(0);
Damien429d7192013-10-04 19:53:11 +01001491 }
1492}
1493
Damien George6be0b0a2014-08-15 14:30:52 +01001494STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001495 EMIT_ARG(load_const_small_int, 0); // level 0 import
1496 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1497 qstr q_base;
1498 do_import_name(comp, pn, &q_base);
1499 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001500}
1501
Damien George969a6b32014-12-10 22:07:04 +00001502STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001503 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1504}
1505
Damien George969a6b32014-12-10 22:07:04 +00001506STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001507 mp_parse_node_t pn_import_source = pns->nodes[0];
1508
1509 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1510 uint import_level = 0;
1511 do {
1512 mp_parse_node_t pn_rel;
1513 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)) {
1514 // This covers relative imports with dots only like "from .. import"
1515 pn_rel = pn_import_source;
1516 pn_import_source = MP_PARSE_NODE_NULL;
1517 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1518 // This covers relative imports starting with dot(s) like "from .foo import"
1519 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1520 pn_rel = pns_2b->nodes[0];
1521 pn_import_source = pns_2b->nodes[1];
1522 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1523 } else {
1524 // Not a relative import
1525 break;
1526 }
1527
1528 // get the list of . and/or ...'s
1529 mp_parse_node_t *nodes;
1530 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1531
1532 // count the total number of .'s
1533 for (int i = 0; i < n; i++) {
1534 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1535 import_level++;
1536 } else {
1537 // should be an MP_TOKEN_ELLIPSIS
1538 import_level += 3;
1539 }
1540 }
1541 } while (0);
1542
Damiend99b0522013-12-21 18:17:45 +00001543 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001544 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001545
1546 // build the "fromlist" tuple
1547#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001549#else
Damien George708c0732014-04-27 19:23:46 +01001550 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001551 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001552#endif
1553
1554 // do the import
Damien George635543c2014-04-10 12:56:52 +01001555 qstr dummy_q;
1556 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001557 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001558
Damien429d7192013-10-04 19:53:11 +01001559 } else {
Damien George635543c2014-04-10 12:56:52 +01001560 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001561
1562 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001563 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001564 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001565#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001566 {
1567 vstr_t *vstr = vstr_new();
1568 vstr_printf(vstr, "(");
1569 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001570 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1571 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1572 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001573 if (i > 0) {
1574 vstr_printf(vstr, ", ");
1575 }
1576 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001577 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001578 const byte *str = qstr_data(id2, &len);
1579 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001580 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001581 }
Damien02f89412013-12-12 15:13:36 +00001582 if (n == 1) {
1583 vstr_printf(vstr, ",");
1584 }
1585 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001587 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001588 }
Damiendb4c3612013-12-10 17:27:24 +00001589#else
1590 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001591 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1592 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1593 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001594 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001595 }
Damien Georgeb9791222014-01-23 00:34:21 +00001596 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001597#endif
1598
1599 // do the import
Damien George635543c2014-04-10 12:56:52 +01001600 qstr dummy_q;
1601 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001602 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001603 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1604 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1605 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001607 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001609 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001610 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001611 }
1612 }
1613 EMIT(pop_top);
1614 }
1615}
1616
Damien George584ba672014-12-21 17:26:45 +00001617STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1618 bool added;
1619 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1620 if (!added) {
1621 compile_syntax_error(comp, pn, "identifier already used");
1622 return;
1623 }
1624 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1625
1626 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1627 id_info = scope_find_global(comp->scope_cur, qst);
1628 if (id_info != NULL) {
1629 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1630 }
1631}
1632
Damien George969a6b32014-12-10 22:07:04 +00001633STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001634 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001635 mp_parse_node_t *nodes;
1636 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1637 for (int i = 0; i < n; i++) {
1638 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001639 }
1640 }
1641}
1642
Damien George584ba672014-12-21 17:26:45 +00001643STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1644 bool added;
1645 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1646 if (!added) {
1647 compile_syntax_error(comp, pn, "identifier already used");
1648 return;
1649 }
1650 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1651 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
1652 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1653 return;
1654 }
1655 id_info->kind = ID_INFO_KIND_FREE;
1656 scope_close_over_in_parents(comp->scope_cur, qst);
1657}
1658
Damien George969a6b32014-12-10 22:07:04 +00001659STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001660 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001661 if (comp->scope_cur->kind == SCOPE_MODULE) {
1662 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1663 return;
1664 }
1665 mp_parse_node_t *nodes;
1666 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1667 for (int i = 0; i < n; i++) {
1668 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001669 }
1670 }
1671}
1672
Damien George969a6b32014-12-10 22:07:04 +00001673STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001674 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001675 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001676 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001677 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001678 // assertion message
1679 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001680 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001681 }
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(raise_varargs, 1);
1683 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001684}
1685
Damien George969a6b32014-12-10 22:07:04 +00001686STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001687 // TODO proper and/or short circuiting
1688
Damien George6f355fd2014-04-10 14:11:31 +01001689 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001690
Damien George391db862014-10-17 17:57:33 +00001691 // optimisation: don't emit anything when "if False" (not in CPython)
1692 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1693 uint l_fail = comp_next_label(comp);
1694 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001695
Damien George391db862014-10-17 17:57:33 +00001696 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001697
Damien George391db862014-10-17 17:57:33 +00001698 // optimisation: skip everything else when "if True" (not in CPython)
1699 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1700 goto done;
1701 }
1702
1703 if (
1704 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1705 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1706 // optimisation: don't jump if last instruction was return
1707 && !EMIT(last_emit_was_return_value)
1708 ) {
1709 // jump over elif/else blocks
1710 EMIT_ARG(jump, l_end);
1711 }
1712
1713 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001714 }
1715
Damien George235f9b32014-10-17 17:30:16 +00001716 // compile elif blocks (if any)
1717 mp_parse_node_t *pn_elif;
1718 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1719 for (int i = 0; i < n_elif; i++) {
1720 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1721 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001722
Damien George391db862014-10-17 17:57:33 +00001723 // optimisation: don't emit anything when "if False" (not in CPython)
1724 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1725 uint l_fail = comp_next_label(comp);
1726 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001727
Damien George391db862014-10-17 17:57:33 +00001728 compile_node(comp, pns_elif->nodes[1]); // elif block
1729
1730 // optimisation: skip everything else when "elif True" (not in CPython)
1731 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1732 goto done;
1733 }
1734
1735 // optimisation: don't jump if last instruction was return
1736 if (!EMIT(last_emit_was_return_value)) {
1737 EMIT_ARG(jump, l_end);
1738 }
1739 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001740 }
1741 }
1742
1743 // compile else block
1744 compile_node(comp, pns->nodes[3]); // can be null
1745
Damien George391db862014-10-17 17:57:33 +00001746done:
Damien Georgeb9791222014-01-23 00:34:21 +00001747 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001748}
1749
Damien Georgecbddb272014-02-01 20:08:18 +00001750#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001751 uint16_t old_break_label = comp->break_label; \
1752 uint16_t old_continue_label = comp->continue_label; \
1753 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001754 uint break_label = comp_next_label(comp); \
1755 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001756 comp->break_label = break_label; \
1757 comp->continue_label = continue_label; \
1758 comp->break_continue_except_level = comp->cur_except_level;
1759
1760#define END_BREAK_CONTINUE_BLOCK \
1761 comp->break_label = old_break_label; \
1762 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001763 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001764
Damien George969a6b32014-12-10 22:07:04 +00001765STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001766 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001767
Damience89a212013-10-15 22:25:17 +01001768 // compared to CPython, we have an optimised version of while loops
1769#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001770 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(setup_loop, break_label);
1772 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001773 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1774 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001775 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001776 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001777 }
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001779 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1780 // this is a small hack to agree with CPython
1781 if (!node_is_const_true(pns->nodes[0])) {
1782 EMIT(pop_block);
1783 }
Damience89a212013-10-15 22:25:17 +01001784#else
Damien George391db862014-10-17 17:57:33 +00001785 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1786 uint top_label = comp_next_label(comp);
1787 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1788 EMIT_ARG(jump, continue_label);
1789 }
1790 EMIT_ARG(label_assign, top_label);
1791 compile_node(comp, pns->nodes[1]); // body
1792 EMIT_ARG(label_assign, continue_label);
1793 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1794 }
Damience89a212013-10-15 22:25:17 +01001795#endif
1796
1797 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001798 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001799
1800 compile_node(comp, pns->nodes[2]); // else
1801
Damien Georgeb9791222014-01-23 00:34:21 +00001802 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001803}
1804
Damien George2ac4af62014-08-15 16:45:41 +01001805#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001806// This function compiles an optimised for-loop of the form:
1807// for <var> in range(<start>, <end>, <step>):
1808// <body>
1809// else:
1810// <else>
1811// <var> must be an identifier and <step> must be a small-int.
1812//
1813// Semantics of for-loop require:
1814// - final failing value should not be stored in the loop variable
1815// - if the loop never runs, the loop variable should never be assigned
1816// - assignments to <var>, <end> or <step> in the body do not alter the loop
1817// (<step> is a constant for us, so no need to worry about it changing)
1818//
1819// If <end> is a small-int, then the stack during the for-loop contains just
1820// the current value of <var>. Otherwise, the stack contains <end> then the
1821// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001822STATIC 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 +00001823 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001824
Damien George6f355fd2014-04-10 14:11:31 +01001825 uint top_label = comp_next_label(comp);
1826 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001827
Damien Georgee181c0d2014-12-12 17:19:56 +00001828 // put the end value on the stack if it's not a small-int constant
1829 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1830 if (end_on_stack) {
1831 compile_node(comp, pn_end);
1832 }
1833
1834 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001835 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001836
Damien Georgeb9791222014-01-23 00:34:21 +00001837 EMIT_ARG(jump, entry_label);
1838 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001839
Damien Georgec33ce602014-12-11 17:35:23 +00001840 // duplicate next value and store it to var
1841 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001842 c_assign(comp, pn_var, ASSIGN_STORE);
1843
Damienf3822fc2013-11-09 20:12:03 +00001844 // compile body
1845 compile_node(comp, pn_body);
1846
Damien Georgeb9791222014-01-23 00:34:21 +00001847 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001848
Damien Georgee181c0d2014-12-12 17:19:56 +00001849 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001850 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001851 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001852
Damien Georgeb9791222014-01-23 00:34:21 +00001853 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001854
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001855 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001856 if (end_on_stack) {
1857 EMIT(dup_top_two);
1858 EMIT(rot_two);
1859 } else {
1860 EMIT(dup_top);
1861 compile_node(comp, pn_end);
1862 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001863 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1864 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001865 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001866 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001867 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001868 }
Damien Georgeb9791222014-01-23 00:34:21 +00001869 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001870
1871 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001872 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001873
1874 compile_node(comp, pn_else);
1875
Damien Georgeb9791222014-01-23 00:34:21 +00001876 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001877
1878 // discard final value of var that failed the loop condition
1879 EMIT(pop_top);
1880
1881 // discard <end> value if it's on the stack
1882 if (end_on_stack) {
1883 EMIT(pop_top);
1884 }
Damienf72fd0e2013-11-06 20:20:49 +00001885}
Damien George2ac4af62014-08-15 16:45:41 +01001886#endif
Damienf72fd0e2013-11-06 20:20:49 +00001887
Damien George969a6b32014-12-10 22:07:04 +00001888STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001889#if !MICROPY_EMIT_CPYTHON
1890 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1891 // this is actually slower, but uses no heap memory
1892 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001893 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_power)) {
Damiend99b0522013-12-21 18:17:45 +00001894 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001895 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1896 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1897 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1898 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001899 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1900 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001901 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001902 mp_parse_node_t pn_range_start;
1903 mp_parse_node_t pn_range_end;
1904 mp_parse_node_t pn_range_step;
1905 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001906 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001907 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001908 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001909 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001910 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001911 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001912 } else if (n_args == 2) {
1913 pn_range_start = args[0];
1914 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001915 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001916 } else {
1917 pn_range_start = args[0];
1918 pn_range_end = args[1];
1919 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001920 // We need to know sign of step. This is possible only if it's constant
1921 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1922 optimize = false;
1923 }
Damienf72fd0e2013-11-06 20:20:49 +00001924 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001925 }
1926 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001927 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1928 return;
1929 }
1930 }
1931 }
1932#endif
1933
Damien Georgecbddb272014-02-01 20:08:18 +00001934 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001935 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001936
Damien George6f355fd2014-04-10 14:11:31 +01001937 uint pop_label = comp_next_label(comp);
1938 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001939
Damience89a212013-10-15 22:25:17 +01001940 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1941#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001942 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001943#endif
1944
Damien429d7192013-10-04 19:53:11 +01001945 compile_node(comp, pns->nodes[1]); // iterator
1946 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001947 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001948 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001949 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1950 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001951 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001952 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001953 }
Damien Georgeb9791222014-01-23 00:34:21 +00001954 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001955 EMIT(for_iter_end);
1956
1957 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001958 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001959
Damience89a212013-10-15 22:25:17 +01001960#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001961 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001962#endif
Damien429d7192013-10-04 19:53:11 +01001963
1964 compile_node(comp, pns->nodes[3]); // else (not tested)
1965
Damien Georgeb9791222014-01-23 00:34:21 +00001966 EMIT_ARG(label_assign, break_label);
1967 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001968}
1969
Damien George6be0b0a2014-08-15 14:30:52 +01001970STATIC 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 +01001971 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001972 uint l1 = comp_next_label(comp);
1973 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001974
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001976 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001977
Damien429d7192013-10-04 19:53:11 +01001978 compile_node(comp, pn_body); // body
1979 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001980 EMIT_ARG(jump, success_label); // jump over exception handler
1981
1982 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001983 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001984
Damien George6f355fd2014-04-10 14:11:31 +01001985 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001986
1987 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001988 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1989 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001990
1991 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001992 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001993
Damiend99b0522013-12-21 18:17:45 +00001994 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001995 // this is a catch all exception handler
1996 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001997 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001998 return;
1999 }
2000 } else {
2001 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00002002 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
2003 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
2004 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
2005 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002006 // handler binds the exception to a local
2007 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002008 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002009 }
2010 }
2011 EMIT(dup_top);
2012 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002013 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00002014 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002015 }
2016
2017 EMIT(pop_top);
2018
2019 if (qstr_exception_local == 0) {
2020 EMIT(pop_top);
2021 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002022 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002023 }
2024
2025 EMIT(pop_top);
2026
Damien George6f355fd2014-04-10 14:11:31 +01002027 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002028 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002029 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002030 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002031 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002032 }
2033 compile_node(comp, pns_except->nodes[1]);
2034 if (qstr_exception_local != 0) {
2035 EMIT(pop_block);
2036 }
2037 EMIT(pop_except);
2038 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002039 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2040 EMIT_ARG(label_assign, l3);
2041 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2042 EMIT_ARG(store_id, qstr_exception_local);
2043 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002044
Damien George8dcc0c72014-03-27 10:55:21 +00002045 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002046 EMIT(end_finally);
2047 }
Damien Georgeb9791222014-01-23 00:34:21 +00002048 EMIT_ARG(jump, l2);
2049 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002050 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002051 }
2052
Damien George8dcc0c72014-03-27 10:55:21 +00002053 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002054 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002055 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002056
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002058 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002059 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002060}
2061
Damien George6be0b0a2014-08-15 14:30:52 +01002062STATIC 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 +01002063 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002064
Damien Georgeb9791222014-01-23 00:34:21 +00002065 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002066 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002067
Damien429d7192013-10-04 19:53:11 +01002068 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002069 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002070 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002071 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002072 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002073 } else {
2074 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2075 }
2076 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002077 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2078 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002079 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002080
Damien George8dcc0c72014-03-27 10:55:21 +00002081 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002082 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002083}
2084
Damien George969a6b32014-12-10 22:07:04 +00002085STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002086 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2087 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2088 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002089 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002090 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2091 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002092 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002093 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002094 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002095 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002096 // no finally
2097 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2098 } else {
2099 // have finally
Damiend99b0522013-12-21 18:17:45 +00002100 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 +01002101 }
2102 } else {
2103 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002104 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002105 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002106 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002107 }
2108 } else {
2109 // shouldn't happen
2110 assert(0);
2111 }
2112}
2113
Damien George6be0b0a2014-08-15 14:30:52 +01002114STATIC 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 +01002115 if (n == 0) {
2116 // no more pre-bits, compile the body of the with
2117 compile_node(comp, body);
2118 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002119 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002120 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002121 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002122 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002123 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002124 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002125 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2126 } else {
2127 // this pre-bit is just an expression
2128 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002129 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002130 EMIT(pop_top);
2131 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002132 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002133 // compile additional pre-bits and the body
2134 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2135 // finish this with block
2136 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002137 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2138 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002139 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002140 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002141 EMIT(end_finally);
2142 }
2143}
2144
Damien George969a6b32014-12-10 22:07:04 +00002145STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002146 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002147 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002148 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2149 assert(n > 0);
2150
2151 // compile in a nested fashion
2152 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2153}
2154
Damien George969a6b32014-12-10 22:07:04 +00002155STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002156 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002157 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2158 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002159 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002160 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002161 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002162 EMIT(pop_top);
2163
Damien429d7192013-10-04 19:53:11 +01002164 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002165 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002166 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2167 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002168 // do nothing with a lonely constant
2169 } else {
2170 compile_node(comp, pns->nodes[0]); // just an expression
2171 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2172 }
Damien429d7192013-10-04 19:53:11 +01002173 }
2174 } else {
Damiend99b0522013-12-21 18:17:45 +00002175 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2176 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002177 if (kind == PN_expr_stmt_augassign) {
2178 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2179 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002180 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002181 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002182 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002183 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2184 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2185 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2186 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2187 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2188 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2189 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2190 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2191 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2192 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2193 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2194 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2195 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002196 }
Damien George7e5fb242014-02-01 22:18:47 +00002197 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002198 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2199 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002200 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2201 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002202 // following CPython, we store left-most first
2203 if (rhs > 0) {
2204 EMIT(dup_top);
2205 }
2206 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2207 for (int i = 0; i < rhs; i++) {
2208 if (i + 1 < rhs) {
2209 EMIT(dup_top);
2210 }
Damiend99b0522013-12-21 18:17:45 +00002211 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002212 }
2213 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002214 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002215 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2216 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2217 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002218 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002219 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2220 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002221 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2222 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2223 // can't optimise when it's a star expression on the lhs
2224 goto no_optimisation;
2225 }
Damien429d7192013-10-04 19:53:11 +01002226 compile_node(comp, pns10->nodes[0]); // rhs
2227 compile_node(comp, pns10->nodes[1]); // rhs
2228 EMIT(rot_two);
2229 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2230 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002231 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2232 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2233 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2234 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002235 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002236 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2237 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002238 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2239 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2240 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2241 // can't optimise when it's a star expression on the lhs
2242 goto no_optimisation;
2243 }
Damien429d7192013-10-04 19:53:11 +01002244 compile_node(comp, pns10->nodes[0]); // rhs
2245 compile_node(comp, pns10->nodes[1]); // rhs
2246 compile_node(comp, pns10->nodes[2]); // rhs
2247 EMIT(rot_three);
2248 EMIT(rot_two);
2249 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2250 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2251 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2252 } else {
Damien George495d7812014-04-08 17:51:47 +01002253 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002254 compile_node(comp, pns1->nodes[0]); // rhs
2255 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2256 }
2257 } else {
2258 // shouldn't happen
2259 assert(0);
2260 }
2261 }
2262}
2263
Damien George6be0b0a2014-08-15 14:30:52 +01002264STATIC 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 +00002265 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002266 compile_node(comp, pns->nodes[0]);
2267 for (int i = 1; i < num_nodes; i += 1) {
2268 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002269 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002270 }
2271}
2272
Damien George969a6b32014-12-10 22:07:04 +00002273STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002274 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2275 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002276
Damien George6f355fd2014-04-10 14:11:31 +01002277 uint l_fail = comp_next_label(comp);
2278 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002279 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2280 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002281 EMIT_ARG(jump, l_end);
2282 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002283 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002284 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002285 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002286}
2287
Damien George969a6b32014-12-10 22:07:04 +00002288STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002289 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002290 //mp_parse_node_t pn_params = pns->nodes[0];
2291 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002292
Damien George36db6bc2014-05-07 17:24:22 +01002293 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002294 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002295 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 +01002296 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002297 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002298 }
2299
2300 // get the scope for this lambda
2301 scope_t *this_scope = (scope_t*)pns->nodes[2];
2302
2303 // make the lambda
2304 close_over_variables_etc(comp, this_scope, 0, 0);
2305}
2306
Damien George969a6b32014-12-10 22:07:04 +00002307STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002308 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002309 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002310 for (int i = 0; i < n; i += 1) {
2311 compile_node(comp, pns->nodes[i]);
2312 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002313 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002314 }
2315 }
Damien Georgeb9791222014-01-23 00:34:21 +00002316 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002317}
2318
Damien George969a6b32014-12-10 22:07:04 +00002319STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002320 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002321 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002322 for (int i = 0; i < n; i += 1) {
2323 compile_node(comp, pns->nodes[i]);
2324 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002325 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002326 }
2327 }
Damien Georgeb9791222014-01-23 00:34:21 +00002328 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002329}
2330
Damien George969a6b32014-12-10 22:07:04 +00002331STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002332 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002333 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002334}
2335
Damien George969a6b32014-12-10 22:07:04 +00002336STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002337 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002338 compile_node(comp, pns->nodes[0]);
2339 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002340 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002341 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002342 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002343 }
2344 for (int i = 1; i + 1 < num_nodes; i += 2) {
2345 compile_node(comp, pns->nodes[i + 1]);
2346 if (i + 2 < num_nodes) {
2347 EMIT(dup_top);
2348 EMIT(rot_three);
2349 }
Damien George7e5fb242014-02-01 22:18:47 +00002350 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002351 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002352 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002353 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2354 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2355 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2356 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2357 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2358 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2359 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2360 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002361 }
2362 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002363 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2364 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2365 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002366 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002367 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002368 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002369 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002370 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002371 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002372 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002373 }
2374 } else {
2375 // shouldn't happen
2376 assert(0);
2377 }
2378 } else {
2379 // shouldn't happen
2380 assert(0);
2381 }
2382 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002383 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002384 }
2385 }
2386 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002387 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002388 EMIT_ARG(jump, l_end);
2389 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002390 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002391 EMIT(rot_two);
2392 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002393 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002394 }
2395}
2396
Damien George969a6b32014-12-10 22:07:04 +00002397STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002398 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002399}
2400
Damien George969a6b32014-12-10 22:07:04 +00002401STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002402 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002403}
2404
Damien George969a6b32014-12-10 22:07:04 +00002405STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002406 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002407}
2408
Damien George969a6b32014-12-10 22:07:04 +00002409STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002410 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002411}
2412
Damien George969a6b32014-12-10 22:07:04 +00002413STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002414 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002415 compile_node(comp, pns->nodes[0]);
2416 for (int i = 1; i + 1 < num_nodes; i += 2) {
2417 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002418 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002419 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002420 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002421 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002422 } else {
2423 // shouldn't happen
2424 assert(0);
2425 }
2426 }
2427}
2428
Damien George969a6b32014-12-10 22:07:04 +00002429STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002430 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002431 compile_node(comp, pns->nodes[0]);
2432 for (int i = 1; i + 1 < num_nodes; i += 2) {
2433 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002434 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002435 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002436 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002437 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002438 } else {
2439 // shouldn't happen
2440 assert(0);
2441 }
2442 }
2443}
2444
Damien George969a6b32014-12-10 22:07:04 +00002445STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002446 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002447 compile_node(comp, pns->nodes[0]);
2448 for (int i = 1; i + 1 < num_nodes; i += 2) {
2449 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002450 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002451 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002452 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002453 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002454 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002455 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002456 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002457 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002458 } else {
2459 // shouldn't happen
2460 assert(0);
2461 }
2462 }
2463}
2464
Damien George969a6b32014-12-10 22:07:04 +00002465STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002466 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002467 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002468 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002469 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002470 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002471 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002472 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002473 } else {
2474 // shouldn't happen
2475 assert(0);
2476 }
2477}
2478
Damien George969a6b32014-12-10 22:07:04 +00002479STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002480 // this is to handle special super() call
2481 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2482
2483 compile_generic_all_nodes(comp, pns);
2484}
2485
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002486STATIC 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 +01002487 // function to call is on top of stack
2488
Damien George35e2a4e2014-02-05 00:51:47 +00002489#if !MICROPY_EMIT_CPYTHON
2490 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002491 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002492 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002493 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002494 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002495 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002496 // first argument found; load it and call super
Damien George2bf7c092014-04-09 15:26:46 +01002497 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002498 EMIT_ARG(call_function, 2, 0, 0);
2499 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002500 }
2501 }
Damien George01039b52014-12-21 17:44:27 +00002502 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002503 return;
2504 }
2505#endif
2506
Damien George2c0842b2014-04-27 16:46:51 +01002507 // get the list of arguments
2508 mp_parse_node_t *args;
2509 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002510
Damien George2c0842b2014-04-27 16:46:51 +01002511 // compile the arguments
2512 // Rather than calling compile_node on the list, we go through the list of args
2513 // explicitly here so that we can count the number of arguments and give sensible
2514 // error messages.
2515 int n_positional = n_positional_extra;
2516 uint n_keyword = 0;
2517 uint star_flags = 0;
2518 for (int i = 0; i < n_args; i++) {
2519 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2520 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2521 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2522 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2523 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2524 return;
2525 }
2526 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2527 compile_node(comp, pns_arg->nodes[0]);
2528 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2529 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2530 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2531 return;
2532 }
2533 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2534 compile_node(comp, pns_arg->nodes[0]);
2535 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2536 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2537 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2538 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2539 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2540 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2541 return;
2542 }
Damien George968bf342014-04-27 19:12:05 +01002543 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002544 compile_node(comp, pns2->nodes[0]);
2545 n_keyword += 1;
2546 } else {
2547 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2548 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2549 n_positional++;
2550 }
2551 } else {
2552 goto normal_argument;
2553 }
2554 } else {
2555 normal_argument:
2556 if (n_keyword > 0) {
2557 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2558 return;
2559 }
2560 compile_node(comp, args[i]);
2561 n_positional++;
2562 }
Damien429d7192013-10-04 19:53:11 +01002563 }
2564
Damien George2c0842b2014-04-27 16:46:51 +01002565 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002566 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002567 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002568 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002569 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002570 }
Damien429d7192013-10-04 19:53:11 +01002571}
2572
Damien George969a6b32014-12-10 22:07:04 +00002573STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002574 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002575 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002576 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 +01002577 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002578 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2579 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002580 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002581 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002582 i += 1;
2583 } else {
2584 compile_node(comp, pns->nodes[i]);
2585 }
Damien George35e2a4e2014-02-05 00:51:47 +00002586 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002587 }
2588}
2589
Damien George969a6b32014-12-10 22:07:04 +00002590STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002591 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002592 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002593}
2594
Damien George969a6b32014-12-10 22:07:04 +00002595STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002596 // a list of strings
Damien63321742013-12-10 17:41:49 +00002597
2598 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002599 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002600 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002601 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002602 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002603 int pn_kind;
2604 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2605 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2606 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2607 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2608 } else {
2609 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2610 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2611 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2612 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002613 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002614 }
Damien63321742013-12-10 17:41:49 +00002615 if (i == 0) {
2616 string_kind = pn_kind;
2617 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002618 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002619 return;
2620 }
Damien429d7192013-10-04 19:53:11 +01002621 }
Damien63321742013-12-10 17:41:49 +00002622
Damien63321742013-12-10 17:41:49 +00002623 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002624 byte *q_ptr;
2625 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002626 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002627 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002628 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002629 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2630 memcpy(s_dest, s, s_len);
2631 s_dest += s_len;
2632 } else {
2633 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002634 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2635 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002636 }
Damien63321742013-12-10 17:41:49 +00002637 }
Damien George55baff42014-01-21 21:40:13 +00002638 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002639
Damien Georgeb9791222014-01-23 00:34:21 +00002640 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002641}
2642
2643// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002644STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002645 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2646 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2647 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002648
Damien George36db6bc2014-05-07 17:24:22 +01002649 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002650 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002651 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 +01002652 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002653 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002654 }
2655
2656 // get the scope for this comprehension
2657 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2658
2659 // compile the comprehension
2660 close_over_variables_etc(comp, this_scope, 0, 0);
2661
2662 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2663 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002664 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002665}
2666
Damien George969a6b32014-12-10 22:07:04 +00002667STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002668 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002669 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002670 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2671 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2672 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2673 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2674 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2675 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2676 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002677 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002678 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002679 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002680 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002681 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002682 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002684 // generator expression
2685 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2686 } else {
2687 // tuple with 2 items
2688 goto tuple_with_2_items;
2689 }
2690 } else {
2691 // tuple with 2 items
2692 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002693 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002694 }
2695 } else {
2696 // parenthesis around a single item, is just that item
2697 compile_node(comp, pns->nodes[0]);
2698 }
2699}
2700
Damien George969a6b32014-12-10 22:07:04 +00002701STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002702 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002703 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002705 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2706 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2707 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2708 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2709 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002710 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002711 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002712 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002713 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002714 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002715 // list of many items
2716 compile_node(comp, pns2->nodes[0]);
2717 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002719 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002720 // list comprehension
2721 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2722 } else {
2723 // list with 2 items
2724 goto list_with_2_items;
2725 }
2726 } else {
2727 // list with 2 items
2728 list_with_2_items:
2729 compile_node(comp, pns2->nodes[0]);
2730 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002731 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002732 }
2733 } else {
2734 // list with 1 item
2735 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002737 }
2738}
2739
Damien George969a6b32014-12-10 22:07:04 +00002740STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002741 mp_parse_node_t pn = pns->nodes[0];
2742 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002743 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002744 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002745 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2746 pns = (mp_parse_node_struct_t*)pn;
2747 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002748 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002749 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002750 compile_node(comp, pn);
2751 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002752 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2753 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2754 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2755 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002756 // dict/set with multiple elements
2757
2758 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002759 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002760 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2761
2762 // first element sets whether it's a dict or set
2763 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002764 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002765 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002766 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002767 compile_node(comp, pns->nodes[0]);
2768 EMIT(store_map);
2769 is_dict = true;
2770 } else {
2771 // a set
2772 compile_node(comp, pns->nodes[0]); // 1st value of set
2773 is_dict = false;
2774 }
2775
2776 // process rest of elements
2777 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002778 mp_parse_node_t pn = nodes[i];
2779 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002780 compile_node(comp, pn);
2781 if (is_dict) {
2782 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002783 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002784 return;
2785 }
2786 EMIT(store_map);
2787 } else {
2788 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002789 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002790 return;
2791 }
2792 }
2793 }
2794
Damien Georgee37dcaa2014-12-27 17:07:16 +00002795 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002796 // if it's a set, build it
2797 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002798 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002799 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002800 #endif
Damiend99b0522013-12-21 18:17:45 +00002801 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002802 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002803 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002804 // a dictionary comprehension
2805 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2806 } else {
2807 // a set comprehension
2808 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2809 }
2810 } else {
2811 // shouldn't happen
2812 assert(0);
2813 }
2814 } else {
2815 // set with one element
2816 goto set_with_one_element;
2817 }
2818 } else {
2819 // set with one element
2820 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002821 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002822 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002823 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002824 #else
2825 assert(0);
2826 #endif
Damien429d7192013-10-04 19:53:11 +01002827 }
2828}
2829
Damien George969a6b32014-12-10 22:07:04 +00002830STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002831 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002832}
2833
Damien George969a6b32014-12-10 22:07:04 +00002834STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002835 // object who's index we want is on top of stack
2836 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002837 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002838}
2839
Damien George969a6b32014-12-10 22:07:04 +00002840STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002841 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002843}
2844
Damien George83204f32014-12-27 17:20:41 +00002845#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002846STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002847 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2848 mp_parse_node_t pn = pns->nodes[0];
2849 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002850 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2852 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002853 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2854 pns = (mp_parse_node_struct_t*)pn;
2855 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002857 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002858 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002859 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002861 } else {
2862 // [?::x]
2863 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002865 }
Damiend99b0522013-12-21 18:17:45 +00002866 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002867 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002868 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2869 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2870 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2871 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002872 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002874 } else {
2875 // [?:x:x]
2876 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002878 }
2879 } else {
2880 // [?:x]
2881 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002882 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002883 }
2884 } else {
2885 // [?:x]
2886 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002887 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002888 }
2889}
2890
Damien George969a6b32014-12-10 22:07:04 +00002891STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002892 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002893 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2894 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002895}
2896
Damien George969a6b32014-12-10 22:07:04 +00002897STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002898 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002899 compile_subscript_3_helper(comp, pns);
2900}
Damien George83204f32014-12-27 17:20:41 +00002901#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002902
Damien George969a6b32014-12-10 22:07:04 +00002903STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002904 // if this is called then we are compiling a dict key:value pair
2905 compile_node(comp, pns->nodes[1]); // value
2906 compile_node(comp, pns->nodes[0]); // key
2907}
2908
Damien George969a6b32014-12-10 22:07:04 +00002909STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002910 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002911 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002912 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002913}
2914
Damien George969a6b32014-12-10 22:07:04 +00002915STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002916 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002917 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002918 return;
2919 }
Damiend99b0522013-12-21 18:17:45 +00002920 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002921 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002922 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002923 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2924 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002925 compile_node(comp, pns->nodes[0]);
2926 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002927 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002928 EMIT(yield_from);
2929 } else {
2930 compile_node(comp, pns->nodes[0]);
2931 EMIT(yield_value);
2932 }
2933}
2934
Damiend99b0522013-12-21 18:17:45 +00002935typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002936STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002937#define nc NULL
2938#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002939#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002940#include "grammar.h"
2941#undef nc
2942#undef c
2943#undef DEF_RULE
2944};
2945
Damien George6be0b0a2014-08-15 14:30:52 +01002946STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002947 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002948 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002949 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002950 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002951 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002952 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002953 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002954 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002955 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002956 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2957 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2958 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2959 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002960 case MP_PARSE_NODE_TOKEN:
2961 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002962 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002963 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002964 // do nothing
2965 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002966 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002967 }
2968 break;
Damien429d7192013-10-04 19:53:11 +01002969 default: assert(0);
2970 }
2971 } else {
Damiend99b0522013-12-21 18:17:45 +00002972 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002973 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002974 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002975 EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]), false);
Damien429d7192013-10-04 19:53:11 +01002976 } else {
Damien George5042bce2014-05-25 22:06:06 +01002977 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2978 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01002979#if MICROPY_DEBUG_PRINTERS
Damien George01039b52014-12-21 17:44:27 +00002980 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien George5042bce2014-05-25 22:06:06 +01002981 mp_parse_node_print(pn, 0);
2982#endif
2983 compile_syntax_error(comp, pn, "internal compiler error");
2984 } else {
2985 f(comp, pns);
2986 }
Damien429d7192013-10-04 19:53:11 +01002987 }
2988 }
2989}
2990
Damien George2ac4af62014-08-15 16:45:41 +01002991STATIC 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) {
Damien429d7192013-10-04 19:53:11 +01002992 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002993 qstr param_name = MP_QSTR_NULL;
2994 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002995 if (MP_PARSE_NODE_IS_ID(pn)) {
2996 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002997 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002998 // comes after a star, so counts as a keyword-only parameter
2999 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003000 } else {
Damien George2827d622014-04-27 15:50:52 +01003001 // comes before a star, so counts as a positional parameter
3002 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003003 }
Damienb14de212013-10-06 00:28:28 +01003004 } else {
Damiend99b0522013-12-21 18:17:45 +00003005 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3006 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3007 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3008 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003009 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003010 // comes after a star, so counts as a keyword-only parameter
3011 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003012 } else {
Damien George2827d622014-04-27 15:50:52 +01003013 // comes before a star, so counts as a positional parameter
3014 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003015 }
Damiend99b0522013-12-21 18:17:45 +00003016 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003017 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003018 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003019 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003020 // bare star
3021 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003022 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003023 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003024 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003025 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003026 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003027 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003028 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003029 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003030 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3031 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003032 } else {
3033 // shouldn't happen
3034 assert(0);
3035 }
Damiend99b0522013-12-21 18:17:45 +00003036 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3037 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003038 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003039 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003040 } else {
Damienb14de212013-10-06 00:28:28 +01003041 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003042 assert(0);
3043 }
Damien429d7192013-10-04 19:53:11 +01003044 }
3045
Damien George2827d622014-04-27 15:50:52 +01003046 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003047 bool added;
3048 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3049 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003050 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003051 return;
3052 }
Damien429d7192013-10-04 19:53:11 +01003053 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003054 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003055 }
3056}
3057
Damien George2827d622014-04-27 15:50:52 +01003058STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003059 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003060}
3061
Damien George2827d622014-04-27 15:50:52 +01003062STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003063 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3064}
3065
3066STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3067 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3068 // no annotation
3069 return;
3070 }
3071
3072 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3073 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3074 // named parameter with possible annotation
3075 // fallthrough
3076 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3077 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3078 // named star with possible annotation
3079 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3080 // fallthrough
3081 } else {
3082 // no annotation
3083 return;
3084 }
3085 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3086 // double star with possible annotation
3087 // fallthrough
3088 } else {
3089 // no annotation
3090 return;
3091 }
3092
3093 mp_parse_node_t pn_annotation = pns->nodes[1];
3094
3095 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3096 #if MICROPY_EMIT_NATIVE
3097 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3098 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3099 assert(id_info != NULL);
3100
3101 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3102 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3103 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3104 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3105 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003106 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003107 }
3108 }
3109 #endif // MICROPY_EMIT_NATIVE
3110 }
Damien429d7192013-10-04 19:53:11 +01003111}
3112
Damien George6be0b0a2014-08-15 14:30:52 +01003113STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
Damien429d7192013-10-04 19:53:11 +01003114 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003115 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003116 // no more nested if/for; compile inner expression
3117 compile_node(comp, pn_inner_expr);
3118 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003119 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003120 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003121 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003122 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003123 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003124 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003125 #endif
Damien429d7192013-10-04 19:53:11 +01003126 } else {
3127 EMIT(yield_value);
3128 EMIT(pop_top);
3129 }
Damiend99b0522013-12-21 18:17:45 +00003130 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003131 // if condition
Damiend99b0522013-12-21 18:17:45 +00003132 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003133 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3134 pn_iter = pns_comp_if->nodes[1];
3135 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003136 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003137 // for loop
Damiend99b0522013-12-21 18:17:45 +00003138 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003139 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003140 uint l_end2 = comp_next_label(comp);
3141 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003142 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003143 EMIT_ARG(label_assign, l_top2);
3144 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003145 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3146 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
Damien Georgeb9791222014-01-23 00:34:21 +00003147 EMIT_ARG(jump, l_top2);
3148 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003149 EMIT(for_iter_end);
3150 } else {
3151 // shouldn't happen
3152 assert(0);
3153 }
3154}
3155
Damien George1463c1f2014-04-25 23:52:57 +01003156STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3157#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003158 // see http://www.python.org/dev/peps/pep-0257/
3159
3160 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003161 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003162 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003163 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003164 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003165 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3166 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003167 for (int i = 0; i < num_nodes; i++) {
3168 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003169 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 +00003170 // not a newline, so this is the first statement; finish search
3171 break;
3172 }
3173 }
3174 // 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 +00003175 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003176 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003177 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003178 } else {
3179 return;
3180 }
3181
3182 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003183 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3184 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003185 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3186 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3187 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3188 // compile the doc string
3189 compile_node(comp, pns->nodes[0]);
3190 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003191 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003192 }
3193 }
Damien George1463c1f2014-04-25 23:52:57 +01003194#endif
Damien429d7192013-10-04 19:53:11 +01003195}
3196
Damien George1463c1f2014-04-25 23:52:57 +01003197STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003198 comp->pass = pass;
3199 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003200 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003201 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003202
Damien George36db6bc2014-05-07 17:24:22 +01003203 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003204 // reset maximum stack sizes in scope
3205 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003206 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003207 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003208 }
3209
Damien5ac1b2e2013-10-18 19:58:12 +01003210#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003211 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003212 scope_print_info(scope);
3213 }
Damien5ac1b2e2013-10-18 19:58:12 +01003214#endif
Damien429d7192013-10-04 19:53:11 +01003215
3216 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003217 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3218 assert(scope->kind == SCOPE_MODULE);
3219 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3220 compile_node(comp, pns->nodes[0]); // compile the expression
3221 EMIT(return_value);
3222 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003223 if (!comp->is_repl) {
3224 check_for_doc_string(comp, scope->pn);
3225 }
Damien429d7192013-10-04 19:53:11 +01003226 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003227 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003228 EMIT(return_value);
3229 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003230 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3231 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3232 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003233
3234 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003235 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003236 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003237 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003238 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003239 } else {
3240 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003241
Damien George2ac4af62014-08-15 16:45:41 +01003242 // argument annotations
3243 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3244
3245 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003246 mp_parse_node_t pn_annotation = pns->nodes[2];
3247 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3248 #if MICROPY_EMIT_NATIVE
3249 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3250 // nodes[2] can be null or a test-expr
3251 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3252 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3253 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3254 } else {
3255 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3256 }
Damien George2ac4af62014-08-15 16:45:41 +01003257 }
Damien Georgea5190a72014-08-15 22:39:08 +01003258 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003259 }
Damien George2ac4af62014-08-15 16:45:41 +01003260 }
Damien429d7192013-10-04 19:53:11 +01003261
3262 compile_node(comp, pns->nodes[3]); // 3 is function body
3263 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003264 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003265 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003266 EMIT(return_value);
3267 }
3268 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003269 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3270 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3271 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003272
3273 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003274 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003275 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003276 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003277 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3278 }
3279
3280 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003281
3282 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3283 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3284 EMIT(pop_top);
3285 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3286 }
Damien429d7192013-10-04 19:53:11 +01003287 EMIT(return_value);
3288 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3289 // a bit of a hack at the moment
3290
Damiend99b0522013-12-21 18:17:45 +00003291 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3292 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3293 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3294 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3295 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003296
Damien George708c0732014-04-27 19:23:46 +01003297 // We need a unique name for the comprehension argument (the iterator).
3298 // CPython uses .0, but we should be able to use anything that won't
3299 // clash with a user defined variable. Best to use an existing qstr,
3300 // so we use the blank qstr.
3301#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003302 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003303#else
3304 qstr qstr_arg = MP_QSTR_;
3305#endif
Damien George36db6bc2014-05-07 17:24:22 +01003306 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003307 bool added;
3308 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3309 assert(added);
3310 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003311 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003312 }
3313
3314 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003315 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003316 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003317 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003318 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003319 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003320 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003321 #endif
Damien429d7192013-10-04 19:53:11 +01003322 }
3323
Damien George6f355fd2014-04-10 14:11:31 +01003324 uint l_end = comp_next_label(comp);
3325 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003326 EMIT_ARG(load_id, qstr_arg);
3327 EMIT_ARG(label_assign, l_top);
3328 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003329 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3330 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003331 EMIT_ARG(jump, l_top);
3332 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003333 EMIT(for_iter_end);
3334
3335 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003336 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003337 }
3338 EMIT(return_value);
3339 } else {
3340 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003341 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3342 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3343 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003344
Damien George36db6bc2014-05-07 17:24:22 +01003345 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003346 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003347 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003348 assert(added);
3349 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003350 }
3351
Damien Georgeb9791222014-01-23 00:34:21 +00003352 EMIT_ARG(load_id, MP_QSTR___name__);
3353 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003354 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
Damien Georgeb9791222014-01-23 00:34:21 +00003355 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003356
3357 check_for_doc_string(comp, pns->nodes[2]);
3358 compile_node(comp, pns->nodes[2]); // 2 is class body
3359
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003360 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003361 assert(id != NULL);
3362 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003363 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003364 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003365#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003366 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003367#else
Damien George2bf7c092014-04-09 15:26:46 +01003368 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003369#endif
Damien429d7192013-10-04 19:53:11 +01003370 }
3371 EMIT(return_value);
3372 }
3373
Damien415eb6f2013-10-05 12:19:06 +01003374 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003375
3376 // make sure we match all the exception levels
3377 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003378}
3379
Damien George094d4502014-04-02 17:31:27 +01003380#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003381// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003382STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003383 comp->pass = pass;
3384 comp->scope_cur = scope;
3385 comp->next_label = 1;
3386
3387 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003388 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003389 return;
3390 }
3391
Damien George36db6bc2014-05-07 17:24:22 +01003392 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003393 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003394 }
3395
Damien826005c2013-10-05 23:17:28 +01003396 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003397 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3398 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3399 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003400
Damiend99b0522013-12-21 18:17:45 +00003401 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003402
Damiena2f2f7d2013-10-06 00:14:13 +01003403 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003404 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003405 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003406 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003407 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003408 }
3409
Damiend99b0522013-12-21 18:17:45 +00003410 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003411
Damiend99b0522013-12-21 18:17:45 +00003412 mp_parse_node_t pn_body = pns->nodes[3]; // body
3413 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003414 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3415
Damien Georgecbd2f742014-01-19 11:48:48 +00003416 /*
Damien George36db6bc2014-05-07 17:24:22 +01003417 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003418 //printf("----\n");
3419 scope_print_info(scope);
3420 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003421 */
Damien826005c2013-10-05 23:17:28 +01003422
3423 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003424 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3425 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003426 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3427 // no instructions
3428 continue;
3429 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3430 // an instruction; fall through
3431 } else {
3432 // not an instruction; error
3433 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3434 return;
3435 }
Damiend99b0522013-12-21 18:17:45 +00003436 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3437 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3438 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3439 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3440 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3441 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3442 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3443 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3444 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3445 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003446 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3447
3448 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003449 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003450 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003451 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003452 return;
3453 }
Damien George6f355fd2014-04-10 14:11:31 +01003454 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003455 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003456 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003457 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003458 } else if (op == MP_QSTR_align) {
3459 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3460 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3461 return;
3462 }
Damien George36db6bc2014-05-07 17:24:22 +01003463 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003464 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3465 }
3466 } else if (op == MP_QSTR_data) {
3467 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3468 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3469 return;
3470 }
Damien George36db6bc2014-05-07 17:24:22 +01003471 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003472 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003473 for (uint i = 1; i < n_args; i++) {
3474 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3475 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3476 return;
3477 }
3478 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3479 }
3480 }
Damien826005c2013-10-05 23:17:28 +01003481 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003482 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003483 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003484 }
3485 }
3486 }
3487
Damien George36db6bc2014-05-07 17:24:22 +01003488 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003489 bool success = EMIT_INLINE_ASM(end_pass);
3490 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003491 // TODO get proper exception from inline assembler
3492 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003493 }
Damienb05d7072013-10-05 13:37:10 +01003494 }
Damien429d7192013-10-04 19:53:11 +01003495}
Damien George094d4502014-04-02 17:31:27 +01003496#endif
Damien429d7192013-10-04 19:53:11 +01003497
Damien George1463c1f2014-04-25 23:52:57 +01003498STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003499#if !MICROPY_EMIT_CPYTHON
3500 // in Micro Python we put the *x parameter after all other parameters (except **y)
3501 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3502 id_info_t *id_param = NULL;
3503 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3504 id_info_t *id = &scope->id_info[i];
3505 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3506 if (id_param != NULL) {
3507 // swap star param with last param
3508 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3509 }
3510 break;
3511 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3512 id_param = id;
3513 }
3514 }
3515 }
3516#endif
3517
Damien429d7192013-10-04 19:53:11 +01003518 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003519 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003520 scope->num_locals = 0;
3521 for (int i = 0; i < scope->id_info_len; i++) {
3522 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003523 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003524 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3525 continue;
3526 }
3527 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3528 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3529 }
Damien George2827d622014-04-27 15:50:52 +01003530 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003531 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003532 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003533 }
3534 }
3535
3536 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003537#if MICROPY_EMIT_CPYTHON
3538 int num_cell = 0;
3539#endif
Damien9ecbcff2013-12-11 00:41:43 +00003540 for (int i = 0; i < scope->id_info_len; i++) {
3541 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003542#if MICROPY_EMIT_CPYTHON
3543 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003544 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003545 id->local_num = num_cell;
3546 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003547 }
Damien George6baf76e2013-12-30 22:32:17 +00003548#else
3549 // in Micro Python the cells come right after the fast locals
3550 // parameters are not counted here, since they remain at the start
3551 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003552 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003553 id->local_num = scope->num_locals;
3554 scope->num_locals += 1;
3555 }
3556#endif
Damien9ecbcff2013-12-11 00:41:43 +00003557 }
Damien9ecbcff2013-12-11 00:41:43 +00003558
3559 // compute the index of free vars (freevars[idx] in CPython)
3560 // make sure they are in the order of the parent scope
3561 if (scope->parent != NULL) {
3562 int num_free = 0;
3563 for (int i = 0; i < scope->parent->id_info_len; i++) {
3564 id_info_t *id = &scope->parent->id_info[i];
3565 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3566 for (int j = 0; j < scope->id_info_len; j++) {
3567 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003568 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003569 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003570#if MICROPY_EMIT_CPYTHON
3571 // in CPython the frees are numbered after the cells
3572 id2->local_num = num_cell + num_free;
3573#else
3574 // in Micro Python the frees come first, before the params
3575 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003576#endif
3577 num_free += 1;
3578 }
3579 }
3580 }
Damien429d7192013-10-04 19:53:11 +01003581 }
Damien George6baf76e2013-12-30 22:32:17 +00003582#if !MICROPY_EMIT_CPYTHON
3583 // in Micro Python shift all other locals after the free locals
3584 if (num_free > 0) {
3585 for (int i = 0; i < scope->id_info_len; i++) {
3586 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003587 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003588 id->local_num += num_free;
3589 }
3590 }
Damien George2827d622014-04-27 15:50:52 +01003591 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 +00003592 scope->num_locals += num_free;
3593 }
3594#endif
Damien429d7192013-10-04 19:53:11 +01003595 }
3596
Damien George8725f8f2014-02-15 19:33:11 +00003597 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003598
3599#if MICROPY_EMIT_CPYTHON
3600 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003601 if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3602 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003603 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003604 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003605 if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
Damien George8725f8f2014-02-15 19:33:11 +00003606 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003607 }
3608 }
Damien George882b3632014-04-02 15:56:31 +01003609#endif
3610
Damien429d7192013-10-04 19:53:11 +01003611 int num_free = 0;
3612 for (int i = 0; i < scope->id_info_len; i++) {
3613 id_info_t *id = &scope->id_info[i];
3614 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3615 num_free += 1;
3616 }
3617 }
3618 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003619 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003620 }
3621}
3622
Damien George65cad122014-04-06 11:48:15 +01003623mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien Georgeb140bff2014-04-09 12:54:21 +01003624 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003625 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003626 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003627 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003628
Damien826005c2013-10-05 23:17:28 +01003629 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003630 mp_map_t consts;
3631 mp_map_init(&consts, 0);
3632 pn = fold_constants(comp, pn, &consts);
3633 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003634
3635 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003636 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003637
Damien826005c2013-10-05 23:17:28 +01003638 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003639 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003640 comp->emit_method_table = &emit_pass1_method_table;
3641 comp->emit_inline_asm = NULL;
3642 comp->emit_inline_asm_method_table = NULL;
3643 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003644 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003645 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003646#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003647 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003648 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003649#endif
Damien826005c2013-10-05 23:17:28 +01003650 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003651 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003652 }
3653
3654 // update maximim number of labels needed
3655 if (comp->next_label > max_num_labels) {
3656 max_num_labels = comp->next_label;
3657 }
Damien429d7192013-10-04 19:53:11 +01003658 }
3659
Damien826005c2013-10-05 23:17:28 +01003660 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003661 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003662 compile_scope_compute_things(comp, s);
3663 }
3664
Damien826005c2013-10-05 23:17:28 +01003665 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003666 emit_pass1_free(comp->emit);
3667
Damien826005c2013-10-05 23:17:28 +01003668 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003669#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003670 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003671#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003672 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003673#endif
Damien3ef4abb2013-10-12 16:53:13 +01003674#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003675 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003676#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003677#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003678 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003679 if (false) {
3680 // dummy
3681
Damien3ef4abb2013-10-12 16:53:13 +01003682#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003683 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003684 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003685 if (emit_inline_thumb == NULL) {
3686 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3687 }
3688 comp->emit = NULL;
3689 comp->emit_method_table = NULL;
3690 comp->emit_inline_asm = emit_inline_thumb;
3691 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003692 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003693 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003694 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003695 }
Damienc025ebb2013-10-12 14:30:21 +01003696#endif
3697
Damien826005c2013-10-05 23:17:28 +01003698 } else {
Damienc025ebb2013-10-12 14:30:21 +01003699
3700 // choose the emit type
3701
Damien3ef4abb2013-10-12 16:53:13 +01003702#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003703 comp->emit = emit_cpython_new(max_num_labels);
3704 comp->emit_method_table = &emit_cpython_method_table;
3705#else
Damien826005c2013-10-05 23:17:28 +01003706 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003707
3708#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003709 case MP_EMIT_OPT_NATIVE_PYTHON:
3710 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003711#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003712 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003713 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003714 }
Damien13ed3a62013-10-08 09:05:10 +01003715 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003716#elif MICROPY_EMIT_X86
3717 if (emit_native == NULL) {
3718 emit_native = emit_native_x86_new(max_num_labels);
3719 }
3720 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003721#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003722 if (emit_native == NULL) {
3723 emit_native = emit_native_thumb_new(max_num_labels);
3724 }
3725 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003726#elif MICROPY_EMIT_ARM
3727 if (emit_native == NULL) {
3728 emit_native = emit_native_arm_new(max_num_labels);
3729 }
3730 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003731#endif
3732 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003733 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien George36db6bc2014-05-07 17:24:22 +01003734
3735 // native emitters need an extra pass to compute stack size
3736 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3737
Damien7af3d192013-10-07 00:02:49 +01003738 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003739#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003740
Damien826005c2013-10-05 23:17:28 +01003741 default:
3742 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003743 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003744 }
3745 comp->emit = emit_bc;
3746 comp->emit_method_table = &emit_bc_method_table;
3747 break;
3748 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003749#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003750
Damien George36db6bc2014-05-07 17:24:22 +01003751 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003752 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003753 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3754 }
3755
3756 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003757 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003758 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003759 }
Damien6cdd3af2013-10-05 18:08:26 +01003760 }
Damien429d7192013-10-04 19:53:11 +01003761 }
3762
Damien George41d02b62014-01-24 22:42:28 +00003763 // free the emitters
3764#if !MICROPY_EMIT_CPYTHON
3765 if (emit_bc != NULL) {
3766 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003767 }
Damien George41d02b62014-01-24 22:42:28 +00003768#if MICROPY_EMIT_NATIVE
3769 if (emit_native != NULL) {
3770#if MICROPY_EMIT_X64
3771 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003772#elif MICROPY_EMIT_X86
3773 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003774#elif MICROPY_EMIT_THUMB
3775 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003776#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003777 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003778#endif
3779 }
3780#endif
3781#if MICROPY_EMIT_INLINE_THUMB
3782 if (emit_inline_thumb != NULL) {
3783 emit_inline_thumb_free(emit_inline_thumb);
3784 }
3785#endif
3786#endif // !MICROPY_EMIT_CPYTHON
3787
Damien George52b5d762014-09-23 15:31:56 +00003788 // free the parse tree
3789 mp_parse_node_free(pn);
3790
Damien George41d02b62014-01-24 22:42:28 +00003791 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003792 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003793 for (scope_t *s = module_scope; s;) {
3794 scope_t *next = s->next;
3795 scope_free(s);
3796 s = next;
3797 }
Damien5ac1b2e2013-10-18 19:58:12 +01003798
Damien George41d02b62014-01-24 22:42:28 +00003799 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003800 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003801 m_del_obj(compiler_t, comp);
3802
Damien Georgea91ac202014-10-05 19:01:34 +01003803 if (compile_error != MP_OBJ_NULL) {
3804 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003805 } else {
3806#if MICROPY_EMIT_CPYTHON
3807 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003808 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003809 return mp_const_true;
3810#else
3811 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003812 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003813#endif
3814 }
Damien429d7192013-10-04 19:53:11 +01003815}