blob: 7023e9c403f03b43241a3df73925c6bc8d25661e [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
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/scope.h"
35#include "py/emit.h"
36#include "py/compile.h"
37#include "py/smallint.h"
38#include "py/runtime.h"
39#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010040
41// TODO need to mangle __attr names
42
43typedef enum {
Damien George00208ce2014-01-23 00:00:53 +000044#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000045#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010046#undef DEF_RULE
47 PN_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010048 PN_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000049 PN_bytes, // special node for non-interned bytes
Damien429d7192013-10-04 19:53:11 +010050} pn_kind_t;
51
Damien Georgeb9791222014-01-23 00:34:21 +000052#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
53#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
54#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
55#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 +010056
Damien Georgea91ac202014-10-05 19:01:34 +010057// elements in this struct are ordered to make it compact
Damien429d7192013-10-04 19:53:11 +010058typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000059 qstr source_file;
Damien Georgea91ac202014-10-05 19:01:34 +010060
Damien George78035b92014-04-09 12:27:39 +010061 uint8_t is_repl;
62 uint8_t pass; // holds enum type pass_kind_t
Damien George78035b92014-04-09 12:27:39 +010063 uint8_t func_arg_is_super; // used to compile special case of super() function call
Damien Georgea91ac202014-10-05 19:01:34 +010064 uint8_t have_star;
65
66 // try to keep compiler clean from nlr
67 // this is set to an exception object if we have a compile error
68 mp_obj_t compile_error;
Damien429d7192013-10-04 19:53:11 +010069
Damien George6f355fd2014-04-10 14:11:31 +010070 uint next_label;
Damienb05d7072013-10-05 13:37:10 +010071
Damien George69b89d22014-04-11 13:38:30 +000072 uint16_t num_dict_params;
73 uint16_t num_default_params;
Damien429d7192013-10-04 19:53:11 +010074
Damien Georgea91ac202014-10-05 19:01:34 +010075 uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
76 uint16_t continue_label;
77 uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien George090c9232014-10-17 14:08:49 +000078 uint16_t break_continue_except_level;
Damien Georgea91ac202014-10-05 19:01:34 +010079
Damien429d7192013-10-04 19:53:11 +010080 scope_t *scope_head;
81 scope_t *scope_cur;
82
Damien6cdd3af2013-10-05 18:08:26 +010083 emit_t *emit; // current emitter
84 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010085
86 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
87 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 +010088} compiler_t;
89
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010090STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
Damien Georgea91ac202014-10-05 19:01:34 +010091 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
92 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010093 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damien Georgea91ac202014-10-05 19:01:34 +010094 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 +010095 } else {
Damien Georgea91ac202014-10-05 19:01:34 +010096 // we don't have a line number, so just pass 0
97 mp_obj_exception_add_traceback(exc, comp->source_file, 0, MP_QSTR_NULL);
Damien Georgeb7ffdcc2014-04-08 16:41:02 +010098 }
Damien Georgea91ac202014-10-05 19:01:34 +010099 comp->compile_error = exc;
Damien Georgef41fdd02014-03-03 23:19:11 +0000100}
101
Damien Georgeddd1e182015-01-10 14:07:24 +0000102#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100103STATIC const mp_map_elem_t mp_constants_table[] = {
Paul Sokolovsky82158472014-06-28 03:03:47 +0300104 #if MICROPY_PY_UCTYPES
105 { MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
106 #endif
Damien George57e99eb2014-04-10 22:42:11 +0100107 // Extra constants as defined by a port
Damien George58ebde42014-05-21 20:32:59 +0100108 MICROPY_PORT_CONSTANTS
Damien George57e99eb2014-04-10 22:42:11 +0100109};
Damien Georgeddd1e182015-01-10 14:07:24 +0000110STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
111#endif
Damien George57e99eb2014-04-10 22:42:11 +0100112
Damien Georgeffae48d2014-05-08 15:58:39 +0000113// this function is essentially a simple preprocessor
114STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
115 if (0) {
116 // dummy
Damien George58ebde42014-05-21 20:32:59 +0100117#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000118 } else if (MP_PARSE_NODE_IS_ID(pn)) {
119 // lookup identifier in table of dynamic constants
120 qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
121 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
122 if (elem != NULL) {
123 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
124 }
125#endif
126 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
Damiend99b0522013-12-21 18:17:45 +0000127 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100128
Damien Georgeffae48d2014-05-08 15:58:39 +0000129 // fold some parse nodes before folding their arguments
130 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien George58ebde42014-05-21 20:32:59 +0100131#if MICROPY_COMP_CONST
Damien Georgeffae48d2014-05-08 15:58:39 +0000132 case PN_expr_stmt:
133 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
134 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
135 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
136 if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
137 && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
138 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
139 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
140 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
141 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
142 ) {
143 // code to assign dynamic constants: id = const(value)
144
145 // get the id
146 qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
147
148 // get the value
149 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
150 pn_value = fold_constants(comp, pn_value, consts);
151 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
152 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
153 break;
154 }
Damien George40f3c022014-07-03 13:25:24 +0100155 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
Damien Georgeffae48d2014-05-08 15:58:39 +0000156
157 // store the value in the table of dynamic constants
158 mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
159 if (elem->value != MP_OBJ_NULL) {
160 compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
161 break;
162 }
163 elem->value = MP_OBJ_NEW_SMALL_INT(value);
164
165 // replace const(value) with value
166 pns1->nodes[0] = pn_value;
167
168 // finished folding this assignment
169 return pn;
170 }
171 }
172 }
173 break;
174#endif
Damien George5042bce2014-05-25 22:06:06 +0100175 case PN_string:
Damien George4c81ba82015-01-13 16:21:23 +0000176 case PN_bytes:
Damien George5042bce2014-05-25 22:06:06 +0100177 return pn;
Damien429d7192013-10-04 19:53:11 +0100178 }
179
Damien Georgeffae48d2014-05-08 15:58:39 +0000180 // fold arguments
181 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
182 for (int i = 0; i < n; i++) {
183 pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
184 }
185
186 // try to fold this parse node
Damiend99b0522013-12-21 18:17:45 +0000187 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100188 case PN_atom_paren:
189 if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
190 // (int)
191 pn = pns->nodes[0];
192 }
193 break;
194
195 case PN_expr:
196 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
197 // int | int
Damien George40f3c022014-07-03 13:25:24 +0100198 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
199 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100200 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
201 }
202 break;
203
204 case PN_and_expr:
205 if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
206 // int & int
Damien George40f3c022014-07-03 13:25:24 +0100207 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
208 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damien Georgea26dc502014-04-12 17:54:52 +0100209 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
210 }
211 break;
212
Damien429d7192013-10-04 19:53:11 +0100213 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +0000214 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 +0100215 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
216 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000217 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100218 // int << int
Damien George963a5a32015-01-16 17:47:07 +0000219 if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
Damien Georgea26dc502014-04-12 17:54:52 +0100220 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
221 }
Damiend99b0522013-12-21 18:17:45 +0000222 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100223 // int >> int
Damien George963a5a32015-01-16 17:47:07 +0000224 if (arg1 >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200225 // Shifting to big amounts is underfined behavior
226 // in C and is CPU-dependent; propagate sign bit.
227 arg1 = BITS_PER_WORD - 1;
228 }
Damiend99b0522013-12-21 18:17:45 +0000229 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100230 } else {
231 // shouldn't happen
232 assert(0);
233 }
234 }
235 break;
236
237 case PN_arith_expr:
Damien George40f3c022014-07-03 13:25:24 +0100238 // overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
Damiend99b0522013-12-21 18:17:45 +0000239 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 +0100240 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
241 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100243 // int + int
Damien Georgeaf272592014-04-04 11:21:58 +0000244 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000245 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100246 // int - int
Damien Georgeaf272592014-04-04 11:21:58 +0000247 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100248 } else {
249 // shouldn't happen
250 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100251 }
Damien Georged1e355e2014-05-28 14:51:12 +0100252 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100253 //printf("%ld + %ld\n", arg0, arg1);
Damien Georgeaf272592014-04-04 11:21:58 +0000254 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100255 }
256 }
257 break;
258
259 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000260 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 +0100261 mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
262 mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000263 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100264 // int * int
Damien Georgeaf272592014-04-04 11:21:58 +0000265 if (!mp_small_int_mul_overflow(arg0, arg1)) {
266 arg0 *= arg1;
Damien Georged1e355e2014-05-28 14:51:12 +0100267 if (MP_SMALL_INT_FITS(arg0)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000268 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
269 }
270 }
Damiend99b0522013-12-21 18:17:45 +0000271 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100272 // int / int
273 // pass
Damiend99b0522013-12-21 18:17:45 +0000274 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100275 // int%int
Damien Georgeecf5b772014-04-04 11:13:51 +0000276 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000277 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300278 if (arg1 != 0) {
Damien Georgea26dc502014-04-12 17:54:52 +0100279 // int // int
Damien Georgeecf5b772014-04-04 11:13:51 +0000280 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 +0300281 }
Damien429d7192013-10-04 19:53:11 +0100282 } else {
283 // shouldn't happen
284 assert(0);
285 }
286 }
287 break;
288
289 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000290 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Damien George40f3c022014-07-03 13:25:24 +0100291 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100293 // +int
Damiend99b0522013-12-21 18:17:45 +0000294 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
295 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100296 // -int
Damiend99b0522013-12-21 18:17:45 +0000297 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
298 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgea26dc502014-04-12 17:54:52 +0100299 // ~int
Damiend99b0522013-12-21 18:17:45 +0000300 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100301 } else {
302 // shouldn't happen
303 assert(0);
304 }
305 }
306 break;
307
308 case PN_power:
Damien George57e99eb2014-04-10 22:42:11 +0100309 if (0) {
310#if MICROPY_EMIT_CPYTHON
311 } 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 +0100312 // int ** x
Damien George57e99eb2014-04-10 22:42:11 +0100313 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000314 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
315 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200316 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100317 if (power >= 0) {
318 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200319 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100320 for (; power > 0; power--) {
321 ans *= base;
322 }
Damiend99b0522013-12-21 18:17:45 +0000323 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100324 }
325 }
Damien George57e99eb2014-04-10 22:42:11 +0100326#endif
Damien Georgeddd1e182015-01-10 14:07:24 +0000327#if MICROPY_COMP_MODULE_CONST
Damien George57e99eb2014-04-10 22:42:11 +0100328 } 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])) {
329 // id.id
330 // look it up in constant table, see if it can be replaced with an integer
331 mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
332 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
333 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
334 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
335 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
336 if (elem != NULL) {
337 mp_obj_t dest[2];
338 mp_load_method_maybe(elem->value, q_attr, dest);
339 if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
Damien George40f3c022014-07-03 13:25:24 +0100340 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
Damien Georgeddd1e182015-01-10 14:07:24 +0000341 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
Damien George57e99eb2014-04-10 22:42:11 +0100342 }
343 }
Damien Georgeddd1e182015-01-10 14:07:24 +0000344#endif
Damien429d7192013-10-04 19:53:11 +0100345 }
346 break;
347 }
348 }
349
350 return pn;
351}
352
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200353STATIC 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 +0100354STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
Damien George8dcc0c72014-03-27 10:55:21 +0000355STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100356
Damien George6f355fd2014-04-10 14:11:31 +0100357STATIC uint comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100358 return comp->next_label++;
359}
360
Damien George8dcc0c72014-03-27 10:55:21 +0000361STATIC void compile_increase_except_level(compiler_t *comp) {
362 comp->cur_except_level += 1;
363 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
364 comp->scope_cur->exc_stack_size = comp->cur_except_level;
365 }
366}
367
368STATIC void compile_decrease_except_level(compiler_t *comp) {
369 assert(comp->cur_except_level > 0);
370 comp->cur_except_level -= 1;
371}
372
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200373STATIC 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 +0100374 scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
Damien429d7192013-10-04 19:53:11 +0100375 scope->parent = comp->scope_cur;
376 scope->next = NULL;
377 if (comp->scope_head == NULL) {
378 comp->scope_head = scope;
379 } else {
380 scope_t *s = comp->scope_head;
381 while (s->next != NULL) {
382 s = s->next;
383 }
384 s->next = scope;
385 }
386 return scope;
387}
388
Damien George963a5a32015-01-16 17:47:07 +0000389STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
390 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) {
Damiend99b0522013-12-21 18:17:45 +0000391 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
392 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100393 for (int i = 0; i < num_nodes; i++) {
394 f(comp, pns->nodes[i]);
395 }
Damiend99b0522013-12-21 18:17:45 +0000396 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100397 f(comp, pn);
398 }
399}
400
Damien George963a5a32015-01-16 17:47:07 +0000401STATIC int list_get(mp_parse_node_t *pn, pn_kind_t pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000402 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100403 *nodes = NULL;
404 return 0;
Damiend99b0522013-12-21 18:17:45 +0000405 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100406 *nodes = pn;
407 return 1;
408 } else {
Damiend99b0522013-12-21 18:17:45 +0000409 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
410 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100411 *nodes = pn;
412 return 1;
413 } else {
414 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000415 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100416 }
417 }
418}
419
Damien George969a6b32014-12-10 22:07:04 +0000420STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +0000421 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100422 for (int i = 0; i < num_nodes; i++) {
423 compile_node(comp, pns->nodes[i]);
424 }
425}
426
Damien3ef4abb2013-10-12 16:53:13 +0100427#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200428STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damien George5042bce2014-05-25 22:06:06 +0100429 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
430 return true;
431 }
Damien George4c81ba82015-01-13 16:21:23 +0000432 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
433 return true;
434 }
Damiend99b0522013-12-21 18:17:45 +0000435 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100436 return false;
437 }
Damiend99b0522013-12-21 18:17:45 +0000438 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100439 return false;
440 }
441 return true;
442}
443
Damien George5042bce2014-05-25 22:06:06 +0100444STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
Damien02f89412013-12-12 15:13:36 +0000445 bool has_single_quote = false;
446 bool has_double_quote = false;
447 for (int i = 0; i < len; i++) {
448 if (str[i] == '\'') {
449 has_single_quote = true;
450 } else if (str[i] == '"') {
451 has_double_quote = true;
452 }
453 }
454 if (bytes) {
455 vstr_printf(vstr, "b");
456 }
457 bool quote_single = false;
458 if (has_single_quote && !has_double_quote) {
459 vstr_printf(vstr, "\"");
460 } else {
461 quote_single = true;
462 vstr_printf(vstr, "'");
463 }
464 for (int i = 0; i < len; i++) {
465 if (str[i] == '\n') {
466 vstr_printf(vstr, "\\n");
467 } else if (str[i] == '\\') {
468 vstr_printf(vstr, "\\\\");
469 } else if (str[i] == '\'' && quote_single) {
470 vstr_printf(vstr, "\\'");
471 } else {
472 vstr_printf(vstr, "%c", str[i]);
473 }
474 }
475 if (has_single_quote && !has_double_quote) {
476 vstr_printf(vstr, "\"");
477 } else {
478 vstr_printf(vstr, "'");
479 }
480}
481
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200482STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damien George4c81ba82015-01-13 16:21:23 +0000483 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string) || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
Damien George5042bce2014-05-25 22:06:06 +0100484 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George4c81ba82015-01-13 16:21:23 +0000485 cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes));
Damien George5042bce2014-05-25 22:06:06 +0100486 return;
487 }
488
Damiend99b0522013-12-21 18:17:45 +0000489 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200490 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
491 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
492 return;
493 }
494
Damien George42f3de92014-10-03 17:44:14 +0000495 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000496 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
497 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000498 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
499 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
Damien George5042bce2014-05-25 22:06:06 +0100500 case MP_PARSE_NODE_STRING:
501 case MP_PARSE_NODE_BYTES: {
Damien George00be7a82014-10-03 20:05:44 +0100502 mp_uint_t len;
Damien George5042bce2014-05-25 22:06:06 +0100503 const byte *str = qstr_data(arg, &len);
504 cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
505 break;
506 }
Damiend99b0522013-12-21 18:17:45 +0000507 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100508 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000509 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
510 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
511 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100512 default: assert(0); // shouldn't happen
Damien429d7192013-10-04 19:53:11 +0100513 }
514 break;
515 default: assert(0);
516 }
517}
518
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200519STATIC 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 +0100520 int n = 0;
521 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000522 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100523 }
524 int total = n;
525 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000526 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100527 total += 1;
Damien3a205172013-10-12 15:01:56 +0100528 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100529 is_const = false;
530 }
531 }
532 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100533 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100534 is_const = false;
535 break;
536 }
537 }
538 if (total > 0 && is_const) {
539 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000540 vstr_t *vstr = vstr_new();
541 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000542 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000543 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100544 need_comma = true;
545 }
546 for (int i = 0; i < n; i++) {
547 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000548 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100549 }
Damien02f89412013-12-12 15:13:36 +0000550 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100551 need_comma = true;
552 }
553 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000554 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100555 } else {
Damien02f89412013-12-12 15:13:36 +0000556 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100557 }
Damien George0d3cb672015-01-28 23:43:01 +0000558 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +0000559 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100560 } else {
Damiend99b0522013-12-21 18:17:45 +0000561 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100562 compile_node(comp, pn);
563 }
564 for (int i = 0; i < n; i++) {
565 compile_node(comp, pns_list->nodes[i]);
566 }
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100568 }
569}
Damien3a205172013-10-12 15:01:56 +0100570#endif
571
572// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damien George2c0842b2014-04-27 16:46:51 +0100573STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100574#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100575 cpython_c_tuple(comp, pn, pns_list);
576#else
577 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000578 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100579 compile_node(comp, pn);
580 total += 1;
581 }
582 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000583 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100584 for (int i = 0; i < n; i++) {
585 compile_node(comp, pns_list->nodes[i]);
586 }
587 total += n;
588 }
Damien Georgeb9791222014-01-23 00:34:21 +0000589 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100590#endif
591}
Damien429d7192013-10-04 19:53:11 +0100592
Damien George969a6b32014-12-10 22:07:04 +0000593STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100594 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000595 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100596}
597
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200598STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000599 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
600 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100601}
602
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200603STATIC bool node_is_const_true(mp_parse_node_t pn) {
Damien George391db862014-10-17 17:57:33 +0000604 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
605 || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
Damien429d7192013-10-04 19:53:11 +0100606}
607
Damien3ef4abb2013-10-12 16:53:13 +0100608#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100609// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200610STATIC 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 +0100611 if (node_is_const_false(pn)) {
612 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000613 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100614 }
615 return;
616 } else if (node_is_const_true(pn)) {
617 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000618 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100619 }
620 return;
Damiend99b0522013-12-21 18:17:45 +0000621 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
622 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
623 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
624 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100625 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100626 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100627 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100628 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100629 }
Damien3a205172013-10-12 15:01:56 +0100630 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000631 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100632 } else {
633 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100634 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100635 }
636 }
637 return;
Damiend99b0522013-12-21 18:17:45 +0000638 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100639 if (jump_if == false) {
640 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100641 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100642 }
643 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100644 uint label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100645 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100646 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100647 }
Damien3a205172013-10-12 15:01:56 +0100648 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000649 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100650 }
651 return;
Damiend99b0522013-12-21 18:17:45 +0000652 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100653 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100654 return;
655 }
656 }
657
658 // nothing special, fall back to default compiling for node and jump
659 compile_node(comp, pn);
660 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000661 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100662 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000663 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100664 }
665}
Damien3a205172013-10-12 15:01:56 +0100666#endif
Damien429d7192013-10-04 19:53:11 +0100667
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200668STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100669#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100670 cpython_c_if_cond(comp, pn, jump_if, label, false);
671#else
672 if (node_is_const_false(pn)) {
673 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000674 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100675 }
676 return;
677 } else if (node_is_const_true(pn)) {
678 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000679 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100680 }
681 return;
Damiend99b0522013-12-21 18:17:45 +0000682 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
683 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
684 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
685 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100686 if (jump_if == false) {
Damien George6f355fd2014-04-10 14:11:31 +0100687 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100688 for (int i = 0; i < n - 1; i++) {
689 c_if_cond(comp, pns->nodes[i], true, label2);
690 }
691 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000692 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100693 } else {
694 for (int i = 0; i < n; i++) {
695 c_if_cond(comp, pns->nodes[i], true, label);
696 }
697 }
698 return;
Damiend99b0522013-12-21 18:17:45 +0000699 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100700 if (jump_if == false) {
701 for (int i = 0; i < n; i++) {
702 c_if_cond(comp, pns->nodes[i], false, label);
703 }
704 } else {
Damien George6f355fd2014-04-10 14:11:31 +0100705 uint label2 = comp_next_label(comp);
Damien3a205172013-10-12 15:01:56 +0100706 for (int i = 0; i < n - 1; i++) {
707 c_if_cond(comp, pns->nodes[i], false, label2);
708 }
709 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000710 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100711 }
712 return;
Damiend99b0522013-12-21 18:17:45 +0000713 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100714 c_if_cond(comp, pns->nodes[0], !jump_if, label);
715 return;
Damien Georgeeb4e18f2014-08-29 20:04:01 +0100716 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
717 // cond is something in parenthesis
718 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
719 // empty tuple, acts as false for the condition
720 if (jump_if == false) {
721 EMIT_ARG(jump, label);
722 }
723 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
724 // non-empty tuple, acts as true for the condition
725 if (jump_if == true) {
726 EMIT_ARG(jump, label);
727 }
728 } else {
729 // parenthesis around 1 item, is just that item
730 c_if_cond(comp, pns->nodes[0], jump_if, label);
731 }
732 return;
Damien3a205172013-10-12 15:01:56 +0100733 }
734 }
735
736 // nothing special, fall back to default compiling for node and jump
737 compile_node(comp, pn);
738 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000739 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100740 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000741 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100742 }
743#endif
Damien429d7192013-10-04 19:53:11 +0100744}
745
746typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damien George6be0b0a2014-08-15 14:30:52 +0100747STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100748
Damien George6be0b0a2014-08-15 14:30:52 +0100749STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100750 if (assign_kind != ASSIGN_AUG_STORE) {
751 compile_node(comp, pns->nodes[0]);
752 }
753
Damiend99b0522013-12-21 18:17:45 +0000754 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
755 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
756 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
757 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100758 if (assign_kind != ASSIGN_AUG_STORE) {
759 for (int i = 0; i < n - 1; i++) {
760 compile_node(comp, pns1->nodes[i]);
761 }
762 }
Damiend99b0522013-12-21 18:17:45 +0000763 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
764 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100765 }
Damiend99b0522013-12-21 18:17:45 +0000766 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100767 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000768 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100769 if (assign_kind == ASSIGN_AUG_STORE) {
770 EMIT(rot_three);
771 EMIT(store_subscr);
772 } else {
773 compile_node(comp, pns1->nodes[0]);
774 if (assign_kind == ASSIGN_AUG_LOAD) {
775 EMIT(dup_top_two);
Damien George729f7b42014-04-17 22:10:53 +0100776 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +0100777 } else {
778 EMIT(store_subscr);
779 }
780 }
Damiend99b0522013-12-21 18:17:45 +0000781 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
782 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100783 if (assign_kind == ASSIGN_AUG_LOAD) {
784 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000785 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100786 } else {
787 if (assign_kind == ASSIGN_AUG_STORE) {
788 EMIT(rot_two);
789 }
Damien Georgeb9791222014-01-23 00:34:21 +0000790 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100791 }
792 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100793 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100794 }
795 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100796 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100797 }
798
Damiend99b0522013-12-21 18:17:45 +0000799 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100800 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100801 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100802
803 return;
804
805cannot_assign:
806 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
Damien429d7192013-10-04 19:53:11 +0100807}
808
Damien George0288cf02014-04-11 11:53:00 +0000809// 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 +0100810STATIC 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 +0000811 uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
812
813 // look for star expression
Damien George963a5a32015-01-16 17:47:07 +0000814 uint have_star_index = -1;
Damien George0288cf02014-04-11 11:53:00 +0000815 if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
816 EMIT_ARG(unpack_ex, 0, num_tail);
817 have_star_index = 0;
818 }
Damien George963a5a32015-01-16 17:47:07 +0000819 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000820 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
Damien George963a5a32015-01-16 17:47:07 +0000821 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000822 EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
823 have_star_index = num_head + i;
Damien429d7192013-10-04 19:53:11 +0100824 } else {
Damien George9d181f62014-04-27 16:55:27 +0100825 compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
Damien429d7192013-10-04 19:53:11 +0100826 return;
827 }
828 }
829 }
Damien George963a5a32015-01-16 17:47:07 +0000830 if (have_star_index == (uint)-1) {
Damien George0288cf02014-04-11 11:53:00 +0000831 EMIT_ARG(unpack_sequence, num_head + num_tail);
Damien429d7192013-10-04 19:53:11 +0100832 }
Damien George0288cf02014-04-11 11:53:00 +0000833 if (num_head != 0) {
834 if (0 == have_star_index) {
835 c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100836 } else {
Damien George0288cf02014-04-11 11:53:00 +0000837 c_assign(comp, node_head, ASSIGN_STORE);
838 }
839 }
Damien George963a5a32015-01-16 17:47:07 +0000840 for (uint i = 0; i < num_tail; i++) {
Damien George0288cf02014-04-11 11:53:00 +0000841 if (num_head + i == have_star_index) {
842 c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
843 } else {
844 c_assign(comp, nodes_tail[i], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100845 }
846 }
847}
848
849// assigns top of stack to pn
Damien George6be0b0a2014-08-15 14:30:52 +0100850STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100851 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000852 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100853 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000854 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
855 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien George42f3de92014-10-03 17:44:14 +0000856 qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100857 switch (assign_kind) {
858 case ASSIGN_STORE:
859 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000860 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100861 break;
862 case ASSIGN_AUG_LOAD:
Damien Georged2d64f02015-01-14 21:32:42 +0000863 default:
Damien Georgeb9791222014-01-23 00:34:21 +0000864 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100865 break;
866 }
867 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100868 compile_syntax_error(comp, pn, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100869 return;
870 }
871 } else {
Damiend99b0522013-12-21 18:17:45 +0000872 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
873 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100874 case PN_power:
875 // lhs is an index or attribute
876 c_assign_power(comp, pns, assign_kind);
877 break;
878
879 case PN_testlist_star_expr:
880 case PN_exprlist:
881 // lhs is a tuple
882 if (assign_kind != ASSIGN_STORE) {
883 goto bad_aug;
884 }
Damien George0288cf02014-04-11 11:53:00 +0000885 c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100886 break;
887
888 case PN_atom_paren:
889 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000890 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100891 // empty tuple
Damien George9d181f62014-04-27 16:55:27 +0100892 goto cannot_assign;
Damiend99b0522013-12-21 18:17:45 +0000893 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
894 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100895 goto testlist_comp;
896 } else {
897 // parenthesis around 1 item, is just that item
898 pn = pns->nodes[0];
899 goto tail_recursion;
900 }
901 break;
902
903 case PN_atom_bracket:
904 // lhs is something in brackets
905 if (assign_kind != ASSIGN_STORE) {
906 goto bad_aug;
907 }
Damiend99b0522013-12-21 18:17:45 +0000908 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100909 // empty list, assignment allowed
Damien George0288cf02014-04-11 11:53:00 +0000910 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000911 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
912 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100913 goto testlist_comp;
914 } else {
915 // brackets around 1 item
Damien George0288cf02014-04-11 11:53:00 +0000916 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damien429d7192013-10-04 19:53:11 +0100917 }
918 break;
919
920 default:
Damien George9d181f62014-04-27 16:55:27 +0100921 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100922 }
923 return;
924
925 testlist_comp:
926 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000927 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
928 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
929 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100930 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000931 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien George0288cf02014-04-11 11:53:00 +0000932 c_assign_tuple(comp, pns->nodes[0], 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000933 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100934 // sequence of many items
Damien George0288cf02014-04-11 11:53:00 +0000935 uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
936 c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
Damiend99b0522013-12-21 18:17:45 +0000937 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100938 // TODO can we ever get here? can it be compiled?
Damien George9d181f62014-04-27 16:55:27 +0100939 goto cannot_assign;
Damien429d7192013-10-04 19:53:11 +0100940 } else {
941 // sequence with 2 items
942 goto sequence_with_2_items;
943 }
944 } else {
945 // sequence with 2 items
946 sequence_with_2_items:
Damien George0288cf02014-04-11 11:53:00 +0000947 c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100948 }
949 return;
950 }
951 return;
952
Damien George9d181f62014-04-27 16:55:27 +0100953 cannot_assign:
954 compile_syntax_error(comp, pn, "can't assign to expression");
955 return;
956
Damien429d7192013-10-04 19:53:11 +0100957 bad_aug:
Damien Georgeb7ffdcc2014-04-08 16:41:02 +0100958 compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100959}
960
961// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100962// if we are not in CPython compatibility mode then:
963// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
964// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
965// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George6be0b0a2014-08-15 14:30:52 +0100966STATIC 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 +0100967 assert(n_pos_defaults >= 0);
968 assert(n_kw_defaults >= 0);
969
Damien429d7192013-10-04 19:53:11 +0100970 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000971 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100972 int nfree = 0;
973 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000974 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
975 id_info_t *id = &comp->scope_cur->id_info[i];
976 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
977 for (int j = 0; j < this_scope->id_info_len; j++) {
978 id_info_t *id2 = &this_scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +0100979 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George6baf76e2013-12-30 22:32:17 +0000980#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +0100981 EMIT_ARG(load_closure, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000982#else
983 // in Micro Python we load closures using LOAD_FAST
Damien George0abb5602015-01-16 12:24:49 +0000984 EMIT_ARG(load_fast, id->qst, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000985#endif
Damien318aec62013-12-10 18:28:17 +0000986 nfree += 1;
987 }
988 }
Damien429d7192013-10-04 19:53:11 +0100989 }
990 }
991 }
Damien429d7192013-10-04 19:53:11 +0100992
993 // make the function/closure
994 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100995 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100996 } else {
Damien George3558f622014-04-20 17:50:40 +0100997 EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100998 }
999}
1000
Damien George6be0b0a2014-08-15 14:30:52 +01001001STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001002 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001003 comp->have_star = true;
1004 /* don't need to distinguish bare from named star
Damiend99b0522013-12-21 18:17:45 +00001005 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1006 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001007 // bare star
Damien George8b19db02014-04-11 23:25:34 +01001008 } else {
1009 // named star
Damien429d7192013-10-04 19:53:11 +01001010 }
Damien George8b19db02014-04-11 23:25:34 +01001011 */
Damien Georgef41fdd02014-03-03 23:19:11 +00001012
1013 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
Damien George8b19db02014-04-11 23:25:34 +01001014 // named double star
Damien Georgef41fdd02014-03-03 23:19:11 +00001015 // TODO do we need to do anything with this?
1016
1017 } else {
1018 mp_parse_node_t pn_id;
1019 mp_parse_node_t pn_colon;
1020 mp_parse_node_t pn_equal;
1021 if (MP_PARSE_NODE_IS_ID(pn)) {
1022 // this parameter is just an id
1023
1024 pn_id = pn;
1025 pn_colon = MP_PARSE_NODE_NULL;
1026 pn_equal = MP_PARSE_NODE_NULL;
1027
1028 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1029 // this parameter has a colon and/or equal specifier
1030
1031 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1032 pn_id = pns->nodes[0];
1033 pn_colon = pns->nodes[1];
1034 pn_equal = pns->nodes[2];
1035
1036 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001037 // XXX what to do here?
Damien Georgef41fdd02014-03-03 23:19:11 +00001038 assert(0);
1039 return;
1040 }
1041
1042 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
1043 // this parameter does not have a default value
1044
1045 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
Damien George8b19db02014-04-11 23:25:34 +01001046 if (!comp->have_star && comp->num_default_params != 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001047 compile_syntax_error(comp, pn, "non-default argument follows default argument");
Damien Georgef41fdd02014-03-03 23:19:11 +00001048 return;
1049 }
1050
1051 } else {
1052 // this parameter has a default value
1053 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
1054
Damien George8b19db02014-04-11 23:25:34 +01001055 if (comp->have_star) {
Damien George69b89d22014-04-11 13:38:30 +00001056 comp->num_dict_params += 1;
Damien Georgef0778a72014-06-07 22:01:00 +01001057#if MICROPY_EMIT_CPYTHON
1058 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
1059 compile_node(comp, pn_equal);
1060#else
Damien George69b89d22014-04-11 13:38:30 +00001061 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
1062 if (comp->num_dict_params == 1) {
Damien George7b433012014-04-12 00:05:49 +01001063 // in Micro Python we put the default positional parameters into a tuple using the bytecode
1064 // we need to do this here before we start building the map for the default keywords
1065 if (comp->num_default_params > 0) {
1066 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001067 } else {
1068 EMIT(load_null); // sentinel indicating empty default positional args
Damien George7b433012014-04-12 00:05:49 +01001069 }
Damien George69b89d22014-04-11 13:38:30 +00001070 // first default dict param, so make the map
1071 EMIT_ARG(build_map, 0);
Damien Georgef41fdd02014-03-03 23:19:11 +00001072 }
Damien Georgef0778a72014-06-07 22:01:00 +01001073
1074 // compile value then key, then store it to the dict
Damien George69b89d22014-04-11 13:38:30 +00001075 compile_node(comp, pn_equal);
Damien Georgef0778a72014-06-07 22:01:00 +01001076 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
Damien George69b89d22014-04-11 13:38:30 +00001077 EMIT(store_map);
1078#endif
Damien Georgef41fdd02014-03-03 23:19:11 +00001079 } else {
Damien George69b89d22014-04-11 13:38:30 +00001080 comp->num_default_params += 1;
1081 compile_node(comp, pn_equal);
Damien Georgef41fdd02014-03-03 23:19:11 +00001082 }
1083 }
1084
1085 // TODO pn_colon not implemented
1086 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +01001087 }
1088}
1089
1090// leaves function object on stack
1091// returns function name
Damien George969a6b32014-12-10 22:07:04 +00001092STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001093 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001094 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +00001095 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001096 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001097 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001098 }
1099
1100 // save variables (probably don't need to do this, since we can't have nested definitions..?)
Damien George8b19db02014-04-11 23:25:34 +01001101 uint old_have_star = comp->have_star;
Damien George69b89d22014-04-11 13:38:30 +00001102 uint old_num_dict_params = comp->num_dict_params;
1103 uint old_num_default_params = comp->num_default_params;
Damien429d7192013-10-04 19:53:11 +01001104
1105 // compile default parameters
Damien George8b19db02014-04-11 23:25:34 +01001106 comp->have_star = false;
Damien George69b89d22014-04-11 13:38:30 +00001107 comp->num_dict_params = 0;
1108 comp->num_default_params = 0;
Damien429d7192013-10-04 19:53:11 +01001109 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +00001110
Damien Georgea91ac202014-10-05 19:01:34 +01001111 if (comp->compile_error != MP_OBJ_NULL) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001112 return MP_QSTR_NULL;
1113 }
1114
Damien Georgee337f1e2014-03-31 15:18:37 +01001115#if !MICROPY_EMIT_CPYTHON
1116 // in Micro Python we put the default positional parameters into a tuple using the bytecode
Damien George7b433012014-04-12 00:05:49 +01001117 // the default keywords args may have already made the tuple; if not, do it now
1118 if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
Damien George69b89d22014-04-11 13:38:30 +00001119 EMIT_ARG(build_tuple, comp->num_default_params);
Damien George3558f622014-04-20 17:50:40 +01001120 EMIT(load_null); // sentinel indicating empty default keyword args
Damien Georgee337f1e2014-03-31 15:18:37 +01001121 }
1122#endif
1123
Damien429d7192013-10-04 19:53:11 +01001124 // get the scope for this function
1125 scope_t *fscope = (scope_t*)pns->nodes[4];
1126
1127 // make the function
Damien George69b89d22014-04-11 13:38:30 +00001128 close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
Damien429d7192013-10-04 19:53:11 +01001129
1130 // restore variables
Damien George8b19db02014-04-11 23:25:34 +01001131 comp->have_star = old_have_star;
Damien George69b89d22014-04-11 13:38:30 +00001132 comp->num_dict_params = old_num_dict_params;
1133 comp->num_default_params = old_num_default_params;
Damien429d7192013-10-04 19:53:11 +01001134
1135 // return its name (the 'f' in "def f(...):")
1136 return fscope->simple_name;
1137}
1138
1139// leaves class object on stack
1140// returns class name
Damien George969a6b32014-12-10 22:07:04 +00001141STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien George36db6bc2014-05-07 17:24:22 +01001142 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01001143 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +00001144 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +01001145 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001146 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001147 }
1148
1149 EMIT(load_build_class);
1150
1151 // scope for this class
1152 scope_t *cscope = (scope_t*)pns->nodes[3];
1153
1154 // compile the class
1155 close_over_variables_etc(comp, cscope, 0, 0);
1156
1157 // get its name
Damien George968bf342014-04-27 19:12:05 +01001158 EMIT_ARG(load_const_str, cscope->simple_name, false);
Damien429d7192013-10-04 19:53:11 +01001159
1160 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +01001161 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
1162 mp_parse_node_t parents = pns->nodes[1];
1163 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
1164 parents = MP_PARSE_NODE_NULL;
1165 }
Damien Georgebbcd49a2014-02-06 20:30:16 +00001166 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +01001167 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +01001168
1169 // return its name (the 'C' in class C(...):")
1170 return cscope->simple_name;
1171}
1172
Damien6cdd3af2013-10-05 18:08:26 +01001173// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001174STATIC 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 +00001175 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +01001176 return false;
1177 }
1178
1179 if (name_len != 2) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001180 compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001181 return true;
1182 }
1183
Damiend99b0522013-12-21 18:17:45 +00001184 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien George3417bc22014-05-10 10:36:38 +01001185 if (attr == MP_QSTR_bytecode) {
Damien George96f137b2014-05-12 22:35:37 +01001186 *emit_options = MP_EMIT_OPT_BYTECODE;
Damience89a212013-10-15 22:25:17 +01001187#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001188 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001189 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001190 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001191 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001192#endif
Damien3ef4abb2013-10-12 16:53:13 +01001193#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001194 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001195 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001196#endif
Damien6cdd3af2013-10-05 18:08:26 +01001197 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001198 compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001199 }
1200
1201 return true;
1202}
1203
Damien George969a6b32014-12-10 22:07:04 +00001204STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001205 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001206 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001207 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1208
Damien6cdd3af2013-10-05 18:08:26 +01001209 // inherit emit options for this function/class definition
1210 uint emit_options = comp->scope_cur->emit_options;
1211
1212 // compile each decorator
1213 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001214 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001215 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1216 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001217
1218 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001219 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001220 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1221
1222 // check for built-in decorators
1223 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1224 // this was a built-in
1225 num_built_in_decorators += 1;
1226
1227 } else {
1228 // not a built-in, compile normally
1229
1230 // compile the decorator function
1231 compile_node(comp, name_nodes[0]);
Damien George50912e72015-01-20 11:55:10 +00001232 for (int j = 1; j < name_len; j++) {
1233 assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
1234 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]));
Damien6cdd3af2013-10-05 18:08:26 +01001235 }
1236
1237 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001238 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001239 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001240 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001241 compile_node(comp, pns_decorator->nodes[1]);
1242 }
Damien429d7192013-10-04 19:53:11 +01001243 }
1244 }
1245
1246 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001247 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001248 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001249 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001250 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001251 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001252 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001253 } else {
1254 // shouldn't happen
1255 assert(0);
1256 }
1257
1258 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001259 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien George922ddd62014-04-09 12:43:17 +01001260 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001261 }
1262
1263 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001264 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001265}
1266
Damien George969a6b32014-12-10 22:07:04 +00001267STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001268 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001269 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001270 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001271}
1272
Damien George6be0b0a2014-08-15 14:30:52 +01001273STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00001274 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001275 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001276 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1277 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001278
1279 compile_node(comp, pns->nodes[0]); // base of the power node
1280
Damiend99b0522013-12-21 18:17:45 +00001281 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1282 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1283 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1284 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001285 for (int i = 0; i < n - 1; i++) {
1286 compile_node(comp, pns1->nodes[i]);
1287 }
Damiend99b0522013-12-21 18:17:45 +00001288 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1289 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001290 }
Damiend99b0522013-12-21 18:17:45 +00001291 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001292 // can't delete function calls
1293 goto cannot_delete;
Damiend99b0522013-12-21 18:17:45 +00001294 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001295 compile_node(comp, pns1->nodes[0]);
1296 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001297 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1298 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001299 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001300 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001301 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001302 }
1303 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001304 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001305 }
1306
Damiend99b0522013-12-21 18:17:45 +00001307 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001308 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001309 }
Damiend99b0522013-12-21 18:17:45 +00001310 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1311 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1312 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1313 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001314 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1315
Damiend99b0522013-12-21 18:17:45 +00001316 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1317 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1318 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001319 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001320 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001321 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001322 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001323 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001324 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001325 c_del_stmt(comp, pns->nodes[0]);
1326 for (int i = 0; i < n; i++) {
1327 c_del_stmt(comp, pns1->nodes[i]);
1328 }
Damiend99b0522013-12-21 18:17:45 +00001329 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001330 // TODO not implemented; can't del comprehension?
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001331 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001332 } else {
1333 // sequence with 2 items
1334 goto sequence_with_2_items;
1335 }
1336 } else {
1337 // sequence with 2 items
1338 sequence_with_2_items:
1339 c_del_stmt(comp, pns->nodes[0]);
1340 c_del_stmt(comp, pns->nodes[1]);
1341 }
1342 } else {
1343 // tuple with 1 element
1344 c_del_stmt(comp, pn);
1345 }
1346 } else {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001347 // TODO is there anything else to implement?
1348 goto cannot_delete;
Damien429d7192013-10-04 19:53:11 +01001349 }
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001350
1351 return;
1352
1353cannot_delete:
1354 compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
Damien429d7192013-10-04 19:53:11 +01001355}
1356
Damien George969a6b32014-12-10 22:07:04 +00001357STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001358 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1359}
1360
Damien George969a6b32014-12-10 22:07:04 +00001361STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001362 if (comp->break_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001363 compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001364 }
Damien George090c9232014-10-17 14:08:49 +00001365 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001366 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001367}
1368
Damien George969a6b32014-12-10 22:07:04 +00001369STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001370 if (comp->continue_label == 0) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001371 compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001372 }
Damien George090c9232014-10-17 14:08:49 +00001373 assert(comp->cur_except_level >= comp->break_continue_except_level);
Damien Georgecbddb272014-02-01 20:08:18 +00001374 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001375}
1376
Damien George969a6b32014-12-10 22:07:04 +00001377STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001378 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001379 compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001380 return;
1381 }
Damiend99b0522013-12-21 18:17:45 +00001382 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001383 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001384 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001385 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001386 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001387 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1388 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 +01001389
Damien George6f355fd2014-04-10 14:11:31 +01001390 uint l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001391 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1392 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1393 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001394 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001395 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1396 } else {
1397 compile_node(comp, pns->nodes[0]);
1398 }
1399 EMIT(return_value);
1400}
1401
Damien George969a6b32014-12-10 22:07:04 +00001402STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001403 compile_node(comp, pns->nodes[0]);
1404 EMIT(pop_top);
1405}
1406
Damien George969a6b32014-12-10 22:07:04 +00001407STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00001408 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001409 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001410 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001411 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001412 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001413 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001414 compile_node(comp, pns->nodes[0]);
1415 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001417 } else {
1418 // raise x
1419 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001420 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001421 }
1422}
1423
Damien George635543c2014-04-10 12:56:52 +01001424// q_base holds the base of the name
1425// eg a -> q_base=a
1426// a.b.c -> q_base=a
Damien George6be0b0a2014-08-15 14:30:52 +01001427STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
Damien429d7192013-10-04 19:53:11 +01001428 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001429 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1430 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001431 // a name of the form x as y; unwrap it
Damien George635543c2014-04-10 12:56:52 +01001432 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001433 pn = pns->nodes[0];
1434 is_as = true;
1435 }
Damien George635543c2014-04-10 12:56:52 +01001436 if (MP_PARSE_NODE_IS_NULL(pn)) {
1437 // empty name (eg, from . import x)
1438 *q_base = MP_QSTR_;
1439 EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1440 } else if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001441 // just a simple name
Damien George635543c2014-04-10 12:56:52 +01001442 qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001443 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001444 *q_base = q_full;
Damien429d7192013-10-04 19:53:11 +01001445 }
Damien George635543c2014-04-10 12:56:52 +01001446 EMIT_ARG(import_name, q_full);
Damiend99b0522013-12-21 18:17:45 +00001447 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1448 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1449 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001450 // a name of the form a.b.c
1451 if (!is_as) {
Damien George635543c2014-04-10 12:56:52 +01001452 *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001453 }
Damiend99b0522013-12-21 18:17:45 +00001454 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001455 int len = n - 1;
1456 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001457 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001458 }
Damien George55baff42014-01-21 21:40:13 +00001459 byte *q_ptr;
1460 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001461 for (int i = 0; i < n; i++) {
1462 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001463 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001464 }
Damien George39dc1452014-10-03 19:52:22 +01001465 mp_uint_t str_src_len;
Damien George55baff42014-01-21 21:40:13 +00001466 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001467 memcpy(str_dest, str_src, str_src_len);
1468 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001469 }
Damien George635543c2014-04-10 12:56:52 +01001470 qstr q_full = qstr_build_end(q_ptr);
1471 EMIT_ARG(import_name, q_full);
Damien429d7192013-10-04 19:53:11 +01001472 if (is_as) {
1473 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001474 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001475 }
1476 }
1477 } else {
Damien George635543c2014-04-10 12:56:52 +01001478 // shouldn't happen
1479 assert(0);
Damien429d7192013-10-04 19:53:11 +01001480 }
1481 } else {
Damien George635543c2014-04-10 12:56:52 +01001482 // shouldn't happen
1483 assert(0);
Damien429d7192013-10-04 19:53:11 +01001484 }
1485}
1486
Damien George6be0b0a2014-08-15 14:30:52 +01001487STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien George635543c2014-04-10 12:56:52 +01001488 EMIT_ARG(load_const_small_int, 0); // level 0 import
1489 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
1490 qstr q_base;
1491 do_import_name(comp, pn, &q_base);
1492 EMIT_ARG(store_id, q_base);
Damien429d7192013-10-04 19:53:11 +01001493}
1494
Damien George969a6b32014-12-10 22:07:04 +00001495STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001496 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1497}
1498
Damien George969a6b32014-12-10 22:07:04 +00001499STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George635543c2014-04-10 12:56:52 +01001500 mp_parse_node_t pn_import_source = pns->nodes[0];
1501
1502 // extract the preceeding .'s (if any) for a relative import, to compute the import level
1503 uint import_level = 0;
1504 do {
1505 mp_parse_node_t pn_rel;
1506 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)) {
1507 // This covers relative imports with dots only like "from .. import"
1508 pn_rel = pn_import_source;
1509 pn_import_source = MP_PARSE_NODE_NULL;
1510 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
1511 // This covers relative imports starting with dot(s) like "from .foo import"
1512 mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
1513 pn_rel = pns_2b->nodes[0];
1514 pn_import_source = pns_2b->nodes[1];
1515 assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
1516 } else {
1517 // Not a relative import
1518 break;
1519 }
1520
1521 // get the list of . and/or ...'s
1522 mp_parse_node_t *nodes;
1523 int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1524
1525 // count the total number of .'s
1526 for (int i = 0; i < n; i++) {
1527 if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
1528 import_level++;
1529 } else {
1530 // should be an MP_TOKEN_ELLIPSIS
1531 import_level += 3;
1532 }
1533 }
1534 } while (0);
1535
Damiend99b0522013-12-21 18:17:45 +00001536 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien George635543c2014-04-10 12:56:52 +01001537 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001538
1539 // build the "fromlist" tuple
1540#if MICROPY_EMIT_CPYTHON
Damien George0d3cb672015-01-28 23:43:01 +00001541 EMIT_ARG(load_const_verbatim_strn, "('*',)", 6);
Damiendb4c3612013-12-10 17:27:24 +00001542#else
Damien George708c0732014-04-27 19:23:46 +01001543 EMIT_ARG(load_const_str, MP_QSTR__star_, false);
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001545#endif
1546
1547 // do the import
Damien George635543c2014-04-10 12:56:52 +01001548 qstr dummy_q;
1549 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001550 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001551
Damien429d7192013-10-04 19:53:11 +01001552 } else {
Damien George635543c2014-04-10 12:56:52 +01001553 EMIT_ARG(load_const_small_int, import_level);
Damiendb4c3612013-12-10 17:27:24 +00001554
1555 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001556 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001557 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001558#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001559 {
1560 vstr_t *vstr = vstr_new();
1561 vstr_printf(vstr, "(");
1562 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001563 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1564 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1565 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001566 if (i > 0) {
1567 vstr_printf(vstr, ", ");
1568 }
1569 vstr_printf(vstr, "'");
Damien George00be7a82014-10-03 20:05:44 +01001570 mp_uint_t len;
Damien George55baff42014-01-21 21:40:13 +00001571 const byte *str = qstr_data(id2, &len);
1572 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001573 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001574 }
Damien02f89412013-12-12 15:13:36 +00001575 if (n == 1) {
1576 vstr_printf(vstr, ",");
1577 }
1578 vstr_printf(vstr, ")");
Damien George0d3cb672015-01-28 23:43:01 +00001579 EMIT_ARG(load_const_verbatim_strn, vstr_str(vstr), vstr_len(vstr));
Damien02f89412013-12-12 15:13:36 +00001580 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001581 }
Damiendb4c3612013-12-10 17:27:24 +00001582#else
1583 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001584 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1585 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1586 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001587 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001588 }
Damien Georgeb9791222014-01-23 00:34:21 +00001589 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001590#endif
1591
1592 // do the import
Damien George635543c2014-04-10 12:56:52 +01001593 qstr dummy_q;
1594 do_import_name(comp, pn_import_source, &dummy_q);
Damien429d7192013-10-04 19:53:11 +01001595 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001596 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1597 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1598 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001599 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001600 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001601 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001602 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001603 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001604 }
1605 }
1606 EMIT(pop_top);
1607 }
1608}
1609
Damien George584ba672014-12-21 17:26:45 +00001610STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1611 bool added;
1612 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1613 if (!added) {
1614 compile_syntax_error(comp, pn, "identifier already used");
1615 return;
1616 }
1617 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1618
1619 // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
1620 id_info = scope_find_global(comp->scope_cur, qst);
1621 if (id_info != NULL) {
1622 id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
1623 }
1624}
1625
Damien George969a6b32014-12-10 22:07:04 +00001626STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001627 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001628 mp_parse_node_t *nodes;
1629 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1630 for (int i = 0; i < n; i++) {
1631 compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001632 }
1633 }
1634}
1635
Damien George584ba672014-12-21 17:26:45 +00001636STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) {
1637 bool added;
1638 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
1639 if (!added) {
1640 compile_syntax_error(comp, pn, "identifier already used");
1641 return;
1642 }
1643 id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst);
1644 if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
1645 compile_syntax_error(comp, pn, "no binding for nonlocal found");
1646 return;
1647 }
1648 id_info->kind = ID_INFO_KIND_FREE;
1649 scope_close_over_in_parents(comp->scope_cur, qst);
1650}
1651
Damien George969a6b32014-12-10 22:07:04 +00001652STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George36db6bc2014-05-07 17:24:22 +01001653 if (comp->pass == MP_PASS_SCOPE) {
Damien George584ba672014-12-21 17:26:45 +00001654 if (comp->scope_cur->kind == SCOPE_MODULE) {
1655 compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code");
1656 return;
1657 }
1658 mp_parse_node_t *nodes;
1659 int n = list_get(&pns->nodes[0], PN_name_list, &nodes);
1660 for (int i = 0; i < n; i++) {
1661 compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001662 }
1663 }
1664}
1665
Damien George969a6b32014-12-10 22:07:04 +00001666STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01001667 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001668 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001669 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 +00001670 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001671 // assertion message
1672 compile_node(comp, pns->nodes[1]);
Damien George922ddd62014-04-09 12:43:17 +01001673 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01001674 }
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(raise_varargs, 1);
1676 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001677}
1678
Damien George969a6b32014-12-10 22:07:04 +00001679STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001680 // TODO proper and/or short circuiting
1681
Damien George6f355fd2014-04-10 14:11:31 +01001682 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001683
Damien George391db862014-10-17 17:57:33 +00001684 // optimisation: don't emit anything when "if False" (not in CPython)
1685 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns->nodes[0])) {
1686 uint l_fail = comp_next_label(comp);
1687 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
Damien429d7192013-10-04 19:53:11 +01001688
Damien George391db862014-10-17 17:57:33 +00001689 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001690
Damien George391db862014-10-17 17:57:33 +00001691 // optimisation: skip everything else when "if True" (not in CPython)
1692 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns->nodes[0])) {
1693 goto done;
1694 }
1695
1696 if (
1697 // optimisation: don't jump over non-existent elif/else blocks (not in CPython)
1698 (MICROPY_EMIT_CPYTHON || !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])))
1699 // optimisation: don't jump if last instruction was return
1700 && !EMIT(last_emit_was_return_value)
1701 ) {
1702 // jump over elif/else blocks
1703 EMIT_ARG(jump, l_end);
1704 }
1705
1706 EMIT_ARG(label_assign, l_fail);
Damien Georgeaf6edc62014-04-02 16:12:28 +01001707 }
1708
Damien George235f9b32014-10-17 17:30:16 +00001709 // compile elif blocks (if any)
1710 mp_parse_node_t *pn_elif;
1711 int n_elif = list_get(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1712 for (int i = 0; i < n_elif; i++) {
1713 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
1714 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i];
Damien429d7192013-10-04 19:53:11 +01001715
Damien George391db862014-10-17 17:57:33 +00001716 // optimisation: don't emit anything when "if False" (not in CPython)
1717 if (MICROPY_EMIT_CPYTHON || !node_is_const_false(pns_elif->nodes[0])) {
1718 uint l_fail = comp_next_label(comp);
1719 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
Damien429d7192013-10-04 19:53:11 +01001720
Damien George391db862014-10-17 17:57:33 +00001721 compile_node(comp, pns_elif->nodes[1]); // elif block
1722
1723 // optimisation: skip everything else when "elif True" (not in CPython)
1724 if (!MICROPY_EMIT_CPYTHON && node_is_const_true(pns_elif->nodes[0])) {
1725 goto done;
1726 }
1727
1728 // optimisation: don't jump if last instruction was return
1729 if (!EMIT(last_emit_was_return_value)) {
1730 EMIT_ARG(jump, l_end);
1731 }
1732 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001733 }
1734 }
1735
1736 // compile else block
1737 compile_node(comp, pns->nodes[3]); // can be null
1738
Damien George391db862014-10-17 17:57:33 +00001739done:
Damien Georgeb9791222014-01-23 00:34:21 +00001740 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001741}
1742
Damien Georgecbddb272014-02-01 20:08:18 +00001743#define START_BREAK_CONTINUE_BLOCK \
Damien George090c9232014-10-17 14:08:49 +00001744 uint16_t old_break_label = comp->break_label; \
1745 uint16_t old_continue_label = comp->continue_label; \
1746 uint16_t old_break_continue_except_level = comp->break_continue_except_level; \
Damien George6f355fd2014-04-10 14:11:31 +01001747 uint break_label = comp_next_label(comp); \
1748 uint continue_label = comp_next_label(comp); \
Damien Georgecbddb272014-02-01 20:08:18 +00001749 comp->break_label = break_label; \
1750 comp->continue_label = continue_label; \
1751 comp->break_continue_except_level = comp->cur_except_level;
1752
1753#define END_BREAK_CONTINUE_BLOCK \
1754 comp->break_label = old_break_label; \
1755 comp->continue_label = old_continue_label; \
Damien George090c9232014-10-17 14:08:49 +00001756 comp->break_continue_except_level = old_break_continue_except_level;
Damien Georgecbddb272014-02-01 20:08:18 +00001757
Damien George969a6b32014-12-10 22:07:04 +00001758STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001759 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001760
Damience89a212013-10-15 22:25:17 +01001761 // compared to CPython, we have an optimised version of while loops
1762#if MICROPY_EMIT_CPYTHON
Damien George6f355fd2014-04-10 14:11:31 +01001763 uint done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001764 EMIT_ARG(setup_loop, break_label);
1765 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001766 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1767 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001768 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001769 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001770 }
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001772 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1773 // this is a small hack to agree with CPython
1774 if (!node_is_const_true(pns->nodes[0])) {
1775 EMIT(pop_block);
1776 }
Damience89a212013-10-15 22:25:17 +01001777#else
Damien George391db862014-10-17 17:57:33 +00001778 if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False"
1779 uint top_label = comp_next_label(comp);
1780 if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True"
1781 EMIT_ARG(jump, continue_label);
1782 }
1783 EMIT_ARG(label_assign, top_label);
1784 compile_node(comp, pns->nodes[1]); // body
1785 EMIT_ARG(label_assign, continue_label);
1786 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1787 }
Damience89a212013-10-15 22:25:17 +01001788#endif
1789
1790 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001791 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001792
1793 compile_node(comp, pns->nodes[2]); // else
1794
Damien Georgeb9791222014-01-23 00:34:21 +00001795 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001796}
1797
Damien George2ac4af62014-08-15 16:45:41 +01001798#if !MICROPY_EMIT_CPYTHON
Damien Georgee181c0d2014-12-12 17:19:56 +00001799// This function compiles an optimised for-loop of the form:
1800// for <var> in range(<start>, <end>, <step>):
1801// <body>
1802// else:
1803// <else>
1804// <var> must be an identifier and <step> must be a small-int.
1805//
1806// Semantics of for-loop require:
1807// - final failing value should not be stored in the loop variable
1808// - if the loop never runs, the loop variable should never be assigned
1809// - assignments to <var>, <end> or <step> in the body do not alter the loop
1810// (<step> is a constant for us, so no need to worry about it changing)
1811//
1812// If <end> is a small-int, then the stack during the for-loop contains just
1813// the current value of <var>. Otherwise, the stack contains <end> then the
1814// current value of <var>.
Damien George6be0b0a2014-08-15 14:30:52 +01001815STATIC 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 +00001816 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001817
Damien George6f355fd2014-04-10 14:11:31 +01001818 uint top_label = comp_next_label(comp);
1819 uint entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001820
Damien Georgee181c0d2014-12-12 17:19:56 +00001821 // put the end value on the stack if it's not a small-int constant
1822 bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end);
1823 if (end_on_stack) {
1824 compile_node(comp, pn_end);
1825 }
1826
1827 // compile: start
Damienf72fd0e2013-11-06 20:20:49 +00001828 compile_node(comp, pn_start);
Damienf72fd0e2013-11-06 20:20:49 +00001829
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(jump, entry_label);
1831 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001832
Damien Georgec33ce602014-12-11 17:35:23 +00001833 // duplicate next value and store it to var
1834 EMIT(dup_top);
Damien George3ff2d032014-03-31 18:02:22 +01001835 c_assign(comp, pn_var, ASSIGN_STORE);
1836
Damienf3822fc2013-11-09 20:12:03 +00001837 // compile body
1838 compile_node(comp, pn_body);
1839
Damien Georgeb9791222014-01-23 00:34:21 +00001840 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001841
Damien Georgee181c0d2014-12-12 17:19:56 +00001842 // compile: var + step
Damienf72fd0e2013-11-06 20:20:49 +00001843 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001844 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001845
Damien Georgeb9791222014-01-23 00:34:21 +00001846 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001847
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001848 // compile: if var <cond> end: goto top
Damien Georgee181c0d2014-12-12 17:19:56 +00001849 if (end_on_stack) {
1850 EMIT(dup_top_two);
1851 EMIT(rot_two);
1852 } else {
1853 EMIT(dup_top);
1854 compile_node(comp, pn_end);
1855 }
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001856 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1857 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001858 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001859 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001860 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001861 }
Damien Georgeb9791222014-01-23 00:34:21 +00001862 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001863
1864 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001865 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001866
1867 compile_node(comp, pn_else);
1868
Damien Georgeb9791222014-01-23 00:34:21 +00001869 EMIT_ARG(label_assign, break_label);
Damien Georgee181c0d2014-12-12 17:19:56 +00001870
1871 // discard final value of var that failed the loop condition
1872 EMIT(pop_top);
1873
1874 // discard <end> value if it's on the stack
1875 if (end_on_stack) {
1876 EMIT(pop_top);
1877 }
Damienf72fd0e2013-11-06 20:20:49 +00001878}
Damien George2ac4af62014-08-15 16:45:41 +01001879#endif
Damienf72fd0e2013-11-06 20:20:49 +00001880
Damien George969a6b32014-12-10 22:07:04 +00001881STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001882#if !MICROPY_EMIT_CPYTHON
1883 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1884 // this is actually slower, but uses no heap memory
1885 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001886 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 +00001887 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001888 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1889 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1890 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1891 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001892 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1893 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001894 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001895 mp_parse_node_t pn_range_start;
1896 mp_parse_node_t pn_range_end;
1897 mp_parse_node_t pn_range_step;
1898 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001899 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001900 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001901 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001902 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001903 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001904 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001905 } else if (n_args == 2) {
1906 pn_range_start = args[0];
1907 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001908 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001909 } else {
1910 pn_range_start = args[0];
1911 pn_range_end = args[1];
1912 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001913 // We need to know sign of step. This is possible only if it's constant
1914 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1915 optimize = false;
1916 }
Damienf72fd0e2013-11-06 20:20:49 +00001917 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001918 }
1919 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001920 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1921 return;
1922 }
1923 }
1924 }
1925#endif
1926
Damien Georgecbddb272014-02-01 20:08:18 +00001927 START_BREAK_CONTINUE_BLOCK
Damien George25c84642014-05-30 15:20:41 +01001928 comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
Damien429d7192013-10-04 19:53:11 +01001929
Damien George6f355fd2014-04-10 14:11:31 +01001930 uint pop_label = comp_next_label(comp);
1931 uint end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001932
Damience89a212013-10-15 22:25:17 +01001933 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1934#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001935 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001936#endif
1937
Damien429d7192013-10-04 19:53:11 +01001938 compile_node(comp, pns->nodes[1]); // iterator
1939 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001940 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001941 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001942 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1943 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001944 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001945 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001946 }
Damien Georgeb9791222014-01-23 00:34:21 +00001947 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001948 EMIT(for_iter_end);
1949
1950 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001951 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001952
Damience89a212013-10-15 22:25:17 +01001953#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001954 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001955#endif
Damien429d7192013-10-04 19:53:11 +01001956
1957 compile_node(comp, pns->nodes[3]); // else (not tested)
1958
Damien Georgeb9791222014-01-23 00:34:21 +00001959 EMIT_ARG(label_assign, break_label);
1960 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001961}
1962
Damien George6be0b0a2014-08-15 14:30:52 +01001963STATIC 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 +01001964 // setup code
Damien George6f355fd2014-04-10 14:11:31 +01001965 uint l1 = comp_next_label(comp);
1966 uint success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001967
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001969 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001970
Damien429d7192013-10-04 19:53:11 +01001971 compile_node(comp, pn_body); // body
1972 EMIT(pop_block);
Damien George069a35e2014-04-10 17:22:19 +00001973 EMIT_ARG(jump, success_label); // jump over exception handler
1974
1975 EMIT_ARG(label_assign, l1); // start of exception handler
Damien Georgeb601d952014-06-30 05:17:25 +01001976 EMIT(start_except_handler);
Damien George069a35e2014-04-10 17:22:19 +00001977
Damien George6f355fd2014-04-10 14:11:31 +01001978 uint l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001979
1980 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001981 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1982 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001983
1984 qstr qstr_exception_local = 0;
Damien George6f355fd2014-04-10 14:11:31 +01001985 uint end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001986
Damiend99b0522013-12-21 18:17:45 +00001987 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001988 // this is a catch all exception handler
1989 if (i + 1 != n_except) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01001990 compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
Damien Georgec935d692015-01-13 23:33:16 +00001991 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001992 return;
1993 }
1994 } else {
1995 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001996 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1997 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1998 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1999 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01002000 // handler binds the exception to a local
2001 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002002 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002003 }
2004 }
2005 EMIT(dup_top);
2006 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01002007 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00002008 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01002009 }
2010
2011 EMIT(pop_top);
2012
2013 if (qstr_exception_local == 0) {
2014 EMIT(pop_top);
2015 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002016 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01002017 }
2018
2019 EMIT(pop_top);
2020
Damien George6f355fd2014-04-10 14:11:31 +01002021 uint l3 = 0;
Damien429d7192013-10-04 19:53:11 +01002022 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01002023 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002024 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00002025 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002026 }
2027 compile_node(comp, pns_except->nodes[1]);
2028 if (qstr_exception_local != 0) {
2029 EMIT(pop_block);
2030 }
2031 EMIT(pop_except);
2032 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00002033 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2034 EMIT_ARG(label_assign, l3);
2035 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2036 EMIT_ARG(store_id, qstr_exception_local);
2037 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00002038
Damien George8dcc0c72014-03-27 10:55:21 +00002039 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002040 EMIT(end_finally);
2041 }
Damien Georgeb9791222014-01-23 00:34:21 +00002042 EMIT_ARG(jump, l2);
2043 EMIT_ARG(label_assign, end_finally_label);
Damien Georged66ae182014-04-10 17:28:54 +00002044 EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
Damien429d7192013-10-04 19:53:11 +01002045 }
2046
Damien George8dcc0c72014-03-27 10:55:21 +00002047 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002048 EMIT(end_finally);
Damien Georgeb601d952014-06-30 05:17:25 +01002049 EMIT(end_except_handler);
Damien Georgecbddb272014-02-01 20:08:18 +00002050
Damien Georgeb9791222014-01-23 00:34:21 +00002051 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01002052 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00002053 EMIT_ARG(label_assign, l2);
Damien429d7192013-10-04 19:53:11 +01002054}
2055
Damien George6be0b0a2014-08-15 14:30:52 +01002056STATIC 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 +01002057 uint l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002058
Damien Georgeb9791222014-01-23 00:34:21 +00002059 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00002060 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00002061
Damien429d7192013-10-04 19:53:11 +01002062 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00002063 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien Georged66ae182014-04-10 17:28:54 +00002064 EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
Damien429d7192013-10-04 19:53:11 +01002065 compile_node(comp, pn_body);
Damien Georged66ae182014-04-10 17:28:54 +00002066 EMIT_ARG(adjust_stack_size, -3);
Damien429d7192013-10-04 19:53:11 +01002067 } else {
2068 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
2069 }
2070 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002071 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2072 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01002073 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00002074
Damien George8dcc0c72014-03-27 10:55:21 +00002075 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002076 EMIT(end_finally);
Damien429d7192013-10-04 19:53:11 +01002077}
2078
Damien George969a6b32014-12-10 22:07:04 +00002079STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002080 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2081 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2082 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01002083 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00002084 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
2085 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01002086 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00002087 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002088 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002089 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01002090 // no finally
2091 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
2092 } else {
2093 // have finally
Damiend99b0522013-12-21 18:17:45 +00002094 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 +01002095 }
2096 } else {
2097 // just try-except
Damiend99b0522013-12-21 18:17:45 +00002098 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01002099 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00002100 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01002101 }
2102 } else {
2103 // shouldn't happen
2104 assert(0);
2105 }
2106}
2107
Damien George6be0b0a2014-08-15 14:30:52 +01002108STATIC 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 +01002109 if (n == 0) {
2110 // no more pre-bits, compile the body of the with
2111 compile_node(comp, body);
2112 } else {
Damien George6f355fd2014-04-10 14:11:31 +01002113 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002114 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01002115 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00002116 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01002117 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002118 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002119 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
2120 } else {
2121 // this pre-bit is just an expression
2122 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002123 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01002124 EMIT(pop_top);
2125 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002126 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002127 // compile additional pre-bits and the body
2128 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
2129 // finish this with block
2130 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00002131 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2132 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002133 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02002134 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01002135 EMIT(end_finally);
2136 }
2137}
2138
Damien George969a6b32014-12-10 22:07:04 +00002139STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002140 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00002141 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002142 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
2143 assert(n > 0);
2144
2145 // compile in a nested fashion
2146 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
2147}
2148
Damien George969a6b32014-12-10 22:07:04 +00002149STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002150 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01002151 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
2152 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00002153 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01002154 compile_node(comp, pns->nodes[0]);
Damien George922ddd62014-04-09 12:43:17 +01002155 EMIT_ARG(call_function, 1, 0, 0);
Damien5ac1b2e2013-10-18 19:58:12 +01002156 EMIT(pop_top);
2157
Damien429d7192013-10-04 19:53:11 +01002158 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01002159 // for non-REPL, evaluate then discard the expression
Damien George5042bce2014-05-25 22:06:06 +01002160 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
Damien George4c81ba82015-01-13 16:21:23 +00002161 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
2162 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)) {
Damien5ac1b2e2013-10-18 19:58:12 +01002163 // do nothing with a lonely constant
2164 } else {
2165 compile_node(comp, pns->nodes[0]); // just an expression
2166 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
2167 }
Damien429d7192013-10-04 19:53:11 +01002168 }
2169 } else {
Damiend99b0522013-12-21 18:17:45 +00002170 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2171 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01002172 if (kind == PN_expr_stmt_augassign) {
2173 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
2174 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00002175 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01002176 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00002177 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002178 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
2179 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
2180 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
2181 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
2182 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
2183 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
2184 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
2185 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
2186 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
2187 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
2188 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002189 case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
Damien429d7192013-10-04 19:53:11 +01002190 }
Damien George7e5fb242014-02-01 22:18:47 +00002191 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01002192 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
2193 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00002194 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
2195 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01002196 // following CPython, we store left-most first
2197 if (rhs > 0) {
2198 EMIT(dup_top);
2199 }
2200 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2201 for (int i = 0; i < rhs; i++) {
2202 if (i + 1 < rhs) {
2203 EMIT(dup_top);
2204 }
Damiend99b0522013-12-21 18:17:45 +00002205 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01002206 }
2207 } else if (kind == PN_expr_stmt_assign) {
Damien Georgeffae48d2014-05-08 15:58:39 +00002208 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
Damiend99b0522013-12-21 18:17:45 +00002209 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2210 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
2211 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01002212 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002213 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2214 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002215 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2216 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
2217 // can't optimise when it's a star expression on the lhs
2218 goto no_optimisation;
2219 }
Damien429d7192013-10-04 19:53:11 +01002220 compile_node(comp, pns10->nodes[0]); // rhs
2221 compile_node(comp, pns10->nodes[1]); // rhs
2222 EMIT(rot_two);
2223 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2224 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00002225 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
2226 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
2227 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
2228 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01002229 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00002230 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
2231 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien George495d7812014-04-08 17:51:47 +01002232 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
2233 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
2234 || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
2235 // can't optimise when it's a star expression on the lhs
2236 goto no_optimisation;
2237 }
Damien429d7192013-10-04 19:53:11 +01002238 compile_node(comp, pns10->nodes[0]); // rhs
2239 compile_node(comp, pns10->nodes[1]); // rhs
2240 compile_node(comp, pns10->nodes[2]); // rhs
2241 EMIT(rot_three);
2242 EMIT(rot_two);
2243 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
2244 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
2245 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
2246 } else {
Damien George495d7812014-04-08 17:51:47 +01002247 no_optimisation:
Damien429d7192013-10-04 19:53:11 +01002248 compile_node(comp, pns1->nodes[0]); // rhs
2249 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
2250 }
2251 } else {
2252 // shouldn't happen
2253 assert(0);
2254 }
2255 }
2256}
2257
Damien George6be0b0a2014-08-15 14:30:52 +01002258STATIC 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 +00002259 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002260 compile_node(comp, pns->nodes[0]);
2261 for (int i = 1; i < num_nodes; i += 1) {
2262 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00002263 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01002264 }
2265}
2266
Damien George969a6b32014-12-10 22:07:04 +00002267STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002268 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
2269 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002270
Damien George6f355fd2014-04-10 14:11:31 +01002271 uint l_fail = comp_next_label(comp);
2272 uint l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002273 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
2274 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00002275 EMIT_ARG(jump, l_end);
2276 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002277 EMIT_ARG(adjust_stack_size, -1); // adjust stack size
Damien429d7192013-10-04 19:53:11 +01002278 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00002279 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002280}
2281
Damien George969a6b32014-12-10 22:07:04 +00002282STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002283 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00002284 //mp_parse_node_t pn_params = pns->nodes[0];
2285 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002286
Damien George36db6bc2014-05-07 17:24:22 +01002287 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002288 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00002289 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 +01002290 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002291 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002292 }
2293
2294 // get the scope for this lambda
2295 scope_t *this_scope = (scope_t*)pns->nodes[2];
2296
2297 // make the lambda
2298 close_over_variables_etc(comp, this_scope, 0, 0);
2299}
2300
Damien George969a6b32014-12-10 22:07:04 +00002301STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002302 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002303 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002304 for (int i = 0; i < n; i += 1) {
2305 compile_node(comp, pns->nodes[i]);
2306 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002307 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002308 }
2309 }
Damien Georgeb9791222014-01-23 00:34:21 +00002310 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002311}
2312
Damien George969a6b32014-12-10 22:07:04 +00002313STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George6f355fd2014-04-10 14:11:31 +01002314 uint l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002315 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002316 for (int i = 0; i < n; i += 1) {
2317 compile_node(comp, pns->nodes[i]);
2318 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002319 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002320 }
2321 }
Damien Georgeb9791222014-01-23 00:34:21 +00002322 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002323}
2324
Damien George969a6b32014-12-10 22:07:04 +00002325STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002326 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002327 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002328}
2329
Damien George969a6b32014-12-10 22:07:04 +00002330STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002331 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002332 compile_node(comp, pns->nodes[0]);
2333 bool multi = (num_nodes > 3);
Damien George6f355fd2014-04-10 14:11:31 +01002334 uint l_fail = 0;
Damien429d7192013-10-04 19:53:11 +01002335 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002336 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002337 }
2338 for (int i = 1; i + 1 < num_nodes; i += 2) {
2339 compile_node(comp, pns->nodes[i + 1]);
2340 if (i + 2 < num_nodes) {
2341 EMIT(dup_top);
2342 EMIT(rot_three);
2343 }
Damien George7e5fb242014-02-01 22:18:47 +00002344 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002345 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002346 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002347 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2348 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2349 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2350 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2351 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2352 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
Damien Georged2d64f02015-01-14 21:32:42 +00002353 case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
Damien George7e5fb242014-02-01 22:18:47 +00002354 }
2355 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002356 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2357 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2358 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002359 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002360 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002361 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002362 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002363 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002364 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002365 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002366 }
2367 } else {
2368 // shouldn't happen
2369 assert(0);
2370 }
2371 } else {
2372 // shouldn't happen
2373 assert(0);
2374 }
2375 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002376 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002377 }
2378 }
2379 if (multi) {
Damien George6f355fd2014-04-10 14:11:31 +01002380 uint l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002381 EMIT_ARG(jump, l_end);
2382 EMIT_ARG(label_assign, l_fail);
Damien Georged66ae182014-04-10 17:28:54 +00002383 EMIT_ARG(adjust_stack_size, 1);
Damien429d7192013-10-04 19:53:11 +01002384 EMIT(rot_two);
2385 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002386 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002387 }
2388}
2389
Damien George969a6b32014-12-10 22:07:04 +00002390STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George9d181f62014-04-27 16:55:27 +01002391 compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
Damien429d7192013-10-04 19:53:11 +01002392}
2393
Damien George969a6b32014-12-10 22:07:04 +00002394STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002395 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002396}
2397
Damien George969a6b32014-12-10 22:07:04 +00002398STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002399 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002400}
2401
Damien George969a6b32014-12-10 22:07:04 +00002402STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002403 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002404}
2405
Damien George969a6b32014-12-10 22:07:04 +00002406STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002407 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002408 compile_node(comp, pns->nodes[0]);
2409 for (int i = 1; i + 1 < num_nodes; i += 2) {
2410 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002411 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002412 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002413 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002414 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002415 } else {
2416 // shouldn't happen
2417 assert(0);
2418 }
2419 }
2420}
2421
Damien George969a6b32014-12-10 22:07:04 +00002422STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002423 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002424 compile_node(comp, pns->nodes[0]);
2425 for (int i = 1; i + 1 < num_nodes; i += 2) {
2426 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002427 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002428 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002429 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002430 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002431 } else {
2432 // shouldn't happen
2433 assert(0);
2434 }
2435 }
2436}
2437
Damien George969a6b32014-12-10 22:07:04 +00002438STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002439 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002440 compile_node(comp, pns->nodes[0]);
2441 for (int i = 1; i + 1 < num_nodes; i += 2) {
2442 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002443 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002444 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002445 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002446 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002447 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002448 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002449 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002450 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002451 } else {
2452 // shouldn't happen
2453 assert(0);
2454 }
2455 }
2456}
2457
Damien George969a6b32014-12-10 22:07:04 +00002458STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002459 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002460 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002461 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002463 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002464 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002465 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002466 } else {
2467 // shouldn't happen
2468 assert(0);
2469 }
2470}
2471
Damien George969a6b32014-12-10 22:07:04 +00002472STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George35e2a4e2014-02-05 00:51:47 +00002473 // this is to handle special super() call
2474 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2475
2476 compile_generic_all_nodes(comp, pns);
2477}
2478
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002479STATIC 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 +01002480 // function to call is on top of stack
2481
Damien George35e2a4e2014-02-05 00:51:47 +00002482#if !MICROPY_EMIT_CPYTHON
2483 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002484 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 +00002485 EMIT_ARG(load_id, MP_QSTR___class__);
Damien George01039b52014-12-21 17:44:27 +00002486 // look for first argument to function (assumes it's "self")
Damien George35e2a4e2014-02-05 00:51:47 +00002487 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
Damien George2bf7c092014-04-09 15:26:46 +01002488 if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
Damien George01039b52014-12-21 17:44:27 +00002489 // first argument found; load it and call super
Damien George0abb5602015-01-16 12:24:49 +00002490 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
Damien George01039b52014-12-21 17:44:27 +00002491 EMIT_ARG(call_function, 2, 0, 0);
2492 return;
Damien George35e2a4e2014-02-05 00:51:47 +00002493 }
2494 }
Damien George01039b52014-12-21 17:44:27 +00002495 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError
Damien George35e2a4e2014-02-05 00:51:47 +00002496 return;
2497 }
2498#endif
2499
Damien George2c0842b2014-04-27 16:46:51 +01002500 // get the list of arguments
2501 mp_parse_node_t *args;
2502 int n_args = list_get(&pn_arglist, PN_arglist, &args);
Damien429d7192013-10-04 19:53:11 +01002503
Damien George2c0842b2014-04-27 16:46:51 +01002504 // compile the arguments
2505 // Rather than calling compile_node on the list, we go through the list of args
2506 // explicitly here so that we can count the number of arguments and give sensible
2507 // error messages.
2508 int n_positional = n_positional_extra;
2509 uint n_keyword = 0;
2510 uint star_flags = 0;
2511 for (int i = 0; i < n_args; i++) {
2512 if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
2513 mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
2514 if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
2515 if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
2516 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
2517 return;
2518 }
2519 star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
2520 compile_node(comp, pns_arg->nodes[0]);
2521 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
2522 if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
2523 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
2524 return;
2525 }
2526 star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
2527 compile_node(comp, pns_arg->nodes[0]);
2528 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
2529 assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
2530 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
2531 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2532 if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
2533 compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
2534 return;
2535 }
Damien George968bf342014-04-27 19:12:05 +01002536 EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
Damien George2c0842b2014-04-27 16:46:51 +01002537 compile_node(comp, pns2->nodes[0]);
2538 n_keyword += 1;
2539 } else {
2540 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
2541 compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
2542 n_positional++;
2543 }
2544 } else {
2545 goto normal_argument;
2546 }
2547 } else {
2548 normal_argument:
2549 if (n_keyword > 0) {
2550 compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
2551 return;
2552 }
2553 compile_node(comp, args[i]);
2554 n_positional++;
2555 }
Damien429d7192013-10-04 19:53:11 +01002556 }
2557
Damien George2c0842b2014-04-27 16:46:51 +01002558 // emit the function/method call
Damien429d7192013-10-04 19:53:11 +01002559 if (is_method_call) {
Damien George2c0842b2014-04-27 16:46:51 +01002560 EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002561 } else {
Damien George2c0842b2014-04-27 16:46:51 +01002562 EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
Damien429d7192013-10-04 19:53:11 +01002563 }
Damien429d7192013-10-04 19:53:11 +01002564}
2565
Damien George969a6b32014-12-10 22:07:04 +00002566STATIC void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002567 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002568 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002569 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 +01002570 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002571 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2572 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002573 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002574 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002575 i += 1;
2576 } else {
2577 compile_node(comp, pns->nodes[i]);
2578 }
Damien George35e2a4e2014-02-05 00:51:47 +00002579 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002580 }
2581}
2582
Damien George969a6b32014-12-10 22:07:04 +00002583STATIC void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002584 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002585 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002586}
2587
Damien George969a6b32014-12-10 22:07:04 +00002588STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002589 // a list of strings
Damien63321742013-12-10 17:41:49 +00002590
2591 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002592 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002593 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002594 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002595 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002596 int pn_kind;
2597 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
2598 pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2599 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
2600 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2601 } else {
2602 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
2603 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George4c81ba82015-01-13 16:21:23 +00002604 if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) {
2605 pn_kind = MP_PARSE_NODE_STRING;
2606 } else {
2607 assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes);
2608 pn_kind = MP_PARSE_NODE_BYTES;
2609 }
Damien George40f3c022014-07-03 13:25:24 +01002610 n_bytes += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002611 }
Damien63321742013-12-10 17:41:49 +00002612 if (i == 0) {
2613 string_kind = pn_kind;
2614 } else if (pn_kind != string_kind) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002615 compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002616 return;
2617 }
Damien429d7192013-10-04 19:53:11 +01002618 }
Damien63321742013-12-10 17:41:49 +00002619
Damien George65ef6b72015-01-14 21:17:27 +00002620 // if we are not in the last pass, just load a dummy object
2621 if (comp->pass != MP_PASS_EMIT) {
2622 EMIT_ARG(load_const_obj, mp_const_none);
2623 return;
2624 }
2625
Damien63321742013-12-10 17:41:49 +00002626 // concatenate string/bytes
Damien George05005f62015-01-21 22:48:37 +00002627 vstr_t vstr;
2628 vstr_init_len(&vstr, n_bytes);
2629 byte *s_dest = (byte*)vstr.buf;
Damien63321742013-12-10 17:41:49 +00002630 for (int i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +01002631 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
Damien George39dc1452014-10-03 19:52:22 +01002632 mp_uint_t s_len;
Damien George5042bce2014-05-25 22:06:06 +01002633 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
2634 memcpy(s_dest, s, s_len);
2635 s_dest += s_len;
2636 } else {
2637 mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
Damien George40f3c022014-07-03 13:25:24 +01002638 memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
2639 s_dest += (mp_uint_t)pns_string->nodes[1];
Damien George5042bce2014-05-25 22:06:06 +01002640 }
Damien63321742013-12-10 17:41:49 +00002641 }
Damien George65ef6b72015-01-14 21:17:27 +00002642
2643 // load the object
Damien George05005f62015-01-21 22:48:37 +00002644 EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr));
Damien429d7192013-10-04 19:53:11 +01002645}
2646
2647// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damien George6be0b0a2014-08-15 14:30:52 +01002648STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +00002649 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2650 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2651 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002652
Damien George36db6bc2014-05-07 17:24:22 +01002653 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01002654 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002655 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 +01002656 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002657 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002658 }
2659
2660 // get the scope for this comprehension
2661 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2662
2663 // compile the comprehension
2664 close_over_variables_etc(comp, this_scope, 0, 0);
2665
2666 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2667 EMIT(get_iter);
Damien George922ddd62014-04-09 12:43:17 +01002668 EMIT_ARG(call_function, 1, 0, 0);
Damien429d7192013-10-04 19:53:11 +01002669}
2670
Damien George969a6b32014-12-10 22:07:04 +00002671STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002672 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002673 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002674 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2675 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2676 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2677 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2678 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2679 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2680 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002681 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002682 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002683 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002684 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002685 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002686 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002687 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002688 // generator expression
2689 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2690 } else {
2691 // tuple with 2 items
2692 goto tuple_with_2_items;
2693 }
2694 } else {
2695 // tuple with 2 items
2696 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002697 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002698 }
2699 } else {
2700 // parenthesis around a single item, is just that item
2701 compile_node(comp, pns->nodes[0]);
2702 }
2703}
2704
Damien George969a6b32014-12-10 22:07:04 +00002705STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002706 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002707 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002708 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002709 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2710 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2711 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2712 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2713 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002714 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002715 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002716 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002717 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002718 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002719 // list of many items
2720 compile_node(comp, pns2->nodes[0]);
2721 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002722 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002723 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002724 // list comprehension
2725 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2726 } else {
2727 // list with 2 items
2728 goto list_with_2_items;
2729 }
2730 } else {
2731 // list with 2 items
2732 list_with_2_items:
2733 compile_node(comp, pns2->nodes[0]);
2734 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002735 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002736 }
2737 } else {
2738 // list with 1 item
2739 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002740 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002741 }
2742}
2743
Damien George969a6b32014-12-10 22:07:04 +00002744STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002745 mp_parse_node_t pn = pns->nodes[0];
2746 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002747 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002748 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002749 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2750 pns = (mp_parse_node_struct_t*)pn;
2751 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002752 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002753 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002754 compile_node(comp, pn);
2755 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002756 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2757 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2758 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2759 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002760 // dict/set with multiple elements
2761
2762 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002763 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002764 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2765
2766 // first element sets whether it's a dict or set
2767 bool is_dict;
Damien Georgee37dcaa2014-12-27 17:07:16 +00002768 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002769 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002770 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002771 compile_node(comp, pns->nodes[0]);
2772 EMIT(store_map);
2773 is_dict = true;
2774 } else {
2775 // a set
2776 compile_node(comp, pns->nodes[0]); // 1st value of set
2777 is_dict = false;
2778 }
2779
2780 // process rest of elements
2781 for (int i = 0; i < n; i++) {
Damien George50912e72015-01-20 11:55:10 +00002782 mp_parse_node_t pn_i = nodes[i];
2783 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
2784 compile_node(comp, pn_i);
Damien429d7192013-10-04 19:53:11 +01002785 if (is_dict) {
2786 if (!is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002787 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
Damien429d7192013-10-04 19:53:11 +01002788 return;
2789 }
2790 EMIT(store_map);
2791 } else {
2792 if (is_key_value) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002793 compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
Damien429d7192013-10-04 19:53:11 +01002794 return;
2795 }
2796 }
2797 }
2798
Damien Georgee37dcaa2014-12-27 17:07:16 +00002799 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002800 // if it's a set, build it
2801 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002802 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002803 }
Damien Georgee37dcaa2014-12-27 17:07:16 +00002804 #endif
Damiend99b0522013-12-21 18:17:45 +00002805 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002806 // dict/set comprehension
Damien Georgee37dcaa2014-12-27 17:07:16 +00002807 if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002808 // a dictionary comprehension
2809 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2810 } else {
2811 // a set comprehension
2812 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2813 }
2814 } else {
2815 // shouldn't happen
2816 assert(0);
2817 }
2818 } else {
2819 // set with one element
2820 goto set_with_one_element;
2821 }
2822 } else {
2823 // set with one element
2824 set_with_one_element:
Damien Georgee37dcaa2014-12-27 17:07:16 +00002825 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01002826 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002827 EMIT_ARG(build_set, 1);
Damien Georgee37dcaa2014-12-27 17:07:16 +00002828 #else
2829 assert(0);
2830 #endif
Damien429d7192013-10-04 19:53:11 +01002831 }
2832}
2833
Damien George969a6b32014-12-10 22:07:04 +00002834STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002835 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002836}
2837
Damien George969a6b32014-12-10 22:07:04 +00002838STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002839 // object who's index we want is on top of stack
2840 compile_node(comp, pns->nodes[0]); // the index
Damien George729f7b42014-04-17 22:10:53 +01002841 EMIT(load_subscr);
Damien429d7192013-10-04 19:53:11 +01002842}
2843
Damien George969a6b32014-12-10 22:07:04 +00002844STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002845 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002847}
2848
Damien George83204f32014-12-27 17:20:41 +00002849#if MICROPY_PY_BUILTINS_SLICE
Damien George969a6b32014-12-10 22:07:04 +00002850STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damiend99b0522013-12-21 18:17:45 +00002851 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2852 mp_parse_node_t pn = pns->nodes[0];
2853 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002854 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002855 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2856 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002857 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2858 pns = (mp_parse_node_struct_t*)pn;
2859 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002861 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002862 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002863 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002864 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002865 } else {
2866 // [?::x]
2867 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002868 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002869 }
Damiend99b0522013-12-21 18:17:45 +00002870 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002871 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002872 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2873 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2874 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2875 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002876 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002878 } else {
2879 // [?:x:x]
2880 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002881 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002882 }
2883 } else {
2884 // [?:x]
2885 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002886 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002887 }
2888 } else {
2889 // [?:x]
2890 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002892 }
2893}
2894
Damien George969a6b32014-12-10 22:07:04 +00002895STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002896 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002897 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2898 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002899}
2900
Damien George969a6b32014-12-10 22:07:04 +00002901STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002902 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002903 compile_subscript_3_helper(comp, pns);
2904}
Damien George83204f32014-12-27 17:20:41 +00002905#endif // MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +01002906
Damien George969a6b32014-12-10 22:07:04 +00002907STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002908 // if this is called then we are compiling a dict key:value pair
2909 compile_node(comp, pns->nodes[1]); // value
2910 compile_node(comp, pns->nodes[0]); // key
2911}
2912
Damien George969a6b32014-12-10 22:07:04 +00002913STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002914 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002915 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002916 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002917}
2918
Damien George969a6b32014-12-10 22:07:04 +00002919STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien George0e3329a2014-04-11 13:10:21 +00002920 if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01002921 compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002922 return;
2923 }
Damiend99b0522013-12-21 18:17:45 +00002924 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002925 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002926 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002927 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2928 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002929 compile_node(comp, pns->nodes[0]);
2930 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002931 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002932 EMIT(yield_from);
2933 } else {
2934 compile_node(comp, pns->nodes[0]);
2935 EMIT(yield_value);
2936 }
2937}
2938
Damien George65ef6b72015-01-14 21:17:27 +00002939STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
2940 // only create and load the actual str object on the last pass
2941 if (comp->pass != MP_PASS_EMIT) {
2942 EMIT_ARG(load_const_obj, mp_const_none);
2943 } else {
2944 EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false));
2945 }
2946}
2947
2948STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
2949 // only create and load the actual bytes object on the last pass
2950 if (comp->pass != MP_PASS_EMIT) {
2951 EMIT_ARG(load_const_obj, mp_const_none);
2952 } else {
2953 EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], (mp_uint_t)pns->nodes[1]));
2954 }
2955}
2956
Damiend99b0522013-12-21 18:17:45 +00002957typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002958STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002959#define nc NULL
2960#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002961#define DEF_RULE(rule, comp, kind, ...) comp,
Damien George51dfcb42015-01-01 20:27:54 +00002962#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +01002963#undef nc
2964#undef c
2965#undef DEF_RULE
Damien George65ef6b72015-01-14 21:17:27 +00002966 NULL,
2967 compile_string,
2968 compile_bytes,
Damien429d7192013-10-04 19:53:11 +01002969};
2970
Damien George6be0b0a2014-08-15 14:30:52 +01002971STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +00002972 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002973 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002974 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002975 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002976 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002977 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George40f3c022014-07-03 13:25:24 +01002978 mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002979 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002980 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002981 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2982 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2983 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2984 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damien Georged2d64f02015-01-14 21:32:42 +00002985 case MP_PARSE_NODE_TOKEN: default:
Damiend99b0522013-12-21 18:17:45 +00002986 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002987 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002988 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002989 // do nothing
2990 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002991 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002992 }
2993 break;
Damien429d7192013-10-04 19:53:11 +01002994 }
2995 } else {
Damiend99b0522013-12-21 18:17:45 +00002996 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002997 EMIT_ARG(set_line_number, pns->source_line);
Damien George65ef6b72015-01-14 21:17:27 +00002998 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
2999 if (f == NULL) {
Damien George5042bce2014-05-25 22:06:06 +01003000#if MICROPY_DEBUG_PRINTERS
Damien George65ef6b72015-01-14 21:17:27 +00003001 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
3002 mp_parse_node_print(pn, 0);
Damien George5042bce2014-05-25 22:06:06 +01003003#endif
Damien George65ef6b72015-01-14 21:17:27 +00003004 compile_syntax_error(comp, pn, "internal compiler error");
3005 } else {
3006 f(comp, pns);
Damien429d7192013-10-04 19:53:11 +01003007 }
3008 }
3009}
3010
Damien George2ac4af62014-08-15 16:45:41 +01003011STATIC 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 +01003012 // TODO verify that *k and **k are last etc
Damien George2827d622014-04-27 15:50:52 +01003013 qstr param_name = MP_QSTR_NULL;
3014 uint param_flag = ID_FLAG_IS_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003015 if (MP_PARSE_NODE_IS_ID(pn)) {
3016 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien George8b19db02014-04-11 23:25:34 +01003017 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003018 // comes after a star, so counts as a keyword-only parameter
3019 comp->scope_cur->num_kwonly_args += 1;
Damien429d7192013-10-04 19:53:11 +01003020 } else {
Damien George2827d622014-04-27 15:50:52 +01003021 // comes before a star, so counts as a positional parameter
3022 comp->scope_cur->num_pos_args += 1;
Damien429d7192013-10-04 19:53:11 +01003023 }
Damienb14de212013-10-06 00:28:28 +01003024 } else {
Damiend99b0522013-12-21 18:17:45 +00003025 assert(MP_PARSE_NODE_IS_STRUCT(pn));
3026 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3027 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
3028 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George8b19db02014-04-11 23:25:34 +01003029 if (comp->have_star) {
Damien George2827d622014-04-27 15:50:52 +01003030 // comes after a star, so counts as a keyword-only parameter
3031 comp->scope_cur->num_kwonly_args += 1;
Damienb14de212013-10-06 00:28:28 +01003032 } else {
Damien George2827d622014-04-27 15:50:52 +01003033 // comes before a star, so counts as a positional parameter
3034 comp->scope_cur->num_pos_args += 1;
Damienb14de212013-10-06 00:28:28 +01003035 }
Damiend99b0522013-12-21 18:17:45 +00003036 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
Damien George8b19db02014-04-11 23:25:34 +01003037 comp->have_star = true;
Damien George2827d622014-04-27 15:50:52 +01003038 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
Damiend99b0522013-12-21 18:17:45 +00003039 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003040 // bare star
3041 // TODO see http://www.python.org/dev/peps/pep-3102/
Damienb14de212013-10-06 00:28:28 +01003042 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00003043 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01003044 // named star
Damien George8725f8f2014-02-15 19:33:11 +00003045 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003046 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2ac4af62014-08-15 16:45:41 +01003047 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damien George8b19db02014-04-11 23:25:34 +01003048 // named star with possible annotation
Damien George8725f8f2014-02-15 19:33:11 +00003049 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00003050 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3051 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01003052 } else {
3053 // shouldn't happen
3054 assert(0);
3055 }
Damiend99b0522013-12-21 18:17:45 +00003056 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3057 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien George2827d622014-04-27 15:50:52 +01003058 param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
Damien George8725f8f2014-02-15 19:33:11 +00003059 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01003060 } else {
Damienb14de212013-10-06 00:28:28 +01003061 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01003062 assert(0);
3063 }
Damien429d7192013-10-04 19:53:11 +01003064 }
3065
Damien George2827d622014-04-27 15:50:52 +01003066 if (param_name != MP_QSTR_NULL) {
Damien429d7192013-10-04 19:53:11 +01003067 bool added;
3068 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
3069 if (!added) {
Damien George9d181f62014-04-27 16:55:27 +01003070 compile_syntax_error(comp, pn, "name reused for argument");
Damien429d7192013-10-04 19:53:11 +01003071 return;
3072 }
Damien429d7192013-10-04 19:53:11 +01003073 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003074 id_info->flags = param_flag;
Damien429d7192013-10-04 19:53:11 +01003075 }
3076}
3077
Damien George2827d622014-04-27 15:50:52 +01003078STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003079 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star);
Damien429d7192013-10-04 19:53:11 +01003080}
3081
Damien George2827d622014-04-27 15:50:52 +01003082STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien George2ac4af62014-08-15 16:45:41 +01003083 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
3084}
3085
3086STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
3087 if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
3088 // no annotation
3089 return;
3090 }
3091
3092 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3093 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
3094 // named parameter with possible annotation
3095 // fallthrough
3096 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
3097 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3098 // named star with possible annotation
3099 pns = (mp_parse_node_struct_t*)pns->nodes[0];
3100 // fallthrough
3101 } else {
3102 // no annotation
3103 return;
3104 }
3105 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) {
3106 // double star with possible annotation
3107 // fallthrough
3108 } else {
3109 // no annotation
3110 return;
3111 }
3112
3113 mp_parse_node_t pn_annotation = pns->nodes[1];
3114
3115 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3116 #if MICROPY_EMIT_NATIVE
3117 qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3118 id_info_t *id_info = scope_find(comp->scope_cur, param_name);
3119 assert(id_info != NULL);
3120
3121 if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
3122 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3123 qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3124 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
3125 } else {
Damien Georgea5190a72014-08-15 22:39:08 +01003126 compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
Damien George2ac4af62014-08-15 16:45:41 +01003127 }
3128 }
3129 #endif // MICROPY_EMIT_NATIVE
3130 }
Damien429d7192013-10-04 19:53:11 +01003131}
3132
Damien George6be0b0a2014-08-15 14:30:52 +01003133STATIC 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 +01003134 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00003135 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01003136 // no more nested if/for; compile inner expression
3137 compile_node(comp, pn_inner_expr);
3138 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003139 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01003140 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003141 EMIT_ARG(map_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003142 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003143 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003144 EMIT_ARG(set_add, for_depth + 2);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003145 #endif
Damien429d7192013-10-04 19:53:11 +01003146 } else {
3147 EMIT(yield_value);
3148 EMIT(pop_top);
3149 }
Damiend99b0522013-12-21 18:17:45 +00003150 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01003151 // if condition
Damiend99b0522013-12-21 18:17:45 +00003152 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003153 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
3154 pn_iter = pns_comp_if->nodes[1];
3155 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00003156 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01003157 // for loop
Damiend99b0522013-12-21 18:17:45 +00003158 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01003159 compile_node(comp, pns_comp_for2->nodes[1]);
Damien George6f355fd2014-04-10 14:11:31 +01003160 uint l_end2 = comp_next_label(comp);
3161 uint l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01003162 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00003163 EMIT_ARG(label_assign, l_top2);
3164 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01003165 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
3166 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 +00003167 EMIT_ARG(jump, l_top2);
3168 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01003169 EMIT(for_iter_end);
3170 } else {
3171 // shouldn't happen
3172 assert(0);
3173 }
3174}
3175
Damien George1463c1f2014-04-25 23:52:57 +01003176STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
3177#if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
Damien429d7192013-10-04 19:53:11 +01003178 // see http://www.python.org/dev/peps/pep-0257/
3179
3180 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00003181 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00003182 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00003183 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00003184 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00003185 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
3186 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00003187 for (int i = 0; i < num_nodes; i++) {
3188 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00003189 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 +00003190 // not a newline, so this is the first statement; finish search
3191 break;
3192 }
3193 }
3194 // 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 +00003195 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00003196 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00003197 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01003198 } else {
3199 return;
3200 }
3201
3202 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00003203 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
3204 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +01003205 if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
3206 && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
3207 || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
3208 // compile the doc string
3209 compile_node(comp, pns->nodes[0]);
3210 // store the doc string
Damien Georgeb9791222014-01-23 00:34:21 +00003211 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01003212 }
3213 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003214#else
3215 (void)comp;
3216 (void)pn;
Damien George1463c1f2014-04-25 23:52:57 +01003217#endif
Damien429d7192013-10-04 19:53:11 +01003218}
3219
Damien George1463c1f2014-04-25 23:52:57 +01003220STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien429d7192013-10-04 19:53:11 +01003221 comp->pass = pass;
3222 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01003223 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00003224 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01003225
Damien George36db6bc2014-05-07 17:24:22 +01003226 if (comp->pass == MP_PASS_SCOPE) {
Damien George8dcc0c72014-03-27 10:55:21 +00003227 // reset maximum stack sizes in scope
3228 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01003229 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00003230 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01003231 }
3232
Damien5ac1b2e2013-10-18 19:58:12 +01003233#if MICROPY_EMIT_CPYTHON
Damien George36db6bc2014-05-07 17:24:22 +01003234 if (comp->pass == MP_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +01003235 scope_print_info(scope);
3236 }
Damien5ac1b2e2013-10-18 19:58:12 +01003237#endif
Damien429d7192013-10-04 19:53:11 +01003238
3239 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00003240 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
3241 assert(scope->kind == SCOPE_MODULE);
3242 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3243 compile_node(comp, pns->nodes[0]); // compile the expression
3244 EMIT(return_value);
3245 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01003246 if (!comp->is_repl) {
3247 check_for_doc_string(comp, scope->pn);
3248 }
Damien429d7192013-10-04 19:53:11 +01003249 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00003250 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003251 EMIT(return_value);
3252 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00003253 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3254 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3255 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01003256
3257 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003258 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003259 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003260 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003261 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
Damien George2ac4af62014-08-15 16:45:41 +01003262 } else {
3263 // compile annotations; only needed on latter compiler passes
Damien429d7192013-10-04 19:53:11 +01003264
Damien George2ac4af62014-08-15 16:45:41 +01003265 // argument annotations
3266 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
3267
3268 // pns->nodes[2] is return/whole function annotation
Damien Georgea5190a72014-08-15 22:39:08 +01003269 mp_parse_node_t pn_annotation = pns->nodes[2];
3270 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
3271 #if MICROPY_EMIT_NATIVE
3272 if (scope->emit_options == MP_EMIT_OPT_VIPER) {
3273 // nodes[2] can be null or a test-expr
3274 if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
3275 qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
3276 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
3277 } else {
3278 compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
3279 }
Damien George2ac4af62014-08-15 16:45:41 +01003280 }
Damien Georgea5190a72014-08-15 22:39:08 +01003281 #endif // MICROPY_EMIT_NATIVE
Damien George2ac4af62014-08-15 16:45:41 +01003282 }
Damien George2ac4af62014-08-15 16:45:41 +01003283 }
Damien429d7192013-10-04 19:53:11 +01003284
3285 compile_node(comp, pns->nodes[3]); // 3 is function body
3286 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01003287 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00003288 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003289 EMIT(return_value);
3290 }
3291 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00003292 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3293 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3294 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01003295
3296 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01003297 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien George36db6bc2014-05-07 17:24:22 +01003298 if (comp->pass == MP_PASS_SCOPE) {
Damien George8b19db02014-04-11 23:25:34 +01003299 comp->have_star = false;
Damien429d7192013-10-04 19:53:11 +01003300 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
3301 }
3302
3303 compile_node(comp, pns->nodes[1]); // 1 is lambda body
Damien George0e3329a2014-04-11 13:10:21 +00003304
3305 // if the lambda is a generator, then we return None, not the result of the expression of the lambda
3306 if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
3307 EMIT(pop_top);
3308 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3309 }
Damien429d7192013-10-04 19:53:11 +01003310 EMIT(return_value);
3311 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
3312 // a bit of a hack at the moment
3313
Damiend99b0522013-12-21 18:17:45 +00003314 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3315 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3316 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
3317 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
3318 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01003319
Damien George708c0732014-04-27 19:23:46 +01003320 // We need a unique name for the comprehension argument (the iterator).
3321 // CPython uses .0, but we should be able to use anything that won't
3322 // clash with a user defined variable. Best to use an existing qstr,
3323 // so we use the blank qstr.
3324#if MICROPY_EMIT_CPYTHON
Damien George55baff42014-01-21 21:40:13 +00003325 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien George708c0732014-04-27 19:23:46 +01003326#else
3327 qstr qstr_arg = MP_QSTR_;
3328#endif
Damien George36db6bc2014-05-07 17:24:22 +01003329 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003330 bool added;
3331 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
3332 assert(added);
3333 id_info->kind = ID_INFO_KIND_LOCAL;
Damien George2827d622014-04-27 15:50:52 +01003334 scope->num_pos_args = 1;
Damien429d7192013-10-04 19:53:11 +01003335 }
3336
3337 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003338 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01003339 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003340 EMIT_ARG(build_map, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003341 #if MICROPY_PY_BUILTINS_SET
Damien429d7192013-10-04 19:53:11 +01003342 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00003343 EMIT_ARG(build_set, 0);
Damien Georgee37dcaa2014-12-27 17:07:16 +00003344 #endif
Damien429d7192013-10-04 19:53:11 +01003345 }
3346
Damien George6f355fd2014-04-10 14:11:31 +01003347 uint l_end = comp_next_label(comp);
3348 uint l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00003349 EMIT_ARG(load_id, qstr_arg);
3350 EMIT_ARG(label_assign, l_top);
3351 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01003352 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
3353 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00003354 EMIT_ARG(jump, l_top);
3355 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01003356 EMIT(for_iter_end);
3357
3358 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00003359 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003360 }
3361 EMIT(return_value);
3362 } else {
3363 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00003364 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3365 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3366 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01003367
Damien George36db6bc2014-05-07 17:24:22 +01003368 if (comp->pass == MP_PASS_SCOPE) {
Damien429d7192013-10-04 19:53:11 +01003369 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003370 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01003371 assert(added);
3372 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01003373 }
3374
Damien Georgeb9791222014-01-23 00:34:21 +00003375 EMIT_ARG(load_id, MP_QSTR___name__);
3376 EMIT_ARG(store_id, MP_QSTR___module__);
Damien George968bf342014-04-27 19:12:05 +01003377 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 +00003378 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01003379
3380 check_for_doc_string(comp, pns->nodes[2]);
3381 compile_node(comp, pns->nodes[2]); // 2 is class body
3382
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003383 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01003384 assert(id != NULL);
3385 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00003386 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01003387 } else {
Damien George6baf76e2013-12-30 22:32:17 +00003388#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00003389 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00003390#else
Damien George0abb5602015-01-16 12:24:49 +00003391 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00003392#endif
Damien429d7192013-10-04 19:53:11 +01003393 }
3394 EMIT(return_value);
3395 }
3396
Damien415eb6f2013-10-05 12:19:06 +01003397 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00003398
3399 // make sure we match all the exception levels
3400 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01003401}
3402
Damien George094d4502014-04-02 17:31:27 +01003403#if MICROPY_EMIT_INLINE_THUMB
Damien George36db6bc2014-05-07 17:24:22 +01003404// requires 3 passes: SCOPE, CODE_SIZE, EMIT
Damien George1463c1f2014-04-25 23:52:57 +01003405STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
Damien826005c2013-10-05 23:17:28 +01003406 comp->pass = pass;
3407 comp->scope_cur = scope;
3408 comp->next_label = 1;
3409
3410 if (scope->kind != SCOPE_FUNCTION) {
Damien George01039b52014-12-21 17:44:27 +00003411 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
Damien826005c2013-10-05 23:17:28 +01003412 return;
3413 }
3414
Damien George36db6bc2014-05-07 17:24:22 +01003415 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003416 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003417 }
3418
Damien826005c2013-10-05 23:17:28 +01003419 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003420 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3421 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3422 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003423
Damiend99b0522013-12-21 18:17:45 +00003424 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003425
Damiena2f2f7d2013-10-06 00:14:13 +01003426 // parameters are in pns->nodes[1]
Damien George36db6bc2014-05-07 17:24:22 +01003427 if (comp->pass == MP_PASS_CODE_SIZE) {
Damiend99b0522013-12-21 18:17:45 +00003428 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003429 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien George2827d622014-04-27 15:50:52 +01003430 scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003431 }
3432
Damiend99b0522013-12-21 18:17:45 +00003433 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003434
Damiend99b0522013-12-21 18:17:45 +00003435 mp_parse_node_t pn_body = pns->nodes[3]; // body
3436 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003437 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3438
Damien Georgecbd2f742014-01-19 11:48:48 +00003439 /*
Damien George36db6bc2014-05-07 17:24:22 +01003440 if (comp->pass == MP_PASS_EMIT) {
Damien826005c2013-10-05 23:17:28 +01003441 //printf("----\n");
3442 scope_print_info(scope);
3443 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003444 */
Damien826005c2013-10-05 23:17:28 +01003445
3446 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003447 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3448 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
Damien Georgea26dc502014-04-12 17:54:52 +01003449 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
3450 // no instructions
3451 continue;
3452 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
3453 // an instruction; fall through
3454 } else {
3455 // not an instruction; error
3456 compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
3457 return;
3458 }
Damiend99b0522013-12-21 18:17:45 +00003459 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3460 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3461 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3462 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3463 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3464 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3465 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3466 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3467 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3468 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003469 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3470
3471 // emit instructions
Damien Georgee5f8a772014-04-21 13:33:15 +01003472 if (op == MP_QSTR_label) {
Damiend99b0522013-12-21 18:17:45 +00003473 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgeb7ffdcc2014-04-08 16:41:02 +01003474 compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003475 return;
3476 }
Damien George6f355fd2014-04-10 14:11:31 +01003477 uint lab = comp_next_label(comp);
Damien George36db6bc2014-05-07 17:24:22 +01003478 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003479 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003480 }
Damien Georgee5f8a772014-04-21 13:33:15 +01003481 } else if (op == MP_QSTR_align) {
3482 if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3483 compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
3484 return;
3485 }
Damien George36db6bc2014-05-07 17:24:22 +01003486 if (pass > MP_PASS_SCOPE) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003487 EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
3488 }
3489 } else if (op == MP_QSTR_data) {
3490 if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
3491 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
3492 return;
3493 }
Damien George36db6bc2014-05-07 17:24:22 +01003494 if (pass > MP_PASS_SCOPE) {
Damien George40f3c022014-07-03 13:25:24 +01003495 mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
Damien George50912e72015-01-20 11:55:10 +00003496 for (uint j = 1; j < n_args; j++) {
3497 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) {
Damien Georgee5f8a772014-04-21 13:33:15 +01003498 compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
3499 return;
3500 }
Damien George50912e72015-01-20 11:55:10 +00003501 EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j]));
Damien Georgee5f8a772014-04-21 13:33:15 +01003502 }
3503 }
Damien826005c2013-10-05 23:17:28 +01003504 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003505 if (pass > MP_PASS_SCOPE) {
Damien Georgeb9791222014-01-23 00:34:21 +00003506 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003507 }
3508 }
3509 }
3510
Damien George36db6bc2014-05-07 17:24:22 +01003511 if (comp->pass > MP_PASS_SCOPE) {
Damien Georgea26dc502014-04-12 17:54:52 +01003512 bool success = EMIT_INLINE_ASM(end_pass);
3513 if (!success) {
Damien Georgea91ac202014-10-05 19:01:34 +01003514 // TODO get proper exception from inline assembler
3515 compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler error");
Damien Georgea26dc502014-04-12 17:54:52 +01003516 }
Damienb05d7072013-10-05 13:37:10 +01003517 }
Damien429d7192013-10-04 19:53:11 +01003518}
Damien George094d4502014-04-02 17:31:27 +01003519#endif
Damien429d7192013-10-04 19:53:11 +01003520
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003521STATIC void scope_compute_things(scope_t *scope) {
Damien George2827d622014-04-27 15:50:52 +01003522#if !MICROPY_EMIT_CPYTHON
3523 // in Micro Python we put the *x parameter after all other parameters (except **y)
3524 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
3525 id_info_t *id_param = NULL;
3526 for (int i = scope->id_info_len - 1; i >= 0; i--) {
3527 id_info_t *id = &scope->id_info[i];
3528 if (id->flags & ID_FLAG_IS_STAR_PARAM) {
3529 if (id_param != NULL) {
3530 // swap star param with last param
3531 id_info_t temp = *id_param; *id_param = *id; *id = temp;
3532 }
3533 break;
3534 } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
3535 id_param = id;
3536 }
3537 }
3538 }
3539#endif
3540
Damien429d7192013-10-04 19:53:11 +01003541 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003542 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003543 scope->num_locals = 0;
3544 for (int i = 0; i < scope->id_info_len; i++) {
3545 id_info_t *id = &scope->id_info[i];
Damien George7ff996c2014-09-08 23:05:16 +01003546 if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003547 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3548 continue;
3549 }
3550 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3551 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3552 }
Damien George2827d622014-04-27 15:50:52 +01003553 // params always count for 1 local, even if they are a cell
Damien George11d8cd52014-04-09 14:42:51 +01003554 if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George2827d622014-04-27 15:50:52 +01003555 id->local_num = scope->num_locals++;
Damien9ecbcff2013-12-11 00:41:43 +00003556 }
3557 }
3558
3559 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003560#if MICROPY_EMIT_CPYTHON
3561 int num_cell = 0;
3562#endif
Damien9ecbcff2013-12-11 00:41:43 +00003563 for (int i = 0; i < scope->id_info_len; i++) {
3564 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003565#if MICROPY_EMIT_CPYTHON
3566 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003567 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003568 id->local_num = num_cell;
3569 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003570 }
Damien George6baf76e2013-12-30 22:32:17 +00003571#else
3572 // in Micro Python the cells come right after the fast locals
3573 // parameters are not counted here, since they remain at the start
3574 // of the locals, even if they are cell vars
Damien George11d8cd52014-04-09 14:42:51 +01003575 if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003576 id->local_num = scope->num_locals;
3577 scope->num_locals += 1;
3578 }
3579#endif
Damien9ecbcff2013-12-11 00:41:43 +00003580 }
Damien9ecbcff2013-12-11 00:41:43 +00003581
3582 // compute the index of free vars (freevars[idx] in CPython)
3583 // make sure they are in the order of the parent scope
3584 if (scope->parent != NULL) {
3585 int num_free = 0;
3586 for (int i = 0; i < scope->parent->id_info_len; i++) {
3587 id_info_t *id = &scope->parent->id_info[i];
3588 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3589 for (int j = 0; j < scope->id_info_len; j++) {
3590 id_info_t *id2 = &scope->id_info[j];
Damien George7ff996c2014-09-08 23:05:16 +01003591 if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) {
Damien George11d8cd52014-04-09 14:42:51 +01003592 assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
Damien George6baf76e2013-12-30 22:32:17 +00003593#if MICROPY_EMIT_CPYTHON
3594 // in CPython the frees are numbered after the cells
3595 id2->local_num = num_cell + num_free;
3596#else
3597 // in Micro Python the frees come first, before the params
3598 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003599#endif
3600 num_free += 1;
3601 }
3602 }
3603 }
Damien429d7192013-10-04 19:53:11 +01003604 }
Damien George6baf76e2013-12-30 22:32:17 +00003605#if !MICROPY_EMIT_CPYTHON
3606 // in Micro Python shift all other locals after the free locals
3607 if (num_free > 0) {
3608 for (int i = 0; i < scope->id_info_len; i++) {
3609 id_info_t *id = &scope->id_info[i];
Damien George2bf7c092014-04-09 15:26:46 +01003610 if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
Damien George6baf76e2013-12-30 22:32:17 +00003611 id->local_num += num_free;
3612 }
3613 }
Damien George2827d622014-04-27 15:50:52 +01003614 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 +00003615 scope->num_locals += num_free;
3616 }
3617#endif
Damien429d7192013-10-04 19:53:11 +01003618 }
3619
Damien George8725f8f2014-02-15 19:33:11 +00003620 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003621
3622#if MICROPY_EMIT_CPYTHON
3623 // these flags computed here are for CPython compatibility only
Damien429d7192013-10-04 19:53:11 +01003624 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) {
3625 assert(scope->parent != NULL);
Damien George0e3329a2014-04-11 13:10:21 +00003626 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien George8725f8f2014-02-15 19:33:11 +00003627 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien George08d07552014-01-29 18:58:52 +00003628 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 +00003629 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003630 }
3631 }
Damien George882b3632014-04-02 15:56:31 +01003632#endif
3633
Damien429d7192013-10-04 19:53:11 +01003634 int num_free = 0;
3635 for (int i = 0; i < scope->id_info_len; i++) {
3636 id_info_t *id = &scope->id_info[i];
3637 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3638 num_free += 1;
3639 }
3640 }
3641 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003642 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003643 }
3644}
3645
Damien George65cad122014-04-06 11:48:15 +01003646mp_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 +01003647 compiler_t *comp = m_new0(compiler_t, 1);
Damien Georgecbd2f742014-01-19 11:48:48 +00003648 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003649 comp->is_repl = is_repl;
Damien Georgea91ac202014-10-05 19:01:34 +01003650 comp->compile_error = MP_OBJ_NULL;
Damien429d7192013-10-04 19:53:11 +01003651
Damien826005c2013-10-05 23:17:28 +01003652 // optimise constants
Damien Georgeffae48d2014-05-08 15:58:39 +00003653 mp_map_t consts;
3654 mp_map_init(&consts, 0);
3655 pn = fold_constants(comp, pn, &consts);
3656 mp_map_deinit(&consts);
Damien826005c2013-10-05 23:17:28 +01003657
3658 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003659 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003660
Damien826005c2013-10-05 23:17:28 +01003661 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003662 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003663 comp->emit_method_table = &emit_pass1_method_table;
3664 comp->emit_inline_asm = NULL;
3665 comp->emit_inline_asm_method_table = NULL;
3666 uint max_num_labels = 0;
Damien Georgea91ac202014-10-05 19:01:34 +01003667 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003668 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003669#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003670 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien George36db6bc2014-05-07 17:24:22 +01003671 compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
Damienc025ebb2013-10-12 14:30:21 +01003672#endif
Damien826005c2013-10-05 23:17:28 +01003673 } else {
Damien George36db6bc2014-05-07 17:24:22 +01003674 compile_scope(comp, s, MP_PASS_SCOPE);
Damien826005c2013-10-05 23:17:28 +01003675 }
3676
3677 // update maximim number of labels needed
3678 if (comp->next_label > max_num_labels) {
3679 max_num_labels = comp->next_label;
3680 }
Damien429d7192013-10-04 19:53:11 +01003681 }
3682
Damien826005c2013-10-05 23:17:28 +01003683 // compute some things related to scope and identifiers
Damien Georgea91ac202014-10-05 19:01:34 +01003684 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00003685 scope_compute_things(s);
Damien429d7192013-10-04 19:53:11 +01003686 }
3687
Damien826005c2013-10-05 23:17:28 +01003688 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003689 emit_pass1_free(comp->emit);
3690
Damien826005c2013-10-05 23:17:28 +01003691 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003692#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003693 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003694#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003695 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003696#endif
Damien3ef4abb2013-10-12 16:53:13 +01003697#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003698 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003699#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003700#endif // !MICROPY_EMIT_CPYTHON
Damien Georgea91ac202014-10-05 19:01:34 +01003701 for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003702 if (false) {
3703 // dummy
3704
Damien3ef4abb2013-10-12 16:53:13 +01003705#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003706 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003707 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003708 if (emit_inline_thumb == NULL) {
3709 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3710 }
3711 comp->emit = NULL;
3712 comp->emit_method_table = NULL;
3713 comp->emit_inline_asm = emit_inline_thumb;
3714 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
Damien George36db6bc2014-05-07 17:24:22 +01003715 compile_scope_inline_asm(comp, s, MP_PASS_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_inline_asm(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003718 }
Damienc025ebb2013-10-12 14:30:21 +01003719#endif
3720
Damien826005c2013-10-05 23:17:28 +01003721 } else {
Damienc025ebb2013-10-12 14:30:21 +01003722
3723 // choose the emit type
3724
Damien3ef4abb2013-10-12 16:53:13 +01003725#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003726 comp->emit = emit_cpython_new(max_num_labels);
3727 comp->emit_method_table = &emit_cpython_method_table;
3728#else
Damien826005c2013-10-05 23:17:28 +01003729 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003730
3731#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003732 case MP_EMIT_OPT_NATIVE_PYTHON:
3733 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003734#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003735 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003736 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003737 }
Damien13ed3a62013-10-08 09:05:10 +01003738 comp->emit_method_table = &emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +01003739#elif MICROPY_EMIT_X86
3740 if (emit_native == NULL) {
3741 emit_native = emit_native_x86_new(max_num_labels);
3742 }
3743 comp->emit_method_table = &emit_native_x86_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003744#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003745 if (emit_native == NULL) {
3746 emit_native = emit_native_thumb_new(max_num_labels);
3747 }
3748 comp->emit_method_table = &emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003749#elif MICROPY_EMIT_ARM
3750 if (emit_native == NULL) {
3751 emit_native = emit_native_arm_new(max_num_labels);
3752 }
3753 comp->emit_method_table = &emit_native_arm_method_table;
Damienc025ebb2013-10-12 14:30:21 +01003754#endif
3755 comp->emit = emit_native;
Damien Georgea5190a72014-08-15 22:39:08 +01003756 EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0);
Damien7af3d192013-10-07 00:02:49 +01003757 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003758#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003759
Damien826005c2013-10-05 23:17:28 +01003760 default:
3761 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003762 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003763 }
3764 comp->emit = emit_bc;
3765 comp->emit_method_table = &emit_bc_method_table;
3766 break;
3767 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003768#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003769
Damien George1e1779e2015-01-14 00:20:28 +00003770 // need a pass to compute stack size
3771 compile_scope(comp, s, MP_PASS_STACK_SIZE);
3772
Damien George36db6bc2014-05-07 17:24:22 +01003773 // second last pass: compute code size
Damien Georgea91ac202014-10-05 19:01:34 +01003774 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003775 compile_scope(comp, s, MP_PASS_CODE_SIZE);
3776 }
3777
3778 // final pass: emit code
Damien Georgea91ac202014-10-05 19:01:34 +01003779 if (comp->compile_error == MP_OBJ_NULL) {
Damien George36db6bc2014-05-07 17:24:22 +01003780 compile_scope(comp, s, MP_PASS_EMIT);
Damien Georgea26dc502014-04-12 17:54:52 +01003781 }
Damien6cdd3af2013-10-05 18:08:26 +01003782 }
Damien429d7192013-10-04 19:53:11 +01003783 }
3784
Damien George41d02b62014-01-24 22:42:28 +00003785 // free the emitters
3786#if !MICROPY_EMIT_CPYTHON
3787 if (emit_bc != NULL) {
3788 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003789 }
Damien George41d02b62014-01-24 22:42:28 +00003790#if MICROPY_EMIT_NATIVE
3791 if (emit_native != NULL) {
3792#if MICROPY_EMIT_X64
3793 emit_native_x64_free(emit_native);
Damien Georgec90f59e2014-09-06 23:06:36 +01003794#elif MICROPY_EMIT_X86
3795 emit_native_x86_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003796#elif MICROPY_EMIT_THUMB
3797 emit_native_thumb_free(emit_native);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02003798#elif MICROPY_EMIT_ARM
Damien Georgedda46462014-09-03 22:47:23 +01003799 emit_native_arm_free(emit_native);
Damien George41d02b62014-01-24 22:42:28 +00003800#endif
3801 }
3802#endif
3803#if MICROPY_EMIT_INLINE_THUMB
3804 if (emit_inline_thumb != NULL) {
3805 emit_inline_thumb_free(emit_inline_thumb);
3806 }
3807#endif
3808#endif // !MICROPY_EMIT_CPYTHON
3809
Damien George52b5d762014-09-23 15:31:56 +00003810 // free the parse tree
3811 mp_parse_node_free(pn);
3812
Damien George41d02b62014-01-24 22:42:28 +00003813 // free the scopes
Damien Georgedf8127a2014-04-13 11:04:33 +01003814 mp_raw_code_t *outer_raw_code = module_scope->raw_code;
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003815 for (scope_t *s = module_scope; s;) {
3816 scope_t *next = s->next;
3817 scope_free(s);
3818 s = next;
3819 }
Damien5ac1b2e2013-10-18 19:58:12 +01003820
Damien George41d02b62014-01-24 22:42:28 +00003821 // free the compiler
Damien Georgea91ac202014-10-05 19:01:34 +01003822 mp_obj_t compile_error = comp->compile_error;
Damien George41d02b62014-01-24 22:42:28 +00003823 m_del_obj(compiler_t, comp);
3824
Damien Georgea91ac202014-10-05 19:01:34 +01003825 if (compile_error != MP_OBJ_NULL) {
Damien George0bfc7632015-02-07 18:33:58 +00003826 nlr_raise(compile_error);
Damien George1fb03172014-01-03 14:22:03 +00003827 } else {
3828#if MICROPY_EMIT_CPYTHON
3829 // can't create code, so just return true
Damien Georgedf8127a2014-04-13 11:04:33 +01003830 (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
Damien George1fb03172014-01-03 14:22:03 +00003831 return mp_const_true;
3832#else
3833 // return function that executes the outer module
Damien Georgedf8127a2014-04-13 11:04:33 +01003834 return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003835#endif
3836 }
Damien429d7192013-10-04 19:53:11 +01003837}