blob: 972ab831827d6096c8855d82c88049824855d237 [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 George969a6b32014-12-10 22:07:04 +00001617STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001618 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001619 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1620 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001621 } else {
Damiend99b0522013-12-21 18:17:45 +00001622 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1623 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001624 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001625 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001626 }
Damien429d7192013-10-04 19:53:11 +01001627 }
1628 }
1629}
1630
Damien George969a6b32014-12-10 22:07:04 +00001631STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001632 if (comp->pass == MP_PASS_SCOPE) {
Damiend99b0522013-12-21 18:17:45 +00001633 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1634 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001635 } else {
Damiend99b0522013-12-21 18:17:45 +00001636 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1637 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001638 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001639 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001640 }
Damien429d7192013-10-04 19:53:11 +01001641 }
1642 }
1643}
1644
Damien George969a6b32014-12-10 22:07:04 +00001645STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001646 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001647 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001648 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 +00001649 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001650 // assertion message
1651 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001652 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001653 }
Damien Georgeb9791222014-01-23 00:34:21 +00001654 EMIT_ARG(raise_varargs, 1);
1655 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001656}
1657
Damien George969a6b32014-12-10 22:07:04 +00001658STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001659 // TODO proper and/or short circuiting
1660
Damien George6f355fd2014-04-10 14:11:31 +01001661 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001662
Damien George391db862014-10-17 17:57:33 +00001663 // optimisation: don't emit anything when "if False" (not in CPython)
1664 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1665 uint l_fail = comp_next_label(comp);
1666 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001667
Damien George391db862014-10-17 17:57:33 +00001668 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001669
Damien George391db862014-10-17 17:57:33 +00001670 // optimisation: skip everything else when "if True" (not in CPython)
1671 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1672 goto done;
1673 }
1674
1675 if (
1676 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1677 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1678 // optimisation: don't jump if last instruction was return
1679 && !EMIT(last_emit_was_return_value)
1680 ) {
1681 // jump over elif/else blocks
1682 EMIT_ARG(jump, l_end);
1683 }
1684
1685 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001686 }
1687
Damien George235f9b32014-10-17 17:30:16 +00001688 // compile elif blocks (if any)
1689 mp_parse_node_t *pn_elif;
1690 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1691 for (int i = 0; i < n_elif; i++) {
1692 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1693 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001694
Damien George391db862014-10-17 17:57:33 +00001695 // optimisation: don't emit anything when "if False" (not in CPython)
1696 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1697 uint l_fail = comp_next_label(comp);
1698 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001699
Damien George391db862014-10-17 17:57:33 +00001700 compile_node(comp, pns_elif->nodes[1]); // elif block
1701
1702 // optimisation: skip everything else when "elif True" (not in CPython)
1703 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1704 goto done;
1705 }
1706
1707 // optimisation: don't jump if last instruction was return
1708 if (!EMIT(last_emit_was_return_value)) {
1709 EMIT_ARG(jump, l_end);
1710 }
1711 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001712 }
1713 }
1714
1715 // compile else block
1716 compile_node(comp, pns->nodes[3]); // can be null
1717
Damien George391db862014-10-17 17:57:33 +00001718done:
Damien Georgeb9791222014-01-23 00:34:21 +00001719 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001720}
1721
Damien Georgecbddb272014-02-01 20:08:18 +00001722#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001723 uint16_t old_break_label = comp->break_label; \
1724 uint16_t old_continue_label = comp->continue_label; \
1725 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001726 uint break_label = comp_next_label(comp); \
1727 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001728 comp->break_label = break_label; \
1729 comp->continue_label = continue_label; \
1730 comp->break_continue_except_level = comp->cur_except_level;
1731
1732#define END_BREAK_CONTINUE_BLOCK \
1733 comp->break_label = old_break_label; \
1734 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001735 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001736
Damien George969a6b32014-12-10 22:07:04 +00001737STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001738 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001739
Damience89a212013-10-15 22:25:17 +01001740 // compared to CPython, we have an optimised version of while loops
1741#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001742 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(setup_loop, break_label);
1744 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001745 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1746 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001747 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001748 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001749 }
Damien Georgeb9791222014-01-23 00:34:21 +00001750 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001751 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1752 // this is a small hack to agree with CPython
1753 if (!node_is_const_true(pns->nodes[0])) {
1754 EMIT(pop_block);
1755 }
Damience89a212013-10-15 22:25:17 +01001756#else
Damien George391db862014-10-17 17:57:33 +00001757 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1758 uint top_label = comp_next_label(comp);
1759 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1760 EMIT_ARG(jump, continue_label);
1761 }
1762 EMIT_ARG(label_assign, top_label);
1763 compile_node(comp, pns->nodes[1]); // body
1764 EMIT_ARG(label_assign, continue_label);
1765 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1766 }
Damience89a212013-10-15 22:25:17 +01001767#endif
1768
1769 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001770 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001771
1772 compile_node(comp, pns->nodes[2]); // else
1773
Damien Georgeb9791222014-01-23 00:34:21 +00001774 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001775}
1776
Damien George2ac4af62014-08-15 16:45:41 +01001777#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001778// This function compiles an optimised for-loop of the form:
1779// for <var> in range(<start>, <end>, <step>):
1780// <body>
1781// else:
1782// <else>
1783// <var> must be an identifier and <step> must be a small-int.
1784//
1785// Semantics of for-loop require:
1786// - final failing value should not be stored in the loop variable
1787// - if the loop never runs, the loop variable should never be assigned
1788// - assignments to <var>, <end> or <step> in the body do not alter the loop
1789// (<step> is a constant for us, so no need to worry about it changing)
1790//
1791// If <end> is a small-int, then the stack during the for-loop contains just
1792// the current value of <var>. Otherwise, the stack contains <end> then the
1793// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001794STATIC 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 +00001795 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001796
Damien George6f355fd2014-04-10 14:11:31 +01001797 uint top_label = comp_next_label(comp);
1798 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001799
Damien Georgee181c0d2014-12-12 17:19:56 +00001800 // put the end value on the stack if it's not a small-int constant
1801 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1802 if (end_on_stack) {
1803 compile_node(comp, pn_end);
1804 }
1805
1806 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001807 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001808
Damien Georgeb9791222014-01-23 00:34:21 +00001809 EMIT_ARG(jump, entry_label);
1810 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001811
Damien Georgec33ce602014-12-11 17:35:23 +00001812 // duplicate next value and store it to var
1813 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001814 c_assign(comp, pn_var, ASSIGN_STORE);
1815
Damienf3822fc2013-11-09 20:12:03 +00001816 // compile body
1817 compile_node(comp, pn_body);
1818
Damien Georgeb9791222014-01-23 00:34:21 +00001819 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001820
Damien Georgee181c0d2014-12-12 17:19:56 +00001821 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001822 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001823 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001824
Damien Georgeb9791222014-01-23 00:34:21 +00001825 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001826
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001827 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001828 if (end_on_stack) {
1829 EMIT(dup_top_two);
1830 EMIT(rot_two);
1831 } else {
1832 EMIT(dup_top);
1833 compile_node(comp, pn_end);
1834 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001835 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1836 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001837 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001838 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001839 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001840 }
Damien Georgeb9791222014-01-23 00:34:21 +00001841 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001842
1843 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001844 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001845
1846 compile_node(comp, pn_else);
1847
Damien Georgeb9791222014-01-23 00:34:21 +00001848 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001849
1850 // discard final value of var that failed the loop condition
1851 EMIT(pop_top);
1852
1853 // discard <end> value if it's on the stack
1854 if (end_on_stack) {
1855 EMIT(pop_top);
1856 }
Damienf72fd0e2013-11-06 20:20:49 +00001857}
Damien George2ac4af62014-08-15 16:45:41 +01001858#endif
Damienf72fd0e2013-11-06 20:20:49 +00001859
Damien George969a6b32014-12-10 22:07:04 +00001860STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001861#if !MICROPY_EMIT_CPYTHON
1862 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1863 // this is actually slower, but uses no heap memory
1864 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001865 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 +00001866 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001867 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1868 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1869 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1870 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001871 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1872 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001873 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001874 mp_parse_node_t pn_range_start;
1875 mp_parse_node_t pn_range_end;
1876 mp_parse_node_t pn_range_step;
1877 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001878 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001879 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001880 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001881 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001882 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001883 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001884 } else if (n_args == 2) {
1885 pn_range_start = args[0];
1886 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001887 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001888 } else {
1889 pn_range_start = args[0];
1890 pn_range_end = args[1];
1891 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001892 // We need to know sign of step. This is possible only if it's constant
1893 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1894 optimize = false;
1895 }
Damienf72fd0e2013-11-06 20:20:49 +00001896 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001897 }
1898 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001899 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1900 return;
1901 }
1902 }
1903 }
1904#endif
1905
Damien Georgecbddb272014-02-01 20:08:18 +00001906 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001907 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001908
Damien George6f355fd2014-04-10 14:11:31 +01001909 uint pop_label = comp_next_label(comp);
1910 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001911
Damience89a212013-10-15 22:25:17 +01001912 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1913#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001914 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001915#endif
1916
Damien429d7192013-10-04 19:53:11 +01001917 compile_node(comp, pns->nodes[1]); // iterator
1918 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001919 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001920 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001921 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1922 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001923 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001924 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001925 }
Damien Georgeb9791222014-01-23 00:34:21 +00001926 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001927 EMIT(for_iter_end);
1928
1929 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001930 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001931
Damience89a212013-10-15 22:25:17 +01001932#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001933 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001934#endif
Damien429d7192013-10-04 19:53:11 +01001935
1936 compile_node(comp, pns->nodes[3]); // else (not tested)
1937
Damien Georgeb9791222014-01-23 00:34:21 +00001938 EMIT_ARG(label_assign, break_label);
1939 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001940}
1941
Damien George6be0b0a2014-08-15 14:30:52 +01001942STATIC 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 +01001943 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001944 uint l1 = comp_next_label(comp);
1945 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001946
Damien Georgeb9791222014-01-23 00:34:21 +00001947 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001948 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001949
Damien429d7192013-10-04 19:53:11 +01001950 compile_node(comp, pn_body); // body
1951 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001952 EMIT_ARG(jump, success_label); // jump over exception handler
1953
1954 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001955 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001956
Damien George6f355fd2014-04-10 14:11:31 +01001957 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001958
1959 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001960 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1961 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001962
1963 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001964 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001965
Damiend99b0522013-12-21 18:17:45 +00001966 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001967 // this is a catch all exception handler
1968 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001969 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001970 return;
1971 }
1972 } else {
1973 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001974 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1975 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1976 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1977 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001978 // handler binds the exception to a local
1979 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001980 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001981 }
1982 }
1983 EMIT(dup_top);
1984 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001985 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001986 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001987 }
1988
1989 EMIT(pop_top);
1990
1991 if (qstr_exception_local == 0) {
1992 EMIT(pop_top);
1993 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001994 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001995 }
1996
1997 EMIT(pop_top);
1998
Damien George6f355fd2014-04-10 14:11:31 +01001999 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002000 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002001 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002002 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002003 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002004 }
2005 compile_node(comp, pns_except->nodes[1]);
2006 if (qstr_exception_local != 0) {
2007 EMIT(pop_block);
2008 }
2009 EMIT(pop_except);
2010 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002011 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2012 EMIT_ARG(label_assign, l3);
2013 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2014 EMIT_ARG(store_id, qstr_exception_local);
2015 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002016
Damien George8dcc0c72014-03-27 10:55:21 +00002017 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002018 EMIT(end_finally);
2019 }
Damien Georgeb9791222014-01-23 00:34:21 +00002020 EMIT_ARG(jump, l2);
2021 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002022 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002023 }
2024
Damien George8dcc0c72014-03-27 10:55:21 +00002025 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002026 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002027 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002028
Damien Georgeb9791222014-01-23 00:34:21 +00002029 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002030 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002031 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002032}
2033
Damien George6be0b0a2014-08-15 14:30:52 +01002034STATIC 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 +01002035 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002036
Damien Georgeb9791222014-01-23 00:34:21 +00002037 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002038 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002039
Damien429d7192013-10-04 19:53:11 +01002040 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002041 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002042 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002043 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002044 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002045 } else {
2046 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2047 }
2048 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002049 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2050 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002051 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002052
Damien George8dcc0c72014-03-27 10:55:21 +00002053 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002054 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002055}
2056
Damien George969a6b32014-12-10 22:07:04 +00002057STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002058 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2059 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2060 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002061 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002062 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2063 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002064 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002065 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002066 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002067 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002068 // no finally
2069 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2070 } else {
2071 // have finally
Damiend99b0522013-12-21 18:17:45 +00002072 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 +01002073 }
2074 } else {
2075 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002076 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002077 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002078 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002079 }
2080 } else {
2081 // shouldn't happen
2082 assert(0);
2083 }
2084}
2085
Damien George6be0b0a2014-08-15 14:30:52 +01002086STATIC 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 +01002087 if (n == 0) {
2088 // no more pre-bits, compile the body of the with
2089 compile_node(comp, body);
2090 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002091 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002092 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002093 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002094 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002095 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002096 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002097 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2098 } else {
2099 // this pre-bit is just an expression
2100 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002101 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002102 EMIT(pop_top);
2103 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002104 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002105 // compile additional pre-bits and the body
2106 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2107 // finish this with block
2108 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002109 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2110 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002111 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002112 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002113 EMIT(end_finally);
2114 }
2115}
2116
Damien George969a6b32014-12-10 22:07:04 +00002117STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002118 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002119 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002120 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2121 assert(n > 0);
2122
2123 // compile in a nested fashion
2124 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2125}
2126
Damien George969a6b32014-12-10 22:07:04 +00002127STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002128 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002129 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2130 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002131 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002132 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002133 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002134 EMIT(pop_top);
2135
Damien429d7192013-10-04 19:53:11 +01002136 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002137 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002138 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
2139 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002140 // do nothing with a lonely constant
2141 } else {
2142 compile_node(comp, pns->nodes[0]); // just an expression
2143 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2144 }
Damien429d7192013-10-04 19:53:11 +01002145 }
2146 } else {
Damiend99b0522013-12-21 18:17:45 +00002147 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2148 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002149 if (kind == PN_expr_stmt_augassign) {
2150 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2151 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002152 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002153 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002154 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002155 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2156 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2157 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2158 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2159 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2160 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2161 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2162 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2163 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2164 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2165 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2166 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2167 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01002168 }
Damien George7e5fb242014-02-01 22:18:47 +00002169 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002170 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2171 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002172 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2173 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002174 // following CPython, we store left-most first
2175 if (rhs > 0) {
2176 EMIT(dup_top);
2177 }
2178 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2179 for (int i = 0; i < rhs; i++) {
2180 if (i + 1 < rhs) {
2181 EMIT(dup_top);
2182 }
Damiend99b0522013-12-21 18:17:45 +00002183 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002184 }
2185 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002186 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002187 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2188 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2189 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002190 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002191 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2192 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002193 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2194 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2195 // can't optimise when it's a star expression on the lhs
2196 goto no_optimisation;
2197 }
Damien429d7192013-10-04 19:53:11 +01002198 compile_node(comp, pns10->nodes[0]); // rhs
2199 compile_node(comp, pns10->nodes[1]); // rhs
2200 EMIT(rot_two);
2201 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2202 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002203 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2204 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2205 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2206 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002207 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002208 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2209 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002210 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2211 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2212 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2213 // can't optimise when it's a star expression on the lhs
2214 goto no_optimisation;
2215 }
Damien429d7192013-10-04 19:53:11 +01002216 compile_node(comp, pns10->nodes[0]); // rhs
2217 compile_node(comp, pns10->nodes[1]); // rhs
2218 compile_node(comp, pns10->nodes[2]); // rhs
2219 EMIT(rot_three);
2220 EMIT(rot_two);
2221 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2222 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2223 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2224 } else {
Damien George495d7812014-04-08 17:51:47 +01002225 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002226 compile_node(comp, pns1->nodes[0]); // rhs
2227 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2228 }
2229 } else {
2230 // shouldn't happen
2231 assert(0);
2232 }
2233 }
2234}
2235
Damien George6be0b0a2014-08-15 14:30:52 +01002236STATIC 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 +00002237 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002238 compile_node(comp, pns->nodes[0]);
2239 for (int i = 1; i < num_nodes; i += 1) {
2240 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002241 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002242 }
2243}
2244
Damien George969a6b32014-12-10 22:07:04 +00002245STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002246 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2247 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002248
Damien George6f355fd2014-04-10 14:11:31 +01002249 uint l_fail = comp_next_label(comp);
2250 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002251 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2252 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002253 EMIT_ARG(jump, l_end);
2254 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002255 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002256 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002257 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002258}
2259
Damien George969a6b32014-12-10 22:07:04 +00002260STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002261 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002262 //mp_parse_node_t pn_params = pns->nodes[0];
2263 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002264
Damien George36db6bc2014-05-07 17:24:22 +01002265 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002266 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002267 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 +01002268 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002269 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002270 }
2271
2272 // get the scope for this lambda
2273 scope_t *this_scope = (scope_t*)pns->nodes[2];
2274
2275 // make the lambda
2276 close_over_variables_etc(comp, this_scope, 0, 0);
2277}
2278
Damien George969a6b32014-12-10 22:07:04 +00002279STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002280 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002281 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002282 for (int i = 0; i < n; i += 1) {
2283 compile_node(comp, pns->nodes[i]);
2284 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002285 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002286 }
2287 }
Damien Georgeb9791222014-01-23 00:34:21 +00002288 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002289}
2290
Damien George969a6b32014-12-10 22:07:04 +00002291STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002292 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002293 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002294 for (int i = 0; i < n; i += 1) {
2295 compile_node(comp, pns->nodes[i]);
2296 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002297 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002298 }
2299 }
Damien Georgeb9791222014-01-23 00:34:21 +00002300 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002301}
2302
Damien George969a6b32014-12-10 22:07:04 +00002303STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002304 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002305 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002306}
2307
Damien George969a6b32014-12-10 22:07:04 +00002308STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002309 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002310 compile_node(comp, pns->nodes[0]);
2311 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002312 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002313 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002314 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002315 }
2316 for (int i = 1; i + 1 < num_nodes; i += 2) {
2317 compile_node(comp, pns->nodes[i + 1]);
2318 if (i + 2 < num_nodes) {
2319 EMIT(dup_top);
2320 EMIT(rot_three);
2321 }
Damien George7e5fb242014-02-01 22:18:47 +00002322 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002323 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002324 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002325 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2326 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2327 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2328 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2329 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2330 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2331 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2332 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002333 }
2334 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002335 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2336 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2337 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002338 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002339 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002340 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002341 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002342 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002343 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002344 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002345 }
2346 } else {
2347 // shouldn't happen
2348 assert(0);
2349 }
2350 } else {
2351 // shouldn't happen
2352 assert(0);
2353 }
2354 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002355 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002356 }
2357 }
2358 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002359 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002360 EMIT_ARG(jump, l_end);
2361 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002362 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002363 EMIT(rot_two);
2364 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002365 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002366 }
2367}
2368
Damien George969a6b32014-12-10 22:07:04 +00002369STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002370 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002371}
2372
Damien George969a6b32014-12-10 22:07:04 +00002373STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002374 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002375}
2376
Damien George969a6b32014-12-10 22:07:04 +00002377STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002378 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002379}
2380
Damien George969a6b32014-12-10 22:07:04 +00002381STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002382 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002383}
2384
Damien George969a6b32014-12-10 22:07:04 +00002385STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002386 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002387 compile_node(comp, pns->nodes[0]);
2388 for (int i = 1; i + 1 < num_nodes; i += 2) {
2389 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002390 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002391 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002392 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002393 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002394 } else {
2395 // shouldn't happen
2396 assert(0);
2397 }
2398 }
2399}
2400
Damien George969a6b32014-12-10 22:07:04 +00002401STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002402 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002403 compile_node(comp, pns->nodes[0]);
2404 for (int i = 1; i + 1 < num_nodes; i += 2) {
2405 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002406 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002407 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002408 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002409 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002410 } else {
2411 // shouldn't happen
2412 assert(0);
2413 }
2414 }
2415}
2416
Damien George969a6b32014-12-10 22:07:04 +00002417STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002418 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002419 compile_node(comp, pns->nodes[0]);
2420 for (int i = 1; i + 1 < num_nodes; i += 2) {
2421 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002422 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002423 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002424 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002425 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002426 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002427 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002428 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002429 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002430 } else {
2431 // shouldn't happen
2432 assert(0);
2433 }
2434 }
2435}
2436
Damien George969a6b32014-12-10 22:07:04 +00002437STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002438 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002439 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002440 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002441 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002442 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002443 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002444 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002445 } else {
2446 // shouldn't happen
2447 assert(0);
2448 }
2449}
2450
Damien George969a6b32014-12-10 22:07:04 +00002451STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002452 // this is to handle special super() call
2453 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2454
2455 compile_generic_all_nodes(comp, pns);
2456}
2457
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002458STATIC 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 +01002459 // function to call is on top of stack
2460
Damien George35e2a4e2014-02-05 00:51:47 +00002461#if !MICROPY_EMIT_CPYTHON
2462 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002463 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 +00002464 EMIT_ARG(load_id, MP_QSTR___class__);
2465 // get first argument to function
2466 bool found = false;
2467 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002468 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
2469 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
Damien George35e2a4e2014-02-05 00:51:47 +00002470 found = true;
2471 break;
2472 }
2473 }
2474 if (!found) {
2475 printf("TypeError: super() call cannot find self\n");
2476 return;
2477 }
Damien George922ddd62014-04-09 12:43:17 +01002478 EMIT_ARG(call_function, 2, 0, 0);
Damien George35e2a4e2014-02-05 00:51:47 +00002479 return;
2480 }
2481#endif
2482
Damien George2c0842b2014-04-27 16:46:51 +01002483 // get the list of arguments
2484 mp_parse_node_t *args;
2485 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002486
Damien George2c0842b2014-04-27 16:46:51 +01002487 // compile the arguments
2488 // Rather than calling compile_node on the list, we go through the list of args
2489 // explicitly here so that we can count the number of arguments and give sensible
2490 // error messages.
2491 int n_positional = n_positional_extra;
2492 uint n_keyword = 0;
2493 uint star_flags = 0;
2494 for (int i = 0; i < n_args; i++) {
2495 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2496 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2497 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2498 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2499 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2500 return;
2501 }
2502 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2503 compile_node(comp, pns_arg->nodes[0]);
2504 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2505 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2506 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2507 return;
2508 }
2509 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2510 compile_node(comp, pns_arg->nodes[0]);
2511 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2512 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2513 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2514 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2515 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2516 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2517 return;
2518 }
Damien George968bf342014-04-27 19:12:05 +01002519 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002520 compile_node(comp, pns2->nodes[0]);
2521 n_keyword += 1;
2522 } else {
2523 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2524 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2525 n_positional++;
2526 }
2527 } else {
2528 goto normal_argument;
2529 }
2530 } else {
2531 normal_argument:
2532 if (n_keyword > 0) {
2533 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2534 return;
2535 }
2536 compile_node(comp, args[i]);
2537 n_positional++;
2538 }
Damien429d7192013-10-04 19:53:11 +01002539 }
2540
Damien George2c0842b2014-04-27 16:46:51 +01002541 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002542 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002543 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002544 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002545 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002546 }
Damien429d7192013-10-04 19:53:11 +01002547}
2548
Damien George969a6b32014-12-10 22:07:04 +00002549STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002550 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002551 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002552 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 +01002553 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002554 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2555 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002556 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002557 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002558 i += 1;
2559 } else {
2560 compile_node(comp, pns->nodes[i]);
2561 }
Damien George35e2a4e2014-02-05 00:51:47 +00002562 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002563 }
2564}
2565
Damien George969a6b32014-12-10 22:07:04 +00002566STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002567 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002568 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002569}
2570
Damien George969a6b32014-12-10 22:07:04 +00002571STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002572 // a list of strings
Damien63321742013-12-10 17:41:49 +00002573
2574 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002575 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002576 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002577 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002578 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002579 int pn_kind;
2580 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2581 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2582 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2583 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2584 } else {
2585 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2586 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
2587 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
2588 pn_kind = MP_PARSE_NODE_STRING;
Damien George40f3c022014-07-03 13:25:24 +01002589 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002590 }
Damien63321742013-12-10 17:41:49 +00002591 if (i == 0) {
2592 string_kind = pn_kind;
2593 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002594 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002595 return;
2596 }
Damien429d7192013-10-04 19:53:11 +01002597 }
Damien63321742013-12-10 17:41:49 +00002598
Damien63321742013-12-10 17:41:49 +00002599 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002600 byte *q_ptr;
2601 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002602 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002603 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002604 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002605 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2606 memcpy(s_dest, s, s_len);
2607 s_dest += s_len;
2608 } else {
2609 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002610 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2611 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002612 }
Damien63321742013-12-10 17:41:49 +00002613 }
Damien George55baff42014-01-21 21:40:13 +00002614 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002615
Damien Georgeb9791222014-01-23 00:34:21 +00002616 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002617}
2618
2619// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002620STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002621 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2622 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2623 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002624
Damien George36db6bc2014-05-07 17:24:22 +01002625 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002626 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002627 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 +01002628 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002629 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002630 }
2631
2632 // get the scope for this comprehension
2633 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2634
2635 // compile the comprehension
2636 close_over_variables_etc(comp, this_scope, 0, 0);
2637
2638 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2639 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002640 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002641}
2642
Damien George969a6b32014-12-10 22:07:04 +00002643STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002644 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002645 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002646 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2647 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2648 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2649 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2650 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2651 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2652 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002653 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002654 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002655 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002656 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002657 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002658 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002659 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002660 // generator expression
2661 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2662 } else {
2663 // tuple with 2 items
2664 goto tuple_with_2_items;
2665 }
2666 } else {
2667 // tuple with 2 items
2668 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002669 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002670 }
2671 } else {
2672 // parenthesis around a single item, is just that item
2673 compile_node(comp, pns->nodes[0]);
2674 }
2675}
2676
Damien George969a6b32014-12-10 22:07:04 +00002677STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002678 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002679 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002680 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002681 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2682 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2683 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2684 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2685 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002686 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002687 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002688 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002689 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002690 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002691 // list of many items
2692 compile_node(comp, pns2->nodes[0]);
2693 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002694 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002695 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002696 // list comprehension
2697 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2698 } else {
2699 // list with 2 items
2700 goto list_with_2_items;
2701 }
2702 } else {
2703 // list with 2 items
2704 list_with_2_items:
2705 compile_node(comp, pns2->nodes[0]);
2706 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002707 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002708 }
2709 } else {
2710 // list with 1 item
2711 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002712 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002713 }
2714}
2715
Damien George969a6b32014-12-10 22:07:04 +00002716STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002717 mp_parse_node_t pn = pns->nodes[0];
2718 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002719 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002720 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002721 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2722 pns = (mp_parse_node_struct_t*)pn;
2723 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002724 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002725 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002726 compile_node(comp, pn);
2727 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002728 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2729 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2730 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2731 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002732 // dict/set with multiple elements
2733
2734 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002735 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002736 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2737
2738 // first element sets whether it's a dict or set
2739 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002740 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002741 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002742 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002743 compile_node(comp, pns->nodes[0]);
2744 EMIT(store_map);
2745 is_dict = true;
2746 } else {
2747 // a set
2748 compile_node(comp, pns->nodes[0]); // 1st value of set
2749 is_dict = false;
2750 }
2751
2752 // process rest of elements
2753 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002754 mp_parse_node_t pn = nodes[i];
2755 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002756 compile_node(comp, pn);
2757 if (is_dict) {
2758 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002759 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002760 return;
2761 }
2762 EMIT(store_map);
2763 } else {
2764 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002765 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002766 return;
2767 }
2768 }
2769 }
2770
2771 // if it's a set, build it
2772 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002773 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002774 }
Damiend99b0522013-12-21 18:17:45 +00002775 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002776 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002777 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002778 // a dictionary comprehension
2779 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2780 } else {
2781 // a set comprehension
2782 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2783 }
2784 } else {
2785 // shouldn't happen
2786 assert(0);
2787 }
2788 } else {
2789 // set with one element
2790 goto set_with_one_element;
2791 }
2792 } else {
2793 // set with one element
2794 set_with_one_element:
2795 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002797 }
2798}
2799
Damien George969a6b32014-12-10 22:07:04 +00002800STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002801 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002802}
2803
Damien George969a6b32014-12-10 22:07:04 +00002804STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002805 // object who's index we want is on top of stack
2806 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002807 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002808}
2809
Damien George969a6b32014-12-10 22:07:04 +00002810STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002811 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002813}
2814
Damien George969a6b32014-12-10 22:07:04 +00002815STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002816 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2817 mp_parse_node_t pn = pns->nodes[0];
2818 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002819 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002820 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2821 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002822 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2823 pns = (mp_parse_node_struct_t*)pn;
2824 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002825 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002826 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002827 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002828 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002829 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002830 } else {
2831 // [?::x]
2832 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002833 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002834 }
Damiend99b0522013-12-21 18:17:45 +00002835 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002836 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002837 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2838 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2839 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2840 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002841 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002842 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002843 } else {
2844 // [?:x:x]
2845 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002847 }
2848 } else {
2849 // [?:x]
2850 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002851 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002852 }
2853 } else {
2854 // [?:x]
2855 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002857 }
2858}
2859
Damien George969a6b32014-12-10 22:07:04 +00002860STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002861 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002862 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2863 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002864}
2865
Damien George969a6b32014-12-10 22:07:04 +00002866STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002867 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002868 compile_subscript_3_helper(comp, pns);
2869}
2870
Damien George969a6b32014-12-10 22:07:04 +00002871STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002872 // if this is called then we are compiling a dict key:value pair
2873 compile_node(comp, pns->nodes[1]); // value
2874 compile_node(comp, pns->nodes[0]); // key
2875}
2876
Damien George969a6b32014-12-10 22:07:04 +00002877STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002878 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002879 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002880 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002881}
2882
Damien George969a6b32014-12-10 22:07:04 +00002883STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002884 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002885 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002886 return;
2887 }
Damiend99b0522013-12-21 18:17:45 +00002888 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002889 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002890 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002891 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2892 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002893 compile_node(comp, pns->nodes[0]);
2894 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002896 EMIT(yield_from);
2897 } else {
2898 compile_node(comp, pns->nodes[0]);
2899 EMIT(yield_value);
2900 }
2901}
2902
Damiend99b0522013-12-21 18:17:45 +00002903typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002904STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002905#define nc NULL
2906#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002907#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002908#include "grammar.h"
2909#undef nc
2910#undef c
2911#undef DEF_RULE
2912};
2913
Damien George6be0b0a2014-08-15 14:30:52 +01002914STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002915 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002916 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002917 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002918 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002919 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002920 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002921 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002922 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002923 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002924 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2925 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2926 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2927 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002928 case MP_PARSE_NODE_TOKEN:
2929 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002930 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002931 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002932 // do nothing
2933 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002934 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002935 }
2936 break;
Damien429d7192013-10-04 19:53:11 +01002937 default: assert(0);
2938 }
2939 } else {
Damiend99b0522013-12-21 18:17:45 +00002940 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(set_line_number, pns->source_line);
Damien George5042bce2014-05-25 22:06:06 +01002942 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
Damien George40f3c022014-07-03 13:25:24 +01002943 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 +01002944 } else {
Damien George5042bce2014-05-25 22:06:06 +01002945 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2946 if (f == NULL) {
2947 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2948#if MICROPY_DEBUG_PRINTERS
2949 mp_parse_node_print(pn, 0);
2950#endif
2951 compile_syntax_error(comp, pn, "internal compiler error");
2952 } else {
2953 f(comp, pns);
2954 }
Damien429d7192013-10-04 19:53:11 +01002955 }
2956 }
2957}
2958
Damien George2ac4af62014-08-15 16:45:41 +01002959STATIC 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 +01002960 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01002961 qstr param_name = MP_QSTR_NULL;
2962 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002963 if (MP_PARSE_NODE_IS_ID(pn)) {
2964 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01002965 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002966 // comes after a star, so counts as a keyword-only parameter
2967 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01002968 } else {
Damien George2827d622014-04-27 15:50:52 +01002969 // comes before a star, so counts as a positional parameter
2970 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01002971 }
Damienb14de212013-10-06 00:28:28 +01002972 } else {
Damiend99b0522013-12-21 18:17:45 +00002973 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2974 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2975 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2976 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01002977 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01002978 // comes after a star, so counts as a keyword-only parameter
2979 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01002980 } else {
Damien George2827d622014-04-27 15:50:52 +01002981 // comes before a star, so counts as a positional parameter
2982 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01002983 }
Damiend99b0522013-12-21 18:17:45 +00002984 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01002985 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01002986 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00002987 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002988 // bare star
2989 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01002990 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002991 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002992 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002993 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002994 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01002995 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01002996 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00002997 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002998 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2999 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003000 } else {
3001 // shouldn't happen
3002 assert(0);
3003 }
Damiend99b0522013-12-21 18:17:45 +00003004 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3005 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003006 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003007 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003008 } else {
Damienb14de212013-10-06 00:28:28 +01003009 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003010 assert(0);
3011 }
Damien429d7192013-10-04 19:53:11 +01003012 }
3013
Damien George2827d622014-04-27 15:50:52 +01003014 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003015 bool added;
3016 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3017 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003018 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003019 return;
3020 }
Damien429d7192013-10-04 19:53:11 +01003021 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003022 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003023 }
3024}
3025
Damien George2827d622014-04-27 15:50:52 +01003026STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003027 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003028}
3029
Damien George2827d622014-04-27 15:50:52 +01003030STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003031 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3032}
3033
3034STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3035 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3036 // no annotation
3037 return;
3038 }
3039
3040 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3041 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3042 // named parameter with possible annotation
3043 // fallthrough
3044 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3045 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3046 // named star with possible annotation
3047 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3048 // fallthrough
3049 } else {
3050 // no annotation
3051 return;
3052 }
3053 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3054 // double star with possible annotation
3055 // fallthrough
3056 } else {
3057 // no annotation
3058 return;
3059 }
3060
3061 mp_parse_node_t pn_annotation = pns->nodes[1];
3062
3063 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3064 #if MICROPY_EMIT_NATIVE
3065 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3066 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3067 assert(id_info != NULL);
3068
3069 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3070 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3071 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3072 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3073 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003074 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003075 }
3076 }
3077 #endif // MICROPY_EMIT_NATIVE
3078 }
Damien429d7192013-10-04 19:53:11 +01003079}
3080
Damien George6be0b0a2014-08-15 14:30:52 +01003081STATIC 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 +01003082 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003083 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003084 // no more nested if/for; compile inner expression
3085 compile_node(comp, pn_inner_expr);
3086 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003087 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003088 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003089 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003090 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003091 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003092 } else {
3093 EMIT(yield_value);
3094 EMIT(pop_top);
3095 }
Damiend99b0522013-12-21 18:17:45 +00003096 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003097 // if condition
Damiend99b0522013-12-21 18:17:45 +00003098 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003099 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3100 pn_iter = pns_comp_if->nodes[1];
3101 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003102 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003103 // for loop
Damiend99b0522013-12-21 18:17:45 +00003104 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003105 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003106 uint l_end2 = comp_next_label(comp);
3107 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003108 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003109 EMIT_ARG(label_assign, l_top2);
3110 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003111 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3112 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 +00003113 EMIT_ARG(jump, l_top2);
3114 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003115 EMIT(for_iter_end);
3116 } else {
3117 // shouldn't happen
3118 assert(0);
3119 }
3120}
3121
Damien George1463c1f2014-04-25 23:52:57 +01003122STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3123#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003124 // see http://www.python.org/dev/peps/pep-0257/
3125
3126 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003127 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003128 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003129 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003130 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003131 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3132 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003133 for (int i = 0; i < num_nodes; i++) {
3134 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003135 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 +00003136 // not a newline, so this is the first statement; finish search
3137 break;
3138 }
3139 }
3140 // 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 +00003141 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003142 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003143 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003144 } else {
3145 return;
3146 }
3147
3148 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003149 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3150 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003151 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3152 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3153 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3154 // compile the doc string
3155 compile_node(comp, pns->nodes[0]);
3156 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003157 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003158 }
3159 }
Damien George1463c1f2014-04-25 23:52:57 +01003160#endif
Damien429d7192013-10-04 19:53:11 +01003161}
3162
Damien George1463c1f2014-04-25 23:52:57 +01003163STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003164 comp->pass = pass;
3165 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003166 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003167 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003168
Damien George36db6bc2014-05-07 17:24:22 +01003169 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003170 // reset maximum stack sizes in scope
3171 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003172 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003173 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003174 }
3175
Damien5ac1b2e2013-10-18 19:58:12 +01003176#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003177 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003178 scope_print_info(scope);
3179 }
Damien5ac1b2e2013-10-18 19:58:12 +01003180#endif
Damien429d7192013-10-04 19:53:11 +01003181
3182 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003183 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3184 assert(scope->kind == SCOPE_MODULE);
3185 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3186 compile_node(comp, pns->nodes[0]); // compile the expression
3187 EMIT(return_value);
3188 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003189 if (!comp->is_repl) {
3190 check_for_doc_string(comp, scope->pn);
3191 }
Damien429d7192013-10-04 19:53:11 +01003192 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003193 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003194 EMIT(return_value);
3195 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003196 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3197 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3198 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003199
3200 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003201 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003202 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003203 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003204 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003205 } else {
3206 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003207
Damien George2ac4af62014-08-15 16:45:41 +01003208 // argument annotations
3209 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3210
3211 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003212 mp_parse_node_t pn_annotation = pns->nodes[2];
3213 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3214 #if MICROPY_EMIT_NATIVE
3215 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3216 // nodes[2] can be null or a test-expr
3217 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3218 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3219 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3220 } else {
3221 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3222 }
Damien George2ac4af62014-08-15 16:45:41 +01003223 }
Damien Georgea5190a72014-08-15 22:39:08 +01003224 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003225 }
Damien George2ac4af62014-08-15 16:45:41 +01003226 }
Damien429d7192013-10-04 19:53:11 +01003227
3228 compile_node(comp, pns->nodes[3]); // 3 is function body
3229 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003230 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003231 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003232 EMIT(return_value);
3233 }
3234 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003235 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3236 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3237 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003238
3239 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003240 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003241 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003242 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003243 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3244 }
3245
3246 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003247
3248 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3249 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3250 EMIT(pop_top);
3251 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3252 }
Damien429d7192013-10-04 19:53:11 +01003253 EMIT(return_value);
3254 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3255 // a bit of a hack at the moment
3256
Damiend99b0522013-12-21 18:17:45 +00003257 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3258 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3259 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3260 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3261 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003262
Damien George708c0732014-04-27 19:23:46 +01003263 // We need a unique name for the comprehension argument (the iterator).
3264 // CPython uses .0, but we should be able to use anything that won't
3265 // clash with a user defined variable. Best to use an existing qstr,
3266 // so we use the blank qstr.
3267#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003268 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003269#else
3270 qstr qstr_arg = MP_QSTR_;
3271#endif
Damien George36db6bc2014-05-07 17:24:22 +01003272 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003273 bool added;
3274 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3275 assert(added);
3276 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003277 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003278 }
3279
3280 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003281 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003282 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003283 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01003284 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003285 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01003286 }
3287
Damien George6f355fd2014-04-10 14:11:31 +01003288 uint l_end = comp_next_label(comp);
3289 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003290 EMIT_ARG(load_id, qstr_arg);
3291 EMIT_ARG(label_assign, l_top);
3292 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003293 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3294 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003295 EMIT_ARG(jump, l_top);
3296 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003297 EMIT(for_iter_end);
3298
3299 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003300 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003301 }
3302 EMIT(return_value);
3303 } else {
3304 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003305 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3306 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3307 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003308
Damien George36db6bc2014-05-07 17:24:22 +01003309 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003310 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003311 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003312 assert(added);
3313 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003314 }
3315
Damien Georgeb9791222014-01-23 00:34:21 +00003316 EMIT_ARG(load_id, MP_QSTR___name__);
3317 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003318 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 +00003319 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003320
3321 check_for_doc_string(comp, pns->nodes[2]);
3322 compile_node(comp, pns->nodes[2]); // 2 is class body
3323
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003324 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003325 assert(id != NULL);
3326 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003327 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003328 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003329#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003330 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003331#else
Damien George2bf7c092014-04-09 15:26:46 +01003332 EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003333#endif
Damien429d7192013-10-04 19:53:11 +01003334 }
3335 EMIT(return_value);
3336 }
3337
Damien415eb6f2013-10-05 12:19:06 +01003338 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003339
3340 // make sure we match all the exception levels
3341 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003342}
3343
Damien George094d4502014-04-02 17:31:27 +01003344#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003345// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003346STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003347 comp->pass = pass;
3348 comp->scope_cur = scope;
3349 comp->next_label = 1;
3350
3351 if (scope->kind != SCOPE_FUNCTION) {
3352 printf("Error: inline assembler must be a function\n");
3353 return;
3354 }
3355
Damien George36db6bc2014-05-07 17:24:22 +01003356 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003357 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003358 }
3359
Damien826005c2013-10-05 23:17:28 +01003360 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003361 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3362 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3363 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003364
Damiend99b0522013-12-21 18:17:45 +00003365 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003366
Damiena2f2f7d2013-10-06 00:14:13 +01003367 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003368 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003369 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003370 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003371 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003372 }
3373
Damiend99b0522013-12-21 18:17:45 +00003374 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003375
Damiend99b0522013-12-21 18:17:45 +00003376 mp_parse_node_t pn_body = pns->nodes[3]; // body
3377 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003378 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3379
Damien Georgecbd2f742014-01-19 11:48:48 +00003380 /*
Damien George36db6bc2014-05-07 17:24:22 +01003381 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003382 //printf("----\n");
3383 scope_print_info(scope);
3384 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003385 */
Damien826005c2013-10-05 23:17:28 +01003386
3387 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003388 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3389 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003390 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3391 // no instructions
3392 continue;
3393 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3394 // an instruction; fall through
3395 } else {
3396 // not an instruction; error
3397 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3398 return;
3399 }
Damiend99b0522013-12-21 18:17:45 +00003400 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3401 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3402 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3403 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3404 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3405 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3406 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3407 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3408 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3409 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003410 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3411
3412 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003413 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003414 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003415 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003416 return;
3417 }
Damien George6f355fd2014-04-10 14:11:31 +01003418 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003419 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003420 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003421 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003422 } else if (op == MP_QSTR_align) {
3423 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3424 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3425 return;
3426 }
Damien George36db6bc2014-05-07 17:24:22 +01003427 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003428 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3429 }
3430 } else if (op == MP_QSTR_data) {
3431 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3432 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3433 return;
3434 }
Damien George36db6bc2014-05-07 17:24:22 +01003435 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003436 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien Georgee5f8a772014-04-21 13:33:15 +01003437 for (uint i = 1; i < n_args; i++) {
3438 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
3439 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3440 return;
3441 }
3442 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
3443 }
3444 }
Damien826005c2013-10-05 23:17:28 +01003445 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003446 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003447 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003448 }
3449 }
3450 }
3451
Damien George36db6bc2014-05-07 17:24:22 +01003452 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003453 bool success = EMIT_INLINE_ASM(end_pass);
3454 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003455 // TODO get proper exception from inline assembler
3456 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003457 }
Damienb05d7072013-10-05 13:37:10 +01003458 }
Damien429d7192013-10-04 19:53:11 +01003459}
Damien George094d4502014-04-02 17:31:27 +01003460#endif
Damien429d7192013-10-04 19:53:11 +01003461
Damien George1463c1f2014-04-25 23:52:57 +01003462STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003463#if !MICROPY_EMIT_CPYTHON
3464 // in Micro Python we put the *x parameter after all other parameters (except **y)
3465 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3466 id_info_t *id_param = NULL;
3467 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3468 id_info_t *id = &scope->id_info[i];
3469 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3470 if (id_param != NULL) {
3471 // swap star param with last param
3472 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3473 }
3474 break;
3475 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3476 id_param = id;
3477 }
3478 }
3479 }
3480#endif
3481
Damien429d7192013-10-04 19:53:11 +01003482 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003483 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003484 scope->num_locals = 0;
3485 for (int i = 0; i < scope->id_info_len; i++) {
3486 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003487 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003488 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3489 continue;
3490 }
3491 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3492 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3493 }
Damien George2827d622014-04-27 15:50:52 +01003494 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003495 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003496 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003497 }
3498 }
3499
3500 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003501#if MICROPY_EMIT_CPYTHON
3502 int num_cell = 0;
3503#endif
Damien9ecbcff2013-12-11 00:41:43 +00003504 for (int i = 0; i < scope->id_info_len; i++) {
3505 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003506#if MICROPY_EMIT_CPYTHON
3507 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003508 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003509 id->local_num = num_cell;
3510 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003511 }
Damien George6baf76e2013-12-30 22:32:17 +00003512#else
3513 // in Micro Python the cells come right after the fast locals
3514 // parameters are not counted here, since they remain at the start
3515 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003516 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003517 id->local_num = scope->num_locals;
3518 scope->num_locals += 1;
3519 }
3520#endif
Damien9ecbcff2013-12-11 00:41:43 +00003521 }
Damien9ecbcff2013-12-11 00:41:43 +00003522
3523 // compute the index of free vars (freevars[idx] in CPython)
3524 // make sure they are in the order of the parent scope
3525 if (scope->parent != NULL) {
3526 int num_free = 0;
3527 for (int i = 0; i < scope->parent->id_info_len; i++) {
3528 id_info_t *id = &scope->parent->id_info[i];
3529 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3530 for (int j = 0; j < scope->id_info_len; j++) {
3531 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003532 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003533 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003534#if MICROPY_EMIT_CPYTHON
3535 // in CPython the frees are numbered after the cells
3536 id2->local_num = num_cell + num_free;
3537#else
3538 // in Micro Python the frees come first, before the params
3539 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003540#endif
3541 num_free += 1;
3542 }
3543 }
3544 }
Damien429d7192013-10-04 19:53:11 +01003545 }
Damien George6baf76e2013-12-30 22:32:17 +00003546#if !MICROPY_EMIT_CPYTHON
3547 // in Micro Python shift all other locals after the free locals
3548 if (num_free > 0) {
3549 for (int i = 0; i < scope->id_info_len; i++) {
3550 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003551 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003552 id->local_num += num_free;
3553 }
3554 }
Damien George2827d622014-04-27 15:50:52 +01003555 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 +00003556 scope->num_locals += num_free;
3557 }
3558#endif
Damien429d7192013-10-04 19:53:11 +01003559 }
3560
Damien George8725f8f2014-02-15 19:33:11 +00003561 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003562
3563#if MICROPY_EMIT_CPYTHON
3564 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003565 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) {
3566 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003567 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003568 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003569 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 +00003570 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003571 }
3572 }
Damien George882b3632014-04-02 15:56:31 +01003573#endif
3574
Damien429d7192013-10-04 19:53:11 +01003575 int num_free = 0;
3576 for (int i = 0; i < scope->id_info_len; i++) {
3577 id_info_t *id = &scope->id_info[i];
3578 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3579 num_free += 1;
3580 }
3581 }
3582 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003583 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003584 }
3585}
3586
Damien George65cad122014-04-06 11:48:15 +01003587mp_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 +01003588 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003589 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003590 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003591 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003592
Damien826005c2013-10-05 23:17:28 +01003593 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003594 mp_map_t consts;
3595 mp_map_init(&consts, 0);
3596 pn = fold_constants(comp, pn, &consts);
3597 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003598
3599 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003600 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003601
Damien826005c2013-10-05 23:17:28 +01003602 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003603 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003604 comp->emit_method_table = &emit_pass1_method_table;
3605 comp->emit_inline_asm = NULL;
3606 comp->emit_inline_asm_method_table = NULL;
3607 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003608 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003609 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003610#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003611 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003612 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003613#endif
Damien826005c2013-10-05 23:17:28 +01003614 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003615 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003616 }
3617
3618 // update maximim number of labels needed
3619 if (comp->next_label > max_num_labels) {
3620 max_num_labels = comp->next_label;
3621 }
Damien429d7192013-10-04 19:53:11 +01003622 }
3623
Damien826005c2013-10-05 23:17:28 +01003624 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003625 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003626 compile_scope_compute_things(comp, s);
3627 }
3628
Damien826005c2013-10-05 23:17:28 +01003629 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003630 emit_pass1_free(comp->emit);
3631
Damien826005c2013-10-05 23:17:28 +01003632 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003633#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003634 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003635#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003636 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003637#endif
Damien3ef4abb2013-10-12 16:53:13 +01003638#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003639 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003640#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003641#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003642 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003643 if (false) {
3644 // dummy
3645
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) {
Damienc025ebb2013-10-12 14:30:21 +01003648 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003649 if (emit_inline_thumb == NULL) {
3650 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3651 }
3652 comp->emit = NULL;
3653 comp->emit_method_table = NULL;
3654 comp->emit_inline_asm = emit_inline_thumb;
3655 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003656 compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
Damien Georgea91ac202014-10-05 19:01:34 +01003657 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003658 compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003659 }
Damienc025ebb2013-10-12 14:30:21 +01003660#endif
3661
Damien826005c2013-10-05 23:17:28 +01003662 } else {
Damienc025ebb2013-10-12 14:30:21 +01003663
3664 // choose the emit type
3665
Damien3ef4abb2013-10-12 16:53:13 +01003666#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003667 comp->emit = emit_cpython_new(max_num_labels);
3668 comp->emit_method_table = &emit_cpython_method_table;
3669#else
Damien826005c2013-10-05 23:17:28 +01003670 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003671
3672#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003673 case MP_EMIT_OPT_NATIVE_PYTHON:
3674 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003675#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003676 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003677 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003678 }
Damien13ed3a62013-10-08 09:05:10 +01003679 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003680#elif MICROPY_EMIT_X86
3681 if (emit_native == NULL) {
3682 emit_native = emit_native_x86_new(max_num_labels);
3683 }
3684 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003685#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003686 if (emit_native == NULL) {
3687 emit_native = emit_native_thumb_new(max_num_labels);
3688 }
3689 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003690#elif MICROPY_EMIT_ARM
3691 if (emit_native == NULL) {
3692 emit_native = emit_native_arm_new(max_num_labels);
3693 }
3694 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003695#endif
3696 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003697 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 +01003698
3699 // native emitters need an extra pass to compute stack size
3700 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3701
Damien7af3d192013-10-07 00:02:49 +01003702 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003703#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003704
Damien826005c2013-10-05 23:17:28 +01003705 default:
3706 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003707 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003708 }
3709 comp->emit = emit_bc;
3710 comp->emit_method_table = &emit_bc_method_table;
3711 break;
3712 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003713#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003714
Damien George36db6bc2014-05-07 17:24:22 +01003715 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003716 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003717 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3718 }
3719
3720 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003721 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003722 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003723 }
Damien6cdd3af2013-10-05 18:08:26 +01003724 }
Damien429d7192013-10-04 19:53:11 +01003725 }
3726
Damien George41d02b62014-01-24 22:42:28 +00003727 // free the emitters
3728#if !MICROPY_EMIT_CPYTHON
3729 if (emit_bc != NULL) {
3730 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003731 }
Damien George41d02b62014-01-24 22:42:28 +00003732#if MICROPY_EMIT_NATIVE
3733 if (emit_native != NULL) {
3734#if MICROPY_EMIT_X64
3735 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003736#elif MICROPY_EMIT_X86
3737 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003738#elif MICROPY_EMIT_THUMB
3739 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003740#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003741 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003742#endif
3743 }
3744#endif
3745#if MICROPY_EMIT_INLINE_THUMB
3746 if (emit_inline_thumb != NULL) {
3747 emit_inline_thumb_free(emit_inline_thumb);
3748 }
3749#endif
3750#endif // !MICROPY_EMIT_CPYTHON
3751
Damien George52b5d762014-09-23 15:31:56 +00003752 // free the parse tree
3753 mp_parse_node_free(pn);
3754
Damien George41d02b62014-01-24 22:42:28 +00003755 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003756 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003757 for (scope_t *s = module_scope; s;) {
3758 scope_t *next = s->next;
3759 scope_free(s);
3760 s = next;
3761 }
Damien5ac1b2e2013-10-18 19:58:12 +01003762
Damien George41d02b62014-01-24 22:42:28 +00003763 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003764 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003765 m_del_obj(compiler_t, comp);
3766
Damien Georgea91ac202014-10-05 19:01:34 +01003767 if (compile_error != MP_OBJ_NULL) {
3768 return compile_error;
Damien George1fb03172014-01-03 14:22:03 +00003769 } else {
3770#if MICROPY_EMIT_CPYTHON
3771 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003772 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003773 return mp_const_true;
3774#else
3775 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003776 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003777#endif
3778 }
Damien429d7192013-10-04 19:53:11 +01003779}